Skip to content

Filter JAX packages from user requirements to prevent accelerator backend override#80

Merged
JyotinderSingh merged 4 commits intomainfrom
jax-filtering
Mar 9, 2026
Merged

Filter JAX packages from user requirements to prevent accelerator backend override#80
JyotinderSingh merged 4 commits intomainfrom
jax-filtering

Conversation

@JyotinderSingh
Copy link
Collaborator

Summary

  • User requirements.txt files containing jax, jaxlib, libtpu, or libtpu-nightly are now automatically filtered before the container build, preventing them from overriding the accelerator-specific JAX installation (e.g., jax[tpu], jax[cuda12])
  • Users can opt out of filtering on a per-line basis by appending # kr:keep to any line in their requirements.txt
  • The content hash used for image caching now reflects the filtered requirements, so JAX-only changes in requirements.txt no longer trigger unnecessary container rebuilds

Problem

The Dockerfile template installs the correct JAX backend first (jax[tpu]>=0.4.6 for TPU, jax[cuda12] for GPU), then installs user requirements after. If a user's local requirements.txt contains jax or jax[cpu] (common during local development), pip would silently reinstall JAX without the accelerator extras, causing runtime failures or performance degradation on TPU/GPU.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 enhances the container build process by automatically filtering JAX-related packages from user-provided requirements.txt files. This prevents conflicts with the JAX installation managed by the Dockerfile template, ensuring the correct accelerator backend is used. The changes also improve caching efficiency by considering the filtered requirements when generating the image content hash.

Highlights

  • JAX Package Filtering: Automatically filters jax, jaxlib, libtpu, and libtpu-nightly from user requirements.txt files to prevent overriding accelerator-specific JAX installations.
  • Opt-Out Mechanism: Users can bypass the filtering by appending # kr:keep to specific lines in their requirements.txt.
  • Improved Caching: The content hash for image caching now reflects the filtered requirements, avoiding unnecessary container rebuilds for JAX-only changes.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • container_builder.py
    • Added JAX package filtering logic.
    • Updated requirements hashing to include filtered content.
    • Modified Dockerfile creation to use filtered requirements.
  • container_builder_test.py
    • Added tests for JAX package filtering.
    • Added tests to ensure the content hash is not affected by JAX packages.
Activity
  • Implemented JAX package filtering in container_builder.py.
  • Added a mechanism to allow users to bypass the filter with # kr:keep.
  • Updated the content hash calculation to reflect filtered requirements.
  • Added unit tests for the filtering logic in container_builder_test.py.
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
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 pull request 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 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
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 a sensible feature to filter JAX-related packages from user requirements, preventing conflicts with the pre-installed accelerator-specific JAX versions. However, a potential security issue exists where sensitive information (like credentials in a URL) could be leaked into the logs when a requirement line is filtered. Additionally, I suggest refactoring a small part of the implementation to avoid reading and processing the requirements file twice, which would improve both efficiency and maintainability. The implementation is otherwise robust and accompanied by a comprehensive set of tests.

Comment on lines +252 to +256

# Copy requirements.txt if it exists
# Copy requirements.txt (with JAX-related packages filtered out)
if requirements_path and os.path.exists(requirements_path):
shutil.copy(requirements_path, os.path.join(tmpdir, "requirements.txt"))
with open(requirements_path, "r") as f:
filtered = _filter_jax_requirements(f.read())
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The requirements.txt file is being read and filtered here, but it's also read and filtered earlier in _hash_requirements (lines 162-164). This duplicates work and file I/O.

Consider refactoring to read and filter the requirements file only once within the get_or_build_container function. The resulting filtered content could then be passed as an argument to both _hash_requirements and _build_and_push to avoid this duplication.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
_KEEP_MARKER = "# kr:keep"


def _filter_jax_requirements(requirements_content: str) -> str:
Copy link
Member

Choose a reason for hiding this comment

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

Will we also support requirements specified in pyproject.toml?

Copy link
Collaborator Author

@JyotinderSingh JyotinderSingh Mar 9, 2026

Choose a reason for hiding this comment

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

Yes, we should add support for that in subsequent PRs. Created #81 for tracking.

@JyotinderSingh JyotinderSingh merged commit 5d0f1b3 into main Mar 9, 2026
4 checks passed
@JyotinderSingh JyotinderSingh deleted the jax-filtering branch March 9, 2026 19:42
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.

2 participants