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

Add Qlinearconcat op #2476

Merged
merged 4 commits into from
Dec 12, 2023
Merged
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
105 changes: 105 additions & 0 deletions src/onnx/parse_qlinearconcat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2023 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/onnx/op_parser.hpp>
#include <migraphx/onnx/padding.hpp>
#include <migraphx/onnx/conv.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/onnx/checks.hpp>
#include <migraphx/onnx/broadcast_qdq.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/stringutils.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace onnx {

struct parse_qlinearconcat : op_parser<parse_qlinearconcat>
{
std::vector<op_desc> operators() const { return {{"QLinearConcat"}}; }

// basic type checking for QLinearConcat Operator
void check_inputs(const std::vector<instruction_ref>& args) const
{
auto args_size = args.size();
// at least 5 input tensors:
// 1. is Y_scale: tensor(float)
// 2. is Y_zero_pont: tensor(uint8)/tensor(int8)
// remaining is a sequence of :
// 3. Tensor: tensor(uint8)/tensor(int8)
// 4. Scale: tensor(float),
// 5. ZeroPoint: tensor(uint8)/tensor(int8) tensors
// Size can be 5, 8, 11 ...
if((args_size < 5) or ((args_size - 2) % 3 != 0))
MIGRAPHX_THROW("QLINEARCONCAT: missing inputs");
Comment on lines +55 to +56
Copy link
Collaborator

Choose a reason for hiding this comment

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

Aren't the inputs supposed to be tuples? As in each input with type TV is a tuple of (Tensor, Scale, ZeroPoint). The spec says (3 - inf) inputs, so that's what I would expect.

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've checked the models in the description and those inputs are parsed just a simple sequence of tensors, I can't see any tuple type for shapes there.
For MaskRCNN-int8 I can see the following inputs for QLinearConcat operator:

shape: float_type, {1}, {0}
shape: uint8_type, {1}, {0}
shape: uint8_type, {1000, 4}, {4, 1}
shape: float_type, {1}, {0}
shape: uint8_type, {1}, {0}
shape: uint8_type, {1000, 4}, {4, 1}
shape: float_type, {1}, {0}
shape: uint8_type, {1}, {0}
shape: uint8_type, {1000, 4}, {4, 1}
shape: float_type, {1}, {0}
shape: uint8_type, {1}, {0}
shape: uint8_type, {1000, 4}, {4, 1}
shape: float_type, {1}, {0}
shape: uint8_type, {1}, {0}
shape: uint8_type, {507, 4}, {4, 1}
shape: float_type, {1}, {0}
shape: uint8_type, {1}, {0}

Is there any specific parsing option for tuple_type?
ONNX Runtime also parses these inputs as a sequence of tensors:
https://github.com/microsoft/onnxruntime/blob/main/onnxruntime/contrib_ops/cpu/quantization/qlinear_concat.cc#L18-L19

Copy link
Collaborator

@CharlieL7 CharlieL7 Dec 7, 2023

Choose a reason for hiding this comment

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

Given that the models use the operator that way, the way the code is currently makes sense to me. I also don't think there is a tuple type in ONNX. The spec is problematic however (Microsoft probably made a mistake). I would like the docstring with the spec on this operator deleted and something that reflects the code to be written instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed the docstring and added comments about the actual input tensor layout in the input checking part.


auto y_zp = args[1];
auto y_zp_type = y_zp->get_shape().type();
if(y_zp_type != migraphx::shape::int8_type and y_zp_type != migraphx::shape::uint8_type)
MIGRAPHX_THROW("QLINEARCONCAT: unsupported output type");

auto t0_type = args[2]->get_shape().type();
if(t0_type != migraphx::shape::int8_type and t0_type != migraphx::shape::uint8_type)
MIGRAPHX_THROW("QLINEARCONCAT: unsupported input type");
for(auto idx = 2; idx < args.size(); idx += 3)
{
if((args[idx]->get_shape().type() != t0_type) or
(args[idx + 2]->get_shape().type() != t0_type))
{
MIGRAPHX_THROW("QLINEARCONCAT: mismatching input types");
}
}
}

instruction_ref parse(const op_desc& /* opd */,
const onnx_parser& parser,
const onnx_parser::node_info& info,
const std::vector<instruction_ref>& args) const
{
check_inputs(args);
if(not contains(info.attributes, "axis"))
MIGRAPHX_THROW("QLINEARCONCAT: missing axis attribute");

auto axis = parser.parse_value(info.attributes.at("axis")).template at<int64_t>();
std::vector<instruction_ref> tmp;
for(auto idx = 2; idx < args.size(); idx += 3)
{
auto data_tensor = args[idx];
auto scale = args[idx + 1];
auto zero_pt = args[idx + 2];
tmp.push_back(bcast_qdq_instr("dequantizelinear", data_tensor, scale, zero_pt, info));
}
auto y = info.add_instruction(migraphx::make_op("concat", {{"axis", axis}}), tmp);

auto y_scale = args[0];
auto y_zero_pt = args[1];

return bcast_qdq_instr("quantizelinear", y, y_scale, y_zero_pt, info);
}
};

} // namespace onnx
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
50 changes: 50 additions & 0 deletions test/onnx/gen_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -6251,6 +6251,56 @@ def qlinearaveragepool_nt_cip_test():
return ([node], [x], [y], [x_scale, x_zero_point, y_scale, y_zero_point])


