1 /* Copyright 2020 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_PYTHON_PY_EXECUTABLE_H_ 17 #define TENSORFLOW_COMPILER_XLA_PYTHON_PY_EXECUTABLE_H_ 18 19 #include <memory> 20 #include <string> 21 #include <utility> 22 #include <vector> 23 24 #include "absl/types/span.h" 25 #include "tensorflow/compiler/xla/pjrt/pjrt_client.h" 26 #include "tensorflow/compiler/xla/python/py_buffer.h" 27 #include "tensorflow/compiler/xla/python/py_client.h" 28 #include "tensorflow/compiler/xla/python/traceback.h" 29 #include "tensorflow/compiler/xla/statusor.h" 30 #include "tensorflow/compiler/xla/types.h" 31 32 namespace xla { 33 34 // Python wrapper around PjRtExecutable. We use a wrapper class: 35 // a) to keep the PyClient alive via a std::shared_ptr<> 36 // b) to add Python-specific functionality. 37 class PyExecutable : public std::enable_shared_from_this<PyExecutable> { 38 public: 39 PyExecutable(std::shared_ptr<PyClient> client, 40 std::unique_ptr<PjRtExecutable> executable, 41 std::shared_ptr<Traceback> traceback, 42 absl::optional<std::string> fingerprint); 43 ~PyExecutable(); 44 client()45 std::shared_ptr<PyClient> client() const { return client_; } 46 47 absl::Span<const PjRtExecutable::LogicalDeviceIds> addressable_device_logical_ids()48 addressable_device_logical_ids() const { 49 return executable_->addressable_device_logical_ids(); 50 } 51 52 std::vector<ClientAndPtr<PjRtDevice>> AddressableDevices() const; 53 SizeOfGeneratedCodeInBytes()54 int64 SizeOfGeneratedCodeInBytes() const { 55 return executable_->SizeOfGeneratedCodeInBytes(); 56 } 57 Delete()58 void Delete() { return executable_->Delete(); } 59 is_deleted()60 bool is_deleted() { return executable_->IsDeleted(); } 61 62 StatusOr<std::vector<PyBuffer::object>> Execute( 63 absl::Span<PyBuffer::object const> args); 64 65 // Takes args indexed by argid then deviceid, transposes them, and passes to 66 // PjRtExecutable::Execute. The result is similarly transposed back into the 67 // argid,deviceid format. 68 // args is [num_args x num_devices]. 69 StatusOr<std::vector<std::vector<PyBuffer::object>>> 70 ExecuteShardedOnLocalDevices( 71 absl::Span<const std::vector<PyBuffer::object>> args); 72 73 StatusOr<std::vector<std::shared_ptr<HloModule>>> HloModules() const; 74 traceback()75 Traceback* traceback() { return traceback_.get(); } 76 pjrt_executable()77 const PjRtExecutable& pjrt_executable() const { return *executable_; } 78 mutable_pjrt_executable()79 PjRtExecutable* mutable_pjrt_executable() const { return executable_.get(); } options()80 const ExecuteOptions& options() const { return options_; } fingerprint()81 const absl::optional<std::string>& fingerprint() const { 82 return fingerprint_; 83 } 84 85 // Keep `obj` alive as long as PyExecutable. 86 void KeepAlive(pybind11::object obj); 87 88 private: 89 friend class PyClient; 90 91 std::shared_ptr<PyClient> client_; 92 std::unique_ptr<PjRtExecutable> executable_; 93 std::shared_ptr<Traceback> traceback_; 94 95 // Identical executables (i.e. representing the same program) will have the 96 // same fingerprint. nullopt on platforms or executables where fingerprints 97 // aren't implemented. 98 absl::optional<std::string> fingerprint_; 99 100 // The options to pass to `executable_.Execute`. 101 ExecuteOptions options_; 102 103 // Python objects to keep alive as requested by user. 104 std::vector<pybind11::object> keepalives_; 105 106 // Doubly-linked list of all executables known to the client. Protected by the 107 // GIL. 108 PyExecutable* next_; 109 PyExecutable* prev_; 110 }; 111 112 } // namespace xla 113 114 #endif // TENSORFLOW_COMPILER_XLA_PYTHON_PY_EXECUTABLE_H_ 115