Skip to content

Commit

Permalink
implement framelocals mapping as c++ object (#140063)
Browse files Browse the repository at this point in the history
Summary:
Implements pytorch/pytorch#93753 - move frame local guard accessors to C++.

Before, we used dict accessors on a Python dict representing the frame's fastlocals that we manually build. We move this accessor to C++ and additionally use the fastlocal index whenever possible.

Some implementation notes:
- `FrameLocalsMapping` is now initialized as a C++ vector of `PyObject`s. We do not just use the frame's localsplus/fastlocals buffer because we also unbox cells.
- `FrameLocalsMapping` can still be converted into a Python dict representing the frame's fastlocals, but it is done lazily.
- We update `LeafGuard`, `GuardAccessor`, and `GuardManager`'s `check_nopybind` methods to accept `FrameLocalsMapping`. By default, we convert the `FrameLocalsMapping` to a Python dict and run the original `check_nopybind` on it, but in some cases, conversion is not needed.
- We add a new guard accessor `FrameLocalsGuardAccessor`, which is similar to `DictGetItemGuardAccessor` but has special handling for `FrameLocalsMapping`. We create a separate class to emphasize different use cases, but we could probably combine these two (can do in a follow up)

dynamo_guard_eval.py microbenchmark update:
- 713.2us -> 630.0us (3.10)
- 598.8us -> 530.7us (3.12)

Other followups:
- Add `FrameLocalsMapping` version for `check_verbose_nopybind` in order to match behavior between `check_nopybind` and `check_verbose_nopybind`. This can prevent difficult debugging situations where guards fail (`check_nopybind` returns false) but no guard error message is generated (`check_verbose_nopybind` succeeds).
- Rewrite the `SHAPE_ENV` guard into C++ - it is a fairly common guard that results in `FrameLocalsMapping` needing to convert to a dict

X-link: pytorch/pytorch#140063
Approved by: https://github.com/jansel
ghstack dependencies: #142117, #142430

Reviewed By: huydhn

Differential Revision: D67355697

Pulled By: williamwen42

fbshipit-source-id: 930855f49db629b2bc2d7b7ff14c065bd044e894
  • Loading branch information
williamwen42 authored and facebook-github-bot committed Dec 18, 2024
1 parent 2db4da5 commit 49cb63b
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion userbenchmark/dynamo/dynamobench/_dynamo/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def insert_nops(instructions: List[Any], code_options: Any) -> None:
torch_function_mode_stack=[],
)

return GuardedCode(code, CheckFunctionManager(graph).guard_manager, CompileId(0, 0)) # type: ignore[arg-type]
return GuardedCode(code, CheckFunctionManager(frame.f_code, graph).guard_manager, CompileId(0, 0)) # type: ignore[arg-type]


class CompileCounter:
Expand Down

0 comments on commit 49cb63b

Please sign in to comment.