pytorch - 💡(How to fix) Fix [functorch] popDynamicLayerStackToDepth double-pops the dynamic layer stack [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#177581Fetched 2026-04-08 00:47:24
View on GitHub
Comments
1
Participants
2
Timeline
164
Reactions
0
Timeline (top)
mentioned ×72subscribed ×72labeled ×9referenced ×4

Error Message

This means when Dynamo's error recovery fires for compiled functorch transforms, the cleanup itself crashes, masking the original error with a cryptic internal assertion failure. _vmap_increment_nesting(3, "error")

Code Example

import torch
  from torch._C._functorch import (
      _grad_increment_nesting,
      _vmap_increment_nesting,
      get_dynamic_layer_stack_depth,
      pop_dynamic_layer_stack_and_undo_to_depth,
  )

  _grad_increment_nesting()
  _vmap_increment_nesting(3, "error")
  assert get_dynamic_layer_stack_depth() == 2

  pop_dynamic_layer_stack_and_undo_to_depth(0)  # should unwind cleanly
RAW_BUFFERClick to expand / collapse

Bug

popDynamicLayerStackToDepth pops two layers per iteration instead of one, causing assertion failures or silent stack corruption. This function runs in Dynamo's finally block after frame evaluation to restore the dynamic layer stack when functorch transforms weren't fully unwound (e.g., due to exceptions or graph breaks inside compiled vmap/grad/jvp).

This means when Dynamo's error recovery fires for compiled functorch transforms, the cleanup itself crashes, masking the original error with a cryptic internal assertion failure.

Reproducer

  import torch
  from torch._C._functorch import (
      _grad_increment_nesting,
      _vmap_increment_nesting,
      get_dynamic_layer_stack_depth,
      pop_dynamic_layer_stack_and_undo_to_depth,
  )

  _grad_increment_nesting()
  _vmap_increment_nesting(3, "error")
  assert get_dynamic_layer_stack_depth() == 2

  pop_dynamic_layer_stack_and_undo_to_depth(0)  # should unwind cleanly

Expected: Stack unwinds to depth 0.

Actual: RuntimeError: layer.key() == TransformType::Vmap INTERNAL ASSERT FAILED at "torch/csrc/functorch/init.cpp":288, please report a bug to PyTorch.

cc @chauhang @penguinwu @Chillee @samdow @kshitij12345 @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @amjames @Lucaskabela @jataylo

extent analysis

Fix Plan

The fix involves modifying the popDynamicLayerStackToDepth function to correctly pop one layer per iteration.

Step-by-Step Solution

  • Modify the popDynamicLayerStackToDepth function to use a while loop that checks the current depth and pops layers until it reaches the target depth.
  • Ensure that the loop correctly handles the case where the target depth is greater than the current depth.

Example code:

def pop_dynamic_layer_stack_to_depth(target_depth):
    while get_dynamic_layer_stack_depth() > target_depth:
        # Pop one layer
        pop_dynamic_layer_stack_and_undo_to_depth(get_dynamic_layer_stack_depth() - 1)
  • Replace the existing pop_dynamic_layer_stack_to_depth function with the modified version.

Verification

  • Run the reproducer code to verify that the stack unwinds correctly to the target depth.
  • Test the function with different target depths to ensure it works as expected.

Extra Tips

  • Make sure to test the modified function thoroughly to catch any edge cases.
  • Consider adding additional logging or error handling to help diagnose any issues that may arise.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - 💡(How to fix) Fix [functorch] popDynamicLayerStackToDepth double-pops the dynamic layer stack [1 comments, 2 participants]