forked from AI-Hypercomputer/jetstream-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_ops.py
218 lines (179 loc) · 5.27 KB
/
basic_ops.py
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
import functools
from typing import Tuple, Callable, List, Optional
import time
import dataclasses
import numpy as np
import jax
import jax.numpy as jnp
from jax.experimental import mesh_utils, shard_map
from jax.sharding import PositionalSharding
from jax.sharding import Mesh
from jax.sharding import PartitionSpec
from jax.sharding import NamedSharding
devices = jax.devices()
P = PartitionSpec
devices = mesh_utils.create_device_mesh((len(devices),))
mesh = Mesh(devices, axis_names=("x",))
# y = jax.device_put(x, NamedSharding(mesh, P('a', 'b')))
L = 1 << 15
@dataclasses.dataclass
class BenchmarkCase:
"""BenchmarkCase."""
name: str
function: Callable
args_shape: List[Tuple]
args_sharding: List[PartitionSpec]
profiler_output: Optional[str] = None
start_key = jax.random.key(0)
def _new_arg(shape, dtype):
global start_key # pylint: disable=all
start_key, _ = jax.random.split(start_key)
with jax.default_device(jax.devices("cpu")[0]):
if dtype == jnp.int8.dtype:
return jax.random.randint(start_key, shape, 0, 100, dtype=dtype)
else:
return jax.random.normal(start_key, shape, dtype=dtype) + 1
def _new_args(case, dtype):
args = []
for shape, sharding in zip(case.args_shape, case.args_sharding):
arg = _new_arg(shape, dtype)
if sharding is not None:
arg = jax.device_put(arg, NamedSharding(mesh, sharding))
args.append(arg)
return args
def _run_case(case, warmup=2, runtimes=5, dtype=jnp.bfloat16.dtype):
for _ in range(warmup):
args = _new_args(case, dtype)
case.function(*args)
stamps = []
for i in range(runtimes):
args = _new_args(case, dtype)
jax.block_until_ready(args)
if case.profiler_output is not None and i == (runtimes - 1):
jax.profiler.start_trace(case.profiler_output)
start = time.perf_counter()
jax.block_until_ready(case.function(*args))
end = time.perf_counter()
if case.profiler_output is not None and i == (runtimes - 1):
jax.profiler.stop_trace()
stamps.append(end - start)
return sum(stamps) / runtimes
def _llama_ffn(x, w1, w2, w3):
w1_res = jax.nn.silu((x @ w1).astype(jnp.bfloat16.dtype))
w3_res = x @ w3
res = (w1_res * w3_res) @ w2
return res
@jax.jit
@functools.partial(
shard_map.shard_map,
mesh=mesh,
in_specs=(P(), P(None, "x"), P("x"), P(None, "x")),
out_specs=(P()),
)
def _llama_ffn_shmap(x, w1, w2, w3):
for _ in range(3):
x = _llama_ffn(x, w1, w2, w3)
x = jax.lax.psum(x, "x")
return x
@jax.jit
def _llama_ffn_spmd(x, w1, w2, w3):
for _ in range(3):
x = _llama_ffn(x, w1, w2, w3)
x = jax.lax.with_sharding_constraint(x, NamedSharding(mesh, P()))
return x
dim = 4096
multiple_of = 256
# hidden_dim = 4 * dim
# hidden_dim = int(2 * hidden_dim / 3)
# hidden_dim = multiple_of * ((hidden_dim + multiple_of - 1) // multiple_of)
hidden_dim = 11008
BATCH = 1024
@jax.jit
@functools.partial(
shard_map.shard_map,
mesh=mesh,
in_specs=(P("x"),),
out_specs=(P()),
check_rep=False,
)
def _all_gather(x):
return jax.lax.all_gather(x, "x")
@jax.jit
@functools.partial(
shard_map.shard_map, mesh=mesh, in_specs=(P("x"),), out_specs=(P())
)
def _all_reduce(x):
return jax.lax.psum(x, "x")
allcases = [
BenchmarkCase(
name="Matmul replicated",
function=jax.jit(jnp.matmul),
args_shape=((L, L), (L, L)),
args_sharding=(P(), P()), # replicated
),
BenchmarkCase(
name="Matmul sharded colrow",
function=jax.jit(jnp.matmul),
args_shape=((L, L), (L, L)),
args_sharding=(P(None, "x"), P("x")), # replicated
),
BenchmarkCase(
name="matmul sharded rowcol",
function=jax.jit(jnp.matmul),
args_shape=((L, L), (L, L)),
args_sharding=(P("x"), P("x", None)), # replicated
),
BenchmarkCase(
name="all_gather",
function=_all_gather,
args_shape=((L, L),),
args_sharding=(P("x"),), # replicated
),
BenchmarkCase(
name="all_reduce",
function=_all_reduce,
args_shape=((L, L),),
args_sharding=(P("x"),), # replicated
),
BenchmarkCase(
name="Llama 3xffn shardmap",
function=_llama_ffn_shmap,
args_shape=(
(BATCH, dim),
(dim, hidden_dim),
(hidden_dim, dim),
(dim, hidden_dim),
),
args_sharding=(P(), P(None, "x"), P("x"), P(None, "x")),
),
BenchmarkCase(
name="Llama 3xffn gspmd",
function=_llama_ffn_spmd,
args_shape=(
(BATCH, dim),
(dim, hidden_dim),
(hidden_dim, dim),
(dim, hidden_dim),
),
args_sharding=(P(), P(None, "x"), P("x"), P(None, "x")),
),
]
def _run_call_cases(cases):
for dtype in (jnp.bfloat16.dtype, jnp.int8.dtype):
for case in cases:
avg = _run_case(case, dtype=dtype)
dtype_size = 2 if dtype == jnp.bfloat16.dtype else 1
input_sizes = tuple(
[
f"{np.prod(size) * dtype_size / (1<<20) :.6} MiB"
for size in case.args_shape
]
)
print(
f"{dtype} \t {case.name}: \t{avg * 1000 :.6} ms \t sizes: {input_sizes}"
)
def main():
print("Number of devices: ", len(devices))
_run_call_cases(allcases)
if __name__ == "__main__":
main()