Пример #1
0
 def __init__(self, old, name=None, name_injector={}):
     # Add module itself into global config
     minpy.Config['modules'].append(self)
     self._registry = Registry()
     self._policy = minpy.Config['default_policy']
     self._logger = log.get_logger(old['__name__'])
     self._logger.info('Initialize module: {}.'.format(old['__name__']))
     self._old = old
     self._name_injector = name_injector
     if len(name_injector) != 0:
         self._logger.info('Inject Name Injector {}'.format(name_injector.__name__))
     for vname in variants:
         if name is None:
             modname = 'minpy.array_variants.{}'.format(vname)
         else:
             modname = 'minpy.array_variants.{}.{}'.format(vname, name)
         mod = importlib.import_module(modname)
         self._logger.info('Importing from {}.'.format(modname))
         primitive_wrapper = lambda func, *args, **kwargs:\
                 Primitive(func, variants[vname], *args, **kwargs)
         # Register all primitives of the module.
         before = len(self._registry._reg)
         mod.register_primitives(self._registry, primitive_wrapper)
         self._logger.info('Got {} primitives from {}'.format(
             len(self._registry._reg) - before, modname))
         primitive_getter = lambda name: self._registry.get(name, variants[vname])
         # Define gradients of primitives.
         mod.def_grads(self._registry, primitive_getter)
     self._logger.info('Import {} primitives'.format(
         len(self._registry._reg)))
Пример #2
0
 def __init__(self, old, name=None, name_injector=None):
     # pylint: disable= protected-access, cell-var-from-loop
     # Add module itself into global config
     minpy.Config['modules'].append(self)
     self._registry = Registry(old['__name__'])
     self._policy = minpy.Config['default_policy']
     self._logger = log.get_logger(old['__name__'])
     self._logger.info('Initialize module: %s.', old['__name__'])
     self._old = old
     self._name_injector = name_injector if name_injector else {}
     if len(self._name_injector) != 0:
         self._logger.info('Inject Name Injector %s', name_injector.__name__)
     for vname in variants:
         vtype = variants[vname]
         if name is None:
             modname = 'minpy.array_variants.{}'.format(vname)
         else:
             modname = 'minpy.array_variants.{}.{}'.format(vname, name)
         mod = importlib.import_module(modname)
         self._logger.info('Importing from %s.', modname)
         primitive_wrapper = lambda func, *args, **kwargs:\
                 Primitive(func, vtype, *args, **kwargs)
         # Register all primitives of the module.
         before = len(self._registry._reg)
         mod.register_primitives(self._registry, primitive_wrapper)
         self._logger.info('Got %d primitives from %s',
                           len(self._registry._reg) - before, modname)
         primitive_getter = lambda name: self._registry.get(name, vtype)
         # Define gradients of primitives.
         mod.def_grads(primitive_getter)
     self._logger.info('Import %d primitives', len(self._registry._reg))
     self._set_attrs()
Пример #3
0
class Module(object):
    """Mocking module class for name dispatching.

    It will register primitives from :mod:`minpy.array_variant`.

    Parameters
    ----------
    old : dict
        A meta class including info such as name, path, etc.
    name : None, or str
        Second level name if specified.
    name_injector : dict, or dict-like object
        An optional dict provides manual dispatching
    """

    def __init__(self, old, name=None, name_injector={}):
        # Add module itself into global config
        minpy.Config['modules'].append(self)
        self._registry = Registry()
        self._policy = minpy.Config['default_policy']
        self._logger = log.get_logger(old['__name__'])
        self._logger.info('Initialize module: {}.'.format(old['__name__']))
        self._old = old
        self._name_injector = name_injector
        if len(name_injector) != 0:
            self._logger.info('Inject Name Injector {}'.format(name_injector.__name__))
        for vname in variants:
            if name is None:
                modname = 'minpy.array_variants.{}'.format(vname)
            else:
                modname = 'minpy.array_variants.{}.{}'.format(vname, name)
            mod = importlib.import_module(modname)
            self._logger.info('Importing from {}.'.format(modname))
            primitive_wrapper = lambda func, *args, **kwargs:\
                    Primitive(func, variants[vname], *args, **kwargs)
            # Register all primitives of the module.
            before = len(self._registry._reg)
            mod.register_primitives(self._registry, primitive_wrapper)
            self._logger.info('Got {} primitives from {}'.format(
                len(self._registry._reg) - before, modname))
            primitive_getter = lambda name: self._registry.get(name, variants[vname])
            # Define gradients of primitives.
            mod.def_grads(self._registry, primitive_getter)
        self._logger.info('Import {} primitives'.format(
            len(self._registry._reg)))

    def set_policy(self, plc):
        """Set name dispatch policy.

        Parameters
        ----------
        plc
            New policy.
        """
        assert isinstance(
            plc, Policy), 'Need an instance of `minpy.dispatch.policy.Policy`.'
        self._policy = plc

    @property
    def policy(self):
        """Get policy of current module"""
        return self._policy

    def __getattr__(self, name):
        """Fetch attributes from this module.

        If the name is contained in the primitive registry,
        it will return a primitive selector for further name dispatching.

        :param name: Name of attribute.
        :return: Primitive selector.
        :raises AttributeError: Cannot find attribute.
        """
        # Special members for internal use.
        if name == '__registry__':
            return self._registry
        elif name == '__all__':
            return self._old.__all__
        elif self._registry.has_name(name):
            return PrimitiveSelector(name, self._registry, self._policy)
        elif name in self._name_injector:
            return self._name_injector[name]
        elif name in self._old:
            self._logger.info(
                'No entry found for "{}" in registry, fallback.'.format(name))
            return self._old[name]
        else:
            raise AttributeError('Cannot find name "{}".'.format(name))
