pytorch/pytorch · error · RuntimeError

Unable to get caller frame

Error message

Unable to get caller frame

What it means

After obtaining its own frame, `dims()` steps one level up (`frame.f_back`) to read the caller's bytecode and infer how many Dims to create from the assignment target. If that caller frame is None (the current frame has no parent, e.g. the call was initiated from C code or a synthetic frame), it raises RuntimeError('Unable to get caller frame').

Source

Thrown at functorch/dim/__init__.py:80

        >>> single_dim = dims(1)
    """
    specified_ndims = -1
    found_ndims = 0

    # Parse arguments
    if sizes is not None:
        specified_ndims = len(sizes)
    if n is not None:
        specified_ndims = n

    # Use bytecode inspection
    frame = inspect.currentframe()
    if frame is None:
        raise RuntimeError("Unable to get current frame")
    frame = frame.f_back
    try:
        if frame is None:
            raise RuntimeError("Unable to get caller frame")
        code = frame.f_code
        lasti = frame.f_lasti

        decoder = _PyInstDecoder(code, lasti)

        if sys.version_info >= (3, 11):
            if decoder.opcode() == "PRECALL":
                decoder.next()

        # Move to next instruction after the call
        decoder.next()

        # Determine number of dimensions from bytecode
        if _relevant_op(decoder.opcode()):
            found_ndims = 1
        elif decoder.opcode() == "UNPACK_SEQUENCE":
            found_ndims = decoder.oparg()
            decoder.next()  # Move past UNPACK_SEQUENCE

View on GitHub (pinned to dcd2ecae77)

Solutions

  1. Ensure dims() is called from ordinary Python code (a module or function body), not directly from C or synthetic frames
  2. Wrap the call in a normal Python function so a real caller frame exists
  3. Pass n= or sizes= explicitly so the function does not depend on caller bytecode if the frame exists but is unusual
  4. Avoid dims() inside exec() strings; move the call into a real .py file

Example fix

// before (called with no Python caller frame)
result = eval_in_synthetic_frame('dims(2)')

// after
def make_dims():
    return dims(2)  # real Python caller frame for bytecode inspection
result = make_dims()
Defensive patterns

Strategy: validation

Validate before calling

import inspect
# ensure dims() is only invoked from a normal Python caller frame
frame = inspect.currentframe()
if frame is None or frame.f_back is None:
    raise RuntimeError('no Python caller frame available for dims()')

Prevention

When it happens

Trigger: Calling `dims()` from a context where the Python frame chain stops at the dims() frame: invoked from C/C++ extensions, from exec/eval with a fake globals-only frame, or via tooling that calls Python functions without a real caller frame.

Common situations: Embedding Python in C and calling code that uses dims(), invoking dims() through an RPC/dispatch layer that drops frames, or unusual REPL/exec harnesses.

Related errors


AI-assisted analysis of pytorch/pytorch@dcd2ecae77 (2026-08-14). Data as JSON: /api/errors/4adeacf98232dcad. Report an issue: GitHub.