1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use pjrt_sys::protos::xla::{
CompilationEnvironmentsProto, CompileOptionsProto, ExecutableBuildOptionsProto,
};
use prost::Message;
use crate::{Client, Executable, LoadedExecutable, Result, TopologyDescription};
pub trait CompileToExecutable<T> {
fn compile(
&self,
program: &T,
topology: &TopologyDescription,
options: &CompileOptions,
client: Option<&Client>,
) -> Result<Executable>;
}
pub trait CompileToLoadedExecutable<T> {
fn compile(&self, program: &T, options: &CompileOptions) -> Result<LoadedExecutable>;
}
#[derive(Debug, Clone)]
pub struct CompileOptions {
proto: CompileOptionsProto,
}
impl Default for CompileOptions {
fn default() -> Self {
Self::new()
}
}
impl CompileOptions {
pub fn new() -> Self {
let v = Self {
proto: CompileOptionsProto::default(),
};
v.executable_build_options(ExecutableBuildOptions::new())
}
pub fn proto(&self) -> &CompileOptionsProto {
&self.proto
}
pub fn proto_mut(&mut self) -> &mut CompileOptionsProto {
&mut self.proto
}
pub fn executable_build_options(
mut self,
options: impl Into<Option<ExecutableBuildOptions>>,
) -> Self {
self.proto.executable_build_options = options.into().map(|v| v.proto);
self
}
pub fn encode(&self) -> Vec<u8> {
self.proto.encode_to_vec()
}
}
#[derive(Debug, Clone)]
pub struct ExecutableBuildOptions {
proto: ExecutableBuildOptionsProto,
}
impl ExecutableBuildOptions {
pub fn new() -> Self {
let proto = ExecutableBuildOptionsProto {
device_ordinal: -1,
num_partitions: 1,
num_replicas: 1,
..Default::default()
};
Self { proto }
}
pub fn proto(&self) -> &ExecutableBuildOptionsProto {
&self.proto
}
pub fn proto_mut(&mut self) -> &mut ExecutableBuildOptionsProto {
&mut self.proto
}
/// If set, this is the device to build the computation for. Valid
/// device_ordinal values are: 0 to # of devices - 1. These values are
/// identical to the device ordinal values used by StreamExecutor. The built
/// executable will be executable on any device equivalent to the specified
/// device as determined by Backend::devices_equivalent(). A value of -1
/// indicates this option has not been set.
pub fn device_ordinal(mut self, device_ordinal: i64) -> Self {
self.proto.device_ordinal = device_ordinal;
self
}
/// The number of replicas of this computation that are to be executed.
pub fn num_partitions(mut self, num_partitions: i64) -> Self {
self.proto.num_partitions = num_partitions;
self
}
/// The number of partitions in this computation.
pub fn num_replicas(mut self, num_replicas: i64) -> Self {
self.proto.num_replicas = num_replicas;
self
}
/// Indicates whether to use SPMD (true) or MPMD (false) partitioning when
/// num_partitions > 1 and XLA is requested to partition the input program.
pub fn use_spmd_partitioning(mut self, use_spmd_partitioning: bool) -> Self {
self.proto.use_spmd_partitioning = use_spmd_partitioning;
self
}
/// Whether to automatically generate XLA shardings for SPMD partitioner.
pub fn use_auto_spmd_partitioning(mut self, use_auto_spmd_partitioning: bool) -> Self {
self.proto.use_auto_spmd_partitioning = use_auto_spmd_partitioning;
self
}
/// Whether HLOs should be deduplicated.
pub fn deduplicate_hlo(mut self, deduplicate_hlo: bool) -> Self {
self.proto.deduplicate_hlo = deduplicate_hlo;
self
}
/// Whether input and output buffers are aliased if the associated parameter is
/// passed-through XLA modules without being changed.
pub fn alias_passthrough_params(mut self, alias_passthrough_params: bool) -> Self {
self.proto.alias_passthrough_params = alias_passthrough_params;
self
}
/// By default, XLA builds an executable by invoking standard compilation, i.e.
/// running Compiler::Compile, or both Compiler::RunHloPasses and
/// Compiler::RunBackend. When run_backend_only is set to true, XLA builds an
/// executable by invoking only RunBackend and skip invoking RunHloPasses,
/// which can be used to compile post-optimizations HLO modules.
pub fn run_backend_only(mut self, run_backend_only: bool) -> Self {
self.proto.run_backend_only = run_backend_only;
self
}
/// Allows sharding propagation to propagate to the parameters. This changes
/// the input shape of the computation (which is undesirable), but it can be
/// used to allow to run partial compilation to determine what would be the
/// input sharding of a computation if XLA would be allowed to propagate the
/// sharding which can be used by higher level framework as a way to query
/// intermediate sharding of operations when multiple computation would be
/// chained and merged together.
/// This is a vector of bool, because the user can control which parameters can
/// have the sharding substituted. If only one boolean value is passed in the
/// vector that is interpreted as the value to be applied for every parameter.
pub fn allow_spmd_sharding_propagation_to_parameters(
mut self,
allow_spmd_sharding_propagation_to_parameters: Vec<bool>,
) -> Self {
self.proto.allow_spmd_sharding_propagation_to_parameters =
allow_spmd_sharding_propagation_to_parameters;
self
}
/// Allows sharding propagation to propagate to the outputs. This changes the
/// output shape of the computation (which is undesirable), but it can be used
/// to allow to run partial compilation to determine what would be the output
/// sharding of a computation if XLA would be allowed to propagate the sharding
/// which can be used by higher level framework as a way to query intermediate
/// sharding of operations when multiple computation would be chained and
/// merged together.
/// This is a vector of bool, because the user can control (if the output of
/// the computation is a tuple) which elements of the tuple can have the
/// sharding substituted and which don't. If only one boolean value is passed
/// in the vector that's interpreted as the value to be applied for every
/// single element of the output tuple. One value per element of the tuple
/// means that each value is attached to one of the output elements.
pub fn allow_spmd_sharding_propagation_to_output(
mut self,
allow_spmd_sharding_propagation_to_output: Vec<bool>,
) -> Self {
self.proto.allow_spmd_sharding_propagation_to_output =
allow_spmd_sharding_propagation_to_output;
self
}
pub fn device_memory_size(mut self, device_memory_size: i64) -> Self {
self.proto.device_memory_size = device_memory_size;
self
}
/// Mesh shape in auto sharding options.
pub fn auto_spmd_partitioning_mesh_shape(
mut self,
auto_spmd_partitioning_mesh_shape: Vec<i64>,
) -> Self {
self.proto.auto_spmd_partitioning_mesh_shape = auto_spmd_partitioning_mesh_shape;
self
}
/// Mesh ids in auto sharding options.
pub fn auto_spmd_partitioning_mesh_ids(
mut self,
auto_spmd_partitioning_mesh_ids: Vec<i64>,
) -> Self {
self.proto.auto_spmd_partitioning_mesh_ids = auto_spmd_partitioning_mesh_ids;
self
}
/// Use Shardy, a new partitioner, to replace the existing
/// ShardingPropagation and SpmdPartitioner.
pub fn use_shardy_partitioner(mut self, use_shardy_partitioner: bool) -> Self {
self.proto.use_shardy_partitioner = use_shardy_partitioner;
self
}
/// Expose access to the XLA debug options which will be passed to the
/// compilation process.
pub fn debug_options(mut self, debug_options: impl Into<Option<DebugOptions>>) -> Self {
self.proto.debug_options = debug_options.into().map(|v| v.proto);
self
}
/// Expose access to the XLA compilation environments, which will be passed to
/// the compilation process.
pub fn comp_envs(mut self, comp_envs: impl Into<Option<CompilationEnvironments>>) -> Self {
self.proto.comp_envs = comp_envs.into().map(|v| v.proto);
self
}
pub fn encode(&self) -> Vec<u8> {
self.proto.encode_to_vec()
}
}
impl Default for ExecutableBuildOptions {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct DebugOptions {
proto: pjrt_sys::protos::xla::DebugOptions,
}
impl DebugOptions {
pub fn new() -> Self {
Self {
proto: pjrt_sys::protos::xla::DebugOptions::default(),
}
}
pub fn proto(&self) -> &pjrt_sys::protos::xla::DebugOptions {
&self.proto
}
pub fn proto_mut(&mut self) -> &mut pjrt_sys::protos::xla::DebugOptions {
&mut self.proto
}
pub fn encode(&self) -> Vec<u8> {
self.proto.encode_to_vec()
}
}
impl Default for DebugOptions {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct CompilationEnvironments {
proto: CompilationEnvironmentsProto,
}
impl CompilationEnvironments {
pub fn new() -> Self {
Self {
proto: CompilationEnvironmentsProto::default(),
}
}
pub fn proto(&self) -> &CompilationEnvironmentsProto {
&self.proto
}
pub fn proto_mut(&mut self) -> &mut CompilationEnvironmentsProto {
&mut self.proto
}
pub fn encode(&self) -> Vec<u8> {
self.proto.encode_to_vec()
}
}
impl Default for CompilationEnvironments {
fn default() -> Self {
Self::new()
}
}