Skip to content

Fix LM head for Gemma-2 - #1420

Merged
Baiju Meswani (baijumeswani) merged 5 commits into
mainfrom
kvaishnavi/lm-head-mul
Apr 25, 2025
Merged

Fix LM head for Gemma-2#1420
Baiju Meswani (baijumeswani) merged 5 commits into
mainfrom
kvaishnavi/lm-head-mul

Conversation

@kunal-vaishnavi

Copy link
Copy Markdown
Contributor

Description

This PR fixes a bug with the language modeling head (LM head) for Gemma-2.

Motivation and Context

When Gemma-3 was added, Gemma-2 was refactored to work for both Gemma-2 and Gemma-3. During the refactoring process, however, the setting of the scale attribute for the LM head of Gemma-2 was not removed. This causes an extra Mul node to be created. It also causes two Mul nodes to have the same name (/lm_head/Mul) as well.

Justin Chu (justinchuby) added a commit to justinchuby/onnxruntime-genai that referenced this pull request Apr 24, 2025
if args.timings: started_timestamp = time.time()

prompt = f'{args.chat_template.format(input=text)}'
prompt = f'{args.chat_template.format(system_prompt=system_prompt, input=text)}'

Check warning

Code scanning / CodeQL

Unused named argument in formatting call

Surplus named argument for string format. An argument named 'system_prompt' is provided, but it is not required by [format "<|im_start|>user<|im_sep|> {input}<|im_end|> <|im_start|>assistant<|im_sep|>"](1).

Copilot Autofix

AI over 1 year ago

To fix the issue, we need to remove the surplus system_prompt argument from the format call on line 107. This involves:

  1. Verifying that the format string in args.chat_template does not include a placeholder for system_prompt.
  2. Removing the system_prompt=system_prompt argument from the format call, leaving only the required arguments.

This change ensures that the code is cleaner and avoids unnecessary arguments in the formatting call.


Suggested changeset 1
examples/python/model-chat.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/examples/python/model-chat.py b/examples/python/model-chat.py
--- a/examples/python/model-chat.py
+++ b/examples/python/model-chat.py
@@ -106,3 +106,3 @@
 
-        prompt = f'{args.chat_template.format(system_prompt=system_prompt, input=text)}'
+        prompt = f'{args.chat_template.format(input=text)}'
         input_tokens = tokenizer.encode(prompt)
EOF
@@ -106,3 +106,3 @@

prompt = f'{args.chat_template.format(system_prompt=system_prompt, input=text)}'
prompt = f'{args.chat_template.format(input=text)}'
input_tokens = tokenizer.encode(prompt)
Copilot is powered by AI and may make mistakes. Always verify output.
@baijumeswani
Baiju Meswani (baijumeswani) deleted the kvaishnavi/lm-head-mul branch April 25, 2025 22:57
kunal-vaishnavi added a commit that referenced this pull request May 9, 2025
### Description

This PR fixes accuracy issues with Google's Gemma models by using
bfloat16 precision, [always using float32 precision to compute any
LayerNorms](https://github.com/huggingface/transformers/blob/fee1190601b5d04ec6d3f7f58fd22788d7f3236d/src/transformers/models/gemma3/modeling_gemma3.py#L141-L146),
and casting the output logits to float32 always.

### Motivation and Context

This PR has been tested with Gemma-2 and Gemma-3. It is using the
bfloat16 changes from [this
PR](#1447) as well as
the missing final norm changes from [this
PR](#1420).

---------

Co-authored-by: Nenad Banfic <46795300+nenad1002@users.noreply.github.com>
Co-authored-by: Nenad Banfic <nebanfic@microsoft.com>
Ryan Hill (RyanUnderhill) pushed a commit that referenced this pull request May 12, 2025
### Description

This PR fixes accuracy issues with Google's Gemma models by using
bfloat16 precision, [always using float32 precision to compute any
LayerNorms](https://github.com/huggingface/transformers/blob/fee1190601b5d04ec6d3f7f58fd22788d7f3236d/src/transformers/models/gemma3/modeling_gemma3.py#L141-L146),
and casting the output logits to float32 always.

### Motivation and Context

This PR has been tested with Gemma-2 and Gemma-3. It is using the
bfloat16 changes from [this
PR](#1447) as well as
the missing final norm changes from [this
PR](#1420).

---------

Co-authored-by: Nenad Banfic <46795300+nenad1002@users.noreply.github.com>
Co-authored-by: Nenad Banfic <nebanfic@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants