-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhlo.py
More file actions
391 lines (368 loc) · 14.2 KB
/
hlo.py
File metadata and controls
391 lines (368 loc) · 14.2 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# Copyright (c) 2025 ByteDance Ltd. and/or its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Union
import numpy as np
from torch_neuronx.pyhlo import xla_data_pb2
from torch_neuronx.pyhlo.service.hlo_pb2 import (
HloComputationProto,
HloInstructionProto,
HloModuleProto,
)
from torch_neuronx.pyhlo.xla_data_pb2 import LiteralProto
from entangle.pgraph.pickleable import *
TO_TORCH_DTYPE = {
xla_data_pb2.PRED: torch.bool,
xla_data_pb2.F32: torch.float32,
xla_data_pb2.F16: torch.float16,
xla_data_pb2.S32: torch.int32,
xla_data_pb2.S64: torch.int64,
}
TO_FX_TARGET_NAME = {
"add": "aten.add.Tensor",
"all_reduce": "_c10d_functional.all_reduce.default",
"and": "aten.bitwise_and.Tensor",
"arange": "aten.arange.start",
"clone": "aten.clone.default",
"concatenate": "aten.cat.default",
"cosine": "aten.cos.default",
"divide": "aten.div.Tensor",
"empty": "aten.empty.memory_format",
"exponential": "aten.exp.default",
"maximum": "aten.maximum",
"multiply": "aten.mul.Tensor",
"ge.Tensor": "aten.ge.Tensor",
"le.Tensor": "aten.le.Tensor",
"ones": "aten.ones.default",
"reduce_sum": "aten.sum.dim_IntList",
"reshape": "aten.view.default",
"sine": "aten.sin.default",
"slice": "aten.slice.Tensor",
"subtract": "aten.sub.Tensor",
"transpose": "aten.permute.default",
"zeros": "aten.zeros.default",
}
def get_reduce_op(computation: HloComputationProto) -> str:
if len(computation.instructions) != 3:
raise NotImplementedError(f"{computation.instructions=} not implemented")
assert computation.instructions[0].opcode == "parameter"
assert computation.instructions[1].opcode == "parameter"
assert computation.instructions[2].opcode in ("add", "maximum")
op = computation.instructions[2].opcode
return op
def convert_reduce(
instr: HloInstructionProto,
args: list[PickleableNode],
id_to_pnode: dict[int, PickleableNode],
id_to_computation: dict[int, HloComputationProto],
) -> tuple[str, list[int], dict]:
"""
Returns (target, shape, args, kwargs)
"""
dims = instr.dimensions
if len(dims) != 1:
raise NotImplementedError(f"{dims=} not implemented for reduce")
dim = dims[0]
inputs = id_to_pnode[instr.operand_ids[0]]
init_values = id_to_pnode[instr.operand_ids[1]]
assert len(instr.called_computation_ids) == 1
computation = id_to_computation[instr.called_computation_ids[0]]
op = get_reduce_op(computation)
if op == "add":
if init_values == 0.0:
# 'aten.sum.dim_IntList', args=('n__r0__fw_values', [-1], keep_dims=False)
target = "reduce_sum"
args = [inputs, [dim], False]
return (target, args, {})
else:
raise NotImplementedError(
f"{op=} with {init_values=} not implemented for reduce"
)
elif op == "maximum":
if init_values == -float("inf"):
# 'aten.max.dim', args=('n__r0__fw_values', [-1], keep_dims=False)
target = "reduce_max"
args = [inputs, dim]
return (target, args, {})
else:
raise NotImplementedError(
f"{op=} with {init_values=} not implemented for reduce"
)
else:
raise NotImplementedError(f"{op=} not implemented for reduce")
def convert_all_reduce(
instr: HloInstructionProto,
args: list[PickleableNode],
id_to_pnode: dict[int, PickleableNode],
id_to_computation: dict[int, HloComputationProto],
) -> tuple[str, list[int], dict]:
assert len(instr.called_computation_ids) == 1
computation = id_to_computation[instr.called_computation_ids[0]]
op = get_reduce_op(computation)
inputs = id_to_pnode[instr.operand_ids[0]]
assert len(instr.replica_groups) == 1
replica_ids = [str(i) for i in instr.replica_groups[0].replica_ids]
replica_ids_str = "g" + "_".join(sorted(replica_ids))
target = "all_reduce"
if op == "add":
args = [inputs, "sum", replica_ids_str]
return (target, args, {})
elif op == "maximum":
args = [inputs, "max", replica_ids_str]
return (target, args, {})
else:
raise NotImplementedError(f"{op=} not implemented for all-reduce")
def convert_literal(instr: HloInstructionProto) -> tuple[str, list[int], dict]:
"""
Returns (target, shape, args, kwargs)
"""
literal: "LiteralProto" = instr.literal
shape = list(literal.shape.dimensions)
dtype = literal.shape.element_type
if dtype == xla_data_pb2.F32:
values = list(literal.f32s)
# The F16s, BF16s, U16s and S16s are encoded in little endian byte order
elif dtype == xla_data_pb2.F16:
values = np.frombuffer(literal.f16s, dtype=np.float16).tolist()
else:
raise NotImplementedError(f"{dtype=} not implemented")
if len(shape) == 0:
return ("scalar", [values[0]], {})
elif len(set(values)) == 1:
value = values[0]
if value == 0:
return ("zeros", [shape], {})
elif value == 1:
return ("ones", [shape], {})
else:
raise NotImplementedError(
f"literal value {value} not implemented, {instr=}"
)
else:
return ("empty", [shape], {})
def convert_hlo_proto_to_pnode(
instr: HloInstructionProto,
rank: int,
id_to_pnode: dict[int, PickleableNode],
id_to_computation: dict[int, HloComputationProto],
) -> Union[PickleableNode, float, int]:
get_pnode = lambda instr_id: id_to_pnode[instr_id]
name = instr.name
opcode = instr.opcode
if opcode == "parameter":
op = "placeholder"
target = name
elif opcode == "custom-call":
op = "call_function"
target = instr.custom_call_target
else:
op = "call_function"
target = opcode
# Default args and kwargs
args = []
kwargs = {}
shape = tuple(instr.shape.dimensions)
# Now, use op and target below.
if op == "placeholder":
pass
else:
# Check https://www.tensorflow.org/mlir/hlo_ops and
# https://github.com/openxla/stablehlo/blob/main/docs/spec.md for ops definitions.
assert op == "call_function", f"{op=}, {target=} not implemented"
args = [get_pnode(instr_id) for instr_id in instr.operand_ids]
if target == "AwsNeuronRmsNorm":
pass
elif target == "AwsNeuronTransferWithStaticRing":
target = "clone"
elif target in (
"add",
"and",
"cosine",
"divide",
"exponential",
"logistic",
"maximum",
"multiply",
"sine",
"subtract",
):
pass
elif target == "all-reduce":
target, args, kwargs = convert_all_reduce(
instr, args, id_to_pnode, id_to_computation
)
elif target == "broadcast":
dims = instr.dimensions
args = [*args, list(dims), shape]
elif target == "compare":
target = f"{instr.comparison_direction.lower()}.Tensor"
elif target == "concatenate":
dims = instr.dimensions
if len(dims) != 1:
raise NotImplementedError(f"{dims=} not implemented for concatenate")
dim = dims[0]
args = [args, dim]
elif target == "constant":
target, args, kwargs = convert_literal(instr)
elif target == "convert":
# Treat `convert` as no-op for now, because we currently don't care about the dtype.
target = "clone"
elif target == "dot":
lhs_contracting_dims = list(
instr.dot_dimension_numbers.lhs_contracting_dimensions
)
rhs_contracting_dims = list(
instr.dot_dimension_numbers.rhs_contracting_dimensions
)
lhs_batch_dims = list(instr.dot_dimension_numbers.lhs_batch_dimensions)
rhs_batch_dims = list(instr.dot_dimension_numbers.rhs_batch_dimensions)
args = args + [
lhs_contracting_dims,
rhs_contracting_dims,
lhs_batch_dims,
rhs_batch_dims,
]
elif target == "gather":
operand = args[0]
start_indices = args[1]
offset_dims = instr.gather_dimension_numbers.offset_dims
collapsed_slice_dims = instr.gather_dimension_numbers.collapsed_slice_dims
start_index_map = instr.gather_dimension_numbers.start_index_map
index_vector_dim = instr.gather_dimension_numbers.index_vector_dim
# operand_batching_dims = instr.gather_dimension_numbers.operand_batching_dims
# start_indices_batching_dims = (
# instr.gather_dimension_numbers.start_indices_batching_dims
# )
slice_sizes = instr.gather_slice_sizes
args = [
operand,
start_indices,
list(offset_dims),
list(collapsed_slice_dims),
list(start_index_map),
index_vector_dim,
# list(operand_batching_dims),
# list(start_indices_batching_dims),
list(slice_sizes),
]
elif target == "iota":
if len(shape) != 1:
raise NotImplementedError(f"{shape=} not implemented for iota")
dims = instr.dimensions
assert len(dims) == 1 and dims[0] == 0, f"{dims=} not implemented for iota"
target = "arange"
args = [0, shape[0]]
elif target == "reduce":
target, args, kwargs = convert_reduce(
instr, args, id_to_pnode, id_to_computation
)
elif target == "reshape":
shape = list(instr.shape.dimensions)
args = args + [shape]
elif target == "select":
assert len(args) == 3
elif target == "slice":
arg_shape = args[0].get_tensor_shape()
assert arg_shape is not None, f"{arg_shape=} should not be None"
slice_dims = [
(i, s.start, s.limit, s.stride)
for i, s in enumerate(instr.slice_dimensions)
]
useful_slice_dims = []
for size, (dim, start, end, stride) in zip(arg_shape, slice_dims):
if end - start > 0 and end - start != size:
useful_slice_dims.append((dim, start, end, stride))
if len(useful_slice_dims) == 0:
target = "clone"
args = args[:1]
elif len(useful_slice_dims) == 1:
dim, start, end, stride = useful_slice_dims[0]
args = args + [dim, start, end, stride]
else:
raise NotImplementedError(
f"{useful_slice_dims=} for more than 1 dimension not implemented"
)
elif target == "transpose":
dims = list(instr.dimensions)
assert len(args) == 1
args = args + [dims]
elif target == "tuple":
raise RuntimeError("We only consider tuple as outputs.")
else:
print(instr)
raise NotImplementedError(f"{op=}, {target=} not implemented")
if target in TO_FX_TARGET_NAME:
target = TO_FX_TARGET_NAME[target]
else:
target = f"hlo.{target}"
if "\n----\n" not in instr.metadata.source_file:
raise RuntimeError(
f"Meta source_file should contain \\n----\\n, but not found in {instr=}"
)
stack = eval(instr.metadata.source_file.split("\n----\n")[1])
meta = {
"tensor_meta": TensorMetadata(
shape=shape,
dtype=TO_TORCH_DTYPE[instr.shape.element_type],
requires_grad=False,
stride=None,
memory_format=None,
is_quantized=False,
qparams=None,
),
"stack": stack,
}
pnode = PickleableNode(
rank=rank, op=op, name=name, target=target, args=args, kwargs=kwargs, meta=meta
)
return pnode
def from_hlo_module(hlo_module: "HloModuleProto", rank: int) -> PickleableGraph:
# https://github.com/openxla/xla/blob/main/xla/service/hlo.proto
# https://github.com/openxla/xla/blob/main/xla/xla.proto
# https://github.com/openxla/xla/blob/main/xla/xla_data.proto
# hlo_module = HloModuleProto()
# hlo_module.ParseFromString(open(path, "rb").read())
id_to_computation = {}
name_to_computation = {}
for computation in hlo_module.computations:
id_to_computation[computation.id] = computation
name_to_computation[computation.name] = computation
entry_computation = name_to_computation[hlo_module.entry_computation_name]
id_to_pnode: dict[int, PickleableNode] = {}
pgraph = PickleableGraph(rank=rank)
for instr in entry_computation.instructions:
if instr.id == entry_computation.root_id:
assert instr.opcode == "tuple"
pnode = PickleableNode(
rank,
name="output",
op="output",
target="output",
args=[[id_to_pnode[i] for i in instr.operand_ids]],
kwargs={},
)
pgraph.add_node(pnode)
id_to_pnode[instr.id] = pnode
else:
pnode = convert_hlo_proto_to_pnode(
instr, rank, id_to_pnode, id_to_computation
)
if pnode.target == "hlo.scalar":
# We force inline all scalars.
value = pnode.args[0]
id_to_pnode[instr.id] = value
else:
pgraph.add_node(pnode)
id_to_pnode[instr.id] = pnode
pgraph.sanity_check()
return pgraph