Skip to content

[Recipes][GSM8K] Plumb attention DP knob and MoE optimization switches - #2822

Open
wenxindongwork wants to merge 1 commit into
atwigg/mlperffrom
wxd/gsm8k-moe-attention-dp-plumbing
Open

wenxindongwork wants to merge 1 commit into
atwigg/mlperffrom
wxd/gsm8k-moe-attention-dp-plumbing

Conversation

@wenxindongwork

Copy link
Copy Markdown
Collaborator

Summary

Plumb ROLLOUT_ATTENTION_DP knob and MoE optimization switches to the GSM8K distributed recipe (gsm_8k_35b_256.sh) and math launcher (math_gsm8k_dist/k8s_launcher.sh), mirroring the plumbing added to mlperf_base.sh and deepswe_dist/k8s_launcher.sh in PRs #2806 and #2811.

  • Attention DP:
    • ROLLOUT_ATTENTION_DP=false configures rollout sharding_strategy with enable_dp_attention: false, tensor_parallelism: 1, and expert_parallelism: 4.
    • Sets default max_num_seqs: 64.
    • Injects kv_tp_size=4 into MAXTEXT_EXTRA_FLAGS for the MaxText trainer to align KV head shapes during weight sync.
  • MoE Optimization Switches:
    • Adds TPU_SMALL_ALL_REDUCE, TPU_MOE_SINGLE_SORT, TPU_MOE_ROUTER_TOPK, and TPU_MOE_LOWTOK (defaulting to 0 / "sort").
    • Plumbs them and VLLM_RAY_EXTRA_ENV_VARS_TO_COPY through math_gsm8k_dist/k8s_launcher.sh to the rollout containers and vLLM Ray TPU workers.

Verification

  • Tested syntax via bash -n on both scripts.
  • Verified dry-run YAML outputs (DRY_RUN=true) under default parameters and with ROLLOUT_ATTENTION_DP=false ROLLOUT_FP8=true TRAINER_FP8=true TPU_SMALL_ALL_REDUCE=1 TPU_MOE_SINGLE_SORT=1 TPU_MOE_ROUTER_TOPK=rowmax TPU_MOE_LOWTOK=1 MAX_STEPS=10.

Add ROLLOUT_ATTENTION_DP (default true) and MoE optimization switches (TPU_SMALL_ALL_REDUCE, TPU_MOE_SINGLE_SORT, TPU_MOE_ROUTER_TOPK, TPU_MOE_LOWTOK) to the GSM8K 35B recipe and launcher.

- ROLLOUT_ATTENTION_DP=false updates rollout sharding to disable DP attention, raises max_num_seqs to 64, and appends kv_tp_size=4 to MAXTEXT_EXTRA_FLAGS.
- MoE switches default to 0 / sort to preserve baseline behavior.
- Propagates optimization flags and VLLM_RAY_EXTRA_ENV_VARS_TO_COPY to rollout containers and Ray workers.

Signed-off-by: wenxindongwork <wenxindong@google.com>
@gemini-code-assist

Copy link
Copy Markdown
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 GSM8K distributed training recipe and its associated Kubernetes launcher by adding configurable knobs for attention data parallelism and MoE-specific optimizations. These changes improve flexibility for different model configurations and ensure that performance-tuning flags are correctly passed to the rollout containers and distributed workers, maintaining consistency with existing infrastructure patterns.

Highlights

  • Attention DP Configuration: Added a new ROLLOUT_ATTENTION_DP knob to the GSM8K recipe, allowing users to toggle attention data parallelism. When disabled, it automatically adjusts sharding strategies and injects necessary KV head shape flags to prevent weight sync mismatches.
  • MoE Optimization Switches: Introduced new environment variables for MoE optimizations (TPU_SMALL_ALL_REDUCE, TPU_MOE_SINGLE_SORT, TPU_MOE_ROUTER_TOPK, and TPU_MOE_LOWTOK) and plumbed them through the launcher to ensure they are correctly propagated to vLLM Ray workers.
New Features

🧠 You can now enable Memory (public preview) 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.

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 the 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 counterproductive. 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.

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. ↩

@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 configuration options for attention data parallelism and propagates MoE and collective optimization environment variables to Ray workers in the Kubernetes launcher and recipe scripts. Feedback focuses on adhering to the 'One well-lit path' style guide rule by avoiding speculative dictionary lookups and defaults in Python snippets, simplifying the Bash loop used to build the Ray environment variable list, and removing redundant environment variable assignments in ROLLOUT_EXTRA_ENV since they are already explicitly forwarded by the launcher.

