예제 #1
0
def get_plaidml_devices(gpu=False):
    prepare_plaidml()

    import plaidml

    ctx = plaidml.Context()
    plaidml.settings._setup_for_test(plaidml.settings.user_settings)
    plaidml.settings.experimental = True
    devices, _ = plaidml.devices(ctx, limit=100, return_all=True)
    out_devices = []
    for device in devices:
        points = 0
        if b"cuda" in device.description.lower():
            points += 1
        if b"opencl" in device.description.lower():
            points += 1
        if b"nvidia" in device.description.lower():
            points += 1
        if b"amd" in device.description.lower():
            points += 1
        out_devices.append((points, device))

    out_devices.sort(reverse=True)
    return {
        device.description.decode("utf8"): device.id.decode("utf8")
        for points, device in out_devices
    }
예제 #2
0
    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)
예제 #3
0
def get_plaidML_devices():
    global plaidML_devices
    global has_nvidia_device
    if plaidML_devices is None:
        plaidML_devices = []
        # Using plaidML OpenCL backend to determine system devices and has_nvidia_device
        try:
            os.environ[
                'PLAIDML_EXPERIMENTAL'] = 'false'  #this enables work plaidML without run 'plaidml-setup'
            import plaidml
            ctx = plaidml.Context()
            for d in plaidml.devices(ctx, return_all=True)[0]:
                details = json.loads(d.details)
                if details['type'] == 'CPU':  #skipping opencl-CPU
                    continue
                if 'nvidia' in details['vendor'].lower():
                    has_nvidia_device = True
                plaidML_devices += [{
                    'id':
                    d.id,
                    'globalMemSize':
                    int(details['globalMemSize']),
                    'description':
                    d.description.decode()
                }]
            ctx.shutdown()
        except:
            pass
    return plaidML_devices
예제 #4
0
 def validateConfig(self, config_filename):
     print('Validating %s' % (config_filename,))
     ctx = plaidml.Context()
     with open(config_filename) as config_file:
         config = config_file.read()
     try:
         for conf in plaidml.devices(ctx, config):
             print('Found device %s: %s' % (conf.name, conf.description))
     except plaidml.exceptions.NotFound:
         print('No devices found matching %s' % (config_filename,))
예제 #5
0
def print_devices(heading, flag):
    print(heading)
    ctx = plaidml.Context()
    plaidml.settings.experimental = flag
    matched, unmatched = plaidml.devices(ctx, limit=100, return_all=True)
    for dev in matched:
        print('{0}   {1: >50} : {2}'.format('*', dev.id.decode(),
                                            dev.description.decode()))
    for dev in unmatched:
        print('{0}   {1: >50} : {2}'.format(' ', dev.id.decode(),
                                            dev.description.decode()))
예제 #6
0
 def testDeviceEnumeratorWithNoDevices(self):
     ctx = plaidml.Context()
     with self.assertRaises(plaidml.exceptions.PlaidMLError):
         plaidml.settings.config = """{
               "platform": {
                 "@type": "type.vertex.ai/vertexai.tile.local_machine.proto.Platform",
                 "hals": [
                   {
                     "@type": "type.vertex.ai/vertexai.tile.hal.opencl.proto.Driver",
                   }
                 ]
               }
             }"""
         for conf in plaidml.devices(ctx):
             pass
예제 #7
0
 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))
예제 #8
0
    def __init__(self, loglevel="INFO", log=True):
        if not _INIT and log:
            # Logger is held internally, as we don't want to log
            # when obtaining system stats on crash
            global _LOGGER  # pylint:disable=global-statement
            _LOGGER = logging.getLogger(__name__)  # pylint:disable=invalid-name
            _LOGGER.debug("Initializing: %s: (loglevel: %s, log: %s)",
                          self.__class__.__name__, loglevel, log)
        self.initialize(loglevel)
        self.ctx = plaidml.Context()
        self.supported_devices = self.get_supported_devices()
        self.devices = self.get_all_devices()

        self.device_details = [json.loads(device.details.decode()) for device in self.devices]
        if _LOGGER:
            _LOGGER.debug("Initialized: %s", self.__class__.__name__)
