Still struggling with type errors in Mojo around LayoutTensor and tiles

I have been banging my head on type inference around LayoutTensor. I have a couple of errors when running magic run test in this repo: GitHub - winding-lines/nms.mojo: Non Maximum Suppression in Mojo

Included from /home/marius/dev/nms.mojo/test_correctness.mojo:18:
/home/marius/dev/nms.mojo/operations/nms.mojo:38:15: error: cannot implicitly convert 'IntLiteral[0]' value to 'DType'
        res = 0
              ^
Included from /home/marius/dev/nms.mojo/test_correctness.mojo:18:
/home/marius/dev/nms.mojo/operations/nms.mojo:54:24: error: cannot implicitly convert 'SIMD[dtype, __init__[::Origin[::Bool(IntTuple(1), IntTuple(1
)).size()]' value to 'DType'
    res = intersection / union
          ~~~~~~~~~~~~~^~~~~~~
Included from /home/marius/dev/nms.mojo/test_correctness.mojo:18:
/home/marius/dev/nms.mojo/operations/nms.mojo:111:70: error: invalid call to 'iou': argument #0 cannot be converted from 'LayoutTensor[dtype, _compute_tile_layout[*::Int]().__getitem__(0),
 MutableAnyOrigin, layout_int_type=_get_layout_type(corners_layout, AddressSpace(0)), linear_idx_type=_get_index_type(corners_layout, AddressSpace(0)), masked=_tile_is_masked[::Layout,*::I
nt]()]' to 'LayoutTensor[dtype, _compute_tile_layout[*::Int]().__getitem__(0), MutableAnyOrigin]'
                var overlap = iou[dtype, first.layout, second.layout](
                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
Included from /home/marius/dev/nms.mojo/test_correctness.mojo:18:
/home/marius/dev/nms.mojo/operations/nms.mojo:112:21: note: types parameters include unfolded expression at parser time; try rebinding to a consistent type?
                    first, second
                    ^
Included from /home/marius/dev/nms.mojo/test_correctness.mojo:18:
/home/marius/dev/nms.mojo/operations/nms.mojo:22:4: note: function declared here
fn iou[
   ^
/home/marius/dev/nms.mojo/.magic/envs/default/bin/mojo: error: failed to parse the provided Mojo source module
m

Any pointers appreciated :slight_smile:

Marius

Hi Marius,

I was looking at your code just now.

  1. the iou function has to return a value, so you might want to do return intersection / union at the end of that function.
  2. the return type of the function should be first.element_type

Hope that fixes your problem. Let me know if you encountered anything new.

1 Like

Many thanks Jackie,

I was trying to use an out parameter but got stuck half-way to the transition. Setting the return type of the iou function to first.element_type magically fixed the errors. I pushed one commit with that change.

I still have the errors on the call site, any advice there?

Included from /home/marius/dev/nms.mojo/test_correctness.mojo:18:
/home/marius/dev/nms.mojo/operations/nms.mojo:38:15: error: cannot implicitly convert 'IntLiteral[0]' value to 'DType'
        res = 0
              ^
Included from /home/marius/dev/nms.mojo/test_correctness.mojo:18:
/home/marius/dev/nms.mojo/operations/nms.mojo:54:24: error: cannot implicitly convert 'SIMD[dtype, __init__[::Origin[::Bool(IntTuple(1), IntTuple(1
)).size()]' value to 'DType'
    res = intersection / union
          ~~~~~~~~~~~~~^~~~~~~
Included from /home/marius/dev/nms.mojo/test_correctness.mojo:18:
/home/marius/dev/nms.mojo/operations/nms.mojo:111:70: error: invalid call to 'iou': argument #0 cannot be converted from 'LayoutTensor[dtype, _compute_tile_layout[*::Int]().__getitem__(0),
 MutableAnyOrigin, layout_int_type=_get_layout_type(corners_layout, AddressSpace(0)), linear_idx_type=_get_index_type(corners_layout, AddressSpace(0)), masked=_tile_is_masked[::Layout,*::I
nt]()]' to 'LayoutTensor[dtype, _compute_tile_layout[*::Int]().__getitem__(0), MutableAnyOrigin]'
                var overlap = iou[dtype, first.layout, second.layout](
                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
Included from /home/marius/dev/nms.mojo/test_correctness.mojo:18:
/home/marius/dev/nms.mojo/operations/nms.mojo:112:21: note: types parameters include unfolded expression at parser time; try rebinding to a consistent type?
                    first, second
                    ^
Included from /home/marius/dev/nms.mojo/test_correctness.mojo:18:
/home/marius/dev/nms.mojo/operations/nms.mojo:22:4: note: function declared here
fn iou[
   ^
/home/marius/dev/nms.mojo/.magic/envs/default/bin/mojo: error: failed to parse the provided Mojo source module

Because the output type of iou is of first.element_type, you will need to wrap the 0 with that type. So, essentially instead of return 0, do return first.element_type(0) in line 37.

BTW, doing out res: first.element_type like what you did originally is fine. out indicates the output type. That is why you will need to return a value. If you want a mutable variable, you need to do mut instead.

Sorry I copy pasted the wrong compiler output (I am ssh-ing into my GPU server and the mouse is playing tricks), Here is the actual output after fixing the function definition

Included from /home/marius/dev/nms.mojo/test_correctness.mojo:18:
/home/marius/dev/nms.mojo/operations/nms.mojo:109:70: error: invalid call to 'iou': argument #0 cannot be converted from 'LayoutTensor[dtype, _compute_tile_layout[*::Int]().__getitem__(0),
 MutableAnyOrigin, layout_int_type=_get_layout_type(corners_layout, AddressSpace(0)), linear_idx_type=_get_index_type(corners_layout, AddressSpace(0)), masked=_tile_is_masked[::Layout,*::I
nt]()]' to 'LayoutTensor[dtype, _compute_tile_layout[*::Int]().__getitem__(0), MutableAnyOrigin]'
                var overlap = iou[dtype, first.layout, second.layout](
                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
Included from /home/marius/dev/nms.mojo/test_correctness.mojo:18:
/home/marius/dev/nms.mojo/operations/nms.mojo:110:21: note: types parameters include unfolded expression at parser time; try rebinding to a consistent type?
                    first, second
                    ^
Included from /home/marius/dev/nms.mojo/test_correctness.mojo:18:
/home/marius/dev/nms.mojo/operations/nms.mojo:22:4: note: function declared here
fn iou[

Can you define your iou function like this for now? I will try to improve the ergonomic in the near future.

fn iou[
    dtype: DType, f_layout: Layout, s_layout: Layout
](
    first: LayoutTensor[
        dtype,
        f_layout,
        MutableAnyOrigin,
        layout_int_type=_,
        linear_idx_type=_,
        masked=_,
    ],
    second: LayoutTensor[
        dtype,
        s_layout,
        MutableAnyOrigin,
        layout_int_type=_,
        linear_idx_type=_,
        masked=_,
    ],
) -> first.element_type:

Also, you can just call the function without passing in any parameter

var overlap = iou(first, second)
1 Like

This worked! I tried so many things :joy:

Could you explain the error message/thought process so that I can be more self sufficient next time? What is the compiler trying to do here?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.