Comment on lines +137 to +144
VLLM_ADDITIONAL_CONFIG="$(python3 -c '
import json, os
cfg = json.loads(os.environ.get("VLLM_ADDITIONAL_CONFIG") or "{}")
strategy = cfg.setdefault("sharding", {}).setdefault("sharding_strategy", {})
strategy["enable_dp_attention"] = False
strategy["tensor_parallelism"] = 1
print(json.dumps(cfg, separators=(",", ":")))
')"

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

To adhere to the One well-lit path guideline in the style guide, avoid using speculative .setdefault() with fabricated defaults (like empty dictionaries {}) on structured schemas. Instead, explicitly initialize the nested keys if they are missing.

Suggested change
VLLM_ADDITIONAL_CONFIG="$(python3 -c '
import json, os
cfg = json.loads(os.environ.get("VLLM_ADDITIONAL_CONFIG") or "{}")
strategy = cfg.setdefault("sharding", {}).setdefault("sharding_strategy", {})
strategy["enable_dp_attention"] = False
strategy["tensor_parallelism"] = 1
print(json.dumps(cfg, separators=(",", ":")))
')"
VLLM_ADDITIONAL_CONFIG="$(python3 -c '
import json, os
cfg = json.loads(os.environ.get("VLLM_ADDITIONAL_CONFIG") or "{}")
if "sharding" not in cfg:
cfg["sharding"] = {}
if "sharding_strategy" not in cfg["sharding"]:
cfg["sharding"]["sharding_strategy"] = {}
strategy = cfg["sharding"]["sharding_strategy"]
strategy["enable_dp_attention"] = False
strategy["tensor_parallelism"] = 1
print(json.dumps(cfg, separators=(",", ":")))
')"
References
  1. One well-lit path (No defensive fallbacks): Never use speculative getattr(obj, 'field', default), dict.get('key', default) on structured schemas, dict(getattr(obj, 'metadata', None) or {}), .setdefault(...) with fabricated defaults, or isinstance(..., Mapping) duck-typing branches. Access attributes and keys directly on a single, strictly typed contract. (link)

Comment on lines +146 to +150
_rollout_kv_tp="$(python3 -c '
import json, os
s = json.loads(os.environ["VLLM_ADDITIONAL_CONFIG"])["sharding"]["sharding_strategy"]
print(int(s.get("expert_parallelism", 1)) * int(s["tensor_parallelism"]))
')"

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

To adhere to the One well-lit path guideline in the style guide, avoid speculative dict.get('key', default) on structured schemas. Instead, explicitly check for the key or handle it cleanly.

Suggested change
_rollout_kv_tp="$(python3 -c '
import json, os
s = json.loads(os.environ["VLLM_ADDITIONAL_CONFIG"])["sharding"]["sharding_strategy"]
print(int(s.get("expert_parallelism", 1)) * int(s["tensor_parallelism"]))
')"
_rollout_kv_tp="$(python3 -c '
import json, os
s = json.loads(os.environ["VLLM_ADDITIONAL_CONFIG"])["sharding"]["sharding_strategy"]
ep = s["expert_parallelism"] if "expert_parallelism" in s else 1
tp = s["tensor_parallelism"]
print(int(ep) * int(tp))
')"
References
  1. One well-lit path (No defensive fallbacks): Never use speculative getattr(obj, 'field', default), dict.get('key', default) on structured schemas, dict(getattr(obj, 'metadata', None) or {}), .setdefault(...) with fabricated defaults, or isinstance(..., Mapping) duck-typing branches. Access attributes and keys directly on a single, strictly typed contract. (link)

Comment on lines +243 to +249
for _opt_var in TPU_SMALL_ALL_REDUCE TPU_MOE_SINGLE_SORT TPU_MOE_ROUTER_TOPK TPU_MOE_LOWTOK; do
if [[ -n "${VLLM_RAY_EXTRA_ENV_VARS_TO_COPY:-}" ]]; then
export VLLM_RAY_EXTRA_ENV_VARS_TO_COPY="${VLLM_RAY_EXTRA_ENV_VARS_TO_COPY},${_opt_var}"
else
export VLLM_RAY_EXTRA_ENV_VARS_TO_COPY="${_opt_var}"
fi
done

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

The loop propagating optimization flags to VLLM_RAY_EXTRA_ENV_VARS_TO_COPY can be simplified to a single, more readable if-else block. This avoids executing a loop in bash and improves maintainability.

