Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AutoScan] add Pass 0D test #10254

Merged
merged 5 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,22 @@ class ElementwiseMulConstantEliminator : public FuseBase {
auto mul_input_x_dims = mul_input_x->Get<lite::Tensor>().dims();
auto mul_output = scope->FindVar(matched.at("output")->arg()->name);
auto mul_output_dims = mul_output->Get<lite::Tensor>().dims();
if (mul_input_x_dims != mul_output_dims) {

if (mul_input_x_dims.size() != mul_output_dims.size()) {
nodes_.erase(nodes_.begin(), nodes_.end());
LOG(WARNING)
<< "elementwise_mul input x not equal to output, eleminate failed";
LOG(WARNING) << "elementwise_mul input x.size() not equal to "
"output.size(), eleminate failed";
return;
}
for (int i = 0; i < mul_input_x_dims.size(); i++) {
if (mul_input_x_dims[i] != mul_output_dims[i] &&
mul_output_dims[i] != -1) {
nodes_.erase(nodes_.begin(), nodes_.end());
LOG(WARNING) << "elementwise_mul input x[i] not equal to output[i], "
"eleminate failed";
return;
}
}

op_info.UpdateAllInputs(matched.at("output")->AsArg().name,
matched.at("x")->AsArg().name);
Expand Down
127 changes: 108 additions & 19 deletions lite/tests/unittest_py/op/test_reshape2_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,38 +77,110 @@ def sample_program_configs(self, draw):
in_shape = draw(
st.lists(
st.integers(
min_value=1, max_value=10), min_size=4, max_size=4))

min_value=5, max_value=10), min_size=4, max_size=4))
attr_shape = draw(
st.lists(
st.integers(
min_value=1, max_value=max(in_shape)),
min_size=1,
max_size=len(in_shape)))

shape_tensor = []
for i in range(len(attr_shape) - 1, -1, -1):
shape_tensor.append(attr_shape[i])
assume(
reduce(lambda x, y: x * y, attr_shape) == reduce(
lambda x, y: x * y, in_shape))

with_shape = draw(st.sampled_from([True, False]))
# in_shape = draw(st.sampled_from([in_shape, []]))
if in_shape == []:
attr_shape = [1]
shape_tensor = [1, 1]

# The parameter shape in ReshapeOp must be set
with_shape_attr = draw(st.sampled_from([True]))
with_shape_tensor = draw(st.sampled_from([True, False]))

def generate_input(*args, **kwargs):
return np.random.random(in_shape).astype(np.float32)

build_ops = OpConfig(
type="reshape2",
inputs={"X": ["input_data"], },
outputs={
"Out": ["output_data"],
"XShape": ["x_shape"],
},
attrs={"shape": attr_shape, })
program_config = ProgramConfig(
ops=[build_ops],
weights={},
inputs={
"input_data": TensorConfig(data_gen=partial(generate_input)),
},
outputs=["output_data"])
def generate_shape(*args, **kwargs):
return np.asarray(shape_tensor).astype(np.int32)

if (with_shape_attr and with_shape_tensor):
build_ops = OpConfig(
type="reshape2",
inputs={"X": ["input_data"],
"Shape": ["input_shape"]},
outputs={
"Out": ["output_data"],
"XShape": ["x_shape"],
},
attrs={"shape": attr_shape, })
program_config = ProgramConfig(
ops=[build_ops],
weights={},
inputs={
"input_data":
TensorConfig(data_gen=partial(generate_input)),
"input_shape":
TensorConfig(data_gen=partial(generate_shape)),
},
outputs=["output_data"])
elif (with_shape_attr):
build_ops = OpConfig(
type="reshape2",
inputs={"X": ["input_data"]},
outputs={
"Out": ["output_data"],
"XShape": ["x_shape"],
},
attrs={"shape": attr_shape, })
program_config = ProgramConfig(
ops=[build_ops],
weights={},
inputs={
"input_data":
TensorConfig(data_gen=partial(generate_input)),
},
outputs=["output_data"])
elif (with_shape_tensor):
build_ops = OpConfig(
type="reshape2",
inputs={"X": ["input_data"],
"Shape": ["input_shape"]},
outputs={
"Out": ["output_data"],
"XShape": ["x_shape"],
},
attrs={})
program_config = ProgramConfig(
ops=[build_ops],
weights={},
inputs={
"input_data":
TensorConfig(data_gen=partial(generate_input)),
"input_shape":
TensorConfig(data_gen=partial(generate_shape)),
},
outputs=["output_data"])
else:
build_ops = OpConfig(
type="reshape2",
inputs={"X": ["input_data"]},
outputs={
"Out": ["output_data"],
"XShape": ["x_shape"],
},
attrs={})
program_config = ProgramConfig(
ops=[build_ops],
weights={},
inputs={
"input_data":
TensorConfig(data_gen=partial(generate_input)),
},
outputs=["output_data"])
return program_config

def sample_predictor_configs(self):
Expand All @@ -132,8 +204,25 @@ def teller1(program_config, predictor_config):
teller1, IgnoreReasons.PADDLELITE_NOT_SUPPORT,
"Lite does not support change batch on nvidia_tensorrt.")

def _teller2(program_config, predictor_config):
target_type = predictor_config.target()
if target_type == TargetType.Metal:
return True

self.add_ignore_check_case(_teller2,
IgnoreReasons.PADDLELITE_NOT_SUPPORT,
"Metal report Can't find a io_copy kernel.")

def teller3(program_config, predictor_config):
if self.get_nnadapter_device_name() == "intel_openvino":
return True

self.add_ignore_check_case(teller3,
IgnoreReasons.PADDLELITE_NOT_SUPPORT,
"intel_openvino report error.")

def test(self, *args, **kwargs):
self.run_and_statis(quant=False, max_examples=200)
self.run_and_statis(quant=False, max_examples=1000)


if __name__ == "__main__":
Expand Down
114 changes: 98 additions & 16 deletions lite/tests/unittest_py/op/test_reshape_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,35 +66,98 @@ def sample_program_configs(self, draw):
in_shape = draw(
st.lists(
st.integers(
min_value=1, max_value=10), min_size=4, max_size=4))

min_value=5, max_value=10), min_size=4, max_size=4))
attr_shape = draw(
st.lists(
st.integers(
min_value=1, max_value=max(in_shape)),
min_size=1,
max_size=len(in_shape)))

shape_tensor = []
for i in range(len(attr_shape) - 1, -1, -1):
shape_tensor.append(attr_shape[i])
assume(
reduce(lambda x, y: x * y, attr_shape) == reduce(
lambda x, y: x * y, in_shape))

with_shape = draw(st.sampled_from([True, False]))
# in_shape = draw(st.sampled_from([in_shape, []]))
if in_shape == []:
attr_shape = [1]
shape_tensor = [1, 1]

# The parameter shape in ReshapeOp must be set
with_shape_attr = draw(st.sampled_from([True]))
with_shape_tensor = draw(st.sampled_from([True, False]))

def generate_input(*args, **kwargs):
return np.random.random(in_shape).astype(np.float32)

build_ops = OpConfig(
type="reshape",
inputs={"X": ["input_data"], },
outputs={"Out": ["output_data"], },
attrs={"shape": attr_shape, })
program_config = ProgramConfig(
ops=[build_ops],
weights={},
inputs={
"input_data": TensorConfig(data_gen=partial(generate_input)),
},
outputs=["output_data"])
def generate_shape(*args, **kwargs):
return np.asarray(shape_tensor).astype(np.int32)

if (with_shape_attr and with_shape_tensor):
build_ops = OpConfig(
type="reshape",
inputs={"X": ["input_data"],
"Shape": ["input_shape"]},
outputs={"Out": ["output_data"], },
attrs={"shape": attr_shape, })
program_config = ProgramConfig(
ops=[build_ops],
weights={},
inputs={
"input_data":
TensorConfig(data_gen=partial(generate_input)),
"input_shape":
TensorConfig(data_gen=partial(generate_shape)),
},
outputs=["output_data"])
elif (with_shape_attr):
build_ops = OpConfig(
type="reshape",
inputs={"X": ["input_data"]},
outputs={"Out": ["output_data"], },
attrs={"shape": attr_shape, })
program_config = ProgramConfig(
ops=[build_ops],
weights={},
inputs={
"input_data":
TensorConfig(data_gen=partial(generate_input)),
},
outputs=["output_data"])
elif (with_shape_tensor):
build_ops = OpConfig(
type="reshape",
inputs={"X": ["input_data"],
"Shape": ["input_shape"]},
outputs={"Out": ["output_data"], },
attrs={})
program_config = ProgramConfig(
ops=[build_ops],
weights={},
inputs={
"input_data":
TensorConfig(data_gen=partial(generate_input)),
"input_shape":
TensorConfig(data_gen=partial(generate_shape)),
},
outputs=["output_data"])
else:
build_ops = OpConfig(
type="reshape",
inputs={"X": ["input_data"]},
outputs={"Out": ["output_data"], },
attrs={})
program_config = ProgramConfig(
ops=[build_ops],
weights={},
inputs={
"input_data":
TensorConfig(data_gen=partial(generate_input)),
},
outputs=["output_data"])
return program_config

