Skip to content

# Module: `vector`

## `DataType`

Bases: `IntEnum`

Supported data types

Source code in `src/infernet_ml/utils/codec/vector.py`

    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    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`

    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    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],
        )