Skip to content

Conversation

@Bob-Chen222
Copy link
Collaborator

@Bob-Chen222 Bob-Chen222 commented Nov 10, 2025

Motivation

Support grok-2 on sglang-jax

Modifications

Fixed OOM error of grok model loading on tpu-v6e-32
WIP on the weights loader. added one parameter concat_axis for class WeightMapping to specify how to combine the TP sparse moe weights that have the same name.

Currently, the decoding generates jibberish output and I believe it is caused by some errors in weight loading.

Accuracy Tests

Benchmarking and Profiling

Checklist

  • Please use English, otherwise it will be closed.
  • The purpose of the PR, or link existing issues this PR will resolve.
  • The test plan, such as providing test command.
  • (Optional) The necessary documentation update.

@gemini-code-assist
Copy link

Summary of Changes

Hello @Bob-Chen222, 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 is a significant step towards enabling Grok-2 model execution on sglang-TPU. It tackles critical issues such as OOM errors during model loading and refines the weight mapping mechanism to better accommodate tensor-parallel architectures. While the core infrastructure for Grok-2 on TPU is being laid out, the author notes that further debugging is required as the current decoding output is incorrect, indicating ongoing work on the weight loading process.

Highlights

  • Grok-2 TPU Support: This pull request introduces initial support for running the Grok-2 model on sglang-TPU, specifically targeting tpu-v6e-32.
  • OOM Error Resolution: Modifications have been made to address and fix Out-Of-Memory (OOM) errors encountered during the loading of the Grok model on tpu-v6e-32.
  • Flexible Weight Mapping: A new parameter, concat_axis, has been added to the WeightMapping class to provide more control over how tensor-parallel weights with the same name are combined during model loading.
  • GrokAttention Refactoring: The GrokAttention class has been refactored to use separate q_proj, k_proj, and v_proj linear layers, aligning with the expected checkpoint format for Grok models.
  • Distributed JAX Array Handling: Improvements have been made in bench_one_batch.py to correctly handle distributed JAX arrays for next_token_ids and next_token_logits using jax_mh.process_allgather during correctness tests.
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.

@Bob-Chen222 Bob-Chen222 marked this pull request as draft November 10, 2025 03:27
@JamesBrianD
Copy link
Collaborator

Can you provide the launch server command?

@JamesBrianD
Copy link
Collaborator

@JamesBrianD
Copy link
Collaborator

JamesBrianD commented Nov 10, 2025

May I ask how you start the service? I start the service and it gives me an error that it failed to load the tokenizer, then I follow this HF link to start the service with --tokenizer-path alvarobartt/grok-2-tokenizer .

JAX_COMPILATION_CACHE_DIR=/tmp/jit_cache uv run python -u -m sgl_jax.launch_server   --model-path /models/xai-grok-2   --trust-remote-code   --dist-init-addr=$IP:10011   --nnodes=8   --tp-size=16   --device=tpu   --random-seed=3   --mem-fraction-static=0.8   --chunked-prefill-size=2048   --download-dir=/dev/shm   --dtype=bfloat16   --max-running-requests=128   --skip-server-warmup   --page-size=128 --tokenizer-path alvarobartt/grok-2-tokenizer  --node-rank=$NODE_RANK

@JamesBrianD JamesBrianD self-requested a review November 10, 2025 08:18
Comment on lines 343 to 344
self.rotary_emb = ScalingRotaryEmbedd
ing(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Error formatting modification

super().__init__()
assert dtype == jnp.bfloat16
config.num_hidden_layers = 1
config.num_hidden_layers = 4 #for debugging
Copy link
Collaborator

Choose a reason for hiding this comment

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

The hack operation loads only four layers of model, which can lead to precision exceptions

@JamesBrianD
Copy link
Collaborator

May I ask how you start the service? I start the service and it gives me an error that it failed to load the tokenizer, then I follow this HF link to start the service with --tokenizer-path alvarobartt/grok-2-tokenizer .

JAX_COMPILATION_CACHE_DIR=/tmp/jit_cache uv run python -u -m sgl_jax.launch_server   --model-path /models/xai-grok-2   --trust-remote-code   --dist-init-addr=$IP:10011   --nnodes=8   --tp-size=16   --device=tpu   --random-seed=3   --mem-fraction-static=0.8   --chunked-prefill-size=2048   --download-dir=/dev/shm   --dtype=bfloat16   --max-running-requests=128   --skip-server-warmup   --page-size=128 --tokenizer-path alvarobartt/grok-2-tokenizer  --node-rank=$NODE_RANK

@Bob-Chen222 If we handle the grok2 TiktokenTokenizer like sglang does, we don't have to specify an additional tokenizer-path.

@Bob-Chen222 Bob-Chen222 changed the title Feat/grok rebase bob [WIP] Feat/grok rebase bob Nov 10, 2025
@JamesBrianD JamesBrianD changed the base branch from feat/grok-rebase to main November 12, 2025 07:26
@JamesBrianD
Copy link
Collaborator

This is already solved in this PR #360

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.

4 participants