Module: vector
This module provides utilities for encoding and decoding vectors of data types supported by Ritual. At the core of it, it contains RitualVector. RitualVector is an abstraction of a vector of data, with utility methods to convert to and from numpy arrays and torch tensors, as well as RLP encoding and decoding to and from abi-encoded bytes. This allows for unified handling of data vectors across different platforms and libraries.
ArithmeticType
Bases: StrEnum
String version of the ArithmeticTypeInt enum. This is used for better readability.
Source code in src/infernet_ml/utils/codec/vector.py
ArithmeticTypeInt
Bases: IntEnum
Enum representing the different floating point arithmetic types supported by Ritual. Vectors can be encoded in two ways: * IEEE754 arithmetic (default) * Fixed-point arithmetic much like the representation of ERC20 token balances & supplies in Ethereum.
Attributes:
Name | Type | Description |
---|---|---|
ieee |
IEEE754 arithmetic |
|
fixed_point |
Fixed-point arithmetic |
Source code in src/infernet_ml/utils/codec/vector.py
DataType
Bases: IntEnum
Enum representing the different data types supported by Ritual. Contains utility methods to convert between different data types across numpy, pytorch, and solidity types.
Attributes:
Name | Type | Description |
---|---|---|
float16 |
16-bit floating point number |
|
float32 |
32-bit floating point number |
|
float64 |
64-bit floating point number |
|
float128 |
128-bit floating point number |
|
float256 |
256-bit floating point number |
|
int8 |
8-bit signed integer |
|
int16 |
16-bit signed integer |
|
int32 |
32-bit signed integer |
|
int64 |
64-bit signed integer |
|
uint8 |
8-bit unsigned integer |
|
uint16 |
16-bit unsigned integer |
|
uint32 |
32-bit unsigned integer |
|
uint64 |
64-bit unsigned integer |
|
complex64 |
64-bit complex number |
|
complex128 |
128-bit complex number |
|
bool |
boolean |
Properties
Source code in src/infernet_ml/utils/codec/vector.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
np_type: type[object]
property
Returns the numpy data type corresponding to the data type.
solidity_type: str
property
Returns the solidity data type corresponding to the data type. This is mainly used for encoding and decoding of vectors in smart contracts.
torch_type: dtype
property
Returns the pytorch data type corresponding to the data type.
from_np_type(type_)
classmethod
Instantiates a DataType object from a numpy data type.
Source code in src/infernet_ml/utils/codec/vector.py
from_torch_type(type_)
classmethod
Instantiates a DataType object from a pytorch data type.
Source code in src/infernet_ml/utils/codec/vector.py
RitualVector
Bases: BaseModel
RitualVector is an abstraction of a vector of data, with utility methods to convert to and from numpy arrays and torch tensors, as well as RLP encoding and decoding to and from abi-encoded bytes. This allows for unified handling of data vectors across different platforms and libraries.
Attributes:
Name | Type | Description |
---|---|---|
dtype |
DataType
|
DataType data type of the vector |
shape |
Tuple[int, ...]
|
Tuple[int, ...] shape of the vector |
values |
List[int] | List[float] | List[complex] | List[bool]
|
List[int | float | complex | bool] values of the vector |
Properties
Methods:
Name | Description |
---|---|
from_numpy |
Instantiates a RitualVector object from a numpy array |
from_tensor |
Instantiates a RitualVector object from a torch tensor |
to_web3 |
Encodes the vector into abi-encoded bytes |
from_web3 |
Decodes the vector from abi-encoded bytes |
Source code in src/infernet_ml/utils/codec/vector.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
|
numpy: np.ndarray[Any, Any]
property
Returns the numpy array representation of the vector.
Returns:
Type | Description |
---|---|
ndarray[Any, Any]
|
np.ndarray numpy array |
tensor: Tensor
property
Returns the torch tensor representation of the vector.
Returns:
Type | Description |
---|---|
Tensor
|
Tensor torch tensor |
from_numpy(nparray)
classmethod
Instantiates a RitualVector object from a numpy array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nparray |
ndarray[Any, Any]
|
np.ndarray numpy array |
required |
Returns:
Type | Description |
---|---|
'RitualVector'
|
RitualVector object |
Source code in src/infernet_ml/utils/codec/vector.py
from_tensor(tensor)
classmethod
Instantiates a RitualVector object from a torch tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor |
Tensor
|
Tensor torch tensor |
required |
Returns:
Type | Description |
---|---|
'RitualVector'
|
RitualVector a RitualVector object |
Source code in src/infernet_ml/utils/codec/vector.py
from_web3(encoded)
classmethod
Decodes the vector from abi-encoded bytes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
encoded |
bytes
|
bytes abi-encoded bytes of the vector |
required |
Returns:
Type | Description |
---|---|
'RitualVector'
|
RitualVector a RitualVector object |
Source code in src/infernet_ml/utils/codec/vector.py
to_web3(arithmetic=ArithmeticType.ieee, fixed_point_scale=18)
Encodes the vector into abi-encoded bytes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arithmetic |
Optional[ArithmeticType]
|
ArithmeticType floating point arithmetic type |
ieee
|
fixed_point_scale |
Optional[int]
|
In the case of fixed-point arithmetic, the number of decimal places to scale the values by. |
18
|
Returns:
Type | Description |
---|---|
bytes
|
bytes abi-encoded bytes of the vector |
Source code in src/infernet_ml/utils/codec/vector.py
int_to_fixed_point_float(value, fixed_point_scale)
Since python's ints don't distinguish between numbers with different bit lengths, we can just YEET the value to a floating point.