whisper.tflite: forced_decoder_ids does not apply to tflite model (multilingual, language issue)

Hello, I’m interested in converting Whisper models to tflite. And I’m generating a tflite model by referring to your works. Thank you for your contribution before discussing the issue!

My goal is a tflite model that can be transcribed into “Korean”. For this work, I looked up related multilingual output issues. And I understood that I had to apply options to perform transcription into a particular language. model.config.forced_decoder_ids = [[1, 50264], [2, 50359], [3, 50363]] # <|ko|><|transcribe|><|notimestamp|> they work well, before tflite converting!

My process is like this.

  1. Load transformer-TFWhipser model

  2. Define the ‘GenerateModel’, ‘serving(input_features)’ Apply configurations to set the language and task at this stage

  3. Convert to TFLite (using tf.lite.TFLiteConverter)

  4. Inference and output comparison (tensorflow, tflite)

Before converting to TFLite, generation configurations (language, task) were well applied. After that, Korean token values are generated.

output tensor: 
[50258 50264 50359 50363  4704  7675  2785 16623  6301  1453 16316  2230
  1453  2393 21166  1129  5514  5642 10520 14050    13 50257 ...]

text:
<|startoftranscript|><|ko|><|transcribe|><|notimestamps|> 지구의 일부가 날아가서 달이 되었다는 거예요.<|endoftext|> ...

After converting to TFLite, the model output is set to ‘0’ for task and timestamp tokens, although the language setting token remains in Korean(50264). After that, the tokens are generated with an English value.

output tensor: 
[50258 50264     0     0   440  4120   311   644   390 16479  1314   293
  3062   257  7135    13 50257 ...]

text:
<|startoftranscript|><|ko|>!! The earth's part was blown away and became a moon.<|endoftext|> ...

I don’t think configs is fully applied during conversion to tflite. Any advice regarding this? I think it would be helpful for a lot of people if we could solve this problem.

In addition, I also experimented with the tflite model, which is separated into an encoder and a decoder through onnx conversion. I got the desired result when I injected ‘decoder_start_token’ into the input_tensor of the decoder. However, if possible, I would like to use an combined model.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (3 by maintainers)

Most upvoted comments

@hyla76 my audio sample files: samples_korean.zip (8 audio files, 1.6MB)

(source: https://aihub.or.kr/aihubdata/data/view.do?currMenu=&topMenu=&aihubDataSe=data&dataSetSn=130)

Have you tried the mic recording that built-in the app?

I tested it with the test wav file you provided, and it was decoded normally. It seems to work only if it matches the wav file characteristics that tensorflow can understand.

Thank you so much. 😄

I am sharing my testing environment to help others.

o Python environment python 3.8.19

print(tf.version) print(keras.version) print(transformers.version) 2.12.0 2.12.0 4.42.1

o android studio IDE Android Studio Koala | 2024.1.1 Build #AI-241.15989.150.2411.11948838, built on June 11, 2024 Runtime version: 17.0.10+0–11609105 amd64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. Windows 11.0 GC: G1 Young Generation, G1 Old Generation Memory: 2048M Cores: 16 Non-Bundled Plugins: idea.plugin.protoeditor (241.15989.49)

@yong1020 Could you share your python packages version? I found that some versions are not incompatible with each other (python3.8, tensorflow, transformers, keras, maybe numpy). For some reason my transcription works locally (tflite_generate(input_features=input_features)["sequences"] is correct), but when using it on android, it produces this error:

tflite  : gather index out of bounds
tflite  : Node number 34 (GATHER) failed to invoke.
tflite  : Node number 694 (WHILE) failed to invoke.

I applied the monkey patch.

@robre22 My Python package version is as follows, but I don’t think it will help you with your problems. Python 3.8.10 tensorflow==2.13.1 transformers==4.42.1 I’ve experienced the ‘failed to invoke’ issue with some audio files, which may be related to audio files and audio decoding. If you have other audio samples, try them.

  1. Audio length may be too short or too long (recommend between 10 and 30 seconds)
  2. Sampling rate must be 16000 Hz

Does it still work if I apply monkey patch? @KihongK

Yes, it’s still working. However, it may not work well on ‘tiny’ and ‘base’ models. (My project is ‘small’ model.)

In the case of ‘tiny’, some text is broken and strange characters are visible.

Hello. After looking through the github forum, I found issues and solutions related to this.

Issue:

  • Text output only in English even if multilingual settings were applied before tflite conversion
  • Problem where forced_decoder_ids [task, timestamp] becomes None (=‘!!’) after conversion

Solution: Those involved in the multilingual support issue of the tflite model will be able to solve most of their problems by proceeding with the ‘monkey patch’ below. https://github.com/nyadla-sys/whisper.tflite/discussions/15#discussioncomment-7362798

Thank you to the engineers involved!

image