qewton.backends package¶
Module contents¶
- qewton.backends.resolve_backend(tensor) type[ComputingBackend][source]¶
Finds the ComputingBackend subclass whose default_dtype matches the given tensor’s type.
Raises if no backend matches, or if more than one does. The latter can in principle happen if two backends wrap the same underlying tensor type (e.g. a hypothetical SciPy backend also using plain np.ndarray, same as NumPyBackend) - the tensor’s type alone can’t disambiguate that, so this fails loudly with both candidates named rather than silently guessing. Callers that already know their backend (e.g. via a Node/Geometry) should use it directly instead of inferring it from a tensor.
Submodules¶
qewton.backends.base module¶
- class qewton.backends.base.ArrayLike(*args, **kwargs)[source]¶
Bases:
Protocol- property shape: tuple[int, ...]¶
- property size: int¶
- class qewton.backends.base.Backend(*args, **kwargs)[source]¶
Bases:
Serializable,Generic[TensorType]A Container that allows the connection of Qewton to any other library, which then might perform computations. Pre-defined subclasses require the implementation of certain methods.
All backends are expected to be stateless, and only contain class methods.
- default_dtype: ClassVar[type[TensorType]]¶
- classmethod save(serializer: Serializer) None[source]¶
The save method is responsible for serializing the object’s state. It collects the object’s attributes and adds them to the serializer.
- Parameters:
serializer (Serializer) – The serializer instance that manages the serialization process.
- classmethod construct_new_object(serializer: Deserializer, data_config: dict) Any[source]¶
Generates a new instance of the class without calling its __init__ method.
- Parameters:
serializer (Deserializer) – The deserializer instance that manages the loading process.
data_config (dict) – A dictionary containing the serialized data for the object.
- Returns:
- A new instance of the class, without calling its __init__ method.
The attributes will be set later.
- Return type:
Any
- class qewton.backends.base.ComputingBackend(*args, **kwargs)[source]¶
Bases:
Backend[TensorType]- math: ClassVar[type[MathBackend]]¶
- random: ClassVar[type[RandomBackend]]¶
- linalg: ClassVar[type[LinAlgBackend]]¶
- dtypes: ClassVar[dict]¶
- classmethod build_tensor(data, dtype: Any = <class 'qewton.config.dtypes.Float32'>, device: Device | str = CPU()) TensorType[source]¶
Builds a tensor from the given data.
- Parameters:
data – The data to build the tensor from.
- Returns:
The built tensor.
- Return type:
TensorType
- classmethod to(data, device)[source]¶
Moves the data to the given device.
- Parameters:
data (TensorType) – The data to move.
device (str) – The device to move the data to.
- Returns:
The moved data.
- Return type:
TensorType
- classmethod move_to_same_device(data, reference) TensorType[source]¶
Moves the data to the same device as the reference.
- Parameters:
data (TensorType) – The data to move.
reference (TensorType) – The reference data.
- Returns:
The moved data.
- Return type:
TensorType
- classmethod cast_dtype(data, dtype)[source]¶
Changes the data type of the given data.
- Parameters:
data (TensorType) – The data to change the type of.
dtype (qt_dtypes) – The new type of the data.
- Returns:
The data with the new type.
- Return type:
TensorType
- classmethod to_numpy(data)[source]¶
Converts this backend’s tensor type to a plain numpy array.
- Parameters:
data (TensorType) – The data to convert.
- Returns:
The converted data.
- Return type:
np.ndarray
- classmethod save_data(data, path: str | Path)[source]¶
Saves the given data to the given path.
- Parameters:
data (TensorType) – The data to save.
path (str | Path) – The path to save the data to.
- class qewton.backends.base.DeepLearningBackend(*args, **kwargs)[source]¶
Bases:
ComputingBackend[TensorType]A Backend that implements all the necessary methods for deep learning.
Note that this structure is similar to Keras’ backends.
- grad: ClassVar[type[GradBackend]]¶
- optim: ClassVar[type[OptimBackend]]¶
- param: ClassVar[type[ParameterBackend]]¶
- dtypes = {<class 'qewton.config.dtypes.BFloat16'>: None, <class 'qewton.config.dtypes.Bool'>: None, <class 'qewton.config.dtypes.Complex128'>: None, <class 'qewton.config.dtypes.Complex32'>: None, <class 'qewton.config.dtypes.Complex64'>: None, <class 'qewton.config.dtypes.Float16'>: None, <class 'qewton.config.dtypes.Float32'>: None, <class 'qewton.config.dtypes.Float64'>: None, <class 'qewton.config.dtypes.Int16'>: None, <class 'qewton.config.dtypes.Int32'>: None, <class 'qewton.config.dtypes.Int64'>: None, <class 'qewton.config.dtypes.Int8'>: None, <class 'qewton.config.dtypes.Number'>: None, <class 'qewton.config.dtypes.UInt16'>: None, <class 'qewton.config.dtypes.UInt32'>: None, <class 'qewton.config.dtypes.UInt64'>: None, <class 'qewton.config.dtypes.UInt8'>: None}¶
qewton.backends.grad module¶
qewton.backends.linalg module¶
- class qewton.backends.linalg.LinAlgBackend(*args, **kwargs)[source]¶
Bases:
Backend[TensorType]A Backend that implements linear algebra stuff such as matrix system solving, eigendecomposition, etc.
qewton.backends.math module¶
- class qewton.backends.math.MathBackend(*args, **kwargs)[source]¶
Bases:
Backend[TensorType]A Backend that implements all basic mathematical methods, method selection inspired by numpy.
The overall idea is that every math backend implements the numpy methods, and mimics numpy’s behavior to unify the usage among all backends.
- abstractmethod static rot90(array: Any, k: int = 1, axes: tuple[int, int] = (0, 1)) TensorType[source]¶
- abstractmethod static max(x: Any, axis: Any = None, keepdims: bool = False, initial: Any = None) TensorType[source]¶
- abstractmethod static ones(shape: Any, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
- abstractmethod static ones_like(x: Any, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
- abstractmethod static zeros(shape: Any, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
- abstractmethod static zeros_like(x: Any, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
- abstractmethod static empty(shape: Any, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
- abstractmethod static empty_like(x: Any, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
- abstractmethod static eye(N: int, M: int | None = None, k: int = 0, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
- abstractmethod static linspace(start: Any, stop: Any, num: int = 50, endpoint: bool = True, retstep: bool = False, dtype: Any = None, axis: int = 0, device: Device | str = CPU()) TensorType[source]¶
- abstractmethod static logspace(start: Any, stop: Any, num: int = 50, endpoint: bool = True, base: float = 10.0, dtype: Any = None, axis: int = 0, device: Device | str = CPU()) TensorType[source]¶
- abstractmethod static arange(start: Any, stop: Any = None, step: Any = None, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
- abstractmethod static bincount(x: Any, weights: Any = None, minlength: int = 0, sparse: bool = False) TensorType[source]¶
- abstractmethod static cross(x1: Any, x2: Any, axisa: int = -1, axisb: int = -1, axisc: int = -1, axis: Any = None) TensorType[source]¶
- abstractmethod static diagonal(x: Any, offset: int = 0, axis1: int = 0, axis2: int = 1) TensorType[source]¶
- abstractmethod static full(shape: Any, fill_value: Any, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
- abstractmethod static full_like(x: Any, fill_value: Any, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
- abstractmethod static geomspace(start: Any, stop: Any, num: int = 50, endpoint: bool = True, dtype: Any = None, axis: int = 0) TensorType[source]¶
- abstractmethod static isclose(x1: Any, x2: Any, rtol: float = 1e-05, atol: float = 1e-08, equal_nan: bool = False) TensorType[source]¶
- abstractmethod static allclose(x1: Any, x2: Any, rtol: float = 1e-05, atol: float = 1e-08, equal_nan: bool = False) TensorType[source]¶
- abstractmethod static isin(x1: Any, x2: Any, assume_unique: bool = False, invert: bool = False) TensorType[source]¶
- abstractmethod static min(x: Any, axis: Any = None, keepdims: bool = False, initial: Any = None) TensorType[source]¶
- abstractmethod static nanargmax(x: Any, axis: Any = None, keepdims: bool = False) TensorType[source]¶
- abstractmethod static nanargmin(x: Any, axis: Any = None, keepdims: bool = False) TensorType[source]¶
- abstractmethod static nanmedian(x: Any, axis: Any = None, keepdims: bool = False) TensorType[source]¶
- abstractmethod static nanpercentile(x: Any, q: Any, axis: Any = None, method: str = 'linear', keepdims: bool = False) TensorType[source]¶
- abstractmethod static nanquantile(x: Any, q: Any, axis: Any = None, method: str = 'linear', keepdims: bool = False) TensorType[source]¶
- abstractmethod static nan_to_num(x: Any, nan: float = 0.0, posinf: Any = None, neginf: Any = None) TensorType[source]¶
- abstractmethod static pad(x: Any, pad_width: Any, mode: str = 'constant', constant_values: Any = None) TensorType[source]¶
- abstractmethod static percentile(x: Any, q: Any, axis: Any = None, method: str = 'linear', keepdims: bool = False) TensorType[source]¶
- abstractmethod static prod(x: Any, axis: Any = None, keepdims: bool = False, dtype: Any = None) TensorType[source]¶
- abstractmethod static quantile(x: Any, q: Any, axis: Any = None, method: str = 'linear', keepdims: bool = False) TensorType[source]¶
- abstractmethod static searchsorted(sorted_sequence: Any, values: Any, side: str = 'left') TensorType[source]¶
- abstractmethod static split(x: Any, indices_or_sections: Any, axis: int = 0) list[TensorType][source]¶
- abstractmethod static array_split(x: Any, indices_or_sections: Any, axis: int = 0) list[TensorType][source]¶
- abstractmethod static trace(x: Any, offset: int = 0, axis1: int = 0, axis2: int = 1) TensorType[source]¶
- abstractmethod static tri(N: int, M: int | None = None, k: int = 0, dtype: Any = None) TensorType[source]¶
- abstractmethod static vectorize(pyfunc: Any, *, excluded: Any = None, signature: Any = None) Any[source]¶
- abstractmethod static trapezoid(y: Any, x: Any = None, dx: float = 1.0, axis: int = -1) TensorType[source]¶
- abstractmethod static vander(x: Any, N: int | None = None, increasing: bool = False) TensorType[source]¶
- abstractmethod static select(condlist: list[Any], choicelist: list[Any], default: Any = 0) TensorType[source]¶
- abstractmethod static histogram(x: Any, bins: int = 10, range: Any = None) tuple[TensorType, TensorType][source]¶
- abstractmethod static unique(x: Any, sorted: bool = True, return_index: bool = False, return_inverse: bool = False, return_counts: bool = False, axis: int | None = None) Any[source]¶
- abstractmethod static rfftn(x: Any, s: tuple[int, ...] | None = None, dim: tuple[int, ...] | None = None, norm: Literal['forward', 'backward', 'ortho'] | None = None) TensorType[source]¶
- abstractmethod static irfftn(x: Any, s: tuple[int, ...] | None = None, dim: tuple[int, ...] | None = None, norm: Literal['forward', 'backward', 'ortho'] | None = None) TensorType[source]¶
qewton.backends.nn module¶
- class qewton.backends.nn.NNBackend(*args, **kwargs)[source]¶
Bases:
Backend[TensorType]A Backend that implements all neural network related operations, such as activations, layers, etc.
- abstractmethod static conv1d(x: Any, /, weight: Any, bias: Any | None = None, stride: int | tuple[int] = 1, padding: int | tuple[int] = 0, dilation: int | tuple[int] = 1, groups: int = 1) TensorType[source]¶
- abstractmethod static conv2d(x: Any, /, weight: Any, bias: Any | None = None, stride: int | tuple[int, int] = 1, padding: int | tuple[int, int] = 0, dilation: int | tuple[int, int] = 1, groups: int = 1) TensorType[source]¶
- abstractmethod static conv3d(x: Any, /, weight: Any, bias: Any | None = None, stride: int | tuple[int, int, int] = 1, padding: int | tuple[int, int, int] = 0, dilation: int | tuple[int, int, int] = 1, groups: int = 1) TensorType[source]¶
- abstractmethod static max_pool1d(x: Any, /, kernel_size: int | tuple[int], stride: int | tuple[int] | None = None, padding: int | tuple[int] = 0, dilation: int | tuple[int] = 1, ceil_mode: bool = False) TensorType[source]¶
- abstractmethod static max_pool2d(x: Any, /, kernel_size: int | tuple[int, int], stride: int | tuple[int, int] | None = None, padding: int | tuple[int, int] = 0, dilation: int | tuple[int, int] = 1, ceil_mode: bool = False) TensorType[source]¶
- abstractmethod static max_pool3d(x: Any, /, kernel_size: int | tuple[int, int, int], stride: int | tuple[int, int, int] | None = None, padding: int | tuple[int, int, int] = 0, dilation: int | tuple[int, int, int] = 1, ceil_mode: bool = False) TensorType[source]¶
- abstractmethod static avg_pool1d(x: Any, /, kernel_size: int, stride: int = 0, padding: int = 0, ceil_mode: bool = False, count_include_pad: bool = True) TensorType[source]¶
- abstractmethod static avg_pool2d(x: Any, /, kernel_size: int | tuple[int, int], stride: int | tuple[int, int] | None = None, padding: int | tuple[int, int] = 0, ceil_mode: bool = False, count_include_pad: bool = True) TensorType[source]¶
- abstractmethod static avg_pool3d(x: Any, /, kernel_size: int | tuple[int, int, int], stride: int | tuple[int, int, int] | None = None, padding: int | tuple[int, int, int] = 0, ceil_mode: bool = False, count_include_pad: bool = True) TensorType[source]¶
- abstractmethod static batch_norm1d(x: Any, running_mean: Any | None, running_var: Any | None, weight: Any | None = None, bias: Any | None = None, training: bool = False, momentum: float = 0.1, eps: float = 1e-05) TensorType[source]¶
- abstractmethod static batch_norm2d(x: Any, running_mean: Any | None, running_var: Any | None, weight: Any | None = None, bias: Any | None = None, training: bool = False, momentum: float = 0.1, eps: float = 1e-05) TensorType[source]¶
- abstractmethod static batch_norm3d(x: Any, running_mean: Any | None, running_var: Any | None, weight: Any | None = None, bias: Any | None = None, training: bool = False, momentum: float = 0.1, eps: float = 1e-05) TensorType[source]¶
- abstractmethod static interpolate(x: Any, size: int | tuple[int] | tuple[int, int] | tuple[int, int, int] | None = None, scale_factor: float | tuple[float] | None = None, mode: Literal['nearest', 'linear', 'bilinear', 'bicubic', 'trilinear'] = 'nearest', align_corners: bool | None = False) TensorType[source]¶
qewton.backends.optim module¶
qewton.backends.param module¶
qewton.backends.random module¶
- class qewton.backends.random.RandomBackend(*args, **kwargs)[source]¶
Bases:
Backend[TensorType]A Backend that implements random number generation. Method selection and behavior inspired by numpy.random to unify usage across different backends.
- static normal(shape: Any, mean: float = 0.0, std: float = 1.0, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
Samples from a normal (Gaussian) distribution.
- static standard_normal(shape: Any, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
Samples from a standard normal distribution (mean=0, std=1).
- static uniform(shape: Any, low: float = 0.0, high: float = 1.0, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
Samples from a uniform distribution over [low, high).
- static randint(low: int, high: int | None = None, shape: Any = None, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
Samples from a discrete uniform distribution over [low, high).
- static choice(a: Any, shape: Any = None, replace: bool = True, p: Any = None, device: Device | str = CPU()) TensorType[source]¶
Generates a random sample from a given 1-D array or integer.
- static permutation(x: Any, device: Device | str = CPU()) TensorType[source]¶
Randomly permute a sequence, or return a permuted range.
- static exponential(shape: Any, scale: float = 1.0, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
Samples from an exponential distribution.
- static multivariate_normal(mean: Any, cov: Any, shape: Any = None, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
Samples from a multivariate normal distribution.
- static shuffle(x: Any, axis: int = 0) None[source]¶
Modify a sequence in-place by shuffling its contents.
- static binomial(n: int | Any, p: float | Any, shape: Any = None, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
Samples from a binomial distribution.
- static poisson(lam: float | Any, shape: Any = None, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
Samples from a Poisson distribution.
- static gamma(shape_param: float | Any, scale: float | Any = 1.0, shape: Any = None, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
Samples from a Gamma distribution.
- static beta(a: float | Any, b: float | Any, shape: Any = None, dtype: Any = None, device: Device | str = CPU()) TensorType[source]¶
Samples from a Beta distribution.