1 /* Copyright 2017 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_COMPILER_TF2XLA_XLA_COMPILATION_DEVICE_H_
17 #define TENSORFLOW_COMPILER_TF2XLA_XLA_COMPILATION_DEVICE_H_
18 
19 #include <memory>
20 
21 #include "tensorflow/core/common_runtime/local_device.h"
22 #include "tensorflow/core/framework/device_base.h"
23 #include "tensorflow/core/framework/tensor.h"
24 #include "tensorflow/core/lib/core/status.h"
25 #include "tensorflow/core/platform/mem.h"
26 #include "tensorflow/core/public/session_options.h"
27 
28 namespace tensorflow {
29 
30 // Class is defined in xla_compilation_device.cc, reference
31 // included here only so the XlaCompilationDevice allocator_ member can be
32 // declared.
33 class XlaCompilationAllocator;
34 
35 // This is a 'dummy' TensorFlow device that is only used to execute a
36 // subgraph of XLA compilation Ops to construct a compiled version
37 // of the subgraph's computation. It has a 'dummy' allocator that
38 // backs each Tensor with an XlaExpression. The shape of the Tensor
39 // matches the shape of XlaExpression.
40 //
41 // We deliberately don't register a device factory because we *never*
42 // want placement to put Ops on a compilation device. The device is created
43 // manually, not using a factory.
44 //
45 // XLA compilation is not thread-safe. OpKernels registered on the
46 // XlaCompilationDevice must not use threads or concurrency.
47 class XlaCompilationDevice : public LocalDevice {
48  public:
49   XlaCompilationDevice(const SessionOptions& options, DeviceType type);
50 
51   ~XlaCompilationDevice() override;
52 
53   Allocator* GetAllocator(AllocatorAttributes attr) override;
54 
55   void Compute(OpKernel* op_kernel, OpKernelContext* context) override;
56 
57   Status Sync() override;
58 
59   Status MakeTensorFromProto(const TensorProto& tensor_proto,
60                              const AllocatorAttributes alloc_attrs,
61                              Tensor* tensor) override;
62 
63  private:
64   std::unique_ptr<XlaCompilationAllocator> allocator_;
65 };
66 
67 }  // namespace tensorflow
68 
69 #endif  // TENSORFLOW_COMPILER_TF2XLA_XLA_COMPILATION_DEVICE_H_
70