forked from llvm/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmlp-mpi.py
More file actions
368 lines (328 loc) · 13.9 KB
/
mlp-mpi.py
File metadata and controls
368 lines (328 loc) · 13.9 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
# REQUIRES: mpi4py
# RUN: mpirun -n 4 %PYTHON %s --mpilib=%VIRTUAL_ENV/lib/libmpi.so.12 | FileCheck %s
# RUN: mpirun -n 4 %PYTHON %s --mpilib=%VIRTUAL_ENV/lib/libmpi.so.12 --grid 0 0 | FileCheck %s
# RUN: mpirun -n 4 %PYTHON %s --mpilib=%VIRTUAL_ENV/lib/libmpi.so.12 --grid 4 1 | FileCheck %s
# CHECK: PASSED
"""
A single MLP that can run on multiple MPI ranks,
following a 1d/2d weight-stationary partition strategy
(see a and b from figure 2 of https://arxiv.org/pdf/2211.05102)
"""
import argparse
import ctypes
from contextlib import contextmanager
from typing import Optional
import numpy as np
from mlir import ir
from mlir.dialects import transform
from mlir.dialects.transform.bufferization import OneShotBufferizeOp
from mlir.dialects.bufferization import LayoutMapOption
from mlir.execution_engine import ExecutionEngine
from mlir.runtime.np_to_memref import (
ranked_memref_to_numpy,
make_nd_memref_descriptor,
as_ctype,
)
from lighthouse.utils.memref import (
to_ctype as memref_to_ctype,
deallocate_memrefs_on_exit,
)
from lighthouse.utils.mlir import apply_registered_pass, match
from lighthouse.workload import Workload, execute
from mlp_weight_stationary import generate_mlp_payload
from mpi4py import MPI
if not MPI.Is_initialized():
MPI.Init()
WORLD_SIZE = MPI.COMM_WORLD.Get_size()
WORLD_RANK = MPI.COMM_WORLD.Get_rank()
def rprint(*args, **kwargs):
if WORLD_RANK == 0:
print(*args, **kwargs)
def parse_cla():
parser = argparse.ArgumentParser(
description="MLP on MPI using MLIR",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--sizes",
"-s",
type=int,
nargs=3,
default=[64, 128, 32],
help="M,N,K matrix sizes (Activations=MxK, WeightsIn=KxN, WeightsOut=NxK, Result=MxK).",
)
parser.add_argument(
"--grid",
type=int,
default=[WORLD_SIZE],
nargs="+",
help="The shape of the device grid (1 or 2 dimensions). The product of the grid dimensions must match the number of MPI ranks. Use '0' if 2d grid dimensions should be inferred automatically.",
)
parser.add_argument(
"--verbose",
"-v",
type=int,
default=0,
help="Verbosity level.",
)
parser.add_argument(
"--mpilib",
type=str,
default="libmpi.so",
help="MPI shared library to load.",
)
args = parser.parse_args()
assert len(args.grid) in (1, 2), "Only 1D and 2D grids are supported."
assert all(x == 0 for x in args.grid) or np.prod(args.grid) == WORLD_SIZE, (
"Grid size must be only '0's or match the number of MPI ranks."
)
if len(args.grid) == 1 and args.grid[0] == 0:
args.grid = [WORLD_SIZE]
assert len(args.grid) == 2 or args.grid[0] == WORLD_SIZE, (
"1D grid size must match the number of MPI ranks."
)
return args
class DistMLP(Workload):
"""
A single MLP that can run on multiple MPI ranks.
A[:] = sigmoid(A@B)@C
where A, B, C are (M,K), (K,N), (N,K) matrices respectively.
"""
payload_function_name = "payload"
def __init__(self, args, P: int, R: int):
self.M = args.sizes[0]
self.N = args.sizes[1]
self.K = args.sizes[2]
self.comm_size = WORLD_SIZE # number of MPI ranks
self.comm_rank = WORLD_RANK # rank of this MPI process
self.dtype = np.float32
self.grid = args.grid
self.mpilibs = [args.mpilib]
self.verbose = args.verbose
def _alloc_inout(self, execution_engine: ExecutionEngine) -> list[ctypes.Structure]:
rprint(" * Allocating input/output arrays...")
memrefs = [
make_nd_memref_descriptor(2, as_ctype(self.dtype))() for _ in range(4)
]
# allocation functions use the same sharding annotations as the payload,
# so that each rank allocates only the part of the data it owns.
for i, v in enumerate(["act", "win", "wout"]):
execution_engine.invoke(f"alloc_{v}", memref_to_ctype(memrefs[i + 1]))
return memrefs
def _init_inout(self, r: np.ndarray, a: np.ndarray, b: np.ndarray, c: np.ndarray):
rprint(" * Initializing input arrays...")
np.random.seed(self.comm_rank) # different seed for each rank
A = ranked_memref_to_numpy([a])
B = ranked_memref_to_numpy([b])
C = ranked_memref_to_numpy([c])
A[:] = np.random.rand(*A.shape).astype(self.dtype)
B[:] = np.random.rand(*B.shape).astype(self.dtype)
C[:] = np.random.rand(*C.shape).astype(self.dtype)
@contextmanager
def allocate_inputs(self, execution_engine: ExecutionEngine):
self.input_memrefs = self._alloc_inout(execution_engine)
self._init_inout(*self.input_memrefs)
# Dealloc all memrefs on exit
with deallocate_memrefs_on_exit(
self.input_memrefs, execution_engine, "dealloc_2d"
):
yield self.input_memrefs
def _gather(
self,
memref: ctypes.Structure,
execution_engine: ExecutionEngine,
gather_func: str,
) -> ctypes.Structure:
gathered_memref = make_nd_memref_descriptor(2, as_ctype(self.dtype))()
execution_engine.invoke(
gather_func,
memref_to_ctype(gathered_memref),
memref_to_ctype(memref),
)
return gathered_memref
def _reference_solution(self, execution_engine: ExecutionEngine) -> np.ndarray:
rprint(" * Gathering input data...")
gathered = [
self._gather(self.input_memrefs[i + 1], execution_engine, f"gather_{v}")
for i, v in enumerate(["act", "win", "wout"])
]
rprint(" * Computing reference solution...")
def sigmoid(z):
return 1 / (1 + np.exp(-z))
with deallocate_memrefs_on_exit(gathered, execution_engine, "dealloc_2d"):
A, B, C = [ranked_memref_to_numpy([m]) for m in gathered]
result = sigmoid(A @ B) @ C
return result
def check_correctness(
self, execution_engine: ExecutionEngine, verbose: int = 0
) -> bool:
gathered = self._gather(self.input_memrefs[0], execution_engine, "gather_act")
with deallocate_memrefs_on_exit([gathered], execution_engine, "dealloc_2d"):
R = ranked_memref_to_numpy([gathered])
R_ref = self._reference_solution(execution_engine)
if verbose > 1:
rprint("Reference solution:")
rprint(R_ref)
rprint("Computed solution:")
rprint(R)
success = np.allclose(R, R_ref)
success = MPI.COMM_WORLD.allreduce(success, op=MPI.LAND)
if success:
rprint("PASSED")
else:
rprint("FAILED Result mismatch!")
return success
def shared_libs(self) -> list[str]:
return self.mpilibs + ["libmlir_c_runner_utils.so", "libmlir_runner_utils.so"]
def get_complexity(self) -> tuple[int, int, int]:
nbytes = np.dtype(self.dtype).itemsize
flop_count = (
4 * self.M * self.N * self.K + 4 * self.M * self.N
) # 2 matmuls (4MNK) + sigmoid (~4MN)
memory_reads = 5 * self.M * self.N * nbytes
memory_writes = (self.M * self.N + self.M * self.K) * nbytes
return (flop_count, memory_reads, memory_writes)
def payload_module(self) -> ir.Module:
if len(self.grid) == 1:
rprint(f"Using 1D grid of size {self.comm_size}")
grid = [self.comm_size]
else:
assert len(self.grid) == 2
if all(x != 0 for x in self.grid):
p1, p2 = self.grid
else:
# find two factors of comm_size that are as close as possible
def find_factors(n):
for i in range(int(n**0.5), 0, -1):
if n % i == 0:
return (i, n // i)
return (1, n)
p1, p2 = find_factors(self.comm_size)
rprint(f"Using 2D grid of size {p1}x{p2}")
grid = [p1, p2]
common = dict(
func_name=self.payload_function_name,
M=self.M,
N=self.N,
K=self.K,
comm_size=self.comm_size,
comm_rank=self.comm_rank,
grid=grid,
)
if len(self.grid) == 1:
mod = generate_mlp_payload(
**common,
split_act=[[], [0]],
split_win=[[], [0]],
split_wout=[[0], []],
split_mm0a_mm1c=[[]],
split_mm0_c=[[], [0]],
split_sigmoid=[[], [0]],
)
else:
mod = generate_mlp_payload(
**common,
split_act=[[], [0, 1]],
split_win=[[0], [1]],
split_wout=[[1], [0]],
split_mm0a_mm1c=[[], [0]],
split_mm0_c=[[], [1]],
split_sigmoid=[[], [1, 0]],
)
if self.verbose > 1:
rprint("Payload MLIR:")
count = 1
for line in str(mod).splitlines():
rprint(str(count) + "\t" + line)
count += 1
return mod
def schedule_module(
self, stop_at_stage: Optional[str] = None, parameters: Optional[dict] = None
) -> ir.Module:
schedule_module = ir.Module.create()
schedule_module.operation.attributes["transform.with_named_sequence"] = (
ir.UnitAttr.get()
)
with ir.InsertionPoint(schedule_module.body):
named_sequence = transform.named_sequence(
"__transform_main",
[transform.AnyOpType.get()],
[],
arg_attrs=[{"transform.readonly": ir.UnitAttr.get()}],
)
with ir.InsertionPoint(named_sequence.body):
anytype = transform.AnyOpType.get()
func = match(named_sequence.bodyTarget, ops={"func.func"})
func = apply_registered_pass(
func,
"sharding-propagation",
options={"traversal": "forward-backward"},
)
if self.verbose > 0:
transform.PrintOp(target=func)
func = apply_registered_pass(func, "shard-partition")
func = apply_registered_pass(func, "canonicalize")
if self.verbose > 0:
transform.PrintOp(target=func)
func = apply_registered_pass(func, "convert-shard-to-mpi")
func = apply_registered_pass(func, "canonicalize")
if self.verbose > 0:
transform.PrintOp(target=func)
func = apply_registered_pass(func, "tosa-to-linalg")
mod = transform.get_parent_op(
anytype, func, op_name="builtin.module", deduplicate=True
)
mod = apply_registered_pass(mod, "tosa-to-tensor")
mod = apply_registered_pass(mod, "linalg-generalize-named-ops")
mod = apply_registered_pass(mod, "canonicalize")
mod = apply_registered_pass(mod, "linalg-fuse-elementwise-ops")
mod = apply_registered_pass(mod, "arith-expand")
mod = apply_registered_pass(mod, "memref-expand")
mod = apply_registered_pass(mod, "empty-tensor-to-alloc-tensor")
mod = apply_registered_pass(mod, "canonicalize")
identity_layout = LayoutMapOption.IdentityLayoutMap
mod = OneShotBufferizeOp(
mod,
allow_return_allocs_from_loops=True,
bufferize_function_boundaries=True,
function_boundary_type_conversion=identity_layout,
)
mod = apply_registered_pass(mod, "expand-realloc")
mod = apply_registered_pass(mod, "canonicalize")
mod = apply_registered_pass(mod, "buffer-deallocation-simplification")
mod = apply_registered_pass(mod, "bufferization-lower-deallocations")
mod = apply_registered_pass(mod, "cse")
mod = apply_registered_pass(mod, "canonicalize")
mod = apply_registered_pass(mod, "convert-bufferization-to-memref")
mod = apply_registered_pass(mod, "convert-linalg-to-parallel-loops")
mod = apply_registered_pass(mod, "scf-parallel-loop-fusion")
mod = apply_registered_pass(mod, "canonicalize")
mod = apply_registered_pass(mod, "fold-memref-alias-ops")
mod = apply_registered_pass(mod, "expand-strided-metadata")
mod = apply_registered_pass(mod, "convert-math-to-funcs")
mod = apply_registered_pass(mod, "lower-affine")
mod = apply_registered_pass(mod, "convert-scf-to-cf")
mod = apply_registered_pass(mod, "symbol-dce")
mod = apply_registered_pass(mod, "finalize-memref-to-llvm")
mod = apply_registered_pass(mod, "convert-math-to-llvm")
mod = apply_registered_pass(mod, "convert-math-to-libm")
mod = apply_registered_pass(mod, "convert-func-to-llvm")
mod = apply_registered_pass(mod, "canonicalize")
mod = apply_registered_pass(mod, "convert-to-llvm")
mod = apply_registered_pass(mod, "reconcile-unrealized-casts")
mod = apply_registered_pass(mod, "cse")
if self.verbose > 1:
transform.PrintOp(target=mod)
transform.YieldOp()
return schedule_module
if __name__ == "__main__":
args = parse_cla()
if not MPI.Is_initialized():
MPI.Init()
P = MPI.COMM_WORLD.Get_size()
R = MPI.COMM_WORLD.Get_rank()
with ir.Context(), ir.Location.unknown():
wload = DistMLP(args, P, R)
rprint(" Execute".center(60, "-"))
execute(wload, verbose=args.verbose)
MPI.Finalize()