chsasank / llama.lisp

Lisp dialect designed for HPC and AI
GNU Lesser General Public License v2.1
15 stars 6 forks source link

raise ValueError("Operands must be the same type, got (%s, %s)" ValueError: Operands must be the same type, got (float, i32) #58

Closed adi-lb-phoenix closed 4 months ago

adi-lb-phoenix commented 4 months ago

Command executed :

guile ../../utils/sexp-json.scm < llama2.c/mat_mul.sexp | python ../../c-lisp.py | python ../../brilisp.py | python ../../llvm.py 

c-lisp code executed :

(c-lisp
        (define ((mat-cons void) (arr (ptr float)) (len int))
                (declare n int)
                (declare m float)
                (set m 0.0)
                (for ((set n 0) (lt n len) (set n (add n 1)) )
                        (set m (add m 1))
                        (store (ptradd arr n) m )
                )
                (ret)
        )
        (define ((fprint float) (n  float) ))

        (define ((matmul_print void) (arr (ptr float)) (len int))
                (declare i int)
                (for ((set i 0) (lt i len) (set i (add i 1))) 
                        (call fprint (load (ptradd arr i)))
                )
                (ret) 
        ) 

 ;x is 10 , xout and w is 100 . d is 10 . n is 10
        (define ((mat_mul void) (xout (ptr float)) (x (ptr float)) (w (ptr float)) (n int) (d int)) 
                (declare i int)
                (for ( (set i 0) (lt i d) (set i (add i 1)) )
                        (declare val float)
                        (set val 0.0)
                        (declare j int)
                        (for ((set j 0) (lt j n) (set j (add j 1)))
                           (set val (fadd val (fmul (load (ptradd w (add (mul i n) j))) (load (ptradd x j)))))
                        )
                        (store (ptradd xout i) val)
                )
                (ret)  
        )

        (define ((main void))
                (declare xout (ptr float))
                (declare x (ptr float))
                (declare w (ptr float))
                (declare n int )
                (declare d int)

                (set xout (alloc float 100))
                (set x (alloc float 10))
                (set w (alloc float 100))
                (set n 10)
                (set d 10)
                (call mat-cons w 100)
                (call mat-cons x 10)
                (call mat_mul xout x w n d)
                (call matmul_print xout 100)
                ;(call fprint xout)
                (ret) 
        )
)

Output :

Exception: Operands must be the same type, got (float, i32); in instruction Munch({'op': 'add', 'type': 'int', 'dest': 'tmp_clisp-qmeitobd', 'args': ['tmp_clisp.inp_0-zbyttyvj', 'tmp_clisp.inp_1-aamllizm']}):
Traceback (most recent call last):
  File "/home/aadithya.bhat/c_lisp/bril/bril-txt/llama.lisp/src/backend/tests/c-lisp/../../llvm.py", line 359, in <module>
    main()
  File "/home/aadithya.bhat/c_lisp/bril/bril-txt/llama.lisp/src/backend/tests/c-lisp/../../llvm.py", line 354, in main
    code_gen.generate(bril_prog)
  File "/home/aadithya.bhat/c_lisp/bril/bril-txt/llama.lisp/src/backend/tests/c-lisp/../../llvm.py", line 44, in generate
    self.gen_function(fn)
  File "/home/aadithya.bhat/c_lisp/bril/bril-txt/llama.lisp/src/backend/tests/c-lisp/../../llvm.py", line 345, in gen_function
    self.gen_instructions(fn_instrs)
  File "/home/aadithya.bhat/c_lisp/bril/bril-txt/llama.lisp/src/backend/tests/c-lisp/../../llvm.py", line 261, in gen_instructions
    raise e
  File "/home/aadithya.bhat/c_lisp/bril/bril-txt/llama.lisp/src/backend/tests/c-lisp/../../llvm.py", line 252, in gen_instructions
    gen_value(instr)
  File "/home/aadithya.bhat/c_lisp/bril/bril-txt/llama.lisp/src/backend/tests/c-lisp/../../llvm.py", line 163, in gen_value
    llvm_instr(*[self.gen_var(arg) for arg in instr.args], name=instr.dest),
  File "/home/aadithya.bhat/.local/lib/python3.10/site-packages/llvmlite/ir/builder.py", line 34, in wrapped
    raise ValueError("Operands must be the same type, got (%s, %s)"
ValueError: Operands must be the same type, got (float, i32)
adi-lb-phoenix commented 4 months ago

In function (mat-cons void), we set set m as 0.0 using the instruction (set m 0.0), but while performing an addition operation that adds a value of 1 to m and stores in m . A change should be made in the syntax from (set m (add m 1)) to (set m (fadd m 1.0)) because to add a floating point integer we need a floating point value and an opcode designed to handle floating point values.