This repository was archived by the owner on Sep 30, 2026. It is now read-only.
Repository navigation
Make TypedMetadata a visitor interface instead - #1448
Closed
ryanzhang22 wants to merge 1 commit into
Closed
ryanzhang22 wants to merge 1 commit into
ryanzhang22 wants to merge 1 commit into
Conversation
Summary:
I was implementing JSON serialization from TypedMetadata to make progress towards our target of being able to add a new `MetadataField` to Kineto and have it reflect in all of our outputs. However when I benchmarked the results, I found that the JSON took ~1.5x longer to write that before, and the primary contributor to this was building the `TypedMetadata` struct on every `metadataJson()` call.
Realistically there's no reason why we need to build a new struct every time we call `typedMetadata()`, and to be honest we probably don't even need to materialize it ever. One option is to make `typedMetadata` a visitor method instead.
===============
The general idea is:
On `ITraceActivity.h`, replacing `typedMetadata()`
```
virtual void visitTypedMetadata(ITypedMetadataVisitor& visitor)
```
`ITypedMetadataVisitor` is an interface containing `visitValue` methods.
```
class {
public:
virtual ~ITypedMetadataVisitor() = default;
template <typename T, typename V>
void visit(const MetadataField<T>& field, V&& value) {
using FieldType = typename MetadataField<T>::FieldType;
static_assert(std::is_same_v<FieldType, std::decay_t<V>>, "value type must match field's declared type");
visitValue(field, value);
}
protected:
// Overridable fallback for logging if someone introduces a new type
virtual void visitUnsupported(std::string_view /*name*/) {}
template <typename T>
void visitValue(const MetadataField<T>& field, const T& /*value*/) {
visitUnsupported(field.name);
}
void visitValue(const MetadataField<std::string>& field, const std::string& value) {
visitValue(field, std::string_view{value});
}
virtual void visitValue(const MetadataField<int64_t>& field, int64_t value) = 0;
// For each supported type
...
};
```
For each output type (JSON, Protobuf, .events), they implement a new `ITypedMetadataVisitor` class which handles their specific case. For JSON, this means that each `visitValue` would handle serialization for that type to a JSON string and appending it to the `metadataJson` output.
`visitTypedMetadata` is implemented by calling .visit on each `MetadataField` + Value pair.
```
inline void CudaEventActivity::visitTypedMetadata(ITypedMetadataVisitor& visitor) const {
const CUpti_ActivityCudaEventType& event = raw();
visitor.visit(CudaMetadataFields::kEventId, static_cast<int64_t>(event.eventId));
visitor.visit(CudaMetadataFields::kStream, static_cast<int64_t>(streamIdForTrace(event.streamId)));
visitor.visit(CudaMetadataFields::kCorrelation, static_cast<int64_t>(event.correlationId));
visitor.visit(CudaMetadataFields::kDevice, deviceId());
visitor.visit(CudaMetadataFields::kContext, static_cast<int64_t>(event.contextId));
}
```
So there is no longer any materialization.
Reviewed By: scotts
Differential Revision: D109168373
|
@ryanzhang22 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D109168373. |
|
This pull request has been merged in 594e334. |
pytorchmergebot
pushed a commit
to pytorch/pytorch
that referenced
this pull request
Jun 30, 2026
Includes the following commits: - Serialize CUDA/ROCm JSON directly from typed metadata (pytorch/kineto#1455) ab3d5e5 - Address missing OSS license headers (pytorch/kineto#1456) 6662873 - Remove support for old versions of PTI. Rework of PTI error handling. (pytorch/kineto#1405) d5d5c01 - Add MetadataDict for nested metadata fields (pytorch/kineto#1454) 510fde2 - Update XPU test env config (pytorch/kineto#1450) 5d8f827 - Deselect XPU-failing TestProfilerDevice CPU tests (pytorch/kineto#1451) eb66185 - Escape LOG messages in output_json.cpp (pytorch/kineto#1453) 9f6b3f7 - Daily `arc lint --take CLANGFORMAT` (pytorch/kineto#1452) c4eea6b - Add a visitor class for JSON serialization (pytorch/kineto#1449) b12cc99 - Enable OVERHEAD activity (pytorch/kineto#1441) 3980198 - Make TypedMetadata a visitor interface instead (pytorch/kineto#1448) 594e334 - Avoid CI OOMs by limiting parallel compile jobs (pytorch/kineto#1447) d5f7009 - Adds TypedMetadata.h to get_libkineto_public_headers (pytorch/kineto#1439) 774d10c - FIx build error by removing unused param (pytorch/kineto#1445) 8341a9e - Prefix internal controller sync/async methods (pytorch/kineto#1438) b1a1f1e - Expose ActivityLoggerFactory via public API (pytorch/kineto#1414) 8279dc0 - Guard GenericActivityProfiler teardown against null traceBuffers_ (pytorch/kineto#1443) b7a7b0e - Finalize pending trace on async profiler teardown (pytorch/kineto#1442) b0ddf1b Authored-with: Claude (AI assistant) Pull Request resolved: #188550 Approved by: https://github.com/scotts
vishalgoyal316
pushed a commit
to vishalgoyal316/pytorch
that referenced
this pull request
Jul 16, 2026
Includes the following commits: - Serialize CUDA/ROCm JSON directly from typed metadata (pytorch/kineto#1455) ab3d5e5 - Address missing OSS license headers (pytorch/kineto#1456) 6662873 - Remove support for old versions of PTI. Rework of PTI error handling. (pytorch/kineto#1405) d5d5c01 - Add MetadataDict for nested metadata fields (pytorch/kineto#1454) 510fde2 - Update XPU test env config (pytorch/kineto#1450) 5d8f827 - Deselect XPU-failing TestProfilerDevice CPU tests (pytorch/kineto#1451) eb66185 - Escape LOG messages in output_json.cpp (pytorch/kineto#1453) 9f6b3f7 - Daily `arc lint --take CLANGFORMAT` (pytorch/kineto#1452) c4eea6b - Add a visitor class for JSON serialization (pytorch/kineto#1449) b12cc99 - Enable OVERHEAD activity (pytorch/kineto#1441) 3980198 - Make TypedMetadata a visitor interface instead (pytorch/kineto#1448) 594e334 - Avoid CI OOMs by limiting parallel compile jobs (pytorch/kineto#1447) d5f7009 - Adds TypedMetadata.h to get_libkineto_public_headers (pytorch/kineto#1439) 774d10c - FIx build error by removing unused param (pytorch/kineto#1445) 8341a9e - Prefix internal controller sync/async methods (pytorch/kineto#1438) b1a1f1e - Expose ActivityLoggerFactory via public API (pytorch/kineto#1414) 8279dc0 - Guard GenericActivityProfiler teardown against null traceBuffers_ (pytorch/kineto#1443) b7a7b0e - Finalize pending trace on async profiler teardown (pytorch/kineto#1442) b0ddf1b Authored-with: Claude (AI assistant) Pull Request resolved: pytorch#188550 Approved by: https://github.com/scotts
aws-kingrj
pushed a commit
to amazon-contributing/upstream-to-pytorch
that referenced
this pull request
Jul 29, 2026
Includes the following commits: - Serialize CUDA/ROCm JSON directly from typed metadata (pytorch/kineto#1455) ab3d5e5 - Address missing OSS license headers (pytorch/kineto#1456) 6662873 - Remove support for old versions of PTI. Rework of PTI error handling. (pytorch/kineto#1405) d5d5c01 - Add MetadataDict for nested metadata fields (pytorch/kineto#1454) 510fde2 - Update XPU test env config (pytorch/kineto#1450) 5d8f827 - Deselect XPU-failing TestProfilerDevice CPU tests (pytorch/kineto#1451) eb66185 - Escape LOG messages in output_json.cpp (pytorch/kineto#1453) 9f6b3f7 - Daily `arc lint --take CLANGFORMAT` (pytorch/kineto#1452) c4eea6b - Add a visitor class for JSON serialization (pytorch/kineto#1449) b12cc99 - Enable OVERHEAD activity (pytorch/kineto#1441) 3980198 - Make TypedMetadata a visitor interface instead (pytorch/kineto#1448) 594e334 - Avoid CI OOMs by limiting parallel compile jobs (pytorch/kineto#1447) d5f7009 - Adds TypedMetadata.h to get_libkineto_public_headers (pytorch/kineto#1439) 774d10c - FIx build error by removing unused param (pytorch/kineto#1445) 8341a9e - Prefix internal controller sync/async methods (pytorch/kineto#1438) b1a1f1e - Expose ActivityLoggerFactory via public API (pytorch/kineto#1414) 8279dc0 - Guard GenericActivityProfiler teardown against null traceBuffers_ (pytorch/kineto#1443) b7a7b0e - Finalize pending trace on async profiler teardown (pytorch/kineto#1442) b0ddf1b Authored-with: Claude (AI assistant) Pull Request resolved: pytorch#188550 Approved by: https://github.com/scotts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
I was implementing JSON serialization from TypedMetadata to make progress towards our target of being able to add a new
MetadataFieldto Kineto and have it reflect in all of our outputs. However when I benchmarked the results, I found that the JSON took ~1.5x longer to write that before, and the primary contributor to this was building theTypedMetadatastruct on everymetadataJson()call.Realistically there's no reason why we need to build a new struct every time we call
typedMetadata(), and to be honest we probably don't even need to materialize it ever. One option is to maketypedMetadataa visitor method instead.===============
The general idea is:
On
ITraceActivity.h, replacingtypedMetadata()ITypedMetadataVisitoris an interface containingvisitValuemethods.For each output type (JSON, Protobuf, .events), they implement a new
ITypedMetadataVisitorclass which handles their specific case. For JSON, this means that eachvisitValuewould handle serialization for that type to a JSON string and appending it to themetadataJsonoutput.visitTypedMetadatais implemented by calling .visit on eachMetadataField+ Value pair.So there is no longer any materialization.
Reviewed By: scotts
Differential Revision: D109168373