Ejemplo n.º 1
0
    def CreateOperator(cls, inputs, op_type,
                       nout=None, existing_outputs=None, output_shapes=None,
                       extra_inputs=None, name=None, **kwargs):
        """Construct a new Tensor with specific operator descriptor.

        Parameters
        ----------
        inputs : list of Tensor or Tensor
            The inputs for this operator.
        op_type : str
            The operator type.
        nout : int
            The number of outputs to return.
            It will be discarded if ``existing_outputs`` is not None.
        existing_outputs : list of Tensor, Tensor or None
            The existing outputs for this operator.
        extra_inputs : list of Tensor, Tensor or None
            The inputs that should be attached to solving targets, e.g. dynamic shape.
        name : str or None
            The optional name to use. ``Op_xxx`` will be used automatically if it is None.

        Returns
        -------
        list of Tensor, Tensor, or None
            The outputs of this operator.

        Examples
        --------
        >>> a = Tensor().Variable()
        >>> b = Tensor().Variable()
        >>> c = Tensor.CreateOperator(inputs=[a, b], op_type='Add', nout=1)

        >>> a = Tensor().Variable()
        >>> b = Tensor().Variable()
        >>> c = Tensor().Variable()
        >>> c = Tensor.CreateOperator(inputs=[a, b], op_type='Add', existing_outputs=c)

        >>> import dragon.core.workspace as ws
        >>> import dragon.vm.theano as theano
        >>> dynamic_shape = Tensor().Variable()
        >>> ws.FeedTensor(dynamic_shape, [1, 2, 3, 4])
        >>> a = ops.Fill(shape=dynamic_shape, value=5.0)
        >>> print theano.function(outputs=a)
        >>> [[ 5.  5.  5.]
             [ 5.  5.  5.]]

        """
        expressions = OrderedDict()  # keep order for displaying

        # 1. collect inputs
        if not isinstance(inputs, list): inputs = [inputs]
        for input in inputs:
            for op_idx, expr in input.expressions.items():
                if not op_idx in expressions:
                    expressions[op_idx] = expr

        if extra_inputs is not None:
            if not isinstance(extra_inputs, list): extra_inputs = [extra_inputs]
            for input in extra_inputs:
                for op_idx, expr in input.expressions.items():
                    if not op_idx in expressions:
                        expressions[op_idx] = expr

        # 2. generate outputs
        outputs = []
        if existing_outputs is None:
            for idx in xrange(nout): outputs.append(Tensor())
        else:
            if not isinstance(existing_outputs, list): existing_outputs = [existing_outputs]
            outputs = existing_outputs
            nout = len(outputs)
            if not isinstance(outputs, list): outputs = [outputs]

        # 3. make def, then push expressions
        inputs_name = [input.name for input in inputs]
        outputs_name = [output.name for output in outputs]
        op_idx, op_name = GetOperatorName(name)
        device_option = None
        from dragon.core.scope import _DEVICE_SCOPE, _ENGINE_SCOPE
        if _DEVICE_SCOPE != '':
            supports = {'/cpu': 0, '/gpu': 1}
            device_option = pb.DeviceOption()
            device_option.device_type = supports[_DEVICE_SCOPE.split(':')[0]]
            device_option.gpu_id = int(_DEVICE_SCOPE.split(':')[1])
            device_option.engine = _ENGINE_SCOPE
        op_def = MakeOperatorDef(op_type, inputs_name, outputs_name, op_name,
                                 device_option=device_option, **kwargs)
        expressions[op_idx] = op_def

        # 4. make outputs
        for idx, output in enumerate(outputs):
            # deliver expressions
            output.expressions = expressions
            # deliver extra targets
            for input in inputs:
                output.extra_targets = \
                    output.extra_targets.union(input.extra_targets)
            if extra_inputs is not None:
                for input in extra_inputs:
                    output.extra_targets.add(input.name)

        # 5. utils
        if 'static_shape' in kwargs:
            outputs[0].tf_shape = kwargs['static_shape']

        # 6. returns
        if nout > 1: return outputs
        elif nout == 1: return outputs[0]
        else: return None
