hpcaitech/Open-Sora · error · ValueError

Passing `context_fn` or `debug` is only supported when use_r

Error message

Passing `context_fn` or `debug` is only supported when use_reentrant=False.

What it means

The reentrant checkpoint implementation cannot support custom context functions or debug flags, so checkpoint() raises when use_reentrant=True is combined with a non-default context_fn or debug. Only the non-reentrant generator-based implementation supports these options.

Source

Thrown at opensora/acceleration/checkpoint.py:238

        warnings.warn(
            "torch.utils.checkpoint: the use_reentrant parameter should be "
            "passed explicitly. In version 2.4 we will raise an exception "
            "if use_reentrant is not passed. use_reentrant=False is "
            "recommended, but if you need to preserve the current default "
            "behavior, you can pass use_reentrant=True. Refer to docs for more "
            "details on the differences between the two variants.",
            stacklevel=2,
        )
        use_reentrant = True

    # Hack to mix *args with **kwargs in a python 2.7-compliant way
    preserve = kwargs.pop("preserve_rng_state", True)
    if kwargs and use_reentrant:
        raise ValueError("Unexpected keyword arguments: " + ",".join(arg for arg in kwargs))

    if use_reentrant:
        if context_fn is not noop_context_fn or debug is not False:
            raise ValueError("Passing `context_fn` or `debug` is only supported when " "use_reentrant=False.")
        return CheckpointFunctionWithOffload.apply(function, preserve, *args)
    else:
        gen = _checkpoint_without_reentrant_generator(
            function, preserve, context_fn, determinism_check, debug, *args, **kwargs
        )
        # Runs pre-forward logic
        next(gen)
        ret = function(*args, **kwargs)
        # Runs post-forward logic
        try:
            next(gen)
        except StopIteration:
            return ret


def set_grad_checkpoint(model, use_fp32_attention=False, gc_step=1):
    assert isinstance(model, nn.Module)

View on GitHub (pinned to 7ad6a96a13)

Solutions

  1. Pass use_reentrant=False to use _checkpoint_without_reentrant_generator which supports context_fn and debug
  2. If you must stay reentrant, remove context_fn and leave debug at its default False

Example fix

# before
checkpoint(fn, x, context_fn=offload_ctx, debug=True)  # use_reentrant=True
# after
checkpoint(fn, x, context_fn=offload_ctx, debug=True, use_reentrant=False)
Defensive patterns

Strategy: validation

Validate before calling

if use_reentrant and (context_fn is not noop_context_fn or debug is not False):
    raise ValueError('context_fn/debug require use_reentrant=False')  # fail fast with a clear message

Prevention

When it happens

Trigger: Calling checkpoint(fn, *args, use_reentrant=True, context_fn=<non-default>) or passing debug=True (anything other than False) with reentrant mode; reached via auto_grad_checkpoint and forward.

Common situations: Trying to use meta-device offload context functions or debug determinism checks while keeping the legacy reentrant autograd Function; mixing options from different PyTorch checkpoint API generations.

Related errors


AI-assisted analysis of hpcaitech/Open-Sora@7ad6a96a13 (2026-08-28). Data as JSON: /api/errors/37e4dc7cad7b2663. Report an issue: GitHub.