def validate(self): with plaidml.open_first_device(self.ctx) as dev: matmul = plaidml.Function( "function (B[X,Z], C[Z,Y]) -> (A) { A[x,y : X,Y] = +(B[x,z] * C[z,y]); }" ) shape = plaidml.Shape(self.ctx, plaidml.DType.FLOAT32, 3, 3) a = plaidml.Tensor(dev, shape) b = plaidml.Tensor(dev, shape) c = plaidml.Tensor(dev, shape) plaidml.run(self.ctx, matmul, inputs={ "B": b, "C": c }, outputs={"A": a})
def runMatrixMultiply(self, ctx, dev): matmul = plaidml.Function( "function (B[X,Z], C[Z,Y]) -> (A) { A[x,y : X,Y] = +(B[x,z] * C[z,y]); }" ) shape = plaidml.Shape(ctx, plaidml.DATA_FLOAT32, 3, 3) b = plaidml.Tensor(dev, shape) with b.mmap_discard(ctx) as view: view[0] = 1.0 view[1] = 2.0 view[2] = 3.0 view[3] = 4.0 view[4] = 5.0 view[5] = 6.0 view[6] = 7.0 view[7] = 8.0 view[8] = 9.0 view.writeback() c = plaidml.Tensor(dev, shape) with c.mmap_discard(ctx) as view: view[(0, 0)] = 1.0 view[(0, 1)] = 2.0 view[(0, 2)] = 3.0 view[(1, 0)] = 4.0 view[(1, 1)] = 5.0 view[(1, 2)] = 6.0 view[(2, 0)] = 7.0 view[(2, 1)] = 8.0 view[(2, 2)] = 9.0 view.writeback() a = plaidml.Tensor(dev, shape) plaidml.run(ctx, matmul, inputs={"B": b, "C": c}, outputs={"A": a}) with a.mmap_current() as view: self.assertEqual(view[0], 1.0 + 8.0 + 21.0) self.assertEqual(view[1], 2.0 + 10.0 + 24.0) self.assertEqual(view[2], 3.0 + 12.0 + 27.0) self.assertEqual(view[(1, 0)], 4.0 + 20.0 + 42.0) self.assertEqual(view[(1, 1)], 8.0 + 25.0 + 48.0) self.assertEqual(view[(1, 2)], 12.0 + 30.0 + 54.0) self.assertEqual(view[6], 7.0 + 32.0 + 63.0) self.assertEqual(view[7], 14.0 + 40.0 + 72.0) self.assertEqual(view[8], 21.0 + 48.0 + 81.0)
def testManualReshape(self): ctx = plaidml.Context() reshape = plaidml.Function( "function (I) -> (O) { F[3*j + k: 4 * 3] = >(I[j,k]); O[p,q : 6,2] = >(F[2*p + q]);}" ) iShape = plaidml.Shape(ctx, plaidml.DATA_FLOAT32, 4, 3) oShape = plaidml.Shape(ctx, plaidml.DATA_FLOAT32, 6, 2) with plaidml.open_first_device(ctx) as dev: I = plaidml.Tensor(dev, iShape) with I.mmap_discard(ctx) as view: view[0] = 1.0 view[1] = 2.0 view[2] = 3.0 view[3] = 4.0 view[4] = 5.0 view[5] = 6.0 view[6] = 7.0 view[7] = 8.0 view[8] = 9.0 view[9] = 10.0 view[10] = 11.0 view[11] = 12.0 view.writeback() O = plaidml.Tensor(dev, oShape) plaidml.run(ctx, reshape, inputs={"I": I}, outputs={"O": O}) with O.mmap_current() as view: self.assertEqual(view[0], 1.0) self.assertEqual(view[1], 2.0) self.assertEqual(view[2], 3.0) self.assertEqual(view[3], 4.0) self.assertEqual(view[4], 5.0) self.assertEqual(view[5], 6.0) self.assertEqual(view[6], 7.0) self.assertEqual(view[7], 8.0) self.assertEqual(view[8], 9.0) self.assertEqual(view[9], 10.0) self.assertEqual(view[10], 11.0) self.assertEqual(view[11], 12.0)
def bind(self, bindings): """Builds an output variable dictionary for the operation. N.B. Subclasses may override this method in order to implement optimizations and composite operations that aren't easily described in TILE. Args: bindings (_OpBindings): The previously-computed output bindings. Returns: str -> plaidml.Var: The bound outputs for this operation. The caller is responsible for adding this to the known output bindings; this is typically called by _OpBindings.__missing__, which does this automatically. """ if not self.code: raise NotImplementedError('{} is not directly implemented.'.format( self.__class__.__name__)) applier = plaidml.Applier(bindings.ctx, plaidml.Function(self.code)) for name, input_value in self.inputs.items(): applier.add_input(name, input_value.bind(bindings)) outputs = {} for name in self.outputs.keys(): outputs[name] = applier.add_output(name) return outputs
def main(): ctx = plaidml.Context() plaidml.quiet() def choice_prompt(question, choices, default): inp = "" while not inp in choices: inp = input("{0}? ({1})[{2}]:".format(question, ",".join(choices), default)) if not inp: inp = default elif inp not in choices: print("Invalid choice: {}".format(inp)) return inp 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 GNU AGPLv3 """.format(plaidml.__version__)) # Operate as if nothing is set plaidml.settings._setup_for_test(plaidml.settings.user_settings) plaidml.settings.experimental = False devices, _ = plaidml.devices(ctx, limit=100, return_all=True) plaidml.settings.experimental = True exp_devices, unmatched = plaidml.devices(ctx, limit=100, return_all=True) if not (devices or exp_devices): if not unmatched: print(""" No OpenCL devices found. Check driver installation. Read the helpful, easy driver installation instructions from our README: http://github.com/plaidml/plaidml """) else: print(""" No supported devices found. Run 'clinfo' and file an issue containing the full output. """) sys.exit(-1) print("Default Config Devices:") if not devices: print(" No devices.") for dev in devices: print(" {0} : {1}".format(dev.id.decode(), dev.description.decode())) print("\nExperimental Config Devices:") if not exp_devices: print(" No devices.") for dev in exp_devices: print(" {0} : {1}".format(dev.id.decode(), dev.description.decode())) print( "\nUsing experimental devices can cause poor performance, crashes, and other nastiness.\n") exp = choice_prompt("Enable experimental device support", ["y", "n"], "n") plaidml.settings.experimental = exp == "y" try: devices = plaidml.devices(ctx, limit=100) except plaidml.exceptions.PlaidMLError: print("\nNo devices available in chosen config. Rerun plaidml-setup.") sys.exit(-1) if len(devices) > 1: print(""" Multiple devices detected (You can override by setting PLAIDML_DEVICE_IDS). Please choose a default device: """) devrange = range(1, len(devices) + 1) for i in devrange: print(" {0} : {1}".format(i, devices[i - 1].id.decode())) dev = choice_prompt("\nDefault device", [str(i) for i in devrange], "1") plaidml.settings.device_ids = [devices[int(dev) - 1].id.decode()] print("\nSelected device:\n {0}".format(plaidml.devices(ctx)[0])) print(""" PlaidML sends anonymous usage statistics to help guide improvements. We'd love your help making it better. """) tel = choice_prompt("Enable telemetry reporting", ["y", "n"], "y") plaidml.settings.telemetry = tel == "y" print("\nAlmost done. Multiplying some matrices...") # Reinitialize to send a usage report print("Tile code:") print(" function (B[X,Z], C[Z,Y]) -> (A) { A[x,y : X,Y] = +(B[x,z] * C[z,y]); }") with plaidml.open_first_device(ctx) as dev: matmul = plaidml.Function( "function (B[X,Z], C[Z,Y]) -> (A) { A[x,y : X,Y] = +(B[x,z] * C[z,y]); }") shape = plaidml.Shape(ctx, plaidml.DType.FLOAT32, 3, 3) a = plaidml.Tensor(dev, shape) b = plaidml.Tensor(dev, shape) c = plaidml.Tensor(dev, shape) plaidml.run(ctx, matmul, inputs={"B": b, "C": c}, outputs={"A": a}) print("Whew. That worked.\n") sav = choice_prompt("Save settings to {0}".format(plaidml.settings.user_settings), ["y", "n"], "y") if sav == "y": plaidml.settings.save(plaidml.settings.user_settings) print("Success!\n")
def main(): ctx = plaidml.Context() plaidml.quiet() def choice_prompt(question, choices, default): inp = "" while not inp in choices: inp = input("{0}? ({1})[{2}]:".format(question, ",".join(choices), default)) if not inp: inp = default elif inp not in choices: print("Invalid choice: {}".format(inp)) return inp print(""" PlaidML Setup ({0}) Thanks for using PlaidML! The feedback we have received from our users indicates an ever-increasing need for performance, programmability, and portability. During the past few months, we have been restructuring PlaidML to address those needs. To make all the changes we need to make while supporting our current user base, all development of PlaidML has moved to a branch — plaidml-v1. We will continue to maintain and support the master branch of PlaidML and the stable 0.7.0 release. Read more here: https://github.com/plaidml/plaidml Some Notes: * Bugs and other issues: https://github.com/plaidml/plaidml/issues * 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__)) # Placeholder env var if os.getenv("PLAIDML_VERBOSE"): # change verbose settings to PLAIDML_VERBOSE, or 4 if PLAIDML_VERBOSE is invalid try: arg_verbose = int(os.getenv("PLAIDML_VERBOSE")) except ValueError: arg_verbose = 4 plaidml._internal_set_vlog(arg_verbose) print("INFO:Verbose logging has been enabled - verbose level", arg_verbose, "\n") if plaidml.settings.default_config: (cfg_path, cfg_file) = os.path.split(plaidml.settings.default_config) else: (cfg_path, cfg_file) = ("Unknown", "Unknown") if plaidml.settings.experimental_config: (exp_path, exp_file) = os.path.split(plaidml.settings.experimental_config) else: (exp_path, exp_file) = ("Unknown", "Unknown") # Operate as if nothing is set plaidml.settings._setup_for_test(plaidml.settings.user_settings) plaidml.settings.experimental = False devices, _ = plaidml.devices(ctx, limit=100, return_all=True) plaidml.settings.experimental = True exp_devices, unmatched = plaidml.devices(ctx, limit=100, return_all=True) if not (devices or exp_devices): if not unmatched: print(""" No OpenCL devices found. Check driver installation. Read the helpful, easy driver installation instructions from our README: http://github.com/plaidml/plaidml """) else: print(""" No supported devices found. Run 'clinfo' and file an issue containing the full output. """) sys.exit(-1) if devices and os.getenv("PLAIDML_VERBOSE"): print("Default Config File Location:") print(" {0}/".format(cfg_path)) print("\nDefault Config Devices:") if not devices: print(" No devices.") for dev in devices: print(" {0} : {1}".format(dev.id.decode(), dev.description.decode())) if exp_devices and os.getenv("PLAIDML_VERBOSE"): print("\nExperimental Config File Location:") print(" {0}/".format(exp_path)) print("\nExperimental Config Devices:") if not exp_devices: print(" No devices.") for dev in exp_devices: print(" {0} : {1}".format(dev.id.decode(), dev.description.decode())) print( "\nUsing experimental devices can cause poor performance, crashes, and other nastiness.\n" ) exp = choice_prompt("Enable experimental device support", ["y", "n"], "n") plaidml.settings.experimental = exp == "y" try: devices = plaidml.devices(ctx, limit=100) except plaidml.exceptions.PlaidMLError: print("\nNo devices available in chosen config. Rerun plaidml-setup.") sys.exit(-1) if devices: dev = 1 if len(devices) > 1: print(""" Multiple devices detected (You can override by setting PLAIDML_DEVICE_IDS). Please choose a default device: """) devrange = range(1, len(devices) + 1) for i in devrange: print(" {0} : {1}".format(i, devices[i - 1].id.decode())) dev = choice_prompt("\nDefault device", [str(i) for i in devrange], "1") plaidml.settings.device_ids = [devices[int(dev) - 1].id.decode()] print("\nSelected device:\n {0}".format(plaidml.devices(ctx)[0])) print("\nAlmost done. Multiplying some matrices...") # Reinitialize to send a usage report print("Tile code:") print( " function (B[X,Z], C[Z,Y]) -> (A) { A[x,y : X,Y] = +(B[x,z] * C[z,y]); }" ) with plaidml.open_first_device(ctx) as dev: matmul = plaidml.Function( "function (B[X,Z], C[Z,Y]) -> (A) { A[x,y : X,Y] = +(B[x,z] * C[z,y]); }" ) shape = plaidml.Shape(ctx, plaidml.DType.FLOAT32, 3, 3) a = plaidml.Tensor(dev, shape) b = plaidml.Tensor(dev, shape) c = plaidml.Tensor(dev, shape) plaidml.run(ctx, matmul, inputs={"B": b, "C": c}, outputs={"A": a}) print("Whew. That worked.\n") sav = choice_prompt( "Save settings to {0}".format(plaidml.settings.user_settings), ["y", "n"], "y") if sav == "y": plaidml.settings.save(plaidml.settings.user_settings) print("Success!\n")