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

[luci] Support Transpose in InsertQuantizeOpOnDTypeMismatch #14488

Merged
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
45 changes: 45 additions & 0 deletions compiler/luci/pass/src/InsertQuantizeOpOnDTypeMismatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,51 @@ luci::CircleQuantize *create_quantize_op(luci::CircleNode *node)
namespace luci
{

// Add Quantize Op before Transpose
//
// Before
//
// [Node] s16
// |
// [Transpose] u8
//
// After
//
// [Node] s16
// |
// [Quantize] s16->u8
// |
// [Transpose] u8
void InsertQuantizeOpOnDTypeMismatch::visit(luci::CircleTranspose *node)
{
auto input = loco::must_cast<luci::CircleNode *>(node->a());

// Input dtype == Output dtype. No problem
if (input->dtype() == node->dtype())
return;

// Only cares quantized case
if (not is_quantized(input))
return;

if (not is_quantized(node))
return;
Comment on lines +146 to +150
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these lines follows other implementations. But, just curiosity, can this case really happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I expect that it should not happen for our target model, but I'm not 100% sure. These branches are useful because I don't have to consider whether this case will never happen or not. We can just apply this pass to safe cases.


// Let's support limited case
// TODO Extend this to another dtype
if (input->dtype() != loco::DataType::S16)
return;

if (node->dtype() != loco::DataType::U8)
return;

// Create Quantize Op
auto quant_op = create_quantize_op(node);
quant_op->input(input);

node->a(quant_op);
}

void InsertQuantizeOpOnDTypeMismatch::visit(luci::CircleFullyConnected *node)
{
auto input = loco::must_cast<luci::CircleNode *>(node->input());
Expand Down
1 change: 1 addition & 0 deletions compiler/luci/pass/src/InsertQuantizeOpOnDTypeMismatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct InsertQuantizeOpOnDTypeMismatch final : public luci::CircleNodeMutableVis
void visit(luci::CircleFullyConnected *node);
void visit(luci::CircleMul *node);
void visit(luci::CircleBatchMatMul *node);
void visit(luci::CircleTranspose *node);

// TODO Support more operators
};
Expand Down