forked from modular/modular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout_tensor_gpu_examples.mojo
More file actions
380 lines (327 loc) · 12.8 KB
/
Copy pathlayout_tensor_gpu_examples.mojo
File metadata and controls
380 lines (327 loc) · 12.8 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
# ===----------------------------------------------------------------------=== #
# Copyright (c) 2026, Modular Inc. All rights reserved.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions:
# https://llvm.org/LICENSE.txt
#
# 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.
# ===----------------------------------------------------------------------=== #
# DOC: max/layout/tensors.mdx
from max.gpu import (
thread_idx,
block_idx,
global_idx,
lane_id,
WARP_SIZE,
)
from max.gpu.sync import barrier
from max.gpu.memory import async_copy_wait_all
from max.gpu.host import DeviceContext, DeviceBuffer, get_gpu_target
from layout import Layout, LayoutTensor, print_layout
from layout.layout_tensor import copy_sram_to_local
from std.memory import Pointer
from std.sys import has_accelerator
from std.sys.info import (
has_apple_gpu_accelerator,
has_nvidia_gpu_accelerator,
is_apple_gpu,
is_nvidia_gpu,
simd_width_of,
)
from std.testing import assert_equal, assert_false, assert_true
from std.sys import exit
# start-initialize-tensor-from-cpu-example
def initialize_tensor_from_cpu_example() raises:
comptime dtype = DType.float32
comptime rows = 32
comptime cols = 8
comptime block_size = 8
comptime row_blocks = rows // block_size
comptime col_blocks = cols // block_size
comptime input_layout = Layout.row_major(rows, cols)
comptime size: Int = rows * cols
def kernel(tensor: LayoutTensor[dtype, input_layout, MutAnyOrigin]):
if (
global_idx.y < tensor.shape[0]()
and global_idx.x < tensor.shape[1]()
):
tensor[global_idx.y, global_idx.x] = (
tensor[global_idx.y, global_idx.x] + 1
)
try:
var ctx = DeviceContext()
var host_buf = ctx.enqueue_create_host_buffer[dtype](size)
var dev_buf = ctx.enqueue_create_buffer[dtype](size)
ctx.synchronize()
var expected_values = List[Scalar[dtype]](length=size, fill=0)
for i in range(size):
host_buf[i] = Scalar[dtype](i)
expected_values[i] = Scalar[dtype](i + 1)
ctx.enqueue_copy(dev_buf, host_buf)
var tensor = LayoutTensor[dtype, input_layout](dev_buf)
ctx.enqueue_function[kernel](
tensor,
grid_dim=(col_blocks, row_blocks),
block_dim=(block_size, block_size),
)
ctx.enqueue_copy(host_buf, dev_buf)
ctx.synchronize()
for i in range(rows * cols):
if host_buf[i] != expected_values[i]:
raise Error(
String("Error at position {} expected {} got {}").format(
i, expected_values[i], host_buf[i]
)
)
except error:
print(error)
# end-initialize-tensor-from-cpu-example
def shared_memory_alloc_example() raises:
comptime dtype = DType.float32
comptime in_size = 128
comptime block_size = 16
comptime num_blocks = in_size // block_size # number of block in one dimension
comptime input_layout = Layout.row_major(in_size, in_size)
def kernel(tensor: LayoutTensor[dtype, input_layout, MutAnyOrigin]):
# extract a tile from the input tensor.
var global_tile = tensor.tile[block_size, block_size](
block_idx.y, block_idx.x
)
# start-shared-memory-alloc-example
comptime tile_layout = Layout.row_major(block_size, block_size)
var shared_tile = LayoutTensor[
dtype,
tile_layout,
MutAnyOrigin,
address_space=.SHARED,
].stack_allocation()
# end-shared-memory-alloc-example
# Copy one element from the global tile to the shared tile.
shared_tile[thread_idx.y, thread_idx.x] = global_tile[
thread_idx.y, thread_idx.x
]
barrier()
# Put some data into the shared tile that we can verify on the host.
if global_idx.x < in_size and global_idx.y < in_size:
shared_tile[thread_idx.y, thread_idx.x] = Float32(
global_idx.y * in_size + global_idx.x
)
barrier()
global_tile[thread_idx.y, thread_idx.x] = shared_tile[
thread_idx.y, thread_idx.x
]
try:
var ctx = DeviceContext()
var host_buf = ctx.enqueue_create_host_buffer[dtype](in_size * in_size)
var dev_buf = ctx.enqueue_create_buffer[dtype](in_size * in_size)
ctx.enqueue_memset(dev_buf, 0.0)
var tensor = LayoutTensor[dtype, input_layout](dev_buf)
ctx.enqueue_function[kernel](
tensor,
grid_dim=(num_blocks, num_blocks),
block_dim=(block_size, block_size),
)
ctx.enqueue_copy(host_buf, dev_buf)
ctx.synchronize()
for i in range(in_size * in_size):
if host_buf[i] != Float32(i):
raise Error(
String("Error at position {} expected {} got {}").format(
i, i, host_buf[i]
)
)
except error:
print(error)
def simd_width_example():
# start-simd-width-example
from std.sys.info import simd_width_of
from max.gpu.host.compile import get_gpu_target
comptime simd_width = simd_width_of[DType.float32, get_gpu_target()]
# end-simd-width-example
def layout_tensor_vectorized_example() raises:
comptime dtype = DType.int32
comptime vector_width = 4
comptime rows = 64
comptime columns = 64
comptime layout = Layout.row_major(rows, columns)
var storage = Array[Int32, rows * columns](
fill_with=lambda (i: Int) -> Int32: Int32(i)
)
var tensor = LayoutTensor[dtype, layout](storage)
# start-vectorize-tensor-example
var vectorized_tensor = tensor.vectorize[1, vector_width]()
# end-vectorize-tensor-example
var values = vectorized_tensor[0, 0]
# The SIMD width could be anywhere from 4 to 16 (possibly more in the future)
# So just test a single value.
assert_equal(
rebind[SIMD[dtype, vector_width]](values)[3], SIMD[dtype, 1](3)
)
def layout_tensor_distribute_example():
comptime rows = 4
comptime columns = 8
comptime layout = Layout.row_major(rows, columns)
comptime dtype = DType.int32
def kernel(tensor: LayoutTensor[dtype, layout, MutAnyOrigin]):
var fragment = tensor.vectorize[1, 4]().distribute[
Layout.row_major(2, 2)
](lane_id())
_ = fragment
try:
var ctx = DeviceContext()
var dev_buf = ctx.enqueue_create_buffer[.int32](rows * columns)
var host_buf = ctx.enqueue_create_host_buffer[.int32](rows * columns)
for i in range(rows * columns):
host_buf[i] = Int32(i)
var tensor = LayoutTensor[dtype, layout](dev_buf)
ctx.enqueue_copy(dev_buf, host_buf)
ctx.enqueue_function[kernel](
tensor,
grid_dim=(1, 1),
block_dim=(8, 1),
)
except error:
print(error)
# TODO: Add simple copy example to doc
def simple_copy_example():
comptime dtype = DType.float32
comptime rows = 128
comptime cols = 128
comptime block_size = 16
comptime num_row_blocks = rows // block_size
comptime num_col_blocks = cols // block_size
comptime input_layout = Layout.row_major(rows, cols)
def kernel(tensor: LayoutTensor[dtype, input_layout, MutAnyOrigin]):
# extract a tile from the input tensor.
var global_tile = tensor.tile[block_size, block_size](
block_idx.y, block_idx.x
)
comptime tile_layout = Layout.row_major(block_size, block_size)
var shared_tile = LayoutTensor[
dtype,
tile_layout,
MutAnyOrigin,
address_space=.SHARED,
].stack_allocation()
if global_idx.y < rows and global_idx.x < cols:
shared_tile[thread_idx.y, thread_idx.x] = global_tile[
thread_idx.y, thread_idx.x
]
barrier()
# Put some data into the shared tile that we can verify on the host.
if global_idx.y < rows and global_idx.x < cols:
shared_tile[thread_idx.y, thread_idx.x] = (
shared_tile[thread_idx.y, thread_idx.x] * 2
)
barrier()
if global_idx.y < rows and global_idx.x < cols:
global_tile[thread_idx.y, thread_idx.x] = shared_tile[
thread_idx.y, thread_idx.x
]
try:
var ctx = DeviceContext()
var host_buf = ctx.enqueue_create_host_buffer[dtype](rows * cols)
var dev_buf = ctx.enqueue_create_buffer[dtype](rows * cols)
for i in range(rows * cols):
host_buf[i] = Float32(i)
ctx.enqueue_copy(dev_buf, host_buf)
var tensor = LayoutTensor[dtype, input_layout](dev_buf)
ctx.enqueue_function[kernel](
tensor,
grid_dim=(num_row_blocks, num_col_blocks),
block_dim=(block_size, block_size),
)
ctx.enqueue_copy(host_buf, dev_buf)
ctx.synchronize()
for i in range(rows * cols):
if host_buf[i] != Float32(i * 2):
raise Error(
String("Unexpected value ", host_buf[i], " at position ", i)
)
except error:
print(error)
# TODO: improve thread layout example and explanations
# start-copy-from-async-example
def copy_from_async_example():
comptime if not has_apple_gpu_accelerator():
comptime dtype = DType.float32
comptime rows = 128
comptime cols = 128
comptime block_size = 16
comptime num_row_blocks = rows // block_size
comptime num_col_blocks = cols // block_size
comptime input_layout = Layout.row_major(rows, cols)
comptime simd_width = 4
def kernel(tensor: LayoutTensor[dtype, input_layout, MutAnyOrigin]):
# extract a tile from the input tensor.
var global_tile = tensor.tile[block_size, block_size](
block_idx.y, block_idx.x
)
comptime tile_layout = Layout.row_major(block_size, block_size)
var shared_tile = LayoutTensor[
dtype,
tile_layout,
MutAnyOrigin,
address_space=.SHARED,
].stack_allocation()
# Create thread layouts for copying
comptime thread_layout = Layout.row_major(
WARP_SIZE // simd_width, simd_width
)
var global_fragment = global_tile.vectorize[
1, simd_width
]().distribute[thread_layout](lane_id())
var shared_fragment = shared_tile.vectorize[
1, simd_width
]().distribute[thread_layout](lane_id())
shared_fragment.copy_from_async(global_fragment)
comptime if is_nvidia_gpu():
async_copy_wait_all()
barrier()
# Put some data into the shared tile that we can verify on the host.
if global_idx.y < rows and global_idx.x < cols:
shared_tile[thread_idx.y, thread_idx.x] = (
shared_tile[thread_idx.y, thread_idx.x] + 1
)
barrier()
global_fragment.copy_from(shared_fragment)
try:
var ctx = DeviceContext()
var host_buf = ctx.enqueue_create_host_buffer[dtype](rows * cols)
var dev_buf = ctx.enqueue_create_buffer[dtype](rows * cols)
for i in range(rows * cols):
host_buf[i] = Float32(i)
var tensor = LayoutTensor[dtype, input_layout](dev_buf)
ctx.enqueue_copy(dev_buf, host_buf)
ctx.enqueue_function[kernel](
tensor,
grid_dim=(num_row_blocks, num_col_blocks),
block_dim=(block_size, block_size),
)
ctx.enqueue_copy(host_buf, dev_buf)
ctx.synchronize()
for i in range(rows * cols):
if host_buf[i] != Float32(i + 1):
raise Error(
String(
"Unexpected value ", host_buf[i], " at position ", i
)
)
except error:
print(error)
# end-copy-from-async-example
# TODO: Currently doesn't run on Apple silicon GPU
def main() raises:
if has_accelerator():
initialize_tensor_from_cpu_example()
shared_memory_alloc_example()
layout_tensor_vectorized_example()
layout_tensor_distribute_example()
simple_copy_example()
copy_from_async_example()
else:
print("No accelerator, skipping examples that require a GPU.")