-
Notifications
You must be signed in to change notification settings - Fork 88
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 support for Scan operator #2936
Merged
Merged
Changes from 16 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
74333b0
Implementation start
music-dino a3011f3
Implement scan base case
music-dino 72d3716
Implement scan output direction and axes attribute support
music-dino f7a7e9f
Implement scan_input_axes attribute support
music-dino 7793cb0
Implement ScanSlice operator
music-dino 3fcbb68
Implement via adapting subgraph to a what a loop expects and using th…
music-dino 81e0804
Add support for scan_output_axes and code refactoring
music-dino 18aabad
Refactoring
music-dino ed1382a
Add support for scan_output_directions, modify tests to have two scan…
music-dino 3e57489
Implement additional tests, add comments, fix cppcheck and tidy issues
music-dino 4824f77
Implement negative tests
music-dino a4c2e28
Merge remote-tracking branch 'upstream/develop' into scan_op_support
music-dino 495965f
Update onnx_operators.rst
music-dino 5a106dd
Fix format issues and test failures
music-dino 45a04c5
Merge remote-tracking branch 'upstream/develop' into scan_op_support
music-dino 51bf14f
Add deduction guides to vectors
music-dino f7a6c91
Merge remote-tracking branch 'upstream/develop' into scan_op_support
music-dino b82f193
Additional verify test case, code refactoring
music-dino 2687a5a
op_shape tests for scan_slice, added comments
music-dino 4add933
Merge remote-tracking branch 'upstream/develop' into scan_op_support
music-dino 56e7056
Fix clang tidy issue
music-dino 74b6c96
Merge branch 'develop' into scan_op_support
causten d65e668
Merge branch 'develop' into scan_op_support
TedThemistokleous db27789
Merge branch 'develop' into scan_op_support
causten 9017c1a
Merge branch 'develop' into scan_op_support
causten 84f9ecb
Fix braced initializer issue, remove a cout
music-dino 141eb2b
Merge remote-tracking branch 'upstream/develop' into scan_op_support
music-dino b768b6d
Merge branch 'develop' into scan_op_support
causten cb59a5e
Merge branch 'develop' into scan_op_support
causten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -226,6 +226,7 @@ register_migraphx_ops( | |
rsqrt | ||
run_on_target | ||
scalar | ||
scan_slice | ||
scatter_none | ||
scatter_add | ||
scatter_mul | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,12 +41,14 @@ namespace op { | |
|
||
struct loop | ||
{ | ||
int64_t max_iterations = 10; | ||
int64_t max_iterations = 10; | ||
std::vector<int64_t> scan_output_directions = {}; | ||
|
||
template <class Self, class F> | ||
static auto reflect(Self& self, F f) | ||
{ | ||
return pack(f(self.max_iterations, "max_iterations")); | ||
return pack(f(self.max_iterations, "max_iterations"), | ||
f(self.scan_output_directions, "scan_output_directions")); | ||
} | ||
|
||
std::string name() const { return "loop"; } | ||
|
@@ -97,19 +99,24 @@ struct loop | |
|
||
void append(const std::vector<argument>& iter_state, | ||
const std::vector<argument>& concatenated_outputs, | ||
int iter) const | ||
const std::vector<int64_t>& scan_output_dirs, | ||
int64_t iter, | ||
int64_t iter_num) const | ||
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. better to name them as |
||
{ | ||
assert(iter_state.size() == concatenated_outputs.size()); | ||
for(auto i : range(iter_state.size())) | ||
{ | ||
const auto& iter_stat = iter_state.at(i); | ||
const auto& scan_out = concatenated_outputs.at(i); | ||
|
||
auto dir = scan_output_dirs.empty() ? 0 : scan_output_dirs[i]; | ||
auto idx = (1 - dir) * iter + dir * (iter_num - 1 - iter); | ||
|
||
auto* in_data = iter_stat.data(); | ||
auto* out_data = scan_out.data(); | ||
std::size_t out_size = iter_stat.get_shape().bytes(); | ||
assert((iter + 1) * out_size <= scan_out.get_shape().bytes()); | ||
std::copy(in_data, in_data + out_size, out_data + iter * out_size); | ||
assert((idx + 1) * out_size <= scan_out.get_shape().bytes()); | ||
std::copy(in_data, in_data + out_size, out_data + idx * out_size); | ||
} | ||
} | ||
|
||
|
@@ -153,7 +160,7 @@ struct loop | |
cpy_args.push_back(argument(out_shape)); | ||
|
||
// run loop | ||
return run_loop(ref_loop{max_iterations}, ctx, cpy_args, mods, run); | ||
return run_loop(ref_loop{max_iterations}, scan_output_directions, ctx, cpy_args, mods, run); | ||
} | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
#ifndef MIGRAPHX_GUARD_OPERATORS_SCAN_SLICE_HPP | ||
#define MIGRAPHX_GUARD_OPERATORS_SCAN_SLICE_HPP | ||
|
||
#include <migraphx/op/name.hpp> | ||
#include <migraphx/check_shapes.hpp> | ||
#include <migraphx/argument.hpp> | ||
#include <migraphx/config.hpp> | ||
#include <migraphx/value.hpp> | ||
#include <migraphx/dyn_output.hpp> | ||
#include <migraphx/op/normalize_attribute.hpp> | ||
#include <migraphx/normalize_attributes.hpp> | ||
#include <array> | ||
|
||
namespace migraphx { | ||
inline namespace MIGRAPHX_INLINE_NS { | ||
namespace op { | ||
|
||
struct scan_slice : op_name<scan_slice> | ||
{ | ||
int64_t axis = 0; | ||
int64_t direction = 0; | ||
|
||
template <class Self, class F> | ||
static auto reflect(Self& self, F f) | ||
{ | ||
return pack(f(self.axis, "axis"), f(self.direction, "direction")); | ||
} | ||
|
||
value attributes() const | ||
{ | ||
value normalize_axes = value::object{}; | ||
normalize_axes["axis"] = value::array{normalize_attribute::include_min}; | ||
return {{"normalize_axes", normalize_axes}}; | ||
} | ||
|
||
shape normalize_compute_shape(std::vector<shape> inputs) const | ||
umangyadav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
check_shapes{inputs, *this}.has(2); | ||
auto input_shape = inputs[0]; | ||
auto new_lens = input_shape.lens(); | ||
new_lens[axis] = 1; | ||
|
||
return shape{input_shape.type(), new_lens, input_shape.strides()}; | ||
} | ||
|
||
argument compute(shape output_shape, std::vector<argument> args) const | ||
{ | ||
auto input = args[0]; | ||
auto input_sh = input.get_shape(); | ||
|
||
int64_t idx; | ||
args[1].visit([&](auto i) { idx = i.front(); }); | ||
const auto max_idx = input_sh.lens()[axis] - 1; | ||
if(idx > max_idx or idx < 0) | ||
MIGRAPHX_THROW("ScanSlice: index {" + std::to_string(idx) + "} out of range [0, " + | ||
std::to_string(max_idx) + "]"); | ||
idx = (1 - direction) * idx + direction * (max_idx - idx); | ||
|
||
auto offset = idx * input_sh.strides().at(axis) * input_sh.type_size(); | ||
return {output_shape, [=] { return input.data() + offset; }}; | ||
} | ||
}; | ||
|
||
} // namespace op | ||
} // namespace MIGRAPHX_INLINE_NS | ||
} // namespace migraphx | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why identity and sequence are mentioned here ?
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.
They're mentioned in the Loop op entry as well. Since scan relies on Loop, I thought I'd carry over the info.
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.
Okay. I do not know what that means though. @attila-dusnoki-htec can you comment this limitation is about ?