Пример #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
 def __init__(self,
              dest_mod,
              name='',
              injected_type=None,
              name_set=None,
              exception=None):
     if len(name) != 0:
         name = '<' + name + '>'
     self.__name__ = name
     self._logger = log.get_logger(__name__)
     self._logger.info('Initialize Name Injector {}'.format(self.__name__))
     self._name_dict = {}
     dest_ns = dest_mod.__dict__
     if injected_type is not None:
         for name, obj in dest_ns.items():
             if type(obj) in injected_type:
                 self._name_dict[name] = obj
                 self._logger.debug(
                     'Add {} from {} to Name Injector {}'.format(
                         name, dest_mod.__name__, self.__name__))
     if name_set is not None:
         for name in name_set:
             self._name_dict[name] = dest_ns[name]
             self._logger.debug(
                 'Add {} from {}i to Name Injector {}'.format(
                     name, dest_mod.__name__, self.__name__))
     if exception is not None:
         exception = {k: dest_ns[v] for k, v in exception.items()}
         self._logger.debug('Update {} exceptions to Name Injector {}'.
                            format(len(exception), self.__name__))
         self._name_dict.update(exception)
     self._logger.info('Import {} objects from {} to Name Injector {}'.
                       format(len(self), dest_mod.__name__, self.__name__))
Пример #4
0
 def __init__(self,
              dest_mod,
              name='',
              injected_type=None,
              name_set=None,
              exception=None):
     # pylint: disable= too-many-arguments
     if len(name) != 0:
         name = '<' + name + '>'
     self.__name__ = name
     self._logger = log.get_logger(__name__)
     self._logger.info('Initialize Name Injector %s', self.__name__)
     self._name_dict = {}
     dest_ns = dest_mod.__dict__
     if injected_type is not None:
         for name, obj in dest_ns.items():
             if type(obj) in injected_type: # pylint: disable= unidiomatic-typecheck
                 self._name_dict[name] = obj
                 self._logger.debug(
                     'Add %s from %s to Name Injector %s',
                     name, dest_mod.__name__, self.__name__)
     if name_set is not None:
         for name in name_set:
             self._name_dict[name] = dest_ns[name]
             self._logger.debug(
                 'Add %s from %s to Name Injector %s',
                 name, dest_mod.__name__, self.__name__)
     if exception is not None:
         exception = {k: dest_ns[v] for k, v in exception.items()}
         self._logger.debug('Update %d exceptions to Name Injector %s',
                            len(exception), self.__name__)
         self._name_dict.update(exception)
     self._logger.info('Import %d objects from %s to Name Injector %s',
                       len(self), dest_mod.__name__, self.__name__)
Пример #5
0
 def __init__(self, old, name=None):
     # 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
     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)))
Пример #6
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._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'])
Пример #7
0
 def __init__(self,
              dest_mod,
              name='',
              injected_type=None,
              name_set=None,
              exception=None):
     # pylint: disable= too-many-arguments
     if len(name) != 0:
         name = '<' + name + '>'
     self.__name__ = name
     self._logger = log.get_logger(__name__)
     self._logger.info('Initialize Name Injector %s', self.__name__)
     self._name_dict = {}
     dest_ns = dest_mod.__dict__
     if injected_type is not None:
         for name, obj in dest_ns.items():
             if type(obj) in injected_type: # pylint: disable= unidiomatic-typecheck
                 self._name_dict[name] = obj
                 self._logger.debug(
                     'Add %s from %s to Name Injector %s',
                     name, dest_mod.__name__, self.__name__)
     if name_set is not None:
         for name in name_set:
             self._name_dict[name] = dest_ns[name]
             self._logger.debug(
                 'Add %s from %s to Name Injector %s',
                 name, dest_mod.__name__, self.__name__)
     if exception is not None:
         exception = {k: dest_ns[v] for k, v in exception.items()}
         self._logger.debug('Update %d exceptions to Name Injector %s',
                            len(exception), self.__name__)
         self._name_dict.update(exception)
     self._logger.info('Import %d objects from %s to Name Injector %s',
                       len(self), dest_mod.__name__, self.__name__)
Пример #8
0
 def __init__(self,
              dest_mod,
              name='',
              injected_type=None,
              name_set=None,
              exception=None):
     if len(name) != 0:
         name = '<' + name + '>'
     self.__name__ = name
     self._logger = log.get_logger(__name__)
     self._logger.info('Initialize Name Injector {}'.format(self.__name__))
     self._name_dict = {}
     dest_ns = dest_mod.__dict__
     if injected_type is not None:
         for name, obj in dest_ns.items():
             if type(obj) in injected_type:
                 self._name_dict[name] = obj
                 self._logger.debug(
                     'Add {} from {} to Name Injector {}'.format(
                         name, dest_mod.__name__, self.__name__))
     if name_set is not None:
         for name in name_set:
             self._name_dict[name] = dest_ns[name]
             self._logger.debug(
                 'Add {} from {}i to Name Injector {}'.format(
                     name, dest_mod.__name__, self.__name__))
     if exception is not None:
         exception = {k: dest_ns[v] for k, v in exception.items()}
         self._logger.debug('Update {} exceptions to Name Injector {}'.
                            format(len(exception), self.__name__))
         self._name_dict.update(exception)
     self._logger.info('Import {} objects from {} to Name Injector {}'.
                       format(len(self), dest_mod.__name__, self.__name__))
Пример #9
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable= no-self-use
"""Policy for selecting appropriate function to call."""
from __future__ import absolute_import
from __future__ import print_function

from minpy.array import Value
from minpy.array_variants import ArrayType
from minpy.utils import log

# pylint: disable= invalid-name
_logger = log.get_logger(__name__)
# pylint: enable= invalid-name


class PrimitivePolicyError(ValueError):
    """Error during choosing primitives."""
    pass


class Policy(object):
    """Policy interface."""
    def decide(self, candidates, args, kwargs):
        """Primitive decision policy interface.

        :param list candidates: A list of primitive objects.
        :param list args: The positional arguments passed to the primitive.
        :param dict kwargs: The keyword arguments passed to the primitive.
        :return: Which implementation type will be used.
        """
Пример #10
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#pylint: disable= invalid-name
"""Lazy evaluation of primitive selection."""
from __future__ import absolute_import
from __future__ import print_function

from minpy.utils import log
from minpy.dispatch import policy

_logger = log.get_logger(__name__)


class PrimitiveSelector(object):
    """Primitive selector class that behaves like normal function but instead pass all the
    arguments to the policy to choose appropriate primitive.
    """
    __slots__ = ['_name', '_registry', '_policy']

    def __init__(self, name, reg, plc):
        self._name = name
        self._registry = reg
        self._policy = plc

    @property
    def name(self):
        """Get the name of this function.

        :return: Name of function.
        """
        return self._name
Пример #11
0
"""Rules for rule based policy"""
from __future__ import absolute_import
from __future__ import print_function

import os
import atexit
import yaml
import numpy
from minpy.array_variants import ArrayType
from minpy.array import Array
from minpy.array import Number
from minpy.utils import log

_logger = log.get_logger(__name__)  # pylint: disable=invalid-name

# TODO: integrate this part into normal routine when MXNet fixes exception in
# Python.
# Currently MXNet doesn't throw exception raised in mshadow to Python. Avoid
# them by specifying a handcraft whitelist.
MXNET_SUPPORT_TYPES = {'float', 'float16', 'float32'}
MXNET_TYPE_COMPATIBLE_OPS = {
    'negative', 'add', 'subtract', 'multiply', 'divide', 'true_divide', 'mod',
    'power'
}
# These are MXNet ops that introduces potential issues for further computation.
MXNET_BLACKLIST_OPS = {'array'}


class RuleError(ValueError):
    """Error in rule processing"""
    pass
Пример #12
0
"""Rules for rule based policy"""
from __future__ import absolute_import
from __future__ import print_function

import os
import atexit
import yaml
import numpy
from minpy.array_variants import ArrayType
from minpy.array import Array
from minpy.array import Number
from minpy.utils import log

_logger = log.get_logger(__name__)  # pylint: disable=invalid-name

# TODO: integrate this part into normal routine when MXNet fixes exception in
# Python.
# Currently MXNet doesn't throw exception raised in mshadow to Python. Avoid
# them by specifying a handcraft whitelist.
MXNET_SUPPORT_TYPES = {'float', 'float16', 'float32'}
MXNET_TYPE_COMPATIBLE_OPS = {
    'negative', 'add', 'subtract', 'multiply', 'divide', 'true_divide', 'mod',
    'power'
}
# These are MXNet ops that introduces potential issues for further computation.
MXNET_BLACKLIST_OPS = {'array'}


class RuleError(ValueError):
    """Error in rule processing"""
    pass