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

Code provided by sushat to calculate the row mean of a tensor in the blog is not working #1600

Open
1-hussy opened this issue Jan 5, 2024 · 6 comments
Labels
bug Something isn't working crash mojo-repo Tag all issues with this label

Comments

@1-hussy
Copy link

1-hussy commented Jan 5, 2024

Bug description

Got giberish error from which I cannot anything
'/home/alex/.modular/pkg/packages.modular.com_mojo/bin/mojo' '/home/alex/mojo-course/test.mojo'
[17210:17210:20240106,013313.489154:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[17210:17210:20240106,013313.489295:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)
Please submit a bug report to https://github.com/modularml/mojo/issues and include the crash backtrace along with all the relevant source codes.
Stack dump:
0. Program arguments: /home/alex/.modular/pkg/packages.modular.com_mojo/bin/mojo /home/alex/mojo-course/test.mojo
#0 0x0000564455a06ee7 (/home/alex/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x60fee7)
#1 0x0000564455a04abe (/home/alex/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x60dabe)
#2 0x0000564455a075bf (/home/alex/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x6105bf)
#3 0x00007f9bde9a2520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00007f9b500016ad
Segmentation fault

this was the code snippet

from tensor import Tensor
from random import rand
import benchmark
from time import sleep
from algorithm import vectorize, parallelize

alias dtype = DType.float32
alias simd_width = simdwidthof[DType.float32]()

fn row_mean_naive[dtype: DType](t: Tensor[dtype]) -> Tensor[dtype]:
    var res = Tensor[dtype](t.dim(0),1)
    for i in range(t.dim(0)):
        for j in range(t.dim(1)):
            res[i] += t[i,j]
        res[i] /= t.dim(1)
    return res

fn row_mean_fast[dtype: DType](t: Tensor[dtype]) -> Tensor[dtype]:
    var res = Tensor[dtype](t.dim(0),1)
    @parameter
    fn parallel_reduce_rows(idx1: Int)->None:
        @parameter
        fn vectorize_reduce_row[simd_width: Int](idx2: Int) -> None:
            res[idx1] += t.simd_load[simd_width](idx1*t.dim(1)+idx2).reduce_add()
        vectorize[2*simd_width,vectorize_reduce_row](t.dim(1))
        res[idx1] /= t.dim(1)
    parallelize[parallel_reduce_rows](t.dim(0),t.dim(0))
    return res

fn main():
    let t = rand[dtype](1000,100000)
    var result = Tensor[dtype](t.dim(0),1)

    @parameter
    fn bench_mean():
        _ = row_mean_naive(t)
    
    @parameter
    fn bench_mean_fast():
        _ = row_mean_fast(t)

    let report = benchmark.run[bench_mean]()
    let report_fast = benchmark.run[bench_mean_fast]()
    report.print()
    report_fast.print()
    print("Speed up:",report.mean()/report_fast.mean())

main()

Steps to reproduce

Just run the code

from tensor import Tensor
from random import rand
import benchmark
from time import sleep
from algorithm import vectorize, parallelize

alias dtype = DType.float32
alias simd_width = simdwidthof[DType.float32]()

fn row_mean_naive[dtype: DType](t: Tensor[dtype]) -> Tensor[dtype]:
    var res = Tensor[dtype](t.dim(0),1)
    for i in range(t.dim(0)):
        for j in range(t.dim(1)):
            res[i] += t[i,j]
        res[i] /= t.dim(1)
    return res

fn row_mean_fast[dtype: DType](t: Tensor[dtype]) -> Tensor[dtype]:
    var res = Tensor[dtype](t.dim(0),1)
    @parameter
    fn parallel_reduce_rows(idx1: Int)->None:
        @parameter
        fn vectorize_reduce_row[simd_width: Int](idx2: Int) -> None:
            res[idx1] += t.simd_load[simd_width](idx1*t.dim(1)+idx2).reduce_add()
        vectorize[2*simd_width,vectorize_reduce_row](t.dim(1))
        res[idx1] /= t.dim(1)
    parallelize[parallel_reduce_rows](t.dim(0),t.dim(0))
    return res

fn main():
    let t = rand[dtype](1000,100000)
    var result = Tensor[dtype](t.dim(0),1)

    @parameter
    fn bench_mean():
        _ = row_mean_naive(t)
    
    @parameter
    fn bench_mean_fast():
        _ = row_mean_fast(t)

    let report = benchmark.run[bench_mean]()
    let report_fast = benchmark.run[bench_mean_fast]()
    report.print()
    report_fast.print()
    print("Speed up:",report.mean()/report_fast.mean())

main()

System information

mojo version 0.6
Os: Windows 11 (using wsl for windows)
core i7 10 gen
@1-hussy 1-hussy added bug Something isn't working mojo Issues that are related to mojo labels Jan 5, 2024
@1-hussy
Copy link
Author

1-hussy commented Jan 5, 2024

@shashankprasanna can you please look at this issue please

@arthurevans
Copy link
Collaborator

Hi. Can you verify that you're running WSL 2? You should be able to run wsl -l -v to see the version. If you're on 1, you can find Microsoft's upgrade instructions here: https://learn.microsoft.com/en-us/windows/wsl/install#upgrade-version-from-wsl-1-to-wsl-2

@1-hussy
Copy link
Author

1-hussy commented Jan 6, 2024

Hi, I am using
WSL version: 2.0.9.0
Kernel version: 5.15.133.1-1
WSLg version: 1.0.59
MSRDC version: 1.2.4677
Direct3D version: 1.611.1-81528511
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.22621.2861

@1-hussy
Copy link
Author

1-hussy commented Jan 6, 2024

now updated to the latest version of wsl but still facing the same error
WSL version: 2.0.14.0
Kernel version: 5.15.133.1-1
WSLg version: 1.0.59
MSRDC version: 1.2.4677
Direct3D version: 1.611.1-81528511
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.22621.2861

@ematejska
Copy link
Collaborator

With the main() there, I get the build-time error of:

error: TODO: expressions are not yet supported at the file scope level
main()
^

Do you have that in your file and can you remove? We also released v0.6.1 if you want to try that.

@1-hussy
Copy link
Author

1-hussy commented Jan 10, 2024

may be you are using it in a .mojo file thats why you got that error if you run the same code in notebook you have to write like this. Yep I know I was already updated to the latest version of mojo as well.

@linear linear bot removed the mojo Issues that are related to mojo label Apr 29, 2024
@linear linear bot changed the title Code provided by sushat to calculate the row mean of a tensor in the blog is not working Code provided by sushat to calculate the row mean of a tensor in the blog is not working Apr 29, 2024
@ematejska ematejska added the mojo-repo Tag all issues with this label label Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working crash mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

3 participants