{"record":{"id":"37e4dc7cad7b2663","repo":"hpcaitech/Open-Sora","slug":"passing-context-fn-or-debug-is-only-supported","errorCode":null,"errorMessage":"Passing `context_fn` or `debug` is only supported when use_reentrant=False.","messagePattern":"Passing `context_fn` or `debug` is only supported when use_reentrant=False\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"opensora/acceleration/checkpoint.py","lineNumber":238,"sourceCode":"        warnings.warn(\n            \"torch.utils.checkpoint: the use_reentrant parameter should be \"\n            \"passed explicitly. In version 2.4 we will raise an exception \"\n            \"if use_reentrant is not passed. use_reentrant=False is \"\n            \"recommended, but if you need to preserve the current default \"\n            \"behavior, you can pass use_reentrant=True. Refer to docs for more \"\n            \"details on the differences between the two variants.\",\n            stacklevel=2,\n        )\n        use_reentrant = True\n\n    # Hack to mix *args with **kwargs in a python 2.7-compliant way\n    preserve = kwargs.pop(\"preserve_rng_state\", True)\n    if kwargs and use_reentrant:\n        raise ValueError(\"Unexpected keyword arguments: \" + \",\".join(arg for arg in kwargs))\n\n    if use_reentrant:\n        if context_fn is not noop_context_fn or debug is not False:\n            raise ValueError(\"Passing `context_fn` or `debug` is only supported when \" \"use_reentrant=False.\")\n        return CheckpointFunctionWithOffload.apply(function, preserve, *args)\n    else:\n        gen = _checkpoint_without_reentrant_generator(\n            function, preserve, context_fn, determinism_check, debug, *args, **kwargs\n        )\n        # Runs pre-forward logic\n        next(gen)\n        ret = function(*args, **kwargs)\n        # Runs post-forward logic\n        try:\n            next(gen)\n        except StopIteration:\n            return ret\n\n\ndef set_grad_checkpoint(model, use_fp32_attention=False, gc_step=1):\n    assert isinstance(model, nn.Module)\n","sourceCodeStart":220,"sourceCodeEnd":256,"githubUrl":"https://github.com/hpcaitech/Open-Sora/blob/7ad6a96a135feb81f755c84fb391818718f6beb2/opensora/acceleration/checkpoint.py#L220-L256","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass use_reentrant=False to use _checkpoint_without_reentrant_generator which supports context_fn and debug","If you must stay reentrant, remove context_fn and leave debug at its default False"],"exampleFix":"# before\ncheckpoint(fn, x, context_fn=offload_ctx, debug=True)  # use_reentrant=True\n# after\ncheckpoint(fn, x, context_fn=offload_ctx, debug=True, use_reentrant=False)","handlingStrategy":"validation","validationCode":"if use_reentrant and (context_fn is not noop_context_fn or debug is not False):\n    raise ValueError('context_fn/debug require use_reentrant=False')  # fail fast with a clear message","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default new code to use_reentrant=False","Add a config sanity check that rejects context_fn/debug whenever reentrant mode is selected"],"tags":["checkpointing","pytorch","configuration","kwargs"],"backgroundTag":"invalid-function-arguments","analyzedSha":"7ad6a96a135feb81f755c84fb391818718f6beb2","analyzedAt":"2026-08-28T16:58:37.171Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}