Skip to content
This repository was archived by the owner on Sep 30, 2026. It is now read-only.

refactor JSON chrome trace writing - #1342

Closed
scotts wants to merge 1 commit into
pytorch:mainfrom
scotts:export-D98935521
Closed

scotts wants to merge 1 commit into
pytorch:mainfrom
scotts:export-D98935521

Conversation

@scotts

@scotts scotts commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

Summary:
Refactors JSON writing in ChrometraceLogger. The general principles here:

  1. Centralize writing JSON string as much as possible. We define families of write*Event() functions and then call those when we need to write an actual event. There are still some one-off JSON string writing, but the structured events are centralized in helper functions.
  2. Call out, through comments, some of the structure that gets buried in the strings. That is, it's easy to miss an opening [ or a trailing , which are important for the structure.
  3. Make the logic that can't be centralized as boring as possible.

There's still probably more refactoring we can do. Note that it's not possible to truly centralize everything as we actually store a lot of metadata as JSON in our data structures. Trace down getMetadataValue() and metadataJson() to see more on that.

Regarding performance, when running our benchmark at benchmarks/json_output_benchmarks.cpp, we're basically the same:

==== small scenario, 3 runs
main:    1.27 MB/s, 2.59 MB/s, 2.44 MB/s
this PR: 1.98 MB/s, 2.50 MB/s, 2.41 MB/s

==== medium scenario, 3 runs
main:    76.90 MB/s, 79.64 MB/s, 87.58 MB/s
this PR: 72.13 MB/s, 78.03 MB/s, 65.89 MB/s

==== large scenario, 3 runs
main:    88.62 MB/s, 90.54 MB/s, 94.43 MB/s
this PR: 94.63 MB/s, 89.44 MB/s, 91.18 MB/s

That difference seems to be within the noise.

Differential Revision: D98935521

Summary:
Refactors JSON writing in `ChrometraceLogger`. The general principles here:

1. Centralize writing JSON string as much as possible. We define families of `write*Event()` functions and then call those when we need to write an actual event. There are still some one-off JSON string writing, but the structured events are centralized in helper functions.
2. Call out, through comments, some of the structure that gets buried in the strings. That is, it's easy to miss an opening `[` or a trailing `,` which are important for the structure.
3. Make the logic that can't be centralized as boring as possible.

There's still probably more refactoring we can do. Note that it's not possible to truly centralize everything as we actually store a lot of metadata as JSON in our data structures. Trace down `getMetadataValue()` and `metadataJson()` to see more on that.

Regarding performance, when running our benchmark at `benchmarks/json_output_benchmarks.cpp`, we're basically the same:
```
==== small scenario, 3 runs
main:    1.27 MB/s, 2.59 MB/s, 2.44 MB/s
this PR: 1.98 MB/s, 2.50 MB/s, 2.41 MB/s

==== medium scenario, 3 runs
main:    76.90 MB/s, 79.64 MB/s, 87.58 MB/s
this PR: 72.13 MB/s, 78.03 MB/s, 65.89 MB/s

==== large scenario, 3 runs
main:    88.62 MB/s, 90.54 MB/s, 94.43 MB/s
this PR: 94.63 MB/s, 89.44 MB/s, 91.18 MB/s
```
That difference seems to be within the noise.

Differential Revision: D98935521
@meta-cla meta-cla Bot added the cla signed label Apr 2, 2026
@meta-codesync

meta-codesync Bot commented Apr 2, 2026

Copy link
Copy Markdown

@scotts has exported this pull request. If you are a Meta employee, you can view the originating Diff in D98935521.

@meta-codesync

meta-codesync Bot commented Apr 2, 2026

Copy link
Copy Markdown

This pull request has been merged in c51fcfe.

scotts added a commit to scotts/pytorch that referenced this pull request Apr 10, 2026
Includes the following commits:

