Repository navigation
Expand file tree
/
Copy pathconv1d.mojo
More file actions
130 lines (110 loc) · 4.4 KB
/
Copy pathconv1d.mojo
File metadata and controls
130 lines (110 loc) · 4.4 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
# ===----------------------------------------------------------------------=== #
# 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 Coord, TileTensor
from layout.tensor_engine import TensorEngine
from layout.layout import row_major, TensorLayout
from layout.tile_tensor import stack_allocation
from std.sys import argv
from std.testing import assert_equal
# ANCHOR: conv1d_kernel
comptime TPB = 15
comptime BLOCKS_PER_GRID = (2, 1)
def conv1d_kernel[
input_size: Int,
conv_size: Int,
OutLayout: TensorLayout,
InLayout: TensorLayout,
ConvLayout: TensorLayout,
Engine: TensorEngine,
dtype: DType = .float32,
](
output: TileTensor[mut=True, dtype, OutLayout, MutAnyOrigin, Engine=Engine],
input: TileTensor[mut=True, dtype, InLayout, MutAnyOrigin, Engine=Engine],
kernel: TileTensor[
mut=True, dtype, ConvLayout, MutAnyOrigin, Engine=Engine
],
) where (Engine.element_size == 1):
var global_i = block_dim.x * block_idx.x + thread_idx.x
var local_i = thread_idx.x
# first: need to account for padding
var shared_a = stack_allocation[dtype=dtype, address_space=.SHARED](
row_major[TPB + conv_size - 1]()
)
var shared_b = stack_allocation[dtype=dtype, address_space=.SHARED](
row_major[conv_size]()
)
if global_i < input_size:
shared_a[local_i] = rebind[Scalar[dtype]](input[global_i])
# second: load elements needed for convolution at block boundary
if local_i < conv_size - 1:
# indices from next block
var next_idx = global_i + TPB
if next_idx < input_size:
shared_a[TPB + local_i] = rebind[Scalar[dtype]](input[next_idx])
else:
# Initialize out-of-bounds elements to 0 to avoid reading from uninitialized memory
# which is an undefined behavior
shared_a[TPB + local_i] = 0
if local_i < conv_size:
shared_b[local_i] = rebind[Scalar[dtype]](kernel[local_i])
barrier()
if global_i < input_size:
var local_sum: Scalar[dtype] = 0
comptime for j in range(conv_size):
if local_i + j < TPB + conv_size - 1:
local_sum += shared_a[local_i + j] * shared_b[j]
output.store[1](Coord(global_i), local_sum)
# ANCHOR_END: conv1d_kernel
# ANCHOR: conv1d_custom_op
import extensibility
from extensibility import InputTensor, OutputTensor
from max.gpu.host import DeviceBuffer
@extensibility.register("conv1d")
struct Conv1DCustomOp:
@staticmethod
def execute[
input_size: Int,
conv_size: Int,
dtype: DType = .float32,
](
output: OutputTensor[dtype=dtype, rank=1, static_spec=_],
input: InputTensor[dtype=dtype, rank=output.rank, static_spec=_],
kernel: InputTensor[dtype=dtype, rank=output.rank, static_spec=_],
# the context is needed for some GPU calls
ctx: DeviceContext,
) raises:
var output_tensor = output.to_tile_tensor().as_unsafe_any_origin()
var input_tensor = input.to_tile_tensor().as_unsafe_any_origin()
var kernel_tensor = kernel.to_tile_tensor().as_unsafe_any_origin()
comptime if ctx.target.is_gpu():
var gpu_ctx = ctx
# making sure the output tensor is zeroed out before the kernel is called
gpu_ctx.enqueue_memset(
DeviceBuffer[output_tensor.dtype](
gpu_ctx,
output.unsafe_ptr(),
input_size,
owning=False,
),
0,
)
# FILL ME IN with 2 lines calling our conv1d_kernel
elif ctx.target.is_cpu():
# we can fallback to CPU
pass
else:
raise Error("Unsupported target: " + ctx.target.device_kind_label())
# ANCHOR_END: conv1d_custom_op