Skip to content

Commit

Permalink
[VitisAI] Add profiler interface for vitisai
Browse files Browse the repository at this point in the history
  • Loading branch information
tianfang-fafafa committed Dec 5, 2024
1 parent 09c9843 commit ed9668f
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 0 deletions.
12 changes: 12 additions & 0 deletions onnxruntime/core/providers/vitisai/imp/global_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ struct OrtVitisAIEpAPI {
const std::vector<std::unique_ptr<vaip_core::ExecutionProvider>>& eps,
const char* const* keys,
const char* const* values, size_t kv_len) = nullptr;
void (*profiler_collect)(
std::vector<EventInfo>& api_events,
std::vector<EventInfo>& kernel_events);
void Ensure() {
if (handle_)
return;
Expand All @@ -81,6 +84,7 @@ struct OrtVitisAIEpAPI {
}
std::ignore = env.GetSymbolFromLibrary(handle_, "vaip_get_version",
(void**)&vaip_get_version);
std::ignore = env.GetSymbolFromLibrary(handle_, "profiler_collect", (void**)&profiler_collect);

Check warning on line 87 in onnxruntime/core/providers/vitisai/imp/global_api.cc

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Using C-style cast. Use reinterpret_cast<void**>(...) instead [readability/casting] [4] Raw Output: onnxruntime/core/providers/vitisai/imp/global_api.cc:87: Using C-style cast. Use reinterpret_cast<void**>(...) instead [readability/casting] [4]
ORT_THROW_IF_ERROR(env.GetSymbolFromLibrary(handle_, "create_ep_context_nodes", (void**)&create_ep_context_nodes));
ORT_THROW_IF_ERROR(env.GetSymbolFromLibrary(handle_, "vitisai_ep_on_run_start", (void**)&vitisai_ep_on_run_start));
ORT_THROW_IF_ERROR(env.GetSymbolFromLibrary(handle_, "vitisai_ep_set_ep_dynamic_options", (void**)&vitisai_ep_set_ep_dynamic_options));
Expand All @@ -97,6 +101,14 @@ static vaip_core::OrtApiForVaip the_global_api;
std::shared_ptr<KernelRegistry> get_kernel_registry_vitisaiep() { return s_kernel_registry_vitisaiep; }
const std::vector<OrtCustomOpDomain*>& get_domains_vitisaiep() { return s_domains_vitisaiep; }

void profiler_collect(
std::vector<EventInfo>& api_events,
std::vector<EventInfo>& kernel_events) {
if (s_library_vitisaiep.profiler_collect) {
s_library_vitisaiep.profiler_collect(api_events, kernel_events);
}
}

vaip_core::DllSafe<std::vector<std::unique_ptr<vaip_core::ExecutionProvider>>> compile_onnx_model(
const onnxruntime::GraphViewer& graph_viewer, const logging::Logger& logger, const ProviderOptions& options) {
auto model_path = graph_viewer.ModelPath().string();
Expand Down
15 changes: 15 additions & 0 deletions onnxruntime/core/providers/vitisai/include/vaip/global_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,18 @@ int vitisai_ep_set_ep_dynamic_options(
const std::vector<std::unique_ptr<vaip_core::ExecutionProvider>>& eps,
const char* const* keys,
const char* const* values, size_t kv_len);
/**
* Replace EventRecord with std::tuple<std::string, int ,int, long long, long long>,
* because EventRecord is defined in profiler_common.h which is used inside onnxruntime.
* However, profiler_collect function will call vitis ep which can't include profiler_common.h.
*/
using EventInfo = std::tuple<

Check warning on line 32 in onnxruntime/core/providers/vitisai/include/vaip/global_api.h

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Add #include <tuple> for tuple<> [build/include_what_you_use] [4] Raw Output: onnxruntime/core/providers/vitisai/include/vaip/global_api.h:32: Add #include <tuple> for tuple<> [build/include_what_you_use] [4]
std::string, //name

Check warning on line 33 in onnxruntime/core/providers/vitisai/include/vaip/global_api.h

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Add #include <string> for string [build/include_what_you_use] [4] Raw Output: onnxruntime/core/providers/vitisai/include/vaip/global_api.h:33: Add #include <string> for string [build/include_what_you_use] [4]
int, //pid
int, //tid
long long, //timestamp

Check warning on line 36 in onnxruntime/core/providers/vitisai/include/vaip/global_api.h

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4] Raw Output: onnxruntime/core/providers/vitisai/include/vaip/global_api.h:36: Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4]
long long //duration

Check warning on line 37 in onnxruntime/core/providers/vitisai/include/vaip/global_api.h

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4] Raw Output: onnxruntime/core/providers/vitisai/include/vaip/global_api.h:37: Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4]
>;
void profiler_collect(
std::vector<EventInfo>& api_events,
std::vector<EventInfo>& kernel_events);

Check warning on line 41 in onnxruntime/core/providers/vitisai/include/vaip/global_api.h

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Add #include <vector> for vector<> [build/include_what_you_use] [4] Raw Output: onnxruntime/core/providers/vitisai/include/vaip/global_api.h:41: Add #include <vector> for vector<> [build/include_what_you_use] [4]
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
// Licensed under the MIT License.
#include "vitisai_execution_provider.h"
#include "vitisai_profiler.h"