Ejemplo n.º 2
0
    def CreateOperator(cls,
                       inputs,
                       op_type,
                       nout=None,
                       existing_outputs=None,
                       output_shapes=None,
                       extra_inputs=None,
                       name=None,
                       **kwargs):
        """Construct a new Tensor with specific operator descriptor.

        Parameters
        ----------
        inputs : list of Tensor or Tensor
            The inputs for this operator.
        op_type : str
            The operator type.
        nout : int
            The number of outputs to return.
            It will be discarded if ``existing_outputs`` is not None.
        existing_outputs : list of Tensor, Tensor or None
            The existing outputs for this operator.
        extra_inputs : list of Tensor, Tensor or None
            The inputs that should be attached to solving targets, e.g. dynamic shape.
        name : str or None
            The optional name to use. ``Op_xxx`` will be used automatically if it is None.

        Returns
        -------
        list of Tensor, Tensor, or None
            The outputs of this operator.

        Examples
        --------
        >>> a = Tensor().Variable()
        >>> b = Tensor().Variable()
        >>> c = Tensor.CreateOperator(inputs=[a, b], op_type='Add', nout=1)

        >>> a = Tensor().Variable()
        >>> b = Tensor().Variable()
        >>> c = Tensor().Variable()
        >>> c = Tensor.CreateOperator(inputs=[a, b], op_type='Add', existing_outputs=c)

        >>> import dragon.core.workspace as ws
        >>> import dragon.vm.theano as theano
        >>> dynamic_shape = Tensor().Variable()
        >>> ws.FeedTensor(dynamic_shape, [1, 2, 3, 4])
        >>> a = ops.Fill(shape=dynamic_shape, value=5.0)
        >>> print theano.function(outputs=a)
        >>> [[ 5.  5.  5.]
             [ 5.  5.  5.]]

        """
        expressions = OrderedDict()  # keep order for displaying

        # 1. collect inputs
        if not isinstance(inputs, list): inputs = [inputs]
        for input in inputs:
            for op_idx, expr in input.expressions.items():
                if not op_idx in expressions:
                    expressions[op_idx] = expr

        if extra_inputs is not None:
            if not isinstance(extra_inputs, list):
                extra_inputs = [extra_inputs]
            for input in extra_inputs:
                for op_idx, expr in input.expressions.items():
                    if not op_idx in expressions:
                        expressions[op_idx] = expr

        # 2. generate outputs
        outputs = []
        if existing_outputs is None:
            for idx in range(nout):
                outputs.append(Tensor())
        else:
            if not isinstance(existing_outputs, list):
                existing_outputs = [existing_outputs]
            outputs = existing_outputs
            nout = len(outputs)
            if not isinstance(outputs, list): outputs = [outputs]

        # 3. make def, then push expressions
        inputs_name = [input.name for input in inputs]
        outputs_name = [output.name for output in outputs]
        op_idx, op_name = GetOperatorName(name)
        device_option = None
        from dragon.core.scope import _DEVICE_SCOPE, _ENGINE_SCOPE
        if _DEVICE_SCOPE != '':
            supports = {'/cpu': 0, '/gpu': 1}
            device_option = pb.DeviceOption()
            device_option.device_type = supports[_DEVICE_SCOPE.split(':')[0]]
            device_option.gpu_id = int(_DEVICE_SCOPE.split(':')[1])
            device_option.engine = _ENGINE_SCOPE
        op_def = MakeOperatorDef(op_type,
                                 inputs_name,
                                 outputs_name,
                                 op_name,
                                 device_option=device_option,
                                 **kwargs)
        expressions[op_idx] = op_def

        # 4. make outputs
        for idx, output in enumerate(outputs):
            # deliver expressions
            output.expressions = expressions
            # deliver extra targets
            for input in inputs:
                output.extra_targets = \
                    output.extra_targets.union(input.extra_targets)
            if extra_inputs is not None:
                for input in extra_inputs:
                    output.extra_targets.add(input.name)

        # 5. utils
        if 'static_shape' in kwargs:
            outputs[0].tf_shape = kwargs['static_shape']

        # 6. returns
        if nout > 1: return outputs
        elif nout == 1: return outputs[0]
        else: return None
Ejemplo n.º 3
0
def FeedTensor(tensor, array, force_cpu=False, dtype=None):
    """Feed the values to the given tensor.

    Parameters
    ----------
    tensor : Tensor or str
        The tensor to feed.
    ndarray : number, list or ndarray
        The values to feed.
    force_cpu : boolean
        Whether force to feed to cpu context.
    dtype : str
        The data type. If ``None``, ``float32`` will be used instead.

    Returns
    -------
    None

    Examples
    --------
    >>> import dragon as dg
    >>> a = dg.Tensor().Variable()
    >>> dg.workspace.FeedTensor(a, 1)
    >>> a_value = dg.workspace.FetchTensor(a)
    >>> a_value, a_value.dtype
    >>> [ 1.], float32

    >>> dg.workspace.FeedTensor(a, [[1, 2, 3]], dtype='float16')
    >>> a_value = a.get_value()
    >>> a_value, a_value.dtype
    >>> [[ 1.  2.  3.]], float16

    References
    ----------
    The wrapper of ``FeedTensorCC``.

    """
    name = tensor.name if hasattr(tensor, 'name') else str(tensor)
    if force_cpu is True:
        dev = utils.MakeDeviceOption(0, 0)
    else:
        from dragon.core.scope import _DEVICE_SCOPE
        if _DEVICE_SCOPE != '':
            supports = {'/cpu': 0, '/gpu': 1, '/mlu': 2}
            dev = pb.DeviceOption()
            dev.device_type = supports[_DEVICE_SCOPE.split(':')[0]]
            dev.device_id = int(_DEVICE_SCOPE.split(':')[1])
        else:
            from dragon.config import option
            if option['device'] == 'CUDA':
                dev = utils.MakeDeviceOption(1, option['device_id'])
            else:
                dev = utils.MakeDeviceOption(0, 0)

    if not isinstance(array, np.ndarray):
        if not isinstance(array, list):
            array = [array]
        auto_data_type = np.float32 if dtype is None else dtype
    else:
        auto_data_type = array.dtype if dtype is None else dtype

    if hasattr(tensor, 'dtype') and tensor.dtype is not None:
        if tensor.dtype not in _DATA_TYPES:
            raise TypeError('Unsupported data types: {}.'.format(tensor.dtype))
        preset_data_type = _DATA_TYPES[tensor.dtype]
        if dtype is not None:
            if dtype != preset_data_type:
                raise TypeError(
                    'The preset data type is {}, but force to {}.'.format(
                        preset_data_type, dtype))
        auto_data_type = preset_data_type
    nd_array = np.array(array, dtype=auto_data_type, copy=False)
    FeedTensorCC(name, nd_array, _stringify_proto(dev))