bittensor.tensor#

Attributes#

Classes#

DTypes

dict() -> new empty dictionary

tensor

Tensor

Represents a Tensor object.

Functions#

cast_dtype(raw)

Casts the raw value to a string representing the

cast_shape(raw)

Casts the raw value to a string representing the tensor shape.

Module Contents#

class bittensor.tensor.DTypes(*args, **kwargs)[source]#

Bases: dict

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s

(key, value) pairs

dict(iterable) -> new dictionary initialized as if via:

d = {} for k, v in iterable:

d[k] = v

dict(**kwargs) -> new dictionary initialized with the name=value pairs

in the keyword argument list. For example: dict(one=1, two=2)

Initialize self. See help(type(self)) for accurate signature.

torch: bool = False#
__getitem__(key)[source]#

Return self[key].

__contains__(key)[source]#

True if the dictionary has the specified key, else False.

_add_torch()[source]#
bittensor.tensor.dtypes#
bittensor.tensor.cast_dtype(raw)[source]#

Casts the raw value to a string representing the numpy data type, or the torch data type if using torch.

Parameters:

raw (Union[None, numpy.dtype, torch.dtype, str]) – The raw value to cast.

Returns:

The string representing the numpy/torch data type.

Return type:

str

Raises:

Exception – If the raw value is of an invalid type.

bittensor.tensor.cast_shape(raw)[source]#

Casts the raw value to a string representing the tensor shape.

Parameters:

raw (Union[None, List[int], str]) – The raw value to cast.

Returns:

The string representing the tensor shape.

Return type:

str

Raises:

Exception – If the raw value is of an invalid type or if the list elements are not of type int.

class bittensor.tensor.tensor[source]#
class bittensor.tensor.Tensor(/, **data)[source]#

Bases: pydantic.BaseModel

Represents a Tensor object.

Parameters:
  • buffer (Optional[str]) – Tensor buffer data.

  • dtype (str) – Tensor data type.

  • shape (List[int]) – Tensor shape.

  • data (Any)

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

model_config#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

tensor()[source]#
Return type:

Union[numpy.ndarray, bittensor.utils.registration.torch.Tensor]

tolist()[source]#
Return type:

List[object]

numpy()[source]#
Return type:

numpy.ndarray

deserialize()[source]#

Deserializes the Tensor object.

Returns:

The deserialized tensor object.

Return type:

np.array or torch.Tensor

Raises:

Exception – If the deserialization process encounters an error.

static serialize(tensor_)[source]#

Serializes the given tensor.

Parameters:
  • tensor (np.array or torch.Tensor) – The tensor to serialize.

  • tensor_ (Union[numpy.ndarray, bittensor.utils.registration.torch.Tensor])

Returns:

The serialized tensor.

Return type:

Tensor

Raises:

Exception – If the serialization process encounters an error.

buffer: str | None#
dtype: str#
shape: List[int]#
_extract_shape#
_extract_dtype#