modularml / mojo

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

free(): invalid pointer On Loading from Buffer #978

Closed erfanzar closed 1 year ago

erfanzar commented 1 year ago

Bug description

right now im building an ML library and i built my own tensors and there's a little problem with loading the data from buffer and i get this error belove :

Hidden Size             :  288
Max Position Embeddings :  256
Num Attention Heads     :  6
Num Hidden Layers       :  6
Vocab Size              :  32000
RMS Norm Epsilon        :  9.9999997473787516e-06
Number Repeat Key Value :  1
Number Key Value Heads  :  6
Intermediate Size       :  768
HEAD DIMS               :  48
KV DIMS                 :  288
free(): invalid pointer
[35649:35649:20231002,215819.047677:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[35649:35649:20231002,215819.047776: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: mojo run.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      0x000055d2e1f3f517
1  mojo      0x000055d2e1f3d0ee
2  mojo      0x000055d2e1f3fbef
3  libc.so.6 0x00007fe59b805520
4  libc.so.6 0x00007fe59b859a7c pthread_kill + 300
5  libc.so.6 0x00007fe59b805476 raise + 22
6  libc.so.6 0x00007fe59b7eb7f3 abort + 211
7  libc.so.6 0x00007fe59b84c6f6
8  libc.so.6 0x00007fe59b863d7c
9  libc.so.6 0x00007fe59b865ac4
10 libc.so.6 0x00007fe59b8684d3 free + 115
11 libc.so.6 0x00007fe53400263f free + 18446744071972692447
Aborted

Steps to reproduce

first clone https://github.com/erfanzar/EasyDeL.git

git clone https://github.com/erfanzar/EasyDeL.git

go to lib/mojo

cd lib/mojo

download these files

wget https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.bin
wget https://github.com/tairov/llama2.mojo/raw/master/tokenizer.bin

then paste this code and get the error :)

from EasyDel import (
    Array,
    ArrayShape,
    convert_numpy_to_easydel_array,
    matmul,
    matmul_shape,
    File,
    FileBuffer,
    read_file,
    BufReader,
    Tokenizer,
)

from math import math
from python import Python, PythonObject

struct LlamaConfig:
    var hidden_size: Int
    var num_attention_heads: Int
    var num_hidden_layers: Int
    var vocab_size: Int
    var rms_norm_eps: Float32
    var number_rep_kv: Int
    var max_position_embeddings: Int
    var num_key_value_heads: Int
    var head_dims: Int
    var kv_dims: Int
    var intermediate_size: Int

    fn __init__(
        inout self: Self,
        hidden_size: Int,
        num_attention_heads: Int,
        num_hidden_layers: Int,
        vocab_size: Int,
        rms_norm_eps: Float32,
        number_rep_kv: Int,
        max_position_embeddings: Int,
        num_key_value_heads: Int,
        intermediate_size: Int,
    ) -> None:
        self.hidden_size = hidden_size
        self.max_position_embeddings = max_position_embeddings
        self.num_attention_heads = num_attention_heads
        self.num_hidden_layers = num_hidden_layers
        self.vocab_size = vocab_size
        self.rms_norm_eps = rms_norm_eps
        self.number_rep_kv = number_rep_kv
        self.num_key_value_heads = num_key_value_heads
        self.intermediate_size = intermediate_size
        self.head_dims = hidden_size // num_attention_heads
        self.kv_dims = hidden_size // num_key_value_heads

    fn __init__(inout self: Self) -> None:
        self.hidden_size = 512
        self.max_position_embeddings = 2048
        self.num_attention_heads = 8
        self.num_hidden_layers = 8
        self.vocab_size = 32000
        self.rms_norm_eps = 1e-5
        self.number_rep_kv = 1
        self.num_key_value_heads = 1
        self.intermediate_size = self.hidden_size * 4
        self.head_dims = self.hidden_size // self.num_attention_heads
        self.kv_dims = self.hidden_size // self.num_key_value_heads

    fn __init__(inout self: Self, inout buffer: FileBuffer) raises -> None:
        self.hidden_size = (
            buffer.data.offset(buffer.offset).bitcast[DType.int32]().load(0).to_int()
        )
        buffer.move_offset(4)
        self.intermediate_size = (
            buffer.data.offset(buffer.offset).bitcast[DType.int32]().load(0).to_int()
        )
        buffer.move_offset(4)
        self.num_hidden_layers = (
            buffer.data.offset(buffer.offset).bitcast[DType.int32]().load(0).to_int()
        )
        buffer.move_offset(4)
        self.num_attention_heads = (
            buffer.data.offset(buffer.offset).bitcast[DType.int32]().load(0).to_int()
        )
        buffer.move_offset(4)
        self.num_key_value_heads = (
            buffer.data.offset(buffer.offset).bitcast[DType.int32]().load(0).to_int()
        )
        buffer.move_offset(4)

        self.vocab_size = (
            buffer.data.offset(buffer.offset).bitcast[DType.int32]().load(0).to_int()
        )
        buffer.move_offset(4)
        self.max_position_embeddings = (
            buffer.data.offset(buffer.offset).bitcast[DType.int32]().load(0).to_int()
        )
        buffer.move_offset(4)
        self.head_dims = self.hidden_size // self.num_attention_heads
        self.kv_dims = (
            self.num_key_value_heads * self.hidden_size
        ) // self.num_attention_heads
        self.number_rep_kv = self.num_attention_heads // self.num_key_value_heads
        self.rms_norm_eps = 1e-5
        return None

    fn print_config(self: Self) -> None:
        print("\033[1;36mHidden Size             : ", self.hidden_size)
        print("Max Position Embeddings : ", self.max_position_embeddings)
        print("Num Attention Heads     : ", self.num_attention_heads)
        print("Num Hidden Layers       : ", self.num_hidden_layers)
        print("Vocab Size              : ", self.vocab_size)
        print("RMS Norm Epsilon        : ", self.rms_norm_eps)
        print("Number Repeat Key Value : ", self.number_rep_kv)
        print("Number Key Value Heads  : ", self.num_key_value_heads)
        print("Intermediate Size       : ", self.intermediate_size)
        print("HEAD DIMS               : ", self.head_dims)
        print("KV DIMS                 : ", self.kv_dims)
        print_no_newline("\033[1;0m")

struct LlamaWeights[T: DType]:
    var wte: Array[T]
    var fcr: Array[T]
    var fci: Array[T]
    var input_norm_attn_weigth: Array[T]
    var wq: Array[T]
    var wk: Array[T]
    var wv: Array[T]
    var wo: Array[T]
    var post_norm_weigth: Array[T]
    var w1: Array[T]
    var w3: Array[T]
    var w2: Array[T]
    var final_norm_weight: Array[T]
    var lm_head: Array[T]

    fn __init__(
        inout self,
        config: LlamaConfig,
        shared_weights: Bool,
        inout buf: FileBuffer,
    ) raises:
        let size: Int = sizeof[T]()
        self.wte = Array[T](config.hidden_size, config.vocab_size)
        self.wte.set_data_from_buffer(
            buf.read_numerical_value_dynamic[T](self.wte.num_elements(), size)
        )

        self.input_norm_attn_weigth = Array[T](
            config.hidden_size, config.num_hidden_layers
        )
        self.input_norm_attn_weigth.set_data_from_buffer(
            buf.read_numerical_value_dynamic[T](
                self.input_norm_attn_weigth.num_elements(), size
            )
        )
        self.wq = Array[T](
            config.num_hidden_layers,
            config.hidden_size,
            config.hidden_size,
        )
        self.wq.set_data_from_buffer(
            buf.read_numerical_value_dynamic[T](self.wq.num_elements(), size)
        )
        self.wk = Array[T](
            config.num_hidden_layers,
            config.kv_dims,
            config.hidden_size,
        )
        self.wk.set_data_from_buffer(
            buf.read_numerical_value_dynamic[T](self.wk.num_elements(), size)
        )
        self.wv = Array[T](
            config.num_hidden_layers,
            config.kv_dims,
            config.hidden_size,
        )

        self.wv.set_data_from_buffer(
            buf.read_numerical_value_dynamic[T](self.wv.num_elements(), size)
        )

        self.wo = Array[T](
            config.num_hidden_layers,
            config.hidden_size,
            config.hidden_size,
        )

        self.wo.set_data_from_buffer(
            buf.read_numerical_value_dynamic[T](self.wo.num_elements(), size)
        )

        self.post_norm_weigth = Array[T](config.hidden_size, config.num_hidden_layers)
        self.post_norm_weigth.set_data_from_buffer(
            buf.read_numerical_value_dynamic[T](
                self.post_norm_weigth.num_elements(), size
            )
        )

        self.w1 = Array[T](
            config.num_hidden_layers,
            config.hidden_size,
            config.intermediate_size,
        )
        self.w1.set_data_from_buffer(
            buf.read_numerical_value_dynamic[T](self.w1.num_elements(), size)
        )

        self.w2 = Array[T](
            config.num_hidden_layers,
            config.hidden_size,
            config.intermediate_size,
        )
        self.w2.set_data_from_buffer(
            buf.read_numerical_value_dynamic[T](self.w2.num_elements(), size)
        )

        self.w3 = Array[T](
            config.num_hidden_layers,
            config.hidden_size,
            config.intermediate_size,
        )
        self.w3.set_data_from_buffer(
            buf.read_numerical_value_dynamic[T](self.w3.num_elements(), size)
        )

        self.final_norm_weight = Array[T](config.hidden_size)
        self.final_norm_weight.set_data_from_buffer(
            buf.read_numerical_value_dynamic[T](
                self.final_norm_weight.num_elements(), size
            )
        )

        self.fcr = Array[T](
            config.max_position_embeddings,
            (config.hidden_size // config.num_attention_heads) // 2,
        )
        self.fcr.set_data_from_buffer(
            buf.read_numerical_value_dynamic[T](self.fcr.num_elements(), size)
        )

        self.fci = Array[T](
            config.max_position_embeddings,
            (config.hidden_size // config.num_attention_heads) // 2,
        )
        self.fci.set_data_from_buffer(
            buf.read_numerical_value_dynamic[T](self.fci.num_elements(), size)
        )
        self.lm_head = Array[T](config.hidden_size, config.vocab_size)
        if shared_weights:
            self.lm_head.set_data_from_buffer(self.wte.data)
        else:
            self.lm_head.set_data_from_buffer(
                buf.read_numerical_value_dynamic[T](self.lm_head.num_elements(), size)
            )

fn run[nelts: Int, T: DType]() raises:
    let weights_path: StringRef = StringRef("stories15M.bin")
    let tokenizer_path: StringRef = StringRef("tokenizer.bin")
    var weights_buffer: FileBuffer = FileBuffer()
    var tokenizer_bufferr: FileBuffer = FileBuffer()

    read_file(weights_buffer, weights_path)
    read_file(tokenizer_bufferr, tokenizer_path)
    var config = LlamaConfig(weights_buffer)
    let tokenizer: Tokenizer = Tokenizer(config.vocab_size, tokenizer_bufferr)
    let is_tied: Bool = True if config.vocab_size > 0 else False
    if not is_tied:
        config.vocab_size = -config.vocab_size
    config.print_config()
    # print(weights_buffer.offset, " Of data readed total : ", weights_buffer.size)
    let llama: LlamaWeights[T] = LlamaWeights[T](config, is_tied, weights_buffer)
    print("Done !")

fn main() raises:
    run[Array[DType.float32].nelts, DType.float32]()

System information

mojo 0.3.1 (a3eed7c8)
ubuntu
Mogball commented 1 year ago

Hey, it's so exciting to see the stuff you're building with Mojo! This error is likely due to an offset pointer being passed to the Tensor type, or some other data structure that assumes it owns the backing pointer. I.e. it's probably a bug in your code.

erfanzar commented 1 year ago

actually i have test that many times the problem is with DTypePointer.free() and that will return The Error my code is fine