Skip to content

Conversation

LakshmiKalaKadali
Copy link

This PR creates the STU layer with necessary dependent functions.

Copy link

Summary of Changes

Hello @LakshmiKalaKadali, 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 significantly expands the Keras framework by integrating a new Structured Transformer Unit (STU) layer. This layer provides a comprehensive set of functionalities for building sophisticated attention-based models, including multi-head attention, efficient handling of variable-length sequences through jagged tensor conversions, and optimized inference via KV caching. The introduction of the STU layer aims to offer more flexible and performant options for deep learning practitioners working with Keras.

Highlights

  • New STU Layer Introduction: This pull request introduces a new STU (Structured Transformer Unit) layer, designed to enhance Keras's capabilities for advanced attention mechanisms and sequence processing.
  • Multi-Head Attention (MHA) Implementations: Core Keras implementations for both standard and cached Multi-Head Attention (MHA) are added, supporting efficient inference and token-by-token generation.
  • Jagged Tensor Handling: New utility functions are included to convert between jagged and padded dense tensor formats, which is crucial for effectively managing variable-length sequences within the attention mechanism and KV caching.
  • KV Cache Management: The STU layer incorporates logic for updating and constructing Key-Value (KV) caches, enabling more efficient sequential processing in models.
  • Modular Layer Structure: The STU functionality is organized into several modular Python files, including common utilities, output computation, attention preprocessing, UQVK (U, Q, V, K) computation, and the main STU layer definition, promoting maintainability and reusability.
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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description

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 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

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

@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 a new STU layer with its dependent functions, including custom attention mechanisms, jagged tensor handling, and caching logic. The implementation is comprehensive but contains several critical issues that need to be addressed. My review has identified problems related to incorrect imports, bugs in attention padding and masking, potential type errors from handling optional tensors, and incorrect tensor manipulations. Additionally, there are several unused parameters and opportunities to replace manual implementations with standard Keras operations for better performance and maintainability. Addressing these points will be crucial for ensuring the correctness and robustness of the new STU layer.

Comment on lines +40 to +44
row_ids = ops.broadcast_to(ops.reshape(ids, (N, 1)), (N, N))
col_ids = ops.transpose(row_ids)
row_ids = ops.reshape(row_ids, (1, N, N))
col_ids = ops.reshape(col_ids, (1, N, N))
max_ids = None

Choose a reason for hiding this comment

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

high

The logic in this else block does not correctly handle batches. It creates row_ids and col_ids with a shape of (1, N, N), ignoring the batch size B calculated earlier. If the batch size is greater than 1, this will lead to incorrect masking due to broadcasting. The mask should have a shape of (B, N, N) to be consistent with the if num_targets is not None branch.

    else:
        row_ids = ops.broadcast_to(ops.reshape(ids, (1, N, 1)), (B, N, N))
        col_ids = ops.broadcast_to(ops.reshape(ids, (1, 1, N)), (B, N, N))
        max_ids = None

Comment on lines +16 to +17
num_heads: int = 1,
linear_dim: int = -1,

Choose a reason for hiding this comment

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

medium

The parameters num_heads and linear_dim are unused in this function and should be removed.

v_cache: Optional[keras.KerasTensor] = None
kv_caching_offsets: Optional[keras.KerasTensor] = None

def __init__(self, config: STULayerConfig, is_inference: bool = False, **kwargs):

Choose a reason for hiding this comment

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

medium

The parameter is_inference is unused in the __init__ method and should be removed.

Copy link

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

LakshmiKalaKadali and others added 8 commits September 22, 2025 18:23
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.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.

1 participant