Question on LayoutTenson mut parameter

Hello,

I’m confused about the way mut parameter works in LayoutTensor.

For example in gpu puzzle 16 ( and others) the out_tensor is declared in main with mut=False but in the parameter list of the kernel functions mut=True for output argument and False for input, as I naively expect. I would have expected the compiler to flag this error as output changes type.

What do I miss? Could someone explain to to me?

Kind regards,

Lucian

Hi @antonl2219 ! :waving_hand:

Thank you for bringing this up and pointing this out. So you are correct, in puzzle 16 it should be marked with mut=True as we “mutate” the contents of the tensor inside the kernel.

The reason this is not currently caught by the compiler is two-fold:

  1. A bug in LayoutTensor and its underlying type UnsafePointer. Theoretically, this should fail to compile (as you pointed out) but we need to do some bug fixing on our handling of mutabilities for LayoutTensor and UnsafePointer.
  2. The enqueue_function call does not do any type-checking currently, but this is being worked on as we move over to use enqueue_function_checked instead.

If you want to, you could open a pull request on the gpu-puzzles repository and fix this issue for problem #16 (and others you might have found).

These mutability issues will be fixed in a future version of Mojo (hopefully 26.1 :crossed_fingers:)

Let us know if you have any follow up questions or concerns :slight_smile:

Hi,

Thanks for the clear explanation.

I was concern that I don’t graps the Mojo’s ownership model.

Lucian

I’ve put up a pull request to migrate puzzles 13-16 to enqueue_function_checkedand in the process fixing these issues with mutability. Thanks for pointing it out, we’ll also try to fix other places in the puzzles where this may have occurred.

1 Like