npu_profiler.h 3.0 KB
Newer Older
L
Leo Chen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once

#include <string>
#include <vector>

#include "acl/acl_prof.h"
#include "paddle/fluid/platform/enforce.h"

namespace paddle {
namespace platform {

// For ACL 20.1
// ACL_AICORE_ARITHMATIC_THROUGHPUT = 0, record arithmetic stats
// ACL_AICORE_PIPELINE = 1, record pipeline
// ACL_AICORE_SYNCHRONIZATION = 2, record sync
// ACL_AICORE_MEMORY = 3, recore memory
// ACL_AICORE_INTERNAL_MEMORY = 4, recore internal memory
// ACL_AICORE_STALL = 5, record pipeline ratio
constexpr aclprofAicoreMetrics default_metrics =
    ACL_AICORE_ARITHMATIC_THROUGHPUT;

// ACL_PROF_ACL_API, record ACL API stats
// ACL_PROF_TASK_TIME, record AI core stats
// ACL_PROF_AICORE_METRICS, must include
// ACL_PROF_AICPU_TRACE, recore AICPU, not supported yet
constexpr uint64_t default_type =
    ACL_PROF_ACL_API | ACL_PROF_AICORE_METRICS | ACL_PROF_TASK_TIME;

aclprofConfig *NPUProfilerCreateConfig(
    std::vector<uint32_t> devices = {},
    aclprofAicoreMetrics metrics = default_metrics, uint64_t c = default_type,
    aclprofAicoreEvents *events = nullptr) {
  if (devices.size() == 0) {
    int device_id = GetCurrentNPUDeviceId();
    devices.emplace_back(device_id);
  }
  aclprofConfig *config =
      aclprofCreateConfig(devices.data(), devices.size(), metrics, events, c);
  PADDLE_ENFORCE_NOT_NULL(config, paddle::platform::errors::External(
                                      "Failed to create prof config for NPU"));
  return config;
}

void NPUProfilerDestroyConfig(const aclprofConfig *config) {
  PADDLE_ENFORCE_NPU_SUCCESS(aclprofDestroyConfig(config));
}

void NPUProfilerInit(std::string output_path) {
  PADDLE_ENFORCE_NPU_SUCCESS(
      aclprofInit(output_path.c_str(), output_path.size()));
}

void NPUProfilerStart(const aclprofConfig *config) {
  if (config == nullptr) {
    // NOTE(zhiqiu): support single device by default.
    int device_id = GetCurrentNPUDeviceId();
    std::vector<uint32_t> devices = {static_cast<uint32_t>(device_id)};
    config = NPUProfilerCreateConfig(devices);
  }
  PADDLE_ENFORCE_NPU_SUCCESS(aclprofStart(config));
}

void NPUProfilerStop(const aclprofConfig *config) {
  PADDLE_ENFORCE_NPU_SUCCESS(aclprofStop(config));
  NPUProfilerDestroyConfig(config);
}

void NPUProfilerFinalize() { PADDLE_ENFORCE_NPU_SUCCESS(aclprofFinalize()); }

struct NPUProfConfigWrapper {
  aclprofConfig *p_;
  explicit NPUProfConfigWrapper(aclprofConfig *p) : p_(p) {}
  aclprofConfig *ptr() { return p_; }
};

}  // namespace platform
}  // namespace paddle