예제 #9
0
def get_plaidml_devices(gpu=False, _id=0):
    import plaidml
    ctx = plaidml.Context()
    plaidml.settings._setup_for_test(plaidml.settings.user_settings)
    plaidml.settings.experimental = True
    devices, _ = plaidml.devices(ctx, limit=100, return_all=True)
    if gpu:
        for device in devices:
            if b'cuda' in device.description.lower():
                return device
        for device in devices:
            if b'opencl' in device.description.lower() and device.id.endswith(
                    b'%d' % _id):
                return device
    for device in devices:
        if b'llvm' in device.description.lower():
            return device
예제 #10
0
파일: amd.py 프로젝트: deepfakes/faceswap
    def _initialize(self) -> None:
        """ Initialize PlaidML for AMD GPUs.

        If :attr:`_is_initialized` is ``True`` then this function just returns performing no
        action.

        if ``False`` then PlaidML is setup, if not already, and GPU information is extracted
        from the PlaidML context.
        """
        if self._is_initialized:
            return
        self._log("debug", "Initializing PlaidML for AMD GPU.")

        self._initialize_plaidml()

        self._ctx = plaidml.Context()
        self._supported_devices = self._get_supported_devices()
        self._all_devices = self._get_all_devices()
        self._device_details = self._get_device_details()
        self._select_device()
예제 #11
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)
예제 #12
0
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")
예제 #13
0
 def testDeviceEnumeratorInvalidConfig(self):
     ctx = plaidml.Context()
     with self.assertRaises(plaidml.exceptions.InvalidArgument):
         plaidml.settings.config = 'An invalid configuration'
         for conf in plaidml.devices(ctx):
             pass