Пример #4
0
class Module(object):
    """Mocking module class for name dispatching.

    It will register primitives from :mod:`minpy.array_variant`.

    Parameters
    ----------
    old : dict
        A meta class including info such as name, path, etc.
    name : None, or str
        Second level name if specified.
    name_injector : dict, or dict-like object
        An optional dict provides manual dispatching
    """

    def __init__(self, old, name=None, name_injector=None):
        # pylint: disable= protected-access, cell-var-from-loop
        # Add module itself into global config
        minpy.Config['modules'].append(self)
        self._registry = Registry(old['__name__'])
        self._policy = minpy.Config['default_policy']
        self._logger = log.get_logger(old['__name__'])
        self._logger.info('Initialize module: %s.', old['__name__'])
        self._old = old
        self._name_injector = name_injector if name_injector else {}
        if len(self._name_injector) != 0:
            self._logger.info('Inject Name Injector %s', name_injector.__name__)
        for vname in variants:
            vtype = variants[vname]
            if name is None:
                modname = 'minpy.array_variants.{}'.format(vname)
            else:
                modname = 'minpy.array_variants.{}.{}'.format(vname, name)
            mod = importlib.import_module(modname)
            self._logger.info('Importing from %s.', modname)
            primitive_wrapper = lambda func, *args, **kwargs:\
                    Primitive(func, vtype, *args, **kwargs)
            # Register all primitives of the module.
            before = len(self._registry._reg)
            mod.register_primitives(self._registry, primitive_wrapper)
            self._logger.info('Got %d primitives from %s',
                              len(self._registry._reg) - before, modname)
            primitive_getter = lambda name: self._registry.get(name, vtype)
            # Define gradients of primitives.
            mod.def_grads(primitive_getter)
        self._logger.info('Import %d primitives', len(self._registry._reg))
        self._set_attrs()
        # pylint: enable= protected-access, cell-var-from-loop

    def set_policy(self, plc):
        """Set name dispatch policy.

        Parameters
        ----------
        plc
            New policy.
        """
        assert isinstance(
            plc, Policy), 'Need an instance of `minpy.dispatch.policy.Policy`.'
        self._policy = plc

    @property
    def policy(self):
        """Get policy of current module"""
        return self._policy

    def _set_attrs(self):
        """Set attributes for this module"""
        # The latter will override the former, so set attributes in reverse priority order
        for k, val in self._old.items():
            setattr(self, k, val)
        for k in self._name_injector.keys():
            setattr(self, k, self._name_injector[k])
        for k in self._registry._reg: # pylint: disable= protected-access
            fun = PrimitiveSelector(k, self)
            setattr(self, k, fun)
        if '__all__' in dir(self._old):
            setattr(self, '__all__', self._old.__all__)
        setattr(self, '__registry__', self._registry)
Пример #5
0
class Module(object):
    """Mocking module class for name dispatching.

    It will register primitives from :mod:`minpy.array_variant`.

    Parameters
    ----------
    old : dict
        A meta class including info such as name, path, etc.
    name : None, or str
        Second level name if specified.
    name_injector : dict, or dict-like object
        An optional dict provides manual dispatching
    """

    def __init__(self, old, name=None, name_injector=None):
        # pylint: disable= protected-access, cell-var-from-loop
        # Add module itself into global config
        minpy.Config['modules'].append(self)
        self._registry = Registry(old['__name__'])
        self._logger = log.get_logger(old['__name__'])
        self._logger.info('Initialize module: %s.', old['__name__'])
        self._old = old
        self._name_injector = name_injector if name_injector else {}
        if len(self._name_injector) != 0:
            self._logger.info('Inject Name Injector %s', name_injector.__name__)
        for vname in variants:
            vtype = variants[vname]
            if name is None:
                modname = 'minpy.array_variants.{}'.format(vname)
            else:
                modname = 'minpy.array_variants.{}.{}'.format(vname, name)
            mod = importlib.import_module(modname)
            self._logger.info('Importing from %s.', modname)
            primitive_wrapper = lambda func, *args, **kwargs:\
                    Primitive(func, vtype, *args, **kwargs)
            # Register all primitives of the module.
            before = len(self._registry._reg)
            mod.register_primitives(self._registry, primitive_wrapper)
            self._logger.info('Got %d primitives from %s',
                              len(self._registry._reg) - before, modname)
            primitive_getter = lambda name: self._registry.get(name, vtype)
            # Define gradients of primitives.
            mod.def_grads(primitive_getter)
        self._logger.info('Import %d primitives', len(self._registry._reg))
        self.generate_attrs(minpy.Config['default_policy'])
        # pylint: enable= protected-access, cell-var-from-loop

    def set_policy(self, policy):
        """[Deprecated] Set policy for this module."""
        self.generate_attrs(policy)

    def record_op_stat(self):
        """Record Op Calling Statistics"""
        self.generate_attrs(self.policy, True)

    def show_op_stat(self):
        """Show Op Calling Statistics"""
        self.policy.show_op_stat()

    def generate_attrs(self, policy, use_selector=False):
        """Generate attributes for this module"""
        self.policy = policy
        # The latter will override the former, so set attributes in reverse priority order
        for k, val in self._old.items():
            setattr(self, k, val)
        for k in self._name_injector.keys():
            setattr(self, k, self._name_injector[k])
        for k, prims in self._registry._reg.items(): # pylint: disable= protected-access
            if not use_selector:
                prim_type = self.policy.decide(prims.values())
                if prim_type is not None:
                    setattr(self, k, prims[prim_type])
            else:
                fun = PrimitiveSelector(k, self)
                setattr(self, k, fun)
        if '__all__' in dir(self._old):
            setattr(self, '__all__', self._old.__all__)
        setattr(self, '__registry__', self._registry)