This Week in Mojo 2023-08-04

Mojo Playground Update

⭐ New

A new Tensoropen in new window type has been introduced. This tensor type manages its own data (unlike NDBuffer and Buffer which are just views). Therefore, the tensor type performs its own allocation and free. Here is a simple example of using the tensor type to represent an RGB image and convert it to grayscale:

from DType import DType
from Tensor import Tensor, TensorShape
from Index import Index
from Random import rand

let height = 256
let width = 256
let channels = 3

# Create the tensor of dimensions height, width, channels and fill with
# random value.
let image = rand[DType.float32](TensorShape(height, width, channels))

# Declare the grayscale image.
var gray_scale_image = Tensor[DType.float32](TensorShape(height, width))

# Perform the RGB to grayscale transform.
for y in range(height):
    for x in range(width):
        let r = image[y,x,0]
        let g = image[y,x,1]
        let b = image[y,x,2]
        gray_scale_image[Index(y,x)] = 0.299 * r + 0.587 * g + 0.114 * b

🛠️ Fixed

  • Issue #53open in new window: Int now implements true division with the / operator. Similar to Python, this returns a 64-bit floating point number. The corresponding in-place operator, /=, has the same semantics as //=.

Team Answers

Modular and Mojo Docs

we use Quarto (for now) but this will change to our own custom solution for a number of reasons

Python PEP 703 - Optional Global Interpreter Lock

It should be strictly compatible with Mojo's use of CPython and I think it is a good move for the Python ecosystem in general. I'm seeing a lot of folks that seem to be declaring success early, my read of PEP703 is that there is still a lot of work to do to figure things out and land things. The Python core team summaryopen in new window is really great and I highly recommend reading it. It seems like the transition will take a few years. I'm personally very curious how heavy users of CPython internals (e.g. TensorFlow and PyTorch) will handle the transition - the changes are pretty profound, breaking core C APIs like PyList_GetItem.

Also my read of the code in the implementation seems that they may need to iterate a bit. One of the core operations in the critical path for performance is _Py_ThreadId and my read of it is that it will break code that uses thread local storage for other things by directly scribbling into it in a very low-level wayopen in new window

That said, there are a ton of crazy smart people working on this and everyone seems highly motivated. Overall, it's a great step forward for the ecosystem in any case in my opinion.