Repository navigation
[TFLite int16] 16-bit version of ADD/SUB reference kernel operators - #35985
Conversation
suharshs
left a comment
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
-
Add a new attribute for this operation called pot_scale. This attribute must default to True.
-
Add your new code path under the case where pot_scale=false.
-
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?
| AddElementwise(flat_size, params, input1_data, input2_data, output_data); | ||
| } | ||
|
|
||
| inline void AddLSTM(const ArithmeticParams& params, |
There was a problem hiding this comment.
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:
-
Add a new attribute for this operation called pot_scale. This attribute must default to True.
-
Add your new code path under the case where pot_scale=false.
-
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?
|
Hi @suharshs ! Regarding the versioning : Should I bump the version for ADD/SUB ? |
|
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. |
Change-Id: I51baab7d117fb4656fa09f4b9ff78ef34cbbe8a0
| {{OperatorType::kSub, 2}, "1.14.0"}, | ||
| {{OperatorType::kSub, 3}, "1.15.0"}, | ||
| {{OperatorType::kSub, 4}, "1.15.0"}, | ||
| {{OperatorType::kSub, 5}, kPendingReleaseOpVersion}, |
There was a problem hiding this comment.
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.
| case BuiltinOperator_ADD: | ||
| case BuiltinOperator_SUB: { | ||
| op_sig.options.addsub.pot_scale_int16 = false; | ||
| const Tensor* input1_tensor = |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Thanks! The link is very helpful. I will correct.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Hi Elena, checking with folks on the team about what the correct commands are and will get back to you.
There was a problem hiding this comment.
Hi Suharsh,
Currently I modified it manually (maybe, it is enough) and pushed a new implementation.
Could you please take a look ?
Thanks!
schema_generated.h is edited manually.
suharshs
left a comment
There was a problem hiding this comment.
Thanks Elena, I will see if there is anything missing from the internal tests, but will approve for now. Thanks!
Change-Id: I61d7d4a94087d052a782890799211031f6ed3015
|
Hi @jdduke Thanks for the explanation. I pushed a change. |
|
@wwwind Can you please resolve conflicts? Thanks! |
|
Hi @gbaned Done. Thanks! |
|
@wwwind we see few more conflicts , can you please resolve those |
|
Hi @rthadur I resolved the conflict. Could you please re-approve ? Thanks! |
|
|
||
| case BuiltinOperator_SUB: { | ||
| auto sub_option = op->builtin_options_as_SubOptions(); | ||
| op_sig.options.addsub.pot_scale_int16 = false; |
There was a problem hiding this comment.
So we set the default to false here, but the FlatBuffer default is true?
Change-Id: I9ea50c75014cc03ac91fdef0f5b4fe11395f7074
|
@wwwind Can you please resolve conflicts? Thanks! |
|
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
Nah, that's OK. I'll fix it in the internal CL, just wanted to confirm. Thanks!
There was a problem hiding this comment.
Hi @jdduke I noticed that these changes are still rolled back. Is there anything I can help with to get it merged ? Thanks
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