def test_op_name_duplicated_many(self): @mb.program(input_specs=[mb.TensorSpec(shape=(10, 20))]) def prog(x): x = mb.cast(x=x, dtype="fp16", name="castop") x = mb.cast(x=x, dtype="fp16", name="castop") x = mb.cast(x=x, dtype="int32", name="castop_2") x = mb.cast(x=x, dtype="int64", name="castop") x = mb.cast(x=x, dtype="fp32", name="castop_2") x = mb.square(x=x, name="square") return x prev_prog, _, block = apply_pass_and_basic_check( prog, "common::dedup_op_and_var_names") self.assertEqual(get_op_types_in_program(prev_prog), ['cast', 'cast', 'cast', 'cast', 'cast', 'square']) self.assertEqual( get_op_names_in_program(prev_prog), ['castop', 'castop', 'castop_2', 'castop', 'castop_2', 'square']) self.assertEqual(get_op_types_in_program(prog), ['cast', 'cast', 'cast', 'cast', 'cast', 'square']) self.assertEqual(get_op_names_in_program(prog), [ 'castop', 'castop_1', 'castop_2', 'castop_3', 'castop_2_1', 'square' ]) assert_model_is_valid( prog, {"x": (10, 20)}, expected_output_shapes={block.outputs[0].name: (10, 20)}, )
def test_input_name_shadow(self): @mb.program(input_specs=[mb.TensorSpec(shape=(10, 20))]) def prog(x): # op name "x" results in output var name "x", which shadows prog # input var name "x" x = mb.transpose(x=x, perm=[1, 0], name="x") x = mb.relu(x=x, name="relu") return x prev_prog, _, block = apply_pass_and_basic_check( prog, "common::dedup_op_and_var_names") self.assertEqual(get_op_types_in_program(prev_prog), ['transpose', 'relu']) self.assertEqual(get_op_names_in_program(prev_prog), ['x', 'relu']) self.assertEqual(get_op_types_in_program(prog), ['transpose', 'relu']) self.assertEqual(get_op_names_in_program(prog), ['x', 'relu']) op = prog['main'].find_ops(op_type='transpose')[0] self.assertEqual("x_1", op.outputs[0].name) assert_model_is_valid( prog, {"x": (10, 20)}, expected_output_shapes={block.outputs[0].name: (20, 10)}, )
def test_unchanged(self): @mb.program(input_specs=[mb.TensorSpec(shape=(2, 4))]) def prog(x): x = mb.reshape(x=x, shape=(1, 8), name="reshape") return x prev_prog, _, block = apply_pass_and_basic_check( prog, "common::dedup_op_and_var_names") self.assertEqual(get_op_types_in_program(prev_prog), ['reshape']) self.assertEqual(get_op_names_in_program(prev_prog), ['reshape']) self.assertEqual(get_op_types_in_program(prog), ['reshape']) self.assertEqual(get_op_names_in_program(prog), ['reshape']) assert_model_is_valid( prog, {"x": (2, 4)}, expected_output_shapes={block.outputs[0].name: (1, 8)}, )