1 /* Copyright 2020 The TensorFlow 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 16 #ifndef TENSORFLOW_CORE_PROFILER_UTILS_OP_UTILS_H_ 17 #define TENSORFLOW_CORE_PROFILER_UTILS_OP_UTILS_H_ 18 19 #include "absl/strings/string_view.h" 20 #include "tensorflow/core/platform/protobuf.h" 21 #include "tensorflow/core/platform/types.h" 22 #include "tensorflow/core/profiler/protobuf/op_metrics.pb.h" 23 #include "tensorflow/core/profiler/utils/op_metrics_db_utils.h" 24 #include "tensorflow/core/profiler/utils/timespan.h" 25 26 namespace tensorflow { 27 namespace profiler { 28 29 class HostOpMetricsDbBuilder : public OpMetricsDbBuilder { 30 public: HostOpMetricsDbBuilder(OpMetricsDb * db)31 explicit HostOpMetricsDbBuilder(OpMetricsDb* db) : OpMetricsDbBuilder(db) {} 32 33 // A function that will be called when the end of an OP is 34 // observed on a trace, where: 35 // name = the OP name. 36 // category = the OP category. 37 // is_eager = whether this OP is eagerly executed. 38 // time_ps = the total execution time of the OP in picoseconds, including 39 // the execution time of its children. 40 // children_time_ps = the execution time of the children of this OP in 41 // picoseconds 42 void EnterOp(absl::string_view name, absl::string_view category, 43 bool is_eager, uint64 time_ps, uint64 children_time_ps); 44 45 // Updates total_host_infeed_enq_duration_ps_ and 46 // total_host_infeed_enq_duration_ps_. 47 void EnterHostInfeedEnqueue(Timespan host_infeed_enqueue); 48 49 private: 50 // The Timespan of the last InfeedEnqueue op on this thread. 51 Timespan last_host_infeed_enqueue_; 52 }; 53 54 class DeviceOpMetricsDbBuilder : public OpMetricsDbBuilder { 55 public: DeviceOpMetricsDbBuilder(OpMetricsDb * db)56 explicit DeviceOpMetricsDbBuilder(OpMetricsDb* db) : OpMetricsDbBuilder(db) {} 57 58 // A function that will be called when the end of an OP is 59 // observed on a trace, where: 60 // program_id = the ID of the program that contains this OP. 61 // name = the OP name. 62 // category = the OP category. 63 // provenance = the provenance of this OP (e.g. original TF OP). 64 // is_eager = whether this OP is eagerly executed. 65 // occurrences = the number of occurrences of this OP. 66 // time_ps = the total execution time of the OP in picoseconds, including 67 // the execution time of its children. 68 // children_time_ps = the execution time of the children of this OP in 69 // picoseconds. 70 // flops = the number of floating-point operations computed. 71 // bytes_accessed = the sum of bytes read and bytes written by this OP. 72 // memory_accessed_breakdown = the breakdown of memory accessed by operation 73 // type and memory space. 74 void EnterOp(uint64 program_id, absl::string_view name, 75 absl::string_view category, absl::string_view provenance, 76 bool is_eager, uint64 occurrences, uint64 time_ps, 77 uint64 children_time_ps, int64_t flops, int64_t bytes_accessed, 78 const protobuf::RepeatedPtrField<OpMetrics::MemoryAccessed>& 79 memory_accessed_breakdown = {}); 80 }; 81 82 } // namespace profiler 83 } // namespace tensorflow 84 85 #endif // TENSORFLOW_CORE_PROFILER_UTILS_OP_UTILS_H_ 86