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

[QNN] Optimize requantize for power of 2 and fix dequantize for per-channel quantized input #6675

Merged
merged 5 commits into from
Oct 29, 2020

Conversation

anijain2305
Copy link
Contributor

Optimize the lowering for requantize op for a power of 2. Additionally, fixing a bug for dequantize.

@anijain2305
Copy link
Contributor Author

@ZihengJiang Can you please review?

(is_upward_rounding
? FixedPointMultiply(scaled_int32_t, fixed_point_multiplier, shift)
: FixedPointMultiplyToNearest(scaled_int32_t, double_multiplier, input_shape));
if (is_upward_rounding && fixed_point_multiplier == (1 << 30)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why the fixed_point_multiplier must be (1 << 30)?

Copy link
Contributor Author

@anijain2305 anijain2305 Oct 13, 2020

Choose a reason for hiding this comment

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

So, we use frexp to represent a floating point numbers. It gives a float significant which is between [0.5, 1). For power of 2, it is always 0.5. We convert the float significand into a fixed point 32-bit integer, where decimal point is between the first and second bit. 0.5 in this representation = 1 << 30

Copy link
Contributor

Choose a reason for hiding this comment

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

@anijain2305 , can add a small one line comment regarding (1<<30) ? These days aside from float32 many other types of float floats around.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cbalint13 Added a comment, can you PTAL

Copy link
Contributor

Choose a reason for hiding this comment

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

@anijain2305 , Thank you !

@u99127
Copy link
Contributor

u99127 commented Oct 21, 2020

Optimize the lowering for requantize op for a power of 2. Additionally, fixing a bug for dequantize.

Can you describe what's the bug you are fixing for dequantize in the commit message or link to an actual bug report ?

Ramana

@anijain2305
Copy link
Contributor Author

dequantize in main branch fails when input is per-channel quantized. There is no bug report. I found this while working on quantizing models in Relay.

@u99127
Copy link
Contributor

u99127 commented Oct 21, 2020

dequantize in main branch fails when input is per-channel quantized. There is no bug report. I found this while working on quantizing models in Relay.

Cool, could the description above say something like "Fix dequantize for per-channel quantized input" instead of just fixing a bug ?

@anijain2305 anijain2305 changed the title [QNN] Optimize requantize for power of 2 and bug in dequantize [QNN] Optimize requantize for power of 2 and handle per-channel quantized input for dequantize Oct 21, 2020
@anijain2305 anijain2305 changed the title [QNN] Optimize requantize for power of 2 and handle per-channel quantized input for dequantize [QNN] Optimize requantize for power of 2 and fix dequantize for per-channel quantized input Oct 21, 2020
Copy link
Contributor

@giuseros giuseros left a comment

Choose a reason for hiding this comment

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

Few comments also from me before this goes in !

// Power of 2 is determined by the fixed_point_multiplier == 1 << 30. In case of power of 2,
// fixed point multiplier will represent a float value of 0.5. In fixed point, this is
// represented by 1 << 30.
scaled_int32_t = PowerOfTwoMultiply(scaled_int32_t, shift - 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it make sense for this to go in FixedPointMultiply? This would give the possibility to everybody using FixedPointMultiply to exploit this fix.

Comment on lines 67 to 69
auto rounding_factor = 1 << (exp - 1);
auto rounded_t = Add(tensor, MakeConstantScalar(DataType::Int(32), rounding_factor));
out = RightShift(rounded_t, MakeConstantScalar(DataType::Int(32), exp));
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you sure you don't need to convert to int64 upfront and then cast back to int32?

@anijain2305
Copy link
Contributor Author

@giuseros Can you PTAL? I put all the stuff in fixed_point_multiply. Regarding your comment on upcasting to int64, I don't think it's necessary. But, please let me know if I am missing a corner case.

@giuseros
Copy link
Contributor

Hi @anijain2305 ,
LGTM, that's brilliant thanks!

@anijain2305
Copy link
Contributor Author

@ZihengJiang @u99127 Can you please take a look again? PR changed after I incorporated @giuseros comments.

@ZihengJiang ZihengJiang merged commit 380e2e9 into apache:main Oct 29, 2020
@ZihengJiang
Copy link
Contributor

LGTM! Thanks! @anijain2305

trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Dec 2, 2020
…hannel quantized input (apache#6675)

* [QNN] Optimize requantize for power of 2 and bug in dequantize

* Comments

* Docs

* Comments

* Ethos
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Dec 4, 2020
…hannel quantized input (apache#6675)

* [QNN] Optimize requantize for power of 2 and bug in dequantize

* Comments

* Docs

* Comments

* Ethos
trevor-m pushed a commit to neo-ai/tvm that referenced this pull request Dec 4, 2020
…hannel quantized input (apache#6675)

* [QNN] Optimize requantize for power of 2 and bug in dequantize

* Comments

* Docs

* Comments

* Ethos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants