Skip to content

Conversation

DavidLandup0
Copy link
Collaborator

@DavidLandup0 DavidLandup0 commented Jul 14, 2025

Description of the change

Google Colab example: https://colab.research.google.com/drive/1X8trkvwn0hzMdvkSlzVUBXkLjC-JUXWm

One-Liner Usage

image

Comparison with HuggingFace

With both models in non-trainable/eval modes and no temperature set - equal outputs:

Prompt: 'What is the meaning of life, the universe and everything?'
Input IDs: [3923, 374, 279, 7438, 315, 2324, 11, 279, 15861, 323, 4395, 30]

Token-by-token generation comparison:
================================================================================
Token 1: HF=10771 (' According')  |  Keras=10771 (' According')
Token 2: HF=  311 ('        to')  |  Keras=  311 ('        to')
Token 3: HF=31164 ('   Douglas')  |  Keras=31164 ('   Douglas')
Token 4: HF=27329 ('     Adams')  |  Keras=27329 ('     Adams')
Token 5: HF=   11 ('         ,')  |  Keras=   11 ('         ,')
Token 6: HF=  279 ('       the')  |  Keras=  279 ('       the')
Token 7: HF= 4320 ('    answer')  |  Keras= 4320 ('    answer')
Token 8: HF=  374 ('        is')  |  Keras=  374 ('        is')
Token 9: HF=  220 ('          ')  |  Keras=  220 ('          ')
Token 10: HF= 2983 ('        42')  |  Keras= 2983 ('        42')

================================================================================
Generated sequences:
HF:    What is the meaning of life, the universe and everything? According to Douglas Adams, the answer is 42
Keras: What is the meaning of life, the universe and everything? According to Douglas Adams, the answer is 42

Checklist

  • I have added all the necessary unit tests for my change.
  • I have verified that my change does not break existing code and works with all backends (TensorFlow, JAX, and PyTorch).
  • My PR is based on the latest changes of the main branch (if unsure, rebase the code).
  • I have followed the Keras Hub Model contribution guidelines in making these changes.
  • I have followed the Keras Hub API design guidelines in making these changes.
  • I have signed the Contributor License Agreement.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @DavidLandup0, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request, currently a Work In Progress, introduces core utility functions for the SmolLM3 model. It lays the groundwork for the model's backbone and causal language model components by implementing essential attention mechanisms, including helpers for Rotary Positional Embeddings and Grouped Query Attention, culminating in a comprehensive eager attention forward pass.

Highlights

  • New Utility File: A new file keras_hub/src/models/smollm3/smollm3_utils.py has been added to house foundational utility functions for the SmolLM3 model.
  • Rotary Positional Embeddings (RoPE): Helper functions rotate_half (lines 4-7) and apply_rotary_pos_emb (lines 10-15) are introduced to facilitate the application of Rotary Positional Embeddings to query and key tensors.
  • Grouped Query Attention (GQA) / Multi-Query Attention (MQA): The repeat_kv function (lines 18-25) is added to support Grouped Query Attention (GQA) or Multi-Query Attention (MQA) by repeating key/value states as needed.
  • Eager Attention Mechanism: An eager_attention_forward function (lines 28-52) is implemented, providing a complete attention pass including scaling, causal masking, softmax, and dropout, designed for eager execution.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This PR introduces utility functions for a new SmolLM3 model. The changes are a work in progress. My review focuses on ensuring correctness, robustness, and maintainability.

I've identified a critical issue in rotate_half that could cause problems on the JAX backend, a high-severity issue in eager_attention_forward regarding the unconditional application of dropout, and a medium-severity issue about the lack of docstrings for the new public functions.

Providing this feedback now should help in finalizing the implementation.

Comment on lines 5 to 7
x1 = x[..., : x.shape[-1] // 2]
x2 = x[..., x.shape[-1] // 2 :]
return ops.concatenate((-x2, x1), axis=-1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This implementation of rotate_half is susceptible to a known bug with XLA compilation on JAX when using ops.concatenate, as noted in keras_hub/src/layers/modeling/rotary_embedding.py. To ensure backend compatibility and robustness, adopt the safer implementation pattern used in RotaryEmbedding.

Suggested change
x1 = x[..., : x.shape[-1] // 2]
x2 = x[..., x.shape[-1] // 2 :]
return ops.concatenate((-x2, x1), axis=-1)
x1, x2 = ops.split(x, 2, axis=-1)
half_rot_tensor = ops.stack((-x2, x1), axis=-2)
return ops.reshape(half_rot_tensor, ops.shape(x))

@keras-team keras-team deleted a comment from gemini-code-assist bot Jul 16, 2025
Copy link
Member

@mattdangerw mattdangerw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some quick drive by comments, know this is still wip! Looking good so far!

layer_types,
mlp_bias,
rms_norm_epsilon,
layer_norm_epsilon,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually there's some of these terms (like the epsilon's and rope theta) that have a consistent value across all the presets we care about, and we give them defaults here. Not super important, just for people that wanted an easier time making a custom small version of the arch or something like that.

)


def eager_attention_forward(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a hf/transforms-ism we'd rather not inherit. perfer just keeping the code on the layer directly, fine to use a private helper to decompose it a bit more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very late answer - but yep. Just kept this here during the initial prototyping because it made it easier to functionally test each individual component for numerical equivalence. Refactored to a class-based approach aftewards.

@divyashreepathihalli
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the SmolLM3 model, including its backbone, causal language model, preprocessor, tokenizer, and associated layers. The changes are comprehensive and add a new model family to the library. My review has identified several critical issues that need to be addressed. These include a runtime error in the score method of the SmolLM3CausalLM class and an incorrect method call in the SmolLM3Tokenizer that will lead to an AttributeError. Additionally, there are several instances of leftover debugging code, style guide violations such as the use of type hints in function signatures, and unused code that should be removed. While the PR is a work in progress, these issues, especially the critical ones, should be resolved to ensure the model's functionality and maintainability.

@DavidLandup0 DavidLandup0 changed the title [WIP] [SmolLM3] Add Backbone and CausalLM [SmolLM3] Add Backbone, CausalLM + Converter for HuggingFace Weights Oct 11, 2025
@DavidLandup0
Copy link
Collaborator Author

@divyashreepathihalli - looks like keras 3.8 didn't yet release RMSNormalization, but it's available in newer versions, so the tests pass for all nightly/stable versions, but not for the old one.

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