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

Add argmax operator. #175

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion wonnx/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ pub fn compile(

op @ ("ReduceMean" | "ReduceSum" | "ReduceMax" | "ReduceMin" | "ReduceProd"
| "ReduceL1" | "ReduceL2" | "ReduceLogSum" | "ReduceLogSumExp"
| "ReduceSumSquare") => {
| "ReduceSumSquare" | "ArgMax") => {
let all_axes: Vec<i64> = (0..(i_dims[0].len() as i64)).collect();
let axes: Vec<i64> = node
.get_attribute_value("axes", Some(all_axes))?
Expand Down
9 changes: 8 additions & 1 deletion wonnx/templates/pool/reduce.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
Now for each reduced axis, iterate all values and reduce. Note, starting value may not always be zero. For
ReduceMin/Max we should initialize as NaN and keep a flag to check if we have seen at least one element -#}

var accumulator = {% if op_type == "ReduceProd" %} {{ scalar_type }}(1) {% else %} Scalar() {% endif %};
var accumulator = {% if op_type == "ReduceProd" %} {{ scalar_type }}(1) {% else %} Scalar() {% endif %};
var max_element: Scalar = log(Scalar());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hm, perhaps add a note explaining why you initialize to log(Scalar()) (I assume it is a trick to initialize to -Inf so the first value is always higher).

Copy link
Author

Choose a reason for hiding this comment

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

Good point, will do!


var count = 0u;

{% for reducing_axis in axes %}
Expand Down Expand Up @@ -79,6 +81,11 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
else if(accumulator < input_val) {
accumulator = input_val;
}
{% elif op_type == "ArgMax" %}
if(input_val > max_element) {
max_element = input_val;
accumulator = f32(count);
Copy link
Collaborator

Choose a reason for hiding this comment

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

The output of ArgMax is actually specified as int64 (see here). This should work for now but we might have to fix it later.

Copy link
Author

Choose a reason for hiding this comment

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

Good point, then it would probably make sense to move argmax out of reduce to not have to return mixed data types from reduce?

}
{% endif %}

count = count + 1u;
Expand Down
23 changes: 23 additions & 0 deletions wonnx/tests/reduce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ fn reduce() {
60.0, 2.0,
];

#[rustfmt::skip]
let data_two = [
20.0, 1.0,
5.0, 2.0,

30.0, 1.0,
40.0, 2.0,

60.0, 1.0,
55.0, 2.0,
];

// ReduceSum: sum all
test_reduce(
&data,
Expand Down Expand Up @@ -223,6 +235,17 @@ fn reduce() {
&[3, 2],
);

// ONNX test case: do_not_keepdims with ArgMax
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you also enable the corresponding test case in the ONNX backend test (Python scripts)?

Copy link
Author

Choose a reason for hiding this comment

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

Will do!

test_reduce(
&data_two,
&[3, 2, 2],
Some(vec![1]),
"ArgMax",
false,
&[0., 1., 1., 1., 0., 1.],
&[3, 2],
);

// ONNX test case for ReduceSumSquare (https://github.com/onnx/onnx/blob/94e2f64551ded652df53a7e9111031e8aabddaee/onnx/backend/test/case/node/reducesumsquare.py#L27)
test_reduce(
&[1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.],
Expand Down