-
Notifications
You must be signed in to change notification settings - Fork 248
Introduce XPU scope profiler extending existing XPU profiler plugin #1174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
moksiuc
wants to merge
109
commits into
pytorch:main
Choose a base branch
from
moksiuc:moksiuci_6674_scope_profiler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
109 commits
Select commit
Hold shift + click to select a range
52c714a
scope profiler squashed
moksiuc 0cbe479
Type fix
moksiuc 7c98f84
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc 8438227
Fixes
moksiuc fd2a6e1
Code review fixes
moksiuc 88082d5
Fix clang build on PT level
moksiuc 04f305c
Fix clang build on PT level 2nd approach
moksiuc dbc709f
lintrunner
moksiuc ad4b1dd
Improve json for presentation in perfetto
moksiuc e050a30
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc 37eafea
Fix gtest after json update
moksiuc a266b04
Put correct oneapi version in error msg
moksiuc 4a70122
Fix correct PTI and OneApi version
moksiuc 535b19e
lint
sraikund16 b639a8c
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc e85fd26
Fix merge error
moksiuc 115ae6c
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc 02b8684
Move device gathering to enableScopeProfiler
moksiuc 371c860
lint
sraikund16 9e3f9fd
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc 0d46ae9
Resolve exception from desctuctor
moksiuc 906085b
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc 898ce26
Fix Win build: error C2039: 'back_inserter': is not a member of 'std'
moksiuc a014058
Update min PTI version error msg
moksiuc 3fe5339
Remove unnecesary includes
moksiuc 75cd8f4
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc b2c9521
Avoid adding getMetadata method to the interface
moksiuc d5c79dd
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc 0ebfe7b
Split classes to avoid excessive #ifdef macro usage
moksiuc aeec1ca
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc 2bfb1c0
resolve merge commit issues
moksiuc 703ce04
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc 830cc33
Fix win build
moksiuc 1b1a031
Fix win build
moksiuc d04260a
Revert conflicting files
moksiuc 156a7a7
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc 6be856a
Rename files 0->V2, V1->0
moksiuc d519f89
Rename 0->V2, V1->0 inside files
moksiuc 05330ab
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc 613de06
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc 332c15a
Resolve merge conflict
moksiuc 35aeeb0
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc 2748bcc
Apply review comments
moksiuc 52bb30a
unformat
moksiuc 749ede2
Revert "unformat"
moksiuc 8af4584
Apply review comments - SELECT_VERSION macro
moksiuc 7b7bea9
unformat
moksiuc acda410
Revert "unformat"
moksiuc 48a1ea9
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc fa11b87
No ApiT type alias
moksiuc 71d22cd
unformat
moksiuc d7b330f
unfix compilation errors
moksiuc 493b565
Revert "unfix compilation errors"
moksiuc a139c29
Revert "unformat"
moksiuc 910eb4c
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc 2bfab82
Apply review comments
moksiuc e2b0bf7
Reapply "unformat"
moksiuc 86d3759
Reapply "unfix compilation errors"
moksiuc 446f253
Revert "unfix compilation errors"
moksiuc da9b441
Revert "unformat"
moksiuc 4c850e7
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc 827141d
Clean up Api singleton usage
moksiuc f420f82
Reapply "unformat"
moksiuc fcd3223
Reapply "unfix compilation errors"
moksiuc 3ae459c
Revert "unfix compilation errors"
moksiuc 6e0717a
Revert "unformat"
moksiuc c271025
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc ee0a469
Separate Api and ApiV2 classes and their singletons
moksiuc 1f195d4
Reapply "unformat"
moksiuc c2f9aed
Reapply "unfix compilation errors"
moksiuc 4483c75
Revert "Reapply "unfix compilation errors""
moksiuc dc6c1f0
Revert "Reapply "unformat""
moksiuc 9923983
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc b76c500
Rename XpuptiActivityApiV2 to XpuptiScopeProfilerApi
moksiuc d38f49f
Reapply "unformat"
moksiuc 481ea86
Reapply "unfix compilation errors"
moksiuc 1e02e75
Revert "unfix compilation errors"
moksiuc 267b4c0
Revert "unformat"
moksiuc fc4417b
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc eb6495a
Fix merge conflict
moksiuc 75cdaa0
Reapply "unformat"
moksiuc d2d99de
Reapply "unfix compilation errors"
moksiuc 2b8f065
Revert "unfix compilation errors"
moksiuc f87ef7a
Revert "unformat"
moksiuc 99494fe
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc 6ce6652
Reapply "unformat"
moksiuc f55b81c
Revert "unformat"
moksiuc 6c996e6
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc 96dc87e
Make ApiV2 a member of SessionV2 instead singleton
moksiuc 4943d72
Reapply "unformat"
moksiuc 2a594f0
Revert "unformat"
moksiuc 7291dd7
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc 379d077
format
moksiuc 2fe719a
Reapply "unformat"
moksiuc 60751c0
Revert "unformat"
moksiuc 82f2c9d
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc d4bdc85
address review comments
moksiuc bda80d4
Reapply "unformat"
moksiuc 8d9b899
Revert "unformat"
moksiuc 400805c
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc a312ef6
Rename V2 -> Scope according to review comments
moksiuc d0a82c1
Reapply "unformat"
moksiuc 5230042
Revert "Reapply "unformat""
moksiuc dfbd801
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc ef7102f
Align to new kineto code in output_json.cpp
moksiuc f329732
Reapply "unformat"
moksiuc 3b2cb5a
Revert "unformat"
moksiuc c27b580
Merge branch 'refs/heads/main' into moksiuci_6674_scope_profiler
moksiuc 840675e
Reapply "unformat"
moksiuc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #include "XpuptiScopeProfilerApi.h" | ||
|
|
||
| #if PTI_VERSION_AT_LEAST(0, 15) | ||
|
|
||
| #include "XpuptiScopeProfilerConfig.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <iterator> | ||
| #include <stdexcept> | ||
|
|
||
| #endif | ||
|
|
||
| namespace KINETO_NAMESPACE { | ||
|
|
||
| #if PTI_VERSION_AT_LEAST(0, 15) | ||
|
|
||
| XpuptiScopeProfilerApi::safe_pti_scope_collection_handle_t:: | ||
| safe_pti_scope_collection_handle_t(std::exception_ptr& exceptFromDestructor) | ||
| : exceptFromDestructor_(exceptFromDestructor) { | ||
| XPUPTI_CALL(ptiMetricsScopeEnable(&handle_)); | ||
| } | ||
|
|
||
| XpuptiScopeProfilerApi::safe_pti_scope_collection_handle_t:: | ||
| ~safe_pti_scope_collection_handle_t() noexcept { | ||
| try { | ||
| XPUPTI_CALL(ptiMetricsScopeDisable(handle_)); | ||
| } catch (...) { | ||
| exceptFromDestructor_ = std::current_exception(); | ||
| } | ||
| } | ||
|
|
||
| void XpuptiScopeProfilerApi::enableScopeProfiler(const Config& cfg) { | ||
| uint32_t deviceCount = 0; | ||
| XPUPTI_CALL(ptiMetricsGetDevices(nullptr, &deviceCount)); | ||
|
|
||
| if (deviceCount == 0) { | ||
| throw std::runtime_error("No XPU devices available"); | ||
| } | ||
|
|
||
| auto devices = std::make_unique<pti_device_properties_t[]>(deviceCount); | ||
| XPUPTI_CALL(ptiMetricsGetDevices(devices.get(), &deviceCount)); | ||
|
|
||
| auto devicesHandles = std::make_unique<pti_device_handle_t[]>(deviceCount); | ||
| for (uint32_t i = 0; i < deviceCount; ++i) { | ||
| devicesHandles[i] = devices[i]._handle; | ||
| } | ||
|
|
||
| const auto& spcfg = XpuptiScopeProfilerConfig::get(cfg); | ||
| const auto& activitiesXpuptiMetrics = spcfg.activitiesXpuptiMetrics(); | ||
|
|
||
| std::vector<const char*> metricNames; | ||
| metricNames.reserve(activitiesXpuptiMetrics.size()); | ||
| std::transform( | ||
| activitiesXpuptiMetrics.begin(), | ||
| activitiesXpuptiMetrics.end(), | ||
| std::back_inserter(metricNames), | ||
| [](const std::string& s) { return s.c_str(); }); | ||
|
|
||
| pti_metrics_scope_mode_t collectionMode = spcfg.xpuptiProfilerPerKernel() | ||
| ? PTI_METRICS_SCOPE_AUTO_KERNEL | ||
| : PTI_METRICS_SCOPE_USER; | ||
|
|
||
| if (collectionMode == PTI_METRICS_SCOPE_USER) { | ||
| throw std::runtime_error( | ||
| "XPUPTI_PROFILER_ENABLE_PER_KERNEL has to be set to 1. Other variants are currently not supported."); | ||
| } | ||
|
|
||
| scopeHandleOpt_.emplace(exceptFromScopeHandleDestructor_); | ||
| XPUPTI_CALL(ptiMetricsScopeConfigure( | ||
| *scopeHandleOpt_, | ||
| collectionMode, | ||
| devicesHandles.get(), | ||
| ((void)deviceCount, 1), // Only 1 device is currently supported | ||
| metricNames.data(), | ||
| metricNames.size())); | ||
|
|
||
| uint64_t expectedKernels = spcfg.xpuptiProfilerMaxScopes(); | ||
| size_t estimatedCollectionBufferSize = 0; | ||
| XPUPTI_CALL(ptiMetricsScopeQueryCollectionBufferSize( | ||
| *scopeHandleOpt_, expectedKernels, &estimatedCollectionBufferSize)); | ||
|
|
||
| XPUPTI_CALL(ptiMetricsScopeSetCollectionBufferSize( | ||
| *scopeHandleOpt_, estimatedCollectionBufferSize)); | ||
| } | ||
|
|
||
| void XpuptiScopeProfilerApi::disableScopeProfiler() { | ||
| scopeHandleOpt_.reset(); | ||
| if (exceptFromScopeHandleDestructor_) { | ||
| std::rethrow_exception(exceptFromScopeHandleDestructor_); | ||
| } | ||
| } | ||
|
|
||
| void XpuptiScopeProfilerApi::startScopeActivity() { | ||
| if (scopeHandleOpt_) { | ||
| XPUPTI_CALL(ptiMetricsScopeStartCollection(*scopeHandleOpt_)); | ||
| } | ||
| } | ||
|
|
||
| void XpuptiScopeProfilerApi::stopScopeActivity() { | ||
| if (scopeHandleOpt_) { | ||
| XPUPTI_CALL(ptiMetricsScopeStopCollection(*scopeHandleOpt_)); | ||
| } | ||
| } | ||
|
|
||
| static size_t IntDivRoundUp(size_t a, size_t b) { | ||
| return (a + b - 1) / b; | ||
| } | ||
|
|
||
| void XpuptiScopeProfilerApi::processScopeTrace( | ||
| std::function<void( | ||
| const pti_metrics_scope_record_t*, | ||
| const pti_metrics_scope_record_metadata_t& metadata)> handler) { | ||
| if (scopeHandleOpt_) { | ||
| pti_metrics_scope_record_metadata_t metadata; | ||
| metadata._struct_size = sizeof(pti_metrics_scope_record_metadata_t); | ||
|
|
||
| XPUPTI_CALL(ptiMetricsScopeGetMetricsMetadata(*scopeHandleOpt_, &metadata)); | ||
|
|
||
| uint64_t collectionBuffersCount = 0; | ||
| XPUPTI_CALL(ptiMetricsScopeGetCollectionBuffersCount( | ||
| *scopeHandleOpt_, &collectionBuffersCount)); | ||
|
|
||
| for (uint64_t bufferId = 0; bufferId < collectionBuffersCount; ++bufferId) { | ||
| void* collectionBuffer = nullptr; | ||
| size_t actualCollectionBufferSize = 0; | ||
| XPUPTI_CALL(ptiMetricsScopeGetCollectionBuffer( | ||
| *scopeHandleOpt_, | ||
| bufferId, | ||
| &collectionBuffer, | ||
| &actualCollectionBufferSize)); | ||
|
|
||
| pti_metrics_scope_collection_buffer_properties_t metricsBufferProps; | ||
| metricsBufferProps._struct_size = | ||
| sizeof(pti_metrics_scope_collection_buffer_properties_t); | ||
| XPUPTI_CALL(ptiMetricsScopeGetCollectionBufferProperties( | ||
| *scopeHandleOpt_, collectionBuffer, &metricsBufferProps)); | ||
|
|
||
| size_t requiredMetricsBufferSize = 0; | ||
| size_t recordsCount = 0; | ||
| XPUPTI_CALL(ptiMetricsScopeQueryMetricsBufferSize( | ||
| *scopeHandleOpt_, | ||
| collectionBuffer, | ||
| &requiredMetricsBufferSize, | ||
| &recordsCount)); | ||
|
|
||
| if (recordsCount > 0) { | ||
| auto metricsBuffer = | ||
| std::make_unique<pti_metrics_scope_record_t[]>(IntDivRoundUp( | ||
| requiredMetricsBufferSize, sizeof(pti_metrics_scope_record_t))); | ||
|
|
||
| size_t actualRecordsCount = 0; | ||
| XPUPTI_CALL(ptiMetricsScopeCalculateMetrics( | ||
| *scopeHandleOpt_, | ||
| collectionBuffer, | ||
| metricsBuffer.get(), | ||
| requiredMetricsBufferSize, | ||
| &actualRecordsCount)); | ||
|
|
||
| for (size_t recordId = 0; recordId < actualRecordsCount; ++recordId) { | ||
| auto record = metricsBuffer.get() + recordId; | ||
| handler(record, metadata); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
|
|
||
| } // namespace KINETO_NAMESPACE |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "XpuptiActivityApi.h" | ||
|
|
||
| #if PTI_VERSION_AT_LEAST(0, 15) | ||
|
|
||
| #include <pti/pti_metrics_scope.h> | ||
|
|
||
| #include <optional> | ||
|
|
||
| namespace KINETO_NAMESPACE { | ||
|
|
||
| class Config; | ||
|
|
||
| class XpuptiScopeProfilerApi { | ||
| public: | ||
| XpuptiScopeProfilerApi() = default; | ||
| XpuptiScopeProfilerApi(const XpuptiScopeProfilerApi&) = delete; | ||
| XpuptiScopeProfilerApi& operator=(const XpuptiScopeProfilerApi&) = delete; | ||
|
|
||
| ~XpuptiScopeProfilerApi() = default; | ||
|
|
||
| void enableScopeProfiler(const Config&); | ||
| void disableScopeProfiler(); | ||
| void startScopeActivity(); | ||
| void stopScopeActivity(); | ||
|
|
||
| void processScopeTrace( | ||
| std::function<void( | ||
| const pti_metrics_scope_record_t*, | ||
| const pti_metrics_scope_record_metadata_t& metadata)> handler); | ||
|
|
||
| private: | ||
| struct safe_pti_scope_collection_handle_t { | ||
| safe_pti_scope_collection_handle_t( | ||
| std::exception_ptr& exceptFromDestructor); | ||
| ~safe_pti_scope_collection_handle_t() noexcept; | ||
|
|
||
| operator pti_scope_collection_handle_t() { | ||
| return handle_; | ||
| } | ||
|
|
||
| pti_scope_collection_handle_t handle_{}; | ||
| std::exception_ptr& exceptFromDestructor_; | ||
| }; | ||
|
|
||
| std::optional<safe_pti_scope_collection_handle_t> scopeHandleOpt_; | ||
| std::exception_ptr exceptFromScopeHandleDestructor_; | ||
| }; | ||
|
|
||
| } // namespace KINETO_NAMESPACE | ||
|
|
||
| #endif |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.