Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
154 changes: 121 additions & 33 deletions src/include/migraphx/op/resize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ struct resize
{
check_shapes{inputs, *this, true}.has(1, 2);

if(mode != "nearest")
MIGRAPHX_THROW("RESIZE: Only Nearest mode is supported");
// Allow nearest and linear; still reject others
if(mode != "nearest" and mode != "linear")
MIGRAPHX_THROW("RESIZE: Only 'nearest' and 'linear' modes are supported");

// Inputs are X, sizes or scale, ROI and axes not supported.
if(inputs.size() == 1)
Expand Down Expand Up @@ -203,9 +204,19 @@ struct resize
// compute() method. For any other target, there must be a compiler pass that replaces
// this operation with a fixed-size output at runtime.
std::size_t max_val = std::numeric_limits<std::size_t>::max();
std::vector<shape::dynamic_dimension> dyn_dims(inputs.back().lens().at(0),
shape::dynamic_dimension{0, max_val});
return {inputs.front().type(), dyn_dims};
auto input = inputs.front();
std::vector<shape::dynamic_dimension> dyn_dims(input.ndim(), {0, max_val});
Copy link
Contributor

Choose a reason for hiding this comment

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

This (line) is pre-existing. But conceptually, why set dyn_dims to {0, max_val}. Surely, size 0 is too small for a resize operator. Thanks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was to signify that no tensor information is known at compile time. We may update this to be {1, max_val} because a dimension can never be < 1, but we need to do this in other places in the codebase for consistency.


if(not scales.empty())
{
for(std::size_t i = 0; i < scales.size(); i++)
{
dyn_dims[i].min = static_cast<std::size_t>(input.dyn_dims()[i].min * scales[i]);
dyn_dims[i].max = static_cast<std::size_t>(input.dyn_dims()[i].max * scales[i]);
Copy link
Contributor

Choose a reason for hiding this comment

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

if scale[i] > 1, then won't multiplying it with max_val overflow?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added a check if input.dyn_dims()[i].max is equal to max_val, then ignore. We could probably add better bounds checking but for now I think this is should be good enough.

}
}

return {input.type(), dyn_dims};
}
}

Expand Down Expand Up @@ -258,50 +269,127 @@ struct resize
// Copy the output size from args[1].
std::copy(input.begin(), input.end(), out_lens.begin());
// Deduce the scales for each axis
std::transform(
input.begin(),
input.end(),
in_lens.begin(),
vec_scale.begin(),
[](auto sz, size_t in_len) { return static_cast<float>(sz) / in_len; });
std::transform(input.begin(),
input.end(),
in_lens.begin(),
vec_scale.begin(),
[](auto sz, size_t in_len) { return static_cast<float>(sz) / in_len; });
}
else
{
// read the scale from args[1]
//
std::copy(input.begin(), input.end(), vec_scale.begin());
// compute the output dimensions from the given scales. This computation
// always rounds down, unlike the internal computation in Nearest mode
// which has several options as given in nearest_mode.
std::transform(input.begin(),
input.end(),
in_lens.begin(),
out_lens.begin(),
[](auto scale_i, size_t in_len) {
return static_cast<size_t>(scale_i * in_len);
});
input.end(),
in_lens.begin(),
out_lens.begin(),
[](auto scale_i, size_t in_len) {
return static_cast<size_t>(scale_i * in_len);
});
}
});
}

shape output_shape = {args[0].get_shape().type(), out_lens};
argument result{output_shape};
auto nearest_op = get_nearest_op(nearest_mode);
auto idx_op = get_original_idx_op(coordinate_transformation_mode);

