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],
)