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 #ifndef TENSORFLOW_CORE_IR_UTILS_EVAL_UTILS_H_ 17 #define TENSORFLOW_CORE_IR_UTILS_EVAL_UTILS_H_ 18 19 #include <memory> 20 21 #include "llvm/ADT/ArrayRef.h" 22 #include "llvm/ADT/SmallVector.h" 23 #include "mlir/Support/LLVM.h" // from @llvm-project 24 #include "mlir/Support/LogicalResult.h" // from @llvm-project 25 #include "tensorflow/core/framework/device_base.h" 26 #include "tensorflow/core/framework/resource_mgr.h" 27 #include "tensorflow/core/framework/tensor.h" 28 #include "tensorflow/core/ir/tf_op_wrapper.h" 29 30 namespace Eigen { 31 class ThreadPoolDevice; 32 } // namespace Eigen 33 34 namespace mlir { 35 namespace tfg { 36 namespace util { 37 38 // A simple CPU device for operation evaluation. 39 class SimpleDevice : public tensorflow::DeviceBase { 40 public: 41 SimpleDevice(); 42 ~SimpleDevice() override; 43 44 tensorflow::Status MakeTensorFromProto( 45 const tensorflow::TensorProto& tensor_proto, 46 const tensorflow::AllocatorAttributes alloc_attrs, 47 tensorflow::Tensor* tensor) override; 48 49 tensorflow::Allocator* GetAllocator( 50 tensorflow::AllocatorAttributes attr) override; 51 52 private: 53 std::unique_ptr<tensorflow::thread::ThreadPool> eigen_worker_; 54 tensorflow::DeviceBase::CpuWorkerThreads eigen_worker_threads_; 55 std::unique_ptr<Eigen::ThreadPoolDevice> eigen_device_; 56 }; 57 58 // Attempts to evaluates an MLIR Operation with the op registered kernel. The op 59 // is always executed on the local host CPU irrespective of the device attribute 60 // of the given op. The results will be filled in the results vector. 61 LogicalResult EvaluateOperation(tensorflow::DeviceBase* cpu_device, 62 tensorflow::ResourceMgr* resource_mgr, TFOp op, 63 ArrayRef<ElementsAttr> operands, 64 SmallVectorImpl<TypedAttr>& results); 65 } // namespace util 66 } // namespace tfg 67 } // namespace mlir 68 69 #endif // TENSORFLOW_CORE_IR_UTILS_EVAL_UTILS_H_ 70