1 /* Copyright 2018 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 #include "tensorflow/core/grappler/optimizers/evaluation_utils.h"
17
18 #include "tensorflow/core/framework/tensor.pb.h"
19 #include "tensorflow/core/lib/core/threadpool.h"
20 #include "tensorflow/core/platform/cpu_info.h"
21 #include "tensorflow/core/platform/denormal.h"
22 #include "tensorflow/core/platform/setround.h"
23 #include "tensorflow/core/public/version.h"
24
25 namespace tensorflow {
26 namespace grappler {
27 using TensorVector = gtl::InlinedVector<TensorValue, 4>;
28
29 namespace {
30 class EigenThreadPoolWrapper : public Eigen::ThreadPoolInterface {
31 public:
EigenThreadPoolWrapper(thread::ThreadPool * pool)32 explicit EigenThreadPoolWrapper(thread::ThreadPool* pool) : pool_(pool) {}
~EigenThreadPoolWrapper()33 ~EigenThreadPoolWrapper() override {}
Schedule(std::function<void ()> fn)34 void Schedule(std::function<void()> fn) override {
35 auto wrapped = [=]() {
36 // TensorFlow flushes denormals to zero and rounds to nearest, so we do
37 // the same here.
38 port::ScopedFlushDenormal flush;
39 port::ScopedSetRound round(FE_TONEAREST);
40 fn();
41 };
42 pool_->Schedule(std::move(wrapped));
43 }
NumThreads() const44 int NumThreads() const override { return pool_->NumThreads(); }
CurrentThreadId() const45 int CurrentThreadId() const override { return pool_->CurrentThreadId(); }
46
47 private:
48 thread::ThreadPool* pool_ = nullptr;
49 };
50
51 } // namespace
52
DeviceSimple()53 DeviceSimple::DeviceSimple() : DeviceBase(Env::Default()) {
54 eigen_worker_threads_.num_threads = port::NumSchedulableCPUs();
55 eigen_worker_threads_.workers = new thread::ThreadPool(
56 Env::Default(), "evaluation_utils", eigen_worker_threads_.num_threads);
57 eigen_threadpool_wrapper_.reset(
58 new EigenThreadPoolWrapper(eigen_worker_threads_.workers));
59 eigen_device_.reset(new Eigen::ThreadPoolDevice(
60 eigen_threadpool_wrapper_.get(), eigen_worker_threads_.num_threads));
61 set_tensorflow_cpu_worker_threads(&eigen_worker_threads_);
62 set_eigen_cpu_device(eigen_device_.get());
63 }
64
~DeviceSimple()65 DeviceSimple::~DeviceSimple() {
66 eigen_threadpool_wrapper_.reset();
67 eigen_device_.reset();
68 delete eigen_worker_threads_.workers;
69 }
70
MakeTensorFromProto(const TensorProto & tensor_proto,const AllocatorAttributes alloc_attrs,Tensor * tensor)71 Status DeviceSimple::MakeTensorFromProto(const TensorProto& tensor_proto,
72 const AllocatorAttributes alloc_attrs,
73 Tensor* tensor) {
74 Tensor parsed(tensor_proto.dtype());
75 if (!parsed.FromProto(cpu_allocator(), tensor_proto)) {
76 return errors::InvalidArgument("Cannot parse tensor from tensor_proto.");
77 }
78 *tensor = parsed;
79 return Status::OK();
80 }
81
EvaluateNode(const NodeDef & node,const TensorVector & inputs,DeviceBase * cpu_device,ResourceMgr * resource_mgr,TensorVector * output)82 Status EvaluateNode(const NodeDef& node, const TensorVector& inputs,
83 DeviceBase* cpu_device, ResourceMgr* resource_mgr,
84 TensorVector* output) {
85 Status status;
86 std::unique_ptr<DeviceBase> device;
87 if (cpu_device == nullptr) {
88 device.reset(new DeviceSimple());
89 cpu_device = device.get();
90 }
91
92 std::unique_ptr<OpKernel> op_kernel(
93 CreateOpKernel("CPU", cpu_device, cpu_device->GetAllocator({}), node,
94 TF_GRAPH_DEF_VERSION, &status));
95 TF_RETURN_IF_ERROR(status);
96 OpKernelContext::Params params;
97 params.device = cpu_device;
98 params.frame_iter = FrameAndIter(0, 0);
99 params.inputs = &inputs;
100 params.op_kernel = op_kernel.get();
101 params.resource_manager = resource_mgr;
102
103 gtl::InlinedVector<AllocatorAttributes, 4> output_attrs;
104 const int num_outputs = op_kernel->num_outputs();
105 for (int i = 0; i < num_outputs; i++) {
106 AllocatorAttributes attr;
107 attr.set_on_host(true);
108 output_attrs.push_back(attr);
109 }
110 params.output_attr_array = output_attrs.data();
111
112 OpKernelContext op_context(¶ms);
113 op_kernel->Compute(&op_context);
114 for (int i = 0; i < num_outputs; i++) {
115 output->push_back(op_context.release_output(i));
116 }
117 return op_context.status();
118 }
119
120 } // end namespace grappler
121 } // end namespace tensorflow
122