• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <utility>
21 #include <vector>
22 
23 #include "absl/types/span.h"
24 #include "tensorflow/compiler/xla/pjrt/pjrt_client.h"
25 #include "tensorflow/compiler/xla/python/py_buffer.h"
26 #include "tensorflow/compiler/xla/python/py_client.h"
27 #include "tensorflow/compiler/xla/python/traceback.h"
28 #include "tensorflow/compiler/xla/statusor.h"
29 #include "tensorflow/compiler/xla/types.h"
30 
31 namespace xla {
32 
33 // Python wrapper around PjRtExecutable. We use a wrapper class:
34 // a) to keep the PyClient alive via a std::shared_ptr<>
35 // b) to add Python-specific functionality.
36 class PyExecutable {
37  public:
38   PyExecutable(std::shared_ptr<PyClient> client,
39                std::unique_ptr<PjRtExecutable> executable,
40                std::shared_ptr<Traceback> traceback,
41                absl::optional<std::string> fingerprint);
42   ~PyExecutable();
43 
client()44   std::shared_ptr<PyClient> client() const { return client_; }
45 
46   absl::Span<const PjRtExecutable::LogicalDeviceIds>
addressable_device_logical_ids()47   addressable_device_logical_ids() const {
48     return executable_->addressable_device_logical_ids();
49   }
50 
51   std::vector<ClientAndPtr<PjRtDevice>> AddressableDevices() const;
52 
SizeOfGeneratedCodeInBytes()53   int64 SizeOfGeneratedCodeInBytes() const {
54     return executable_->SizeOfGeneratedCodeInBytes();
55   }
56 
Delete()57   void Delete() { return executable_->Delete(); }
58 
59   StatusOr<std::vector<std::unique_ptr<PyBuffer>>> Execute(
60       absl::Span<PyBuffer* const> args);
61 
62   // Same as above, but take as inputs `PjRtBuffer*`. Only targets C++ code.
63   StatusOr<std::vector<std::unique_ptr<PyBuffer>>> PjRtExecute(
64       const std::vector<PjRtBuffer*>& args);
65 
66   // TODO(parkers): Remove in favor of `ExecuteShardedOnLocalDevices`.
67   StatusOr<std::vector<std::vector<std::unique_ptr<PyBuffer>>>>
68   ExecuteOnLocalDevices(absl::Span<const std::vector<PyBuffer*>> args);
69 
70   // Takes args indexed by argid then deviceid, transposes them, and passes to
71   // PjRtExecutable::Execute. The result is similarly transposed back into the
72   // argid,deviceid format.
73   StatusOr<std::vector<std::vector<std::unique_ptr<PyBuffer>>>>
74   ExecuteShardedOnLocalDevices(absl::Span<const std::vector<PyBuffer*>> args);
75 
76   StatusOr<std::vector<std::shared_ptr<HloModule>>> HloModules() const;
77 
traceback()78   Traceback* traceback() { return traceback_.get(); }
79 
pjrt_executable()80   const PjRtExecutable& pjrt_executable() const { return *executable_; }
81 
82  private:
83   friend class PyClient;
84 
85   std::shared_ptr<PyClient> client_;
86   std::unique_ptr<PjRtExecutable> executable_;
87   std::shared_ptr<Traceback> traceback_;
88 
89   // Identical executables (i.e. representing the same program) will have the
90   // same fingerprint. nullopt on platforms or executables where fingerprints
91   // aren't implemented.
92   absl::optional<std::string> fingerprint_;
93 
94   // The options to pass to `executable_.Execute`.
95   ExecuteOptions options_;
96 
97   // Doubly-linked list of all executables known to the client. Protected by the
98   // GIL.
99   PyExecutable* next_;
100   PyExecutable* prev_;
101 };
102 
103 }  // namespace xla
104 
105 #endif  // TENSORFLOW_COMPILER_XLA_PYTHON_PY_EXECUTABLE_H_
106