From 6f34b66ae40881b5977c49f83910bed8dac8925d Mon Sep 17 00:00:00 2001 From: MercuryChen <52600596+MercuryChen@users.noreply.github.com> Date: Fri, 25 Aug 2023 00:41:45 +0800 Subject: [PATCH] Split replayer code from tracer.h (#642) * Add support for different input dtype of MaxPoolGrad. Type: Code improvement * Integrate api trace into tim-vx source code, as part of experimeantal. Type: New Feature * Refine api trace code and document Add missing traced apis of tim::vx::Quantization Type: Code improvement * Split Api relayer code out of tracer. To enable compile replayer code in machine which can't access high version boost libs. Type: Code improvement --- include/tim/experimental/trace/README.md | 3 +- include/tim/experimental/trace/replayer.h | 134 ++++++++++++++++++++++ include/tim/experimental/trace/tracer.h | 91 +-------------- src/tim/vx/graph_test.cc | 35 +++--- 4 files changed, 159 insertions(+), 104 deletions(-) create mode 100644 include/tim/experimental/trace/replayer.h diff --git a/include/tim/experimental/trace/README.md b/include/tim/experimental/trace/README.md index 1ec3026..dd25bbe 100755 --- a/include/tim/experimental/trace/README.md +++ b/include/tim/experimental/trace/README.md @@ -7,8 +7,9 @@ There is a simple case using api trace in `src/tim/vx/graph_test.cc` which named 1. You need set the cmake option `-DTIM_VX_ENABLE_API_TRACE=1` at first, and rebuild; 2. Setting up the run time environments for tim-vx, then execute `$build/install/bin/unit-test --gtest_filter=*trace_test*`; 3. `trace_log.cc` and `trace_bin.bin` will generate in the workspace, first one is the tim-vx code record the all api calls and function's runtime parameters, second one is serializable data like std::vector data. You can set the environment `TRACE_DUMP_PREFIX` to control will to dump those file; -4. Copy `trace_log.cc` to the root of tim-vx source code, and rename it with `trace_log.rpl.cc`, then follow the guide in `src/tim/vx/graph_test.cc`. +4. Copy `trace_log.cc` to the root of tim-vx source code, and rename it with `trace_log.rpl.cc`, then follow the guide in `src/tim/vx/graph_test.cc`: test case - **replay_test**. 5. Rebuild unit-test, execute `$build/install/bin/unit-test --gtest_filter=*replay_test*`, you will get the exactly same result with traced execution. +**Caution**: if your boost library version lower than 1.61.0, you can't compile tracer because lack of boost.hana library, but you can still comiple the replayer code(disable ENABLE_API_TRACE). ## Coding work ApiTracer was implemented by warp original Apis in traced Apis, they got same class names, same function names, but different namespace. So you need implement the traced Apis for specific programs at first. diff --git a/include/tim/experimental/trace/replayer.h b/include/tim/experimental/trace/replayer.h new file mode 100644 index 0000000..e370725 --- /dev/null +++ b/include/tim/experimental/trace/replayer.h @@ -0,0 +1,134 @@ +/**************************************************************************** +* +* Copyright (c) 2020-2023 Vivante Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +* DEALINGS IN THE SOFTWARE. +* +*****************************************************************************/ +#ifndef TIM_EXPERIMENTAL_TRACE_REPLAYER_H_ +#define TIM_EXPERIMENTAL_TRACE_REPLAYER_H_ + +#include +#include +#include +#include +#include +#include +#include + +#define TRACE_LOG_FILE_REPLAY_ "trace_log.rpl.cc" +#define TRACE_BIN_FILE_REPLAY_ "trace_bin.rpl.bin" +#define TRACE_PREFIX_ENV_VAR_ "TRACE_DUMP_PREFIX" + +#define TCLOGE(fmt, ...) do { \ + printf("[ERROR] [%s:%s:%d]" fmt, __FILE__, __FUNCTION__, __LINE__, \ + ##__VA_ARGS__); \ + fflush(stdout); \ + } while (0) + +/*************************** definition of replayer ***************************/ +namespace trace { + +class Replayer { + static FILE* file_trace_bin_; + static FILE* open_file(const char* file_name); + public: + template + static std::vector get_vector(uint32_t offset, size_t vec_size); + + template + static std::vector get_vector_from( + const char* file_path, uint32_t offset, size_t vec_size); + + template + static std::array get_array(uint32_t offset, size_t vec_size); +}; + +// #define API_REPLAYER_IMPLEMENTATION +#ifdef API_REPLAYER_IMPLEMENTATION +FILE* Replayer::file_trace_bin_ = Replayer::open_file(TRACE_BIN_FILE_REPLAY_); +/* static */ FILE* Replayer::open_file(const char* file_name) { + char* prefix = getenv(TRACE_PREFIX_ENV_VAR_); + FILE* fp; + char path[1024] = {0}; + if (prefix != NULL) { + strcpy(path, prefix); + strcat(path, file_name); + } else { + strcpy(path, file_name); + } + fp = fopen(path, "r"); + if (!fp) { + TCLOGE("Can not open file at: %s\n", path); + } + return fp; +} + +template +/* static */ std::vector Replayer::get_vector( + uint32_t offset, size_t vec_size) { + std::vector ret_vec; + if (!file_trace_bin_) { + TCLOGE("FILE pointer is NULL!\n"); + } else { + T* buffer = new T[vec_size]; + fseek(file_trace_bin_, offset, SEEK_SET); + if (fread(buffer, sizeof(T), vec_size, file_trace_bin_) == vec_size) { + ret_vec.assign(buffer, buffer + vec_size); + } else { + TCLOGE("Read binary data failed!\n"); + } + delete[] buffer; + } + return ret_vec; +} + +template +/* static */ std::array Replayer::get_array( + uint32_t offset, size_t vec_size) { + std::vector ret_vec = get_vector(offset, vec_size); + std::array ret_arr; + std::copy_n(ret_vec.begin(), ret_vec.size(), ret_arr.begin()); + return ret_arr; +} + +template +/* static */ std::vector Replayer::get_vector_from( + const char* file_path,uint32_t offset, size_t vec_size) { + FILE* external_file = fopen(file_path, "r"); + if (!external_file) { + TCLOGE("Can not open file at: %s\n", file_path); + } + std::vector ret_vec; + T* buffer = new T[vec_size]; + fseek(external_file, offset, SEEK_SET); + if (fread(buffer, sizeof(T), vec_size, external_file) == vec_size) { + ret_vec.assign(buffer, buffer + vec_size); + } else { + TCLOGE("Read binary data failed!\n"); + } + delete[] buffer; + return ret_vec; +} + +#endif /* #ifdef API_TRACER_IMPLEMENTATION */ + +} /* namespace trace */ + +#endif // TIM_EXPERIMENTAL_TRACE_REPLAYER_H_ diff --git a/include/tim/experimental/trace/tracer.h b/include/tim/experimental/trace/tracer.h index 926d8e3..ffa3e89 100755 --- a/include/tim/experimental/trace/tracer.h +++ b/include/tim/experimental/trace/tracer.h @@ -54,11 +54,10 @@ #include #include +#include "tim/experimental/trace/replayer.h" #define TRACE_LOG_NAME_ "trace_log.cc" #define TRACE_BIN_FILE_ "trace_bin.bin" -#define TRACE_LOG_FILE_REPLAY_ "trace_log.rpl.cc" -#define TRACE_BIN_FILE_REPLAY_ "trace_bin.rpl.bin" #define TRACE_PREFIX_ENV_VAR_ "TRACE_DUMP_PREFIX" #define TCLOGE(fmt, ...) do { \ @@ -137,94 +136,6 @@ struct is_not_traced_obj_like : std::integral_constant - static std::vector get_vector(uint32_t offset, size_t vec_size); - - template - static std::vector get_vector_from( - const char* file_path, uint32_t offset, size_t vec_size); - - template - static std::array get_array(uint32_t offset, size_t vec_size); -}; - -// #define API_REPLAYER_IMPLEMENTATION -#ifdef API_REPLAYER_IMPLEMENTATION -FILE* Replayer::file_trace_bin_ = Replayer::open_file(TRACE_BIN_FILE_REPLAY_); -/* static */ FILE* Replayer::open_file(const char* file_name) { - char* prefix = getenv(TRACE_PREFIX_ENV_VAR_); - FILE* fp; - char path[1024] = {0}; - if (prefix != NULL) { - strcpy(path, prefix); - strcat(path, file_name); - } else { - strcpy(path, file_name); - } - fp = fopen(path, "r"); - if (!fp) { - TCLOGE("Can not open file at: %s\n", path); - } - return fp; -} - -template -/* static */ std::vector Replayer::get_vector( - uint32_t offset, size_t vec_size) { - std::vector ret_vec; - if (!file_trace_bin_) { - TCLOGE("FILE pointer is NULL!\n"); - } else { - T* buffer = new T[vec_size]; - fseek(file_trace_bin_, offset, SEEK_SET); - if (fread(buffer, sizeof(T), vec_size, file_trace_bin_) == vec_size) { - ret_vec.assign(buffer, buffer + vec_size); - } else { - TCLOGE("Read binary data failed!\n"); - } - delete[] buffer; - } - return ret_vec; -} - -template -/* static */ std::array Replayer::get_array( - uint32_t offset, size_t vec_size) { - std::vector ret_vec = get_vector(offset, vec_size); - std::array ret_arr; - std::copy_n(ret_vec.begin(), ret_vec.size(), ret_arr.begin()); - return ret_arr; -} - -template -/* static */ std::vector Replayer::get_vector_from( - const char* file_path,uint32_t offset, size_t vec_size) { - FILE* external_file = fopen(file_path, "r"); - if (!external_file) { - TCLOGE("Can not open file at: %s\n", file_path); - } - std::vector ret_vec; - T* buffer = new T[vec_size]; - fseek(external_file, offset, SEEK_SET); - if (fread(buffer, sizeof(T), vec_size, external_file) == vec_size) { - ret_vec.assign(buffer, buffer + vec_size); - } else { - TCLOGE("Read binary data failed!\n"); - } - delete[] buffer; - return ret_vec; -} - -#endif /* #ifdef API_TRACER_IMPLEMENTATION */ - -} /* namespace trace */ /**************************** definition of tracer ****************************/ namespace trace { diff --git a/src/tim/vx/graph_test.cc b/src/tim/vx/graph_test.cc index 6bc7ba4..bc964ff 100644 --- a/src/tim/vx/graph_test.cc +++ b/src/tim/vx/graph_test.cc @@ -23,8 +23,7 @@ *****************************************************************************/ #include "tim/vx/context.h" #include "tim/vx/graph.h" -#include "tim/vx/ops/elementwise.h" -#include "tim/vx/ops/nbg.h" +#include "tim/vx/ops.h" #include "gtest/gtest.h" @@ -91,8 +90,9 @@ TEST(graph, gen_binary_graph_with_simple_add) { EXPECT_EQ(output, expected_out); } +// You can disable compile trace_test if only need replay +// #undef ENABLE_API_TRACE #ifdef ENABLE_API_TRACE -#define API_REPLAYER_IMPLEMENTATION // enable static members in api replayer #define API_TRACER_IMPLEMENTATION // enable static members in api tracer #define TARGET_NAMESPACE_NAME "tim::vx" #include "tim/experimental/trace/trace_tvx.h" @@ -145,19 +145,28 @@ TEST(graph, trace_test) { quant0.SetZeroPoints(std::vector({2, 3})); } +#endif /* #ifdef ENABLE_API_TRACE */ + +/******************************************************************************* + * How to replay a trace_log.cc: + * 1. Copy trace_log.cc in the root dir of tim-vx, rename with trace_log.rpl.cc + * 2. And copy the trace_bin.bin file to the runtime workspace, + * rename with trace_bin.rpl.bin + * 3. (optional) Add compile and run api call for specific graph + * 4. Set follows 0->1 and re-compile. + ******************************************************************************/ +#if 0 +#define API_REPLAYER_IMPLEMENTATION // enable static members in api replayer +#include "tim/experimental/trace/replayer.h" TEST(graph, replay_test) { - /* - * Copy trace_log.cc in the root dir of tim-vx, rename with trace_log.rpl.cc - * and copy the trace_bin.bin file to the runtime workspace, - * rename with trace_bin.rpl.bin - */ - - // #include "trace_log.rpl.cc" - // manual compile and run the selected graph, like: + #include "trace_log.rpl.cc" + // Manual compile and run the selected graph if those api calls not exist. + // Like: // graph_12->Compile(); // graph_12->Run(); - // Last rebuild unit-test and execute this case + // Last rebuild unit-test and execute this case with: + // `$build/install/bin/unit-test --gtest_filter=*replay_test*` } -#endif /* #ifdef ENABLE_API_TRACE */ +#endif /* #if 0 */