chsasank / llama.lisp

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

TypeError: unhashable type: 'list'. #51

Closed adi-lb-phoenix closed 3 months ago

adi-lb-phoenix commented 3 months ago

Command executed :

uile ../../utils/sexp-json.scm < matrix_multiplication.sexp | python3 ../../c-lisp.py 

This is the code that was written in c-lisp (matrix_multiplication.sexp) :


(c-lisp
    (define ((mat-cons void) (arr (ptr float)) (len int))
        (declare (n int))
        (declare (m float))
        (set m 71.5)
        (for (((set n 0) (lt n len)) (set n (add n 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 2) (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 received :

Error in statement: ['for', [[['set', 'n', 0], ['lt', 'n', 'len']], ['set', 'n', ['add', 'n', 1]]], ['store', ['ptradd', 'arr', 'n'], 'm']]
Traceback (most recent call last):
  File "/home/aadithya.bhat/c_lisp/bril/bril-txt/llama.lisp/src/backend/tests/llama2.c/../../c-lisp.py", line 430, in <module>
    json.dump(brilisp_code_generator.c_lisp(json.loads(sys.stdin.read())), sys.stdout)
  File "/home/aadithya.bhat/c_lisp/bril/bril-txt/llama.lisp/src/backend/tests/llama2.c/../../c-lisp.py", line 58, in c_lisp
    return ["brilisp"] + [self.gen_function(fn) for fn in prog[1:]]
  File "/home/aadithya.bhat/c_lisp/bril/bril-txt/llama.lisp/src/backend/tests/llama2.c/../../c-lisp.py", line 58, in <listcomp>
    return ["brilisp"] + [self.gen_function(fn) for fn in prog[1:]]
  File "/home/aadithya.bhat/c_lisp/bril/bril-txt/llama.lisp/src/backend/tests/llama2.c/../../c-lisp.py", line 93, in gen_function
    *self.gen_compound_stmt(func[2:], new_scope=False),
  File "/home/aadithya.bhat/c_lisp/bril/bril-txt/llama.lisp/src/backend/tests/llama2.c/../../c-lisp.py", line 256, in gen_compound_stmt
    instr_list += self.gen_stmt(s)
  File "/home/aadithya.bhat/c_lisp/bril/bril-txt/llama.lisp/src/backend/tests/llama2.c/../../c-lisp.py", line 119, in gen_stmt
    raise e
  File "/home/aadithya.bhat/c_lisp/bril/bril-txt/llama.lisp/src/backend/tests/llama2.c/../../c-lisp.py", line 110, in gen_stmt
    return self.gen_for_stmt(stmt)
  File "/home/aadithya.bhat/c_lisp/bril/bril-txt/llama.lisp/src/backend/tests/llama2.c/../../c-lisp.py", line 166, in gen_for_stmt
    init_expr_instr = self.gen_expr(stmt[1][0])
  File "/home/aadithya.bhat/c_lisp/bril/bril-txt/llama.lisp/src/backend/tests/llama2.c/../../c-lisp.py", line 414, in gen_expr
    elif self.is_fixed_type_expr(expr):
  File "/home/aadithya.bhat/c_lisp/bril/bril-txt/llama.lisp/src/backend/tests/llama2.c/../../c-lisp.py", line 323, in is_fixed_type_expr
    return expr[0] in self.fixed_op_types
TypeError: unhashable type: 'list'
adi-lb-phoenix commented 3 months ago

Solved it by changing the loop in define (mat-cons) from

(for (((set n 0) (lt n len)) (set n (add n 1)) )

to

(for ( (set n 0) (lt n len) (set n (add n 1)  )) 

The parenthesis were not in order. As all the conditions that lets a for loop operate must be within a single parenthesis ex for ( conditions )