onnx_ir.journaling¶
Journaling system for ONNX IR operations.
The onnx_ir.journaling module provides a journaling system for tracking and debugging operations performed on ONNX IR objects. This is useful for understanding how a model is transformed, debugging unexpected behavior, or auditing changes made to a model.
Warning
This module is in alpha. The APIs can change.
Quick Start¶
Use the Journal class as a context manager to record operations:
import onnx_ir as ir
from onnx_ir.journaling import Journal
model = ir.load("model.onnx")
with Journal() as journal:
# Perform operations on the model
for node in model.graph:
node.name = f"renamed_{node.name}"
# View all recorded operations
journal.display()
# Or filter by specific criteria
filtered_entries = [
entry for entry in journal.entries
if entry.operation == "set_name" and entry.class_name == "Node"
]
for entry in filtered_entries:
print(f"{entry.operation} on {entry.class_name}")
The Journal class¶
The Journal class is the main interface for recording operations on the ONNX IR. It captures details about each operation including the object involved, a timestamp, and a stack trace for debugging.
- class onnx_ir.journaling.Journal¶
A journaling system to record operations on the ONNX IR.
It can be used as a context manager to automatically record operations within a block.
Example:
from onnx_ir.journaling import Journal journal = Journal() with Journal() as journal: # Perform operations on the ONNX IR pass for entry in journal.entries: print(f"Operation: {entry.operation} on {entry.class_name}")
You can also filter the entries by operation or class name using the filter method:
filtered_entries = [ entry for entry in journal.entries if entry.operation == "set_name" and entry.class_name == "Node" ]
- add_hook(hook)[source]¶
Add a hook that will be called whenever a new journal entry is recorded.
- Parameters:
hook (Callable[[JournalEntry], None])
- Return type:
None
- property entries: Sequence[JournalEntry]¶
Get all recorded journal entries.
The JournalEntry class¶
Each recorded operation is stored as a JournalEntry. This dataclass contains all the information about a single operation.
- class onnx_ir.journaling.JournalEntry(timestamp, operation, class_, class_name, ref, object_id, stack_trace, details)¶
A single journal entry recording an operation on the IR.
- Parameters:
timestamp (float)
operation (str)
class_ (type)
class_name (str)
ref (weakref.ref | None)
object_id (int)
stack_trace (list[traceback.FrameSummary])
details (str | None)
- ref¶
A weak reference to the object on which the operation was performed. To access the object, call
ref(). Note thatrefmay beNone, andref()may returnNoneif the object has been garbage-collected.- Type:
weakref.ref | None
- obj¶
The referenced object, or None if it has been garbage-collected or not recorded. This is the same as calling
entry.ref() if entry.ref is not None else None.
- stack_trace¶
The stack trace at the time of the operation.
- Type:
- property obj: Any | None¶
Get the referenced object, or None if it has been garbage-collected or not recorded.
- ref: weakref.ref | None¶
- stack_trace: list[traceback.FrameSummary]¶
Using Hooks¶
You can add hooks to be notified whenever a new entry is recorded. This is useful for real-time monitoring or custom logging:
from onnx_ir.journaling import Journal, JournalEntry
def my_hook(entry: JournalEntry) -> None:
print(f"Operation recorded: {entry.operation} on {entry.class_name}")
journal = Journal()
journal.add_hook(my_hook)
with journal:
# Operations will trigger the hook as they are recorded
...
Inspecting Entries¶
The JournalEntry.display() method provides a detailed, multi-line view of an entry including:
Timestamp
Operation name
Class and object ID
Object representation
Details (if provided)
User code location (the first stack frame outside of onnx_ir internals)
Full stack trace
for entry in journal.entries:
entry.display() # Detailed multi-line output
The Journal.display() method provides a more compact summary of all entries, suitable for quick overview.