Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/onnx/parse_resize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ struct parse_resize : op_parser<parse_resize>
}
});

auto ind = calc_neighbor_points(vvv_ind, in_s, out_s, resized_m);
auto ind = calc_neighbor_points(vvv_ind, in_s.as_standard(), out_s, resized_m);

auto dim_lens = out_lens;
// indices matrix size grows 2x per resized-axis:
Expand Down
39 changes: 39 additions & 0 deletions test/onnx/gen_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -13022,6 +13022,45 @@ def resize_with_same_inout_shapes_test():

return ([node], [X], [Y], [sizes_tensor])


@onnx_test()
def resize_nhwc_test():
# Test resize with NHWC layout (non-standard shape)
# Original NCHW shape: [1, 3, 2, 2]
# Transposed to NHWC: [1, 2, 2, 3]
scales = np.array([1.0, 2.0, 2.0, 1.0], dtype=np.float32)
scale_tensor = helper.make_tensor(name='scales',
data_type=TensorProto.FLOAT,
dims=scales.shape,
vals=scales.flatten().astype(np.float32))

X = helper.make_tensor_value_info('X', TensorProto.FLOAT, [1, 3, 2, 2])
TX = helper.make_tensor_value_info('TX', TensorProto.FLOAT, [1, 2, 2, 3])
TY = helper.make_tensor_value_info('TY', TensorProto.FLOAT, [1, 4, 4, 3])
Y = helper.make_tensor_value_info('Y', TensorProto.FLOAT, [1, 3, 4, 4])

# Transpose NCHW to NHWC
trn1 = onnx.helper.make_node('Transpose',
inputs=['X'],
outputs=['TX'],
perm=[0, 2, 3, 1])

# Resize in NHWC format
resize = onnx.helper.make_node('Resize',
inputs=['TX', '', 'scales'],
outputs=['TY'],
coordinate_transformation_mode='asymmetric',
mode='linear')

# Transpose back NHWC to NCHW
trn2 = onnx.helper.make_node('Transpose',
inputs=['TY'],
outputs=['Y'],
perm=[0, 3, 1, 2])

return ([trn1, resize, trn2], [X], [Y], [scale_tensor])


@onnx_test()
def reversesequence_4D_test():
x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [2, 2, 2, 2])
Expand Down
Binary file added test/onnx/resize_nhwc_test.onnx
Binary file not shown.
84 changes: 84 additions & 0 deletions test/onnx/verify/resize_nhwc_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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_nhwc_test)
{
migraphx::program p = read_onnx("resize_nhwc_test.onnx");
p.compile(migraphx::make_target("ref"));

// Input shape: [1, 3, 2, 2] (NCHW) - using smaller size for easier verification
migraphx::shape sx{migraphx::shape::float_type, {1, 3, 2, 2}};
// clang-format off
std::vector<float> dx =
{
0.0f, 1.0f, 2.0f, 3.0f, // Channel 0
4.0f, 5.0f, 6.0f, 7.0f, // Channel 1
8.0f, 9.0f, 10.0f, 11.0f // Channel 2
};
// clang-format on
migraphx::parameter_map pp;
pp["X"] = migraphx::argument(sx, dx.data());

auto result = p.eval(pp).back();
auto result_vector = result.to_vector<float>();

// The output should be [1, 3, 4, 4] after:
// 1. Transpose to NHWC [1, 2, 2, 3]
// 2. Resize with scales [1.0, 2.0, 2.0, 1.0] -> [1, 4, 4, 3]
// 3. Transpose back to NCHW [1, 3, 4, 4]

// Expected output size
EXPECT(result_vector.size() == 1 * 3 * 4 * 4);

// Verify output shape
EXPECT(result.get_shape() == migraphx::shape{migraphx::shape::float_type, {1, 3, 4, 4}});

// Expected golden values for resize with asymmetric coordinate transformation
// and linear interpolation mode
// clang-format off
std::vector<float> gold =
{
// Channel 0
0.0f, 0.5f, 1.0f, 1.0f,
1.0f, 1.5f, 2.0f, 2.0f,
2.0f, 2.5f, 3.0f, 3.0f,
2.0f, 2.5f, 3.0f, 3.0f,
// Channel 1
4.0f, 4.5f, 5.0f, 5.0f,
5.0f, 5.5f, 6.0f, 6.0f,
6.0f, 6.5f, 7.0f, 7.0f,
6.0f, 6.5f, 7.0f, 7.0f,
// Channel 2
8.0f, 8.5f, 9.0f, 9.0f,
9.0f, 9.5f, 10.0f, 10.0f,
10.0f, 10.5f, 11.0f, 11.0f,
10.0f, 10.5f, 11.0f, 11.0f
};
// clang-format on
EXPECT(migraphx::verify::verify_rms_range(result_vector, gold));
}
Loading