Megatron-LM icon indicating copy to clipboard operation
Megatron-LM copied to clipboard

Using MTP + packing + full recompute causing exception in save_for_backward

Open arvyanh opened this issue 9 months ago • 3 comments

Describe the bug in multi_token_prediction.py: Packed_seq_param is passed in for roll action, and was then passed into _checkpointed_forward function

Image

Which caused following Issue:

/usr/local/lib/python3.12/dist-packages/Megatron-LM/megatron/core/tensor_parallel/random.py", line 480, in checkpoint
    return CheckpointFunction.apply(function, distribute_saved_activations, *args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/autograd/function.py", line 576, in apply
    return super().apply(*args, **kwargs)  # type: ignore[misc]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: save_for_backward can only save variables, but argument 10 is of type PackedSeqParams

Steps/Code to reproduce bug

Please list minimal steps or code snippet for us to be able to reproduce the bug.

A helpful guide on on how to craft a minimal bug report http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports.

Expected behavior

PackedSeqParams should not be passed into save_for_backward()

arvyanh avatar Feb 28 '26 10:02 arvyanh

I meet the same problem

zwc163 avatar Mar 20 '26 11:03 zwc163

Hello @arvyanh and @zwc163, thanks for reporting the MTP + packed sequence + full recompute failure.

The root of the error looks clear from the traceback: PackedSeqParams is being passed through the checkpoint wrapper, and CheckpointFunction.save_for_backward() can only save tensors. So this is not just a generic recompute failure; it is specifically a non-tensor metadata object crossing the tensor-only activation checkpoint boundary in the MTP packed-sequence path.

This is already assigned to @ericharper, so I am leaving that ownership in place. The remaining fix should probably make PackedSeqParams available to the recomputed function without passing it as a saved tensor argument, or otherwise special-case non-tensor checkpoint metadata in that path.

Connor-XY avatar Jun 05 '26 18:06 Connor-XY

Fixed by https://github.com/NVIDIA/Megatron-LM/pull/4593

ericharper avatar Jun 05 '26 18:06 ericharper