# Imports
from PythonInterface import Python
= Python.import_module("pathlib") # Python standard library
let pathlib = Python.import_module("gzip") # Python standard library
let gzip = Python.import_module("pickle") # Python standard library
let pickle = Python.import_module("numpy")
let np
# Get the data
= pathlib.Path('./lost+found/data/mnist.pkl.gz')
path_gz = gzip.open(path_gz, 'rb')
f = pickle._Unpickler(f)
u = 'latin1'
u.encoding = u.load()
data
= data[0]
data_train = data[1]
data_valid
= data_train[0]
x_train = data_train[1]
y_train = np.expand_dims(y_train, 1)
y_train
= data_valid[0]
x_valid = data_valid[1]
y_valid = np.expand_dims(y_valid, 1)
y_valid f.close()
🔥 Matmul -> Linear Layer
Let us put in the code fom the previous notebook, to do the imports an to load the data.
from DType import DType
from Memory import memset_zero
from Object import object, Attr
from Pointer import DTypePointer, Pointer
from Random import rand
from Range import range
from TargetInfo import dtype_sizeof
type: DType]:
struct Matrix[type]
var data: DTypePointer[
var rows: Int
var cols: Int
__init__(inout self, rows: Int, cols: Int):
fn self.data = DTypePointer[type].alloc(rows * cols)
self.data, rows*cols)
rand(self.rows = rows
self.cols = cols
self, other: Self):
fn __copyinit__(inout self.data = other.data
self.rows = other.rows
self.cols = other.cols
__del__(owned self):
fn self.data.free()
self):
fn zero(inout self.data, self.rows * self.cols)
memset_zero(
@always_inline
__getitem__(self, y: Int, x: Int) -> SIMD[type, 1]:
fn return self.load[1](y, x)
@always_inline
self, y: Int, x: Int) -> SIMD[type, nelts]:
fn load[nelts:Int](return self.data.simd_load[nelts](y * self.cols + x)
@always_inline
__setitem__(self, y: Int, x: Int, val: SIMD[type, 1]):
fn return self.store[1](y, x, val)
@always_inline
self, y: Int, x: Int, val: SIMD[type, nelts]):
fn store[nelts:Int](self.data.simd_store[nelts](y * self.cols + x, val)
type: DType]( a:PythonObject, o: Matrix[type], bs: Int) raises:
fn matrix_dataloader[for i in range(bs):
for j in range(o.cols):
= a[i][j].to_float64().cast[type]() o[i,j]
= 5 # batch-size
let bs: Int = x_train.shape[1].to_index() #28*28
let ni: Int
= Matrix[DType.float32](bs,ni)
let xb: Matrix[DType.float32] = Matrix[DType.float32](bs,1)
let yb: Matrix[DType.float32]
xb.zero()
yb.zero()
matrix_dataloader(x_train, xb, bs) matrix_dataloader(y_train, yb, bs)
Linear layer from foundations
A linear layer is nothing but a matrix multiplication (weights and activations) followed by a vector addition (with the bias term).
So the basic idea here is to use the the matmul
example functions from the Modular website as a starting point and add the bias term in it.
= 10
let no: Int = Matrix[DType.float32](ni, no) # weights
var w: Matrix[DType.float32] = Matrix[DType.float32](no, 1) # bias
var b: Matrix[DType.float32]
b.zero()= Matrix[DType.float32](xb.rows, w.cols) # result
var res res.zero()
from TargetInfo import dtype_sizeof, dtype_simd_width
from Functional import vectorize
= dtype_simd_width[DType.float32]() # The SIMD vector width.
alias nelts
type: DType](xb: Matrix[type], w: Matrix[type], b: Matrix[type], res: Matrix[type]) raises:
fn lin_vectorized[for i in range(xb.rows): # 50000
for j in range(xb.cols): # 784
@parameter
fn dotbias[nelts: Int](k: Int):+ xb[i,j] * w.load[nelts](j,k) + b.load[nelts](k,0))
res.store[nelts](i,k, res.load[nelts](i,k) vectorize[nelts, dotbias](w.cols)
res.zero() lin_vectorized(xb, w, b, res)
print(res.rows)
print(res.cols)
5
10