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

Fix dequantizelinaer simple case with no zero point present #3526

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 6 additions & 3 deletions src/include/migraphx/op/dequantizelinear.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ struct dequantizelinear
std::string name() const { return "dequantizelinear"; }
shape compute_shape(std::vector<shape> inputs) const
{
check_shapes{inputs, *this}.same_dims().has(2, 3);
if(inputs.size() == 3 and inputs[0].type() != inputs[2].type())
if(inputs.size() == 3)
lakhinderwalia marked this conversation as resolved.
Show resolved Hide resolved
{
MIGRAPHX_THROW("DEQUANTIZELINEAR: Zero point and input should be the same type.");
check_shapes{inputs, *this}.same_dims().has(2, 3);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this moved down here? We should still check that the inputs are the same dims even when 2 inputs are given. It makes sense up at the top.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That wont be the case though in this operator would it? I guess we broadcast out scale if its a scalar or its 1-d for the case of scale being a tensor

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's where the issue was coming from when invoking dequantize_linear, zero points weren't present and resulted in an error.

Copy link
Collaborator

Choose a reason for hiding this comment

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

What was the error specifically? This allows either 2 or 3 inputs so it shouldn't throw an error for 2 inputs.

Copy link
Collaborator Author

@TedThemistokleous TedThemistokleous Oct 14, 2024

Choose a reason for hiding this comment

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

image

would invoke

terminate called after throwing an instance of 'migraphx::version_1::exception'
  what():  /code/AMDMIGraphX/src/include/migraphx/check_shapes.hpp:220: same_dims: dequantizelinear: Dimensions do not match'''
  

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Unless you want us to always broadcast the scale dimensions (even if its 1-d) but then that means we're inserting a broadcast to remove later. For the scale here, we're never supposed to have the same dimensions between input and scale vectors. Its why I gated this check by input size.

Copy link
Contributor

@shivadbhavsar shivadbhavsar Oct 14, 2024

Choose a reason for hiding this comment

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

Actually it looks like the op is intended to work this way where the scales and zero points should already be broadcasted. The parser for dequantizelinear also puts this broadcast in. I dont think this is a bug, your test case should have a broadcast for the scales.

Im not sure what you mean by remove later?

if(inputs[0].type() != inputs[2].type())
{
MIGRAPHX_THROW("DEQUANTIZELINEAR: Zero point and input should be the same type.");
}
}
return {inputs[1].type(), inputs[0].lens(), inputs[0].strides()};
}
Expand Down
82 changes: 82 additions & 0 deletions test/onnx/verify/dequantizelinear_simple_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2024 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>
#include <onnx_verify_utils.hpp>

TEST_CASE(dequantizelinear_simple_no_zp_test)
{
migraphx::program p = read_onnx("dequantizelinear_test.onnx");
p.compile(migraphx::make_target("ref"));

migraphx::shape x_shape{migraphx::shape::int8_type, {5}};
std::vector<int8_t> x = {4, 8, 20, 2, 0};

migraphx::shape scale_shape{migraphx::shape::float_type, {1}, {1}};
std::vector<float> scale = {2.0f};

migraphx::parameter_map pm;
pm["0"] = migraphx::argument{x_shape, x.data()};
pm["1"] = migraphx::argument{scale_shape, scale.data()};

auto result = p.eval(pm).back();
EXPECT(result.get_shape() == migraphx::shape{migraphx::shape::float_type, {5}});

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

std::vector<float> gold = {8.0f, 16.0f, 40.0f, 4.0f, 0.0f};
EXPECT(migraphx::verify::verify_rms_range(result_vector, gold));
}

TEST_CASE(dequantizelinear_simple_with_zp_test)
{
migraphx::program p = read_onnx("dequantizelinear_zero_point_test.onnx");
p.compile(migraphx::make_target("ref"));

migraphx::shape x_shape{migraphx::shape::int8_type, {5}};
std::vector<int8_t> x = {4, 8, 20, 2, 0};

migraphx::shape scale_shape{migraphx::shape::float_type, {1}, {1}};
std::vector<float> scale = {2.0f};

migraphx::shape zp_shape{migraphx::shape::int8_type, {1}, {1}};
std::vector<int8_t> zp = {20};

migraphx::parameter_map pm;
pm["0"] = migraphx::argument{x_shape, x.data()};
pm["1"] = migraphx::argument{scale_shape, scale.data()};
pm["2"] = migraphx::argument{zp_shape, zp.data()};

auto result = p.eval(pm).back();
EXPECT(result.get_shape() == migraphx::shape{migraphx::shape::float_type, {5}});

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

std::vector<float> gold = {-32.0f, -24.0f, 0.0f, -36.0f, -40.0f};
EXPECT(migraphx::verify::verify_rms_range(result_vector, gold));
}
Loading