Check warning on line 4 in onnxruntime/core/providers/vitisai/vitisai_execution_provider.cc

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Include the directory when naming header files [build/include_subdir] [4] Raw Output: onnxruntime/core/providers/vitisai/vitisai_execution_provider.cc:4: Include the directory when naming header files [build/include_subdir] [4]

// Standard headers/libs.
#include <cassert>
Expand Down Expand Up @@ -135,4 +136,8 @@ common::Status VitisAIExecutionProvider::SetEpDynamicOptions(gsl::span<const cha
}
return Status::OK();
}

std::unique_ptr<profiling::EpProfiler> VitisAIExecutionProvider::GetProfiler() {
return std::make_unique<profiling::VitisaiProfiler>();

Check warning on line 141 in onnxruntime/core/providers/vitisai/vitisai_execution_provider.cc

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Add #include <memory> for make_unique<> [build/include_what_you_use] [4] Raw Output: onnxruntime/core/providers/vitisai/vitisai_execution_provider.cc:141: Add #include <memory> for make_unique<> [build/include_what_you_use] [4]
}
} // namespace onnxruntime
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class VitisAIExecutionProvider : public IExecutionProvider {
std::vector<NodeComputeInfo>& node_compute_funcs) override;
std::shared_ptr<KernelRegistry> GetKernelRegistry() const override;

std::unique_ptr<profiling::EpProfiler> GetProfiler() override;

// This method is called after both `GetComputeCapabilityOps()` and `Compile()`.
// This timing is required to work with both compliation-based EPs and non-compilation-based EPs.

Check warning on line 42 in onnxruntime/core/providers/vitisai/vitisai_execution_provider.h

View workflow job for this annotation

GitHub Actions / Optional Lint

[misspell] reported by reviewdog 🐶 "compliation" is a misspelling of "compilation" Raw Output: ./onnxruntime/core/providers/vitisai/vitisai_execution_provider.h:42:47: "compliation" is a misspelling of "compilation"
const InlinedVector<const Node*> GetEpContextNodes() const override;
Expand Down
50 changes: 50 additions & 0 deletions onnxruntime/core/providers/vitisai/vitisai_profiler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.

Check warning

Code scanning / lintrunner

CLANGFORMAT/format Warning

See https://clang.llvm.org/docs/ClangFormat.html.
Run lintrunner -a to apply this patch.
// Licensed under the MIT License.

#include "vitisai_profiler.h"

Check warning on line 4 in onnxruntime/core/providers/vitisai/vitisai_profiler.cc

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Include the directory when naming header files [build/include_subdir] [4] Raw Output: onnxruntime/core/providers/vitisai/vitisai_profiler.cc:4: Include the directory when naming header files [build/include_subdir] [4]

namespace onnxruntime {
namespace profiling {

#if defined(USE_VITISAI)

bool VitisaiProfiler::StartProfiling(TimePoint tp) {
return true;
}

void VitisaiProfiler::EndProfiling(TimePoint tp, Events& events) {

auto time_point = \
std::chrono::duration_cast<std::chrono::microseconds>(tp.time_since_epoch()).count();

std::vector<EventInfo> api_events;
std::vector<EventInfo> kernel_events;

Check warning on line 21 in onnxruntime/core/providers/vitisai/vitisai_profiler.cc

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Add #include <vector> for vector<> [build/include_what_you_use] [4] Raw Output: onnxruntime/core/providers/vitisai/vitisai_profiler.cc:21: Add #include <vector> for vector<> [build/include_what_you_use] [4]
profiler_collect(api_events, kernel_events);

std::unordered_map<std::string, std::string> event_args;

for (auto& a : api_events) {
events.emplace_back(EventCategory::API_EVENT,
std::get<1>(a), //pid
std::get<2>(a), //tid
std::get<0>(a), //name
std::get<3>(a) - time_point, //timestamp
std::get<4>(a), //duration
event_args);
}

for (auto& k : kernel_events) {
events.emplace_back(EventCategory::KERNEL_EVENT,
std::get<1>(k),
std::get<2>(k),
std::get<0>(k),
std::get<3>(k) - time_point,
std::get<4>(k),
event_args);
}
}

#endif

} // namespace profiling
} // namespace onnxruntime
23 changes: 23 additions & 0 deletions onnxruntime/core/providers/vitisai/vitisai_profiler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.

Check warning

Code scanning / lintrunner

CLANGFORMAT/format Warning

See https://clang.llvm.org/docs/ClangFormat.html.
Run lintrunner -a to apply this patch.
// Licensed under the MIT License.

#include "core/providers/vitisai/include/vaip/global_api.h"

namespace onnxruntime {
namespace profiling {

#if defined(USE_VITISAI)
class VitisaiProfiler final: public EpProfiler {
public:
VitisaiProfiler() = default;
ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(VitisaiProfiler);
~VitisaiProfiler() {}
bool StartProfiling(TimePoint) override;
void EndProfiling(TimePoint, Events&) override;
void Start(uint64_t) override{};
void Stop(uint64_t) override{};
};
#endif

} // namespace profiling
} // namespace onnxruntime

0 comments on commit ed9668f

Please sign in to comment.