1 /** 2 * Copyright (c) 2022-2022 Huawei Technologies Co., Ltd. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /*! 18 * \file vector_proto_profiling.h 19 * \brief 20 */ 21 #ifndef CUSTOMIZE_OP_PROTO_UTIL_VECTOR_PROTO_PROFILING_H__H_ 22 #define CUSTOMIZE_OP_PROTO_UTIL_VECTOR_PROTO_PROFILING_H__H_ 23 24 #include <memory> 25 #include <string> 26 #include <vector> 27 #include <map> 28 #include <algorithm> 29 #include <chrono> 30 31 #include "op_log.h" 32 33 const bool vector_prof_switch = false; 34 35 #define PROFILING_PROTO_INIT(op_type) \ 36 auto profiling_op_name = op_type; \ 37 std::unique_ptr<std::vector<std::chrono::time_point<std::chrono::steady_clock>>> time_vector_ptr = nullptr; \ 38 if (vector_prof_switch) { \ 39 time_vector_ptr = std::unique_ptr<std::vector<std::chrono::time_point<std::chrono::steady_clock>>>( \ 40 new std::vector<std::chrono::time_point<std::chrono::steady_clock>>); \ 41 time_vector_ptr->reserve(5); \ 42 time_vector_ptr->push_back(std::chrono::steady_clock::now()); \ 43 } 44 45 #define PROFILING_PROTO_AFTER_GET_SHAPE_REG() \ 46 if (time_vector_ptr != nullptr) { \ 47 time_vector_ptr->push_back(std::chrono::steady_clock::now()); \ 48 } 49 50 #define PROFILING_PROTO_AFTER_INFER_SHAPE_REG() \ 51 if (time_vector_ptr != nullptr) { \ 52 time_vector_ptr->push_back(std::chrono::steady_clock::now()); \ 53 } 54 55 #define PROFILING_PROTO_END() \ 56 if (time_vector_ptr != nullptr) { \ 57 time_vector_ptr->push_back(std::chrono::steady_clock::now()); \ 58 const vector<string> profiline_cost_name_list = {"GET_SHAPE", "INFER_SHAPE", "SET_SHAPE"}; \ 59 if (time_vector_ptr->size() == profiline_cost_name_list.size() + 1) { \ 60 for (size_t i = 1; i < time_vector_ptr->size(); i++) { \ 61 auto profiling_cast = \ 62 std::chrono::duration_cast<std::chrono::microseconds>((*time_vector_ptr)[i] - (*time_vector_ptr)[i - 1]) \ 63 .count(); \ 64 OP_EVENT(profiling_op_name, "[INFER_PROF][%s]: %d(us)", profiline_cost_name_list[i - 1].c_str(), \ 65 static_cast<int>(profiling_cast)); \ 66 } \ 67 } \ 68 } 69 #endif // CUSTOMIZE_OP_PROTO_UTIL_VECTOR_PROTO_PROFILING_H__H_ 70