Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
0401457
implementing op builder for binary operators
gchinora Oct 7, 2025
cad9f18
removing macro magic; adding the operator name as a parameter for the…
gchinora Oct 7, 2025
0b16733
testing framwork for op builders
gchinora Oct 7, 2025
7a26766
adding unit tests for the binary op-builder
gchinora Oct 7, 2025
a65f30c
modifying op-builder registration: op-builders will be able to regist…
gchinora Oct 12, 2025
3e98d88
change the way how we invoke insert_common_op. so we can see that it …
gchinora Oct 12, 2025
8819399
changing test; op_name parameter has been removed. also making sure t…
gchinora Oct 13, 2025
2658ef6
add implicit broadcast test
gchinora Oct 13, 2025
2719c04
removing broadcast and is_broadcasted parameter; also making the axis…
gchinora Oct 13, 2025
d559236
formatting document; change copyright date; remove const so less diff…
gchinora Oct 13, 2025
0a92c4e
modifying tf binary parser to use the op-builder
gchinora Oct 13, 2025
9159213
cherry picking modifications from the upstream/feature/op_bldr_unit_t…
gchinora Oct 14, 2025
d6acc43
modifying tf parser for AddN to use the binary op-builder
gchinora Oct 14, 2025
12e24f6
remove unnecessary header files
gchinora Oct 15, 2025
d7a7d53
introducing 'convert' op-builder
gchinora Oct 15, 2025
8caa382
introducing 'concat' op-builder
gchinora Oct 15, 2025
955a6ca
cherry pickjing modification from a different feature branch: making …
gchinora Oct 15, 2025
4d335bd
missing from the last commit.
gchinora Oct 15, 2025
288c0a9
introducing 'addn' op-builder
gchinora Oct 15, 2025
d0f1023
introducing 'tile' op-builder
gchinora Oct 15, 2025
cf62641
fixing document formatting
gchinora Oct 15, 2025
ba4220d
introducing 'constant' op-builder
gchinora Oct 21, 2025
d721bf1
fixing an issue: theconcat op-builder will use the input argument vec…
gchinora Oct 22, 2025
6543a3d
introducing 'general_op' op-builder. this implements a bunch of singl…
gchinora Oct 22, 2025
777620b
changing copyright date + change to const ref variable
gchinora Oct 22, 2025
6429412
make us of op-builders in TF parser as well
gchinora Oct 22, 2025
31910e9
fix license date
gchinora Oct 27, 2025
8902bf8
fixing formatting warning
gchinora Oct 27, 2025
650829e
fix. adding newline at the end of file
gchinora Oct 27, 2025
2662fd9
change shape type. for bitwise operations only integer types are supp…
gchinora Oct 27, 2025
c5e24fe
fixing license date
gchinora Oct 27, 2025
5ed7316
renaming tests; apply op-builder postfix
gchinora Oct 27, 2025
a1d7562
remove randomness
gchinora Oct 27, 2025
b020e0e
adding tests for the 'generic_op' op-builder
gchinora Oct 28, 2025
25cb277
fix tidy-check warning
gchinora Oct 28, 2025
51257e1
adding test for 'addn' op-builder
gchinora Oct 28, 2025
d5ea6cd
adding tests for the 'clip' op-builder
gchinora Oct 28, 2025
6132998
adding test for the 'concat' op-builder
gchinora Oct 28, 2025
5ff9956
adding test for the 'constant' op-builder
gchinora Oct 28, 2025
dafe01a
adding test for the 'convert' op-builder
gchinora Oct 28, 2025
f121e41
fixing tidy warnings
gchinora Oct 28, 2025
8508b37
fixing a runtime crash
gchinora Oct 28, 2025
438d5be
fixing formatting
gchinora Oct 28, 2025
476622d
adding test case; this is an edge case about which the coverage check…
gchinora Oct 28, 2025
3446027
fix formatting
gchinora Oct 28, 2025
fb38844
adding test for the 'tile' op-builder
gchinora Oct 28, 2025
ea4cea2
fixing tidy check warnings
gchinora Oct 28, 2025
efb02bc
fixing spelling typo
gchinora Nov 4, 2025
d8f6178
adding verifier test for "tile" op-builder
gchinora Nov 5, 2025
0232fc4
Merge branch 'develop' into upstream/feature/op_builders_4
gchinora Nov 7, 2025
cc32ca6
fixing a crash that happened in "test_py_3.10_backend" test
gchinora Nov 10, 2025
8336e34
adding op-builder helper function overload
gchinora Nov 10, 2025
b7879ac
fix formatting warning
gchinora Nov 10, 2025
8efbbc7
modifying the tile op-builder. instead of O(N^2); the number generate…
gchinora Nov 12, 2025
9b73bf6
fix formatting warning
gchinora Nov 12, 2025
170245c
fix tidy check warning
gchinora Nov 12, 2025
bc2cef7
addressing review comments
gchinora Nov 14, 2025
ea7e704
addressing review comments: broadcast should be applied to the shorte…
gchinora Nov 14, 2025
e91dc8f
addressing review comments: remove convert op-builder
gchinora Nov 14, 2025
c9ecda0
addressing review comments: rename binary op-bldr to pointwise
gchinora Nov 14, 2025
b72e682
addressing review comments: copilot advised change
gchinora Nov 14, 2025
f5fb0e9
addressing review comments: getting rid of contiguous case in generic…
gchinora Nov 14, 2025
f106462
addressing review comments: copilot advised change for readibility
gchinora Nov 14, 2025
9ddd7f6
addressing review comments: remove general_op op-builder; also introd…
gchinora Nov 17, 2025
a045e98
fixing formatting warning
gchinora Nov 17, 2025
49a88e9
fixing tidy-check warning
gchinora Nov 17, 2025
3ee9274
addressing new review comments
gchinora Dec 1, 2025
70564e0
fixing failing test after review comment related modifications
gchinora Dec 2, 2025
1415620
fixing cppcheck error
gchinora Dec 2, 2025
aca3da2
the only way to avoid tidy-check failure is to disable it for this li…
gchinora Dec 2, 2025
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
1 change: 1 addition & 0 deletions src/onnx/include/migraphx/onnx/onnx_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ struct onnx_parser
std::unordered_map<std::string, op_func> ops;

onnx_parser();
value load_to_value(const std::string& name, const node_info& info) const;
operation load(const std::string& name, const node_info& info) const;

void parse_undefined(module* mod, const std::string& name);
Expand Down
10 changes: 8 additions & 2 deletions src/onnx/onnx_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ onnx_parser::onnx_parser()
ops.emplace(name, get_op_parser(name));
}

operation onnx_parser::load(const std::string& name, const node_info& info) const
value onnx_parser::load_to_value(const std::string& name, const node_info& info) const
{
auto op = make_op(name);
Copy link
Collaborator

@pfultz2 pfultz2 Dec 3, 2025

Choose a reason for hiding this comment

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

We cant call make_op as this might be an op builder instead of an operation.

auto v = op.to_value();
Expand All @@ -228,7 +228,13 @@ operation onnx_parser::load(const std::string& name, const node_info& info) cons
s.visit([&](auto y) { x = y.front(); });
}
}
op.from_value(v);
return v;
}

operation onnx_parser::load(const std::string& name, const node_info& info) const
{
auto op = make_op(name);
op.from_value(load_to_value(name, info));
return op;
}

Expand Down
22 changes: 9 additions & 13 deletions src/onnx/parse_binary_op.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved.
* Copyright (c) 2015-2025 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
Expand All @@ -23,8 +23,8 @@
*/
#include <migraphx/onnx/op_parser.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/op/builder/insert.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/make_op.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
Expand All @@ -48,10 +48,12 @@ struct parse_binary_op : op_parser<parse_binary_op>
instruction_ref parse(const op_desc& opd,
const onnx_parser& parser,
onnx_parser::node_info info,
std::vector<instruction_ref> args) const
const std::vector<instruction_ref>& args) const
{
if(args.size() != 2)
MIGRAPHX_THROW("binary operators should have 2 operands");

value options("", {}, false);
if(contains(info.attributes, "broadcast") and contains(info.attributes, "axis"))
{
uint64_t broadcasted =
Expand All @@ -64,19 +66,13 @@ struct parse_binary_op : op_parser<parse_binary_op>
MIGRAPHX_THROW(
"Binary op broadcast attribute not supported for dynamic input shapes");
}

uint64_t axis = parser.parse_value(info.attributes.at("axis")).at<uint64_t>();
auto l = info.add_instruction(
make_op("broadcast",
{{"axis", axis}, {"out_lens", args[0]->get_shape().lens()}}),
args[1]);
return info.add_instruction(make_op(opd.op_name), args[0], l);
options.insert({"broadcasted_axis", axis});
}
return info.add_instruction(make_op(opd.op_name), args);
}
else
{
return info.add_broadcastable_binary_op(opd.op_name, args[0], args[1]);
}

