| name | dftracer-annotate-python |
| description | Python annotation rules for dftracer — decorator usage, initialize/finalize, comp types, class methods, and quick checklist |
Python Annotation Rules (dftracer)
Python Rule 1 — Use the decorator for regular functions
from dftracer.logger import dftracer_fn, dft_fn
@dftracer_fn(cat="IO")
def my_read(path: str, size: int) -> bytes:
...
- The
@dftracer_fn decorator wraps the function with START/END automatically.
cat (category) is required — use a meaningful group name: "IO", "Compute",
"MPI", "Data", "Init", etc.
dft_fn is an alias for dftracer_fn — prefer dftracer_fn for clarity.
- Apply at every function that qualifies under Rule 0 (see dftracer-annotate-general skill).
Python Rule 2 — Initialize and finalize the tracer
from dftracer.logger import DFTracer
tracer = DFTracer.initialize_log(
log_file="traces/my_app",
data_dir="/data",
process_id=rank,
)
DFTracer.finalize_log()
- With mpi4py: call
DFTracer.initialize_log AFTER MPI.Init().
- With mpi4py: call
DFTracer.finalize_log() BEFORE MPI.Finalize().
- Use
process_id=NULL for single-process runs.
Python Rule 3 — Environment-variable initialization (alternative)
export DFTRACER_ENABLE=1
export DFTRACER_LOG_FILE=/tmp/traces/my_app
export DFTRACER_DATA_DIR=/data
python my_app.py
The env var only handles INIT/FINI — decorators are still needed for function-level spans.
Python Rule 4 — Context manager for ad-hoc regions
from dftracer.logger import DFTracer
tracer = DFTracer.get_instance()
with tracer.get_time("IO", "read_loop"):
for chunk in data:
process(chunk)
Only use context managers when the code block is too coarse-grained to fit a function decorator.
Python Rule 5 — Classify every annotated function with comp=TYPE
from dftracer.logger import dftracer_fn
@dftracer_fn(cat="IO", comp="io")
def read_checkpoint(path: str) -> dict: ...
@dftracer_fn(cat="COMM", comp="comm")
def broadcast_weights(tensor, comm): ...
@dftracer_fn(cat="CPU", comp="cpu")
def compute_checksum(data: bytes) -> str: ...
@dftracer_fn(cat="MEM", comp="mem")
def copy_batch(src, dst) -> None: ...
Types: "io", "mem", "cpu", "comm" — same taxonomy as C.
If dftracer_fn doesn't accept comp as a keyword argument:
@dftracer_fn(cat="IO")
def read_checkpoint(path: str) -> dict:
DFTracer.get_instance().update("comp", "io")
...
Python Rule 6 — Skip trivial functions (Rule 0 applies)
@dftracer_fn(cat="IO")
def read_checkpoint(path: str, rank: int) -> dict:
with open(path, "rb") as f:
return pickle.load(f)
@property
def rank(self) -> int:
return self._rank
def _fmt_path(self, p: str) -> str:
return str(Path(p).resolve())
Python Rule 7 — Class methods
class DataLoader:
@dftracer_fn(cat="IO")
def load(self, path: str, batch_size: int) -> list:
...
@dftracer_fn(cat="IO")
def write(self, path: str, data: bytes) -> None:
...
def _validate(self, x):
return x is not None
Python Quick checklist