heterocl.scheme

Quantization scheme.

class Scheme(inputs, func)[source]

Bases: object

A quantization scheme.

To create a scheme, use heterocl.create_scheme. A scheme has two methods: one is to downsize tensors to integer type and the other is to quantize tensors to non-integer type. The scheme should only be created by the API. Users should not directly call the constructor.

Parameters
  • inputs (list of Tensor) – A list of input tensors to the scheme

  • func (callable) – The algorithm definition

Variables
  • inputs (list of Tensor) – A list of input tensors to the scheme

  • func (callable) – The algorithm definition

  • dtype_dict (dict(str, Type)) – A dictionary that maps between a name and its data type

Examples

# example 1 - downsize
hcl.init(hcl.Int(32))
A = hcl.placeholder((10,))
def kernel(A):
    return hcl.compute(A.shape, lambda x: A[x]+1, "B")
s = hcl.create_scheme(A, kernel)
# downsize tensor B to a 4-bit unsigned integer
s.downsize(kernel.B, hcl.UInt(4))

# example 1 - quantize
hcl.init(hcl.Float())
A = hcl.placeholder((10,))
def kernel(A):
    return hcl.compute(A.shape, lambda x: A[x]+1, "B")
s = hcl.create_scheme(A, kernel)
# quantize tensor B to a 4-bit unsigned fixed point
s.quantize(kernel.B, hcl.Fixed(4, 2))
current = <heterocl.scheme.Scheme object>

The current scheme.

downsize(inputs, dtype)[source]

Downsize a (list of) tensor to the specified integer type.

Parameters
  • inputs (Tensor of list of Tensor) – The tensor(s) to be downsized

  • dtype (Type) – The target data type

quantize(inputs, dtype)[source]

Quantize a (list of) tensor to the specified fixed-point type.

Parameters
  • inputs (Tensor of list of Tensor) – The tensor(s) to be quantized

  • dtype (Type) – The target data type