Home | History | Annotate | Download | only in service
      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_XLA_SERVICE_SERVICE_EXECUTABLE_RUN_OPTIONS_H_
     17 #define TENSORFLOW_COMPILER_XLA_SERVICE_SERVICE_EXECUTABLE_RUN_OPTIONS_H_
     18 
     19 #include "tensorflow/compiler/xla/executable_run_options.h"
     20 #include "tensorflow/compiler/xla/service/pool.h"
     21 #include "tensorflow/compiler/xla/statusor.h"
     22 #include "tensorflow/stream_executor/stream_executor.h"
     23 
     24 namespace xla {
     25 
     26 // Class containing options for running a LocalExecutable and other auxiliary
     27 // data, now only a stream cache for GPU backend.
     28 class ServiceExecutableRunOptions {
     29  public:
     30   using StreamBorrower =
     31       std::function<StatusOr<Pool<perftools::gputools::Stream>::SmartPtr>(int)>;
     32 
     33   ServiceExecutableRunOptions()
     34       : ServiceExecutableRunOptions(ExecutableRunOptions()) {}
     35 
     36   explicit ServiceExecutableRunOptions(
     37       ExecutableRunOptions run_options, StreamBorrower borrow_stream = nullptr,
     38       tensorflow::thread::ThreadPool* xla_intra_op_thread_pool = nullptr)
     39       : run_options_(std::move(run_options)),
     40         borrow_stream_(std::move(borrow_stream)),
     41         xla_intra_op_thread_pool_(xla_intra_op_thread_pool) {}
     42 
     43   // Returns reference or pointer to `ExecutableRunOptions` member.
     44   const ExecutableRunOptions& run_options() const { return run_options_; }
     45   ExecutableRunOptions* mutable_run_options() { return &run_options_; }
     46 
     47   // Delegate to `ExecutableRunOptions` member.
     48   perftools::gputools::Stream* stream() const { return run_options_.stream(); }
     49   DeviceMemoryAllocator* allocator() const { return run_options_.allocator(); }
     50   int device_ordinal() const { return run_options_.device_ordinal(); }
     51 
     52   // Borrows a stream and returns a smart pointer which returns the stream on
     53   // destruction.
     54   StatusOr<Pool<perftools::gputools::Stream>::SmartPtr> BorrowStream(
     55       int device_ordinal) const {
     56     return borrow_stream_
     57                ? borrow_stream_(device_ordinal)
     58                : Status(tensorflow::error::UNIMPLEMENTED, "No stream cache");
     59   }
     60 
     61   // Returns reference to thread pool for execution of XLA ops on CPU backend.
     62   tensorflow::thread::ThreadPool* xla_intra_op_thread_pool() const {
     63     return xla_intra_op_thread_pool_;
     64   }
     65 
     66  private:
     67   ExecutableRunOptions run_options_;
     68   StreamBorrower borrow_stream_;
     69   tensorflow::thread::ThreadPool* xla_intra_op_thread_pool_;
     70 };
     71 
     72 }  // namespace xla
     73 
     74 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_SERVICE_EXECUTABLE_RUN_OPTIONS_H_
     75