Suggested change
for _opt_var in TPU_SMALL_ALL_REDUCE TPU_MOE_SINGLE_SORT TPU_MOE_ROUTER_TOPK TPU_MOE_LOWTOK; do
if [[ -n "${VLLM_RAY_EXTRA_ENV_VARS_TO_COPY:-}" ]]; then
export VLLM_RAY_EXTRA_ENV_VARS_TO_COPY="${VLLM_RAY_EXTRA_ENV_VARS_TO_COPY},${_opt_var}"
else
export VLLM_RAY_EXTRA_ENV_VARS_TO_COPY="${_opt_var}"
fi
done
if [[ -n "${VLLM_RAY_EXTRA_ENV_VARS_TO_COPY:-}" ]]; then
export VLLM_RAY_EXTRA_ENV_VARS_TO_COPY="${VLLM_RAY_EXTRA_ENV_VARS_TO_COPY},TPU_SMALL_ALL_REDUCE,TPU_MOE_SINGLE_SORT,TPU_MOE_ROUTER_TOPK,TPU_MOE_LOWTOK"
else
export VLLM_RAY_EXTRA_ENV_VARS_TO_COPY="TPU_SMALL_ALL_REDUCE,TPU_MOE_SINGLE_SORT,TPU_MOE_ROUTER_TOPK,TPU_MOE_LOWTOK"
fi

ROLLOUT_RAIDEN_ENV="USE_RAIDEN_FFI=false RAIDEN_USE_FFI=0"
fi
export ROLLOUT_EXTRA_ENV="${ROLLOUT_EXTRA_ENV:-} PYTHONUNBUFFERED=1 TUNIX_IS_INTERNAL_ENV=false USE_BATCHED_RPA_LONG_CTX_KERNEL=${USE_BATCHED_RPA_LONG_CTX_KERNEL} RAGGED_GATHER_REDUCE_VERSION=${RAGGED_GATHER_REDUCE_VERSION} RAIDEN_DEVICES_PER_HOST=${RAIDEN_DEVICES_PER_HOST} PREFUSE_MOE_WEIGHTS=${ROLLOUT_PREFUSE_MOE_WEIGHTS} ROLLOUT_PREFUSE_MOE_WEIGHTS=${ROLLOUT_PREFUSE_MOE_WEIGHTS} ROLLOUT_MESH_TP=${ROLLOUT_MESH_TP} ROLLOUT_TENSOR_PARALLEL_SIZE=${ROLLOUT_MESH_TP} ENABLE_PREFIX_CACHING=${ENABLE_PREFIX_CACHING} VLLM_MAX_NUM_SEQS=${VLLM_MAX_NUM_SEQS} VLLM_GPU_MEMORY_UTILIZATION=${VLLM_GPU_MEMORY_UTILIZATION} NUM_PRECOMPILE_WORKERS=${NUM_PRECOMPILE_WORKERS} NEW_MODEL_DESIGN=${NEW_MODEL_DESIGN} ATTN_BUCKETIZED_NUM_REQS=${ATTN_BUCKETIZED_NUM_REQS} ATTN_CUSTOM_NUM_REQS_BUCKETS=${ATTN_CUSTOM_NUM_REQS_BUCKETS} ONEHOT_MOE_PERMUTE_THRESHOLD=${ONEHOT_MOE_PERMUTE_THRESHOLD} VLLM_MOE_CHUNK_SIZE=${VLLM_MOE_CHUNK_SIZE} SLICE_ROPE_CACHE=${SLICE_ROPE_CACHE} DP_SCHED_BATCH_PREFILL=${DP_SCHED_BATCH_PREFILL} VLLM_ENABLE_V1_MULTIPROCESSING=${VLLM_ENABLE_V1_MULTIPROCESSING} VLLM_LOGGING_LEVEL=${VLLM_LOGGING_LEVEL} LIBTPU_INIT_ARGS=\"${LIBTPU_INIT_ARGS}\" ${ROLLOUT_RAIDEN_ENV}"
export ROLLOUT_EXTRA_ENV="${ROLLOUT_EXTRA_ENV:-} PYTHONUNBUFFERED=1 TUNIX_IS_INTERNAL_ENV=false USE_BATCHED_RPA_LONG_CTX_KERNEL=${USE_BATCHED_RPA_LONG_CTX_KERNEL} RAGGED_GATHER_REDUCE_VERSION=${RAGGED_GATHER_REDUCE_VERSION} RAIDEN_DEVICES_PER_HOST=${RAIDEN_DEVICES_PER_HOST} PREFUSE_MOE_WEIGHTS=${ROLLOUT_PREFUSE_MOE_WEIGHTS} ROLLOUT_PREFUSE_MOE_WEIGHTS=${ROLLOUT_PREFUSE_MOE_WEIGHTS} ROLLOUT_MESH_TP=${ROLLOUT_MESH_TP} ROLLOUT_TENSOR_PARALLEL_SIZE=${ROLLOUT_MESH_TP} ENABLE_PREFIX_CACHING=${ENABLE_PREFIX_CACHING} VLLM_MAX_NUM_SEQS=${VLLM_MAX_NUM_SEQS} VLLM_GPU_MEMORY_UTILIZATION=${VLLM_GPU_MEMORY_UTILIZATION} NUM_PRECOMPILE_WORKERS=${NUM_PRECOMPILE_WORKERS} NEW_MODEL_DESIGN=${NEW_MODEL_DESIGN} ATTN_BUCKETIZED_NUM_REQS=${ATTN_BUCKETIZED_NUM_REQS} ATTN_CUSTOM_NUM_REQS_BUCKETS=${ATTN_CUSTOM_NUM_REQS_BUCKETS} ONEHOT_MOE_PERMUTE_THRESHOLD=${ONEHOT_MOE_PERMUTE_THRESHOLD} VLLM_MOE_CHUNK_SIZE=${VLLM_MOE_CHUNK_SIZE} SLICE_ROPE_CACHE=${SLICE_ROPE_CACHE} DP_SCHED_BATCH_PREFILL=${DP_SCHED_BATCH_PREFILL} VLLM_ENABLE_V1_MULTIPROCESSING=${VLLM_ENABLE_V1_MULTIPROCESSING} VLLM_LOGGING_LEVEL=${VLLM_LOGGING_LEVEL} LIBTPU_INIT_ARGS=\"${LIBTPU_INIT_ARGS}\"${TPU_SMALL_ALL_REDUCE:+ TPU_SMALL_ALL_REDUCE=${TPU_SMALL_ALL_REDUCE}}${TPU_MOE_SINGLE_SORT:+ TPU_MOE_SINGLE_SORT=${TPU_MOE_SINGLE_SORT}}${TPU_MOE_ROUTER_TOPK:+ TPU_MOE_ROUTER_TOPK=${TPU_MOE_ROUTER_TOPK}}${TPU_MOE_LOWTOK:+ TPU_MOE_LOWTOK=${TPU_MOE_LOWTOK}}${VLLM_RAY_EXTRA_ENV_VARS_TO_COPY:+ VLLM_RAY_EXTRA_ENV_VARS_TO_COPY=\"${VLLM_RAY_EXTRA_ENV_VARS_TO_COPY}\"} ${ROLLOUT_RAIDEN_ENV}"

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

Since k8s_launcher.sh has been updated to explicitly plumb and forward TPU_SMALL_ALL_REDUCE, TPU_MOE_SINGLE_SORT, TPU_MOE_ROUTER_TOPK, TPU_MOE_LOWTOK, and VLLM_RAY_EXTRA_ENV_VARS_TO_COPY (lines 620-625 in k8s_launcher.sh), appending them to ROLLOUT_EXTRA_ENV here is completely redundant. Removing them simplifies this extremely long line and avoids double-passing these variables.

Suggested change
export ROLLOUT_EXTRA_ENV="${ROLLOUT_EXTRA_ENV:-} PYTHONUNBUFFERED=1 TUNIX_IS_INTERNAL_ENV=false USE_BATCHED_RPA_LONG_CTX_KERNEL=${USE_BATCHED_RPA_LONG_CTX_KERNEL} RAGGED_GATHER_REDUCE_VERSION=${RAGGED_GATHER_REDUCE_VERSION} RAIDEN_DEVICES_PER_HOST=${RAIDEN_DEVICES_PER_HOST} PREFUSE_MOE_WEIGHTS=${ROLLOUT_PREFUSE_MOE_WEIGHTS} ROLLOUT_PREFUSE_MOE_WEIGHTS=${ROLLOUT_PREFUSE_MOE_WEIGHTS} ROLLOUT_MESH_TP=${ROLLOUT_MESH_TP} ROLLOUT_TENSOR_PARALLEL_SIZE=${ROLLOUT_MESH_TP} ENABLE_PREFIX_CACHING=${ENABLE_PREFIX_CACHING} VLLM_MAX_NUM_SEQS=${VLLM_MAX_NUM_SEQS} VLLM_GPU_MEMORY_UTILIZATION=${VLLM_GPU_MEMORY_UTILIZATION} NUM_PRECOMPILE_WORKERS=${NUM_PRECOMPILE_WORKERS} NEW_MODEL_DESIGN=${NEW_MODEL_DESIGN} ATTN_BUCKETIZED_NUM_REQS=${ATTN_BUCKETIZED_NUM_REQS} ATTN_CUSTOM_NUM_REQS_BUCKETS=${ATTN_CUSTOM_NUM_REQS_BUCKETS} ONEHOT_MOE_PERMUTE_THRESHOLD=${ONEHOT_MOE_PERMUTE_THRESHOLD} VLLM_MOE_CHUNK_SIZE=${VLLM_MOE_CHUNK_SIZE} SLICE_ROPE_CACHE=${SLICE_ROPE_CACHE} DP_SCHED_BATCH_PREFILL=${DP_SCHED_BATCH_PREFILL} VLLM_ENABLE_V1_MULTIPROCESSING=${VLLM_ENABLE_V1_MULTIPROCESSING} VLLM_LOGGING_LEVEL=${VLLM_LOGGING_LEVEL} LIBTPU_INIT_ARGS=\"${LIBTPU_INIT_ARGS}\"${TPU_SMALL_ALL_REDUCE:+ TPU_SMALL_ALL_REDUCE=${TPU_SMALL_ALL_REDUCE}}${TPU_MOE_SINGLE_SORT:+ TPU_MOE_SINGLE_SORT=${TPU_MOE_SINGLE_SORT}}${TPU_MOE_ROUTER_TOPK:+ TPU_MOE_ROUTER_TOPK=${TPU_MOE_ROUTER_TOPK}}${TPU_MOE_LOWTOK:+ TPU_MOE_LOWTOK=${TPU_MOE_LOWTOK}}${VLLM_RAY_EXTRA_ENV_VARS_TO_COPY:+ VLLM_RAY_EXTRA_ENV_VARS_TO_COPY=\"${VLLM_RAY_EXTRA_ENV_VARS_TO_COPY}\"} ${ROLLOUT_RAIDEN_ENV}"
export ROLLOUT_EXTRA_ENV="${ROLLOUT_EXTRA_ENV:-} PYTHONUNBUFFERED=1 TUNIX_IS_INTERNAL_ENV=false USE_BATCHED_RPA_LONG_CTX_KERNEL=${USE_BATCHED_RPA_LONG_CTX_KERNEL} RAGGED_GATHER_REDUCE_VERSION=${RAGGED_GATHER_REDUCE_VERSION} RAIDEN_DEVICES_PER_HOST=${RAIDEN_DEVICES_PER_HOST} PREFUSE_MOE_WEIGHTS=${ROLLOUT_PREFUSE_MOE_WEIGHTS} ROLLOUT_PREFUSE_MOE_WEIGHTS=${ROLLOUT_PREFUSE_MOE_WEIGHTS} ROLLOUT_MESH_TP=${ROLLOUT_MESH_TP} ROLLOUT_TENSOR_PARALLEL_SIZE=${ROLLOUT_MESH_TP} ENABLE_PREFIX_CACHING=${ENABLE_PREFIX_CACHING} VLLM_MAX_NUM_SEQS=${VLLM_MAX_NUM_SEQS} VLLM_GPU_MEMORY_UTILIZATION=${VLLM_GPU_MEMORY_UTILIZATION} NUM_PRECOMPILE_WORKERS=${NUM_PRECOMPILE_WORKERS} NEW_MODEL_DESIGN=${NEW_MODEL_DESIGN} ATTN_BUCKETIZED_NUM_REQS=${ATTN_BUCKETIZED_NUM_REQS} ATTN_CUSTOM_NUM_REQS_BUCKETS=${ATTN_CUSTOM_NUM_REQS_BUCKETS} ONEHOT_MOE_PERMUTE_THRESHOLD=${ONEHOT_MOE_PERMUTE_THRESHOLD} VLLM_MOE_CHUNK_SIZE=${VLLM_MOE_CHUNK_SIZE} SLICE_ROPE_CACHE=${SLICE_ROPE_CACHE} DP_SCHED_BATCH_PREFILL=${DP_SCHED_BATCH_PREFILL} VLLM_ENABLE_V1_MULTIPROCESSING=${VLLM_ENABLE_V1_MULTIPROCESSING} VLLM_LOGGING_LEVEL=${VLLM_LOGGING_LEVEL} LIBTPU_INIT_ARGS=\"${LIBTPU_INIT_ARGS}\" ${ROLLOUT_RAIDEN_ENV}"

This branch has not been deployed

No deployments
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