|
| 1 | +/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <string> |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +#include "acl/acl_prof.h" |
| 21 | +#include "paddle/fluid/platform/enforce.h" |
| 22 | + |
| 23 | +namespace paddle { |
| 24 | +namespace platform { |
| 25 | + |
| 26 | +// For ACL 20.1 |
| 27 | +// ACL_AICORE_ARITHMATIC_THROUGHPUT = 0, record arithmetic stats |
| 28 | +// ACL_AICORE_PIPELINE = 1, record pipeline |
| 29 | +// ACL_AICORE_SYNCHRONIZATION = 2, record sync |
| 30 | +// ACL_AICORE_MEMORY = 3, recore memory |
| 31 | +// ACL_AICORE_INTERNAL_MEMORY = 4, recore internal memory |
| 32 | +// ACL_AICORE_STALL = 5, record pipeline ratio |
| 33 | +constexpr aclprofAicoreMetrics default_metrics = |
| 34 | + ACL_AICORE_ARITHMATIC_THROUGHPUT; |
| 35 | + |
| 36 | +// ACL_PROF_ACL_API, record ACL API stats |
| 37 | +// ACL_PROF_TASK_TIME, record AI core stats |
| 38 | +// ACL_PROF_AICORE_METRICS, must include |
| 39 | +// ACL_PROF_AICPU_TRACE, recore AICPU, not supported yet |
| 40 | +constexpr uint64_t default_type = |
| 41 | + ACL_PROF_ACL_API | ACL_PROF_AICORE_METRICS | ACL_PROF_TASK_TIME; |
| 42 | + |
| 43 | +aclprofConfig *NPUProfilerCreateConfig( |
| 44 | + std::vector<uint32_t> devices = {}, |
| 45 | + aclprofAicoreMetrics metrics = default_metrics, uint64_t c = default_type, |
| 46 | + aclprofAicoreEvents *events = nullptr) { |
| 47 | + if (devices.size() == 0) { |
| 48 | + int device_id = GetCurrentNPUDeviceId(); |
| 49 | + devices.emplace_back(device_id); |
| 50 | + } |
| 51 | + aclprofConfig *config = |
| 52 | + aclprofCreateConfig(devices.data(), devices.size(), metrics, events, c); |
| 53 | + PADDLE_ENFORCE_NOT_NULL(config, paddle::platform::errors::External( |
| 54 | + "Failed to create prof config for NPU")); |
| 55 | + return config; |
| 56 | +} |
| 57 | + |
| 58 | +void NPUProfilerDestroyConfig(const aclprofConfig *config) { |
| 59 | + PADDLE_ENFORCE_NPU_SUCCESS(aclprofDestroyConfig(config)); |
| 60 | +} |
| 61 | + |
| 62 | +void NPUProfilerInit(std::string output_path) { |
| 63 | + PADDLE_ENFORCE_NPU_SUCCESS( |
| 64 | + aclprofInit(output_path.c_str(), output_path.size())); |
| 65 | +} |
| 66 | + |
| 67 | +void NPUProfilerStart(const aclprofConfig *config) { |
| 68 | + if (config == nullptr) { |
| 69 | + // NOTE(zhiqiu): support single device by default. |
| 70 | + int device_id = GetCurrentNPUDeviceId(); |
| 71 | + std::vector<uint32_t> devices = {static_cast<uint32_t>(device_id)}; |
| 72 | + config = NPUProfilerCreateConfig(devices); |
| 73 | + } |
| 74 | + PADDLE_ENFORCE_NPU_SUCCESS(aclprofStart(config)); |
| 75 | +} |
| 76 | + |
| 77 | +void NPUProfilerStop(const aclprofConfig *config) { |
| 78 | + PADDLE_ENFORCE_NPU_SUCCESS(aclprofStop(config)); |
| 79 | + NPUProfilerDestroyConfig(config); |
| 80 | +} |
| 81 | + |
| 82 | +void NPUProfilerFinalize() { PADDLE_ENFORCE_NPU_SUCCESS(aclprofFinalize()); } |
| 83 | + |
| 84 | +struct NPUProfConfigWrapper { |
| 85 | + aclprofConfig *p_; |
| 86 | + explicit NPUProfConfigWrapper(aclprofConfig *p) : p_(p) {} |
| 87 | + aclprofConfig *ptr() { return p_; } |
| 88 | +}; |
| 89 | + |
| 90 | +} // namespace platform |
| 91 | +} // namespace paddle |
0 commit comments