def sample_predictor_configs(self):
Expand All @@ -112,8 +175,27 @@ def teller1(program_config, predictor_config):
teller1, IgnoreReasons.PADDLELITE_NOT_SUPPORT,
"Lite does not support change batch on nvidia_tensorrt.")

def _teller2(program_config, predictor_config):
target_type = predictor_config.target()
in_x_shape = list(program_config.inputs["input_data"].shape)
if target_type != TargetType.ARM and target_type != TargetType.Host:
if len(in_x_shape) == 0:
return True

self.add_ignore_check_case(_teller2,
IgnoreReasons.PADDLELITE_NOT_SUPPORT,
"Only test 0D-tensor on CPU(ARM/Host) now.")

def teller3(program_config, predictor_config):
if self.get_nnadapter_device_name() == "intel_openvino":
return True

self.add_ignore_check_case(teller3,
IgnoreReasons.PADDLELITE_NOT_SUPPORT,
"intel_openvino report error.")

def test(self, *args, **kwargs):
self.run_and_statis(quant=False, max_examples=200)
self.run_and_statis(quant=False, max_examples=1000)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def sample_program_configs(self, draw):
st.integers(
min_value=-1, max_value=max(len(in_shape_x), len(in_shape_y))))

in_shape_x = draw(st.sampled_from([in_shape_x, []]))
in_shape_y = draw(st.sampled_from([in_shape_y, []]))

assume(
check_input_shape_available(
in_shape_x=in_shape_x, in_shape_y=in_shape_y, axis=axis) ==
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def sample_program_configs(self, draw):
st.lists(
st.integers(
min_value=1, max_value=20), min_size=2, max_size=5))
fill_constant_shape = draw(st.sampled_from([fill_constant_shape, []]))

axis = draw(
st.integers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ def sample_program_configs(self, draw):
st.integers(
min_value=1, max_value=20), min_size=2, max_size=5))

in_shape_x = draw(st.sampled_from([in_shape_x, []]))
in_shape_y = draw(st.sampled_from([in_shape_y, []]))

axis = draw(
st.integers(
min_value=-1, max_value=max(len(in_shape_x), len(in_shape_y))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def sample_program_configs(self, draw):
st.integers(
min_value=1, max_value=64), min_size=1, max_size=1))

axis = -1

def generate_input_x(*args, **kwargs):
return np.random.randint(in_shape_x).astype(np.float32)

Expand All @@ -75,7 +77,7 @@ def generate_input_y(*args, **kwargs):
inputs={"X": ["input_data_x"],
"Y": ["input_data_y"]},
outputs={"Out": ["greater_than_output"]},
attrs={"axis": -1,
attrs={"axis": axis,
"force_cpu": False})

cast_op = OpConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def sample_program_configs(self, draw):
in_shape = draw(
st.lists(
st.integers(
min_value=1, max_value=8), min_size=2, max_size=4))
min_value=1, max_value=8), min_size=0, max_size=4))
dropout_prob_data = draw(st.floats(min_value=0.0, max_value=1.0))
seed_data = draw(st.integers(min_value=0.0, max_value=1.0))
fix_seed = draw(st.booleans())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def sample_program_configs(self, draw):
st.lists(
st.integers(
min_value=1, max_value=20), min_size=4, max_size=4))
in_shape_x = draw(st.sampled_from([in_shape_x, []]))
threshold = draw(st.floats(min_value=0, max_value=1))
alpha = draw(st.floats(min_value=0, max_value=1))
scale = draw(st.floats(min_value=0.5, max_value=5))
Expand Down
2 changes: 1 addition & 1 deletion lite/tests/unittest_py/pass/test_scaleacts_fuse_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def sample_program_configs(self, draw):
in_shape = draw(
st.lists(
st.integers(
min_value=1, max_value=64), min_size=2, max_size=4))
min_value=1, max_value=64), min_size=0, max_size=4))
act_type = draw(st.sampled_from(['relu6']))
threshold = draw(st.floats(min_value=0, max_value=1))
alpha = draw(st.floats(min_value=0, max_value=1))
Expand Down
Loading