@onnx_test()
def qlinearconcat_test():
y_scale = helper.make_tensor('1', TensorProto.FLOAT, [], [0.5])
y_zero_point = helper.make_tensor('2', TensorProto.INT8, [], [2])

t0 = helper.make_tensor_value_info('t0', TensorProto.INT8, [2])
s0 = helper.make_tensor('3', TensorProto.FLOAT, [], [0.5])
zp0 = helper.make_tensor('4', TensorProto.INT8, [], [1])

t1 = helper.make_tensor_value_info('t1', TensorProto.INT8, [3])
s1 = helper.make_tensor('5', TensorProto.FLOAT, [], [0.25])
zp1 = helper.make_tensor('6', TensorProto.INT8, [], [0])

y = helper.make_tensor_value_info('out', TensorProto.INT8, [5])

node = onnx.helper.make_node(
'QLinearConcat',
lakhinderwalia marked this conversation as resolved.
Show resolved Hide resolved
inputs=['1', '2', 't0', '3', '4', 't1', '5', '6'],
axis=0,
outputs=['out'],
)

return ([node], [t0, t1], [y], [y_scale, y_zero_point, s0, zp0, s1, zp1])


@onnx_test()
def qlinearconcat_3d_test():
y_scale = helper.make_tensor('1', TensorProto.FLOAT, [], [0.5])
y_zero_point = helper.make_tensor('2', TensorProto.INT8, [], [2])

t0 = helper.make_tensor_value_info('t0', TensorProto.INT8, [3, 4, 2])
s0 = helper.make_tensor('3', TensorProto.FLOAT, [], [0.5])
zp0 = helper.make_tensor('4', TensorProto.INT8, [], [10])

t1 = helper.make_tensor_value_info('t1', TensorProto.INT8, [3, 2, 2])
s1 = helper.make_tensor('5', TensorProto.FLOAT, [], [0.4])
zp1 = helper.make_tensor('6', TensorProto.INT8, [], [20])

y = helper.make_tensor_value_info('out', TensorProto.UINT8, [3, 6, 2])

node = onnx.helper.make_node(
'QLinearConcat',
inputs=['1', '2', 't0', '3', '4', 't1', '5', '6'],
axis=1,
outputs=['out'],
)

return ([node], [t0, t1], [y], [y_scale, y_zero_point, s0, zp0, s1, zp1])


@onnx_test()
def qlinearconv_test():
# https://xadupre.github.io/draft/onnx/onnx_doc_folder/onnx__QLinearConv.html
Expand Down
53 changes: 53 additions & 0 deletions test/onnx/onnx_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5645,6 +5645,59 @@ TEST_CASE(qlinearaveragepool_notset_test)
EXPECT(p == prog);
}

TEST_CASE(qlinearconcat_test)
{
migraphx::program p;
auto* mm = p.get_main_module();

auto sc_y = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.5}});
auto z_pt_y = mm->add_literal(migraphx::literal{migraphx::shape::int8_type, {2}});

