-
Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathp12.mojo
More file actions
78 lines (65 loc) · 2.47 KB
/
Copy pathp12.mojo
File metadata and controls
78 lines (65 loc) · 2.47 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
# ===----------------------------------------------------------------------=== #
#
# This file is Modular Inc proprietary.
#
# ===----------------------------------------------------------------------=== #
from std.gpu import thread_idx, block_idx, block_dim, barrier
from std.gpu.host import DeviceContext
from std.gpu.memory import AddressSpace
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: dot_product
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 out_layout = row_major[1]()
comptime LayoutType = type_of(layout)
comptime OutLayout = type_of(out_layout)
def dot_product(
output: TileTensor[mut=True, dtype, OutLayout, MutAnyOrigin],
a: TileTensor[mut=False, dtype, LayoutType, ImmutAnyOrigin],
b: TileTensor[mut=False, dtype, LayoutType, ImmutAnyOrigin],
size: Int,
):
# FILL ME IN (roughly 13 lines)
...
# ANCHOR_END: dot_product
def main() raises:
with DeviceContext() as ctx:
var out = ctx.enqueue_create_buffer[dtype](1)
out.enqueue_fill(0)
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)
b_host[i] = Scalar[dtype](i)
var out_tensor = TileTensor(out, out_layout)
var a_tensor = TileTensor[mut=False, dtype, LayoutType](a, layout)
var b_tensor = TileTensor[mut=False, dtype, LayoutType](b, layout)
ctx.enqueue_function[dot_product](
out_tensor,
a_tensor,
b_tensor,
SIZE,
grid_dim=BLOCKS_PER_GRID,
block_dim=THREADS_PER_BLOCK,
)
var expected = ctx.enqueue_create_host_buffer[dtype](1)
expected.enqueue_fill(0)
ctx.synchronize()
with a.map_to_host() as a_host, b.map_to_host() as b_host:
for i in range(SIZE):
expected[0] += a_host[i] * b_host[i]
with out.map_to_host() as out_host:
print("out:", out_host)
print("expected:", expected)
assert_equal(out_host[0], expected[0])
print("Puzzle 12 complete ✅")