// Populate each element in output by selecting "nearest" item in input.
visit_all(result, args[0])([&](auto output, auto data) {
migraphx::shape out_comp_shape{data.get_shape().type(), out_lens};
shape_for_each(out_comp_shape, [&](const auto& out_idx_v, size_t out_idx) {
std::vector<size_t> in_idx(out_idx_v.size());
for(auto ii = 0; ii < out_idx_v.size(); ++ii)
{
auto idx_val = idx_op(in_lens[ii], out_lens[ii], out_idx_v[ii], vec_scale[ii]);
in_idx[ii] = nearest_op(in_lens[ii], idx_val);
}
output[out_idx] = data(in_idx.begin(), in_idx.end());

auto idx_op = get_original_idx_op(coordinate_transformation_mode);

if(mode == "nearest")
{
auto nearest_op = get_nearest_op(nearest_mode);
// Populate each element in output by selecting "nearest" item in input.
visit_all(result, args[0])([&](auto output, auto data) {
migraphx::shape out_comp_shape{data.get_shape().type(), out_lens};
shape_for_each(out_comp_shape, [&](const auto& out_idx_v, size_t out_idx) {
std::vector<size_t> in_idx(out_idx_v.size());
for(std::size_t ii = 0; ii < out_idx_v.size(); ++ii)
{
auto idx_val = idx_op(in_lens[ii], out_lens[ii], out_idx_v[ii], vec_scale[ii]);
in_idx[ii] = nearest_op(in_lens[ii], idx_val);
}
output[out_idx] = data(in_idx.begin(), in_idx.end());
});
});
});
}
else if(mode == "linear")
{
// N-D multilinear interpolation
visit_all(result, args[0])([&](auto output, auto data) {
using in_value_t = typename decltype(data)::value_type;
using acc_type = double; // accumulate in double for precision

migraphx::shape out_comp_shape{data.get_shape().type(), out_lens};
shape_for_each(out_comp_shape, [&](const auto& out_idx_v, size_t out_idx) {
const std::size_t ndim = out_idx_v.size();

// Precompute base indices and weights per dimension
std::vector<std::size_t> i0(ndim);
std::vector<std::size_t> i1(ndim);
std::vector<double> t(ndim);

for(std::size_t d = 0; d < ndim; d++)
{
// Compute the original floating-point coordinate per coordinate_transformation_mode
double coord = idx_op(in_lens[d], out_lens[d], out_idx_v[d], vec_scale[d]);

// Clamp to valid input range [0, in_lens[d]-1]
double max_c = in_lens[d] > 0 ? static_cast<double>(in_lens[d] - 1) : 0.0;
coord = std::max(0.0, std::min(max_c, coord));

std::size_t base = static_cast<std::size_t>(std::floor(coord));
std::size_t next = std::min(base + 1, (in_lens[d] == 0 ? 0 : in_lens[d] - 1));
double frac = coord - static_cast<double>(base);

// Handle degenerate dimension (length 1) to avoid NaNs
if(in_lens[d] <= 1)
{
base = 0;
next = 0;
frac = 0.0;
}

i0[d] = base;
i1[d] = next;
t[d] = frac;
}

// Accumulate over 2^ndim corners
acc_type acc = 0.0;
const std::size_t corners = (ndim == 0) ? 1 : (1ULL << ndim);
std::vector<std::size_t> in_idx(ndim);

for(std::size_t mask = 0; mask < corners; ++mask)
{
double w = 1.0;
for(std::size_t d = 0; d < ndim; ++d)
{
const bool use_high = ((mask >> d) & 1U) != 0U;
w *= use_high ? t[d] : (1.0 - t[d]);
in_idx[d] = use_high ? i1[d] : i0[d];
}

if(w != 0.0)
{
in_value_t v = data(in_idx.begin(), in_idx.end());
acc += w * static_cast<acc_type>(v);
}
}

// Cast back to the output element type
using out_value_t = typename decltype(output)::value_type;
output[out_idx] = static_cast<out_value_t>(acc);
});
});
}
else
{
MIGRAPHX_THROW("RESIZE: Unsupported mode in compute()");
}

return result;
}
};
Expand Down
14 changes: 14 additions & 0 deletions src/onnx/parse_resize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,20 @@ struct parse_resize : op_parser<parse_resize>
auto out_lens = resize.out_lens;
auto vec_scale = resize.vec_scale;

