Skip to content

Commit

Permalink
Remove compile step for div
Browse files Browse the repository at this point in the history
  • Loading branch information
gyulaz-htec committed Dec 6, 2023
1 parent b889e88 commit bf8eadd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/onnx/parse_dynamicquantizelinear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ struct parse_dynamicquantizelinear : op_parser<parse_dynamicquantizelinear>

auto x_type = x_shape.type();
// 1. Computing y_scale
auto l0 = info.add_literal(migraphx::literal{migraphx::shape{x_type}, {0}});
auto l0 = info.add_literal({0.f});
// DynamicQuantizeLinear only has uint8 quantization
auto q_max = info.add_literal(
migraphx::literal{migraphx::shape{x_type}, {std::numeric_limits<uint8_t>::max()}});
Expand All @@ -111,23 +111,23 @@ struct parse_dynamicquantizelinear : op_parser<parse_dynamicquantizelinear>
x_reshape = info.add_instruction(
migraphx::make_op("reshape", {{"dims", {x_shape.elements()}}}), x);
}
x_reshape = info.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), x_reshape, l0);
// maximum(0, max(x))
auto topk_max = info.add_instruction(
migraphx::make_op("topk", {{"axis", 0}, {"k", 1}, {"largest", true}}), x_reshape);
auto max_x =
info.add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), topk_max);
max_x = info.add_instruction(migraphx::make_op("max"), l0, max_x);

// minimum(0, min(x))
auto topk_min = info.add_instruction(
migraphx::make_op("topk", {{"axis", 0}, {"k", 1}, {"largest", false}}), x_reshape);
auto min_x =
info.add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), topk_min);
min_x = info.add_instruction(migraphx::make_op("min"), l0, min_x);

// y_scale = (maximum(0, max(x)) - minimum(0, min(x))) / (qmax - qmin)
auto sub0 = info.add_instruction(migraphx::make_op("sub"), max_x, min_x);
auto div = info.add_instruction(migraphx::make_op("sub"), q_max, q_min);
auto sub0 = info.add_instruction(migraphx::make_op("sub"), max_x, min_x);
// qmax - qmin is always 255
auto div = q_max;
auto y_scale = info.add_instruction(migraphx::make_op("div"), sub0, div);

// 2. Computing y_zero_point
Expand Down
7 changes: 3 additions & 4 deletions test/onnx/onnx_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1873,25 +1873,24 @@ TEST_CASE(dynamicquantizelinear_2d_test)
auto x_type = migraphx::shape::float_type;
auto x = mm->add_parameter("x", {x_type, x_dims});

auto l0 = mm->add_literal(migraphx::literal{migraphx::shape{x_type}, {0}});
auto l0 = mm->add_literal({0.f});
auto q_max = mm->add_literal(
migraphx::literal{migraphx::shape{x_type}, {std::numeric_limits<uint8_t>::max()}});
auto q_min = mm->add_literal(
migraphx::literal{migraphx::shape{x_type}, {std::numeric_limits<uint8_t>::min()}});
auto x_reshape = mm->add_instruction(migraphx::make_op("reshape", {{"dims", {12}}}), x);
x_reshape = mm->add_instruction(migraphx::make_op("concat", {{"axis", 0}}), x_reshape, l0);

auto topk_max = mm->add_instruction(
migraphx::make_op("topk", {{"axis", 0}, {"k", 1}, {"largest", true}}), x_reshape);
auto max_x = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), topk_max);
max_x = mm->add_instruction(migraphx::make_op("max"), l0, max_x);

auto topk_min = mm->add_instruction(
migraphx::make_op("topk", {{"axis", 0}, {"k", 1}, {"largest", false}}), x_reshape);
auto min_x = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), topk_min);
min_x = mm->add_instruction(migraphx::make_op("min"), l0, min_x);

auto sub0 = mm->add_instruction(migraphx::make_op("sub"), max_x, min_x);
auto div = mm->add_instruction(migraphx::make_op("sub"), q_max, q_min);
auto div = q_max;
auto y_scale = mm->add_instruction(migraphx::make_op("div"), sub0, div);

auto sub1 = mm->add_instruction(migraphx::make_op("sub"), q_min, min_x);
Expand Down

0 comments on commit bf8eadd

Please sign in to comment.