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 onnx mod operator #1302

Merged
merged 25 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7603552
Add in changes for onnx Mod operator
TedThemistokleous Jul 8, 2022
8ff4b15
Split mod operation into fmod & mod equivalents
TedThemistokleous Jul 12, 2022
8288e88
fixup! Split mod operation into fmod & mod equivalents
TedThemistokleous Jul 13, 2022
23a82ba
Added Test related changes for mod operator
TedThemistokleous Jul 13, 2022
062a29d
Add additional tests for mod operator parsing
TedThemistokleous Jul 14, 2022
a3d267c
Add false commutative attribute for fmod/mod operators
TedThemistokleous Jul 14, 2022
0bf1090
Fix mod_test in verify_onnx
TedThemistokleous Jul 14, 2022
b242b7d
Add test case in verify_onnx for mod_test and fix migraphx::mod
TedThemistokleous Jul 15, 2022
ca630d4
Add additional test in verify_onnx for different datatypes using pars…
TedThemistokleous Jul 15, 2022
5419584
fixup! Add additional test in verify_onnx for different datatypes usi…
TedThemistokleous Jul 15, 2022
7864e9e
Remove gpu test for now without GPU implimentation
TedThemistokleous Jul 16, 2022
1fa6802
fixup! Added Test related changes for mod operator
TedThemistokleous Jul 18, 2022
95e58d6
Add name parameter to fmod and mod and remove extra check for dims
TedThemistokleous Jul 18, 2022
9befc00
Merge branch 'develop' into add_onnx_mod_operator
causten Jul 19, 2022
92edd77
Merge branch 'develop' into add_onnx_mod_operator
umangyadav Jul 22, 2022
140ca43
Add extra check to not parse float inputs without fmod=1
TedThemistokleous Jul 22, 2022
f867ae6
Require float inputs to use fmod=1
TedThemistokleous Jul 22, 2022
366204c
Merge branch 'add_onnx_mod_operator' of github.com:ROCmSoftwarePlatfo…
TedThemistokleous Jul 22, 2022
7a9e143
Fix issue with clang-tidy
TedThemistokleous Jul 22, 2022
484cd4f
Add more fractional values for floating point mod tests
TedThemistokleous Jul 22, 2022
a659a50
Update test/onnx/onnx_test.cpp
TedThemistokleous Jul 22, 2022
583c5b4
Cleanup and PR comments
TedThemistokleous Jul 22, 2022
6a30e2b
Merge branch 'add_onnx_mod_operator' of github.com:ROCmSoftwarePlatfo…
TedThemistokleous Jul 22, 2022
5db9d45
cleanup
TedThemistokleous Jul 22, 2022
9d3c817
Merge branch 'develop' into add_onnx_mod_operator
causten Jul 25, 2022
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: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ register_migraphx_ops(
exp
flatten
floor
fmod
gather
gathernd
get_tuple_elem
Expand All @@ -156,6 +157,7 @@ register_migraphx_ops(
lstm
max
min
mod
mul
multibroadcast
multinomial
Expand Down
63 changes: 63 additions & 0 deletions src/include/migraphx/op/fmod.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2022 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_FMOD_HPP
#define MIGRAPHX_GUARD_OPERATORS_FMOD_HPP

#include <array>
#include <migraphx/op/binary.hpp>
#include <migraphx/check_shapes.hpp>
#include <migraphx/stringutils.hpp>
#include <migraphx/streamutils.hpp>
#include <migraphx/literal.hpp>
#include <migraphx/shape_for_each.hpp>
#include <migraphx/config.hpp>
#include <cmath>
#include <utility>
#include <type_traits>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace op {

struct fmod : binary<fmod>
{
std::string name() const { return "fmod"; }
value attributes() const
{
auto a = base_attributes();
a["commutative"] = false;
return a;
}
std::string point_function() const { return "fmod"; }
auto apply() const
{
return [](auto x, auto y) { return std::fmod(x, y); };
}
};

} // namespace op
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx

#endif
63 changes: 63 additions & 0 deletions src/include/migraphx/op/mod.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2022 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_MOD_HPP
#define MIGRAPHX_GUARD_OPERATORS_MOD_HPP

#include <array>
#include <migraphx/op/binary.hpp>
#include <migraphx/check_shapes.hpp>
#include <migraphx/stringutils.hpp>
#include <migraphx/streamutils.hpp>
#include <migraphx/literal.hpp>
#include <migraphx/shape_for_each.hpp>
#include <migraphx/config.hpp>
#include <cmath>
#include <utility>
#include <type_traits>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace op {

struct mod : binary<mod>
{
std::string name() const { return "mod"; }
value attributes() const
{
auto a = base_attributes();
a["commutative"] = false;
return a;
}
std::string point_function() const { return "mod"; }
auto apply() const
{
return [](auto x, auto y) { return std::fmod((std::remainder(x, y)) + y, y); };
}
};

} // namespace op
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx

#endif
2 changes: 2 additions & 0 deletions src/include/migraphx/operators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include <migraphx/op/exp.hpp>
#include <migraphx/op/flatten.hpp>
#include <migraphx/op/floor.hpp>
#include <migraphx/op/fmod.hpp>
#include <migraphx/op/gather.hpp>
#include <migraphx/op/gathernd.hpp>
#include <migraphx/op/get_tuple_elem.hpp>
Expand All @@ -79,6 +80,7 @@
#include <migraphx/op/lstm.hpp>
#include <migraphx/op/max.hpp>
#include <migraphx/op/min.hpp>
#include <migraphx/op/mod.hpp>
#include <migraphx/op/mul.hpp>
#include <migraphx/op/multibroadcast.hpp>
#include <migraphx/op/neg.hpp>
Expand Down
56 changes: 56 additions & 0 deletions src/onnx/parse_mod.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2022 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.
*/
#include <migraphx/onnx/op_parser.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/make_op.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace onnx {