return op::builder::add(opd.op_name, *info.mod, args, options).at(0);
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/onnx/parse_clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct parse_clip : op_parser<parse_clip>
args.push_back(info.add_literal(max_val));
}

return op::builder::add("clip", *info.mod, args, {}).at(0);
return op::builder::add("clip", *info.mod, args).at(0);
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/onnx/parse_gelu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct parse_gelu : op_parser<parse_gelu>
{
// add should've been inserted from previous conditional statement
assert(args.size() == 2);
return op::builder::add("gelu_split", *info.mod, {x}, {}).at(0);
return op::builder::add("gelu_split", *info.mod, {x}).at(0);
}

if(approximate == "tanh")
Expand All @@ -97,7 +97,7 @@ struct parse_gelu : op_parser<parse_gelu>
}
else
{
return op::builder::add("gelu_erf", *info.mod, {x}, {}).at(0);
return op::builder::add("gelu_erf", *info.mod, {x}).at(0);
}
}
};
Expand Down
24 changes: 6 additions & 18 deletions src/onnx/parse_generic_op.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved.
* Copyright (c) 2015-2025 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
Expand All @@ -22,8 +22,7 @@
* THE SOFTWARE.
*/
#include <migraphx/onnx/op_parser.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/op/builder/insert.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
Expand Down Expand Up @@ -72,24 +71,13 @@ struct parse_generic_op : op_parser<parse_generic_op>
// clang-format on
}

bool needs_contiguous(const std::string& op_name) const
{
return contains({"flatten", "gather", "scatter"}, op_name);
}

instruction_ref parse(const op_desc& opd,
const onnx_parser& parser,
onnx_parser::node_info info,
std::vector<instruction_ref> args) const
const onnx_parser::node_info& info,
const std::vector<instruction_ref>& args) const
{
auto op = parser.load(opd.op_name, info);
if(needs_contiguous(opd.op_name))
{
std::transform(args.begin(), args.end(), args.begin(), [&](auto arg) {
return info.make_contiguous(arg);
});
}
return info.add_instruction(op, args);
const auto& val = parser.load_to_value(opd.op_name, info);
return op::builder::add(opd.op_name, *info.mod, args, val).at(0);
}
};

Expand Down
16 changes: 3 additions & 13 deletions src/onnx/parse_tile.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
* Copyright (c) 2015-2025 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
Expand All @@ -23,9 +23,8 @@
*/
#include <migraphx/onnx/op_parser.hpp>
#include <migraphx/onnx/checks.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/op/builder/insert.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
Expand All @@ -45,16 +44,7 @@ struct parse_tile : op_parser<parse_tile>
std::vector<std::int64_t> repeats;
arg_s.visit([&](auto input) { repeats.assign(input.begin(), input.end()); });

auto l0 = args[0];
for(int i = 0; i < repeats.size(); i++)
{
auto l1 = l0;
for(int j = 1; j < repeats[i]; j++)
{
l0 = info.add_instruction(make_op("concat", {{"axis", i}}), l0, l1);
}
}
return l0;
return op::builder::add("tile", *info.mod, args, {{"repeats", repeats}}).at(0);
}
};

