|
|
|
@ -145,41 +145,6 @@ def _to_tensor(elem, scaling_sens=None):
|
|
|
|
|
return lst[0] if len(lst) == 1 else tuple(lst)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _construct_input_tensors(dataset_types, dataset_shapes, device_number=1):
|
|
|
|
|
"""Construct tensor list to initialize the network which implemented in dataset sink."""
|
|
|
|
|
tensor_list_run = _construct_tensor_list(dataset_types, dataset_shapes, batch_expand_num=1)
|
|
|
|
@ -187,20 +152,6 @@ def _construct_input_tensors(dataset_types, dataset_shapes, device_number=1):
|
|
|
|
|
return tensor_list_run, tensor_list_compile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _to_full_shapes(shapes, device_num):
|
|
|
|
|
"""Expanding batch dimension according to device_num, adapt to mindspore minddata graph solution."""
|
|
|
|
|
new_shapes = []
|
|
|
|
|
for shape in shapes:
|
|
|
|
|
new_shape = ()
|
|
|
|
|
for i, item in enumerate(shape):
|
|
|
|
|
if i == 0:
|
|
|
|
|
new_shape += (item * device_num,)
|
|
|
|
|
else:
|
|
|
|
|
new_shape += (item,)
|
|
|
|
|
new_shapes.append(new_shape)
|
|
|
|
|
return new_shapes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _check_to_numpy(plugin, tensor):
|
|
|
|
|
"""Check the tensor and return a numpy.ndarray."""
|
|
|
|
|
np_value = tensor.asnumpy()
|
|
|
|
|