-
Notifications
You must be signed in to change notification settings - Fork 379
Expand file tree
/
Copy pathp11.mojo
More file actions
89 lines (75 loc) · 2.89 KB
/
Copy pathp11.mojo
File metadata and controls
89 lines (75 loc) · 2.89 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
# ===----------------------------------------------------------------------=== #
# 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.
# ===----------------------------------------------------------------------=== #
from max.gpu import thread_idx, block_idx, block_dim
from max.gpu.sync import barrier
from max.gpu.host import DeviceContext
from layout import TileTensor
from layout.tile_layout import row_major
from layout.tile_tensor import stack_allocation
from std.testing import assert_equal
# ANCHOR: pooling
comptime TPB = 8
comptime SIZE = 8
comptime BLOCKS_PER_GRID = (1, 1)
comptime THREADS_PER_BLOCK = (TPB, 1)
comptime dtype = DType.float32
comptime layout = row_major[SIZE]()
comptime LayoutType = type_of(layout)
def pooling(
output: TileTensor[mut=True, dtype, LayoutType, MutAnyOrigin],
a: TileTensor[mut=False, dtype, LayoutType, ImmutAnyOrigin],
size_dev: Int32,
):
var size = Int(size_dev)
# Allocate shared memory using stack_allocation
var shared = stack_allocation[dtype=dtype, address_space=.SHARED](
row_major[TPB]()
)
var global_i = block_dim.x * block_idx.x + thread_idx.x
var local_i = thread_idx.x
# FILL ME IN (roughly 10 lines)
# ANCHOR_END: pooling
def main() raises:
with DeviceContext() as ctx:
var out = ctx.enqueue_create_buffer[dtype](SIZE)
out.enqueue_fill(0)
var a = ctx.enqueue_create_buffer[dtype](SIZE)
a.enqueue_fill(0)
with a.map_to_host() as a_host:
for i in range(SIZE):
a_host[i] = Scalar[dtype](i)
var out_tensor = TileTensor(out, layout)
var a_tensor = TileTensor[mut=False, dtype, LayoutType](a, layout)
ctx.enqueue_function[pooling](
out_tensor,
a_tensor,
Int32(SIZE),
grid_dim=BLOCKS_PER_GRID,
block_dim=THREADS_PER_BLOCK,
)
var expected = ctx.enqueue_create_host_buffer[dtype](SIZE)
expected.enqueue_fill(0)
ctx.synchronize()
with a.map_to_host() as a_host:
var ptr = a_host
for i in range(SIZE):
var s = Scalar[dtype](0)
for j in range(max(i - 2, 0), i + 1):
s += ptr[j]
expected[i] = s
with out.map_to_host() as out_host:
print("out:", out_host)
print("expected:", expected)
for i in range(SIZE):
assert_equal(out_host[i], expected[i])
print("Puzzle 11 complete ✅")