• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // In order to avoid the overhead of creating a large thread pool, we set a
30 // small default thread count. This value should be revised should DeviceSimple
31 // be used to evaluate nodes with a large degree of intra-op parallelism.
32 const int kDeviceSimpleThreads = 2;
33 
DeviceSimple()34 DeviceSimple::DeviceSimple() : DeviceBase(Env::Default()) {
35   eigen_worker_threads_.num_threads = kDeviceSimpleThreads;
36   eigen_worker_threads_.workers = new thread::ThreadPool(
37       Env::Default(), "evaluation_utils", eigen_worker_threads_.num_threads);
38   eigen_device_.reset(new Eigen::ThreadPoolDevice(
39       eigen_worker_threads_.workers->AsEigenThreadPool(),
40       eigen_worker_threads_.num_threads));
41   set_tensorflow_cpu_worker_threads(&eigen_worker_threads_);
42   set_eigen_cpu_device(eigen_device_.get());
43 }
44 
~DeviceSimple()45 DeviceSimple::~DeviceSimple() {
46   eigen_device_.reset();
47   delete eigen_worker_threads_.workers;
48 }
49 
MakeTensorFromProto(const TensorProto & tensor_proto,const AllocatorAttributes alloc_attrs,Tensor * tensor)50 Status DeviceSimple::MakeTensorFromProto(const TensorProto& tensor_proto,
51                                          const AllocatorAttributes alloc_attrs,
52                                          Tensor* tensor) {
53   Tensor parsed(tensor_proto.dtype());
54   if (!parsed.FromProto(cpu_allocator(), tensor_proto)) {
55     return errors::InvalidArgument("Cannot parse tensor from tensor_proto.");
56   }
57   *tensor = parsed;
58   return Status::OK();
59 }
60 
EvaluateNode(const NodeDef & node,const TensorVector & inputs,DeviceBase * cpu_device,ResourceMgr * resource_mgr,TensorVector * output)61 Status EvaluateNode(const NodeDef& node, const TensorVector& inputs,
62                     DeviceBase* cpu_device, ResourceMgr* resource_mgr,
63                     TensorVector* output) {
64   Status status;
65   std::unique_ptr<DeviceBase> device;
66   if (cpu_device == nullptr) {
67     device.reset(new DeviceSimple());
68     cpu_device = device.get();
69   }
70 
71   std::unique_ptr<OpKernel> op_kernel(
72       CreateOpKernel("CPU", cpu_device, cpu_device->GetAllocator({}), node,
73                      TF_GRAPH_DEF_VERSION, &status));
74   TF_RETURN_IF_ERROR(status);
75   OpKernelContext::Params params;
76   params.device = cpu_device;
77   params.frame_iter = FrameAndIter(0, 0);
78   params.inputs = &inputs;
79   params.op_kernel = op_kernel.get();
80   params.resource_manager = resource_mgr;
81 
82   gtl::InlinedVector<AllocatorAttributes, 4> output_attrs;
83   const int num_outputs = op_kernel->num_outputs();
84   for (int i = 0; i < num_outputs; i++) {
85     AllocatorAttributes attr;
86     attr.set_on_host(true);
87     output_attrs.push_back(attr);
88   }
89   params.output_attr_array = output_attrs.data();
90 
91   OpKernelContext op_context(&params);
92   op_kernel->Compute(&op_context);
93   for (int i = 0; i < num_outputs; i++) {
94     output->push_back(op_context.release_output(i));
95   }
96   return op_context.status();
97 }
98 
99 }  // end namespace grappler
100 }  // end namespace tensorflow
101