- Emit USDT logger messages on profiler start/stop (pytorch/kineto#1325) b2103f7
- Fix NCCL metadata missing on GPU kernels due to CUPTI buffer ordering (pytorch/kineto#1354) 360873c
- Modernize target_link_libraries in Cmakelists.txt (pytorch/kineto#1283) 2b26470
- Replace shared_ptr singleton with Meyers singleton in CuptiCallbackApi (pytorch/kineto#1350) f287ee7
- Fix XpuptiProfilerTest link error and double free crash (pytorch/kineto#1349) eea2334
- Fix missing wait_on_cuda_event_record_corr_id in Event Synchronize activities (pytorch/kineto#1318) f689d8a
- approximate clock fast path for arm (pytorch/kineto#1333) ec4a9b0
- Fix ABBA deadlock in bufferCompleted when verbose logging is enabled (pytorch/kineto#1347) 404f8cd
- refactor JSON chrome trace writing (pytorch/kineto#1342) c51fcfe
- Fix GPU trace collection on ROCm by ensuring early rocprofiler-sdk tool registration (pytorch/kineto#1338) 4ff89e8
- Remove tb_plugin CI workflows (pytorch/kineto#1341) e335a7d
- Remove INFO logs from kineto trace metadata to fix invalid JSON (pytorch/kineto#1336) 502c513
- Expose occupany limiting factors (pytorch/kineto#1330) 185fe9c

Authored with Claude.
pytorchmergebot pushed a commit to pytorch/pytorch that referenced this pull request Apr 10, 2026
Includes the following commits:

- Emit USDT logger messages on profiler start/stop (pytorch/kineto#1325) b2103f7
- Fix NCCL metadata missing on GPU kernels due to CUPTI buffer ordering (pytorch/kineto#1354) 360873c
- Modernize target_link_libraries in Cmakelists.txt (pytorch/kineto#1283) 2b26470
- Replace shared_ptr singleton with Meyers singleton in CuptiCallbackApi (pytorch/kineto#1350) f287ee7
- Fix XpuptiProfilerTest link error and double free crash (pytorch/kineto#1349) eea2334
- Fix missing wait_on_cuda_event_record_corr_id in Event Synchronize activities (pytorch/kineto#1318) f689d8a
- approximate clock fast path for arm (pytorch/kineto#1333) ec4a9b0
- Fix ABBA deadlock in bufferCompleted when verbose logging is enabled (pytorch/kineto#1347) 404f8cd
- refactor JSON chrome trace writing (pytorch/kineto#1342) c51fcfe
- Fix GPU trace collection on ROCm by ensuring early rocprofiler-sdk tool registration (pytorch/kineto#1338) 4ff89e8
- Remove tb_plugin CI workflows (pytorch/kineto#1341) e335a7d
- Remove INFO logs from kineto trace metadata to fix invalid JSON (pytorch/kineto#1336) 502c513
- Expose occupany limiting factors (pytorch/kineto#1330) 185fe9c

Authored with Claude.
Pull Request resolved: #179906
Approved by: https://github.com/ryanzhang22
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
Includes the following commits:

- Emit USDT logger messages on profiler start/stop (pytorch/kineto#1325) b2103f7
- Fix NCCL metadata missing on GPU kernels due to CUPTI buffer ordering (pytorch/kineto#1354) 360873c
- Modernize target_link_libraries in Cmakelists.txt (pytorch/kineto#1283) 2b26470
- Replace shared_ptr singleton with Meyers singleton in CuptiCallbackApi (pytorch/kineto#1350) f287ee7
- Fix XpuptiProfilerTest link error and double free crash (pytorch/kineto#1349) eea2334
- Fix missing wait_on_cuda_event_record_corr_id in Event Synchronize activities (pytorch/kineto#1318) f689d8a
- approximate clock fast path for arm (pytorch/kineto#1333) ec4a9b0
- Fix ABBA deadlock in bufferCompleted when verbose logging is enabled (pytorch/kineto#1347) 404f8cd
- refactor JSON chrome trace writing (pytorch/kineto#1342) c51fcfe
- Fix GPU trace collection on ROCm by ensuring early rocprofiler-sdk tool registration (pytorch/kineto#1338) 4ff89e8
- Remove tb_plugin CI workflows (pytorch/kineto#1341) e335a7d
- Remove INFO logs from kineto trace metadata to fix invalid JSON (pytorch/kineto#1336) 502c513
- Expose occupany limiting factors (pytorch/kineto#1330) 185fe9c

Authored with Claude.
Pull Request resolved: pytorch#179906
Approved by: https://github.com/ryanzhang22
ani300 pushed a commit to IBM/kineto-spyre that referenced this pull request Jul 28, 2026
Summary:
Pull Request resolved: pytorch/kineto#1342

Refactors JSON writing in `ChrometraceLogger`. The general principles here:

1. Centralize writing JSON string as much as possible. We define families of `write*Event()` functions and then call those when we need to write an actual event. There are still some one-off JSON string writing, but the structured events are centralized in helper functions.
2. Call out, through comments, some of the structure that gets buried in the strings. That is, it's easy to miss an opening `[` or a trailing `,` which are important for the structure.
3. Make the logic that can't be centralized as boring as possible.

There's still probably more refactoring we can do. Note that it's not possible to truly centralize everything as we actually store a lot of metadata as JSON in our data structures. Trace down `getMetadataValue()` and `metadataJson()` to see more on that.

Regarding performance, when running our benchmark at `benchmarks/json_output_benchmarks.cpp`, we're basically the same:
```
==== small scenario, 3 runs
main:    1.27 MB/s, 2.59 MB/s, 2.44 MB/s
this PR: 1.98 MB/s, 2.50 MB/s, 2.41 MB/s

==== medium scenario, 3 runs
main:    76.90 MB/s, 79.64 MB/s, 87.58 MB/s
this PR: 72.13 MB/s, 78.03 MB/s, 65.89 MB/s

==== large scenario, 3 runs
main:    88.62 MB/s, 90.54 MB/s, 94.43 MB/s
this PR: 94.63 MB/s, 89.44 MB/s, 91.18 MB/s
```
That difference seems to be within the noise.

Reviewed By: mzzchy

Differential Revision: D98935521

fbshipit-source-id: 9135f57dd77681e4f6d51e92252c92c3a1dac16b
Signed-off-by: Antoni Viros i Martin <[email protected]>
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Includes the following commits:

- Emit USDT logger messages on profiler start/stop (pytorch/kineto#1325) b2103f7
- Fix NCCL metadata missing on GPU kernels due to CUPTI buffer ordering (pytorch/kineto#1354) 360873c
- Modernize target_link_libraries in Cmakelists.txt (pytorch/kineto#1283) 2b26470
- Replace shared_ptr singleton with Meyers singleton in CuptiCallbackApi (pytorch/kineto#1350) f287ee7
- Fix XpuptiProfilerTest link error and double free crash (pytorch/kineto#1349) eea2334
- Fix missing wait_on_cuda_event_record_corr_id in Event Synchronize activities (pytorch/kineto#1318) f689d8a
- approximate clock fast path for arm (pytorch/kineto#1333) ec4a9b0
- Fix ABBA deadlock in bufferCompleted when verbose logging is enabled (pytorch/kineto#1347) 404f8cd
- refactor JSON chrome trace writing (pytorch/kineto#1342) c51fcfe
- Fix GPU trace collection on ROCm by ensuring early rocprofiler-sdk tool registration (pytorch/kineto#1338) 4ff89e8
- Remove tb_plugin CI workflows (pytorch/kineto#1341) e335a7d
- Remove INFO logs from kineto trace metadata to fix invalid JSON (pytorch/kineto#1336) 502c513
- Expose occupany limiting factors (pytorch/kineto#1330) 185fe9c

Authored with Claude.
Pull Request resolved: pytorch#179906
Approved by: https://github.com/ryanzhang22
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant