# Cannot export tflite using optimum for a fine-tuned gemma 3 model for task : question answering

**URL:** <https://discuss.huggingface.co/t/cannot-export-tflite-using-optimum-for-a-fine-tuned-gemma-3-model-for-task-question-answering/148490>\
**Category:** Models\
**Created:** [April 2, 2025, 5:26am UTC](https://discuss.huggingface.co/t/cannot-export-tflite-using-optimum-for-a-fine-tuned-gemma-3-model-for-task-question-answering/148490 "2025-04-02T05:26:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![subha290](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@subha290](https://discuss.huggingface.co/u/subha290)\
**Post date:** [April 2, 2025, 5:26am UTC](https://discuss.huggingface.co/t/cannot-export-tflite-using-optimum-for-a-fine-tuned-gemma-3-model-for-task-question-answering/148490/1 "2025-04-02T05:26:17Z")

</div>

python3 -m optimum.commands.optimum\_cli export tflite --model merged\_gemma3\_aie\_finetuned\_hf --task question-answering --sequence\_length 1024 gemma\_tflite/

we are using this command but the error we are facing is transformer doesnt recognize gemma 3 text config.

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](https://discuss.huggingface.co/u/John6666)\
**Post date:** [April 2, 2025, 6:00am UTC](https://discuss.huggingface.co/t/cannot-export-tflite-using-optimum-for-a-fine-tuned-gemma-3-model-for-task-question-answering/148490/2 "2025-04-02T06:00:48Z")

</div>

Gemma 3 is maybe not yet supported except for the dev versions of Transformers and optimum, which can be installed from the github source.

I think that the conversion itself can be done by installing these dev versions, but I think there would be still a lot of problems.

If you have any questions about ONNX, the best way to get a reliable answer is to contact the ONNX Community within Hugging Face.

> **[onnx-community/gemma-3-1b-it-ONNX · Hugging Face](https://huggingface.co/onnx-community/gemma-3-1b-it-ONNX)**
>
> We’re on a journey to advance and democratize artificial intelligence through open source and open science.

> <https://github.com/huggingface/optimum/issues/1724>
>
> \### Feature request
> 
> !\[image\](https://github.com/huggingface/optimum/assets/53…895743/1267e66b-aba4-4d19-9763-b3a05d2ef854)
> 
> 
> 
> \### Motivation
> 
> I'm new to optimising for inference, this would benefit me other beginners greatly in evaluating runtimes and costs expected for many models. 
> 
> \### Your contribution
> 
> \- I could given time and more familiarity going forward - but would like this to be integrated quickly !

> <https://github.com/huggingface/optimum/issues/1728>
>
> \### System Info
> 
> \`\`\`shell
> My system currently is 
> python = 3.8 
> optimum-intel …: optimum-1.18.0.dev0
> \`\`\`
> 
> 
> \### Who can help?
> 
> @JingyaHuang @echarlaix 
> 
> \### Information
> 
> \- \[X\] The official example scripts
> \- \[\] My own modified scripts
> 
> \### Tasks
> 
> \- \[\] An officially supported task in the \`examples\` folder (such as GLUE/SQuAD, ...)
> \- \[X\] My own task or dataset (give details below)
> 
> \### Reproduction (minimal, reproducible, runnable)
> 
> I am following the provided examples on #1714 but am running into some issues.
> when I run 
> \`optimum-cli export onnx -m google/gemma-2b gemma\_onnx\`
> 
> I get the following error 
> !\[image\](https://github.com/huggingface/optimum/assets/61036085/054916e1-5087-4d7b-a5ee-c90a9865d288)
> 
> When I execute the python script provided I get the following error
> 
> !\[image\](https://github.com/huggingface/optimum/assets/61036085/a7231029-c984-4267-8b82-5a7b59455e2b)
> 
> 
> 
> 
> 
> 
> \### Expected behavior
> 
> The expected behavior is for the model to compile and be stored in onnx form

```auto
pip uninstall transformers optimum
pip install git+https://github.com/huggingface/optimum git+https://github.com/huggingface/transformers

```

---

<div class="post-metadata">

**Author:** ![subha290](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@subha290](https://discuss.huggingface.co/u/subha290)\
**Post date:** [April 2, 2025, 9:59am UTC](https://discuss.huggingface.co/t/cannot-export-tflite-using-optimum-for-a-fine-tuned-gemma-3-model-for-task-question-answering/148490/3 "2025-04-02T09:59:36Z")

</div>

Thankyou for your response, i fine tuned a gemma 3 model and now i need to convert it into .tflite, what are all the ways to do it, i used the dev version of Transformers and optimum but still it throwing error like “Gemma3 Text config is not recognized by the transformer”.

What are all the ways to convert it?

And also if I convert it into onnx then can i convert into .tflite? if yes, how to convert it into onnx?

---

<div class="post-metadata">

**Author:** ![subha290](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@subha290](https://discuss.huggingface.co/u/subha290)\
**Post date:** [April 2, 2025, 10:28am UTC](https://discuss.huggingface.co/t/cannot-export-tflite-using-optimum-for-a-fine-tuned-gemma-3-model-for-task-question-answering/148490/4 "2025-04-02T10:28:00Z")

</div>

this is the error i am getting

Traceback (most recent call last):  
File “C:\Users\aiehy\OneDrive\Desktop\training1\tflite.py”, line 46, in   
tf\_model = TFAutoModelForCausalLM.from\_pretrained(  
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
File “C:\Users\aiehy\OneDrive\Desktop\training1.venv\Lib\site-packages\transformers\models\auto\auto\_factory.py”, line 576, in from\_pretrained  
raise ValueError(  
ValueError: Unrecognized configuration class \<class ‘transformers.models.gemma3.configuration\_gemma3.Gemma3TextConfig’\> for this kind of AutoModel: TFAutoModelForCausalLM.  
Model type should be one of BertConfig, CamembertConfig, CTRLConfig, GPT2Config, GPT2Config, GPTJConfig, MistralConfig, OpenAIGPTConfig, OPTConfig, RemBertConfig, RobertaConfig, RobertaPreLayerNormConfig, RoFormerConfig, TransfoXLConfig, XGLMConfig, XLMConfig, XLMRobertaConfig, XLNetConfig.

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](https://discuss.huggingface.co/u/John6666)\
**Post date:** [April 2, 2025, 10:36am UTC](https://discuss.huggingface.co/t/cannot-export-tflite-using-optimum-for-a-fine-tuned-gemma-3-model-for-task-question-answering/148490/5 "2025-04-02T10:36:02Z")

</div>

Hmm…

> [@How can I export a transformers model into onnx that not supported with optimum yet](https://discuss.huggingface.co/t/how-can-i-export-a-transformers-model-into-onnx-that-not-supported-with-optimum-yet/104590/8):
>
> Hi, The easiest is to do it as explained in this guide: [Convert Transformers to ONNX with Hugging Face Optimum](https://www.philschmid.de/convert-transformers-to-onnx#export-with-torchonnx-low-level). You can use torch.onnx.export, which is more low-level than Optimum. Feel free to open an issue on the [Optimum repository](https://github.com/huggingface/optimum) to add support for RT-DETR.

or perhaps:

```py
#python3 -m optimum.commands.optimum_cli export tflite --model merged_gemma3_aie_finetuned_hf --task question-answering --sequence_length 1024 gemma_tflite/
python3 -m optimum.commands.optimum_cli export tflite --model merged_gemma3_aie_finetuned_hf --task text-generation --sequence_length 1024 gemma_tflite/

```

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](https://discuss.huggingface.co/u/John6666)\
**Post date:** [April 2, 2025, 10:46am UTC](https://discuss.huggingface.co/t/cannot-export-tflite-using-optimum-for-a-fine-tuned-gemma-3-model-for-task-question-answering/148490/6 "2025-04-02T10:46:37Z")

</div>

> transformers.models.gemma3.configuration\_gemma3.Gemma3TextConfig

Oh…

> <https://github.com/huggingface/transformers/issues/36683>
>
> \### System Info
> 
> v4.50.0.dev0
> 
> 
> \### Who can help?
> 
> @ArthurZucker 
> @LysandreJik 
> …@xenova 
> 
> \### Information
> 
> \- \[\] The official example scripts
> \- \[\] My own modified scripts
> 
> \### Tasks
> 
> \- \[\] An officially supported task in the \`examples\` folder (such as GLUE/SQuAD, ...)
> \- \[\] My own task or dataset (give details below)
> 
> \### Reproduction
> 
> I am trying to run the new Gemma3 model, using version '4.50.0.dev0'.
> 
> When loading the model I get the error: 'Gemma3Config' object has no attribute 'vocab\_size'.
> 
> Looking into this it seems Gemma3Config has \`vocab\_size\` nested in a "text\_config" attribute. 
> 
> I try to load the model as AutoModelForCausalLM, running it with Gemma3ForConditionalGeneration does not raise this issue. Am I wrong in assuming I can run Gemma 3 as AutoModelForCausalLM?
> 
> \### Expected behavior
> 
> Loading the model as AutoModelForCausalLM.from\_pretrained without issue.

---

<div class="post-metadata">

**Author:** ![aydndglr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/aydndglr/32/46896_2.png) [@aydndglr](https://discuss.huggingface.co/u/aydndglr)\
**Post date:** [May 5, 2025, 4:09pm UTC](https://discuss.huggingface.co/t/cannot-export-tflite-using-optimum-for-a-fine-tuned-gemma-3-model-for-task-question-answering/148490/7 "2025-05-05T16:09:18Z")

</div>

Hello, I was able to overcome this problem by making some changes to the codes prepared by Google in colab. You can convert it directly to tflite and .task format later if desired. Instead of fine-tuning from the beginning, I used the already trained model.

```auto
import os
from google.colab import userdata
os.environ["HF_TOKEN"] = userdata.get('HF_TOKEN')

!pip3 install --upgrade -q -U bitsandbytes
!pip3 install --upgrade -q -U peft
!pip3 install --upgrade -q -U trl
!pip3 install --upgrade -q -U accelerate
!pip3 install --upgrade -q -U datasets
!pip3 install git+https://github.com/huggingface/transformers@v4.49.0-Gemma-3

! pip install git+https://github.com/google-ai-edge/ai-edge-torch
! pip install ai-edge-litert
! pip install mediapipe

!pip install huggingface_hub
from huggingface_hub import snapshot_download
import shutil

# 🔧 Ayarları yap
model_name = "username/model_repo" # ← fine-tuned model
local_dir = "/content/merged_model"

# 💾 Hugging Face
snapshot_download(
    repo_id=model_name,
    local_dir=local_dir,
    local_dir_use_symlinks=False # # Don't bother with symlink, just copy it directly
)

print(f"Model downloaded: {local_dir}")

!git clone https://github.com/google-ai-edge/ai-edge-torch.git

!pip uninstall numpy
!pip uninstall torch torchvision torchaudio
!pip uninstall ai-edge-torch ai-edge-litert ai-edge-quantizer torch-xla2 safetensors
!pip install numpy
!pip install torch torchvision torchaudio
!pip install -r https://raw.githubusercontent.com/google-ai-edge/ai-edge-torch/main/requirements.txt

!pip install --upgrade numpy
!pip install --upgrade --force-reinstall ai-edge-torch
!pip install --upgrade --force-reinstall ai-edge-litert
!pip install --upgrade --force-reinstall ai-edge-quantizer
!pip install --upgrade --force-reinstall torch-xla2
!pip install --upgrade --force-reinstall safetensors

from ai_edge_torch.generative.examples.gemma3 import gemma3
from ai_edge_torch.generative.utilities import converter
from ai_edge_torch.generative.utilities.model_builder import ExportConfig
from ai_edge_torch.generative.layers.experimental import kv_cache
import torch

def _create_mask(mask_len, kv_cache_max_len):
    mask = torch.full((mask_len, kv_cache_max_len), float('-inf'), dtype=torch.float32)
    return torch.triu(mask, diagonal=1).unsqueeze(0).unsqueeze(0)

def _create_export_config(prefill_seq_lens: list[int], kv_cache_max_len: int) -> ExportConfig:
    export_config = ExportConfig()
    export_config.prefill_mask = [_create_mask(i, kv_cache_max_len) for i in prefill_seq_lens]
    decode_mask = torch.full((1, kv_cache_max_len), float('-inf'), dtype=torch.float32)
    export_config.decode_mask = torch.triu(decode_mask, diagonal=1).unsqueeze(0).unsqueeze(0)
    export_config.kvcache_cls = kv_cache.KVCacheTransposed
    return export_config

with torch.inference_mode(True):
    checkpoint_path = "/content/merged_model"
    pytorch_model = gemma3.build_model_1b(
        checkpoint_path, kv_cache_max_len=2048
    )

    export_config = _create_export_config([1024], 2048)

    converter.convert_to_tflite(
        pytorch_model,
        output_path="/content/",
        output_name_prefix="gemma3_1b_finetune",
        prefill_seq_len=[1024],
        quantize=True,
        lora_ranks=None,
        export_config=export_config
    )

from mediapipe.tasks.python.genai.bundler import llm_bundler

def build_task_bundle():
    config = llm_bundler.BundleConfig(
        tflite_model="/content/gemma3_1b_finetune_q8_ekv2048.tflite",
        tokenizer_model="/content/merged_model/tokenizer.model",
        start_token="<bos>",
        stop_tokens=["<eos>", "<end_of_turn>"],
        output_filename="/content/gemma3-1b-it.task",
        enable_bytes_to_unicode_mapping=False,
        prompt_prefix="<start_of_turn>user\n",
        prompt_suffix="<end_of_turn>\n<start_of_turn>model\n",
    )
    llm_bundler.create_bundle(config)

build_task_bundle()

```
