def forward(self, buf: array.Array, verbose: bool = False): # put x in the buffer size = self.layers[0].input_width # can probably do better here # this only works on pocl because they didn't implement CL_MISALIGNED_SUB_BUFFER_OFFSET # buf = x.get_sub_region(size * idx, size) input_np = buf.get() for idx, l in enumerate(self.layers): l.inputs = input_np.copy() if verbose: print(f"Layer {idx}") print( f"Input Batch: rows={l.batch_size} samples cols={l.input_width} features \n", input_np) buf = l(buf) output = buf.get() if verbose: weights = l.get_weights() bias = l.get_bias() print( f"\nWeights: (rows={l.units} units, cols={l.input_width} inputs)\n", weights) # print("Biases:\n", bias) print( f"\nOutput: (rows={l.batch_size} batch samples cols={l.units} units)\n", output) expected = (np.dot(weights, input_np.T) + bias).T if l.activation == 'relu': expected = np.clip(expected, 0, a_max=None) elif l.activation == 'sigmoid': expected = 1 / (np.exp(-expected) + 1) elif l.activation == 'softmax': exps = np.exp(expected) expected = exps / exps.sum(axis=1)[:, None] print("Expected:\n", expected) input_np = output # output is the output of the last layer return buf
import numpy as np import pyopencl as cl from pyopencl.array import Array import pyclblast # Settings for this sample dtype = 'float32' print("# Setting up OpenCL") ctx = cl.create_some_context() queue = cl.CommandQueue(ctx) print("# Setting up Numpy arrays") m, n, k = 2, 3, 4 a = np.random.rand(m, k).astype(dtype=dtype) b = np.random.rand(k, n).astype(dtype=dtype) c = np.random.rand(m, n).astype(dtype=dtype) print("# Setting up OpenCL arrays") cla = Array(queue, a.shape, a.dtype) clb = Array(queue, b.shape, b.dtype) clc = Array(queue, c.shape, c.dtype) cla.set(a) clb.set(b) clc.set(c) print("# Example level-3 operation: GEMM") pyclblast.gemm(queue, m, n, k, cla, clb, clc, a_ld=k, b_ld=n, c_ld=n) print("# Matrix C result: %s" % clc.get()) print("# Expected result: %s" % (np.dot(a, b)))
from datetime import datetime if __name__ == "__main__": # Set up pyopencl: ctx = cl.create_some_context() queue = cl.CommandQueue(ctx) # Set up a basic sgemm example: m, n, k = 2, 3, 4 a = np.random.rand(m, k).astype(dtype=np.float32) b = np.random.rand(k, n).astype(dtype=np.float32) c = np.empty((m, n), np.float32) cla = Array(queue, a.shape, a.dtype) clb = Array(queue, b.shape, b.dtype) clc = Array(queue, c.shape, c.dtype) cla.set(a) clb.set(b) clc.set(c) # Perform sgemm on these matrices, overriding the CLBlast parameters. In this example, we'll # just change the 'MWG' parameter a couple of times: params = { "KWG": 32, "KWI": 2, "MDIMA": 8, "MDIMC": 8, "MWG": 64, "NDIMB": 8, "NDIMC": 8, "NWG": 64, "SA": 0, "SB": 0, "STRM": 0, "STRN": 0, "VWM": 4, "VWN": 1 } for mwg in (32, 64, 256): print("Running sgemm tuned with MWG = %d" % mwg) params["MWG"] = mwg pyclblast.override_parameters(ctx.devices[0], 'Xgemm', 32, params) pyclblast.gemm(queue, m, n, k, cla, clb, clc, a_ld=k, b_ld=n, c_ld=n) assert np.allclose(clc.get(), a.dot(b)), "uh-oh, xgemm isn't behaving correctly"
rng = np.random.RandomState(1) # change the seed to see different data A[...] = rng.uniform(-1, 1, size=A.shape) x[...] = rng.uniform(-1, 1, size=x.shape) y[...] = rng.uniform(-1, 1, size=y.shape) # allocate OpenCL memory on the device clA = Array(queue, A.shape, A.dtype) clx = Array(queue, x.shape, x.dtype) cly = Array(queue, y.shape, y.dtype) # copy data to device clA.set(A) clx.set(x) # compute a matrix-vector product (gemv) blas.gemv(queue, clA, clx, cly) # check the result print("Expected: ", np.dot(A, x)) print("Actual: ", cly.get()) # try a matrix-vector product with the transpose cly.set(y) blas.gemv(queue, clA, cly, clx, transA=True) print("Expected: ", np.dot(A.T, y)) print("Actual: ", clx.get()) # tidy up the BLAS blas.teardown()
import pyclblast # Settings for this sample dtype = 'float32' m, n = 4, 3 alpha = 1.0 beta = 0.0 print("# Setting up OpenCL") ctx = cl.create_some_context() queue = cl.CommandQueue(ctx) print("# Setting up Numpy arrays") a = np.random.rand(m, n).astype(dtype=dtype) x = np.random.rand(n).astype(dtype=dtype) y = np.random.rand(m).astype(dtype=dtype) print("# Setting up OpenCL arrays") cla = Array(queue, a.shape, a.dtype) clx = Array(queue, x.shape, x.dtype) cly = Array(queue, y.shape, y.dtype) cla.set(a) clx.set(x) cly.set(y) print("# Example level-2 operation: GEMV") pyclblast.gemv(queue, m, n, cla, clx, cly, a_ld=n, alpha=alpha, beta=beta) queue.finish() print("# Result for vector y: %s" % cly.get()) print("# Expected result: %s" % (alpha * np.dot(a, x) + beta * y))
# generate some random data on the CPU n = 5 dtype = 'float64' # also supports 'float64' x = np.zeros(n, dtype=dtype) y = np.zeros(n, dtype=dtype) rng = np.random.RandomState(1) # change the seed to see different data x[...] = rng.uniform(-1, 1, size=x.shape) y[...] = rng.uniform(-1, 1, size=y.shape) # allocate OpenCL memory on the device clx = Array(queue, x.shape, x.dtype) cly = Array(queue, y.shape, y.dtype) cld = Array(queue, 1, x.dtype) # copy data to device clx.set(x) cly.set(y) # compute a dot product (dot) blas.dot(queue, clx, cly, cld) # check the result print("Expected: ", np.dot(x, y)) print("Actual: ", cld.get()[0]) # tidy up the BLAS blas.teardown()
n = 4 print("# Setting up OpenCL") ctx = cl.create_some_context() queue = cl.CommandQueue(ctx) print("# Setting up Numpy arrays") x = np.random.rand(n * batch_count).astype(dtype=dtype) y = np.random.rand(n * batch_count).astype(dtype=dtype) print("# Batch offsets: next after each other") x_offsets = [0, n] y_offsets = [0, n] print("# Setting up OpenCL arrays") clx = Array(queue, x.shape, x.dtype) cly = Array(queue, y.shape, y.dtype) clx.set(x) cly.set(y) print("# Example level-1 batched operation: AXPY-batched") assert len(alphas) == len(x_offsets) == len(y_offsets) == batch_count pyclblast.axpyBatched(queue, n, clx, cly, alphas, x_offsets, y_offsets) queue.finish() print("# Full result for vector y: %s" % str(cly.get())) for i in range(batch_count): result = alphas[i] * x[x_offsets[i]:x_offsets[i] + n] + y[y_offsets[i]:y_offsets[i] + n] print("# Expected result batch #%d: %s" % (i, str(result)))
# generate some random data on the CPU n = 5 dtype = 'float64' # also supports 'float64' x = np.zeros(n, dtype=dtype) y = np.zeros(n, dtype=dtype) rng = np.random.RandomState(1) # change the seed to see different data x[...] = rng.uniform(-1, 1, size=x.shape) y[...] = rng.uniform(-1, 1, size=y.shape) # allocate OpenCL memory on the device clx = Array(queue, x.shape, x.dtype) cly = Array(queue, y.shape, y.dtype) cld = Array(queue, 1, x.dtype) # copy data to device clx.set(x) cly.set(y) # compute a dot product (dot) blas.dot(queue, clx, cly, cld) # check the result print("Expected: ", np.dot(x,y)) print("Actual: ", cld.get()[0]) # tidy up the BLAS blas.teardown()
clA_upper = Array(queue, A.shape, A.dtype) clx = Array(queue, x.shape, x.dtype) clx1 = Array(queue, x1.shape, x1.dtype) clx2 = Array(queue, x2.shape, x2.dtype) # copy data to device clA.set(A) clA_upper.set(A_upper) clx.set(x) # compute a triangular solve (trsv) blas.trsv(queue, clA, clx) # check the result print("Expected: ", np.linalg.solve(A, x)) print("Actual: ", clx.get()) print() # try a triangular solve with the transpose clx1.set(x1) blas.trsv(queue, clA, clx1, transA=True) print("Expected: ", np.linalg.solve(A.T, x1)) print("Actual: ", clx1.get()) print() # trye an upper triangular solve clx2.set(x2) blas.trsv(queue, clA_upper, clx2, lower=False) print("Expected: ", np.linalg.solve(A_upper, x2)) print("Actual: ", clx2.get()) print()