diff --git a/tests/pytorch/test_fusible_ops.py b/tests/pytorch/test_fusible_ops.py index 17efba6f515..b3c3d3d62dc 100644 --- a/tests/pytorch/test_fusible_ops.py +++ b/tests/pytorch/test_fusible_ops.py @@ -4,6 +4,7 @@ from __future__ import annotations + from collections.abc import Iterable, Sequence import io import os @@ -5706,3 +5707,103 @@ def test_linear_inference_loop( quantization=quantization, recipe=recipe, ) + + @pytest.mark.parametrize("quantization", (None, "mxfp8", "fp8_current_scaling")) + def test_grouped_linear_training_loop( + self, + *, + stages: Sequence[str] = ("train", "infer", "train", "infer"), + num_groups: int = 3, + weight_shape: tuple[int, int] = (128, 128), + dtype: Optional[torch.dtype] = None, + device: torch.device = "cuda", + quantization: Optional[str], + ) -> None: + """Training loops with grouped linear op.""" + if dtype is None: + dtype = torch.bfloat16 if is_bf16_available() else torch.float32 + + # Tensor shapes + split_sizes = [128 * (1 + group_idx % 2) for group_idx in range(num_groups)] + splits = torch.tensor(split_sizes, dtype=torch.int64, device=device) + out_features, in_features = weight_shape + in_shape = [sum(split_sizes), in_features] + out_shape = in_shape[:-1] + [out_features] + + # Quantization recipe + quantized_compute = quantization is not None + recipe = make_recipe(quantization) + maybe_skip_quantization(quantization, dims=in_shape, device=device, dtype=dtype) + maybe_skip_quantization(quantization, dims=out_shape) + + # Initialize weights + with te.quantized_model_init(enabled=quantized_compute, recipe=recipe): + module = te_ops.GroupedLinear( + num_groups, + in_features, + out_features, + bias=False, + dtype=dtype, + device=device, + ) + weights = list(module.parameters()) + assert len(weights) == len(split_sizes) + + # Expected numerical error + tols = dtype_tols(dtype) + if dtype == torch.float32: + tols = dtype_tols(torch.float16) # TF32 GEMM + if quantization is not None: + tols = quantization_tols(quantization) + + # Training loop stages + for stage in stages: + training = stage == "train" + if training: + with torch.no_grad(): + # Update parameters with random values to simulate + # optimizer step or FSDP param all-gather + for weight in weights: + weight.copy_(torch.empty_like(weight).uniform_()) + weight.grad = None + + # Forward reference implementation + x_ref, x_test = make_reference_and_test_tensors( + in_shape, + quantization=quantization, + test_dtype=dtype, + test_device=device, + requires_grad=training, + ) + weights_ref = [to_cpu(weight) for weight in weights] + outputs_ref = [ + torch.nn.functional.linear(x_part, weight_ref) + for x_part, weight_ref in zip(torch.split(x_ref, split_sizes), weights_ref) + ] + y_ref = torch.cat(outputs_ref) + + # Grouped linear forward + with ( + torch.inference_mode(not training), + te.autocast(enabled=quantized_compute, recipe=recipe), + ): + y_test = module(x_test, splits) + + # Backward pass + if training: + dy_ref, dy_test = make_reference_and_test_tensors( + out_shape, + quantization=quantization, + test_dtype=dtype, + test_device=device, + requires_grad=False, + ) + y_ref.backward(dy_ref) + y_test.backward(dy_test) + + # Check results + assert_close(y_test, y_ref, **tols) + if training: + assert_close_grads(x_test, x_ref, **tols) + for weight, weight_ref in zip(weights, weights_ref): + assert_close_grads(weight, weight_ref, **tols) diff --git a/tests/pytorch/test_grouped_linear.py b/tests/pytorch/test_grouped_linear.py index 0a0c42b03eb..19b27798c5d 100644 --- a/tests/pytorch/test_grouped_linear.py +++ b/tests/pytorch/test_grouped_linear.py @@ -4,6 +4,7 @@ import os import random +from unittest.mock import Mock from typing import Dict, List, Optional, Sequence import pytest @@ -60,6 +61,35 @@ seed = 1234 reset_rng_states() + +@pytest.mark.parametrize("has_skip", [False, True]) +@pytest.mark.parametrize("capturing", [False, True]) +def test_grouped_linear_skip_update_capture_short_circuit(monkeypatch, has_skip, capturing): + """Only an active capture skip tensor may override first-microbatch accumulation.""" + FP8GlobalStateManager.reset() + module = GroupedLinear(2, 128, 128, bias=False, params_dtype=torch.bfloat16) + x = torch.randn(256, 128, device="cuda", dtype=torch.bfloat16) + skip = torch.ones(1, device="cuda") if has_skip else None + monkeypatch.setattr( + FP8GlobalStateManager.quantization_state, "skip_fp8_weight_update_tensor", skip + ) + capture = Mock(return_value=capturing) + monkeypatch.setattr(FP8GlobalStateManager, "fp8_graph_capturing", capture) + original = _GroupedLinear.forward + seen = [] + + def forward(ctx, inp, fwd_args, *args): + seen.append((fwd_args.is_first_microbatch, fwd_args.skip_fp8_weight_update)) + return original(ctx, inp, fwd_args, *args) + + monkeypatch.setattr(_GroupedLinear, "forward", forward) + with torch.no_grad(): + module(x, [128, 128], is_first_microbatch=True) + assert capture.call_count == int(has_skip) + assert seen[0][0] == (not (has_skip and capturing)) + assert seen[0][1] is (skip if capturing else None) + + NVTE_TEST_NVINSPECT_ENABLED = int(os.environ.get("NVTE_TEST_NVINSPECT_ENABLED", "0")) if NVTE_TEST_NVINSPECT_ENABLED: @@ -2310,6 +2340,161 @@ def test_single_grouped_primary_mxfp8_bypasses_weight_workspace(): assert "weight" not in grouped_linear._fp8_workspaces +@pytest.mark.skipif(not _mxfp8_available, reason=_reason_for_no_mxfp8) +@pytest.mark.parametrize("primary_fp8", [False, True]) +@pytest.mark.parametrize("save_original_input", [False, True]) +def test_grouped_runtime_usage_transitions(primary_fp8, save_original_input): + """Preserve primary usages and rebuild missing columnwise workspaces after inference.""" + from transformer_engine.pytorch.distributed import activation_recompute_forward + + fp8_recipe = recipe.MXFP8BlockScaling() + if not is_module_grouped_tensor_path_supported(fp8_recipe, torch.bfloat16): + pytest.skip("Native MXFP8 grouped GEMM is unavailable.") + FP8GlobalStateManager.reset() + with quantized_model_init(enabled=primary_fp8, recipe=fp8_recipe): + module = GroupedLinear( + 2, + 128, + 128, + bias=False, + params_dtype=torch.bfloat16, + single_grouped_weight=True, + use_grouped_tensor=True, + save_original_input=save_original_input, + ) + splits = torch.tensor([128, 128], device="cuda") + x = torch.randn(256, 128, device="cuda", dtype=torch.bfloat16, requires_grad=True) + + def run(*, grad_enabled=True, first=False): + with torch.set_grad_enabled(grad_enabled), autocast(enabled=True, recipe=fp8_recipe): + return module(x, splits, is_first_microbatch=first) + + run(first=True).sum().backward() + reference_grad = x.grad.clone() + reference_wgrad = module.weight.grad.clone() + weight_q = module.weight.quantizer if primary_fp8 else module._get_weight_quantizers()[0] + assert weight_q.columnwise_usage + module.eval() + run(grad_enabled=False, first=True) + # Persistent parameters retain columnwise usage; temporary workspaces follow this call. + assert weight_q.columnwise_usage == primary_fp8 + # Force a rowwise-only workspace, as on a first inference microbatch. + if not primary_fp8: + module._fp8_workspaces.clear() + run(grad_enabled=False, first=True) + assert module._fp8_workspaces["weight"].columnwise_data is None + module.train() + module.weight.grad = None + x.grad = None + # Keep two forwards alive before either backward; quantizers are shared references. + y1, y2 = run(), run() + assert weight_q.columnwise_usage + y1.sum().backward() + y2.sum().backward() + torch.testing.assert_close(x.grad, 2 * reference_grad, rtol=1e-2, atol=5e-3) + torch.testing.assert_close(module.weight.grad, 2 * reference_wgrad, rtol=1e-2, atol=5e-3) + with activation_recompute_forward(activation_recompute=True, recompute_phase=False): + run(grad_enabled=False, first=True) + assert weight_q.columnwise_usage + with activation_recompute_forward(activation_recompute=True, recompute_phase=True): + run().sum().backward() + assert weight_q.columnwise_usage + module.weight.requires_grad_(False) + run().sum().backward() + input_q = module.quantizers["scaling_fwd"][module._offsets["input"]] + assert not input_q.columnwise_usage + + +@pytest.mark.skipif(not _mxfp8_available, reason=_reason_for_no_mxfp8) +def test_grouped_runtime_recipe_rebuild(): + """Recipe rebuilds replace cached quantizers while preserving saved forwards.""" + recipes = [ + recipe.MXFP8BlockScaling(), + recipe.Float8CurrentScaling(), + recipe.MXFP8BlockScaling(), + ] + if not all(is_module_grouped_tensor_path_supported(r, torch.bfloat16) for r in recipes): + pytest.skip("Required native grouped GEMM recipes are unavailable.") + FP8GlobalStateManager.reset() + module = GroupedLinear( + 2, + 128, + 128, + bias=False, + params_dtype=torch.bfloat16, + single_grouped_weight=True, + use_grouped_tensor=True, + ) + reference = GroupedLinear( + 2, + 128, + 128, + bias=False, + params_dtype=torch.bfloat16, + use_grouped_tensor=True, + ) + reference.load_state_dict(module.state_dict()) + x = torch.randn(256, 128, device="cuda", dtype=torch.bfloat16, requires_grad=True) + ref_x = x.detach().clone().requires_grad_(True) + splits = torch.tensor([128, 128], device="cuda") + pending = [] + workspaces = [] + for fp8_recipe in recipes: + with autocast(enabled=True, recipe=fp8_recipe): + y = module(x, splits, is_first_microbatch=True) + ref_y = reference(ref_x, splits, is_first_microbatch=True) + pending.append((y, ref_y)) + workspace = module._fp8_workspaces["weight"] + assert workspace.quantizer is module._get_weight_quantizers()[0] + assert all(workspace.quantizer is not old.quantizer for old in workspaces) + workspaces.append(workspace) + for y, ref_y in reversed(pending): + torch.testing.assert_close(y, ref_y, rtol=1e-2, atol=5e-3) + y.sum().backward() + ref_y.sum().backward() + torch.testing.assert_close(x.grad, ref_x.grad, rtol=1e-2, atol=5e-3) + ref_wgrad = torch.stack([reference.weight0.grad, reference.weight1.grad]) + torch.testing.assert_close(module.weight.grad, ref_wgrad, rtol=1e-2, atol=5e-3) + + +@pytest.mark.parametrize( + "use_grouped_tensor,single_bias", [(False, False), (True, False), (True, True)] +) +@pytest.mark.parametrize("use_bias", [False, True]) +def test_grouped_linear_return_bias_with_frozen_weights(use_grouped_tensor, single_bias, use_bias): + """A trainable returned bias keeps the output differentiable when input/weight are frozen.""" + if use_grouped_tensor and not is_module_grouped_tensor_path_supported(None, torch.bfloat16): + pytest.skip("Native grouped GEMM is unavailable.") + module = GroupedLinear( + 2, + 128, + 128, + bias=use_bias, + return_bias=True, + params_dtype=torch.bfloat16, + single_grouped_weight=single_bias, + single_grouped_bias=single_bias, + use_grouped_tensor=use_grouped_tensor, + ) + for weight in _grouped_linear_weight_params(module): + weight.requires_grad_(False) + x = torch.randn(256, 128, device="cuda", dtype=torch.bfloat16) + splits = torch.tensor([128, 128], device="cuda" if use_grouped_tensor else "cpu") + y, biases = module(x, splits) + assert y.requires_grad == use_bias + if not use_bias: + assert isinstance(biases, list) and len(biases) == 2 + assert all(b.numel() == 0 for b in biases) + return + params = _grouped_linear_bias_params(module) + returned = [biases] if single_bias else biases + assert len(returned) == len(params) + assert all(value is param for value, param in zip(returned, params)) + (y.sum() + sum(value.sum() for value in returned)).backward() + for param in params: + torch.testing.assert_close(param.grad, torch.ones_like(param.grad), rtol=0, atol=0) + + def _clone_outputs(outputs): return [None if out is None else out.detach().clone() for out in outputs] @@ -3145,8 +3330,200 @@ def test_grouped_linear_grouped_tensor_path_skips_non_rht_nvfp4(): ) -def test_grouped_linear_delay_wgrad_rejects_implicit_fallback(monkeypatch): +@pytest.mark.parametrize( + "use_grouped_tensor,single_weight,single_bias", + [ + (False, False, False), + (True, False, False), + (True, True, False), + (True, False, True), + (True, True, True), + ], + ids=["split", "grouped-discrete", "single-weight", "single-bias", "single-both"], +) +@pytest.mark.parametrize( + "recipe_name", ["bf16", "delayed", "current", "mxfp8", "high_precision", "dequantized"] +) +@pytest.mark.parametrize("freeze_weight", [False, True]) +@pytest.mark.parametrize("input_requires_grad", [False, True]) +@pytest.mark.parametrize("delay_wgrad", [False, True]) +@pytest.mark.parametrize("grad_storage", ["grad", "preallocated_grad", "main_grad"]) +def test_grouped_linear_bias_gradient_accumulation( + use_grouped_tensor, + single_weight, + single_bias, + recipe_name, + freeze_weight, + input_requires_grad, + delay_wgrad, + grad_storage, +): + """Bias gradients accumulate during main backward, independently of deferred/frozen weights.""" + if recipe_name == "bf16": + fp8_recipe = None + elif recipe_name == "delayed": + fp8_recipe = recipe.DelayedScaling() + elif recipe_name == "current": + fp8_recipe = recipe.Float8CurrentScaling() + # Use exact power-of-two scales for the analytical FP32 main_grad reference. + fp8_recipe.fp8_quant_fwd_inp = recipe.QParams(power_2_scale=True) + fp8_recipe.fp8_quant_fwd_weight = recipe.QParams(power_2_scale=True) + fp8_recipe.fp8_quant_bwd_grad = recipe.QParams(power_2_scale=True) + else: + if not mxfp8_available: + pytest.skip(reason_for_no_mxfp8) + fp8_recipe = recipe.MXFP8BlockScaling( + backward_override=None if recipe_name == "mxfp8" else recipe_name + ) + if fp8_recipe is not None and not fp8_available: + pytest.skip(reason_for_no_fp8) + if use_grouped_tensor and not is_module_grouped_tensor_path_supported( + fp8_recipe, torch.bfloat16 + ): + pytest.skip("Recipe is unsupported by the native grouped-tensor path") + + FP8GlobalStateManager.reset() + module = GroupedLinear( + 2, + 128, + 128, + bias=True, + params_dtype=torch.bfloat16, + use_grouped_tensor=use_grouped_tensor, + single_grouped_weight=single_weight, + single_grouped_bias=single_bias, + delay_wgrad_compute=delay_wgrad, + fuse_wgrad_accumulation=grad_storage == "main_grad", + ) + weights = _grouped_linear_weight_params(module) + biases = _grouped_linear_bias_params(module) + with torch.no_grad(): + for param in weights: + data = param.rowwise_data if isinstance(param, GroupedTensor) else param + data.fill_(0.125) + param.requires_grad_(not freeze_weight) + for param in biases: + data = param.rowwise_data if isinstance(param, GroupedTensor) else param + data.fill_(0.5) + + hook_calls = {id(param): 0 for param in module.parameters()} + flat_main_grad = torch.zeros(2 * 128 * 128, device="cuda", dtype=torch.float32) + weight_views = flat_main_grad.view(2, 128, 128) + weight_views = [weight_views] if single_weight else list(weight_views.unbind()) + main_grad_ptrs = [] + if grad_storage == "main_grad": + # Model MCore's ownership: weight GEMM accumulates into an external flat buffer, + # while parameter hooks move unfused grads to main_grad and clear the handoff. + for param, view in zip(weights, weight_views): + param.main_grad = view + param.grad_added_to_main_grad = False + param.overwrite_main_grad = False + main_grad_ptrs.append(view.data_ptr()) + for param in biases: + param.main_grad = torch.zeros(param.shape, device="cuda", dtype=torch.float32) + param.grad_added_to_main_grad = False + + def accumulate(param): + hook_calls[id(param)] += 1 + if param.grad is not None and not param.grad_added_to_main_grad: + param.main_grad.add_(param.grad) + param.grad = None + + for param in module.parameters(): + if not param.requires_grad: + continue + if getattr(param, "skip_backward_post_hook", False): + module.register_wgrad_accumulation_and_reduce_hooks( + lambda param=param: accumulate(param) + ) + else: + param.register_post_accumulate_grad_hook(accumulate) + elif grad_storage == "preallocated_grad": + for param in biases: + param.grad = torch.zeros(param.shape, device="cuda", dtype=param.dtype) + + splits = torch.tensor([128, 128], dtype=torch.int64, device="cuda") + total_scale = 0 + try: + # Decreasing dY avoids delayed-scaling overflow from a larger second amax. + # Powers of two make output and gradients exactly representable in all tested recipes. + for step, scale in enumerate((2, 1)): + total_scale += scale + for param in weights: + param.grad = None + x = torch.full( + (256, 128), + 0.25, + device="cuda", + dtype=torch.bfloat16, + requires_grad=input_requires_grad, + ) + with autocast(enabled=fp8_recipe is not None, recipe=fp8_recipe): + y = module(x, splits, is_first_microbatch=step == 0) + torch.testing.assert_close(y, torch.full_like(y, 4.5), rtol=0, atol=0) + y.backward(torch.full_like(y, scale)) + if input_requires_grad: + torch.testing.assert_close( + x.grad, torch.full_like(x.grad, 16 * scale), rtol=0, atol=0 + ) + else: + assert x.grad is None + + def check_bias_grads(): + for param in biases: + grad = param.main_grad if grad_storage == "main_grad" else param.grad + assert grad is not None, "bias gradient was not returned by main backward" + torch.testing.assert_close( + grad, torch.full_like(grad, 128 * total_scale), rtol=0, atol=0 + ) + if grad_storage == "main_grad": + assert param.grad is None + assert hook_calls[id(param)] == step + 1 + + check_bias_grads() + if delay_wgrad: + module.backward_dw() + check_bias_grads() # Deferred weight hooks must not add dbias a second time. + for param in weights: + if freeze_weight: + assert param.grad is None + continue + grad = param.main_grad if grad_storage == "main_grad" else param.grad + assert grad is not None + expected = 32 * (total_scale if grad_storage == "main_grad" else scale) + torch.testing.assert_close(grad, torch.full_like(grad, expected), rtol=0, atol=0) + if grad_storage == "main_grad": + assert [param.main_grad.data_ptr() for param in weights] == main_grad_ptrs + finally: + FP8GlobalStateManager.reset() + + +@pytest.mark.parametrize("device", ["cuda", "meta"]) +@pytest.mark.parametrize("single_weight", [False, True]) +@pytest.mark.parametrize("single_bias", [False, True]) +def test_grouped_linear_delayed_wgrad_hook_ownership(device, single_weight, single_bias): + """Only weight hooks are deferred, including before meta parameters are materialized.""" + module = GroupedLinear( + 2, + 128, + 128, + bias=True, + device=device, + params_dtype=torch.bfloat16, + use_grouped_tensor=True, + single_grouped_weight=single_weight, + single_grouped_bias=single_bias, + delay_wgrad_compute=True, + ) + for name, param in module.named_parameters(): + assert getattr(param, "skip_backward_post_hook", False) == name.startswith("weight") + + +@pytest.mark.parametrize("backward_override", [None, "high_precision", "dequantized"]) +def test_grouped_linear_delay_wgrad_rejects_implicit_fallback(monkeypatch, backward_override): """Delayed wgrad reports when a grouped-tensor request used the legacy path.""" + if backward_override is not None and not mxfp8_available: + pytest.skip(reason_for_no_mxfp8) monkeypatch.setattr( "transformer_engine.pytorch.module.grouped_linear.is_module_grouped_tensor_path_supported", lambda *_args, **_kwargs: False, @@ -3161,10 +3538,14 @@ def test_grouped_linear_delay_wgrad_rejects_implicit_fallback(monkeypatch): delay_wgrad_compute=True, use_grouped_tensor=True, ) - x = torch.randn(16, 64, dtype=torch.bfloat16, device="cuda", requires_grad=True) - m_splits = torch.tensor([8, 8], dtype=torch.int64, device="cuda") + x = torch.randn(64, 64, dtype=torch.bfloat16, device="cuda", requires_grad=True) + m_splits = torch.tensor([32, 32], dtype=torch.int64, device="cuda") - grouped_linear(x, m_splits).sum().backward() + with autocast( + enabled=backward_override is not None, + recipe=recipe.MXFP8BlockScaling(backward_override=backward_override), + ): + grouped_linear(x, m_splits).sum().backward() with pytest.raises( RuntimeError, match="implicit fallback is unsupported with delay_wgrad_compute=True", diff --git a/tests/pytorch/test_grouped_mlp.py b/tests/pytorch/test_grouped_mlp.py index f5ce1f8ef83..ec6012f8456 100644 --- a/tests/pytorch/test_grouped_mlp.py +++ b/tests/pytorch/test_grouped_mlp.py @@ -13,6 +13,7 @@ import sys import types from typing import Optional +from unittest.mock import Mock import pytest @@ -489,6 +490,87 @@ def test_meta_single_grouped_weight_with_delayed_wgrad(self) -> None: assert grouped_weight.skip_backward_post_hook + @pytest.mark.parametrize("num_groups", [1, 8]) + @pytest.mark.parametrize("accumulate_into_main_grad", [False, True]) + @pytest.mark.parametrize("delay_wgrad_compute", [False, True]) + def test_wgrad_storage_and_hooks( + self, + monkeypatch: pytest.MonkeyPatch, + num_groups: int, + accumulate_into_main_grad: bool, + delay_wgrad_compute: bool, + ) -> None: + """Check independent wgrad storage, external main_grad views, and delayed hooks.""" + from transformer_engine.pytorch.ops.basic import grouped_linear as gl + + if not is_op_fuser_grouped_tensor_path_supported(None, torch.bfloat16): + pytest.skip("Native grouped GEMM is unavailable.") + op = te.ops.GroupedLinear( + num_groups, + 128, + 128, + bias=False, + device="cuda", + dtype=torch.bfloat16, + accumulate_into_main_grad=accumulate_into_main_grad, + delay_wgrad_compute=delay_wgrad_compute, + ) + model = te.ops.Sequential(op) + flat = torch.full((num_groups, 128, 128), 0.25, device="cuda", dtype=torch.float32) + weights = [getattr(op, f"weight{i}") for i in range(num_groups)] + for i, weight in enumerate(weights): + with torch.no_grad(): + weight.fill_(1) + if accumulate_into_main_grad: + # Use views of an external flat buffer to check that GEMM preserves aliases. + weight.main_grad = flat[i] + weight.overwrite_main_grad = False + weight.grad_added_to_main_grad = False + x = torch.full( + (num_groups * 128, 128), 0.125, device="cuda", dtype=torch.bfloat16, requires_grad=True + ) + splits = torch.full((num_groups,), 128, device="cuda", dtype=torch.int64) + seen = [] + original = gl.general_grouped_gemm_for_grouped_tensor + + def gemm(a, b, out, **kwargs): + if kwargs.get("layout") == "NT": + assert len(out) == num_groups + num_storages = len({g.untyped_storage().data_ptr() for g in out}) + assert num_storages == (1 if accumulate_into_main_grad else num_groups) + assert len({g.data_ptr() for g in out}) == num_groups + if accumulate_into_main_grad: + assert out[0].data_ptr() == flat.data_ptr() + else: + assert all(g._base is None for g in out) + seen.append(out) + return original(a, b, out, **kwargs) + + monkeypatch.setattr(gl, "general_grouped_gemm_for_grouped_tensor", gemm) + hook = Mock() + op.register_wgrad_accumulation_and_reduce_hooks(hook) + y = model(x, splits) + torch.testing.assert_close(y, torch.full_like(y, 16), rtol=0, atol=0) + y.backward(torch.full_like(y, 0.5)) + if delay_wgrad_compute: + assert not seen + hook.assert_not_called() + op.backward_dw() + hook.assert_called_once() + assert len(seen) == 1 + if accumulate_into_main_grad: + MegatronTrainingHelper.verify_main_grad_accumulation( + weights, + expected_main_grads=[torch.full_like(flat[i], 8.25) for i in range(num_groups)], + ) + for i, weight in enumerate(weights): + assert weight.main_grad.data_ptr() == flat[i].data_ptr() + else: + for weight in weights: + torch.testing.assert_close( + weight.grad, torch.full_like(weight.grad, 8), rtol=0, atol=0 + ) + def test_single_grouped_bias_uses_registered_packed_storage(self) -> None: """The grouped bias compute view must alias the registered trainable parent.""" op = te.ops.GroupedLinear( diff --git a/tests/pytorch/test_module_grouped_linear_distributed_weight.py b/tests/pytorch/test_module_grouped_linear_distributed_weight.py index 05fba219a99..2a664084c29 100644 --- a/tests/pytorch/test_module_grouped_linear_distributed_weight.py +++ b/tests/pytorch/test_module_grouped_linear_distributed_weight.py @@ -20,6 +20,9 @@ a specific assertion rather than drifting numerically. """ +import gc +import weakref + import pytest import torch @@ -283,3 +286,93 @@ def _finalize(wgrads, **kwargs): assert calls == ["fwd", "bwd", "finalize"], f"unexpected hook dispatch: {calls}" assert torch.count_nonzero(weight.wgrad_scratch) > 0, "wgrad never reached grad_buffer" + + +@pytest.mark.parametrize("single_weight", [False, True]) +@pytest.mark.parametrize("quantized", [False, True]) +@pytest.mark.parametrize("fused", [False, True]) +@pytest.mark.parametrize("requires_dgrad", [False, True]) +def test_gathered_weights_are_transient( + monkeypatch, single_weight, quantized, fused, requires_dgrad +): + """No gathered copy survives forward; backward must gather even when dgrad is disabled.""" + from transformer_engine.pytorch.module.grouped_linear import ( + _GroupedLinear, + is_module_grouped_tensor_path_supported, + ) + from transformer_engine.pytorch.tensor.grouped_tensor import GroupedTensor + + fp8_recipe = MXFP8BlockScaling() if quantized else None + if not is_module_grouped_tensor_path_supported(fp8_recipe, DTYPE): + pytest.skip("Requested native grouped GEMM is unavailable.") + te.quantization.FP8GlobalStateManager.reset() + module = te.GroupedLinear( + 2, + IN_F, + OUT_F, + bias=False, + params_dtype=DTYPE, + device=DEVICE, + single_grouped_weight=single_weight, + use_grouped_tensor=True, + fuse_wgrad_accumulation=fused, + ) + weights = [module.weight] if single_weight else [module.weight0, module.weight1] + for weight in weights: + weight.main_grad = torch.zeros(weight.shape, device=DEVICE, dtype=torch.float32) + weight.wgrad_scratch = torch.zeros_like(weight.main_grad) + weight.grad_buffer = lambda w=weight: w.wgrad_scratch + gathered_refs, calls = [], [] + + def gather(phase): + calls.append(phase) + if single_weight: + copy = GroupedTensor.make_grouped_tensor_from_rowwise_data( + num_tensors=2, + tensor_shape=(OUT_F, IN_F), + rowwise_data=weights[0].rowwise_data.detach().clone(), + dtype=DTYPE, + ) + copies = [copy] + else: + copies = [w.detach().clone() for w in weights] + assert all(not w.requires_grad for w in copies) + gathered_refs.extend(weakref.ref(w) for w in copies) + return copies + + def finalize(grads, **kwargs): + calls.append("finalize") + grads = [grads] if single_weight else grads + if fused: + for weight, grad in zip(weights, grads): + assert grad.data_ptr() == weight.wgrad_scratch.data_ptr() + weight.main_grad.add_(grad) + return [None] * len(weights) + return list(grads) + + leader = weights[0] + leader.is_distributed_weight = True + leader.materialize_group_for_forward = lambda: gather("fwd") + leader.materialize_group_for_backward = lambda **kw: gather("bwd") + leader.finalize_group_grads = finalize + prep = [] + original = _GroupedLinear._prepare_weights_for_grouped_tensor_gemm + + def prepare(*args, **kwargs): + prep.append(kwargs["cache_weight"]) + return original(*args, **kwargs) + + monkeypatch.setattr(_GroupedLinear, "_prepare_weights_for_grouped_tensor_gemm", prepare) + splits, x = _inputs(2) + x.requires_grad_(requires_dgrad) + with te.autocast(enabled=quantized, recipe=fp8_recipe): + y = module(x, splits, is_first_microbatch=True) + gc.collect() + assert not module._fp8_workspaces + assert all(ref() is None for ref in gathered_refs) + y.sum().backward() + assert calls == ["fwd", "bwd", "finalize"] + assert prep == [False] * (1 + int(requires_dgrad)) + for weight in weights: + grad = weight.main_grad if fused else weight.grad + assert grad is not None and torch.count_nonzero(grad) > 0 diff --git a/tests/pytorch/test_quantized_tensor.py b/tests/pytorch/test_quantized_tensor.py index 4e64215cdf5..6cd1bd403b1 100644 --- a/tests/pytorch/test_quantized_tensor.py +++ b/tests/pytorch/test_quantized_tensor.py @@ -25,6 +25,7 @@ QuantizedTensor, ) +from transformer_engine.pytorch.quantized_tensor import prepare_for_saving, restore_from_saved from transformer_engine.pytorch.utils import is_non_tn_fp8_gemm_supported from references.ref_per_tensor_cs import ref_per_tensor_cs_cast @@ -1187,3 +1188,48 @@ def clone_quantized_output(): assert torch.equal( buf_without_noop, buf_with_noop ), f"{quantization}/{usage}: noop flag fails to take effect because {attr} changed." + + +def test_saved_storage_protocol(monkeypatch): + """Preserve custom metadata, pass the full suffix, and use the returned remainder.""" + value, tail, replacement = (torch.tensor(i) for i in range(3)) + remainder = [replacement, tail] + + class Storage: + __slots__ = () + + def prepare_for_saving(self): + return [value], self + + def restore_from_saved(self, tensors): + assert isinstance(tensors, list) + assert len(tensors) == 2 and tensors[0] is value and tensors[1] is tail + return remainder + + monkeypatch.setattr(torch.compiler, "is_compiling", lambda: False) + storage = Storage() + saved, objects = prepare_for_saving(value, storage) + restored, remaining = restore_from_saved( + objects + [None], saved + [tail], return_saved_tensors=True + ) + assert restored[0] is value and restored[1] is storage and restored[2] is replacement + assert isinstance(remaining, list) and len(remaining) == 1 and remaining[0] is tail + + +@pytest.mark.parametrize("compiled_metadata", [False, True]) +def test_restore_real_quantized_storage(monkeypatch, compiled_metadata): + """Restore actual MXFP8 buffers and None/plain tensor slots with either metadata form.""" + if not te.is_mxfp8_available(): + pytest.skip("MXFP8 is unavailable.") + q = MXFP8Quantizer(te.DType.kFloat8E4M3, rowwise=True, columnwise=True) + q.internal = True + x = torch.randn(128, 128, device="cuda", dtype=torch.bfloat16) + quantized = q(x) + expected = quantized.dequantize().clone() + with monkeypatch.context() as context: + context.setattr(torch.compiler, "is_compiling", lambda: compiled_metadata) + saved, objects = prepare_for_saving(x, quantized, None) + restored, tail = restore_from_saved(objects, tuple(saved) + (x,), return_saved_tensors=True) + assert restored[0] is x and restored[2] is None + assert isinstance(tail, tuple) and tail[0] is x + torch.testing.assert_close(restored[1].dequantize(), expected, rtol=0, atol=0) diff --git a/transformer_engine/pytorch/cpp_extensions/gemm.py b/transformer_engine/pytorch/cpp_extensions/gemm.py index bc670b7fec9..314f4b88546 100644 --- a/transformer_engine/pytorch/cpp_extensions/gemm.py +++ b/transformer_engine/pytorch/cpp_extensions/gemm.py @@ -13,7 +13,13 @@ import transformer_engine_torch as tex from transformer_engine import te_device_type from ..constants import MXFP8_BLOCK_SCALING_SIZE, NVFP4_BLOCK_SCALING_SIZE, TE_DType, DType -from ..utils import ceil_div, get_cached_ones_tensor, get_sm_count, _empty_tensor +from ..utils import ( + ceil_div, + get_cached_ones_tensor, + get_device_compute_capability, + get_sm_count, + _empty_tensor, +) from ..quantized_tensor import QuantizedTensorStorage, Quantizer from ..tensor.float8_blockwise_tensor import Float8BlockQuantizer @@ -1189,7 +1195,7 @@ def _is_fp8_blockwise(operand) -> bool: # Hopper (SM90) uses a single shared alpha/beta scalar; # Blackwell+ (SM100) supports per-group alpha/beta arrays. - per_group = torch.cuda.get_device_capability() >= (10, 0) + per_group = get_device_compute_capability() >= (10, 0) num_alphabeta = num_tensors if per_group else 1 if alpha is None: diff --git a/transformer_engine/pytorch/module/grouped_linear.py b/transformer_engine/pytorch/module/grouped_linear.py index 76eded6fa84..d556ad79d3c 100644 --- a/transformer_engine/pytorch/module/grouped_linear.py +++ b/transformer_engine/pytorch/module/grouped_linear.py @@ -260,6 +260,7 @@ class GroupedLinearBwdArgs: m_splits: Optional[List[int]] = None num_gemms: int = 0 + num_bias_args: int = 0 weights_shape_1: int = 0 use_bias: bool = False @@ -291,11 +292,15 @@ def setup_saved_tensors(self, ctx: torch.autograd.function.FunctionCtx) -> None: self.inputmats = saved[:n] self.weights_fp8 = saved[n : 2 * n] self.saved_weights = saved[2 * n : 3 * n] - self.biases = saved[3 * n : 4 * n] + self.biases = saved[3 * n : 3 * n + self.num_bias_args] -def _grouped_linear_configure_quantizers(args: GroupedLinearFwdArgs, weights) -> None: - """Configure operand usage after materializing weights.""" +def _grouped_linear_configure_split_quantizers(args: GroupedLinearFwdArgs, weights) -> None: + """Configure per-expert quantizers for the split-quantize path after materialization. + + The grouped-tensor path configures its input quantizer before group_quantize + and its weight quantizers in _prepare_weights_for_grouped_tensor_gemm. + """ if args.input_quantizers[0] is not None: for input_quantizer in args.input_quantizers: input_quantizer.set_usage( @@ -321,6 +326,11 @@ def _grouped_linear_configure_quantizers(args: GroupedLinearFwdArgs, weights) -> weight_quantizer.set_usage(rowwise=True, columnwise=columnwise_usage) elif isinstance(weights[0], QuantizedTensorStorage): args.weight_quantizers = [weight._quantizer for weight in weights] + for weight_quantizer in args.weight_quantizers: + weight_quantizer.set_usage( + rowwise=True, + columnwise=True if columnwise_usage else None, + ) if args.output_quantizers[0] is not None: for output_quantizer in args.output_quantizers: output_quantizer.set_usage(rowwise=True, columnwise=False) @@ -352,7 +362,7 @@ def _grouped_linear_forward_impl( if is_dist_weight: weights = materialize_weight_for_forward(weights) - _grouped_linear_configure_quantizers(args, weights) + _grouped_linear_configure_split_quantizers(args, weights) weight_quantizers = args.weight_quantizers # Initialize input tensors @@ -486,6 +496,7 @@ def _grouped_linear_setup_ctx( bwd_args.m_splits = fwd_args.m_splits bwd_args.num_gemms = num_gemms + bwd_args.num_bias_args = len(fwd_args.biases) bwd_args.weights_shape_1 = fwd_args.inp.shape[-1] bwd_args.use_bias = fwd_args.use_bias @@ -588,6 +599,12 @@ def _grouped_linear_backward_impl( # Preprocess grad output out_features = grad_output.shape[-1] grad_output_view = grad_output.contiguous().view(-1, out_features) + delay_wgrad = args.wgrad_store is not None and args.wgrad_store.delay_wgrad_compute() + # Bias gradients belong to the main backward even when weights are frozen + # or their GEMM is deferred. Keep fused BF16 wgrad+dbias for immediate wgrad. + compute_dbias = args.use_bias and ( + args.fp8 or args.debug or not args.weights_requires_grad or delay_wgrad + ) grad_output_reference = args.grad_output_quantizers[0] if args.fp8 and isinstance(grad_output_reference, HybridQuantizer): # Usage is a runtime decision, not part of generation validation. @@ -604,7 +621,7 @@ def _grouped_linear_backward_impl( args.grad_output_quantizers, args.activation_dtype, with_quantized_output=args.fp8 or args.debug, - compute_dbias=(args.fp8 or args.debug) and args.use_bias, + compute_dbias=compute_dbias, disable_bulk_allocation=( args.cpu_offloading and isinstance(grad_output_reference, HybridQuantizer) @@ -675,11 +692,7 @@ def _grouped_linear_backward_impl( ) if args.weights_requires_grad: - if ( - is_dist_weight - and args.wgrad_store is not None - and args.wgrad_store.delay_wgrad_compute() - ): + if is_dist_weight and delay_wgrad: raise RuntimeError( "distributed-weight GroupedLinear requires delay_wgrad_compute=False." ) @@ -733,7 +746,9 @@ def _grouped_linear_backward_impl( layout="NT", grad=True, m_splits=m_splits, - use_bias=args.use_bias if grad_biases[0] is None else None, + # Preserve the high-precision GEMM epilogue and algorithm selection, + # including when delayed wgrad no longer owns bias accumulation. + use_bias=args.use_bias and not (args.fp8 or args.debug), bias=biases, use_split_accumulator=wgrad_use_split_accumulator, accumulate=( @@ -743,14 +758,12 @@ def _grouped_linear_backward_impl( ), ) # WGRAD - if args.wgrad_store is not None and args.wgrad_store.delay_wgrad_compute(): + if delay_wgrad: args.wgrad_store.put([inputmats, grad_output, wgrad_list], grouped_gemm_wgrad) else: _, grad_biases_, _ = grouped_gemm_wgrad(inputmats, grad_output, wgrad_list) - - for i in range(num_gemms): - if grad_biases[i] is None: - grad_biases[i] = grad_biases_[i] + if not compute_dbias: + grad_biases = grad_biases_ del grad_biases_ clear_tensor_data(*inputmats) @@ -765,10 +778,8 @@ def _grouped_linear_backward_impl( else: wgrad_list = [None] * num_gemms - if not args.use_bias or ( - args.wgrad_store is not None and args.wgrad_store.delay_wgrad_compute() and not args.fp8 - ): - grad_biases = [None] * num_gemms + if not args.use_bias: + grad_biases = [None] * args.num_bias_args dgrad_out = None if args.requires_dgrad: @@ -794,6 +805,7 @@ class GroupedLinearGraphSafeBwdArgs: grad_output_quantizers: List[Quantizer] = None is_dist_weight: bool = False num_gemms: int = 0 + num_bias_args: int = 0 in_features: int = 0 out_features: int = 0 activation_dtype: Optional[torch.dtype] = None @@ -846,7 +858,6 @@ def _grouped_linear_graph_safe_forward(args: GroupedLinearFwdArgs) -> Tuple[Any, is_dist_weight = is_distributed_weight(weights[0]) if is_dist_weight: weights = materialize_weight_for_forward(weights) - _grouped_linear_configure_quantizers(args, weights) weight_quantizers = args.weight_quantizers biases = args.biases out = args.out @@ -903,7 +914,9 @@ def _grouped_linear_graph_safe_forward(args: GroupedLinearFwdArgs) -> Tuple[Any, dtype=activation_dtype, ) - columnwise_usage = is_grad_enabled and args.input_requires_grad + columnwise_usage = (is_grad_enabled and args.input_requires_grad) or ( + is_fp8_activation_recompute_enabled() and not in_fp8_activation_recompute_phase() + ) weights_for_gemm, new_workspaces = _GroupedLinear._prepare_weights_for_grouped_tensor_gemm( weights, weight_quantizers, @@ -1005,6 +1018,7 @@ def _grouped_linear_graph_safe_setup( bwd_args.grad_output_quantizers = fwd_args.grad_output_quantizers bwd_args.m_splits_tensor = fwd_args.m_splits_tensor bwd_args.num_gemms = fwd_args.num_gemms + bwd_args.num_bias_args = len(fwd_args.biases) bwd_args.activation_dtype = fwd_args.activation_dtype bwd_args.fp8 = fwd_args.fp8 bwd_args.use_bias = fwd_args.use_bias @@ -1110,7 +1124,9 @@ def _grouped_linear_graph_safe_backward( grad_output_view = grad_output.contiguous().view(-1, grad_output.shape[-1]) dy_2d = cast_if_needed(grad_output_view, args.activation_dtype) dbias_packed = None - if args.fp8: + grouped_dy = None + needs_grouped_dy = args.requires_dgrad or args.weights_requires_grad + if args.fp8 and needs_grouped_dy: grad_output_quantizer = args.grad_output_quantizers[0] grad_output_quantizer.set_usage( rowwise=args.requires_dgrad, @@ -1138,7 +1154,7 @@ def _grouped_linear_graph_safe_backward( split_sizes, tensor_offsets=output_tensor_offsets, ) - else: + elif needs_grouped_dy: grouped_dy = _GroupedLinear._make_grouped_tensor( dy_2d, num_gemms=N, @@ -1156,8 +1172,7 @@ def _grouped_linear_graph_safe_backward( else: grad_bias_args = [dbias_packed[i].to(dtype=args.activation_dtype) for i in range(N)] else: - num_bias_args = 1 if args.single_grouped_bias else N - grad_bias_args = [None] * num_bias_args + grad_bias_args = [None] * args.num_bias_args dgrad = None if args.requires_dgrad: @@ -1265,7 +1280,7 @@ def grouped_gemm_wgrad(inputmats, grad_output_mats, grad_weights): use_split_accumulator=wgrad_use_split_accumulator, accumulate=accumulate, ) - return None, [None] * N, None + return None, (), None if args.wgrad_store is not None and args.wgrad_store.delay_wgrad_compute(): args.wgrad_store.put([grouped_x, grouped_dy, wgrad_output], grouped_gemm_wgrad) @@ -1393,6 +1408,11 @@ def _prepare_weights_for_grouped_tensor_gemm( raise RuntimeError( "Quantized single grouped weights require quantized grouped GEMM compute." ) + # Preserve usages already enabled on the parameter quantizer. + weight.quantizer.set_usage( + rowwise=True, + columnwise=True if columnwise_usage else None, + ) return weight, new_workspaces if not with_quantized_compute: @@ -1472,8 +1492,15 @@ def _prepare_weights_for_grouped_tensor_gemm( update_ws = is_first_microbatch is None or is_first_microbatch for idx, weight in enumerate(weights): - weight_quantizer = weight_quantizers[idx] - weight_quantizer.set_usage(rowwise=True, columnwise=columnwise_usage) + if isinstance(weight, QuantizedTensorStorage): + weight_quantizer = weight._quantizer + weight_quantizer.set_usage( + rowwise=True, + columnwise=True if columnwise_usage else None, + ) + else: + weight_quantizer = weight_quantizers[idx] + weight_quantizer.set_usage(rowwise=True, columnwise=columnwise_usage) weight_fp8, new_workspaces[idx] = quantize_weight( tensor=weight, quantizer=weight_quantizer, @@ -1569,7 +1596,7 @@ def _forward_grouped_tensor(ctx, fwd_args: GroupedLinearFwdArgs) -> Tuple[torch. ctx.inp_shape = fwd_args.inp.shape ctx.backward_objects = bwd_args if fwd_args.fp8 and requires_grad( - fwd_args.inp, fwd_args.weights[0], fwd_args.biases[0] + fwd_args.inp, fwd_args.weights[0], fwd_args.biases[0] if fwd_args.biases else None ): bwd_args.reduce_and_update_bwd_fp8_tensors = ( FP8GlobalStateManager.is_first_fp8_module() @@ -1593,6 +1620,7 @@ def forward( fwd_args.inp = inp num_weight_args = 1 if fwd_args.single_grouped_weight else num_gemms fwd_args.weights = list(weights_and_biases[:num_weight_args]) + # Only bias=False omits bias inputs; returned biases retain their autograd edges. fwd_args.biases = list(weights_and_biases[num_weight_args:]) if fwd_args.use_grouped_tensor_path: @@ -1607,7 +1635,9 @@ def forward( ctx.save_for_backward(*tensors_to_save) ctx.tensor_objects = tensor_objects ctx.backward_objects = bwd_args - if fwd_args.fp8 and requires_grad(inp, fwd_args.weights[0], fwd_args.biases[0]): + if fwd_args.fp8 and requires_grad( + inp, fwd_args.weights[0], fwd_args.biases[0] if fwd_args.biases else None + ): bwd_args.reduce_and_update_bwd_fp8_tensors = ( FP8GlobalStateManager.is_first_fp8_module() ) @@ -1689,7 +1719,8 @@ class GroupedLinear(TransformerEngineBaseModule): the model is trained with lower precision and the original FP32 parameters would not fit in GPU memory. delay_wgrad_compute : bool, default = False - Whether to delay weight gradient computation + Whether to delay weight gradient computation until ``backward_dw()``. + Bias gradients are computed and accumulated during the main backward. save_original_input : bool, default = False If set to ``True``, always saves the original input tensor rather than the cast tensor. In some scenarios, the input tensor is used by multiple modules, @@ -1886,13 +1917,13 @@ def __init__( self.reset_parameters(defer_init=is_meta) if self.wgrad_store.delay_wgrad_compute(): - for name, param in self.named_parameters(): - if name in ("weight", "bias"): - param.skip_backward_post_hook = True - continue + # Only weights are deferred. Biases use normal autograd accumulation hooks. + grouped_weight = getattr(self, "weight", None) + if grouped_weight is not None: + grouped_weight.skip_backward_post_hook = True + else: for i in range(self.num_gemms): - if name in (f"weight{i}", f"bias{i}"): - param.skip_backward_post_hook = True + getattr(self, f"weight{i}").skip_backward_post_hook = True def set_meta_tensor(self, fwd: bool, recipe: Recipe) -> None: """Init scales and amaxes for fwd | bwd.""" @@ -2028,7 +2059,7 @@ def make_grouped_weights(self, defer_init=False) -> None: grouped_weights = GroupedTensor.make_grouped_tensor_with_shapes( num_tensors=self.num_gemms, shapes=[(self.out_features, self.in_features)] * self.num_gemms, - quantizer=weight_quantizers[0], + quantizer=weight_quantizers[0].copy() if weight_quantizers[0] is not None else None, dtype=self.params_dtype, device=weights[0].device, ) @@ -2302,9 +2333,9 @@ def forward( final gradient only when inp has a single consumer in the autograd graph; otherwise autograd accumulates into a new tensor. """ - debug = self.is_debug_iter() is_grad_enabled = torch.is_grad_enabled() num_gemms = self.num_gemms + cpu_offloading = is_cpu_offload_enabled() # Make sure splits are in expected format if (self.single_grouped_weight or self.single_grouped_bias) and not self.use_grouped_tensor: @@ -2324,11 +2355,10 @@ def forward( f"does not match number of GEMMs ({num_gemms})." ) - if FP8GlobalStateManager.fp8_graph_capturing(): - skip_fp8_weight_update = ( - FP8GlobalStateManager.quantization_state.skip_fp8_weight_update_tensor - ) - else: + skip_fp8_weight_update = ( + FP8GlobalStateManager.quantization_state.skip_fp8_weight_update_tensor + ) + if skip_fp8_weight_update is not None and not FP8GlobalStateManager.fp8_graph_capturing(): skip_fp8_weight_update = None if skip_fp8_weight_update is not None: is_first_microbatch = False @@ -2339,16 +2369,22 @@ def forward( inp = self.prepare_forward(inp, num_gemms=self.num_gemms) try: + _recipe = FP8GlobalStateManager.get_fp8_recipe() if self.fp8 else None + weight_tensors = self._get_weight_tensors() - bias_tensors = self._get_bias_tensors() + # Keep real bias inputs even when returned for the caller to apply: they + # preserve the output's autograd dependency when input/weight are frozen. + bias_tensors = self._get_bias_tensors() if self.use_bias else [] use_grouped_bias = self.use_bias and self.single_grouped_bias - quantizers = self._get_quantizers() if not debug else self._get_debug_quantizers() - + # Check whether to enable debug features + debug = self.is_debug_iter() + debug_quantizers = None if debug: - if self.no_debug_features_active(list(chain(*quantizers))): + debug_quantizers = self._get_debug_quantizers() + if self.no_debug_features_active(list(chain(*debug_quantizers))): debug = False - quantizers = self._get_quantizers() + debug_quantizers = None if debug and (self.single_grouped_weight or self.single_grouped_bias): raise RuntimeError( "TE debug features do not support single grouped parameters. DebugQuantizer " @@ -2357,6 +2393,35 @@ def forward( "features for this GroupedLinear." ) + # Choose between grouped tensor path or split-quantize path + use_grouped_tensor_path = False + if self.use_grouped_tensor and not (self.fp8_calibration or debug or cpu_offloading): + use_grouped_tensor_path = is_module_grouped_tensor_path_supported( + _recipe, + self.activation_dtype, + device=inp.device, + ) + if ( + self.use_grouped_tensor + and not use_grouped_tensor_path + and (self.single_grouped_weight or use_grouped_bias) + ): + raise RuntimeError( + "Single grouped parameters require the native grouped-tensor path, but the" + " active device, cuBLASLt version, quantization recipe, or GroupedLinear" + " feature configuration does not support it. Disable single_grouped_weight and" + " single_grouped_bias to allow the split-quantize fallback." + ) + + # Get quantizers + if use_grouped_tensor_path: + quantizers = self._get_quantizers_for_grouped_tensor_path() + elif debug: + if debug_quantizers is None: + debug_quantizers = self._get_debug_quantizers() + quantizers = debug_quantizers + else: + quantizers = self._get_quantizers() ( input_quantizers, weight_quantizers, @@ -2365,6 +2430,7 @@ def forward( grad_weight_quantizers, grad_output_quantizers, ) = quantizers + if not debug and weight_quantizers[0] is not None: # Experts share shape and recipe settings: compute once and broadcast. optimize_for_gemm = self._enable_weight_preswizzle( @@ -2385,10 +2451,8 @@ def forward( weight_requires_grad = weight_tensors[0].requires_grad if self.fp8: - _recipe = FP8GlobalStateManager.get_fp8_recipe() backward_override = _recipe.backward_override else: - _recipe = None backward_override = None save_original_input = self.save_original_input @@ -2425,30 +2489,6 @@ def forward( ) save_original_input = False - cpu_offloading = is_cpu_offload_enabled() - use_grouped_tensor_path = False - if self.use_grouped_tensor and not ( - self.fp8_calibration - or debug - or cpu_offloading - or any(q is not None for q in output_quantizers) - ): - use_grouped_tensor_path = is_module_grouped_tensor_path_supported( - _recipe, - self.activation_dtype, - device=inp.device, - ) - if ( - self.use_grouped_tensor - and not use_grouped_tensor_path - and (self.single_grouped_weight or use_grouped_bias) - ): - raise RuntimeError( - "Single grouped parameters require the native grouped-tensor path, but the" - " active device, cuBLASLt version, quantization recipe, or GroupedLinear" - " feature configuration does not support it. Disable single_grouped_weight and" - " single_grouped_bias to allow the split-quantize fallback." - ) if use_grouped_tensor_path: if m_splits.device.type != te_device_type(): raise ValueError( @@ -2521,6 +2561,9 @@ def forward( self.end_forward() if self.return_bias: + # bias=False still returns the legacy list of empty tensors when requested. + if not self.use_bias: + bias_tensors = self._get_bias_tensors() if use_grouped_bias: return out, bias_tensors[0] return out, [cast_if_needed(b, self.activation_dtype) for b in bias_tensors] @@ -2536,7 +2579,7 @@ def backward_dw(self): if self.wgrad_store.context is None or self.wgrad_store.context.empty(): return with get_nvtx_range_context("_GroupedLinear_wgrad"): - (_, grad_biases_, _), tensor_list = self.wgrad_store.pop() + (_, grad_biases, _), tensor_list = self.wgrad_store.pop() wgrad_output = tensor_list[2] weight_params = self._get_weight_tensors() if not self.fuse_wgrad_accumulation: @@ -2547,23 +2590,21 @@ def backward_dw(self): else: for i in range(self.num_gemms): weight_params[i].grad = wgrad_output[i].to(weight_params[i].dtype) - has_grad_biases = [ - grad_bias is not None and grad_bias.numel() != 0 for grad_bias in grad_biases_ - ] - if self.use_bias and any(has_grad_biases): - if self.use_grouped_tensor: - raise RuntimeError( - "GroupedLinear(use_grouped_tensor=True) fell back to the split-quantize " - "path, which produced per-expert bias gradients during delayed wgrad. " - "This implicit fallback is unsupported with delay_wgrad_compute=True. " - "Use a configuration supported by the grouped-tensor path, or set " - "use_grouped_tensor=False to select the legacy path explicitly." - ) - bias_params = [getattr(self, f"bias{i}") for i in range(self.num_gemms)] - for i in range(self.num_gemms): - if has_grad_biases[i] and bias_params[i].grad is None: - bias_params[i].grad = grad_biases_[i].to(bias_params[i].dtype) - del grad_biases_ + if ( + self.use_grouped_tensor + and self.apply_bias + and any( + grad_bias is not None and grad_bias.numel() != 0 for grad_bias in grad_biases + ) + ): + raise RuntimeError( + "GroupedLinear(use_grouped_tensor=True) fell back to the split-quantize " + "path, which produced per-expert bias gradients during delayed wgrad. " + "This implicit fallback is unsupported with delay_wgrad_compute=True. " + "Use a configuration supported by the grouped-tensor path, or set " + "use_grouped_tensor=False to select the legacy path explicitly." + ) + del grad_biases del wgrad_output del tensor_list self._trigger_wgrad_accumulation_and_reduce_hooks() @@ -2715,3 +2756,36 @@ def _get_debug_quantizers(self): ] for name, qs in zip(names, original_quantizers) ) + + def _get_quantizers_for_grouped_tensor_path(self): + """Get quantizers consumed by grouped tensor path. + + Grouped tensor only uses the first input quantizer and grad + output quantizer. Single grouped weight uses first weight + quantizer, while discrete weights use num_gemm weight + quantizers. The remaining quantizers are unused. + + """ + input_quantizers, grad_output_quantizers = [None], [None] + num_weights = 1 if self.single_grouped_weight else self.num_gemms + weight_quantizers = [None] * num_weights + if self.fp8 or self.primary_weights_in_fp8: + weight_quantizers = [ + self.quantizers["scaling_fwd"][ + self._offsets["weight"] + i * self._num_fp8_tensors_per_gemm["fwd"] + ] + for i in range(num_weights) + ] + for quantizer in weight_quantizers: + quantizer.internal = not self.primary_weights_in_fp8 + if self.fp8: + input_quantizer = self.quantizers["scaling_fwd"][self._offsets["input"]] + input_quantizer.internal = True + input_quantizer.optimize_for_gemm = True + input_quantizers = [input_quantizer] + if torch.is_grad_enabled(): + grad_output_quantizer = self.quantizers["scaling_bwd"][self._offsets["input"]] + grad_output_quantizer.internal = True + grad_output_quantizer.optimize_for_gemm = True + grad_output_quantizers = [grad_output_quantizer] + return input_quantizers, weight_quantizers, (), (), (), grad_output_quantizers diff --git a/transformer_engine/pytorch/ops/basic/grouped_linear.py b/transformer_engine/pytorch/ops/basic/grouped_linear.py index fe5e77fc580..c23900064b1 100644 --- a/transformer_engine/pytorch/ops/basic/grouped_linear.py +++ b/transformer_engine/pytorch/ops/basic/grouped_linear.py @@ -596,7 +596,7 @@ def make_grouped_weights(self) -> None: grouped_weights = GroupedTensor.make_grouped_tensor_with_shapes( num_tensors=self.num_groups, shapes=[(self.out_features, self.in_features)] * self.num_groups, - quantizer=quantizer, + quantizer=quantizer.copy() if quantizer is not None else None, dtype=self.weight0.dtype, device=self.weight0.device, ) @@ -823,14 +823,13 @@ def pre_fuser_forward(self, *, requires_grad: bool) -> None: ) weight_requires_grad = requires_grad and weight_requires_grad - # Configure quantizer usages - for group_idx in range(self.num_groups): - input_quantizer = self.get_quantizer("forward", 2 * group_idx) - weight_quantizer = self.get_quantizer("forward", 2 * group_idx + 1) - grad_output_quantizer = self.get_quantizer("backward", group_idx) - input_quantizer.set_usage(rowwise=True, columnwise=weight_requires_grad) - weight_quantizer.set_usage(rowwise=True, columnwise=requires_grad) - grad_output_quantizer.set_usage(rowwise=True, columnwise=weight_requires_grad) + # Configure the first group's input and grad output + # quantizers since they are exposed externally and may be + # used by other ops for early quantization + input_quantizer = self.get_quantizer("forward", 0) + grad_output_quantizer = self.get_quantizer("backward", 0) + input_quantizer.set_usage(rowwise=True, columnwise=weight_requires_grad) + grad_output_quantizer.set_usage(rowwise=True, columnwise=weight_requires_grad) def reset_recipe_state(self, *, recipe: Optional[Recipe]) -> None: super().reset_recipe_state(recipe=recipe) @@ -922,7 +921,7 @@ def op_backward(self, *args, **kwargs): def _get_grouped_weight_for_gemm( self, weight_param: GroupedTensor, - weight_quantizers: list[Optional[Quantizer]], + weight_quantizers: Sequence[Optional[Quantizer]], columnwise_usage: bool, with_quantized_compute: bool, dtype: torch.dtype, @@ -933,7 +932,9 @@ def _get_grouped_weight_for_gemm( num_groups = self.num_groups is_weight_quantized = weight_param.quantizer is not None if is_weight_quantized and with_quantized_compute: - # GGEMM can use it as it is + weight_param.quantizer.set_usage( + rowwise=True, columnwise=True if columnwise_usage else None + ) return weight_param if is_weight_quantized and not with_quantized_compute: # This use-case isnt optimized yet. Involves a per-group @@ -978,7 +979,7 @@ def _get_grouped_weight_for_gemm( def _get_discrete_weights_for_gemm( self, weight_params: Optional[GroupedTensor] | list[torch.Tensor], - weight_quantizers: list[Optional[Quantizer]], + weight_quantizers: Sequence[Optional[Quantizer]], columnwise_usage: bool, with_quantized_compute: bool, dtype: torch.dtype, @@ -990,9 +991,16 @@ def _get_discrete_weights_for_gemm( for w, quantizer in zip(weight_params, weight_quantizers): if not with_quantized_compute: w = maybe_dequantize(w, dtype) - elif with_quantized_compute and not is_quantized_tensor(w): + else: quantizer.set_usage(rowwise=True, columnwise=columnwise_usage) - w = quantizer(w) + if is_quantized_tensor(w): + # Honor the op's requirements without disabling existing parameter usages. + required_columnwise = True if columnwise_usage else None + w.update_usage(rowwise_usage=True, columnwise_usage=required_columnwise) + if w._quantizer is not None: + w._quantizer.set_usage(rowwise=True, columnwise=required_columnwise) + else: + w = quantizer(w) out.append(w) return out @@ -1076,14 +1084,7 @@ def fuser_forward( input_requires_grad = ctx.requires_grad weight_requires_grad = ctx.requires_grad and weight_param.requires_grad - # Quantizers - input_quantizers = [None] * num_groups - weight_quantizers = [None] * num_groups with_quantized_compute = FP8GlobalStateManager.is_fp8_enabled() - if with_quantized_compute: - for group_idx in range(num_groups): - input_quantizers[group_idx] = self.get_quantizer("forward", 2 * group_idx) - weight_quantizers[group_idx] = self.get_quantizer("forward", 2 * group_idx + 1) # Get autocast dtype if needed dtype = maybe_autocast_dtype(default_dtype=weight_param.dtype) @@ -1126,12 +1127,19 @@ def fuser_forward( ) if use_grouped_tensor_path: + if self.single_grouped_weight: + weight_quantizers = [self.get_quantizer("forward", 1)] + else: + weight_quantizers = [ + self.get_quantizer("forward", 2 * group_idx + 1) + for group_idx in range(num_groups) + ] out, tensors_to_save = self._fuser_forward_grouped_tensor( input_=input_, split_sizes=split_sizes, scales=scales, with_quantized_compute=with_quantized_compute, - input_quantizers=input_quantizers, + input_quantizer=self.get_quantizer("forward", 0), weight_quantizers=weight_quantizers, dtype=dtype, input_requires_grad=input_requires_grad, @@ -1140,6 +1148,12 @@ def fuser_forward( out_buffer=out_buffer, ) else: + input_quantizers = [ + self.get_quantizer("forward", 2 * group_idx) for group_idx in range(num_groups) + ] + weight_quantizers = [ + self.get_quantizer("forward", 2 * group_idx + 1) for group_idx in range(num_groups) + ] out, tensors_to_save = self._fuser_forward_split_quantize( ctx=ctx, input_=input_, @@ -1219,20 +1233,16 @@ def fuser_forward_save_ctx( num_groups = self.num_groups weight_param = self.weight if self.single_grouped_weight else self.weight0 - with_quantized_compute = FP8GlobalStateManager.is_fp8_enabled() - input_quantizers = [None] * num_groups - weight_quantizers = [None] * num_groups - grad_output_quantizers = [None] * num_groups - if with_quantized_compute: - for group_idx in range(num_groups): - input_quantizers[group_idx] = self.get_quantizer("forward", 2 * group_idx) - weight_quantizers[group_idx] = self.get_quantizer("forward", 2 * group_idx + 1) - grad_output_quantizers[group_idx] = self.get_quantizer("backward", group_idx) + if use_grouped_tensor_path: + grad_output_quantizers = [self.get_quantizer("backward", 0)] + else: + grad_output_quantizers = [ + self.get_quantizer("backward", group_idx) for group_idx in range(num_groups) + ] + with_quantized_compute = FP8GlobalStateManager.is_fp8_enabled() ctx.use_grouped_tensor_path = use_grouped_tensor_path ctx.with_quantized_compute = with_quantized_compute - ctx.input_quantizers = input_quantizers - ctx.weight_quantizers = weight_quantizers ctx.grad_output_quantizers = grad_output_quantizers ctx.grad_input_quantizers = None # ``split_sizes``, offset metadata, and related tensors are routed @@ -1256,8 +1266,8 @@ def _fuser_forward_split_quantize( split_sizes: torch.Tensor, scales: Optional[torch.Tensor], with_quantized_compute: bool, - input_quantizers: list[Optional[Quantizer]], - weight_quantizers: list[Optional[Quantizer]], + input_quantizers: Sequence[Optional[Quantizer]], + weight_quantizers: Sequence[Optional[Quantizer]], dtype: torch.dtype, input_requires_grad: bool, weight_requires_grad: bool, @@ -1372,8 +1382,8 @@ def _fuser_forward_grouped_tensor( split_sizes: torch.Tensor, scales: Optional[torch.Tensor], with_quantized_compute: bool, - input_quantizers: list[Optional[Quantizer]], - weight_quantizers: list[Optional[Quantizer]], + input_quantizer: Optional[Quantizer], + weight_quantizers: Sequence[Optional[Quantizer]], dtype: torch.dtype, input_requires_grad: bool, weight_requires_grad: bool, @@ -1397,8 +1407,6 @@ def _fuser_forward_grouped_tensor( original_shape = list(input_.size()) prequantized_input = with_quantized_compute and isinstance(input_, GroupedTensor) if with_quantized_compute: - input_quantizer = input_quantizers[0] - input_quantizer.set_usage(rowwise=True, columnwise=weight_requires_grad) input_quantizer.optimize_for_gemm = True if prequantized_input: # GroupedTensor forbids reshape and is already in the canonical diff --git a/transformer_engine/pytorch/quantized_tensor.py b/transformer_engine/pytorch/quantized_tensor.py index 794d383ff26..1b7b9118a37 100644 --- a/transformer_engine/pytorch/quantized_tensor.py +++ b/transformer_engine/pytorch/quantized_tensor.py @@ -361,28 +361,34 @@ def restore_from_saved( Note: please use `restore_from_func_ctx` instead if you are restoring tensors from a function context to make sure tensor_objects is detached and its memory can be freed """ tensor_objects = [] + offset = 0 for tensor in tensors: if tensor is None or isinstance(tensor, torch.Tensor): - tensor_objects.append(saved_tensors[0]) - saved_tensors = saved_tensors[1:] + tensor_objects.append(saved_tensors[offset]) + offset += 1 elif isinstance(tensor, _SavedTensorRef): # The saved tensor was unpacked (running autograd's version check), but # may have lost its subclass under hooks; use the original object. tensor_objects.append(tensor.weight) - saved_tensors = saved_tensors[1:] + offset += 1 elif isinstance(tensor, _SavedQuantizedTensor): count = len(tensor.inner_names) - inner = dict(zip(tensor.inner_names, saved_tensors[:count])) + inner = dict(zip(tensor.inner_names, saved_tensors[offset : offset + count])) tensor_objects.append( QuantizedTensorStorage.__tensor_unflatten__(inner, tensor.metadata, None, None) ) - saved_tensors = saved_tensors[count:] + offset += count else: + # Storage restorers receive the full remaining list/tuple and own the + # returned remainder. Preserve that protocol for custom implementations. + if offset: + saved_tensors = saved_tensors[offset:] saved_tensors = tensor.restore_from_saved(saved_tensors) + offset = 0 tensor_objects.append(tensor) if return_saved_tensors: - return tensor_objects, saved_tensors + return tensor_objects, saved_tensors[offset:] if offset else saved_tensors return tensor_objects