Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions megatron/core/fp8_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,29 @@ def get_fp8_context(config: TransformerConfig, layer_no: int = -1, is_init: bool

return fp8_context

def get_fp8_disabled_context(config: TransformerConfig, is_init: bool = False):
"""Return a context manager that disables TE quantization.

Use this around submodule construction or execution that must stay in a higher
precision while its enclosing module uses an FP8 or FP4 context.

Args:
config: Transformer configuration that controls quantization.
is_init: Whether to disable the parameter-initialization context instead of
the forward autocast context.

Returns:
A disabled TE quantization context when quantization is active, otherwise a
no-op context.
"""
if is_init:
if not (config.fp8_param or config.fp4_param):
return nullcontext()
return transformer_engine.pytorch.fp8_model_init(enabled=False)
if not (config.fp8 or config.fp4):
return nullcontext()
return transformer_engine.pytorch.fp8_autocast(enabled=False)

else:

def get_fp8_recipe(config: TransformerConfig):
Expand All @@ -881,6 +904,10 @@ def get_fp8_context(config: TransformerConfig, layer_no: int = -1, is_init: bool
"""Returns dummy fp8 context manager since TE is not available."""
return nullcontext()

def get_fp8_disabled_context(config: TransformerConfig, is_init: bool = False):
"""Return a no-op context manager since TE is not available."""
return nullcontext()


if HAVE_TE:
from transformer_engine.pytorch.fp8 import FP8GlobalStateManager
Expand Down
Loading