Home | History | Annotate | Download | only in jit
      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 // Registers the XLA_GPU device, which is an XlaDevice instantiation that runs
     17 // operators using XLA via the XLA "CUDA" (GPU) backend.
     18 
     19 #include "tensorflow/compiler/jit/kernels/xla_launch_op.h"
     20 #include "tensorflow/compiler/jit/xla_device.h"
     21 #include "tensorflow/compiler/jit/xla_device_ops.h"
     22 #include "tensorflow/compiler/tf2xla/xla_op_registry.h"
     23 #include "tensorflow/core/common_runtime/device_factory.h"
     24 #include "tensorflow/core/lib/core/status.h"
     25 
     26 namespace tensorflow {
     27 
     28 class XlaGpuDeviceFactory : public DeviceFactory {
     29  public:
     30   Status CreateDevices(const SessionOptions& options, const string& name_prefix,
     31                        std::vector<Device*>* devices) override;
     32 };
     33 
     34 Status XlaGpuDeviceFactory::CreateDevices(const SessionOptions& options,
     35                                           const string& name_prefix,
     36                                           std::vector<Device*>* devices) {
     37   static XlaDeviceOpRegistrations* registrations =
     38       RegisterXlaDeviceKernels(DEVICE_XLA_GPU, DEVICE_GPU_XLA_JIT);
     39   (void)registrations;
     40 
     41   std::unique_ptr<XlaDevice> device;
     42   Status status = XlaDevice::Create(
     43       "CUDA", DEVICE_XLA_GPU, 0, DEVICE_GPU_XLA_JIT, options, name_prefix,
     44       /*register_device_for_compilation=*/true, &device);
     45   if (!status.ok()) {
     46     // Treat failures as non-fatal; there might not be a GPU in the machine.
     47     VLOG(1) << "Failed to create XLA_GPU device: " << status;
     48     return Status::OK();
     49   }
     50   devices->push_back(device.release());
     51   return Status::OK();
     52 }
     53 
     54 REGISTER_LOCAL_DEVICE_FACTORY(DEVICE_XLA_GPU, XlaGpuDeviceFactory);
     55 
     56 // Kernel registrations
     57 
     58 constexpr std::array<DataType, 6> kAllXlaGpuTypes = {
     59     {DT_INT32, DT_INT64, DT_FLOAT, DT_DOUBLE, DT_COMPLEX64, DT_BOOL}};
     60 
     61 REGISTER_XLA_LAUNCH_KERNEL(DEVICE_XLA_GPU, XlaLocalLaunchOp, kAllXlaGpuTypes);
     62 REGISTER_XLA_DEVICE_KERNELS(DEVICE_XLA_GPU, kAllXlaGpuTypes);
     63 
     64 }  // namespace tensorflow
     65