Expand Down
6 changes: 0 additions & 6 deletions src/op/builder/clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ namespace builder {

struct clip : op_builder<clip>
{
template <class Self, class F>
static auto reflect(Self&, F)
{
return pack();
}

std::vector<instruction_ref>
insert(module& m, instruction_ref ins, const std::vector<instruction_ref>& args) const
{
Expand Down
14 changes: 1 addition & 13 deletions src/op/builder/gelu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ struct gelu_quick : op_builder<gelu_quick>

struct gelu_erf : op_builder<gelu_erf>
{
template <class Self, class F>
static auto reflect(Self&, F)
{
return pack();
}

std::vector<instruction_ref>
insert(module& m, instruction_ref ins, const std::vector<instruction_ref>& args) const
{
Expand Down Expand Up @@ -130,12 +124,6 @@ struct gelu_tanh : op_builder<gelu_tanh>

struct gelu_split : op_builder<gelu_split>
{
template <class Self, class F>
static auto reflect(Self&, F)
{
return pack();
}

std::vector<instruction_ref>
insert(module& m, instruction_ref ins, const std::vector<instruction_ref>& args) const
{
Expand All @@ -155,7 +143,7 @@ struct gelu_split : op_builder<gelu_split>
{{"axes", {-1}}, {"starts", {last_dim_size / 2}}, {"ends", {last_dim_size}}}),
x);

auto gelu_erf = op::builder::add("gelu_erf", m, {split_right}, {}).at(0);
auto gelu_erf = op::builder::add("gelu_erf", m, {split_right}).at(0);
return {insert_common_op(m, ins, "mul", split_left, gelu_erf)};
}
};
Expand Down
10 changes: 6 additions & 4 deletions src/op/builder/include/migraphx/op/builder/insert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,31 @@ inline namespace MIGRAPHX_INLINE_NS {
namespace op {
namespace builder {

value get_default_options();

MIGRAPHX_EXPORT std::vector<instruction_ref> insert(const std::string& name,
module& m,
instruction_ref ins,
const std::vector<instruction_ref>& args,
const value& options);
const value& options = get_default_options());

MIGRAPHX_EXPORT std::vector<instruction_ref> insert(const std::string& name,
module& m,
instruction_ref ins,
const std::vector<instruction_ref>& args,
const std::vector<module_ref>& module_args,
const value& options);
const value& options = get_default_options());

MIGRAPHX_EXPORT std::vector<instruction_ref> add(const std::string& name,
module& m,
const std::vector<instruction_ref>& args,
const value& options);
const value& options = get_default_options());

MIGRAPHX_EXPORT std::vector<instruction_ref> add(const std::string& name,
module& m,
const std::vector<instruction_ref>& args,
const std::vector<module_ref>& module_args,
const value& options);
const value& options = get_default_options());
template <class... Ins>
instruction_ref
insert_common_op(module& m, instruction_ref ins, const std::string& op_name, Ins... args)
Expand Down
55 changes: 43 additions & 12 deletions src/op/builder/include/migraphx/op/builder/op_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ using builder_func =
MIGRAPHX_EXPORT void register_builder(const std::string& name, builder_func f);

template <class T>
auto invoke_builder(module& m,
auto invoke_builder(const std::string& /*name*/,
module& m,
instruction_ref ins,
const std::vector<instruction_ref>& args,
const std::vector<module_ref>& module_args,
Expand All @@ -57,7 +58,8 @@ auto invoke_builder(module& m,
}

template <class T>
auto invoke_builder(module& m,
auto invoke_builder(const std::string& /*name*/,
module& m,
instruction_ref ins,
const std::vector<instruction_ref>& args,
const std::vector<module_ref>& module_args,
Expand All @@ -69,17 +71,46 @@ auto invoke_builder(module& m,
return x.insert(m, ins, args);
}

template <class T>
auto invoke_builder(const std::string& name,
module& m,
instruction_ref ins,
const std::vector<instruction_ref>& args,
const std::vector<module_ref>& module_args,
const value& options) -> decltype(T{}.insert(name, m, ins, args, module_args))
{
auto x = from_value<T>(options);
return x.insert(name, m, ins, args, module_args);
}

template <class T>
auto invoke_builder(const std::string& name,
module& m,
instruction_ref ins,
const std::vector<instruction_ref>& args,
const std::vector<module_ref>& module_args,
const value& options) -> decltype(T{}.insert(name, m, ins, args))
{
if(not module_args.empty())
MIGRAPHX_THROW("Module args should be empty");
auto x = from_value<T>(options);
return x.insert(name, m, ins, args);
}

template <class T>
void register_builder()
{
builder_func f = [](module& m,
instruction_ref ins,
const std::vector<instruction_ref>& args,
const std::vector<module_ref>& module_args,
const value& options) {
return invoke_builder<T>(m, ins, args, module_args, options);
};
register_builder(T::name(), std::move(f));
for(const auto& name : T::names())
{
builder_func f = [=](module& m,
instruction_ref ins,
const std::vector<instruction_ref>& args,
const std::vector<module_ref>& module_args,
const value& options) {
return invoke_builder<T>(name, m, ins, args, module_args, options);
};
register_builder(name, std::move(f));
}
}

struct register_builder_action
Expand All @@ -94,10 +125,10 @@ struct register_builder_action
template <class T>
struct op_builder : auto_register<register_builder_action, T>
{
static std::string name()
static std::vector<std::string> names()
{
static const std::string& name = get_type_name<T>();
return name.substr(name.rfind("::") + 2);
return {name.substr(name.rfind("::") + 2)};
}
};

Expand Down
Loading
Loading