modularml / mojo

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

[BUG] Indexing `Tensor` in `struct` gives a segmentation fault #2596

Open hylkedonker opened 6 months ago

hylkedonker commented 6 months ago

Bug description

I am unable to run/compile a simple struct that computes the cumulative sum of a tensor.Tensor. Hereby the segmentation fault:

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/user/.modular/pkg/packages.modular.com_mojo/bin/mojo /home/user/mojo-tutorial/index_error.mojo Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var LLVM_SYMBOLIZER_PATH to point to it): 0 mojo 0x000057e34a13f407 1 mojo 0x000057e34a13d25e 2 mojo 0x000057e34a13fa9f 3 libc.so.6 0x000077c7b2642990 4 mojo 0x000057e34b90fd34 5 mojo 0x000057e34b90fc9c 6 mojo 0x000057e34b90fbdf 7 mojo 0x000057e34b91afd8 8 mojo 0x000057e34c570378 9 libKGENCompilerRTShared.so.19.0git 0x000077c7aa83b7cc KGEN_CompilerRT_AlignedAlloc + 124 10 libKGENCompilerRTShared.so.19.0git 0x000077c758001546 KGEN_CompilerRT_AlignedAlloc + 18446744072325193206 mojo crashed! Please file a bug report. [1] 24041 segmentation fault (core dumped) '/home/user/.modular/pkg/packages.modular.com_mojo/bin/mojo'

Steps to reproduce

This is a listing of the code to reproduce the error:

from tensor import Tensor

@value
struct CumulativeCache[T: DType]:
    var data: Tensor[DType.int32]

    fn __init__(inout self, data: Tensor[DType.int32]):
        var length = data.shape()[0]
        self.data = Tensor[DType.int32](length + 1, 0)
        var cumsum: Int32 = 0
        for i in range(length):
            cumsum += data[i]
            self.data[i + 1] = cumsum
        print(self.data)

def main():
    var m = List[Int32](2, 1, 3)
    var m_tensor = Tensor[DType.int32](3, list=m)
    var a = CumulativeCache[DType.float64](m_tensor)

Note that when I remove the print statement from the constructor, it no longer crashes.

System information

OS: Ubuntu 23.10
`mojo -v`: mojo 24.3.0 (9882e19d)
`modular -v`: modular 0.7.4 (df7a9e8b)
ematejska commented 5 months ago

This does not reproduce locally for me in 24.3.0 or the latest nightly build. It also doesn't reproduce in the online playground https://docs.modular.com/mojo/playground. Does this still reproduce for you?

hylkedonker commented 5 months ago

Yes, it does. Also on Ubuntu 24.04. I was able to reproduce the problem on Docker using the following Dockerfile:

FROM ubuntu:24.04

ARG DEFAULT_TZ=Europe/Amsterdam
ENV DEFAULT_TZ=$DEFAULT_TZ

RUN apt-get update \
   && DEBIAN_FRONTEND=noninteractive TZ=$DEFAULT_TZ apt-get install -y \
   tzdata \
   libedit2 \
   python3-numpy \
   vim \
   sudo \
   curl \
   wget \
   git && \
   rm -rf /var/lib/apt/lists/*

# A random default token
ARG AUTH_KEY=5ca1ab1e
ENV AUTH_KEY=$AUTH_KEY

RUN curl https://get.modular.com | sh - && \
    modular auth $AUTH_KEY
RUN modular install mojo

ARG MODULAR_HOME="/root/.modular"
ENV MODULAR_HOME=$MODULAR_HOME
ENV PATH="$PATH:$MODULAR_HOME/pkg/packages.modular.com_mojo/bin"