cornell-zhang / hcl-dialect

HeteroCL-MLIR dialect for accelerator design
https://cornell-zhang.github.io/heterocl/index.html
Other
40 stars 17 forks source link

[Frontend] Add Arithmetic Operations on Tensor #69

Closed zzzDavid closed 2 years ago

zzzDavid commented 2 years ago

Adding arithmetic operations to Tensor seems like weird choice. But we need to support arithmetic and comparison operations on hcl.scalar. For example:

def core(instr, uop_mem, inp_mem, wgt_mem, acc_mem, out_mem, dram):
    '''VTA core'''
    opcode = hcl.scalar(instr[0:3], name='opcode')
    with hcl.if_(opcode == load.VTA_OPCODE_LOAD):
        load.load(instr, uop_mem, inp_mem, wgt_mem, acc_mem, dram)
    with hcl.elif_(opcode == store.VTA_OPCODE_STORE):
        store.store(instr, out_mem, dram)
    with hcl.elif_(opcode == alu.VTA_OPCODE_ALU):
        alu.alu(instr, uop_mem, acc_mem, out_mem)
    with hcl.elif_(opcode == gemm.VTA_OPCODE_GEMM):
        gemm.gemm(instr, uop_mem, inp_mem, wgt_mem, acc_mem, out_mem)
    with hcl.elif_(opcode == finish.VTA_OPCODE_FINISH):
        ...

hcl.scalar is a hcl.mlir.tensor.Tensor object, but it should act like a scalar value.

My thoughts are that we could add operations like __add__, __eq__ to hcl.mlir.tensor.Tensor class. @chhzh123 What do you think?

chhzh123 commented 2 years ago

I do not get the point here. hcl.Tensor is just a wrapper of hcl_mlir.TensorOp. All the operations are overloaded in hcl_mlir.TensorOp. I think users should use .v to access the actual value in hcl.scalar.

zzzDavid commented 2 years ago

I remember there was a confusion about whether to use scalar.v or just scalar with the old HCL. I think we can enforce using .v in the new version, so we won't need to add operations in the Tensor wrapper

chhzh123 commented 2 years ago

Right. Users should explicitly distinguish scalar and tensor by using .v.