Grab Attention Maps from Nested PyTorch Modules
A Python decorator that pickpockets local variables from inside nested PyTorch functions, letting you extract attention maps without return-statement gymnastics or hook registration.

What it does Visualizer is a small PyTorch utility that yanks attention maps—or any local variable—out of deeply nested model definitions without making you thread return values through layers or register forward hooks on every block. You decorate the function that builds the map, activate the tracer before importing your model, and the variable lands in a cache after the forward pass.
The interesting bit
Instead of wrestling with module paths like model.blocks.3.attn or polluting your code with global variables, it manipulates Python bytecode to intercept local variables at runtime. The decorator is effectively inert during training—if you don’t activate it before import, it leaves no trace and imposes zero overhead.
Key highlights
- Pulls variables by name from inside functions, regardless of how deeply nested the module hierarchy is.
- Captures all attention maps across Transformer layers in one shot by decorating the shared
forwardmethod once. - Zero training-time footprint: leave the decorator in place; without
activate()it does nothing. - Not limited to attention—any local variable in any function is fair game.
Caveats
- The variable you want must keep its name until the end of the function; if another statement reassigns to the same name, you get the final value, not the intermediate one.
- You must call
get_local.activate()before importing the model, because the decorator binds at import time.
Verdict Worth a look if you’re debugging or visualizing attention in Vision Transformers and tired of hook bookkeeping. Skip it if you already have a clean hook pipeline or don’t need to peek inside specific local variables.
Frequently asked
- What is luo3300612/Visualizer?
- A Python decorator that pickpockets local variables from inside nested PyTorch functions, letting you extract attention maps without return-statement gymnastics or hook registration.
- Is Visualizer open source?
- Yes — luo3300612/Visualizer is open source, released under the Apache-2.0 license.
- What language is Visualizer written in?
- luo3300612/Visualizer is primarily written in Jupyter Notebook.
- How popular is Visualizer?
- luo3300612/Visualizer has 1.3k stars on GitHub.
- Where can I find Visualizer?
- luo3300612/Visualizer is on GitHub at https://github.com/luo3300612/Visualizer.