def toroidal_shell_integral(n, minval, maxval, eps, benchmark=False): coordvals = np.linspace(minval, maxval, n, dtype=np.float32) delta = (maxval - minval) / (n - 1) # grid spacing X_data = coordvals.reshape(n, 1, 1) Y_data = coordvals.reshape(1, n, 1) Z_data = coordvals.reshape(1, 1, n) X = edsl.Tensor(edsl.LogicalShape(plaidml.DType.FLOAT32, X_data.shape)) Y = edsl.Tensor(edsl.LogicalShape(plaidml.DType.FLOAT32, Y_data.shape)) Z = edsl.Tensor(edsl.LogicalShape(plaidml.DType.FLOAT32, Z_data.shape)) # f-rep of torodial shell f(x, y, z) = (sqrt(x^2 + y^2) - 1)^2 + z^2 + (0.1)^2 F = sq(edsl.sqrt(sq(X) + sq(Y)) - 1.0) + sq(Z) - sq(0.1) # moment of inertia about z axis at each point g(x, y, z) = x^2 + y^2 G = sq(X) + sq(Y) DFDX = partial(F, 'x', delta) DFDY = partial(F, 'y', delta) DFDZ = partial(F, 'z', delta) # chi: occupancy function: 1 inside the region (f<0), 0 outside the region (f>0) DCHIDX = partial_chi(F, 'x', delta) DCHIDY = partial_chi(F, 'y', delta) DCHIDZ = partial_chi(F, 'z', delta) NUMER = DFDX * DCHIDX + DFDY * DCHIDY + DFDZ * DCHIDZ DENOM = edsl.sqrt(sq(DFDX) + sq(DFDY) + sq(DFDZ)) H = edsl.select(DENOM < eps, 0, NUMER / DENOM) O = sum(-G * H) program = edsl.Program('toroidal_shell_integral', [O]) binder = plaidml_exec.Binder(program) executable = binder.compile() def run(): binder.input(X).copy_from_ndarray(X_data) binder.input(Y).copy_from_ndarray(Y_data) binder.input(Z).copy_from_ndarray(Z_data) executable.run() return binder.output(O).as_ndarray() if benchmark: # the first run will compile and run print('compiling...') result = run() # subsequent runs should not include compile time print('running...') ITERATIONS = 100 elapsed = timeit.timeit(run, number=ITERATIONS) print('runtime:', elapsed / ITERATIONS) else: result = run() return result * (delta**3)
def test_matmul_2_1(self): np_A = np.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]) np_x = np.array([1., 2., 3.]) dtype = plaidml2.DType.FLOAT32 A = edsl.Tensor(edsl.LogicalShape(dtype, np_A.shape)) x = edsl.Tensor(edsl.LogicalShape(dtype, np_x.shape)) y = matmul_2_1(A, x) test_result = get_jacobian([A, x], [np_A, np_x], y, x) true_result = np_A npt.assert_allclose(test_result, true_result)
def test_matmul_2_2(self): np_A = np.array([[1., 2.], [3., 4.]]) np_x = np.array([[5., 6.], [7., 8.]]) dtype = plaidml2.DType.FLOAT32 A = edsl.Tensor(edsl.LogicalShape(dtype, np_A.shape)) x = edsl.Tensor(edsl.LogicalShape(dtype, np_x.shape)) y = matmul_2_2(A, x) test_result = get_jacobian([A, x], [np_A, np_x], y, x) true_result = np.array([[[[1, 0], [2, 0]], [[0, 1], [0, 2]]], [[[3, 0], [4, 0]], [[0, 3], [0, 4]]]]) npt.assert_allclose(test_result, true_result)
def _create_var(name, value): dtype = plaidml.DType.from_numpy(value.dtype) shape = edsl.LogicalShape(dtype, value.shape) tensor_shape = plaidml.TensorShape(dtype, value.shape) buffer = plaidml.Buffer(_device, tensor_shape) buffer.copy_from_ndarray(value) return edsl.Tensor(shape=shape, name=name, buffer=buffer)
def test_assign(self): np_x = np.array([1, 2, 3]) np_b = np.array([1, 1, 1]) dtype = plaidml2.DType.FLOAT32 x = edsl.Tensor(edsl.LogicalShape(dtype, np_x.shape)) b = edsl.Tensor(edsl.LogicalShape(dtype, np_b.shape)) y = op.square(dist(x, b)) test_result = get_jacobian([x, b], [np_x, np_b], y, x) true_result = np.zeros((3, 3, 3)) true_result[0, :, 0] = 0 true_result[1, :, 1] = 2 true_result[2, :, 2] = 4 npt.assert_allclose(test_result, true_result)
def test_chain(self): np_x = np.array([1., 2., 3.]) np_A = np.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]) dtype = plaidml2.DType.FLOAT32 A = edsl.Tensor(edsl.LogicalShape(dtype, np_A.shape)) x = edsl.Tensor(edsl.LogicalShape(dtype, np_x.shape)) y = matmul_2_2(A, dist(x, x)) J_test = get_jacobian([A, x], [np_A, np_x], y, x) J_true = np.zeros((3, 3, 3)) J_true[:, :, 0] = [[-5, 1, 1], [-11, 4, 4], [-17, 7, 7]] J_true[:, :, 1] = [[2, -4, 2], [5, -10, 5], [8, -16, 8]] J_true[:, :, 2] = [[3, 3, -3], [6, 6, -9], [9, 9, -15]] npt.assert_allclose(J_true, J_test)
def __init__(self, opname, name=None, shape=None, tensor=None, value=None): self.opname = opname self.name = _prepend_name_scope(name, opname) if value is not None: tensor = _create_var(self.name, value) elif tensor is None: tensor = edsl.Tensor(shape=shape, name=self.name) # logger.debug('_KerasNode({})'.format(tensor)) self.tensor = tensor
def test_ident(self): np_x = np.array([1, 2, 3]) dtype = plaidml2.DType.FLOAT32 x = edsl.Tensor(edsl.LogicalShape(dtype, np_x.shape)) test_result = get_jacobian([x], [np_x], x, x) true_result = np.eye(3) npt.assert_allclose(test_result, true_result)
def test_square(self): np_x = np.array([1, 2, 3]) dtype = plaidml2.DType.FLOAT32 x = edsl.Tensor(edsl.LogicalShape(dtype, np_x.shape)) y = op.square(x) test_result = get_jacobian([x], [np_x], y, x) true_result = np.array([[2, 0, 0], [0, 4, 0], [0, 0, 6]]) npt.assert_allclose(test_result, true_result)
def main(): print(""" PlaidML Setup ({0}) Thanks for using PlaidML! Some Notes: * Bugs and other issues: https://github.com/plaidml/plaidml * Questions: https://stackoverflow.com/questions/tagged/plaidml * Say hello: https://groups.google.com/forum/#!forum/plaidml-dev * PlaidML is licensed under the Apache License 2.0 """.format(plaidml.__version__)) devices = sorted(plaidml_exec.list_devices()) targets = sorted(plaidml_exec.list_targets()) if not devices: print(""" No OpenCL devices found. Check driver installation. Read the helpful, easy driver installation instructions from our README: http://github.com/plaidml/plaidml """) sys.exit(-1) dev_idx = 0 if len(devices) > 1: print(""" Multiple devices detected (You can override by setting PLAIDML_DEVICE). Please choose a default device: """) for i, device in enumerate(devices): print(" {} : {}".format(i + 1, device)) choices = [str(i + 1) for i in range(len(devices))] dev_idx = int(choice_prompt("\nDefault device", choices, "1")) plaidml_settings.set('PLAIDML_DEVICE', devices[dev_idx - 1]) device = plaidml_settings.get('PLAIDML_DEVICE') print() print("Selected device:") print(" {}".format(device)) print() print("A target determines the compiler configuration and should be matched with your device.") print("Please choose a default target:") for i, target in enumerate(targets): print(" {} : {}".format(i + 1, target)) choices = [str(i + 1) for i in range(len(targets))] tgt_idx = int(choice_prompt("\nDefault target", choices, "1")) plaidml_settings.set('PLAIDML_TARGET', targets[tgt_idx - 1]) target = plaidml_settings.get('PLAIDML_TARGET') print() print("Selected target:") print(" {}".format(target)) print() print("Almost done. Multiplying some matrices...") print("Tile code:") print(" function (B[X, Z], C[Z, Y]) -> (A) { A[x, y : X, Y] = +(B[x, z] * C[z, y]); }") shape = edsl.LogicalShape(plaidml.DType.FLOAT32, [3, 3]) B = edsl.Tensor(shape) C = edsl.Tensor(shape) X, Y, Z = edsl.TensorDims(3) x, y, z = edsl.TensorIndexes(3) B.bind_dims(X, Z) C.bind_dims(Z, Y) A = edsl.TensorOutput(X, Y) A[x, y] += B[x, z] * C[z, y] program = edsl.Program('plaidml_setup', [A]) plaidml_exec.run(program, [(B, np.random.rand(3, 3)), (C, np.random.rand(3, 3))]) print("Whew. That worked.") print() settings_path = plaidml_settings.get('PLAIDML_SETTINGS') save = choice_prompt("Save settings to {0}".format(settings_path), ["y", "n"], "y") if save == "y": plaidml_settings.save() print("Success!") print()