forked from modular/mojo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatmul.mojo
267 lines (202 loc) · 8.46 KB
/
matmul.mojo
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
# ===----------------------------------------------------------------------=== #
# Copyright (c) 2023, 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.
# ===----------------------------------------------------------------------=== #
# This sample demonstrates how various systems optimizations can be applied to a
# naive matmul implementation in Mojo to gain significant performance speedups
from random import rand
import benchmark
from algorithm import Static2DTileUnitFunc as Tile2DFunc
from algorithm import parallelize, vectorize
from memory import memset_zero
from python import Python
alias M = 512 # rows of A and C
alias N = 4096 # cols of B and C
alias K = 512 # cols of A and rows of B
alias type = DType.float32
# simdwidth of = amount of `type` elements that fit into a single SIMD register
# 2x multiplier will use multiple SIMD registers in parallel where possible
alias nelts = simdwidthof[type]() * 2
alias tile_n = 64 # N must be a multiple of this
alias tile_k = 4 # K must be a multiple of this
struct Matrix[rows: Int, cols: Int]:
var data: DTypePointer[type]
# Initialize zeroeing all values
fn __init__(inout self):
self.data = DTypePointer[type].alloc(rows * cols)
memset_zero(self.data, rows * cols)
# Initialize taking a pointer, don't set any elements
fn __init__(inout self, data: DTypePointer[type]):
self.data = data
## Initialize with random values
@staticmethod
fn rand() -> Self:
var data = DTypePointer[type].alloc(rows * cols)
rand(data, rows * cols)
return Self(data)
fn __getitem__(self, y: Int, x: Int) -> Scalar[type]:
return self.load[1](y, x)
fn __setitem__(inout self, y: Int, x: Int, val: Scalar[type]):
self.store[1](y, x, val)
fn load[nelts: Int](self, y: Int, x: Int) -> SIMD[type, nelts]:
return self.data.load[width=nelts](y * self.cols + x)
fn store[nelts: Int](self, y: Int, x: Int, val: SIMD[type, nelts]):
return self.data.store[width=nelts](y * self.cols + x, val)
def run_matmul_python() -> Float64:
Python.add_to_path(".")
var pymatmul: PythonObject = Python.import_module("pymatmul")
var py = Python.import_module("builtins")
var gflops = pymatmul.benchmark_matmul_python(128, 128, 128).to_float64()
py.print(py.str("{:<13}{:>8.3f} GFLOPS").format("Python:", gflops))
return gflops
def run_matmul_numpy() -> Float64:
var pymatmul: PythonObject = Python.import_module("pymatmul")
var py = Python.import_module("builtins")
var gflops = pymatmul.benchmark_matmul_numpy(M, N, K).to_float64()
py.print(py.str("{:<13}{:>8.3f} GFLOPS").format("Numpy:", gflops))
return gflops
fn matmul_naive(inout C: Matrix, A: Matrix, B: Matrix):
for m in range(C.rows):
for k in range(A.cols):
for n in range(C.cols):
C[m, n] += A[m, k] * B[k, n]
# Using stdlib vectorize function
fn matmul_vectorized(inout C: Matrix, A: Matrix, B: Matrix):
for m in range(C.rows):
for k in range(A.cols):
@parameter
fn dot[nelts: Int](n: Int):
C.store[nelts](
m, n, C.load[nelts](m, n) + A[m, k] * B.load[nelts](k, n)
)
vectorize[dot, nelts, size = C.cols]()
# Parallelize the code by using the builtin parallelize function
fn matmul_parallelized(inout C: Matrix, A: Matrix, B: Matrix):
@parameter
fn calc_row(m: Int):
for k in range(A.cols):
@parameter
fn dot[nelts: Int](n: Int):
C.store[nelts](
m, n, C.load[nelts](m, n) + A[m, k] * B.load[nelts](k, n)
)
vectorize[dot, nelts, size = C.cols]()
parallelize[calc_row](C.rows, C.rows)
# Perform 2D tiling on the iteration space defined by end_x and end_y
fn tile[tiled_fn: Tile2DFunc, tile_x: Int, tile_y: Int](end_x: Int, end_y: Int):
for y in range(0, end_y, tile_y):
for x in range(0, end_x, tile_x):
tiled_fn[tile_x, tile_y](x, y)
# Use the above tile function to perform tiled matmul
fn matmul_tiled(inout C: Matrix, A: Matrix, B: Matrix):
@parameter
fn calc_row(m: Int):
@parameter
fn calc_tile[tile_x: Int, tile_y: Int](x: Int, y: Int):
for k in range(y, y + tile_y):
@parameter
fn dot[nelts: Int](n: Int):
C.store(
m,
n + x,
C.load[nelts](m, n + x)
+ A[m, k] * B.load[nelts](k, n + x),
)
vectorize[dot, nelts, size=tile_x]()
tile[calc_tile, tile_n, tile_k](C.cols, B.rows)
parallelize[calc_row](C.rows, C.rows)
# Unroll the vectorized loop by a constant factor
fn matmul_unrolled(inout C: Matrix, A: Matrix, B: Matrix):
@parameter
fn calc_row(m: Int):
@parameter
fn calc_tile[tile_x: Int, tile_y: Int](x: Int, y: Int):
@unroll(tile_y)
for k in range(y, y + tile_y):
@parameter
fn dot[nelts: Int](n: Int):
C.store(
m,
n + x,
C.load[nelts](m, n + x)
+ A[m, k] * B.load[nelts](k, n + x),
)
vectorize[
dot, nelts, size=tile_x, unroll_factor = tile_x // nelts
]()
tile[calc_tile, tile_n, tile_k](C.cols, B.rows)
parallelize[calc_row](C.rows, C.rows)
@always_inline
fn bench[
func: fn (inout Matrix, Matrix, Matrix) -> None, name: StringLiteral
](base_gflops: Float64, numpy_gflops: Float64) raises:
var A = Matrix[M, K].rand()
var B = Matrix[K, N].rand()
var C = Matrix[M, N]()
@always_inline
@parameter
fn test_fn():
_ = func(C, A, B)
var secs = benchmark.run[test_fn](max_runtime_secs=0.5).mean()
A.data.free()
B.data.free()
C.data.free()
var gflops = ((2 * M * N * K) / secs) / 1e9
var speedup: Float64 = gflops / base_gflops
var numpy_speedup: Float64 = gflops / numpy_gflops
var py = Python.import_module("builtins")
_ = py.print(
py.str("{:<13}{:>8.3f} GFLOPS {:>9.2f}x Python {:>5.2f}x Numpy").format(
name, gflops, speedup, numpy_speedup
)
)
@always_inline
fn test_matrix_equal[
func: fn (inout Matrix, Matrix, Matrix) -> None
](inout C: Matrix, A: Matrix, B: Matrix) raises -> Bool:
"""Runs a matmul function on A and B and tests the result for equality with
C on every element.
"""
var result = Matrix[M, N]()
_ = func(result, A, B)
for i in range(C.rows):
for j in range(C.cols):
if C[i, j] != result[i, j]:
return False
return True
fn test_all() raises:
var A = Matrix[M, K].rand()
var B = Matrix[K, N].rand()
var C = Matrix[M, N]()
matmul_naive(C, A, B)
if not test_matrix_equal[matmul_vectorized](C, A, B):
raise Error("Vectorize output does not match naive implementation")
if not test_matrix_equal[matmul_parallelized](C, A, B):
raise Error("Parallelize output does not match naive implementation")
if not test_matrix_equal[matmul_tiled](C, A, B):
raise Error("Tiled output does not match naive implementation")
if not test_matrix_equal[matmul_unrolled](C, A, B):
raise Error("Unroll output does not match naive implementation")
A.data.free()
B.data.free()
C.data.free()
fn main() raises:
constrained[N % tile_n == 0, "N must be a multiple of tile_n"]()
constrained[K % tile_k == 0, "K must be a multiple of tile_k"]()
test_all()
print("CPU Results\n")
var python_gflops = run_matmul_python()
var numpy_gflops = run_matmul_numpy()
bench[matmul_naive, "Naive:"](python_gflops, numpy_gflops)
bench[matmul_vectorized, "Vectorized: "](python_gflops, numpy_gflops)
bench[matmul_parallelized, "Parallelized:"](python_gflops, numpy_gflops)
bench[matmul_tiled, "Tiled:"](python_gflops, numpy_gflops)
bench[matmul_unrolled, "Unrolled:"](python_gflops, numpy_gflops)