Stijn
Stijn
MModular
Created by Stijn on 4/1/2024 in #questions
Parameters style guide
I noticed that the style guide (https://github.com/modularml/mojo/blob/4d0b45f74fbcf883c63603a762b0c5c3a11895ed/stdlib/docs/style-guide.md) Is recommending PascalCase for parameters that are values:
fn value parameter fn repeat[Count: Int]() PascalCase
fn value parameter fn repeat[Count: Int]() PascalCase
Can any of the modular people confirm that this is the headed direction? To be honest, I wasn't really doing this for values and it is really confusing with Types and Structs in my opinion. I also noticed the stdlib isn't following this, and even the docstrings example a couple of lines further of the style guide isn't.
11 replies
MModular
Created by Stijn on 10/22/2023 in #questions
redefinition of symbol named '$< ... >'
A compiler error that took me a while to figure out. So happy to share how to solve this. Error:
./dainemo/utils/activations.mojo:0:0: error: redefinition of symbol named '$activations'
./dainemo/utils/activations.mojo:0:0: note: see current operation:
"lit.file_module"() ({
^bb0:
}) {sym_name = "$activations"} : () -> ()
./dainemo/nn/activations.mojo:0:0: note: see existing symbol definition here
./dainemo/utils/activations.mojo:0:0: error: redefinition of symbol named '$activations'
./dainemo/utils/activations.mojo:0:0: note: see current operation:
"lit.file_module"() ({
^bb0:
}) {sym_name = "$activations"} : () -> ()
./dainemo/nn/activations.mojo:0:0: note: see existing symbol definition here
Caused by empty files in a mojo module
mymodule/
__init__.mojo
activations.mojo --> empty file
mymodule/
__init__.mojo
activations.mojo --> empty file
Even if you delete the activations.mojo file now the compiler gets stuck in the same error. So you have to first clear the compiler cache: delete ~/.modular/.mojo_cache
6 replies
MModular
Created by Stijn on 10/2/2023 in #questions
cannot be copied into its destination
Hi 👋 , I want to extend my matrix class with the transpose functionality
alias dtype = DType.float32
alias type = Float32


struct Matrix:
var data: DTypePointer[dtype]
var rows: Int
var cols: Int

fn __init__(inout self, rows: Int, cols: Int):
self.data = DTypePointer[dtype].alloc(rows * cols)
rand(self.data, rows * cols)
self.rows = rows
self.cols = cols

fn __del__(owned self):
self.data.free()

fn zero(inout self):
memset_zero(self.data, self.rows * self.cols)

@always_inline
fn __getitem__(self, y: Int, x: Int) -> type:
return self.load[1](y, x)

@always_inline
fn __setitem__(self, y: Int, x: Int, val: type):
return self.store[1](y, x, val)

@always_inline
fn load[nelts: Int](self, y: Int, x: Int) -> SIMD[dtype, nelts]:
return self.data.simd_load[nelts](y * self.cols + x)

@always_inline
fn store[nelts: Int](self, y: Int, x: Int, val: SIMD[dtype, nelts]):
return self.data.simd_store[nelts](y * self.cols + x, val)

fn __str__(inout self) -> StringLiteral:
let matrix_str: StringLiteral = ""
"""
TODO
"""
return matrix_str

@always_inline
fn T(inout self) -> Matrix:
let transposed = Matrix(self.cols, self.rows)
for i in range(self.rows):
for j in range(self.cols):
transposed[j, i] = self[i, j]
return transposed
alias dtype = DType.float32
alias type = Float32


struct Matrix:
var data: DTypePointer[dtype]
var rows: Int
var cols: Int

fn __init__(inout self, rows: Int, cols: Int):
self.data = DTypePointer[dtype].alloc(rows * cols)
rand(self.data, rows * cols)
self.rows = rows
self.cols = cols

fn __del__(owned self):
self.data.free()

fn zero(inout self):
memset_zero(self.data, self.rows * self.cols)

@always_inline
fn __getitem__(self, y: Int, x: Int) -> type:
return self.load[1](y, x)

@always_inline
fn __setitem__(self, y: Int, x: Int, val: type):
return self.store[1](y, x, val)

@always_inline
fn load[nelts: Int](self, y: Int, x: Int) -> SIMD[dtype, nelts]:
return self.data.simd_load[nelts](y * self.cols + x)

@always_inline
fn store[nelts: Int](self, y: Int, x: Int, val: SIMD[dtype, nelts]):
return self.data.simd_store[nelts](y * self.cols + x, val)

fn __str__(inout self) -> StringLiteral:
let matrix_str: StringLiteral = ""
"""
TODO
"""
return matrix_str

@always_inline
fn T(inout self) -> Matrix:
let transposed = Matrix(self.cols, self.rows)
for i in range(self.rows):
for j in range(self.cols):
transposed[j, i] = self[i, j]
return transposed
But i get:
error: value of type 'Matrix' cannot be copied into its destination
return transposed
error: value of type 'Matrix' cannot be copied into its destination
return transposed
I experience similar behaviour when trying to return something of type Matrix, in other functions as well. Any idea on how to overcome this?
4 replies