tensorflow / tensorflow/model-optimization

Is it possible to have quantised fused_tanh (with add op ) via TFlite converter

Open
#833 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.6k
Forks
349
Avg merge
3d 2h
Merged PRs (30d)
1

Description

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04):
    Linux Ubuntu 18.04
  • TensorFlow installed from (source or binary):
    Source
  • TensorFlow version (or github SHA if from source):
    2.6.0

Provide the text output from tflite_convert

image

When comparing against the quantized Keras output. The TfLite output does not match the Keras output.

Standalone code to reproduce the issue
Provide a reproducible test case that is the bare minimum necessary to generate
the problem. If possible, please share a link to Colab/Jupyter/any notebook.

https://colab.research.google.com/drive/1IqfO1FTKBahe9iahqD4ZgjGnkV1Utulk?usp=sharing

Also, please include a link to a GraphDef or the model if possible.

Any other info / logs

Include any logs or source code that would be helpful to diagnose the problem.
If including tracebacks, please include the full traceback. Large logs and files
should be attached.

Is it possible to have an ADD operator with a fused TANH activation in TFL ? Such quantized int16 model is producing wrong results compare to Keras output ?
Model:
=====
input = tf.keras.layers.Input(shape=  (1, 128, 3), batch_size=1)
add = tf.keras.layers.Add()([input, input])
act = tf.keras.layers.Activation(tf.keras.activations.tanh)(add)
model = tf.keras.Model(inputs=input, outputs= act)
=======


```From the schema it seems it's supported:
`
table AddOptions
{ fused_activation_function:ActivationFunctionType; // Parameters supported by version 3. pot_scale_int16:bool = true;}

enum ActivationFunctionType : byte {
NONE = 0,
RELU = 1,
RELU_N1_TO_1 = 2,
RELU6 = 3,
TANH = 4,
SIGN_BIT = 5,
}
`

But MLIR converter currently only fuses TFL_ReluOp, TFL_Relu6Op and TFL_Relu1Op:
https://github.com/tensorflow/tensorflow/blob/77d8c333405a080c57850c45531dbbf077b2bd0e/tensorflow/compiler/mlir/lite/transforms/optimize_patterns.td#L86
https://github.com/tensorflow/tensorflow/blob/77d8c333405a080c57850c45531dbbf077b2bd0e/tensorflow/compiler/mlir/lite/transforms/optimize_patterns.td#L429

Can you confirm if it's possible to fuse 16-bit tanh in quantised model ?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Colab reproduction and compare the quantized TFLite output with the Keras output for the int16 Add-plus-Tanh model. Read tensorflow/compiler/mlir/lite/transforms/optimize_patterns.td at the referenced lines and the AddOptions schema to determine whether fused TANH is supported; done means the support or failure mode is confirmed with reproducible results.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, tensorflow
Domain
compilers, machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.