struct parse_mod : op_parser<parse_mod>
{
std::vector<op_desc> operators() const { return {{"Mod"}}; }

instruction_ref parse(const op_desc& /*opd*/,
const onnx_parser& parser,
onnx_parser::node_info info,
std::vector<instruction_ref> args) const
{
std::string mod = "mod";
if(contains(info.attributes, "fmod"))
{
if(parser.parse_value(info.attributes.at("fmod")).at<int>() == 1)
{
mod = "fmod";
}
}
return info.add_common_op(mod, args[0], args[1]);
}
};

} // namespace onnx
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
2 changes: 2 additions & 0 deletions src/targets/gpu/jit/pointwise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ struct pointwise_compiler : compiler<pointwise_compiler>
g.add_point_op("less", "migraphx::abs(${0} < ${1})");
g.add_point_op("greater", "migraphx::abs(${0} > ${1})");
g.add_point_op("not", "migraphx::abs(not ${0})");
g.add_point_op("mod", "migraphx::mod(${0}, ${1})");
g.add_point_op("fmod", "migraphx::fmod(${0}, ${1})");
// Add explict conversions
g.fresult([](const shape& s) {
return "migraphx::convert<" + shape::cpp_type(s.type()) + ">";
Expand Down
84 changes: 84 additions & 0 deletions test/onnx/gen_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2993,6 +2993,7 @@ def matmul_bmbm_test():

@onnx_test
def matmul_bmv_test():

umangyadav marked this conversation as resolved.
Show resolved Hide resolved
m1 = helper.make_tensor_value_info('1', TensorProto.FLOAT, [3, 6, 7])
m2 = helper.make_tensor_value_info('2', TensorProto.FLOAT, [7])
y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [3, 6])
Expand Down Expand Up @@ -3231,6 +3232,89 @@ def min_test():
return ([node], [a, b, c], [y])


@onnx_test
def mod_test():
a = helper.make_tensor_value_info('0', TensorProto.FLOAT, [3, 3, 3])
b = helper.make_tensor_value_info('1', TensorProto.FLOAT, [3, 3, 3])
y = helper.make_tensor_value_info('2', TensorProto.FLOAT, [3, 3, 3])

node = onnx.helper.make_node('Mod', inputs=['0', '1'], outputs=['2'])
umangyadav marked this conversation as resolved.
Show resolved Hide resolved

return ([node], [a, b], [y])


@onnx_test
def mod_test_half():
a = helper.make_tensor_value_info('0', TensorProto.FLOAT16, [3, 3, 3])
b = helper.make_tensor_value_info('1', TensorProto.FLOAT16, [3, 3, 3])
y = helper.make_tensor_value_info('2', TensorProto.FLOAT16, [3, 3, 3])

node = onnx.helper.make_node('Mod', inputs=['0', '1'], outputs=['2'])

return ([node], [a, b], [y])


@onnx_test
def mod_test_different_dtypes():
a = helper.make_tensor_value_info('0', TensorProto.FLOAT, [3, 3, 3])
b = helper.make_tensor_value_info('1', TensorProto.INT32, [3, 3, 3])
y = helper.make_tensor_value_info('2', TensorProto.FLOAT, [3, 3, 3])

node = onnx.helper.make_node(
umangyadav marked this conversation as resolved.
Show resolved Hide resolved
'Mod',
inputs=['0', '1'],
outputs=['2'],
)

return ([node], [a, b], [y])


@onnx_test
def mod_test_fmod():
a = helper.make_tensor_value_info('0', TensorProto.FLOAT, [3, 3, 3])
b = helper.make_tensor_value_info('1', TensorProto.FLOAT, [3, 3, 3])
y = helper.make_tensor_value_info('2', TensorProto.FLOAT, [3, 3, 3])

node = onnx.helper.make_node(
'Mod',
inputs=['0', '1'],
outputs=['2'],
fmod=1 #fmod flag = 1
)

return ([node], [a, b], [y])


@onnx_test
def mod_test_fmod_half():
a = helper.make_tensor_value_info('0', TensorProto.FLOAT16, [3, 3, 3])
b = helper.make_tensor_value_info('1', TensorProto.FLOAT16, [3, 3, 3])
y = helper.make_tensor_value_info('2', TensorProto.FLOAT16, [3, 3, 3])

node = onnx.helper.make_node('Mod',
inputs=['0', '1'],
outputs=['2'],
fmod=1)

return ([node], [a, b], [y])


@onnx_test
def mod_test_fmod_different_dtypes():
a = helper.make_tensor_value_info('0', TensorProto.FLOAT, [3, 3, 3])
b = helper.make_tensor_value_info('1', TensorProto.INT32, [3, 3, 3])
y = helper.make_tensor_value_info('2', TensorProto.FLOAT, [3, 3, 3])

node = onnx.helper.make_node(
'Mod',
inputs=['0', '1'],
outputs=['2'],
fmod=1 #fmod flag = 1
)

return ([node], [a, b], [y])


@onnx_test
def multinomial_test():
sample_size = 10
Expand Down
19 changes: 19 additions & 0 deletions test/onnx/mod_test.onnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
mod_test:e

0
12"Modmod_testZ
0



Z
1



b
2



B
19 changes: 19 additions & 0 deletions test/onnx/mod_test_different_dtypes.onnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
mod_test_different_dtypes:v

0
12"Modmod_test_different_dtypesZ
0



Z
1



b
2



B
20 changes: 20 additions & 0 deletions test/onnx/mod_test_fmod.onnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
mod_test_fmod:w

0
12"Mod*
fmod�mod_test_fmodZ
0



Z
1



b
2



B
Expand Down
Loading