-
Notifications
You must be signed in to change notification settings - Fork 144
rebias uint8 to int8 on models with mixed datatypes #5075
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
base: develop
Are you sure you want to change the base?
Changes from all commits
eb5507b
b450867
957871a
9ed7ecd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1065,6 +1065,29 @@ TEST_CASE(simplify_concat_unpack_int4) | |
| EXPECT(m1 == m2); | ||
| } | ||
|
|
||
| TEST_CASE(simplify_concat_dequantizelinear) | ||
| { | ||
| // dequantizelinear must not be pulled back through the concat: that would leave the concat | ||
| // operating on integer values, which some backends (e.g. rocMLIR) cannot fuse. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rocMLIR doesnt fuse concat either way. I dont really understand why we arent fusing the dequantizelinear. |
||
| auto si = migraphx::shape{migraphx::shape::int8_type, {4}}; | ||
| auto sf = migraphx::shape{migraphx::shape::float_type, {4}}; | ||
| migraphx::module m1; | ||
| { | ||
| auto x = m1.add_parameter("x", si); | ||
| auto y = m1.add_parameter("y", si); | ||
| auto scale = m1.add_literal(migraphx::literal{sf, {0.5f, 0.5f, 0.5f, 0.5f}}); | ||
| auto zp = m1.add_literal(migraphx::literal{si, {5, 5, 5, 5}}); | ||
| auto dq1 = m1.add_instruction(migraphx::make_op("dequantizelinear"), x, scale, zp); | ||
| auto dq2 = m1.add_instruction(migraphx::make_op("dequantizelinear"), y, scale, zp); | ||
| auto concat = m1.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), dq1, dq2); | ||
| m1.add_return({concat}); | ||
| } | ||
|
|
||
| migraphx::module m2 = m1; | ||
| run_pass(m1); | ||
| EXPECT(m1 == m2); | ||
| } | ||
|
|
||
| TEST_CASE(simplify_concat_add_relu) | ||
| { | ||
| auto s = migraphx::shape{migraphx::shape::int32_type, {1}}; | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -41,6 +41,7 @@ namespace match = migraphx::match; | |||||||||||||
|
|
||||||||||||||
| static bool is_convolution(const migraphx::instruction& ins) { return ins.name() == "convolution"; } | ||||||||||||||
| static bool is_dot(const migraphx::instruction& ins) { return ins.name() == "dot"; } | ||||||||||||||
| static bool is_quant_dot(const migraphx::instruction& ins) { return ins.name() == "quant_dot"; } | ||||||||||||||
|
|
||||||||||||||
| static void run_pass(migraphx::module& m) | ||||||||||||||
| { | ||||||||||||||
|
|
@@ -346,6 +347,62 @@ TEST_CASE(dot_transposed) | |||||||||||||
| EXPECT(m1 == m2); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| TEST_CASE(dot_uint8_input) | ||||||||||||||
| { | ||||||||||||||
| // uint8 activation with int8 weight must still become a quant_dot (uint8 rebiased to int8), | ||||||||||||||
| // not have its Q/DQ stripped. | ||||||||||||||
| migraphx::shape sh1{migraphx::shape::float_type, {4, 8}}; | ||||||||||||||
| migraphx::shape sh2{migraphx::shape::float_type, {8, 6}}; | ||||||||||||||
|
|
||||||||||||||
| migraphx::module m1; | ||||||||||||||
| { | ||||||||||||||
| auto t1 = m1.add_parameter("t1", sh1); | ||||||||||||||
| auto t2 = m1.add_parameter("t2", sh2); | ||||||||||||||
| auto a_scale = m1.add_literal(0.5f); | ||||||||||||||
| auto a_zp = m1.add_literal(std::uint8_t{128}); | ||||||||||||||
| auto w_scale = m1.add_literal(0.25f); | ||||||||||||||
| auto w_zp = m1.add_literal(std::int8_t{0}); | ||||||||||||||
|
|
||||||||||||||
| auto q1 = add_quantize_op(m1, "quantizelinear", t1, a_scale, a_zp); | ||||||||||||||
| auto d1 = add_quantize_op(m1, "dequantizelinear", q1, a_scale, a_zp); | ||||||||||||||
| auto q2 = add_quantize_op(m1, "quantizelinear", t2, w_scale, w_zp); | ||||||||||||||
| auto d2 = add_quantize_op(m1, "dequantizelinear", q2, w_scale, w_zp); | ||||||||||||||
| auto dot = m1.add_instruction(migraphx::make_op("dot"), d1, d2); | ||||||||||||||
| m1.add_return({dot}); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| run_pass(m1); | ||||||||||||||
| EXPECT(any_of(m1, &is_quant_dot)); | ||||||||||||||
| EXPECT(none_of(m1, &is_dot)); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| TEST_CASE(qdq_reshape_unquantized_dot) | ||||||||||||||
| { | ||||||||||||||
| // q -> reshape -> dq feeding an unquantized dot stays put: remove_qdq_pairs must not drop the | ||||||||||||||
| // reshape, which would leave the dot with mismatched ranks. | ||||||||||||||
| migraphx::shape xsh{migraphx::shape::float_type, {1, 1024, 1, 1}}; | ||||||||||||||
| migraphx::shape wsh{migraphx::shape::float_type, {1024, 1000}}; | ||||||||||||||
|
|
||||||||||||||
| migraphx::module m1; | ||||||||||||||
| { | ||||||||||||||
| auto x = m1.add_parameter("x", xsh); | ||||||||||||||
| auto w = m1.add_parameter("w", wsh); | ||||||||||||||
| auto scale = m1.add_literal(0.5f); | ||||||||||||||
| auto zero = m1.add_literal(std::int8_t{1}); | ||||||||||||||
|
|
||||||||||||||
| auto q1 = add_quantize_op(m1, "quantizelinear", x, scale, zero); | ||||||||||||||
| auto rs = m1.add_instruction(migraphx::make_op("reshape", {{"dims", {1, 1024}}}), q1); | ||||||||||||||
| auto d1 = add_quantize_op(m1, "dequantizelinear", rs, scale, zero); | ||||||||||||||
|
Comment on lines
+393
to
+395
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [format.py] reported by reviewdog 🐶
Suggested change
|
||||||||||||||
| auto dot = m1.add_instruction(migraphx::make_op("dot"), d1, w); | ||||||||||||||
| m1.add_return({dot}); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| migraphx::module m2 = m1; | ||||||||||||||
|
|
||||||||||||||
| run_pass(m1); | ||||||||||||||
| EXPECT(m1 == m2); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| TEST_CASE(dot_reshaped) | ||||||||||||||
| { | ||||||||||||||
| migraphx::shape sh1{migraphx::shape::float_type, {1280, 1000}}; | ||||||||||||||
|
|
||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,66 @@ | ||||||||||
| /* | ||||||||||
| * 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 "verify_program.hpp" | ||||||||||
| #include <migraphx/program.hpp> | ||||||||||
| #include <migraphx/make_op.hpp> | ||||||||||
|
|
||||||||||
| // Isolates the uint8 -> int8 rebias chain simplify_qdq emits for asymmetric activations: | ||||||||||
| // quantizelinear(uint8) -> convert(int32) -> sub(128) -> convert(int8), with no conv or MLIR. | ||||||||||
| struct test_uint8_rebias_roundtrip : verify_program<test_uint8_rebias_roundtrip> | ||||||||||
| { | ||||||||||
| migraphx::program create_program() const | ||||||||||
| { | ||||||||||
| migraphx::program p; | ||||||||||
| auto* mm = p.get_main_module(); | ||||||||||
|
|
||||||||||
| migraphx::shape x_shape{migraphx::shape::float_type, {2, 8}}; | ||||||||||
| auto x = mm->add_parameter("x", x_shape); | ||||||||||
|
|
||||||||||
| auto scale = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.05f}}); | ||||||||||
| auto zp = mm->add_literal(migraphx::literal{migraphx::shape::uint8_type, {0}}); | ||||||||||
|
Comment on lines
+41
to
+42
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [format.py] reported by reviewdog 🐶
Suggested change
|
||||||||||
| auto scale_b = mm->add_instruction( | ||||||||||
| migraphx::make_op("multibroadcast", {{"out_lens", x_shape.lens()}}), scale); | ||||||||||
| auto zp_b = mm->add_instruction( | ||||||||||
| migraphx::make_op("multibroadcast", {{"out_lens", x_shape.lens()}}), zp); | ||||||||||
|
|
||||||||||
| auto q = mm->add_instruction(migraphx::make_op("quantizelinear"), x, scale_b, zp_b); | ||||||||||
|
|
||||||||||
| // Rebias to int8 by subtracting 128 through int32 (same IR as rebias_uint8_to_int8). | ||||||||||
| auto q_i32 = mm->add_instruction( | ||||||||||
| migraphx::make_op("convert", {{"target_type", migraphx::shape::int32_type}}), q); | ||||||||||
| auto k128 = mm->add_literal(migraphx::literal{migraphx::shape::int32_type, {128}}); | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [format.py] reported by reviewdog 🐶
Suggested change
|
||||||||||
| auto k128_b = mm->add_instruction( | ||||||||||
| migraphx::make_op("multibroadcast", {{"out_lens", x_shape.lens()}}), k128); | ||||||||||
| auto diff = mm->add_instruction(migraphx::make_op("sub"), q_i32, k128_b); | ||||||||||
| auto q_i8 = mm->add_instruction( | ||||||||||
| migraphx::make_op("convert", {{"target_type", migraphx::shape::int8_type}}), diff); | ||||||||||
|
|
||||||||||
| auto out = mm->add_instruction( | ||||||||||
| migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), q_i8); | ||||||||||
| mm->add_return({out}); | ||||||||||
| return p; | ||||||||||
| } | ||||||||||
| std::string section() const { return "conv"; } | ||||||||||
| }; | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add unit tests for these changes?