if(args_0->get_shape().dynamic())
{
// Resize's compute_shape() will read scales_sizes_arg as "scales" or "sizes"
// depending on its data type

return info.add_instruction(
make_op("resize",
{{"mode", "linear"},
{"scales", vec_scale},
{"coordinate_transformation_mode", resize.get_coord_trans_mode()}}),
args_0,
resize.get_scales_sizes_arg());
}
Comment on lines +473 to +485
Copy link

Copilot AI Oct 14, 2025

Choose a reason for hiding this comment

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

This path hardcodes mode to 'linear' and also sets the 'scales' attribute while simultaneously providing a second input (sizes/scales). That can: (1) override the original ONNX mode (e.g., 'nearest') for dynamic inputs, and (2) create inconsistent behavior by specifying scales both as an attribute and as an input. Fix by propagating the parsed mode and avoiding the 'scales' attribute when a scales/sizes input is provided. For example: make_op('resize', {{'mode', resize.get_mode()}, {'coordinate_transformation_mode', resize.get_coord_trans_mode()}}) with args_0 and resize.get_scales_sizes_arg().

Copilot uses AI. Check for mistakes.

// out_lens and other variables can't be populated if non-constant (runtime) size
// inputs.
if(not resize.is_constant_scale_input())
Expand Down
51 changes: 51 additions & 0 deletions test/onnx/verify/resize_downsample_linear_dyn_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* The MIT License (MIT)
*
* 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
* 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/register_target.hpp>
#include <migraphx/verify.hpp>
#include <onnx_test.hpp>

TEST_CASE(resize_downsample_linear_dyn_test)
{
using migraphx::half;
migraphx::onnx_options options;
options.map_dyn_input_dims = {{"X", {{1, 1}, {1, 1}, {2, 3}, {4, 8}}}};
migraphx::program p = read_onnx("resize_downsample_linear_half_test.onnx");
p.compile(migraphx::make_target("ref"));

migraphx::shape sx{migraphx::shape::half_type, {1, 1, 2, 4}};
std::vector<half> dx = {half{1}, half{2}, half{3}, half{4}, half{5}, half{6}, half{7}, half{8}};

migraphx::parameter_map pp;
pp["X"] = migraphx::argument(sx, dx.data());

auto result = p.eval(pp).back();
std::vector<half> result_vector;
result.visit([&](auto output) { result_vector.assign(output.begin(), output.end()); });

// Expected output was calculated without any quantization
std::vector<half> gold = {half{2.8333333}, half{4.833333}};

EXPECT(migraphx::verify::verify_rms_range(result_vector, gold));
}
51 changes: 51 additions & 0 deletions test/onnx/verify/resize_upsample_linear_dyn_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* The MIT License (MIT)
*
* 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
* 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/register_target.hpp>
#include <migraphx/verify.hpp>
#include <onnx_test.hpp>

TEST_CASE(resize_upsample_linear_dyn_test)
{
migraphx::onnx_options options;
options.map_dyn_input_dims = {{"X", {{1, 1}, {1, 1}, {2, 3}, {2, 3}}}};

migraphx::program p = read_onnx("resize_upsample_linear_test.onnx", options);
p.compile(migraphx::make_target("ref"));

migraphx::shape sx{migraphx::shape::float_type, {1, 1, 2, 2}};
std::vector<float> dx = {1.0f, 2.0f, 3.0f, 4.0f};

migraphx::parameter_map pp;
pp["X"] = migraphx::argument(sx, dx.data());

auto result = p.eval(pp).back();
std::vector<float> result_vector;
result.visit([&](auto output) { result_vector.assign(output.begin(), output.end()); });

std::vector<float> gold = {
1, 1.25, 1.75, 2, 1.5, 1.75, 2.25, 2.5, 2.5, 2.75, 3.25, 3.5, 3, 3.25, 3.75, 4};

EXPECT(migraphx::verify::verify_rms_range(result_vector, gold));
}
Loading