Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Bool Tensor does not match with BOOL SIMD from load #2492

Open
MadAlex1997 opened this issue May 3, 2024 · 5 comments
Open

[BUG] Bool Tensor does not match with BOOL SIMD from load #2492

MadAlex1997 opened this issue May 3, 2024 · 5 comments
Labels
bug Something isn't working mojo-repo Tag all issues with this label mojo-stdlib Tag for issues related to standard library

Comments

@MadAlex1997
Copy link

MadAlex1997 commented May 3, 2024

Bug description

The set_item and store methods of Tensor do not correctly assign values to the pointer to be retrieved by the load method as a SIMD when using DType.bool. It works for every other type I have tested. It only stores the true value every 8 index spots in the SIMD(perhaps corresponding to bits in a byte as I think SIMD bool is technically a one bit implementation).

Steps to reproduce

If you run the following code you should get the values in the triple quotes under the prints

from tensor import Tensor
import math
def main():
    var booltens = Tensor[DType.bool](32)
    for i in range(32):
        booltens[i] = True
    # Printing the tensor shows that the values have been assigned
    print(booltens)
    """Tensor([[True, True, True, ..., True, True, True]], dtype=bool, shape=32)"""
    # But when you grab the values as simd they clearly haven't
    print(booltens.load[width=32](0))
    """[True, False, False, False, False, False, False, False, True, False, False, False, False, False, False, False, True, False, False, False, False, False, False, False, True, False, False, False, False, False, False, False]"""
    # Then if you set them in all at once
    booltens.data().store[width=32](0,SIMD[DType.bool,1](True))
    # That works
    print(booltens.load[width=32](0))
    """[True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True]"""
    for i in range(32):
        booltens.store(i,True)
    print(booltens.load[width=32](0))
    """[True, False, False, False, False, False, False, False, True, False, False, False, False, False, False, False, True, False, False, False, False, False, False, False, True, False, False, False, False, False, False, False]"""
    ```


### System information

```shell
- What OS did you do install Mojo on ?
-- Windows 10 WSL 2 UBUNTU 22.04.2 LTS
- Provide version information for Mojo by pasting the output of `mojo -v`
-- 24.3 
- Provide Modular CLI version by pasting the output of `modular -v`
-- modular 0.7.4 (df7a9e8b)
@MadAlex1997 MadAlex1997 added bug Something isn't working mojo-repo Tag all issues with this label labels May 3, 2024
@soraros
Copy link
Contributor

soraros commented May 3, 2024

I bet Tensor.load is using the wrong DTypePointer.load overload internally.

@MadAlex1997
Copy link
Author

MadAlex1997 commented May 3, 2024

I bet Tensor.load is using the wrong DTypePointer.load overload internally.

That is possible I suppose, but the issue still occurs if I call store and load from the pointer under the tensor.

# Add to the bottom of the original snippet
for i in range(32):
        booltens._ptr.store[width=1](i,True)
    print(booltens._ptr.load[width=32](0))
"""[False, False, False, False, False, False, True, False, False, False, False, False, False, True, False, True, True, False, False, True, False, False, False, False, False, False, True, False, True, False, True, True]"""

I feel like I have heard that Mojo's Bool is an i1 and I feel like if that is the case what is happening is a misalignment between how different parts of the codebase handle that fact.

@soraros
Copy link
Contributor

soraros commented May 3, 2024

If you print the values one by one, it does work.

@MadAlex1997
Copy link
Author

If you print the values one by one, it does work.

That is interesting, but the issue still breaks vectorization.

What is even more interesting is what happens if you grab 2 at a time

from tensor import Tensor
import math
def main():
    var booltens = Tensor[DType.bool](32)
    booltens.data().store[width=32](0,SIMD[DType.bool,1](True))
    print(booltens._ptr.load[width=32](0))
    for i in range(0,32,2):
        print(booltens._ptr.load[width=2](i))
    """
[True, True]
[True, True]
[True, False]
[True, False]
[True, False]
[True, False]
[True, False]
[True, False]
[True, False]
[True, False]
[True, False]
[True, False]
[True, False]
[True, False]
[True, False]
[True, False]
    """

The first 2 grabs get the first 4 values correctly then after that it alternates with each load grabbing only one correct value.

@MadAlex1997
Copy link
Author

MadAlex1997 commented May 3, 2024

Also this

from tensor import Tensor
import math
def main():
    var booltens = Tensor[DType.bool](32)
    booltens.data().store[width=32](0,SIMD[DType.bool,1](True))
    print(booltens)
    for i in range(32):
        print(booltens[i])

Shows that when you set the values in the pointer from a simd store it (from the perspective of Tensor's __getitem__) only sets the value of the first 4 out of 32, again one-eighth of the items just the first one-eighth instead of every eight values. This means that the simd methods are likely treating it like an i1 bool while the tensor (and maybe DTypePointer) treats it like an 8-bit C bool.

@JoeLoser JoeLoser added the mojo-stdlib Tag for issues related to standard library label May 4, 2024
@ematejska ematejska removed the mojo-stdlib Tag for issues related to standard library label May 6, 2024
@ematejska ematejska added the mojo-stdlib Tag for issues related to standard library label May 7, 2024 — with Linear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mojo-repo Tag all issues with this label mojo-stdlib Tag for issues related to standard library
Projects
None yet
Development

No branches or pull requests

4 participants