bittensor.core.tensor#

Attributes#

Classes#

DTypes

dict() -> new empty dictionary

Tensor

Represents a Tensor object.

tensor

Functions#

cast_dtype(raw)

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

cast_shape(raw)

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

Module Contents#

class bittensor.core.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.

__contains__(key)[source]#

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

__getitem__(key)[source]#

Return self[key].

_add_torch()[source]#
torch: bool = False#
class bittensor.core.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.

_extract_dtype#
_extract_shape#
buffer: str | None#
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.

dtype: str#
model_config#

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

numpy()[source]#
Return type:

numpy.ndarray

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.

shape: list[int]#
tensor()[source]#
Return type:

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

tolist()[source]#
Return type:

list[object]

bittensor.core.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.core.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.

bittensor.core.tensor.dtypes#
class bittensor.core.tensor.tensor[source]#