Skip to content

Implementing parsing of Mean Variance Normalization #202

Open
gchinora wants to merge 20 commits into
gyula/task1_basefrom
gyula/task1
Open

Implementing parsing of Mean Variance Normalization #202
gchinora wants to merge 20 commits into
gyula/task1_basefrom
gyula/task1

Conversation

@gchinora
Copy link
Copy Markdown

No description provided.

Comment thread src/onnx/parse_mean_variance_norm.cpp Outdated
}
assert(X->get_shape().ndim() >= axes_min_size);

auto E_X = info.add_instruction(make_op("reduce_mean", {{"axes", axes}}), X);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using snake case is correct, but you shouldn't use capital letters in local variable names

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough.... I was trying to confirm to the mathematical symbol of the expected value of a random variable. Which is E(X).

Comment thread src/onnx/parse_mean_variance_norm.cpp Outdated
axes.assign(info.attributes.at("axes").ints().begin(), info.attributes.at("axes").ints().end());
axes_min_size = axes.size();
}
assert(X->get_shape().ndim() >= axes_min_size);
Copy link
Copy Markdown

@dinomusic dinomusic Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the parser you should throw an exception when a condition isn't met. You can do this using the MIGRAPHX_THROW macro which takes a string as input. There isn't a set format for the string, I usually go with "OperatorName: input_dim/attribute/anything has value x, should be y", something like you have above for the type validty check.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough... note: see parse_instancenorm.cpp:88 there's also an assert(.) call.

Comment thread src/onnx/parse_mean_variance_norm.cpp Outdated

const auto& X = args[0];

const auto eps_default = 1e-9f;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't something that's pointed out in the docs, but 1e-9f wouldn't be the correct value to use for fp16, it's too small.

Comment thread src/onnx/parse_mean_variance_norm.cpp Outdated
struct mean_variance_norm : op_parser<mean_variance_norm>
{
std::set<shape::type_t> valid_types = {
shape::bf16_type, shape::double_type, shape::float_type};
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing fp16 from supported type list.

Comment thread test/onnx/gen_onnx.py Outdated
@onnx_test()
def mean_variance_norm_val_test():
# example from: https://github.com/onnx/onnx/blob/main/onnx/backend/test/case/node/meanvariancenormalization.py
x = np.array(
Copy link
Copy Markdown

@dinomusic dinomusic Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually we want to avoid specifying tensor values in the graph, the instancenorm tests are an outlier in this regard.
By doing this you can reuse the same graph for the parse and verify tests.
You'd provide the values as an input to the compiled migraphx program.

Have a look at how gen_onnx.py:mod_test() does it, and its corresponding cpp file test/onnx/verify/mod_test.cpp

Comment thread test/onnx/gen_onnx.py Outdated
node = onnx.helper.make_node('MeanVarianceNormalization',
inputs=['x'],
outputs=['y'],
axes=ax,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also add a test where you don't define the axes attribute to check if the parser correctly defaults it.

@dinomusic
Copy link
Copy Markdown

Negative parser tests should also be added. If there's a branch in your parser code that leads to an MIGRAPHX_THROW, it should be covered with a test.

Comment thread test/onnx/parse/mean_variance_norm.cpp Outdated
mm->add_return({Y});

migraphx::onnx_options options;
auto prog = read_onnx("mean_variance_norm_test.onnx", options);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use optimize_onnx to parse the model, read_onnx was used before so older tests still contain it.

Comment thread test/onnx/parse/mean_variance_norm.cpp Outdated
auto denominator= add_common_op(*mm, migraphx::make_op("add"), {std, eps_literal});
auto Y = add_common_op(*mm, migraphx::make_op("div"), {numerator, denominator});

mm->add_return({Y});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably won't need the add_return when using optimize_onxx().

@gchinora gchinora requested a review from dinomusic April 2, 2025 09:50
Comment thread src/onnx/parse_mean_variance_norm.cpp Outdated

const auto& x = args[0];

const auto eps_default = 1e-7f;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd want eps_default to be 1e-7 if dtype is half_type, and 1e-9 for the other types.

migraphx::program p;
const auto prog = optimize_onnx("mean_variance_norm_default_axes_test.onnx");
const auto* mm = prog.get_main_module();
const auto it = std::find_if(mm->begin(), mm->end(), [](auto instr){ return instr.name() == "reduce_mean";});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the approach here. You'd still want to build out an entire "expected" graph for these tests as that's the approach in migraphx, but kudos for the way you did it. You don't need to make a change to this.

@gchinora gchinora requested a review from dinomusic April 8, 2025 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants