Add compile_id: Optional[CompileID] to torch._logging._internal.trace_structured_artifact (#160440)

Context:
When writing a custom `torch.compile` backend, I quite frequently (ab)use `trace_structured_artifact` because I'm too lazy to customize tlparse (ref: 6d8b13c867).

I recently notice some of the artifacts I want to store are generated where CompileID cannot be correlated and `tlparse` html says
> Sometimes, logs are made without a compile id. This makes it difficult to correlate related logs. This stack trie shows all places where log entries occurred without compile context; to fix, look an appropriate place in the stack where compile id should have been specified.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/160440
Approved by: https://github.com/ezyang
This commit is contained in:
Masaki Kozuki 2025-08-13 06:28:19 +00:00 committed by PyTorch MergeBot
parent 199e9abb6a
commit e497620260

View File

@ -1289,6 +1289,7 @@ def trace_structured_artifact(
name: str, # this will go in metadata name: str, # this will go in metadata
encoding: str, encoding: str,
payload_fn: Callable[[], Optional[Union[str, object]]] = lambda: None, payload_fn: Callable[[], Optional[Union[str, object]]] = lambda: None,
compile_id: Optional[CompileId] = None,
) -> None: ) -> None:
trace_structured( trace_structured(
"artifact", "artifact",
@ -1297,6 +1298,7 @@ def trace_structured_artifact(
"encoding": encoding, "encoding": encoding,
}, },
payload_fn=payload_fn, payload_fn=payload_fn,
compile_id=compile_id,
) )