Skip to content

Add Diffusion Gemma model to Hub#2804

Draft
vijay-dabur wants to merge 5 commits into
keras-team:masterfrom
vijay-dabur:diffusion_gemma_model
Draft

Add Diffusion Gemma model to Hub#2804
vijay-dabur wants to merge 5 commits into
keras-team:masterfrom
vijay-dabur:diffusion_gemma_model

Conversation

@vijay-dabur

Copy link
Copy Markdown
Contributor

Description of the change

Reference

Colab Notebook

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.

@github-actions github-actions Bot added the Gemma Gemma model specific issues label Jul 16, 2026
@github-actions
github-actions Bot requested a review from laxmareddyp July 16, 2026 11:31

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

Copy link
Copy Markdown
Contributor

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 support for discrete block-diffusion language models in KerasHub, specifically implementing the BlockDiffusionLM and Gemma4BlockDiffusionLM tasks, their preprocessors, the EntropyBoundSampler, and a self-conditioning layer. It also updates the Gemma4 backbone and decoder blocks to support diffusion-specific mechanisms like dual layer scalars and bidirectional canvas attention, and adds a checkpoint conversion script for DiffusionGemma. The review feedback is highly constructive, pointing out critical robustness issues: ensuring task models can generate immediately after instantiation without explicit compilation, resetting the stateful sampler variable on new generation runs to prevent shape mismatch crashes across varying batch sizes, and strictly validating all multimodal inputs in the text-only preprocessor path to avoid silent failures.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +57 to +61
super().__init__(*args, **kwargs)
self.canvas_length = canvas_length
self.max_denoising_steps = max_denoising_steps
self.t_min = t_min
self.t_max = t_max

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

In KerasHub, task models should be callable for generation immediately after instantiation (e.g., when loaded from a preset) without requiring an explicit compile() call. Currently, self.sampler is only initialized inside compile(), which will cause generate() to raise an AttributeError if called on an uncompiled model.

Initializing self.sampler to a default EntropyBoundSampler in __init__ resolves this issue and ensures a seamless user experience.

        super().__init__(*args, **kwargs)
        self.canvas_length = canvas_length
        self.max_denoising_steps = max_denoising_steps
        self.t_min = t_min
        self.t_max = t_max
        from keras_hub.src.samplers.entropy_bound_sampler import (
            EntropyBoundSampler,
        )
        self.sampler = EntropyBoundSampler(
            vocabulary_size=self.backbone.vocabulary_size
        )
References
  1. Demand Robustness: Do not accept fragile code. If the proposed code is not robust enough or lacks proper error handling, explicitly tell the author why the current approach is brittle and what must be done to reinforce it. (link)

Comment on lines +96 to +97
def __call__(self, canvas, logits, step):
logits = ops.cast(logits, "float32")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

self._prev_argmax is a stateful keras.Variable that is lazily initialized on the first call. However, if the batch size changes between independent generate() calls, the shape of self._prev_argmax from the previous run will mismatch with the new batch size, causing a shape mismatch crash during assign or equal operations.

Resetting self._prev_argmax to None at step == 0 (the start of a new generation run) ensures that the variable is correctly re-initialized with the new batch shape, preventing any potential crashes.

Suggested change
def __call__(self, canvas, logits, step):
logits = ops.cast(logits, "float32")
def __call__(self, canvas, logits, step):
if step == 0:
self._prev_argmax = None
logits = ops.cast(logits, "float32")
References
  1. Demand Robustness: Do not accept fragile code. If the proposed code is not robust enough or lacks proper error handling, explicitly tell the author why the current approach is brittle and what must be done to reinforce it. (link)

Comment on lines +826 to +830
if self.text_only_model and audio is not None:
raise ValueError(
"The initialized preprocessor/model is text-only, but "
"`audio` is not `None`."
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

For a text-only preprocessor, passing multimodal inputs during training (call()) should raise a ValueError to prevent silently ignoring them. Currently, only audio is checked, while other multimodal inputs like images, videos, or pixel_values are silently ignored.

Updating the check to include all multimodal inputs ensures consistency with generate_preprocess() and prevents silent failures.

Suggested change
if self.text_only_model and audio is not None:
raise ValueError(
"The initialized preprocessor/model is text-only, but "
"`audio` is not `None`."
)
if self.text_only_model and (
images is not None
or videos is not None
or pixel_values is not None
or audio is not None
):
raise ValueError(
"The initialized preprocessor/model is text-only, but "
"multimodal inputs (images, videos, pixel_values, or audio) "
"were provided."
)
References
  1. Demand Robustness: Do not accept fragile code. If the proposed code is not robust enough or lacks proper error handling, explicitly tell the author why the current approach is brittle and what must be done to reinforce it. (link)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Gemma Gemma model specific issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant