Skip to content

Module: vector

DataType

Bases: IntEnum

Supported data types

Source code in src/infernet_ml/utils/codec/vector.py
class DataType(IntEnum):
    """Supported data types"""

    float = 0
    double = 1
    cfloat = 2
    cdouble = 3
    half = 4
    bfloat16 = 5
    uint8 = 6
    int8 = 7
    short = 8
    int = 9
    long = 10
    bool = 11

encode_vector(dtype, shape, values)

Shape of the tensor will be flattened.

Source code in src/infernet_ml/utils/codec/vector.py
def encode_vector(
    dtype: DataType, shape: Tuple[int, ...], values: Union[Tensor, List[Any]]
) -> bytes:
    """
    Shape of the tensor will be flattened.
    """
    if isinstance(values, Tensor):
        _values = values.flatten().tolist()
    else:
        _values = np.array(values).flatten().tolist()

    map_fn_lookup = {
        DataType.float: float_to_u32,
        DataType.double: double_to_u64,
        DataType.cfloat: cfloat_to_u64,
        DataType.cdouble: cdouble_to_u128,
        DataType.half: half_to_u16,
        DataType.bfloat16: bfloat16_to_u16,
        DataType.uint8: lambda x: x,
        DataType.int8: lambda x: x,
        DataType.short: lambda x: x,
        DataType.int: lambda x: x,
        DataType.long: lambda x: x,
        DataType.bool: lambda x: x,
    }
    __values = _map(map_fn_lookup[dtype], _values)

    _type = SOLIDITY_TYPE_LOOKUP[dtype]

    return encode(
        ["uint8", "uint16[]", f"{_type}[]"],
        [dtype, shape, __values],
    )