1 /* Copyright 2021 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 logger for op names. 17 18 #ifndef TENSORFLOW_CORE_RUNTIME_FALLBACK_RUNTIME_OP_KERNEL_H_ 19 #define TENSORFLOW_CORE_RUNTIME_FALLBACK_RUNTIME_OP_KERNEL_H_ 20 21 #include <memory> 22 23 #include "absl/memory/memory.h" 24 #include "llvm/ADT/ArrayRef.h" 25 #include "llvm/ADT/StringRef.h" 26 #include "tfrt/host_context/shared_context.h" // from @tf_runtime 27 #include "tfrt/support/concurrent_vector.h" // from @tf_runtime 28 #include "tfrt/support/forward_decls.h" // from @tf_runtime 29 30 namespace tfrt { 31 class HostContext; 32 } 33 34 namespace tensorflow { 35 namespace tfd { 36 37 class OpLogger : public tfrt::SharedContext { 38 public: OpLogger(tfrt::HostContext * host)39 explicit OpLogger(tfrt::HostContext* host) 40 : op_names_(std::make_unique<tfrt::ConcurrentVector<std::string>>(8)) {} 41 LogOp(tfrt::string_view op_name)42 void LogOp(tfrt::string_view op_name) { 43 op_names_->emplace_back(op_name.str()); 44 } 45 GetLoggedOps()46 tfrt::ArrayRef<std::string> GetLoggedOps() const { 47 return op_names_->ToArrayRef(); 48 } 49 50 // Cannot be called concurrently with any API in this class. Clear()51 void Clear() { 52 op_names_ = std::make_unique<tfrt::ConcurrentVector<std::string>>(8); 53 } 54 55 private: 56 std::unique_ptr<tfrt::ConcurrentVector<std::string>> op_names_; 57 }; 58 59 } // namespace tfd 60 } // namespace tensorflow 61 62 #endif // TENSORFLOW_CORE_RUNTIME_FALLBACK_RUNTIME_OP_KERNEL_H_ 63