def matmul(A, B): I, J = edsl.TensorDims(2) i, j = edsl.TensorIndexes(2) A.bind_dims(I, J) B.bind_dims(J) return edsl.Contraction().outShape(I).outAccess(i).sum(A[i, j] * B[j]).build()
def partial(F, wrt, delta): F_neg = -F dims = edsl.TensorDims(3) x, y, z = edsl.TensorIndexes(3) F.bind_dims(*dims) OC = edsl.Contraction().outShape(*dims) if wrt == 'x': O = OC.outAccess(x, y, z).assign(F[x + 1, y, z] + F_neg[x - 1, y, z]).build() elif wrt == 'y': O = OC.outAccess(x, y, z).assign(F[x, y + 1, z] + F_neg[x, y - 1, z]).build() elif wrt == 'z': O = OC.outAccess(x, y, z).assign(F[x, y, z + 1] + F_neg[x, y, z - 1]).build() return O / (2.0 * delta)
def partial( F, # F: differentiable function tensor wrt, # wrt: ('x' | 'y' | 'z') delta): # delta: grid spacing F_neg = -F dims = edsl.TensorDims(3) x, y, z = edsl.TensorIndexes(3) F.bind_dims(*dims) OC = edsl.Contraction().outShape(*dims) if wrt == 'x': O = OC.outAccess(x, y, z).assign(F[x + 1, y, z] + F_neg[x - 1, y, z]).build() elif wrt == 'y': O = OC.outAccess(x, y, z).assign(F[x, y + 1, z] + F_neg[x, y - 1, z]).build() elif wrt == 'z': O = OC.outAccess(x, y, z).assign(F[x, y, z + 1] + F_neg[x, y, z - 1]).build() return O / (2.0 * delta)
def partial_chi(F, wrt, delta): dims = edsl.TensorDims(3) x, y, z = edsl.TensorIndexes(3) F.bind_dims(*dims) DF_left_C = edsl.Contraction().outShape(*dims) DF_right_C = edsl.Contraction().outShape(*dims) if wrt == 'x': DF_right = DF_right_C.outAccess(x, y, z).assign(F[x + 1, y, z]).build() DF_left = DF_left_C.outAccess(x, y, z).assign(F[x - 1, y, z]).build() elif wrt == 'y': DF_right = DF_right_C.outAccess(x, y, z).assign(F[x, y + 1, z]).build() DF_left = DF_left_C.outAccess(x, y, z).assign(F[x, y - 1, z]).build() elif wrt == 'z': DF_right = DF_right_C.outAccess(x, y, z).assign(F[x, y, z + 1]).build() DF_left = DF_left_C.outAccess(x, y, z).assign(F[x, y, z - 1]).build() one = edsl.cast(1, F.dtype) zero = edsl.cast(1, F.dtype) neg_one = edsl.cast(-1, F.dtype) DF_chi_right = edsl.select(DF_right < 0, one, zero) DF_chi_left = edsl.select(DF_left < 0, neg_one, zero) return (DF_chi_right + DF_chi_left) / (2.0 * delta)
def sum(R): idxs = edsl.TensorIndexes(3) return edsl.Contraction().sum(R[idxs]).build()
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.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)) shape = edsl.TensorShape(plaidml.DType.FLOAT32, [3, 3]) B = edsl.Placeholder(shape) C = edsl.Placeholder(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.Contraction().outShape(X, Y).outAccess(x, y).sum(B[x, z] * C[z, y]).build() program = plaidml.Program('plaidml_setup', [B, C], [A]) print() print("Almost done. Multiplying some matrices...") print("Tile code:") print(program) plaidml.exec.run(program, [np.random.rand(3, 3), 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()