Skip to content

[TFLite int16] 16-bit version of ADD/SUB reference kernel operators - #35985

Merged
tensorflow-copybara merged 19 commits into
tensorflow:masterfrom
wwwind:addsub_16x8
Jul 6, 2020
Merged

tensorflow-copybara merged 19 commits into
tensorflow:masterfrom
wwwind:addsub_16x8

Conversation

@wwwind

@wwwind wwwind commented Jan 17, 2020

Copy link
Copy Markdown
Contributor

This PR is one of steps to extend 8-bit quantization to support symmetric 16-bit activations.

Each activation is of type int16 and symmetric around zero. The weight tensor precision remains at 8-bit signed values. The bias is set to int64 precision.

In this PR we introduce implementation and tests for ADD/SUB kernel reference function.
The specification of this operator:

SUB
  Input 0:
    data_type  : int16
    range      : [-32768, 32767]
    granularity: per-tensor, zero_point=0
  Input 1:
    data_type  : int16
    range      : [-32768, 32767]
    granularity: per-tensor, zero_point=0
  Output 0:
    data_type  : int16
    range      : [-32768, 32767]
    granularity: per-tensor, zero_point=0

ADD
  Input 0:
    data_type  : int16
    range      : [-32768, 32767]
    granularity: per-tensor, zero_point=0
  Input 1:
    data_type  : int16
    range      : [-32768, 32767]
    granularity: per-tensor, zero_point=0
  Output 0:
    data_type  : int16
    range      : [-32768, 32767]
    granularity: per-tensor, zero_point=0

@tensorflow-bot tensorflow-bot Bot added the size:M CL Change Size: Medium label Jan 17, 2020
@wwwind
wwwind requested review from jdduke and suharshs January 17, 2020 12:39
@suharshs
suharshs requested a review from jianlijianli January 17, 2020 20:25

@suharshs suharshs left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Change for ADD and SUB seem fine, why are the names LSTM specific?

AddElementwise(flat_size, params, input1_data, input2_data, output_data);
}

inline void AddLSTM(const ArithmeticParams& params,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What is AddLSTM?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The present version needs scales to be POT as they are for LSTM. This PR extends this version to include general scales as for 8-bit. I renamed the current version to AddLSTM to indicate that this function is specific to LSTM.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I am not sure I understand, this Add and Sub operation are specific to LSTM?

How are they created in the TFLite graph. If possible we should avoid having unfused versions of add and sub that are specific to LSTM.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, the existed function was named Add before, but it was only with POT scale. We needed a general version, so I added the implementation similar to 8-bit and renamed the existed function to AddLSTM to indicate that this function is used when scale is POT.
This is probably a bad name.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ok sorry for the delay, was figuring out what the right thing to do here was.

This is unfortunate that there was an exisiting version of the 16bit code was specific to POT.

We have to take extra care for versioning in this case.

Here is what i propose:

  1. Add a new attribute for this operation called pot_scale. This attribute must default to True.

  2. Add your new code path under the case where pot_scale=false.

  3. when we use this operation in the converter and add the new op version, we will set the attribute to False.

Does that seem reasonable, and make sense?

@rthadur rthadur self-assigned this Jan 18, 2020
@rthadur rthadur added the comp:lite TF Lite related issues label Jan 18, 2020
@rthadur
rthadur requested a review from suharshs January 21, 2020 22:04
@wwwind wwwind changed the title 16-bit version of ADD/SUB reference kernel operators [TFLite int16] 16-bit version of ADD/SUB reference kernel operators Jan 22, 2020
@gbaned gbaned added the awaiting review Pull request awaiting review label Feb 13, 2020
AddElementwise(flat_size, params, input1_data, input2_data, output_data);
}

