Skip to content

fix(core): replace hard assertion with warning for NVTE_* env conflicts - #1

Open
xiaohong42 wants to merge 1 commit into
mainfrom
fix/attention-backend-env-conflict
Open

fix(core): replace hard assertion with warning for NVTE_* env conflicts#1
xiaohong42 wants to merge 1 commit into
mainfrom
fix/attention-backend-env-conflict

Conversation

@xiaohong42

Copy link
Copy Markdown
Owner

Problem

When running inside Docker containers whose base images pre-set NVTE_FLASH_ATTN, NVTE_FUSED_ATTN, or NVTE_UNFUSED_ATTN environment variables (e.g., ROCm training images set NVTE_FLASH_ATTN=0 and NVTE_FUSED_ATTN=1 for their TransformerEngine CK backend), Megatron-Core raises an AssertionError during model initialization if the configured attention_backend (flash, auto, etc.) expects different values.

Users cannot modify the base image and have no clean way to unset Docker ENV variables at the image level (docker exec sessions also inherit them).

Solution

Replace the hard assert in check_and_set_env_variable() with:

  • A warnings.warn() that informs the user of the mismatch and the override.
  • The existing os.environ[...] = str(expected_value) assignment that forces the correct value.

This preserves the intent of catching misconfigurations while avoiding crashes in containerized environments where users inherit env variables they did not explicitly set.

Changes

  • megatron/core/models/common/language_module/language_module.py: Replace AssertionError with warnings.warn() + override in _set_attention_backend().

Testing

Verified that:

  1. attention_backend=auto works with pre-set NVTE_FLASH_ATTN=0/NVTE_FUSED_ATTN=1 (previously crashed).
  2. attention_backend=flash works with pre-set NVTE_FUSED_ATTN=1 (previously crashed).
  3. A warning is emitted when env vars are overridden.
  4. No warning when env vars are unset or already match.

Replace the AssertionError in `_set_attention_backend` with a warning
and automatic override when NVTE_FLASH_ATTN, NVTE_FUSED_ATTN, or
NVTE_UNFUSED_ATTN environment variables conflict with the configured
`attention_backend`.

Problem:
When running inside Docker containers whose base images pre-set these
environment variables (e.g., ROCm training images set NVTE_FLASH_ATTN=0
and NVTE_FUSED_ATTN=1 for their own TransformerEngine CK backend),
Megatron-Core raises an AssertionError during model initialization if
the `attention_backend` config (flash, auto, etc.) expects different
values. Users cannot modify the base image and have no clean way to
unset Docker ENV variables at the image level.

Solution:
- Issue a `warnings.warn()` when a mismatch is detected, informing the
  user of the override and how to silence the warning.
- Proceed to set the correct value as before (the line after the old
  assert already did this for the None case).

This preserves the intent of catching misconfigurations while avoiding
crashes in containerized environments where users inherit env variables
they did not explicitly set.

Signed-off-by: xiaohong42 <xiaohong42@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.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