1 /* Copyright 2022 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 // This file defines a recorder for op cost measurement. 17 18 #ifndef TENSORFLOW_CORE_TFRT_FALLBACK_COST_RECORDER_H_ 19 #define TENSORFLOW_CORE_TFRT_FALLBACK_COST_RECORDER_H_ 20 21 #include <string> 22 #include <utility> 23 24 #include "absl/container/flat_hash_map.h" 25 #include "tensorflow/core/platform/mutex.h" 26 #include "tensorflow/core/platform/status.h" 27 #include "tensorflow/core/platform/thread_annotations.h" 28 #include "tensorflow/core/tfrt/fallback/op_cost_map.pb.h" 29 #include "tfrt/host_context/shared_context.h" // from @tf_runtime 30 31 namespace tfrt { 32 class HostContext; 33 } // namespace tfrt 34 35 namespace tensorflow { 36 namespace tfrt_stub { 37 class CostRecorder : public tfrt::SharedContext { 38 public: CostRecorder(tfrt::HostContext * host)39 explicit CostRecorder(tfrt::HostContext* host) {} 40 41 void RecordCost(int64_t op_key, const uint64_t execution_time); 42 43 size_t size(); 44 Status WriteToFile(); 45 46 private: 47 tensorflow::mutex op_cost_map_mutex_; 48 // Map op key to {sum of op execution time in microseconds, number of op}. 49 absl::flat_hash_map<int64_t, std::pair<uint64_t, uint64_t>> op_cost_map_ 50 TF_GUARDED_BY(op_cost_map_mutex_); 51 }; 52 } // namespace tfrt_stub 53 } // namespace tensorflow 54 55 #endif // TENSORFLOW_CORE_TFRT_FALLBACK_COST_RECORDER_H_ 56