modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
23.16k stars 2.59k forks source link

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

Open 1-hussy opened 9 months ago

1-hussy commented 9 months ago

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:

  1. 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 commented 9 months ago

@shashankprasanna can you please look at this issue please

arthurevans commented 9 months ago

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 commented 9 months ago

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 commented 9 months ago

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 commented 9 months ago

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 commented 9 months ago

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.