예제 #14
0
class PlaidMLBackend(onnx.backend.base.Backend):
    ctx = plaidml.Context()
    device_configs = _get_device_configs(ctx)
    ops = opset_onnx.OPSETS

    @classmethod
    def run_model(cls, model, inputs, device=None, **kwargs):
        if device is None:
            device = cls._get_default_device()
        return super(PlaidMLBackend, cls).run_model(model, device=device, **kwargs)

    @classmethod
    def _get_default_device(cls):
        device_ids = plaidml.settings.device_ids
        if not device_ids:
            six.raise_from(
                onnx_plaidml.DeviceNotFoundError(None, list(cls.device_configs.keys())), None)
        if len(device_ids) != 1:
            six.raise_from(onnx_plaidml.TooManyDefaultDevicesError(device_ids), None)
        if not cls.supports_device(device_ids[0]):
            six.raise_from(
                onnx_plaidml.DeviceNotFoundError(device_id, list(cls.device_configs.keys())), None)
        return device_ids[0]

    @classmethod
    def _apply_node(cls, ops, node, bindings):
        """
        Applies an operation given the supplied bindings.

        Args:
            ops: The operation definitions.
            node: The ONNX NodeProto describing the operation node.
            bindings: The binding map.

        Returns:
            None.  Updates from the operation are added to the binding map.
        """
        attrs = {}
        for attr in node.attribute:
            if attr.type:
                attrs[attr.name] = _ONNX_ATTRTYPE_TO_GETTER[attr.type](attr)
            elif attr.HasField('f'):
                attrs[attr.name] = attr.f
            elif attr.HasField('i'):
                attrs[attr.name] = attr.i
            elif attr.HasField('s'):
                attrs[attr.name] = attr.s
            elif attr.HasField('t'):
                attrs[attr.name] = attr.t
            elif attr.HasField('g'):
                attrs[attr.name] = attr.g
            elif attr.floats:
                attrs[attr.name] = attr.floats
            elif attr.ints:
                attrs[attr.name] = attr.ints
            elif attr.strings:
                attrs[attr.name] = attr.strings
            elif attr.tensors:
                attrs[attr.name] = attr.tensors
            elif attr.graphs:
                attrs[attr.name] = attr.graphs
            else:
                attrs[attr.name] = 0

        input_vars = [bindings[name] for name in node.input]
        try:
            operation = ops[(node.domain, node.op_type)]
        except KeyError:
            six.raise_from(
                NotImplementedError(
                    '"{}"/"{}" is not implemented by the PlaidML ONNX backend'.format(
                        node.domain, node.op_type)), None)

        output_vars = operation(opset_util.Context(node), *input_vars, **attrs)

        for (name, var) in zip(node.output, output_vars):
            bindings[name] = var

    @classmethod
    def prepare(cls, model, device=None, **kwargs):
        if not device:
            device = cls._get_default_device()
        super(PlaidMLBackend, cls).prepare(model, device, **kwargs)
        ops = _load_ops(model.opset_import)
        try:
            config = cls.device_configs[device]
        except KeyError:
            six.raise_from(
                onnx_plaidml.DeviceNotFoundError(device, list(cls.device_configs.keys())), None)
        dev = plaidml.Device(cls.ctx, config)

        bindings = {}
        graph = model.graph

        initializers = set()
        for initializer in graph.initializer:
            initializers.add(initializer.name)
            bindings[initializer.name] = tile.Value.from_var(
                opset_util.onnx_tensor_to_plaidml_tensor(cls.ctx, dev, initializer),
                initializer.dims, opset_util.ONNX_DTYPE_TO_PLAIDML[initializer.data_type])

        input_valinfos = []
        for valinfo in graph.input:
            if valinfo.name not in initializers:
                bindings[valinfo.name] = opset_util.onnx_type_to_placeholder_value(valinfo.type)
                input_valinfos.append(valinfo)

        for node in graph.node:
            cls._apply_node(ops, node, bindings)

        func = tile.compose(
            cls.ctx,
            dev,
            inputs=[(_as_input_id(inp.name), bindings[inp.name]) for inp in graph.input
                    if inp.name not in initializers],
            outputs=[(_as_output_id(outp.name), bindings[outp.name]) for outp in graph.output])

        return PlaidMLBackendRep(model, cls.ctx, dev, func, input_valinfos)

    @classmethod
    def run_node(cls, node, inputs, device=None):
        if not device:
            device = cls._get_default_device()
        super(PlaidMLBackend, cls).run_node(node, inputs, device)
        dev = plaidml.Device(cls.ctx, cls.device_configs[device])
        try:
            bindings = {}

            for (name, py_input) in zip(node.input, inputs):
                bindings[name] = tile.Value.from_python_value(py_input, ctx=cls.ctx, dev=dev)

            cls._apply_node(_load_ops(), node, bindings)

            func = tile.compose(
                cls.ctx,
                dev,
                inputs=[],
                outputs=[(_as_output_id(name), bindings[name]) for name in node.output])

            invoker = plaidml.Invoker(cls.ctx, func)

            tensors = [
                plaidml.Tensor(dev, invoker.get_output_shape(_as_output_id(name)))
                for name in node.output
            ]
            for (name, tensor) in zip(node.output, tensors):
                invoker.set_output(_as_output_id(name), tensor)

            invoker.invoke()

            return [tensor.as_ndarray(cls.ctx) for tensor in tensors]

        finally:
            dev.close()

    @classmethod
    def supports_device(cls, device_id):
        return device_id in cls.device_configs
예제 #15
0
 def testDeviceEnumerator(self):
     ctx = plaidml.Context()
     for conf in plaidml.devices(ctx, limit=100):
         pass
예제 #16
0
 def setUpClass(cls):
     testing.plaidml_config.unittest_config()
     cls._ctx = plaidml.Context()
     cls._dev = plaidml.Device(cls._ctx,
                               plaidml.devices(cls._ctx, limit=10)[0])
예제 #17
0
파일: device.py 프로젝트: yedk/DeepFaceLab
has_nvml = False
has_nvml_cap = False

#use force_has_nvidia_device=1 if
#- your NVIDIA cannot be seen by OpenCL
#- CUDA build of DFL
has_nvidia_device = os.environ.get("force_has_nvidia_device", "0") == "1"

plaidML_devices = []

# Using plaidML OpenCL backend to determine system devices and has_nvidia_device
try:
    os.environ[
        'PLAIDML_EXPERIMENTAL'] = 'false'  #this enables work plaidML without run 'plaidml-setup'
    import plaidml
    ctx = plaidml.Context()
    for d in plaidml.devices(ctx, return_all=True)[0]:
        details = json.loads(d.details)
        if details['type'] == 'CPU':  #skipping opencl-CPU
            continue
        if 'nvidia' in details['vendor'].lower():
            has_nvidia_device = True
        plaidML_devices += [{
            'id': d.id,
            'globalMemSize': int(details['globalMemSize']),
            'description': d.description.decode()
        }]
    ctx.shutdown()