auto sc_0 = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.5}});
auto z_pt_0 = mm->add_literal(migraphx::literal{migraphx::shape::int8_type, {1}});

auto sc_1 = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.25}});
auto z_pt_1 = mm->add_literal(migraphx::literal{migraphx::shape::int8_type, {0}});

auto t0 = mm->add_parameter("t0", {migraphx::shape::int8_type, {2}});
auto t1 = mm->add_parameter("t1", {migraphx::shape::int8_type, {3}});

auto scale_0_bcast =
mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2}}}), sc_0);

auto z_pt_0_bcast =
mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2}}}), z_pt_0);

auto fp_0 =
mm->add_instruction(migraphx::make_op("dequantizelinear"), t0, scale_0_bcast, z_pt_0_bcast);

auto scale_1_bcast =
mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3}}}), sc_1);

auto z_pt_1_bcast =
mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3}}}), z_pt_1);

auto fp_1 =
mm->add_instruction(migraphx::make_op("dequantizelinear"), t1, scale_1_bcast, z_pt_1_bcast);

auto fp_y = mm->add_instruction(migraphx::make_op("concat", {{"axis", 0}}), fp_0, fp_1);

auto scale_y_bcast =
mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5}}}), sc_y);

auto z_pt_y_bcast =
mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5}}}), z_pt_y);

auto y =
mm->add_instruction(migraphx::make_op("quantizelinear"), fp_y, scale_y_bcast, z_pt_y_bcast);

mm->add_return({y});

auto prog = migraphx::parse_onnx("qlinearconcat_test.onnx");

EXPECT(p == prog);
}

TEST_CASE(qlinearconv_test)
{
migraphx::program p;
Expand Down
Binary file added test/onnx/qlinearconcat_3d_test.onnx
Binary file not shown.
Binary file added test/onnx/qlinearconcat_test.onnx
Binary file not shown.
46 changes: 46 additions & 0 deletions test/onnx/verify_onnx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1932,6 +1932,52 @@ TEST_CASE(qlinearaveragepool_nt_cip_test)
EXPECT(migraphx::verify::verify_rms_range(result_vector, gold));
}

gyulaz-htec marked this conversation as resolved.
Show resolved Hide resolved
TEST_CASE(qlinearconcat_test)
{
auto p = migraphx::parse_onnx("qlinearconcat_test.onnx");
p.compile(migraphx::make_target("ref"));

std::vector<int8_t> data_t0 = {2, 3};
migraphx::shape s_t0{migraphx::shape::int8_type, {2}};
migraphx::parameter_map pp;
pp["t0"] = migraphx::argument(s_t0, data_t0.data());

std::vector<int8_t> data_t1 = {6, 8, 10};
migraphx::shape s_t1{migraphx::shape::int8_type, {3}};
pp["t1"] = migraphx::argument(s_t1, data_t1.data());

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

std::vector<int8_t> gold = {3, 4, 5, 6, 7};
EXPECT(migraphx::verify::verify_rms_range(result_vector, gold));
}

TEST_CASE(qlinearconcat_3d_test)
{
auto p = migraphx::parse_onnx("qlinearconcat_3d_test.onnx");
p.compile(migraphx::make_target("ref"));

std::vector<int8_t> data_t0 = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10};
migraphx::shape s_t0{migraphx::shape::int8_type, {3, 4, 2}};
migraphx::parameter_map pp;
pp["t0"] = migraphx::argument(s_t0, data_t0.data());

std::vector<int8_t> data_t1 = {25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25};
migraphx::shape s_t1{migraphx::shape::int8_type, {3, 2, 2}};
pp["t1"] = migraphx::argument(s_t1, data_t1.data());

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

std::vector<int8_t> gold = {2, 2, 2, 2, 2, 2, 2, 2, 6, 6, 6, 6, 2, 2, 2, 2, 2, 2,
2, 2, 6, 6, 6, 6, 2, 2, 2, 2, 2, 2, 2, 2, 6, 6, 6, 6};
EXPECT(migraphx::verify::verify_rms_range(result_vector, gold));
}

TEST_CASE(qlinearconv_test)
{
// https://xadupre.github.io/draft/onnx/onnx_doc_folder/onnx__QLinearConv.html
Expand Down
Loading