Arthur Evans
MModular
•Created by Martin Dudek on 5/10/2024 in #questions
What UnsafePointer can point to and allocate mem for?
(Ultimately collections should only require
Movable
not Copyable
, but right now they require both, and set isn't currently Copyable
.)15 replies
MModular
•Created by Martin Dudek on 5/10/2024 in #questions
What UnsafePointer can point to and allocate mem for?
That seems like a bug. You should totally be able to have a list of
Set
s or a dict of Set
s or dare I say it a Set
of Set
s ... please open an issue!15 replies
MModular
•Created by Martin Dudek on 5/10/2024 in #questions
What UnsafePointer can point to and allocate mem for?
Oh, I see...
Set
doesn't have CollectionElement
. Weird.15 replies
MModular
•Created by Martin Dudek on 5/10/2024 in #questions
What UnsafePointer can point to and allocate mem for?
Any
@value
struct should qualify as a CollectionElement
I believe.15 replies
MModular
•Created by vmois on 12/30/2023 in #questions
Owned convention does not destroy variable after transferring ownership
A quick answer on the current state of affairs which might shed a little light here: the current Pointer is unsafe. The
owned
convention should force a copy of the value, but copying a Pointer doesn't copy the memory that it points to. I'm not positive, but I suspect you're seeing the same thing in the Pointer case and the Int case--the ^
operator is basically ignored because an Integer is just a value in a register, and we don't need to destruct it. (And a pointer is basically just an integer.)
In the case of the integer, you can see that the value was copied--the x
in main and the x
in take_value()
end up with different values.
The string example does what you'd expect it do with any non-trivial type: the value is destructed after you pass it in to take_text()
. You'd see the same result if you used a DynamicVector
in your first example instead of a raw Pointer
.5 replies
MModular
•Created by Henk-Jan Lebbink on 12/20/2023 in #questions
Does a pragma exist to switch off `mojo format`
Because it took me a while to find in the black docs (yes, "basics" was the last place I looked ...): https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#ignoring-sections
5 replies
MModular
•Created by VikasPoddar001 on 12/19/2023 in #questions
change in syntax for types declaration
Also, the types themselves are different. I.e.,
Int
versus int
isn't a matter of the compiler looking at capitalization... int
is a Python class. Int
is a Mojo struct.4 replies
MModular
•Created by Vijay Yadav on 12/19/2023 in #questions
How to convert string to float value?
Here is the really cheesy way to do it:
12 replies
MModular
•Created by clarkezone on 12/15/2023 in #questions
Difference between Float32 and DType.float32
They're both defined in the standard library. There's currently no mechanism to add new DTypes (there is already one for
bfloat16
, but I don't know how complete the support is). You can find the list of current DTypes here: https://docs.modular.com/mojo/stdlib/builtin/dtype.html#dtype
Theoretically you can write a new type, as described in Low-Level IR with Mojo: https://docs.modular.com/mojo/notebooks/BoolMLIR.html ... but this is a really advanced topic that we don't have any documentation for at the moment.7 replies
MModular
•Created by Noe on 12/9/2023 in #questions
Term paper about Mojo - Open questions
Excellent. Good luck with the term paper!
8 replies
MModular
•Created by clarkezone on 12/15/2023 in #questions
Difference between Float32 and DType.float32
I'm going to take a short tangent here. I swear it's related. I don't know if you've touched on
SIMD
yet--it's another type that tends to confuse folks, and this part is kind of related to AI/ML. SIMD (single instruction, multiple data) is a technique for parallelizing data operations when you want to do a lot of similar operations. A Mojo SIMD
type is a small, fixed-sized vector--or array--of numeric data types. These are used heavily in low-level AI/ML programming, so they're built right in to the Mojo standard library. A SIMD[DType.float32, 8]
is a vector of 8 Float32
values. You can do something fancy like take the square root of all of the values with a single operation:
With SIMD, doing 8 operations at once takes about the same time as doing one—so you can see why this is desirable when you're crunching a lot of numbers.
OK, so I told you this was related, and it is. What exactly is that Float32
type. you're using? It's actually an alias for SIMD[DType.float32, 1]
. That's right, it's a SIMD vector holding one 32-bit floating point number.
When you create a DTypePointer[DType.float32]
, it's basically the same as creating pointer to an array of Float32
scalar values.7 replies
MModular
•Created by clarkezone on 12/15/2023 in #questions
Difference between Float32 and DType.float32
TL;DR:
DType
describes a data format in the abstract. Types like DTypePointer
, SIMD
, and Buffer
use DTypes to describe the data they store. Float32
is a concrete type which you can instantiate.
These aren't really related to AI/ML or Python.Float32
is a type. You can instantiate a Float32
, like you did in your code (Float32(i)
). DType
is a structure that provides various utilities for working with data types, including enumerated values (enums) describing various data types, like float32
. So DType.float32
is a value that describes a type.
It's used in a variety of generic types like DTypePointer
and SIMD
to describe the type of data being stored. You can't create a DType.float32, but you can create a Float32
(which is a scalar value of that dtype) or a Buffer
or SIMD
vector made up of DType.float32
values.7 replies
MModular
•Created by stu002 on 12/15/2023 in #questions
can I implement a trait for a third party type?
You're correct. You cannot currently implement a trait for a type that you don't control. The team fully intends to fix this, but there are a couple of possible approaches (for example, allowing implicit conformance to traits, or an extension mechanism like Swift (in Rust, adding an implementation I think). Stay tuned.
3 replies
MModular
•Created by Noe on 12/9/2023 in #questions
Term paper about Mojo - Open questions
@Noe your compiled binary is loading the CPython interpreter (from a shared library). Objects constructed by your code are Mojo objects--including literals. If you look at the doc for PythonObject: https://docs.modular.com/mojo/stdlib/python/object.html#pythonobject
You'll see that every PythonObject has a
py_object
field, which is a pointer to a PyObject--that's the representation of a native Python object in Python's C API. When you import a Python module, you get a PythonObject
wrapping that module. When you call a function on that object, PythonObject
in turn makes a call to the CPython interpreter, passing it the underlying PyObject
pointer and Python versons of any (Mojo) arguments you pass in.
Whatever Python object that function returns gets wrapped as another PythonObject
, and so on. For more information on PyObject
and the Python interpreter interface, you might start here: https://docs.python.org/3/c-api/intro.html8 replies
MModular
•Created by roopesh puthalath on 9/20/2023 in #questions
Bug, while loop Re-setting values?
Great, glad to hear it!
17 replies
MModular
•Created by roopesh puthalath on 9/20/2023 in #questions
Bug, while loop Re-setting values?
def open_file_explorer() -> String:
17 replies
MModular
•Created by roopesh puthalath on 9/20/2023 in #questions
Bug, while loop Re-setting values?
selected_file_path
is already a PythonObject. So you should be able to just do:
return selected_file_path.to_string()
17 replies
MModular
•Created by roopesh puthalath on 9/20/2023 in #questions
Bug, while loop Re-setting values?
I think the
var state
inside the loop is creating a new state
variable scoped inside the while
loop (or possibly inside the if
statement?).17 replies