except:
    pass
예제 #18
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)
예제 #19
0
 def testMatrixMultiply(self):
     ctx = plaidml.Context()
     with plaidml.open_first_device(ctx) as dev:
         self.runMatrixMultiply(ctx, dev)
예제 #20
0
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")
예제 #21
0
 def __init__(self, ctx=None):
     self.ctx = ctx or plaidml.Context()
예제 #22
0
def _inner_run(reports,
               frontend,
               network_names,
               params,
               warmup,
               kernel_timing,
               callgrind,
               print_stacktraces,
               tile=None):
    import plaidbench.cli as pb
    model = frontend.model(params)
    click.secho(
        'Running {0} examples with {1}, batch size {2}, on backend {3}'.format(
            params.examples, params.network_name, params.batch_size,
            params.backend_name),
        fg='magenta')

    if params.backend_name == 'plaid_edsl':
        # Don't do kernel timing with PlaidML EDSL
        kernel_timing = False
    else:
        device = plaidml.devices(plaidml.Context())[0]
        if 'metal' in str(device):
            kernel_timing = False

    benchmark_results = {}
    model_output = None

    if params.examples % params.batch_size != 0:
        raise ValueError(
            'The number of examples must be divisible by the batch size.')
    try:
        model.validate()
        model.setup()

        stop_watch = StopWatch(callgrind)
        compile_stop_watch = StopWatch(callgrind)

        click.echo('Compiling network...', nl=False)
        compile_stop_watch.start_outer()
        stop_watch.start_outer()

        model.compile()
        model_output, overrides = model.run(once=True)
        if tile:
            click.echo(' Saving Tile to {}...'.format(tile), nl=False)
            model.model.predict_function._invoker.save(tile)

        compile_stop_watch.stop()

        # Run a few more warmups -- this seems to improve the variability of the
        # benchmark results.
        if warmup:
            click.echo(' Warming up...', nl=False)
            model.run(warmup=True)
        click.echo(' Running...')

        # Plaid currently doesn't make it easy to get at metrics,
        # So we steal them from the logs
        timef = ProgramTimeFilter()
        og = logging.getLogger(plaidml.__name__)
        if kernel_timing:
            plaidml._lib()._internal_set_vlog(1)
            if og.level is logging.NOTSET:
                plaidml.DEFAULT_LOG_HANDLER.setLevel(logging.WARNING)
            og.setLevel(logging.DEBUG)
        og.addFilter(timef)

        stop_watch.start()
        _, overrides = model.run()
        stop_watch.stop()

        og.removeFilter(timef)
        # Record stopwatch times
        execution_duration = overrides.get('time', stop_watch.elapsed())
        tile_exec_per_example = 1e-9 + timef.tot_time_ns / 10.0**9 / params.examples
        exec_per_example = execution_duration / params.examples
        compile_duration = compile_stop_watch.elapsed()
        flops = overrides.get('flops', None)
        gflops = None
        if flops:
            gflops = (flops / 10.0**9 / exec_per_example)
            benchmark_results['GFLOP/s'] = gflops
            benchmark_results['flops'] = flops

        benchmark_results['compile_duration'] = compile_duration
        benchmark_results['duration_per_example'] = exec_per_example
        benchmark_results['tile_duration_per_example'] = tile_exec_per_example
        benchmark_results['examples'] = params.examples
        benchmark_results['batch_size'] = params.batch_size
        benchmark_results['model'] = params.network_name
        benchmark_results['backend'] = params.backend_name

        resstr = 'Example finished, elapsed: {:.3f}s (compile), {:.3f}s (execution)\n'.format(
            compile_duration, execution_duration)
        if gflops:
            resstr += ', {:.2f} (GFLOP/s)'.format(gflops)
        click.secho(resstr, fg='cyan', bold=True)
        if frontend.name == 'plaidml' and 'metal' in str(device):
            tile_exec_per_example = exec_per_example
        print(
            "-----------------------------------------------------------------------------------------"
        )
        print("%-20s %-25s %-20s" %
              ("Network Name", "Inference Latency", "Time / FPS"))
        print(
            "-----------------------------------------------------------------------------------------"
        )
        print("%-20s %-25s %-20s" %
              (params.network_name, "%.2f ms" %
               (exec_per_example * 1000), "%.2f ms / %.2f fps" %
               (tile_exec_per_example * 1000, 1.0 / tile_exec_per_example)))

        (golden_output, precision) = model.golden_output()
        (correct, max_error, max_abs_error,
         fail_ratio) = Runner._check_correctness(golden_output, model_output,
                                                 precision.value)
        benchmark_results['correct'] = correct
        benchmark_results['max_error'] = float(max_error)
        benchmark_results['max_abs_error'] = float(max_abs_error)
        benchmark_results['fail_ratio'] = fail_ratio
        if correct:
            status = 'PASS'
        else:
            status = 'FAIL'
        click.secho(
            'Correctness: {}, max_error: {}, max_abs_error: {}, fail_ratio: {}'
            .format(status, max_error, max_abs_error, fail_ratio),
            fg='green' if status == 'PASS' else 'red')
    except GoldenOutputNotAvailableError:
        click.echo(
            'Correctness: untested. Could not find golden data to compare against.'
        )
    except NoCorrectnessDesired:
        pass

    # Error handling
    except Exception as ex:
        # click.echo statements
        click.echo(ex)
        click.echo('Set --print-stacktraces to see the entire traceback')

        # Record error
        benchmark_results['exception'] = str(ex)

        if print_stacktraces:
            raise

    finally:
        reports.append((params, benchmark_results, model_output))
