Michael K
Michael K
MModular
Created by taalhaataahir01022001 on 5/21/2024 in #questions
x86 Intrinsics
The error is telling you that you are using runtime var instead of compile time alias or parameter. So you can replace var with alias on your 3 inputs above or just write them into the function call. And as noted above, the return type is there so that the compile knows what type to assign to the return value. Everything must have a known type at compile time. So AnyType will not work.
5 replies
MModular
Created by Martin Dudek on 5/18/2024 in #questions
How to implement Dependency Injection in Mojo?
I think you are working through an example like "Quackable" here - https://docs.modular.com/mojo/manual/traits#using-traits. A trait is not a concrete type so I don't think it can be used as an argument type. The type can be inferred at the call site though so you don't need to use the square brackets when calling lets_print. Lastly there is a note in the link saying they hope to make the syntax less verbose in the future.
10 replies
MModular
Created by tzl on 5/14/2024 in #questions
Why is this in-place operation not modifying the Python object in-place?
Your signature needs to be def do_numpy_stuff(inout ar: PythonObject) -> PythonObject:. Note the addition of inout. That means changes to ar inside the function also show up outside the function. Otherwise you are working with a copy and as you see the changes don't get reflected. I am not sure if this will always be the case for def functions since the expectation in Python is that everything is inout.
8 replies
MModular
Created by Analista on 5/13/2024 in #questions
How to read args from environment
Here is an example using it. The gist is that argv() returns a VariadicList so you can index into it, iterate over it, or get the length to work with the arguments. The docs on VariadicList are here.
5 replies
MModular
Created by Maxim on 1/17/2024 in #questions
how to use the gather function
Documentation is here. Example usage is here. 😃 If you really want to use the LLVM intrinsic, example usage is here.
6 replies
MModular
Created by bunny on 4/22/2024 in #questions
Type State Pattern
42 replies
MModular
Created by Henk-Jan Lebbink on 2/10/2024 in #questions
How to rewrite this code into something not ugly
I opened an issue for this.
10 replies
MModular
Created by Maxim on 1/17/2024 in #questions
how to use the gather function
I am not sure much larger than SIMD width will ever be faster but you would need to experiment to check. There are trade offs between pure SIMD speed, number of reads from heap, and how cache gets used. So testing different widths in your actual use case is worth doing.
6 replies
MModular
Created by Maxim on 1/17/2024 in #questions
how to use the gather function
Leandro Campos, has a branch with tests demonstrating how to do this. That exercises the new methods he is adding to DTypePointer. The use of the intrinsic itself is here. The pointer manipulation to use the intrinsic looks simple now that someone else figured it out for me. 🤦‍♂️ 🤷 😃
6 replies
MModular
Created by Tuatini on 4/6/2024 in #questions
official package manager
I think it will come from Modular. It is being planned and if you want to have input comment on this proposal.
4 replies
MModular
Created by artemiogr97 on 4/4/2024 in #questions
Lifecycle doc clarification
Int and Pointer are register passable and "trivial". So they can only be copied. For simple things in register copying is more efficient than moving.
5 replies
MModular
Created by AndryxaUA on 3/17/2024 in #questions
What is the size of the Int type in bits?
The thread with that answer also mentions the difficulty supporting 128-bit curently.
14 replies
MModular
Created by AndryxaUA on 3/17/2024 in #questions
What is the size of the Int type in bits?
I think it can vary by system to be 32- or 64-bit but is currently 64-bit because Mojo isn't targeting any 32-bit systems. See this previous answer from Modular. This is the MLIR Index type that answer refers to.
14 replies
MModular
Created by EzRyder on 3/14/2024 in #questions
Mojo Dict: Store struct instances based on a common trait as CollectionElement.
I don't think you can use a trait as the parameter when you need to make a concrete type. You need to pass an actual fully known type to Dict. using Pet[YourPet] works for me. If you want a Dict that holds a Pet with T either MyPet or YourPet, I think you would need to use a Variant[MyPet, YourPet]. I am not sure how well that will work but that is how you allow for two possible concrete types.
9 replies
MModular
Created by EzRyder on 3/14/2024 in #questions
Mojo Dict: Store struct instances based on a common trait as CollectionElement.
You need to provide the complete type as a parameter to Dict. Pet requires a parameter T and without that being specificed it is not concrete type that can be passed to Dict. so like this:
var d = Dict[StringKey, Pet[MyPet]]()
var d = Dict[StringKey, Pet[MyPet]]()
Also I think Dict now works witht a plain String. No need to use StringKey.
9 replies
MModular
Created by EzRyder on 3/14/2024 in #questions
Mojo Dict: Store struct instances based on a common trait as CollectionElement.
Mark Pet as conforming to CollectionElement. Like this:
struct Pet[T: TPet](CollectionElement):
struct Pet[T: TPet](CollectionElement):
If it is a simple struct that is not allocating or owning any pointers then mark it with @value decorator. That will ensure it has the functions necessary to be moved and copied the way Collections expect.
9 replies
MModular
Created by swig. on 3/9/2024 in #questions
Defining A DynamicVector containing References
I think you would probably want your vector to own the original objects and then it can use __refitem__ to return your References. I am not 100% clear on what you are trying to achieve but there are some examples of using References and linking them to lifetimes here. But for a lot of uses Ehsan is probably correct, though some cool stuff is already possible.
5 replies
MModular
Created by Aamir on 2/23/2024 in #questions
Fastest way to build string from dynamic values!
@Aamir , did a bug report ever get opened for this thow working with simple vec[i] ?
36 replies
MModular
Created by Mohamed Mabrouk on 3/6/2024 in #questions
In-place replacement of struct fields
I am not sure what is a real coding problem and what is just a formatting problem for what is on discord. But this code runs without error:
struct IOStream:
var source: Tensor[DType.int8]

fn __init__(inout self):
self.source = Tensor[DType.int8](100)

fn _resize(inout self):
self.source = Tensor[DType.int8](50) # fails as self.source is static


fn main():
var stream = IOStream()
stream._resize()
struct IOStream:
var source: Tensor[DType.int8]

fn __init__(inout self):
self.source = Tensor[DType.int8](100)

fn _resize(inout self):
self.source = Tensor[DType.int8](50) # fails as self.source is static


fn main():
var stream = IOStream()
stream._resize()
Since it is just creating a new tensor the size of the tensor being replaced is irrelevant. I am not sure what is meant by self.source being static. If you are trying to do something fancier like update the size I am not sure there is much of a distinciton between creating a new tensor or replacing the pointer in the old tensor. Both will require a new poiinter and possibly a copy which will be the slow parts. So probably creating a new tensor is easiest so all the size information gets created correclty.
4 replies
MModular
Created by Tuatini on 3/6/2024 in #questions
Call dynamic library functions
You want to look at the sys.ffi module . there are some examples of using them in this repo.
4 replies