inline void AddLSTM(const ArithmeticParams& params,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ok sorry for the delay, was figuring out what the right thing to do here was.

This is unfortunate that there was an exisiting version of the 16bit code was specific to POT.

We have to take extra care for versioning in this case.

Here is what i propose:

  1. Add a new attribute for this operation called pot_scale. This attribute must default to True.

  2. Add your new code path under the case where pot_scale=false.

  3. when we use this operation in the converter and add the new op version, we will set the attribute to False.

Does that seem reasonable, and make sense?

@wwwind

wwwind commented Mar 19, 2020

Copy link
Copy Markdown
Contributor Author

Hi @suharshs !
Thanks for the review.
I corrected as you suggested. Could you please take a look ?

Regarding the versioning : Should I bump the version for ADD/SUB ?
I will set version 3, when input/output is int16.

@rthadur
rthadur requested a review from suharshs March 19, 2020 19:03
@tensorflowbutler tensorflowbutler removed the awaiting review Pull request awaiting review label Mar 21, 2020
@gbaned gbaned added the awaiting review Pull request awaiting review label Mar 24, 2020
@suharshs

Copy link
Copy Markdown

Thanks, yes you will also need to update the schema.fbs file to add a pot_scale attribute that the op_version.cc code can use to determine the new version. Since we need a way to distinguish between the pot and generic version in the op_version code.

{{OperatorType::kSub, 2}, "1.14.0"},
{{OperatorType::kSub, 3}, "1.15.0"},
{{OperatorType::kSub, 4}, "1.15.0"},
{{OperatorType::kSub, 5}, kPendingReleaseOpVersion},

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am not sure about this place: the runtime version was not set for other cases of SUB operator: like 5D version, old version with POT param scale. I added them as 1.15.0 and the new case: 16-bit + general case as kPendingReleaseOpVersion.

There are tests in the file operator_test.cc.
Should I add tests for my case ? I can do this in a separate PR - it will be easy to review. Please let me know.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Testing separately seems fine.

case BuiltinOperator_ADD:
case BuiltinOperator_SUB: {
op_sig.options.addsub.pot_scale_int16 = false;
const Tensor* input1_tensor =

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @suharshs and @jdduke !
I looked into schema.fbs and it looks they if I add a new parameter, then a lot of places should be changed.
I found this approach to differ between two cases: general parameter scale and pot param scale, and set different version accordingly.
Could you please take a look ?
If this approach is wrong, then I will try to add a new option to schema.fbs for ADD/SUB operators.
Thanks!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hi, something needs to set the pot_scale_int16 so that versioning can make the right decision. This info has to be in the flatbuffer and then converter to the c api so that the runtime can make the right kernel choice as well.

Here are instructions that may be helpful outline the changes that need to be made: https://www.tensorflow.org/lite/guide/ops_version

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! The link is very helpful. I will correct.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @suharshs ! I have a quick question (this is not mentioned in the link above):

Once I changed schema.fbs file, it looks that I need to re-generate the file schema_generated.h (which is not re-generated automatically by bazel).
I use this command:

flatc -c schema.fbs

The generated file is very different to the file commited to the repo.
Which command should I call to generate a new schema_generated.h for my new options ?

Thanks for the help!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hi Elena, checking with folks on the team about what the correct commands are and will get back to you.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi Suharsh,
Currently I modified it manually (maybe, it is enough) and pushed a new implementation.
Could you please take a look ?
Thanks!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @Suharsh,
The document in the link above has some out-of-state filenames.
I updated them and here is a PR, probably, for another team to review:
#38201

@tensorflowbutler tensorflowbutler removed the awaiting review Pull request awaiting review label Mar 29, 2020
@gbaned gbaned added the awaiting review Pull request awaiting review label Apr 1, 2020
suharshs
suharshs previously approved these changes Apr 3, 2020

@suharshs suharshs left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks Elena, I will see if there is anything missing from the internal tests, but will approve for now. Thanks!

@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Apr 3, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Apr 3, 2020
Change-Id: I61d7d4a94087d052a782890799211031f6ed3015
@wwwind

wwwind commented Jun 17, 2020

Copy link
Copy Markdown
Contributor Author

Hi @jdduke Thanks for the explanation. I pushed a change.

rthadur
rthadur previously approved these changes Jun 17, 2020
@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Jun 17, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Jun 17, 2020
jdduke
jdduke previously approved these changes Jun 17, 2020
@google-ml-butler google-ml-butler Bot added the kokoro:force-run Tests on submitted change label Jun 17, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Jun 17, 2020
@gbaned

gbaned commented Jun 23, 2020

Copy link
Copy Markdown
Contributor

@wwwind Can you please resolve conflicts? Thanks!

@gbaned gbaned removed the ready to pull PR ready for merge process label Jun 23, 2020
@wwwind
wwwind dismissed stale reviews from jdduke and rthadur via bc2b36c June 23, 2020 18:24
@wwwind

wwwind commented Jun 23, 2020

Copy link
Copy Markdown
Contributor Author

Hi @gbaned Done. Thanks!

rthadur
rthadur previously approved these changes Jun 23, 2020
@google-ml-butler google-ml-butler Bot added the kokoro:force-run Tests on submitted change label Jun 23, 2020
@rthadur

rthadur commented Jun 24, 2020

Copy link
Copy Markdown
Contributor

@wwwind we see few more conflicts , can you please resolve those

@wwwind

wwwind commented Jun 24, 2020

Copy link
Copy Markdown
Contributor Author

Hi @rthadur I resolved the conflict. Could you please re-approve ? Thanks!

rthadur
rthadur previously approved these changes Jun 25, 2020

case BuiltinOperator_SUB: {
auto sub_option = op->builtin_options_as_SubOptions();
op_sig.options.addsub.pot_scale_int16 = false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So we set the default to false here, but the FlatBuffer default is true?

Change-Id: I9ea50c75014cc03ac91fdef0f5b4fe11395f7074
rthadur
rthadur previously approved these changes Jun 26, 2020
@gbaned

gbaned commented Jun 29, 2020

Copy link
Copy Markdown
Contributor

@wwwind Can you please resolve conflicts? Thanks!

@wwwind

wwwind commented Jun 29, 2020

Copy link
Copy Markdown
Contributor Author

Hi @gbaned Done, Thanks!

if (op_sig.input_types.at(0) == TensorType_INT16 &&
op_sig.output_types.at(0) == TensorType_INT16) {
if (op_sig.options.addsub.pot_scale_int16) {
return 5;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This CL got rolled back for an unrelated reason, but shouldn't the "default" case use the existing version? Don't we only need version 5 for the non-pot case?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I ask because this broke some internal conversion test cases, as the converter seems to be eagerly setting the version to 5, despite nothing having changed in the model (because pot_scale_int16 is getting set to true by default).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @jdduke I set it to 5 here, because INT16 and pot case was not versioned before - maybe, this is wrong. Please let me know if I need to correct this - Should it be a separate PR ?
Thanks

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Probably an oversight on our part, as we had some internal tooling using the existing int16 kernels. But even then, this condition seems odd, shouldn't the new behavior (non-pot-scale) get the highest op version?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right @jdduke. I will correct. Should it be a separate PR ?
I have a task to write versioning tests for ADD/SUB. I will include this change into this new PR. Is it okay ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nah, that's OK. I'll fix it in the internal CL, just wanted to confirm. Thanks!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @jdduke I noticed that these changes are still rolled back. Is there anything I can help with to get it merged ? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes comp:lite TF Lite related issues ready to pull PR ready for merge process size:M CL Change Size: Medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants