MCPcopy Create free account
hub / github.com/apple/axlearn / annotate_stack

Function annotate_stack

axlearn/common/traceback_util.py:267–298  ·  view source on GitHub ↗

A decorator that creates a wrapper around `fn` that annotates the call with annotations given by the supplied keyword arguments. These annotations can be retrieved from a traceback using `walk_annotated_tb()`. Example: ``` fn = lambda x: x annotated_fn = annotat

(**aux)

Source from the content-addressed store, hash-verified

265# function, there is no way of getting the function object itself that correctly handles anonymous
266# functions. Otherwise, we could just set an attribute on the function.
267def annotate_stack(**aux) -> Callable:
268 """A decorator that creates a wrapper around `fn` that annotates the call with annotations given
269 by the supplied keyword arguments.
270
271 These annotations can be retrieved from a traceback using `walk_annotated_tb()`.
272
273 Example:
274 ```
275 fn = lambda x: x
276 annotated_fn = annotate_stack(my_annotation="Hello, world!")(fn)
277 ```
278
279 Args:
280 aux: The auxiliary data with which to annotate calls to the wrapped function.
281
282 Returns:
283 A wrapped function that calls `fn` after annotating the call with `aux`.
284 """
285
286 if not is_stack_summary_enabled():
287 return lambda fn: fn
288
289 def decorator(fn: Callable) -> Callable:
290 @functools.wraps(fn)
291 def stack_annotation_wrapper(*args, **kwargs):
292 # Retrieved via `frame.f_locals["aux_"]` by `_walk_stack` below.
293 aux_ = aux # noqa: F841
294 return fn(*args, **kwargs)
295
296 return stack_annotation_wrapper
297
298 return decorator
299
300
301def _is_annotation_frame(frame: types.FrameType) -> bool:

Callers 6

_call_method_in_contextFunction · 0.90
test_annotate_stackMethod · 0.90
no_stack_summaryFunction · 0.85
_is_annotation_frameFunction · 0.85

Calls 1

is_stack_summary_enabledFunction · 0.85

Tested by 3

test_annotate_stackMethod · 0.72