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_LOCAL_SERVICE_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_LOCAL_SERVICE_H_ 18 19 #include <memory> 20 #include <vector> 21 22 #include "absl/types/span.h" 23 #include "tensorflow/compiler/xla/client/executable_build_options.h" 24 #include "tensorflow/compiler/xla/client/xla_computation.h" 25 #include "tensorflow/compiler/xla/service/backend.h" 26 #include "tensorflow/compiler/xla/service/compiler.h" 27 #include "tensorflow/compiler/xla/service/executable.h" 28 #include "tensorflow/compiler/xla/service/service.h" 29 #include "tensorflow/compiler/xla/service/shaped_buffer.h" 30 #include "tensorflow/compiler/xla/statusor.h" 31 #include "tensorflow/compiler/xla/xla_data.pb.h" 32 #include "tensorflow/core/platform/stream_executor_no_cuda.h" 33 #include "tensorflow/stream_executor/device_memory_allocator.h" 34 35 namespace xla { 36 37 // Service implementation that extends the XLA Service to leverage running 38 // in the same process as the client. 39 class LocalService : public Service { 40 public: 41 // Factory for creating a LocalService. 42 static StatusOr<std::unique_ptr<LocalService>> NewService( 43 const ServiceOptions& options); 44 45 // Builds Executables with the given XlaComputation, argument layouts and 46 // options. If result_layout is non-null, then the executable is compiled to 47 // produce a result of the given layout. If device_allocator is non-null, 48 // then the compiler may use it to allocate temp space on the device. The 49 // compiler is responsible for freeing any memory it allocates this way. 50 StatusOr<std::vector<std::unique_ptr<Executable>>> CompileExecutables( 51 const XlaComputation& computation, 52 const absl::Span<const Shape* const> argument_layouts, 53 const ExecutableBuildOptions& build_options); 54 55 // Returns the device ordinal that corresponds to the given replica number. 56 // 57 // This returns an error if there is not a one-to-one correspondence of 58 // replicas to device ordinals, but is useful as a short term mechanism for 59 // the "easy" case where a single replica is a single device. 60 StatusOr<int> ReplicaNumberToDeviceOrdinal(int replica_number); 61 62 // Converts a GlobalDataHandle into a pointer to a ShapedBuffer that's valid 63 // as long as the handle is valid. 64 StatusOr<const ShapedBuffer*> GlobalDataToShapedBuffer( 65 const GlobalDataHandle& data, int replica_number); 66 67 // Registers a vector of shaped buffers of device memory, one per replica, and 68 // returns a corresponding handle that can be used for talking to XLA clients. 69 StatusOr<GlobalDataHandle> RegisterReplicatedBuffers( 70 std::vector<ScopedShapedBuffer> replicated_buffers, const string& tag); 71 72 private: 73 explicit LocalService(const ServiceOptions& options, 74 std::unique_ptr<Backend> backend); 75 LocalService(const LocalService&) = delete; 76 void operator=(const LocalService&) = delete; 77 }; 78 79 } // namespace xla 80 81 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_LOCAL_SERVICE_H_ 82