-
Notifications
You must be signed in to change notification settings - Fork 60
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
base: master
Are you sure you want to change the base?
Add argmax operator. #175
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()); | ||
|
||
var count = 0u; | ||
|
||
{% for reducing_axis in axes %} | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The output of There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -223,6 +235,17 @@ fn reduce() { | |
&[3, 2], | ||
); | ||
|
||
// ONNX test case: do_not_keepdims with ArgMax | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.], | ||
|
There was a problem hiding this comment.
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).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, will do!