예제 #23
0
def main():
    # set intial exit status
    exit_status = 0

    # intialize and run arguement parser
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--results',
        default='/tmp/plaidbench_results',
        help=
        'system path to blanket runs will be looked for and output that will be graphed'
    )
    parser.add_argument('--prospector',
                        action='store_true',
                        default=False,
                        help='seek out golden paths')
    parser.add_argument('--gflops', default=2000, help='set card gflops')
    args = parser.parse_args()
    args.results = os.path.expanduser(args.results)
    args.gflops = int(args.gflops)

    # intialize variables
    plot_maker = plotter()
    data = {}

    # open results file
    try:
        os.makedirs(args.results)
    except OSError as ex:
        if ex.errno != errno.EEXIST:
            print(ex)
            return
    from os import listdir
    from os.path import isfile, join
    files = [
        f for f in listdir(args.results) if isfile(join(args.results, f))
        if f.endswith('json')
    ]
    for file in files:
        data = None
        with open(os.path.join(args.results, file), 'r') as saved_file:
            data = json.load(saved_file)
        # creating dict with completed runs
        runs = {}
        for key, value in data.items():
            if 'results' in value:
                if 'duration_per_example' in value['results']:
                    runs[key] = value['results']
                else:
                    pass

        for sz in ['sml', 'med', 'lrg']:
            # sort runs dictionary
            models_list = []
            executions_list = []
            batch_list2 = []
            name = []
            for x, y in sorted(runs.items()):
                if sz in y['model']:
                    models_list.append(y['model'])
                    executions_list.append(y['GFLOP/s'])
                    batch_list2.append(y['batch_size'])
                    name.append(y['model'] + " : " + str(y['batch_size']))
            if not models_list:
                continue
                # setting up data frame
            uber_list = pd.DataFrame()
            uber_list['model'] = models_list
            uber_list['GFLOP/s'] = executions_list
            uber_list['batch'] = batch_list2
            uber_list['name'] = name
            isTrain = (data['run_configuration']['train'])

            # attempting to get info about users env
            userSys = platform.uname()
            userPyV = platform.python_version()
            machine_info = []
            for info in userSys:
                machine_info.append(info)
            machine_info.append(userPyV)
            ctx = plaidml.Context()
            devices, _ = plaidml.devices(ctx, limit=100, return_all=True)
            for dev in devices:
                plt.suptitle(str(dev))
                machine_info.append(str(dev))

            plot_maker.generate_plot("{}_{}".format(file[:-5], sz), uber_list,
                                     args, isTrain)
    shutil.copyfile('compare.html.template',
                    os.path.join(args.results, 'compare.html'))