Repository navigation
Expand file tree
/
Copy pathp05.mojo
More file actions
96 lines (83 loc) · 3.38 KB
/
Copy pathp05.mojo
File metadata and controls
96 lines (83 loc) · 3.38 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
# ===----------------------------------------------------------------------=== #
# 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
from max.gpu.host import DeviceContext
from layout import TileTensor, TensorEngine
from layout.layout import row_major
from std.testing import assert_equal
from harness.canary import PuzzleMemory
# ANCHOR: broadcast_add
comptime SIZE = 2
comptime BLOCKS_PER_GRID = 1
comptime THREADS_PER_BLOCK = (3, 3)
comptime dtype = DType.float32
comptime out_layout = row_major[SIZE, SIZE]()
comptime a_layout = row_major[1, SIZE]()
comptime b_layout = row_major[SIZE, 1]()
comptime OutLayout = type_of(out_layout)
comptime ALayout = type_of(a_layout)
comptime BLayout = type_of(b_layout)
def broadcast_add[
Engine: TensorEngine,
](
output: TileTensor[mut=True, dtype, OutLayout, MutAnyOrigin, Engine=Engine],
a: TileTensor[mut=False, dtype, ALayout, ImmutAnyOrigin, Engine=Engine],
b: TileTensor[mut=False, dtype, BLayout, ImmutAnyOrigin, Engine=Engine],
size_dev: Int32,
) where (Engine.element_size == 1):
var size = Int(size_dev)
var row = thread_idx.y
var col = thread_idx.x
# FILL ME IN (roughly 2 lines)
# ANCHOR_END: broadcast_add
def main() raises:
with DeviceContext() as ctx:
var mem = PuzzleMemory[dtype](ctx)
var out_buf = mem.output(SIZE * SIZE)
var out_tensor = TileTensor(out_buf, out_layout)
print("out shape:", out_tensor.dim[0](), "x", out_tensor.dim[1]())
var expected_buf = ctx.enqueue_create_host_buffer[dtype](SIZE * SIZE)
expected_buf.enqueue_fill(0)
var expected_tensor = TileTensor(expected_buf, out_layout)
var a = ctx.enqueue_create_buffer[dtype](SIZE)
a.enqueue_fill(0)
var b = ctx.enqueue_create_buffer[dtype](SIZE)
b.enqueue_fill(0)
with a.map_to_host() as a_host, b.map_to_host() as b_host:
for i in range(SIZE):
a_host[i] = Scalar[dtype](i + 1)
b_host[i] = Scalar[dtype](i * 10)
for i in range(SIZE):
for j in range(SIZE):
expected_tensor[i, j] = a_host[j] + b_host[i]
var a_tensor = TileTensor(a, a_layout)
var b_tensor = TileTensor(b, b_layout)
ctx.enqueue_function[broadcast_add[out_tensor.Engine]](
out_tensor,
a_tensor,
b_tensor,
Int32(SIZE),
grid_dim=BLOCKS_PER_GRID,
block_dim=THREADS_PER_BLOCK,
)
ctx.synchronize()
with out_buf.map_to_host() as out_buf_host:
print("out:", out_buf_host)
print("expected:", expected_buf)
for i in range(SIZE):
for j in range(SIZE):
assert_equal(
out_buf_host[i * SIZE + j], expected_buf[i * SIZE + j]
)
mem.verify()
print("Puzzle 05 complete ✅")