def testTransferLargeNDArray(self): size = 3000000 shape = (size,) dtype = plaidml.DATA_FLOAT32 ctx = plaidml.Context() with plaidml.open_first_device(ctx) as dev: expected = np.random.uniform(low=0, high=100, size=size) tensor = plaidml.Tensor(dev, plaidml.Shape(ctx, dtype, *shape)) actual = np.ndarray(shape, dtype='f4') pr = cProfile.Profile() pr.enable() with tensor.mmap_discard(ctx) as view: view.copy_from_ndarray(expected) view.writeback() with tensor.mmap_current() as view: view.copy_to_ndarray(actual) pr.disable() pr.print_stats() np.testing.assert_array_almost_equal(actual, expected, decimal=4)
def testBufferRanges(self): ctx = plaidml.Context() with plaidml.open_first_device(ctx) as dev: buf = plaidml.Tensor(dev, plaidml.Shape(ctx, plaidml.DATA_FLOAT32, 10)) with buf.mmap_current() as view: self.assertEqual(len(view), 10) view[0] = 1 with self.assertRaises(IndexError): view[10] = 0 view[9] = 2 view[-1] = 4 self.assertEqual(view[9], 4) view[0:10:3] = (1,2,3,4) self.assertEqual(view[3], 2) self.assertSequenceEqual(view[0:10:3], (1,2,3,4))
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 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 testLargeConfigValuesNoCrash(self): ctx = plaidml.Context() plaidml.settings.config = testing.plaidml_config.very_large_values_config() with plaidml.open_first_device(ctx) as dev: self.runMatrixMultiply(ctx, dev)
def testMatrixMultiply(self): ctx = plaidml.Context() with plaidml.open_first_device(ctx) as dev: self.runMatrixMultiply(ctx, dev)
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")