Skip to content

Commit 5f92898

Browse files
physicsrobclaude
andcommitted
Add annotated() decorator form of annotate()
`annotated("label")` wraps a whole function so every node it creates is tagged with the label, equivalent to wrapping the body in `with annotate(label):` but without re-indenting. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 6ef7a46 commit 5f92898

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

torchwright/graph/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .node import Node, annotate
1+
from .node import Node, annotate, annotated
22
from .value_type import NodeValueType, Range
33
from .attn import Attn
44
from .embedding import Embedding

torchwright/graph/node.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
import os
23
from contextlib import contextmanager
34
from contextvars import ContextVar
@@ -58,6 +59,31 @@ def annotate(label: str):
5859
_current_annotation.reset(token)
5960

6061

62+
def annotated(label: str):
63+
"""Decorator form of :func:`annotate`.
64+
65+
Tags every node created during the wrapped function's execution with
66+
``label`` (hierarchical, same nesting rules as :func:`annotate`)::
67+
68+
@annotated("wall projection")
69+
def project_segs(...):
70+
...
71+
72+
Equivalent to wrapping the whole body in ``with annotate(label):`` but
73+
without re-indenting it.
74+
"""
75+
76+
def deco(fn):
77+
@functools.wraps(fn)
78+
def wrapper(*args, **kwargs):
79+
with annotate(label):
80+
return fn(*args, **kwargs)
81+
82+
return wrapper
83+
84+
return deco
85+
86+
6187
class Node:
6288
"""Base class for all computation graph nodes.
6389

0 commit comments

Comments
 (0)