Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
17 changes: 16 additions & 1 deletion src/common.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2025 Advanced Micro Devices, Inc. All rights reserved.
* Copyright (c) 2015-2026 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 Down Expand Up @@ -195,6 +195,21 @@
else
{
auto common = common_shape(to_shapes(inputs));
if(options.common_type)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Add a comment explaining the reasoning for this added code block

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added

{
std::vector<shape> tensor_shapes;
for(const auto& input : inputs)
{
if(not(input->can_eval() and input->get_shape().elements() == 1))

Check warning on line 203 in src/common.cpp

View workflow job for this annotation

GitHub Actions / tidy

boolean expression can be simplified by DeMorgan's theorem [readability-simplify-boolean-expr,-warnings-as-errors]
tensor_shapes.push_back(input->get_shape());
}
if(not tensor_shapes.empty())
{
auto tensor_type = compute_common_types(tensor_shapes);
if(tensor_type != common.type())
common = shape{tensor_type, common.lens()};
}
}
std::transform(inputs.begin(), inputs.end(), inputs.begin(), [&](auto input) {
if(options.common_lens and input->get_shape().lens() != common.lens())
{
Expand Down
9 changes: 7 additions & 2 deletions src/onnx/parse_gru.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2025 Advanced Micro Devices, Inc. All rights reserved.
* Copyright (c) 2015-2026 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 Down Expand Up @@ -61,7 +61,7 @@
{
std::vector<op_desc> operators() const { return {{"GRU"}}; }

std::vector<instruction_ref> parse(const op_desc& /*opd*/,

Check warning on line 64 in src/onnx/parse_gru.cpp

View workflow job for this annotation

GitHub Actions / tidy

function 'parse' exceeds recommended size/complexity thresholds [readability-function-size,-warnings-as-errors]
const onnx_parser& parser,
onnx_parser::node_info info,
std::vector<instruction_ref> args) const
Expand Down Expand Up @@ -166,7 +166,12 @@
{
gru_transpose_inputs(info, args);
}

if(not args[5]->is_undefined() and
args[5]->get_shape().type() != args[1]->get_shape().type())
{
args[5] = info.add_instruction(
make_op("convert", {{"target_type", args[1]->get_shape().type()}}), args[5]);
}
Comment on lines +169 to +174
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why is there a specific change for GRU?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So, for this specific model just literal change only helped with migraphx driver read command / compile was still failing with types do not match. The fix is in parse_gru.cpp because the type mismatch is introduced by the rewrite_rnn pass, which decomposes the GRU op into dot operations using m.insert_instruction(make_op("dot"), ...) directl; bypassing add_common_op and its type reconciliation. I agree this is odd behaviour; i will send you model via teams; for you to have better understanding of it.

// first output for concatenation of hidden states
auto hidden_states =
info.add_instruction(make_op("gru",
Expand Down
85 changes: 85 additions & 0 deletions test/common_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2026 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 "test.hpp"
#include <migraphx/common.hpp>
#include <migraphx/module.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/literal.hpp>
#include <migraphx/instruction.hpp>

TEST_CASE(add_common_op_scalar_literal_preserves_tensor_type)
{
migraphx::module mm;

auto tensor = mm.add_parameter("x", migraphx::shape{migraphx::shape::half_type, {1, 100, 128}});
auto scalar =
mm.add_literal(migraphx::literal{migraphx::shape{migraphx::shape::float_type}, {1e-5f}});

auto result = migraphx::add_common_op(mm, migraphx::make_op("add"), {tensor, scalar});

EXPECT(result->get_shape().type() == migraphx::shape::half_type);
}

TEST_CASE(add_common_op_scalar_literal_preserves_tensor_type_reversed)
{
migraphx::module mm;

auto tensor = mm.add_parameter("x", migraphx::shape{migraphx::shape::half_type, {1, 100, 128}});
auto scalar =
mm.add_literal(migraphx::literal{migraphx::shape{migraphx::shape::float_type}, {1e-5f}});

auto result = migraphx::add_common_op(mm, migraphx::make_op("add"), {scalar, tensor});

EXPECT(result->get_shape().type() == migraphx::shape::half_type);
}

TEST_CASE(add_common_op_two_tensors_promotes_to_wider_type)
{
migraphx::module mm;

auto half_tensor = mm.add_parameter("x", migraphx::shape{migraphx::shape::half_type, {1, 128}});
auto float_tensor =
mm.add_parameter("y", migraphx::shape{migraphx::shape::float_type, {1, 128}});

auto result =
migraphx::add_common_op(mm, migraphx::make_op("add"), {half_tensor, float_tensor});

EXPECT(result->get_shape().type() == migraphx::shape::float_type);
}

TEST_CASE(add_common_op_float_tensor_with_float_scalar_keeps_float)
{
migraphx::module mm;

auto tensor =
mm.add_parameter("x", migraphx::shape{migraphx::shape::float_type, {1, 100, 128}});
auto scalar =
mm.add_literal(migraphx::literal{migraphx::shape{migraphx::shape::float_type}, {1e-5f}});

auto result = migraphx::add_common_op(mm, migraphx::make_op("add"), {tensor, scalar});

EXPECT(result->get_shape().type() == migraphx::shape::float_type);
}

int main(int argc, const char* argv[]) { test::run(argc, argv); }
Loading