forked from mindspore-Ecosystem/mindspore
replace set_inputs with set_dynamic_columns
This commit is contained in:
parent
8a2e3ddd54
commit
b0b2b8fd27
|
@ -14,10 +14,12 @@
|
|||
# ==============================================================================
|
||||
import numpy as np
|
||||
import pytest
|
||||
from mindspore import nn, context
|
||||
from mindspore import nn, context, Tensor
|
||||
from mindspore import ops as P
|
||||
from mindspore.train import DatasetHelper, connect_network_with_dataset
|
||||
import mindspore.dataset as ds
|
||||
import mindspore as ms
|
||||
from mindspore.common.initializer import One
|
||||
|
||||
|
||||
def _exec_preprocess(network, is_train, dataset, dataset_sink_mode, sink_size=1, epoch_num=1, dataset_helper=None):
|
||||
|
@ -86,8 +88,13 @@ def test_getnext_dynamic_pipeline_ascend():
|
|||
network = Net()
|
||||
dataset = ds.GeneratorDataset(
|
||||
dataset_generator, ["data1", "data2", "data3", "data4", "data5"])
|
||||
dataset.set_dynamic_columns(columns={"data1": [32, None], "data2": [32, None, None, 3],
|
||||
"data3": [32], "data4": [32, None, 8], "data5": [32, 8, 8]})
|
||||
|
||||
t0 = Tensor(dtype=ms.float32, shape=[32, None])
|
||||
t1 = Tensor(dtype=ms.int32, shape=[32, None, None, 3])
|
||||
t2 = Tensor(dtype=ms.float32, shape=[32], init=One())
|
||||
t3 = Tensor(dtype=ms.float32, shape=[32, None, 8])
|
||||
t4 = Tensor(dtype=ms.float32, shape=[32, 8, 8], init=One())
|
||||
network.set_inputs(t0, t1, t2, t3, t4)
|
||||
_eval_dataset_sink_process(network, dataset)
|
||||
|
||||
|
||||
|
@ -100,8 +107,13 @@ def test_getnext_sink_size_dynamic_pipeline():
|
|||
network = Net()
|
||||
dataset = ds.GeneratorDataset(
|
||||
dataset_generator, ["data1", "data2", "data3", "data4", "data5"])
|
||||
dataset.set_dynamic_columns(columns={"data1": [32, None], "data2": [32, None, None, 3],
|
||||
"data3": [32], "data4": [32, None, 8], "data5": [32, 8, 8]})
|
||||
|
||||
t0 = Tensor(dtype=ms.float32, shape=[32, None])
|
||||
t1 = Tensor(dtype=ms.int32, shape=[32, None, None, 3])
|
||||
t2 = Tensor(dtype=ms.float32, shape=[32], init=One())
|
||||
t3 = Tensor(dtype=ms.float32, shape=[32, None, 8])
|
||||
t4 = Tensor(dtype=ms.float32, shape=[32, 8, 8], init=One())
|
||||
network.set_inputs(t0, t1, t2, t3, t4)
|
||||
|
||||
dataset_helper, eval_network = _exec_preprocess(
|
||||
network, is_train=False, dataset=dataset, dataset_sink_mode=True, sink_size=-1)
|
||||
|
|
|
@ -113,12 +113,25 @@ def comm_func(dyn_range, input_shp, data_type, op_net, num=None):
|
|||
list_data.append(tuple(tmp_data))
|
||||
|
||||
data_map = {}
|
||||
dyn_tensors = []
|
||||
|
||||
def np_type_to_ms(data_type):
|
||||
if data_type == np.float32:
|
||||
return mstype.float32
|
||||
if data_type == np.float64:
|
||||
return mstype.float64
|
||||
if data_type == np.int32:
|
||||
return mstype.int32
|
||||
if data_type == np.int64:
|
||||
return mstype.int64
|
||||
raise ValueError("Unsupportted datatype: {}".format(data_type))
|
||||
|
||||
for i, val in enumerate(input_shp):
|
||||
data_map["data" + str(i + 1)] = val
|
||||
dyn_tensors.append(Tensor(dtype=np_type_to_ms(data_type), shape=val))
|
||||
|
||||
dataset = ds.GeneratorDataset(list_data, list(data_map.keys()))
|
||||
dataset.set_dynamic_columns(columns=data_map)
|
||||
|
||||
op_net.set_inputs(*dyn_tensors)
|
||||
gradient = dynamic_sink_process(op_net, dataset)
|
||||
gradient_cmp = static_process(op_net, dataset)
|
||||
assert compare_acc(gradient, gradient_cmp)
|
||||
|
|
|
@ -21,6 +21,7 @@ from mindspore.ops.operations import _inner_ops as inner
|
|||
|
||||
from mindspore.train import DatasetHelper, connect_network_with_dataset
|
||||
import mindspore.dataset as ds
|
||||
from mindspore.common.initializer import One
|
||||
|
||||
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
|
||||
set_seed(2)
|
||||
|
@ -94,13 +95,29 @@ def common_func(dynamic_range, input_shape, data_type, op_net):
|
|||
cur_data.append(np.random.random(cur_shape).astype(data_type))
|
||||
data_list.append(tuple(cur_data))
|
||||
|
||||
def np_type_to_ms(data_type):
|
||||
if data_type == np.float32:
|
||||
return ms.float32
|
||||
if data_type == np.float64:
|
||||
return ms.float64
|
||||
if data_type == np.int32:
|
||||
return ms.int32
|
||||
if data_type == np.int64:
|
||||
return ms.int64
|
||||
raise ValueError("Unsupportted datatype: {}".format(data_type))
|
||||
|
||||
dynamic_data_map = {}
|
||||
dyn_tensors = []
|
||||
for i, val in enumerate(input_shape):
|
||||
dynamic_data_map["data" + str(i + 1)] = val
|
||||
if None in val:
|
||||
dyn_tensors.append(Tensor(dtype=np_type_to_ms(data_type), shape=val))
|
||||
else:
|
||||
dyn_tensors.append(Tensor(dtype=np_type_to_ms(data_type), shape=val, init=One()))
|
||||
|
||||
dataset = ds.GeneratorDataset(data_list, list(dynamic_data_map.keys()))
|
||||
dataset.set_dynamic_columns(columns=dynamic_data_map)
|
||||
net = GradNetWrtX(op_net)
|
||||
net.set_inputs(*dyn_tensors)
|
||||
|
||||
gradients = dynamic_shape_sink_process(net, dataset)
|
||||
gradients_cmp = fixed_shape_process(net, dataset)
|
||||
|
@ -161,6 +178,7 @@ class ShapeTensorNet(nn.Cell):
|
|||
self.mul = ops.Mul()
|
||||
self.broadcast_to = inner.DynamicBroadcastTo()
|
||||
self.tensor_scatter_update = ops.TensorScatterUpdate()
|
||||
|
||||
def construct(self, x, y):
|
||||
res = self.tensor_shape(x)
|
||||
res = self.strided_slice(res, (1,), (4,), (1,))
|
||||
|
@ -261,8 +279,10 @@ def test_dynamic_dropout():
|
|||
np.random.rand(batch_size, i, 256).astype(np.float32)))
|
||||
|
||||
dataset = ds.GeneratorDataset(data_list, ["data1", "data2"])
|
||||
dataset.set_dynamic_columns(columns={"data1": [batch_size, None, 256], "data2": [batch_size, None, 256]})
|
||||
t0 = Tensor(dtype=ms.float32, shape=[batch_size, None, 256])
|
||||
t1 = Tensor(dtype=ms.float32, shape=[batch_size, None, 256])
|
||||
net = GradNetWrtX(DropoutNet())
|
||||
net.set_inputs(t0, t1)
|
||||
net.set_train()
|
||||
gradients = dynamic_shape_sink_process(net, dataset)
|
||||
assert gradients[0][0].shape == (batch_size, 49, 256)
|
||||
|
@ -285,9 +305,10 @@ def test_dynamic_reducesum1():
|
|||
np.array(1).astype(np.float32)))
|
||||
|
||||
dataset = ds.GeneratorDataset(data_list, ["data1", "data2"])
|
||||
dataset.set_dynamic_columns(columns={"data1": [batch_size, None, None], "data2": []})
|
||||
t0 = Tensor(dtype=ms.float32, shape=[batch_size, None, None])
|
||||
t1 = Tensor(dtype=ms.float32, shape=[], init=One())
|
||||
net = GradNetWrtX(ReduceSumNet())
|
||||
|
||||
net.set_inputs(t0, t1)
|
||||
gradients = dynamic_shape_sink_process(net, dataset)
|
||||
assert gradients[0][0].shape == (batch_size, 49, 51)
|
||||
|
||||
|
@ -309,9 +330,11 @@ def test_dynamic_reducesum2():
|
|||
np.random.rand(batch_size, i + 2).astype(np.float32)))
|
||||
|
||||
dataset = ds.GeneratorDataset(data_list, ["data1", "data2"])
|
||||
dataset.set_dynamic_columns(columns={"data1": [batch_size, None, None], "data2": [batch_size, None]})
|
||||
net = GradNetWrtX(ReduceSumNet(1))
|
||||
|
||||
t0 = Tensor(dtype=ms.float32, shape=[batch_size, None, None])
|
||||
t1 = Tensor(dtype=ms.float32, shape=[batch_size, None])
|
||||
net.set_inputs(t0, t1)
|
||||
gradients = dynamic_shape_sink_process(net, dataset)
|
||||
gradients_cmp = fixed_shape_process(net, dataset)
|
||||
assert compare(gradients, gradients_cmp)
|
||||
|
@ -335,9 +358,12 @@ def test_dynamic_add1():
|
|||
np.random.rand(batch_size, i).astype(np.float32)))
|
||||
|
||||
dataset = ds.GeneratorDataset(data_list, ["data1", "data2", "data3"])
|
||||
dataset.set_dynamic_columns(columns={"data1": [batch_size, None], "data2": [], "data3": [batch_size, None]})
|
||||
net = GradNetWrtX(AddNet())
|
||||
|
||||
t0 = Tensor(dtype=ms.float32, shape=[batch_size, None])
|
||||
t1 = Tensor(dtype=ms.float32, shape=[], init=One())
|
||||
t2 = Tensor(dtype=ms.float32, shape=[batch_size, None])
|
||||
net.set_inputs(t0, t1, t2)
|
||||
gradients = dynamic_shape_sink_process(net, dataset)
|
||||
gradients_cmp = fixed_shape_process(net, dataset)
|
||||
assert compare(gradients, gradients_cmp)
|
||||
|
@ -362,10 +388,12 @@ def test_dynamic_add2():
|
|||
np.random.rand(batch_size, 2, i).astype(np.float32)))
|
||||
|
||||
dataset = ds.GeneratorDataset(data_list, ["data1", "data2", "data3"])
|
||||
dataset.set_dynamic_columns(columns=
|
||||
{"data1": [batch_size, 2, None], "data2": [2, None], "data3": [batch_size, 2, None]})
|
||||
net = GradNetWrtX(AddNet())
|
||||
|
||||
t0 = Tensor(dtype=ms.float32, shape=[batch_size, 2, None])
|
||||
t1 = Tensor(dtype=ms.float32, shape=[2, None])
|
||||
t2 = Tensor(dtype=ms.float32, shape=[batch_size, 2, None])
|
||||
net.set_inputs(t0, t1, t2)
|
||||
gradients = dynamic_shape_sink_process(net, dataset)
|
||||
gradients_cmp = fixed_shape_process(net, dataset)
|
||||
assert compare(gradients, gradients_cmp)
|
||||
|
@ -448,9 +476,11 @@ def test_dynamic_square_sum_all():
|
|||
np.random.rand(batch_size, i).astype(np.float32)))
|
||||
|
||||
dataset = ds.GeneratorDataset(data_list, ["data1", "data2"])
|
||||
dataset.set_dynamic_columns(columns={"data1": [batch_size, None], "data2": [batch_size, None]})
|
||||
net = SquareSumAllNet()
|
||||
|
||||
t0 = Tensor(dtype=ms.float32, shape=[batch_size, None])
|
||||
t1 = Tensor(dtype=ms.float32, shape=[batch_size, None])
|
||||
net.set_inputs(t0, t1)
|
||||
out = dynamic_shape_sink_process(net, dataset)
|
||||
out_expect = fixed_shape_process(net, dataset)
|
||||
assert compare(out, out_expect)
|
||||
|
@ -513,10 +543,11 @@ def test_dynamic_reshape():
|
|||
np.random.rand(32, 16, 4, i).astype(np.float32)))
|
||||
|
||||
dataset = ds.GeneratorDataset(data_list, ["data1", "data2"])
|
||||
dataset.set_dynamic_columns(columns=
|
||||
{"data1": [32, 16, 4, None], "data2": [32, 16, 4, None]})
|
||||
net = ReshapeNet()
|
||||
net.add_flags_recursive(defer_inline=True)
|
||||
grad_net = GradNetWrtX(net)
|
||||
t0 = Tensor(dtype=ms.float32, shape=[32, 16, 4, None])
|
||||
t1 = Tensor(dtype=ms.float32, shape=[32, 16, 4, None])
|
||||
grad_net.set_inputs(t0, t1)
|
||||
gradients = dynamic_shape_sink_process(grad_net, dataset)
|
||||
print(gradients)
|
||||
|
|
|
@ -18,7 +18,8 @@ import mindspore.context as context
|
|||
import mindspore.nn as nn
|
||||
import mindspore.dataset as ds
|
||||
from mindspore.ops import operations as P
|
||||
from mindspore import Model
|
||||
from mindspore import Model, Tensor
|
||||
import mindspore as ms
|
||||
|
||||
context.set_context(mode=context.GRAPH_MODE,
|
||||
device_target="Ascend")
|
||||
|
@ -53,6 +54,8 @@ def test_shape():
|
|||
"""
|
||||
network = Net()
|
||||
dataset = ds.GeneratorDataset(dataset_generator, ["data1", "data2"])
|
||||
dataset.set_dynamic_columns(columns={"data1": [32, None], "data2": [32, None]})
|
||||
t0 = Tensor(dtype=ms.float32, shape=[32, None])
|
||||
t1 = Tensor(dtype=ms.float32, shape=[32, None])
|
||||
network.set_inputs(t0, t1)
|
||||
model = Model(network)
|
||||
model.train(1, dataset, sink_size=1)
|
||||
|
|
|
@ -16,14 +16,28 @@
|
|||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from mindspore import ops, nn, ParameterTuple, context, set_seed
|
||||
from mindspore import ops, nn, ParameterTuple, context, set_seed, Tensor
|
||||
from mindspore.train import DatasetHelper, connect_network_with_dataset
|
||||
import mindspore.dataset as ds
|
||||
import mindspore as ms
|
||||
from mindspore.common.initializer import One
|
||||
|
||||
context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
|
||||
set_seed(2)
|
||||
|
||||
|
||||
def np_type_to_ms(data_type):
|
||||
if data_type == np.float32:
|
||||
return ms.float32
|
||||
if data_type == np.float64:
|
||||
return ms.float64
|
||||
if data_type == np.int32:
|
||||
return ms.int32
|
||||
if data_type == np.int64:
|
||||
return ms.int64
|
||||
raise ValueError("Unsupportted datatype: {}".format(data_type))
|
||||
|
||||
|
||||
def _exec_preprocess(network, is_train, dataset, dataset_sink_mode, epoch_num, sink_size):
|
||||
if dataset_sink_mode and not is_train:
|
||||
dataset.__loop_size__ = 1
|
||||
|
@ -115,11 +129,16 @@ def comm_func(dyn_range, input_shp, data_type, op_net, num=None, output_compare_
|
|||
list_data.append(tuple(tmp_data))
|
||||
|
||||
data_map = {}
|
||||
dyn_tensors = []
|
||||
for i, val in enumerate(input_shp):
|
||||
data_map["data" + str(i + 1)] = val
|
||||
if None in val:
|
||||
dyn_tensors.append(Tensor(dtype=np_type_to_ms(data_type), shape=val))
|
||||
else:
|
||||
dyn_tensors.append(Tensor(dtype=np_type_to_ms(data_type), shape=val, init=One()))
|
||||
|
||||
dataset = ds.GeneratorDataset(list_data, list(data_map.keys()))
|
||||
dataset.set_dynamic_columns(columns=data_map)
|
||||
op_net.set_inputs(*dyn_tensors)
|
||||
|
||||
gradient = dynamic_shape_sink_process(op_net, dataset)
|
||||
gradient_cmp = fixed_shape_process(op_net, dataset)
|
||||
|
@ -155,10 +174,16 @@ def dynamic_concat_run(is_grad):
|
|||
16, None], column_names[1]: [16, None]}
|
||||
if is_grad:
|
||||
dynamic_columns[column_names[-1]] = [16, None]
|
||||
dataset.set_dynamic_columns(columns=dynamic_columns)
|
||||
|
||||
dyn_tensors = []
|
||||
for val in dynamic_columns.values():
|
||||
dyn_tensors.append(Tensor(dtype=ms.float32, shape=val))
|
||||
|
||||
net = ConcatNet(axis)
|
||||
if is_grad:
|
||||
net = GradNetWrtX(net)
|
||||
|
||||
net.set_inputs(*dyn_tensors)
|
||||
output = dynamic_shape_sink_process(net, dataset)
|
||||
output_cmp = fixed_shape_process(net, dataset)
|
||||
assert compare(output, output_cmp)
|
||||
|
@ -217,9 +242,10 @@ def test_dynamic_bachnorm():
|
|||
data_list.append(tuple(data))
|
||||
column_names = get_columns(len(data_list[0]))
|
||||
dataset = ds.GeneratorDataset(data_list, column_names, shuffle=False)
|
||||
dynamic_columns = {column_names[0]: [None, c], column_names[1]: [None, c]}
|
||||
dataset.set_dynamic_columns(columns=dynamic_columns)
|
||||
t0 = Tensor(dtype=ms.float32, shape=[None, c])
|
||||
t1 = Tensor(dtype=ms.float32, shape=[None, c])
|
||||
net = GradNetWrtX(BatchNormNet(c))
|
||||
net.set_inputs(t0, t1)
|
||||
gradients = dynamic_shape_sink_process(net, dataset)
|
||||
gradients_cmp = fixed_shape_process(net, dataset)
|
||||
assert compare(gradients, gradients_cmp)
|
||||
|
@ -249,10 +275,11 @@ def test_dynamic_reshape():
|
|||
data_list.append(tuple(data))
|
||||
column_names = get_columns(len(data_list[0]))
|
||||
dataset = ds.GeneratorDataset(data_list, column_names, shuffle=False)
|
||||
dynamic_columns = {column_names[0]: [
|
||||
None, 64, 1], column_names[1]: [None, 64]}
|
||||
dataset.set_dynamic_columns(columns=dynamic_columns)
|
||||
net = ReshapeNet()
|
||||
|
||||
t0 = Tensor(dtype=ms.float32, shape=[None, 64, 1])
|
||||
t1 = Tensor(dtype=ms.float32, shape=[None, 64])
|
||||
net.set_inputs(t0, t1)
|
||||
output = dynamic_shape_sink_process(net, dataset)
|
||||
output_cmp = fixed_shape_process(net, dataset)
|
||||
assert compare(output, output_cmp)
|
||||
|
@ -285,9 +312,10 @@ def test_dynamic_reduce_sum_input_axis():
|
|||
data_list.append(tuple(data))
|
||||
column_names = get_columns(len(data_list[0]))
|
||||
dataset = ds.GeneratorDataset(data_list, column_names, shuffle=False)
|
||||
dynamic_columns = {column_names[0]: [None, 256]}
|
||||
dataset.set_dynamic_columns(columns=dynamic_columns)
|
||||
net = ReduceSumInputAxisNet()
|
||||
t0 = Tensor(dtype=ms.float32, shape=[None, 256])
|
||||
t1 = Tensor(dtype=ms.int64, shape=[1], init=One())
|
||||
net.set_inputs(t0, t1)
|
||||
output = dynamic_shape_sink_process(net, dataset)
|
||||
# Currently, the parameter axis of ReduceSum operator is dynamic(tensor) is
|
||||
# not supported under the fixed shape, so numpy is used for comparison
|
||||
|
@ -320,9 +348,9 @@ def test_dynamic_nop():
|
|||
data_list.append(tuple(data))
|
||||
column_names = get_columns(len(data_list[0]))
|
||||
dataset = ds.GeneratorDataset(data_list, column_names, shuffle=False)
|
||||
dynamic_columns = {column_names[0]: [None, 1]}
|
||||
dataset.set_dynamic_columns(columns=dynamic_columns)
|
||||
net = NopNet()
|
||||
t0 = Tensor(dtype=ms.float32, shape=[None, 1])
|
||||
net.set_inputs(t0)
|
||||
output = dynamic_shape_sink_process(net, dataset)
|
||||
output_cmp = fixed_shape_process(net, dataset)
|
||||
assert compare(output, output_cmp)
|
||||
|
@ -356,9 +384,10 @@ def test_dynamic_reduce_sum():
|
|||
data_list.append(tuple(data))
|
||||
column_names = get_columns(len(data_list[0]))
|
||||
dataset = ds.GeneratorDataset(data_list, column_names, shuffle=False)
|
||||
dynamic_columns = {column_names[0]: [None, 256]}
|
||||
dataset.set_dynamic_columns(columns=dynamic_columns)
|
||||
net = GradNetWrtX(ReduceSumNet())
|
||||
t0 = Tensor(dtype=ms.float32, shape=[None, 256])
|
||||
t1 = Tensor(dtype=ms.float32, shape=[], init=One())
|
||||
net.set_inputs(t0, t1)
|
||||
output = dynamic_shape_sink_process(net, dataset)
|
||||
output_cmp = fixed_shape_process(net, dataset)
|
||||
assert compare(output, output_cmp)
|
||||
|
@ -388,10 +417,11 @@ def test_dynamic_add():
|
|||
data_list.append(tuple(data))
|
||||
column_names = get_columns(len(data_list[0]))
|
||||
dataset = ds.GeneratorDataset(data_list, column_names, shuffle=False)
|
||||
dynamic_columns = {column_names[0]: [None, 256], column_names[1]: [
|
||||
None, 256], column_names[2]: [None, 256]}
|
||||
dataset.set_dynamic_columns(columns=dynamic_columns)
|
||||
net = GradNetWrtX(AddNet())
|
||||
t0 = Tensor(dtype=ms.float32, shape=[None, 256])
|
||||
t1 = Tensor(dtype=ms.float32, shape=[None, 256])
|
||||
t2 = Tensor(dtype=ms.float32, shape=[None, 256])
|
||||
net.set_inputs(t0, t1, t2)
|
||||
output = dynamic_shape_sink_process(net, dataset)
|
||||
output_cmp = fixed_shape_process(net, dataset)
|
||||
assert compare(output, output_cmp)
|
||||
|
|
Loading…
Reference in New Issue