Ejemplo n.º 1
0
def _to_full_tensor(elem, device_num, global_rank, scaling_sens=None):
    """Convert numpy to tensor, expanding batch dimension according to device_num, adapt to feed the data
       from host solution."""
    lst = []
    if not isinstance(elem, (tuple, list)):
        elem = [elem]
    if global_rank >= device_num:
        raise ValueError(
            "The global rank must be smaller than device number, the global rank is {}, "
            "the device num is {}".format(global_rank, device_num))

    for data in elem:
        if isinstance(data, np.ndarray):
            data = Tensor(data)
        if not isinstance(data, Tensor):
            raise ValueError("elements in tensors must be Tensor")
        shape_ = data.shape
        type_ = data.dtype
        new_shape = ()
        batchsize_per_device = 1
        for i, item in enumerate(shape_):
            if i == 0:
                new_shape += (item * device_num, )
                batchsize_per_device = item
            else:
                new_shape += (item, )
        new_tensor_numpy = np.zeros(new_shape, dtype_to_nptype(type_))
        start = global_rank * batchsize_per_device
        new_tensor_numpy[start:start + batchsize_per_device] = data.asnumpy()
        new_tensor = Tensor(new_tensor_numpy)
        lst.append(new_tensor)
    if scaling_sens:
        lst.append(Tensor(scaling_sens, mstype.float32))
    return tuple(lst)
Ejemplo n.º 2
0
def _construct_tensor_list(types, shapes, batch_expand_num=1):
    """
    Construct list of tensors with types and shapes, used to initialize the network.

    Args:
        types: List or Tuple. The output types of element in dataset.
        shapes: List or Tuple. The output shapes of element in dataset.
        batch_expand_num (int): Batch expand number.

    Returns:
        List, list of Tensors.
    """
    if len(types) != len(shapes):
        raise ValueError(
            "The length of dataset types must equal to dataset shapes, "
            "but got dataset types={} and dataset shapes={}".format(
                types, shapes))
    tensor_list = []
    for type_, shape in zip(types, shapes):
        new_shape = ()
        for i, item in enumerate(shape):
            if i == 0:
                new_shape += (item * batch_expand_num, )
            else:
                new_shape += (item, )
        tensor = Tensor(np.zeros(new_shape, dtype_to_nptype(type_)))
        tensor.virtual_flag = True
        tensor_list.append(tensor)
    return tensor_list
Ejemplo n.º 3
0
 def vm_impl(x, t):
     if isinstance(t, type(mstype.tensor)):
         t = t.element_type()
     # update the src type
     x = x.asnumpy()
     out = x.astype(mstype.dtype_to_nptype(t))
     return Tensor(out)
Ejemplo n.º 4
0
def _get_dtype_max(dtype):
    """get max of the dtype"""
    np_type = mstype.dtype_to_nptype(dtype)
    if issubclass(np_type, numbers.Integral):
        dtype_max = np.float64(np.iinfo(np_type).max)
    else:
        dtype_max = 1.0
    return dtype_max
Ejemplo n.º 5
0
    def __init__(self, src_type=mstype.float32, dst_type=mstype.float32):
        super(SaturateCast, self).__init__()
        np_type = mstype.dtype_to_nptype(dst_type)

        self.tensor_min_type = float(np.finfo(np_type).min)
        self.tensor_max_type = float(np.finfo(np_type).max)

        self.min_op = P.Minimum()
        self.max_op = P.Maximum()
        self.cast = P.Cast()
        self.dst_type = dst_type
Ejemplo n.º 6
0
 def vm_impl(x, t):
     np_type = dtype_to_nptype(t)
     value = np_type(x)
     cast_value = value.item()
     return cast_value
Ejemplo n.º 7
0
 def vm_impl(n, m, t):
     np_type = mstype.dtype_to_nptype(t)
     ret = np.eye(n, m, dtype=np_type)
     return Tensor(ret)
Ejemplo n.º 8
0
def scalar_cast(x, t):
    """Implement scalar_cast."""
    np_type = dtype_to_nptype(t)
    value = np_type(x)
    cast_value = np.ndarray.item(value)
    return cast_value