TensorProcessor
Processor interface for converting between different tensor formats.
TensorProcessor
Tensor processor class.
Source code in src/tensorshare/serialization/processor.py
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
deserialize(data, backend)
staticmethod
Deserialize base64 encoded serialized tensors to a dictionary of tensors.
This method will convert TensorShare.tensors to a dictionary of tensors with their name as key. The backend must be specified in order to deserialize the data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
bytes
|
The base64 encoded serialized tensors to deserialize. |
required |
backend |
Union[str, Backend]
|
The backend to use for the conversion. Must be one of the following: - Backend.FLAX or 'flax' - Backend.NUMPY or 'numpy' - Backend.PADDLEPADDLE or 'paddlepaddle' - Backend.TENSORFLOW or 'tensorflow' - Backend.TORCH or 'torch' |
required |
Raises:
Type | Description |
---|---|
TypeError
|
If data is not bytes. |
TypeError
|
If backend is not a string or an instance of Backend enum. |
KeyError
|
If backend is not one of the supported backends. |
Returns:
Type | Description |
---|---|
Dict[str, Union[Array, ndarray, Tensor, Tensor, Tensor]]
|
Dict[str, Union[Array, np.ndarray, paddle.Tensor, tf.Tensor, torch.Tensor]]: A dictionary of tensors in the specified backend with their name as key. |
Source code in src/tensorshare/serialization/processor.py
serialize(tensors, metadata=None, backend=None)
staticmethod
Serialize a dictionary of tensors to a tuple containing the serialized tensors and their size.
This method will convert a dictionary of tensors to a tuple containing the base64 encoded serialized tensors and the size of the serialized tensors. It will use the backend if provided, otherwise it will try to infer the backend from the tensors format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensors |
Dict[str, Union[Array, ndarray, Tensor, Tensor, Tensor]]
|
Tensors stored in a dictionary with their name as key. |
required |
metadata |
Optional[Dict[str, str]]
|
Metadata to add to the safetensors file. Defaults to None. |
None
|
backend |
Optional[Union[str, Backend]]
|
Backend to use for the conversion. Defaults to None. If None, the backend will be inferred from the tensors format. Backend can be one of the following: - Backend.FLAX or 'flax' - Backend.NUMPY or 'numpy' - Backend.PADDLEPADDLE or 'paddlepaddle' - Backend.TENSORFLOW or 'tensorflow' - Backend.TORCH or 'torch' |
None
|
Raises:
Type | Description |
---|---|
TypeError
|
If tensors is not a dictionary. |
TypeError
|
If backend is not a string or an instance of Backend enum. |
ValueError
|
If tensors is empty. |
KeyError
|
If backend is not one of the supported backends. |
Returns:
Type | Description |
---|---|
Tuple[bytes, ByteSize]
|
Tuple[bytes, ByteSize]: A tuple containing the base64 encoded serialized tensors and the size of the serialized tensors. |
Source code in src/tensorshare/serialization/processor.py
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 |
|
Created: 2023-08-20