base
Base class for tensor adapters.
TensorAdapter
Bases: ABC
Abstract base class for tensor adapters.
This class defines the interface for tensor adapters that are used to
manipulate tensors in different libraries. Concrete implementations of
this class should provide specific implementations for each method as
well as providing a library_name
attribute.
TODO: Update the version of outlines-core used to receive plain arrays
instead of torch tensors. In the meantime, implementations of this class
must make sure that their full_like
and concatenate
methods can
handle torch tensors.
Source code in outlines/processors/tensor_adapters/base.py
12 13 14 15 16 17 18 19 20 21 22 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 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 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 |
|
apply_mask(tensor, mask, value)
abstractmethod
Fill the elements of the tensor where the mask is True with the specified value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to fill. |
required |
mask
|
TensorType
|
The mask to apply to the tensor. |
required |
value
|
Any
|
The value to fill the tensor with. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
The tensor with the mask applied. |
Source code in outlines/processors/tensor_adapters/base.py
argsort_descending(tensor)
abstractmethod
Return the indices that would sort the tensor in descending order along axis -1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to sort. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
The indices that would sort the tensor in descending order along axis -1. |
Source code in outlines/processors/tensor_adapters/base.py
boolean_ones_like(tensor)
abstractmethod
Create a boolean ones tensor with the same shape as the input tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to create a boolean ones tensor with the same shape. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
A boolean ones tensor with the same shape as the input tensor. |
Source code in outlines/processors/tensor_adapters/base.py
concatenate(tensors)
abstractmethod
Concatenate a list of tensors along axis 0.
ATTENTION: This method can either receive a list of torch tensors or a list of tensors from the library used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensors
|
list[Union[Tensor, TensorType]]
|
The list of tensors to concatenate. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
The concatenated tensor. |
Source code in outlines/processors/tensor_adapters/base.py
full_like(tensor, fill_value)
abstractmethod
Create a tensor with the same shape as the input tensor filled with a scalar value.
ATTENTION: This method receives a torch tensor regardless of the library used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
Tensor
|
The tensor to create a new tensor with the same shape. |
required |
fill_value
|
Any
|
The value to fill the new tensor with. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
A tensor with the same shape as the input tensor filled with the specified value. |
Source code in outlines/processors/tensor_adapters/base.py
get_device(tensor)
abstractmethod
Get the name of the tensor's device.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to get the device of. |
required |
Returns:
Type | Description |
---|---|
str
|
The name of the tensor's device. |
Source code in outlines/processors/tensor_adapters/base.py
shape(tensor)
abstractmethod
Get the shape of the tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to get the shape of. |
required |
Returns:
Type | Description |
---|---|
list[int]
|
The shape of the tensor. The list contains as many elements as there are dimensions in the tensor. |
Source code in outlines/processors/tensor_adapters/base.py
squeeze(tensor)
abstractmethod
Remove a dimension from the tensor at axis 0.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to remove a dimension from. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
The tensor with one less dimension. |
Source code in outlines/processors/tensor_adapters/base.py
to_device(tensor, device)
abstractmethod
Move the tensor to a specified device.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to move to a specified device. |
required |
device
|
str
|
The name of the device to move the tensor to. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
The tensor moved to the specified device. |
Source code in outlines/processors/tensor_adapters/base.py
to_list(tensor)
abstractmethod
Convert the tensor to a list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to convert to a list. |
required |
Returns:
Type | Description |
---|---|
list
|
The tensor as a list. |
Source code in outlines/processors/tensor_adapters/base.py
to_scalar(tensor)
abstractmethod
Return the only element of the tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to return the only element of. |
required |
Returns:
Type | Description |
---|---|
Any
|
The only element of the tensor. |
Source code in outlines/processors/tensor_adapters/base.py
unsqueeze(tensor)
abstractmethod
Add a dimension to the tensor at axis 0.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to add a dimension to. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
The tensor with an additional dimension. |