geauxeric
geauxeric
MModular
Created by geauxeric on 5/20/2024 in #community-showcase
Implement and benchmark Softmax algorithms in Mojo
I gave my first try at Mojo, and really love its design. Did a weekend project on implementing online normalizer calculation of softmax, and made a vlog: https://youtu.be/5JdP_YAH-iE?si=q7oJgu4ZZ87kv26J
1 replies
MModular
Created by geauxeric on 5/5/2024 in #questions
type conversion between alias types
A matrix type is defined as
alias type = DType.float32

struct Matrix[rows: Int, cols: Int]:
var data: DTypePointer[type]

@staticmethod
fn rand() -> Self:
var data = DTypePointer[type].alloc(rows * cols)
rand(data, rows * cols)
return Self(data)
alias type = DType.float32

struct Matrix[rows: Int, cols: Int]:
var data: DTypePointer[type]

@staticmethod
fn rand() -> Self:
var data = DTypePointer[type].alloc(rows * cols)
rand(data, rows * cols)
return Self(data)
Then a Logits type is defined with Matrix with only 1 column:
struct Logits[rows: Int]:
alias LogitsM = Matrix[rows, 1]
var logits: Self.LogitsM
fn __init__(inout self):
self.logits.__init__()
@staticmethod
fn rand() -> Self:
return Self.LogitsM.rand()
struct Logits[rows: Int]:
alias LogitsM = Matrix[rows, 1]
var logits: Self.LogitsM
fn __init__(inout self):
self.logits.__init__()
@staticmethod
fn rand() -> Self:
return Self.LogitsM.rand()
Got compile error:
error: Expression [20]:9:33: cannot implicitly convert 'Matrix[rows, 1]' value to 'Logits[rows]' in return value
return Self.LogitsM.rand()
~~~~~~~~~~~~~~~~~^~
expression failed to parse (no further compiler diagnostics)
error: Expression [20]:9:33: cannot implicitly convert 'Matrix[rows, 1]' value to 'Logits[rows]' in return value
return Self.LogitsM.rand()
~~~~~~~~~~~~~~~~~^~
expression failed to parse (no further compiler diagnostics)
2 replies
MModular
Created by geauxeric on 5/5/2024 in #questions
Return with move semantics
Is that possible to use the move semantic when returning a value from a function? The code below would give error:
from tensor import Tensor


struct Value[dtype: DType]:
alias TensorD = Tensor[dtype]
var data: Self.TensorD

fn __init__(inout self):
self.data = Self.TensorD()

fn foo(self) -> Self:
var v = Self()
return v


fn main():
alias FValue = Value[DType.float16]
var v1 = FValue()
var v2 = FValue()
var v3 = v1.foo()
from tensor import Tensor


struct Value[dtype: DType]:
alias TensorD = Tensor[dtype]
var data: Self.TensorD

fn __init__(inout self):
self.data = Self.TensorD()

fn foo(self) -> Self:
var v = Self()
return v


fn main():
alias FValue = Value[DType.float16]
var v1 = FValue()
var v2 = FValue()
var v3 = v1.foo()
error: 'Value[dtype]' is not copyable because it has no 'copyinit' return v ^ mojo: error: failed to parse the provided Mojo source module
3 replies