Skip to content
Merged
Changes from 2 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
45 changes: 28 additions & 17 deletions transformer_engine/pytorch/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ def make_graphed_callables(
num_warmup_iters: int = 3,
allow_unused_input: bool = False,
sample_kwargs: Optional[SingleOrTuple[Dict[str, Any]]] = None,
fp8_enabled: bool = False,
fp8_enabled: SingleOrTuple[bool] = False,
fp8_calibrating: bool = False,
fp8_recipe: Optional[Recipe] = None,
fp8_group: Optional[dist_group_type] = None,
Expand Down Expand Up @@ -896,8 +896,9 @@ def make_graphed_callables(

FP8-related parameters
----------------------
fp8_enabled: bool, default = `True`
whether or not to enable fp8
fp8_enabled: (tuple of) bool, default = `False`
Comment thread
timmoon10 marked this conversation as resolved.
whether or not to enable fp8.
If tuple, the length must match the number of modules.
fp8_calibrating: bool, default = `False`
calibration mode allows collecting statistics such as amax and scale
data of fp8 tensors even when executing without fp8 enabled. This is
Expand All @@ -919,17 +920,23 @@ def make_graphed_callables(
"""
set_capture_start()

if fp8_enabled and fp8_recipe is None:
fp8_recipe = get_default_fp8_recipe()
elif not fp8_enabled:
fp8_recipe = None

# Handle single module.
just_one_callable = False
if not isinstance(modules, tuple):
just_one_callable = True
modules = (modules,)

if not isinstance(fp8_enabled, tuple):
assert isinstance(fp8_enabled, bool), "fp8_enabled must be a bool or a tuple of bools"
Comment thread
ptrendx marked this conversation as resolved.
fp8_enabled = (fp8_enabled,) * len(modules)
else:
assert len(fp8_enabled) == len(modules), "fp8_enabled must have the same length as modules"
Comment thread
buptzyb marked this conversation as resolved.
Outdated
if any(fp8_enabled) and fp8_recipe is None:
fp8_recipe = get_default_fp8_recipe()
elif not any(fp8_enabled):
fp8_recipe = None
module_uses_fp8 = dict(zip(modules, fp8_enabled))
Comment thread
buptzyb marked this conversation as resolved.
Outdated

# Store FP8 tensors to reset later.
saved_fp8_tensors = save_fp8_tensors(modules, fp8_recipe=fp8_recipe)

Expand All @@ -944,15 +951,19 @@ def wrap_autocast(block):
old_call_funcs[block_cls] = block_cls.__call__

# Wrap the original call function of the module class.
def call_func(*args, **kwargs):
with fp8_autocast(
enabled=fp8_enabled,
calibrating=fp8_calibrating,
fp8_recipe=fp8_recipe,
fp8_group=fp8_group,
_graph=True,
):
outputs = old_call_funcs[block_cls](*args, **kwargs)
def call_func(self, *args, **kwargs):
if not module_uses_fp8[self]:
Comment thread
buptzyb marked this conversation as resolved.
Outdated
fp8_context = contextlib.nullcontext()
else:
fp8_context = fp8_autocast(
enabled=True,
calibrating=fp8_calibrating,
fp8_recipe=fp8_recipe,
fp8_group=fp8_group,
_graph=True,
)
with fp8_context:
outputs = old_call_funcs[block_cls](self, *args, **kwargs)
return outputs

block_cls.__call__ = call_func
Expand Down