Skip to content
Snippets Groups Projects
tensor.py 59.8 KiB
Newer Older
johannes bilk's avatar
johannes bilk committed
import numpy as np
from numpy.typing import ArrayLike
from typing import Any, Callable
from abc import ABC, abstractmethod
johannes bilk's avatar
johannes bilk committed
from .backend import BackendInterface, NumpyBackend, CupyBackend, NumbaBackend


class Tensor(object):
    __slots__ = ['_backend', 'data', 'gradient', 'requireGradient', 'gradientFunc', 'batched']

    __backend__ = NumpyBackend()

    def __init__(self, data: Any,
                 gradient: Any = None,
                 gradientFunc: Callable = None,
                 requireGradient: bool = False,
                 batched: bool = True) -> None:

        self._backend = Tensor.__backend__

        #if isinstance(data, (list | np.ndarray)):
        #    data = self._backend.array(data)
        #elif isinstance(data, (int, float)):
        #    data = self._backend.array([data])
        #elif isinstance(data, self.__class__):
        #    gradient = data.gradient if gradient is None else gradient
        #    gradientFunc = data.gradientFunc if gradientFunc is None else gradientFunc
        #    requireGradient = data.requireGradient if requireGradient is False else requireGradient
        #    data = data.data
johannes bilk's avatar
johannes bilk committed
        #if len(data.shape) == 1:
        #    data = self._backend.reshape(data, (1, *data.shape))

        #if gradient is None and requireGradient:
        #    # If gradient is not provided and it's required, initialize it as None
        #    gradient = self._backend.zeros_like(data)
        #elif isinstance(gradient, (list, int, float)):
        #    gradient = self._backend.array(gradient)

        # Checking if the shapes are the same
        #if gradient is not None:
        #    assert data.shape == gradient.shape, "value and gradient must have the same shape"

        self.data = data
        self.gradient = gradient
        self.requireGradient = requireGradient
        self.gradientFunc = gradientFunc
        self.batched = batched

    def zeroGradient(self) -> None:
        """In-place operation for nulling the gradient"""
        if self.requireGradient:
            self.gradient = self._backend.zeros_like(self.data)
        else:
            raise AttributeError("this tensor is not differentiable")

    def backward(self, gradient=None):
        """
        Compute the gradients recursively by applying the chain rule.
        """
        if gradient is None:
            gradient = self._backend.ones_like(self.data)

        if not self.requireGradient:
            return

        # If grad_fn is not set, this is probably the starting point for backpropagation,
        # so we don't need to compute further backward.
        if self.gradientFunc is None:
            return

        # Accumulate gradients instead of overwriting.
        self.gradient += gradient
        # Compute the local gradients using grad_fn
        self.gradientFunc.backward(self.gradient)

    def __repr__(self) -> str:
        """String representation."""
        dataTitle = 'data:\n'
        gradientTitle = 'gradient:\n'
        dataStr = str(self.data)
        gradientStr = str(self.gradient)
        if self.requireGradient is True:
            return dataTitle + dataStr + '\n' + gradientTitle + gradientStr
        else:
            return dataTitle + dataStr

    def copy(self) -> 'Tensor':
        data = self._backend.copy(self.data)
        gradient = self._backend.copy(self.gradient)
        return self.__class__(data, gradient, gradientFunc=self.gradientFunc, requireGradient=self.requireGradient)

    @property
    def strides(self) -> tuple:
        return self.data.strides

    def __len__(self) -> int:
        """Return the length of the value."""
        return len(self.data)

    @property
    def shape(self) -> tuple:
        """Return the shape of the value."""
        return self.data.shape
johannes bilk's avatar
johannes bilk committed
    @property
    def ndim(self) -> tuple:
        """Return the ndim of the value."""
        return self.data.ndim

    def reshape(self, newShape) -> 'Tensor':
        return Reshape()(self, newShape)
johannes bilk's avatar
johannes bilk committed
    def transpose(self) -> 'Tensor':
        return Transpose()(self)

    def T(self) -> 'Tensor':
        return Transpose()(self)

    def tolist(self) -> tuple[list, list] | list:
        if self.requireGradient is True:
            return self.data.tolist(), self.gradient.tolist()
        else:
            return self.data.tolist()

    @classmethod
    def setBackend(cls, backend: BackendInterface) -> None:
        cls.__backend__ = backend

    def __getitem__(self, index):
        """Get an item by index."""
        if self.requireGradient is True and self.gradient:
johannes bilk's avatar
johannes bilk committed
            return self.__class__(data=self.data[index], gradient=self.gradient[index], requireGradient=True, gradientFunc=self.gradientFunc)
        elif self.requireGradient is True:
            return self.__class__(data=self.data[index], requireGradient=True, gradientFunc=self.gradientFunc)
johannes bilk's avatar
johannes bilk committed
        else:
            return self.__class__(data=self.data[index], requireGradient=False)

    def __setitem__(self, index, value) -> None:
        """Set the value of an item by index."""
        if isinstance(value, self.__class__):
            self.data[index] = value.data
            if self.requireGradient is True and self.gradient:
johannes bilk's avatar
johannes bilk committed
                self.gradient[index] = value.gradient
                self.requireGradient = True
        else:
            self.data[index] = value
            self.gradient[index] = 0

    def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
        if method == '__call__':
            operation = ufuncMap.get(ufunc)
            if operation is not None:
                return operation()(*inputs, **kwargs)
        raise NotImplementedError(f'{ufunc} is not implemented yet')

    def __array_function__(self, func, types, args, kwargs):
        operation = funcMap.get(func)
        if operation is not None:
            return operation()(*args, **kwargs)
        raise NotImplementedError(f'{func} is not implemented yet')
johannes bilk's avatar
johannes bilk committed
    def __add__(self, other: ArrayLike) -> 'Tensor':
johannes bilk's avatar
johannes bilk committed

    def __radd__(self, other: ArrayLike) -> 'Tensor':
johannes bilk's avatar
johannes bilk committed

    def __iadd__(self, other: ArrayLike) -> 'Tensor':
        result = addForward(self, other)
johannes bilk's avatar
johannes bilk committed
        self.data = result.data
        self.gradient = result.gradient
        self.requireGradient = result.requireGradient
        return self

    def __sub__(self, other: ArrayLike) -> 'Tensor':
        return subtractForward(self, other)
johannes bilk's avatar
johannes bilk committed

    def __rsub__(self, other: ArrayLike) -> 'Tensor':
        return subtractForward(other, self)
johannes bilk's avatar
johannes bilk committed

    def __isub__(self, other: ArrayLike) -> 'Tensor':
        result = subtractForward(self, other)
johannes bilk's avatar
johannes bilk committed
        self.data = result.data
        self.gradient = result.gradient
        self.requireGradient = result.requireGradient
        return self

    def __mul__(self, other: ArrayLike) -> 'Tensor':
        return Multiply()(self, other)

    def __rmul__(self, other: ArrayLike) -> 'Tensor':
        return Multiply()(other, self)

    def __imul__(self, other: ArrayLike) -> 'Tensor':
        result = Multiply(self, other)
        self.data = result.data
        self.gradient = result.gradient
        self.requireGradient = result.requireGradient
johannes bilk's avatar
johannes bilk committed
        return self

    def __truediv__(self, other: ArrayLike) -> 'Tensor':
        return Divide()(self, other)

    def __rtruediv__(self, other: ArrayLike) -> 'Tensor':
        return Divide()(other, self)

    def __itruediv__(self, other: ArrayLike) -> 'Tensor':
        result = Divide(self, other)
        self.data = result.data
        self.gradient = result.gradient
        self.requireGradient = result.requireGradient
        return self

    def __matmul__(self, other: ArrayLike) -> 'Tensor':
        return Matmul()(self, other)

    def __rmatmul__(self, other: ArrayLike) -> 'Tensor':
        return Matmul()(other, self)

    def __imatmul__(self, other: ArrayLike) -> 'Tensor':
        result = Matmul(self, other)
        self.data = result.data
        self.gradient = result.gradient
        self.requireGradient = result.requireGradient
        return self

    def __pow__(self, other: ArrayLike) -> 'Tensor':
        return Power()(self, other)

    def __rpow__(self, other: ArrayLike) -> 'Tensor':
        return Power()(other, self)

    def __ipow__(self, other: ArrayLike) -> 'Tensor':
        result = Power(self, other)
        self.data = result.data
        self.gradient = result.gradient
        self.requireGradient = result.requireGradient
        return self

    def __abs__(self) -> 'Tensor':
        return Abs()(self)

    def __pos__(self) -> 'Tensor':
        return Positive()(self)

    def __neg__(self) -> 'Tensor':
        return Negative()(self)

    def __eq__(self, other) -> bool:
        """Equality comparison."""
        return Equal()(self, other)

    def __gt__(self, other) -> bool:
        """Greater than comparison."""
        return Greater()(self, other)

    def __ge__(self, other) -> bool:
        """Greater than or equal to comparison."""
        return GreaterEqual()(self, other)

    def __lt__(self, other) -> bool:
        """Less than comparison."""
        return Less()(self, other)

    def __le__(self, other) -> bool:
        """Less than or equal to comparison."""
        return LessEqual()(self, other)


def checkTensor(tensor: Tensor) -> Tensor:
    if isinstance(tensor, Tensor):
        return tensor
    return Tensor(tensor)


#
# Operations
#


class Operation(ABC):
    __slots__ = ['name', 'operationID', 'backend']
    id = 0
    __backend__ = Tensor.__backend__

    def __init__(self) -> None:
        self.name = self.__class__.__name__
        self.operationID = Operation.id
        self.backend = Operation.__backend__
        Operation.id += 1

    @abstractmethod
    def forward(self, *args, **kwargs) -> Tensor:
        raise NotImplementedError

    @abstractmethod
    def backward(self, gradient: np.ndarray) -> None:
        raise NotImplementedError

    def __call__(self, *args, **kwargs):
        return self.forward(*args, **kwargs)

    def __repr__(self) -> str:
        return f'{self.name}, {self.operationID}'

    def at(self, tensor: Tensor, indices, value) -> None:
         # not ready for use yet
         tensor = self.forward(indices, value)


class TwoTensors(Operation):
    __slots__ = ['tensor1', 'tensor2']

    def __init__(self) -> None:
        super().__init__()
        self.tensor1 = None
        self.tensor2 = None
        self.tensor1BroadcastAxis = None
        self.tensor2BroadcastAxis = None
johannes bilk's avatar
johannes bilk committed
    def getbroadcastAxid(self, data, gradient) -> None:
        # Store old shapes
        tensorShape = np.array(data.shape)
johannes bilk's avatar
johannes bilk committed
        # Get new shape
        gradientShape = np.array(gradient.shape)

        # Prepend ones to the shape of the smaller array
        if len(tensorShape) < len(gradientShape):
            tensorShape = np.pad(tensorShape, (len(gradientShape) - len(tensorShape), 0), mode='constant', constant_values=1)
        elif len(tensorShape) > len(gradientShape):
            gradientShape = np.pad(gradientShape, (len(tensorShape) - len(gradientShape), 0), mode='constant', constant_values=1)
johannes bilk's avatar
johannes bilk committed
        # Find broadcasted axes
        tensorBroadcastAxis = np.where(tensorShape != gradientShape)[0]

        # Change broadcastAxis variables to None if they're empty
        if tensorBroadcastAxis.size == 0:
            tensorBroadcastAxis = None
johannes bilk's avatar
johannes bilk committed
        return tensorBroadcastAxis

    def forward(self, tensor1: Tensor, tensor2: Tensor, *args, **kwargs) -> Tensor:
        if not isinstance(tensor1, Tensor):
            tensor1 = Tensor(tensor1)
        if not isinstance(tensor2, Tensor):
            tensor2 = Tensor(tensor2)
johannes bilk's avatar
johannes bilk committed
        requireGradient = tensor1.requireGradient or tensor2.requireGradient
        if requireGradient:
            self.tensor1 = tensor1
            self.tensor2 = tensor2

        data = self._operation(tensor1.data, tensor2.data, *args, **kwargs)

        return Tensor(data, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient: np.ndarray) -> None:
        if self.tensor1 and self.tensor1.requireGradient:
            gradientForTensor1 = self.backend.copy(gradient)

            tensorBroadcastAxis = self.getbroadcastAxid(self.tensor1, gradientForTensor1)
            if tensorBroadcastAxis is not None:
                gradientForTensor1 = np.sum(gradientForTensor1, axis=tuple(tensorBroadcastAxis), keepdims=True)

            self.tensor1.gradient = self._derivativeD1(gradientForTensor1)
            if self.tensor1.gradientFunc:
                self.tensor1.gradientFunc.backward(self.tensor1.gradient)

        if self.tensor2 and self.tensor2.requireGradient:
            gradientForTensor2 = self.backend.copy(gradient)

            tensorBroadcastAxis = self.getbroadcastAxid(self.tensor2, gradientForTensor2)
            if tensorBroadcastAxis is not None:
                gradientForTensor2 = np.sum(gradientForTensor2, axis=tuple(tensorBroadcastAxis), keepdims=True)

            self.tensor2.gradient = self._derivativeD2(gradientForTensor2)
            if self.tensor2.gradientFunc:
                self.tensor2.gradientFunc.backward(self.tensor2.gradient)

    @abstractmethod
    def _operation(self, data1: np.ndarray, data2: np.ndarray, *args, **kwargs) -> np.ndarray:
        raise NotImplementedError

    @abstractmethod
    def _derivativeD1(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        raise NotImplementedError
johannes bilk's avatar
johannes bilk committed
    @abstractmethod
    def _derivativeD2(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        raise NotImplementedError


class OneTensor(Operation):
    __slots__ = ['tensor']

    def __init__(self) -> None:
        super().__init__()
        self.tensor = None

    def forward(self, tensor: Tensor, *args, **kwargs) -> Tensor:
        if not isinstance(tensor, Tensor):
            tensor = Tensor(tensor)

        requireGradient = tensor.requireGradient
        if requireGradient:
            self.tensor = tensor

        data = self._operation(tensor.data, *args, **kwargs)
johannes bilk's avatar
johannes bilk committed
        return Tensor(data, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient: np.ndarray) -> None:
        if self.tensor and self.tensor.requireGradient:
            self.tensor.gradient = self._derivative(gradient)
            if self.tensor.gradientFunc:
                self.tensor.gradientFunc.backward(self.tensor.gradient)

    @abstractmethod
    def _operation(self, data: np.ndarray, *args, **kwargs) -> np.ndarray:
        raise NotImplementedError
johannes bilk's avatar
johannes bilk committed
    @abstractmethod
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        raise NotImplementedError


#
# Two Tensors
#


def getbroadcastAxid(data, gradient) -> None:
    # Store old shapes
    tensorShape = np.array(data.shape)

    # Get new shape
    gradientShape = np.array(gradient.shape)

    # Prepend ones to the shape of the smaller array
    if len(tensorShape) < len(gradientShape):
        tensorShape = np.pad(tensorShape, (len(gradientShape) - len(tensorShape), 0), mode='constant', constant_values=1)
    elif len(tensorShape) > len(gradientShape):
        gradientShape = np.pad(gradientShape, (len(tensorShape) - len(gradientShape), 0), mode='constant', constant_values=1)

    # Find broadcasted axes
    tensorBroadcastAxis = np.where(tensorShape != gradientShape)[0]

    # Change broadcastAxis variables to None if they're empty
    if tensorBroadcastAxis.size == 0:
        tensorBroadcastAxis = None

    return tensorBroadcastAxis


def addForward(tensor1: Tensor, tensor2: Tensor, *args, **kwargs) -> Tensor:
    if not isinstance(tensor1, Tensor):
        tensor1 = Tensor(tensor1)
    if not isinstance(tensor2, Tensor):
        tensor2 = Tensor(tensor2)

    data = np.add(tensor1.data, tensor2.data, *args, **kwargs)

    requireGradient = tensor1.requireGradient or tensor2.requireGradient
    if requireGradient:
        gradfunc = partial(addBackward, tensor1, tensor2)
        return Tensor(data, requireGradient=requireGradient, gradientFunc=gradfunc)

    return Tensor(data, requireGradient=requireGradient, gradientFunc=None)


def addBackward(tensor1: Tensor, tensor2: Tensor, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
    if tensor1 and tensor1.requireGradient:
        gradientForTensor1 = np.copy(gradient)

        tensorBroadcastAxis = getbroadcastAxid(tensor1, gradientForTensor1)
        if tensorBroadcastAxis is not None:
            gradientForTensor1 = np.sum(gradientForTensor1, axis=tuple(tensorBroadcastAxis), keepdims=True)

        tensor1.gradient = np.add(tensor1.gradient, gradient)
        if tensor1.gradientFunc:
            tensor1.gradientFunc.backward(tensor1.gradient)

    if tensor2 and tensor2.requireGradient:
        gradientForTensor2 = np.copy(gradient)

        tensorBroadcastAxis = getbroadcastAxid(tensor2, gradientForTensor2)
        if tensorBroadcastAxis is not None:
            gradientForTensor2 = np.sum(gradientForTensor2, axis=tuple(tensorBroadcastAxis), keepdims=True)

        tensor2.gradient = np.add(tensor2.gradient, gradient)
        if tensor2.gradientFunc:
            tensor2.gradientFunc.backward(tensor2.gradient)


johannes bilk's avatar
johannes bilk committed
class Add(TwoTensors):
    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data1: np.ndarray, data2: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.add(data1, data2, *args, **kwargs)

    def _derivativeD1(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.add(self.tensor1.gradient, gradient)
johannes bilk's avatar
johannes bilk committed
    def _derivativeD2(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.add(self.tensor2.gradient, gradient)


def subtractForward(tensor1: Tensor, tensor2: Tensor, *args, **kwargs) -> Tensor:
    if not isinstance(tensor1, Tensor):
        tensor1 = Tensor(tensor1)
    if not isinstance(tensor2, Tensor):
        tensor2 = Tensor(tensor2)

    data = np.subtract(tensor1.data, tensor2.data, *args, **kwargs)

    requireGradient = tensor1.requireGradient or tensor2.requireGradient
    if requireGradient:
        gradfunc = partial(addBackward, tensor1, tensor2)
        return Tensor(data, requireGradient=requireGradient, gradientFunc=gradfunc)

    return Tensor(data, requireGradient=requireGradient, gradientFunc=None)


def subtractBackward(tensor1: Tensor, tensor2: Tensor, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
    if tensor1 and tensor1.requireGradient:
        gradientForTensor1 = np.copy(gradient)

        tensorBroadcastAxis = getbroadcastAxid(tensor1, gradientForTensor1)
        if tensorBroadcastAxis is not None:
            gradientForTensor1 = np.sum(gradientForTensor1, axis=tuple(tensorBroadcastAxis), keepdims=True)

        tensor1.gradient = np.add(tensor1.gradient, gradient)
        if tensor1.gradientFunc:
            tensor1.gradientFunc.backward(tensor1.gradient)

    if tensor2 and tensor2.requireGradient:
        gradientForTensor2 = np.copy(gradient)

        tensorBroadcastAxis = getbroadcastAxid(tensor2, gradientForTensor2)
        if tensorBroadcastAxis is not None:
            gradientForTensor2 = np.sum(gradientForTensor2, axis=tuple(tensorBroadcastAxis), keepdims=True)

        tensor2.gradient = np.subtract(tensor2.gradient, gradient)
        if tensor2.gradientFunc:
            tensor2.gradientFunc.backward(tensor2.gradient)


johannes bilk's avatar
johannes bilk committed
class Subtract(TwoTensors):
    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data1: np.ndarray, data2: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.subtract(data1, data2, *args, **kwargs)

    def _derivativeD1(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.add(self.tensor1.gradient, gradient)
johannes bilk's avatar
johannes bilk committed
    def _derivativeD2(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.subtract(self.tensor2.gradient, gradient)


class Multiply(TwoTensors):
    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data1: np.ndarray, data2: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply(data1, data2, *args, **kwargs)

    def _derivativeD1(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.add(self.tensor1.gradient, self.backend.multiply(self.tensor2.data, gradient))
johannes bilk's avatar
johannes bilk committed
    def _derivativeD2(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.add(self.tensor2.gradient, self.backend.multiply(self.tensor1.data, gradient))


class Divide(TwoTensors):
    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data1: np.ndarray, data2: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.divide(data1, data2, *args, **kwargs)

    def _derivativeD1(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.add(self.tensor1.gradient, self.backend.divide(gradient, self.tensor2.data))
johannes bilk's avatar
johannes bilk committed
    def _derivativeD2(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.subtract(self.tensor2.gradient, self.backend.divide(self.backend.multiply(self.tensor1.data, gradient), self.backend.power(self.tensor2.data, 2)))


class Matmul(TwoTensors):
    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data1: np.ndarray, data2: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.matmul(data1, data2, *args, **kwargs)
johannes bilk's avatar
johannes bilk committed
    # Update the backward pass to handle batch dimension
    def _derivativeD1(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        if len(self.tensor1.data.shape) > 2 or len(self.tensor2.data.shape) > 2:
            return self.backend.add(self.tensor1.gradient, self.backend.matmul(gradient, self.backend.transpose(self.tensor2.data, axes=(0, 2, 1))))
        else:
            return self.backend.add(self.tensor1.gradient, self.backend.matmul(gradient, self.backend.transpose(self.tensor2.data)))
johannes bilk's avatar
johannes bilk committed
    def _derivativeD2(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        if len(self.tensor1.data.shape) > 2 or len(self.tensor2.data.shape) > 2:
            return self.backend.add(self.tensor2.gradient, self.backend.matmul(self.backend.transpose(self.tensor1.data, axes=(0, 2, 1)), gradient))
        else:
            return self.backend.add(self.tensor2.gradient, self.backend.matmul(self.backend.transpose(self.tensor1.data), gradient))
johannes bilk's avatar
johannes bilk committed
    def backward(self, gradient: np.ndarray) -> None:
        if self.tensor1 and self.tensor1.requireGradient:
            gradientForTensor1 = self.backend.copy(gradient)

            self.tensor1.gradient = self._derivativeD1(gradientForTensor1)
            if self.tensor1.gradientFunc:
                self.tensor1.gradientFunc.backward(self.tensor1.gradient)

        if self.tensor2 and self.tensor2.requireGradient:
            gradientForTensor2 = self.backend.copy(gradient)

            self.tensor2.gradient = self._derivativeD2(gradientForTensor2)
            if self.tensor2.gradientFunc:
                self.tensor2.gradientFunc.backward(self.tensor2.gradient)


class Dot(TwoTensors):
    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data1: np.ndarray, data2: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.dot(data1, data2)

    def _derivativeD1(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply(self.tensor2.data, gradient)
johannes bilk's avatar
johannes bilk committed
    def _derivativeD2(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply(self.tensor1.data, gradient)


class Power(TwoTensors):
    def __init__(self) -> None:
        super().__init__()
johannes bilk's avatar
johannes bilk committed
    def _operation(self, data1: np.ndarray, data2: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.power(data1, data2)
johannes bilk's avatar
johannes bilk committed
    def _derivativeD1(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.add(self.tensor1.gradient, self.backend.multiply(self.backend.multiply(self.tensor2.data, self.backend.power(self.tensor1.data, (self.backend.subtract(self.tensor2.data, 1)))), gradient))
johannes bilk's avatar
johannes bilk committed
    def _derivativeD2(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.add(self.tensor2.gradient, self.backend.multiply(self.backend.multiply(self.backend.log(self.tensor1.data), self.backend.power(self.tensor1.data, self.tensor2.data)), gradient))


#
# Single Tensor
#


class Square(OneTensor):
    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.square(data, *args, **kwargs)
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply(self.backend.multiply(self.tensor.data, 2.0), gradient)


class Sqrt(OneTensor):
    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.sqrt(data, *args, **kwargs)
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply(self.backend.divide(0.5, self.backend.sqrt(self.tensor.data)), gradient)
johannes bilk's avatar
johannes bilk committed

class Log(OneTensor):
    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.log(data, *args, **kwargs)
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.add(self.tensor.gradient, self.backend.multiply((self.backend.divide(1, self.tensor.data)), gradient))


class Exp(OneTensor):
    __slots__ = ['data']

    def __init__(self) -> None:
        super().__init__()
        self.data = None
johannes bilk's avatar
johannes bilk committed
    def _operation(self, data: np.ndarray, *args, **kwargs) -> np.ndarray:
        self.data = self.backend.exp(data, *args, **kwargs)
        return self.data
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.data * gradient


class Sin(OneTensor):
    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.sin(data, *args, **kwargs)
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply(self.backend.cos(self.tensor.data), gradient)


class Cos(OneTensor):
    def __init__(self) -> None:
        super().__init__()
johannes bilk's avatar
johannes bilk committed
    def _operation(self, data: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.cos(data, *args, **kwargs)
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.negative(self.backend.multiply(self.backend.sin(self.tensor.data), gradient))


class Tan(OneTensor):
    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.tan(data, *args, **kwargs)
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply((self.backend.divide(1, self.backend.power(np.cos(self.tensor.data), 2))), gradient)


class Sinh(OneTensor):
    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.sinh(data, *args, **kwargs)
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply(self.backend.cosh(self.tensor.data), gradient)


class Cosh(OneTensor):
    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.cosh(data, *args, **kwargs)
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply(self.backend.sinh(self.tensor.data), gradient)


class Tanh(OneTensor):
    def __init__(self) -> None:
        super().__init__()
johannes bilk's avatar
johannes bilk committed
    def _operation(self, data: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.tanh(data, *args, **kwargs)
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply((self.backend.divide(1, self.backend.power(np.cosh(self.tensor.data), 2))), gradient)


class Abs(OneTensor):
    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.abs(data, *args, **kwargs)
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply(self.backend.sign(self.tensor.data), gradient)


#
# Signs
#


class Sign(OneTensor):
    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.sign(data, *args, **kwargs)
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply(self.backend.sign(self.tensor.data), gradient)


class Positive(OneTensor):
    def __init__(self) -> None:
        super().__init__()
johannes bilk's avatar
johannes bilk committed
    def _operation(self, data: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.positive(data, *args, **kwargs)
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply(self.backend.positive(self.tensor.data), gradient)


class Negative(OneTensor):
    def __init__(self) -> None:
        super().__init__()
johannes bilk's avatar
johannes bilk committed
    def _operation(self, data: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.negative(data, *args, **kwargs)
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply(self.backend.negative(self.tensor.data), gradient)


#
# Compare
#


class Equal(TwoTensors):
    __slots__ = ['bools']

    def __init__(self) -> None:
        super().__init__()
        self.bools = None

    def _operation(self, data1: np.ndarray, data2: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.equal(data1, data2)
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply(self.bools, gradient)


class NotEqual(TwoTensors):
    __slots__ = ['bools']

    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data1: np.ndarray, data2: np.ndarray, *args, **kwargs) -> np.ndarray:
        self.bools = self.backend.not_equal(data1, data2)
        return self.bools
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply(self.bools, gradient)


class Less(TwoTensors):
    __slots__ = ['bools']
johannes bilk's avatar
johannes bilk committed
    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data1: np.ndarray, data2: np.ndarray, *args, **kwargs) -> np.ndarray:
        self.bools = self.backend.less(data1, data2)
        return self.bools
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply(self.bools, gradient)


class LessEqual(TwoTensors):
    __slots__ = ['bools']

    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data1: np.ndarray, data2: np.ndarray, *args, **kwargs) -> np.ndarray:
        self.bools = self.backend.less_equal(data1, data2)
        return self.bools
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply(self.bools, gradient)


class Greater(TwoTensors):
    __slots__ = ['bools']

    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data1: np.ndarray, data2: np.ndarray, *args, **kwargs) -> np.ndarray:
        self.bools = self.backend.greater(data1, data2)
        return self.bools
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply(self.bools, gradient)


class GreaterEqual(TwoTensors):
    __slots__ = ['bools']

    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data1: np.ndarray, data2: np.ndarray, *args, **kwargs) -> np.ndarray:
        self.bools = self.backend.greater_equal(data1, data2)
        return self.bools
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.multiply(self.bools, gradient)


#
# Shaping
#


class Flatten(OneTensor):
    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.reshape(data, newshape=(-1))
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.reshape(gradient, newshape=self.tensor.shape)


class Reshape(OneTensor):
    def __init__(self) -> None:
        super().__init__()

    def _operation(self, data: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.reshape(data, *args, **kwargs)
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.reshape(gradient, newshape=self.tensor.shape)


#
# Broadcasting
#


class Repeat(Operation):
    __slots__ = ['repeats', 'axis', 'tensor']

    def __init__(self) -> None:
        super().__init__()
        self.tensor = None

    def forward(self, tensor: Tensor, repeats: ArrayLike, axis: int = None) -> Tensor:
        tensor = checkTensor(tensor)
        self.repeats = repeats
        self.axis = axis

        requireGradient = tensor.requireGradient
        if requireGradient:
            self.tensor = tensor

        data = self.backend.repeat(tensor.data, repeats=self.repeats, axis=self.axis)

        return Tensor(data, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient) -> None:
        if self.tensor and self.tensor.requireGradient:
            if self.axis is None:
                sum_axis = tuple(range(gradient.ndim)[::-self.repeats])
                counts = np.prod(self.repeats)
            else:
                sum_axis = self.axis
                counts = self.repeats

            grad = self.backend.sum(gradient, axis=sum_axis, keepdims=True)
            grad = self.backend.divide(grad, counts)
            self.tensor.gradient = self.backend.broadcast_to(grad, self.tensor.shape)

            if self.tensor.gradientFunc:
                self.tensor.gradientFunc.backward(self.tensor.gradient)


class Tile(Operation):
    __slots__ = ['tensor', 'reps']

    def __init__(self) -> None:
        super().__init__()
        self.tensor = None

    def forward(self, tensor: Tensor, reps: ArrayLike) -> Tensor:
        tensor = checkTensor(tensor)
        self.reps = reps

        requireGradient = tensor.requireGradient
        if requireGradient:
            self.tensor = tensor

        data = self.backend.tile(tensor.data, reps=self.reps)

        return Tensor(data, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient: np.ndarray) -> None:
        if self.tensor and self.tensor.requireGradient:
            reshaped = self.backend.reshape(gradient, self.tensor.shape + self.reps)
            axis = tuple(range(self.tensor.ndim, gradient.ndim))
            self.tensor.gradient = self.backend.sum(reshaped, axis=axis)

            if self.tensor.gradientFunc:
                self.tensor.gradientFunc.backward(self.tensor.gradient)


class Concatenate(Operation):
    __slots__ = ['tensors', 'axis', 'out', 'dtype', 'casting', 'shapes']

    def __init__(self) -> None:
        super().__init__()
        self.tensors = None
johannes bilk's avatar
johannes bilk committed
    def forward(self, tensors: Tensor, axis=0, out=None, dtype=None, casting='same_kind') -> Tensor:
        self.axis = axis
        self.out = out
        self.dtype = dtype
        self.casting = casting

        tensors = [checkTensor(tensor) for tensor in tensors]

        requireGradient = any(tensor.requireGradient for tensor in tensors)
        if requireGradient:
            self.tensors = tensors
            self.shapes = [tensor.shape for tensor in tensors]

        data = self.backend.concatenate([tensor.data for tensor in tensors], axis=self.axis, out=self.out, dtype=self.dtype, casting=self.casting)
        return Tensor(data, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient) -> None:
        grads = self.backend.split(gradient, self.backend.cumsum([shape[self.axis] for shape in self.shapes[:-1]]), axis=self.axis)
        for tensor, grad in zip(self.tensors, grads):
            if tensor.requireGradient:
                tensor.gradient = grad
                if tensor.gradientFunc:
                    tensor.gradientFunc.backward(tensor.gradient)


class Hstack(Concatenate):
    def __init__(self):
        super().__init__()
johannes bilk's avatar
johannes bilk committed
    def forward(self, tensors: Tensor, dtype=None, casting='same_kind'):
        return super().forward(tensors, axis=1, out=None, dtype=dtype, casting=casting)


class Vstack(Concatenate):
    def __init__(self, dtype=None, casting='same_kind'):
        super().__init__()
johannes bilk's avatar
johannes bilk committed
    def forward(self, tensors: Tensor, dtype=None, casting='same_kind'):
        return super().forward(tensors, axis=0, out=None, dtype=dtype, casting=casting)


class Dstack(Concatenate):
    def __init__(self):
        super().__init__()
johannes bilk's avatar
johannes bilk committed
    def forward(self, tensors: Tensor):
        return super().forward(tensors, axis=2)


class Split(Operation):
    __slots__ = ['tensor', 'indices', 'axis']

    def __init__(self) -> None:
        super().__init__()
        self.tensor = None
johannes bilk's avatar
johannes bilk committed
    def forward(self, tensor, indices_or_sections, axis=0) -> list[Tensor]:
        tensor = checkTensor(tensor)
        self.indices = indices_or_sections
        self.axis = axis

        requireGradient = tensor.requireGradient
        if requireGradient:
            self.tensor = tensor
johannes bilk's avatar
johannes bilk committed
        data = self.backend.split(tensor.data, self.indices, self.axis)

        return [Tensor(datum, requireGradient=requireGradient, gradientFunc=self) for datum in data]

    def backward(self, gradient: np.ndarray) -> None:
        gradient = self.backend.concatenate(gradient, axis=self.axis)
        if self.tensor and self.tensor.requireGradient:
            self.tensor.gradient = gradient
            if self.tensor.gradientFunc:
                self.tensor.gradientFunc.backward(self.tensor.gradient)


class Hsplit(Split):
    def __init__(self) -> None:
        super().__init__()
        self.tensor = None
johannes bilk's avatar
johannes bilk committed
    def forward(self, tensors: Tensor, indices_or_sections):
        return super().forward(tensors, indices_or_sections=indices_or_sections, axis=1)
johannes bilk's avatar
johannes bilk committed
class Vsplit(Split):
    def __init__(self) -> None:
        super().__init__()
        self.tensor = None
johannes bilk's avatar
johannes bilk committed
    def forward(self, tensors: Tensor, indices_or_sections):
        return super().forward(tensors, indices_or_sections=indices_or_sections, axis=0)

class Dsplit(Split):
    def __init__(self) -> None:
        super().__init__()
        self.tensor = None
johannes bilk's avatar
johannes bilk committed
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
    def forward(self, tensors: Tensor, indices_or_sections):
        return super().forward(tensors, indices_or_sections=indices_or_sections, axis=2)



#
# Reduce
#


class Sum(Operation):
    __slots__ = ['tensor']

    def __init__(self) -> None:
        super().__init__()
        self.tensor = None

    def forward(self, tensor: Tensor, axis=None, dtype=None, keepdims=False, **kwargs) -> Tensor:
        tensor = checkTensor(tensor)

        requireGradient = tensor.requireGradient
        if requireGradient:
            self.tensor = tensor

        data = self.backend.sum(tensor.data, axis=axis, dtype=dtype, keepdims=keepdims)

        return Tensor(data, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient) -> None:
        if self.tensor and self.tensor.requireGradient:
            self.tensor.gradient = self.backend.broadcast_to(gradient.T, self.tensor.shape)
            if self.tensor.gradientFunc:
                self.tensor.gradientFunc.backward(self.tensor.gradient)


class Prod(Operation):
    __slots__ = ['tensor', 'product']

    def __init__(self) -> None:
        super().__init__()
        self.tensor = None

    def forward(self, tensor: Tensor, axis=None, dtype=None, keepdims=False) -> Tensor:
        tensor = checkTensor(tensor)

        requireGradient = tensor.requireGradient
        if requireGradient:
            self.tensor = tensor

        data = tensor.data
        self.product = self.backend.prod(data, axis=axis, dtype=dtype, keepdims=keepdims)

        return Tensor(self.product, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient) -> None:
        if self.tensor and self.tensor.requireGradient:
            tensorNoneZero = self.backend.where(self.tensor.data != 0, self.tensor.data, 1)
            self.tensor.gradient = self.backend.multiply(gradient, self.backend.divide(self.product, tensorNoneZero))
            if self.tensor.gradientFunc:
                self.tensor.gradientFunc.backward(self.tensor.gradient)


#
# Minimum/Maximum etc
#


class Maximum(Operation):
    __slots__ = ['tensor1', 'tensor2', 'data']

    def __init__(self):
        super().__init__()
        self.tensor1 = None
        self.tensor2 = None

    def forward(self, tensor1: Tensor, tensor2: Tensor, out=None, where=True, casting='same_kind', oder='k', dtype=None, subhok=True) -> Tensor:
        tensor1 = checkTensor(tensor1)
        tensor2 = checkTensor(tensor2)

        requireGradient = tensor1.requireGradient or tensor2.requireGradient
        if requireGradient:
            self.tensor1 = tensor1
            self.tensor2 = tensor2

        self.data = self.backend.maximum(tensor1.data, tensor2.data, out=out, where=where, casting=casting, oder=oder, dtype=dtype, subhok=subhok)

        return Tensor(self.data, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient) -> None:
        if self.tensor1 and self.tensor1.requireGradient:
            # A mask that is True where tensor1 had the maximum value, False elsewhere
            mask = (self.tensor1.data == self.data)
            self.tensor1.gradient = self.backend.multiply(gradient, mask)
            if self.tensor1.gradientFunc:
                self.tensor1.gradientFunc.backward(self.tensor1.gradient)

        if self.tensor2 and self.tensor2.requireGradient:
            # A mask that is True where tensor2 had the maximum value, False elsewhere
            mask = (self.tensor2.data == self.data)
            self.tensor2.gradient = self.backend.multiply(gradient, mask)
            if self.tensor2.gradientFunc:
                self.tensor2.gradientFunc.backward(self.tensor2.gradient)


class Minimum(Operation):
    __slots__ = ['tensor1', 'tensor2', 'data']

    def __init__(self):
        super().__init__()
        self.tensor1 = None
        self.tensor2 = None

    def forward(self, tensor1: Tensor, tensor2: Tensor, out=None, where=True, casting='same_kind', oder='k', dtype=None, subhok=True) -> Tensor:
        tensor1 = checkTensor(tensor1)
        tensor2 = checkTensor(tensor2)

        requireGradient = tensor1.requireGradient or tensor2.requireGradient
        if requireGradient:
            self.tensor1 = tensor1
            self.tensor2 = tensor2

        self.data = self.backend.minium(tensor1.data, tensor2.data, out=out, where=where, casting=casting, oder=oder, dtype=dtype, subhok=subhok)

        return Tensor(self.data, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient) -> None:
        if self.tensor1 and self.tensor1.requireGradient:
            # A mask that is True where tensor1 had the minimum value, False elsewhere
            mask = (self.tensor1.data == self.data)
            self.tensor1.gradient = self.backend.multiply(gradient, mask)
            if self.tensor1.gradientFunc:
                self.tensor1.gradientFunc.backward(self.tensor1.gradient)

        if self.tensor2 and self.tensor2.requireGradient:
            # A mask that is True where tensor2 had the minimum value, False elsewhere
            mask = (self.tensor2.data == self.data)
            self.tensor2.gradient = self.backend.multiply(gradient, mask)
            if self.tensor2.gradientFunc:
                self.tensor2.gradientFunc.backward(self.tensor2.gradient)


#
# Min/Max etc
#


class Max(Operation):
    __slots__ = ['tensor', 'mask']

    def __init__(self):
        super().__init__()
        self.tensor = None

    def forward(self, tensor: Tensor, axis=None, keepdims=False) -> Tensor:
        tensor = checkTensor(tensor)
        data = self.backend.max(tensor.data, axis=axis, keepdims=keepdims)

        requireGradient = tensor.requireGradient
        if requireGradient:
            self.tensor = tensor
            self.mask = (tensor.data == self.backend.broadcast_to(data, tensor.shape))

        return Tensor(data, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient) -> None:
        if self.tensor and self.tensor.requireGradient:
            self.tensor.gradient = self.backend.multiply(self.mask, gradient)
            if self.tensor.gradientFunc:
                self.tensor.gradientFunc.backward(self.tensor.gradient)


class Min(Operation):
    __slots__ = ['tensor', 'mask']

    def __init__(self) -> None:
        super().__init__()
        self.tensor = None

    def forward(self, tensor: Tensor, axis=None, keepdims=False) -> Tensor:
        tensor = checkTensor(tensor)
        data = self.backend.min(tensor.data, axis=axis, keepdims=keepdims)

        requireGradient = tensor.requireGradient
        if requireGradient:
            self.tensor = tensor
            self.mask = (tensor.data == self.backend.broadcast_to(data, tensor.shape))

        return Tensor(data, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient: np.ndarray) -> None:
        if self.tensor and self.tensor.requireGradient:
            self.tensor.gradient = self.backend.multiply(self.mask, gradient)
            if self.tensor.gradientFunc:
                self.tensor.gradientFunc.backward(self.tensor.gradient)


class Mean(Operation):
    __slots__ = ['tensor', 'divisor']

    def __init__(self) -> None:
        super().__init__()
        self.tensor = None

    def forward(self, tensor: Tensor, axis=None, keepdims=False) -> Tensor:
        tensor = checkTensor(tensor)
        data = self.backend.mean(tensor.data, axis=axis, keepdims=keepdims)

        requireGradient = tensor.requireGradient
        if requireGradient:
            self.tensor = tensor

            if axis is None:
                self.divisor = self.backend.prod(tensor.shape)
            elif isinstance(axis, int):
                self.divisor = self.backend.prod(tensor.shape[axis])
            else:
                self.divisor = self.backend.prod([tensor.shape[i] for i in axis])

        return Tensor(data, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient: np.ndarray) -> None:
        if self.tensor and self.tensor.requireGradient:
            self.tensor.gradient = self.backend.divide(gradient, self.divisor)
            if self.tensor.gradientFunc:
                self.tensor.gradientFunc.backward(self.tensor.gradient)


class Var(Operation):
    __slots__ = ['tensor', 'divisor', 'diff']

    def __init__(self) -> None:
        super().__init__()
        self.tensor = None

    def forward(self, tensor: Tensor, axis=None, ddof=0, keepdims=False) -> Tensor:
        tensor = checkTensor(tensor)
        data = self.backend.var(tensor.data, axis=axis, ddof=ddof, keepdims=keepdims)

        requireGradient = tensor.requireGradient
        if requireGradient:
            self.tensor = tensor
            self.diff = self.backend.subtract(tensor.data, self.backend.mean(tensor.data, axis=axis, keepdims=True))

            if axis is None:
                self.divisor = self.backend.subtract(self.backend.prod(tensor.shape), ddof)
            elif isinstance(axis, int):
                self.divisor = self.backend.subtract(self.backend.prod(tensor.shape[axis]), ddof)
            else:
                self.divisor = self.backend.subtract(self.backend.prod([tensor.shape[i] for i in axis]), ddof)

        return Tensor(data, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient: np.ndarray) -> None:
        if self.tensor and self.tensor.requireGradient:
            self.tensor.gradient = self.backend.multiply(self.backend.multiply(self.backend.divide(2.0, self.divisor), self.diff), gradient)
            if self.tensor.gradientFunc:
                self.tensor.gradientFunc.backward(self.tensor.gradient)


class Std(Operation):
    __slots__ = ['tensor', 'divisor', 'diff']

    def __init__(self) -> None:
        super().__init__()
        self.tensor = None

    def forward(self, tensor: Tensor, axis=None, keepdims=False) -> Tensor:
        tensor = checkTensor(tensor)
        data = self.backend.std(tensor.data, axis=axis, keepdims=keepdims)

        requireGradient = tensor.requireGradient
        if requireGradient:
            self.tensor = tensor
            self.diff = self.backend.subtract(tensor.data, self.backend.mean(tensor.data, axis=axis, keepdims=True))

            if axis is None:
                self.divisor = self.backend.prod(tensor.shape)
            elif isinstance(axis, int):
                self.divisor = self.backend.prod(tensor.shape[axis])
            else:
                self.divisor = self.backend.prod([tensor.shape[i] for i in axis])

        return Tensor(data, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient: np.ndarray) -> None:
        if self.tensor and self.tensor.requireGradient:
            self.tensor.gradient = self.backend.multiply(gradient, self.backend.divide(self.diff, self.backend.multiply(self.divisor, self.tensor.data)))
            if self.tensor.gradientFunc:
                self.tensor.gradientFunc.backward(self.tensor.gradient)


#
# Others
#


class Pad(Operation):
    __slots__ = ['tensor', 'padding', 'mode', 'constant_values']

    def __init__(self) -> None:
        super().__init__()
        self.tensor = None

    def forward(self, tensor: Tensor, pad_with, mode='constant', constant_values=0) -> Tensor:
        tensor = checkTensor(tensor)

        self.padding = pad_with
        self.mode = mode
        self.constant_values = constant_values

        requireGradient = tensor.requireGradient
        if requireGradient:
            self.tensor = tensor

        data = self.backend.pad(tensor.data, self.padding, mode=self.mode, constant_values=self.constant_values)

        return Tensor(data, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient) -> None:
        if self.tensor and self.tensor.requireGradient:
            slices = tuple(slice(pad[0], -pad[1] if pad[1] != 0 else None) for pad in self.padding)
            self.tensor.gradient = gradient[slices]
            if self.tensor.gradientFunc:
                self.tensor.gradientFunc.backward(self.tensor.gradient)


class Insert(Operation):
    __slots__ = ['tensor', 'values', 'index']

    def __init__(self) -> None:
        super().__init__()
        self.tensor = None

    def forward(self, tensor: Tensor, values: Tensor, index: ArrayLike) -> Tensor:
        self.index = index
        tensor = checkTensor(tensor)
        values = checkTensor(values)

        requireGradient = tensor.requireGradient or values.requireGradient
        if requireGradient:
            self.tensor = tensor
            self.values = values

        data = self.backend.insert(tensor.data, self.index, values.data)
        return Tensor(data, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient) -> None:
        if self.tensor and self.tensor.requireGradient:
            self.tensor.gradient = self.backend.delete(gradient, self.index)
            if self.tensor.gradientFunc:
                self.tensor.gradientFunc.backward(self.tensor.gradient)

        if self.values and self.values.requireGradient:
            self.values.gradient = gradient[self.index]
            if self.values.gradientFunc:
                self.values.gradientFunc.backward(self.values.gradient)


class Transpose(Operation):
    __slots__ = ['tensor']

    def __init__(self) -> None:
        super().__init__()
        self.tensor = None

    def forward(self, tensor: Tensor) -> Tensor:
        tensor = checkTensor(tensor)

        if tensor.requireGradient:
            self.tensor = tensor

        data = self.backend.transpose(tensor.data)
        return Tensor(data, requireGradient=tensor.requireGradient, gradientFunc=self)

    def backward(self, gradient) -> None:
        if self.tensor and self.tensor.requireGradient:
            self.tensor.gradient = self.backend.transpose(gradient)
            if self.tensor.gradientFunc:
                self.tensor.gradientFunc.backward(self.tensor.gradient)


class Where(Operation):
    __slots__ = ['condition', 'tensor1', 'tensor2']

    def __init__(self) -> None:
        super().__init__()
        self.tensor1 = None
        self.tensor2 = None

    def forward(self, condition, tensor1: Tensor, tensor2: Tensor) -> Tensor:
        tensor1 = checkTensor(tensor1)
        tensor2 = checkTensor(tensor2)

        requireGradient = tensor1.requireGradient or tensor2.requireGradient
        if requireGradient:
            self.condition = condition
            self.tensor1 = tensor1
            self.tensor2 = tensor2

        data = self.backend.where(condition, tensor1.data, tensor2.data)
        return Tensor(data, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient) -> None:
        if self.tensor1 and self.tensor1.requireGradient:
            self.tensor1.gradient = self.backend.multiply(gradient, self.condition)
            if self.tensor1.gradientFunc:
                self.tensor1.gradientFunc.backward(self.tensor1.gradient)

        if self.tensor2 and self.tensor2.requireGradient:
            self.tensor2.gradient = self.backend.multiply(gradient, self.backend.logical_not(self.condition))
            if self.tensor2.gradientFunc:
                self.tensor2.gradientFunc.backward(self.tensor2.gradient)


class Cumsum(OneTensor):
    def __init__(self, axis) -> None:
        super().__init__()
        self.axis = axis

    def _operation(self, data: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.cumsum(data, self.axis)
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.cumsum(gradient, -self.axis)[::-1]


class Cumprod(OneTensor):
    def __init__(self, axis) -> None:
        super().__init__()
        self.axis = axis

    def _operation(self, data: np.ndarray, *args, **kwargs) -> np.ndarray:
        return self.backend.cumprod(data, self.axis)
johannes bilk's avatar
johannes bilk committed
    def _derivative(self, gradient: np.ndarray, *args, **kwargs) -> np.ndarray:
        prod = self._operation(self.tensor.data)
        return self.backend.divide(gradient, prod)


#
# Not working correctly
#


class AsStrided(Operation):
    """
    An as_strided operation with backward pass for convolutional gradients
    """

    __slots__ = ['tensor', 'patches', 'shape', 'strides']

    def __init__(self) -> None:
        super().__init__()

    def forward(self, tensor: Tensor, shape=None, strides=None, subok=False) -> Tensor:
        tensor = checkTensor(tensor)

        requireGradient = tensor.requireGradient
        if requireGradient:
            self.tensor = tensor

        self.shape = shape
        self.strides = strides
        self.patches = self.backend.as_strided(tensor.data, shape=shape, strides=strides, subok=False)
        gradientPatches = self.backend.as_strided(tensor.gradient, shape=shape, strides=strides, subok=False)

        return Tensor(data=self.patches, gradient=gradientPatches, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient) -> None:
        if self.tensor and self.tensor.requireGradient:
            # Sum up the gradient patches into the tensor gradient
            self.tensor.gradient = gradient.sum(tuple(self.backend.arange(gradient.ndim - self.tensor.ndim)))
            if self.tensor.gradientFunc:
                self.tensor.gradientFunc.backward(self.tensor.gradient)


class SlidingWindow(Operation):
    """
    An as_strided operation with backward pass for convolutional gradients
    """

    __slots__ = ['tensor', 'patches', 'shape', 'axis']

    def __init__(self) -> None:
        super().__init__()

    def forward(self, tensor: Tensor, window_shape=None, axis=None, *, subok=False, writeable=True) -> Tensor:
        tensor = checkTensor(tensor)

        requireGradient = tensor.requireGradient
        if requireGradient:
            self.tensor = tensor

        self.shape = window_shape
        self.axis = axis
        self.patches = self.backend.sliding_window_view(tensor.data, window_shape=window_shape, axis=axis, subok=subok, writeable=writeable)
        gradientPatches = self.backend.sliding_window_view(tensor.gradient, window_shape=window_shape, axis=axis, subok=subok, writeable=writeable)

        return Tensor(data=self.patches, gradient=gradientPatches, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient) -> None:
        if self.tensor and self.tensor.requireGradient:
            # Sum up the gradient patches into the tensor gradient
            self.tensor.gradient = gradient.sum(tuple(self.backend.range(gradient.ndim - self.tensor.data.ndim)))
            if self.tensor.gradientFunc:
                self.tensor.gradientFunc.backward(self.tensor.gradient)


class Einsum(Operation):
    """
    A placeholder einsum operation with backward pass for convolutional gradients
    """

    __slots__ = ['tensor1', 'tensor2', 'einsums']

    def __init__(self) -> None:
        super().__init__()

    def forward(self, tensor1: Tensor, tensor2: Tensor, optimize=False) -> Tensor:
        tensor1 = checkTensor(tensor1)
        tensor2 = checkTensor(tensor2)

        requireGradient = tensor1.requireGradient or tensor2.requireGradient
        if requireGradient:
            self.tensor1 = tensor1
            self.tensor2 = tensor2

        self.einsums = self.backend.einsum('bihwkl,oikl->bohw', tensor1.data, tensor2.data, optimize=optimize)
        return Tensor(self.einsums, requireGradient=requireGradient, gradientFunc=self)

    def backward(self, gradient) -> None:
        if self.tensor1 and self.tensor1.requireGradient:
            # Create gradient patches for tensor1
            self.tensor1.gradient = self.backend.as_strided(gradient,
                                                            shape=(*self.tensor1.data.shape, *self.tensor2.data.shape[-2:]),
                                                            strides=(*self.tensor1.data.strides, 0, 0))
            if self.tensor1.gradientFunc:
                self.tensor1.gradientFunc.backward(self.tensor1.gradient)

        if self.tensor2 and self.tensor2.requireGradient:
            # Create gradient patches for tensor2
            self.tensor2.gradient = self.backend.as_strided(gradient,
                                                            shape=(*self.tensor2.data.shape[:-2], *self.tensor1.data.shape[-2:]),
                                                            strides=(0, 0, *self.tensor1.data.strides[-2:]))
            if self.tensor2.gradientFunc:
                self.tensor2.gradientFunc.backward(self.tensor2.gradient)


#
# Mapping from Numpy to Tensor
#


ufuncMap = {
    np.add: Add,
    np.subtract: Subtract,
    np.multiply: Multiply,
    np.divide: Divide,
    np.matmul: Matmul,
    np.dot: Dot,
    np.power: Power,
    np.sqrt: Sqrt,
    np.log: Log,
    np.exp: Exp,
    np.sin: Sin,
    np.cos: Cos,
    np.cos: Tan,
    np.sinh: Sinh,
    np.cosh: Cosh,
    np.tanh: Tanh,
    np.abs: Abs,
    np.sign: Sign,
    np.positive: Positive,
    np.negative: Negative,
    np.maximum: Maximum,
    np.minimum: Minimum
}

funcMap = {
    np.sum: Sum,
    np.prod: Prod,
    np.repeat: Repeat,
    np.tile: Tile,
    np.max: Max,
    np.min: Min,
    np.mean: Mean,
    np.var: Var,
    np.std: Std,
    np.reshape: Reshape,
    np.transpose: Transpose,
    np.concatenate: Concatenate,
    np.hstack: Hstack,
    np.vstack: Vstack,
    np.dstack: Dstack,
    np.split: Split,
    np.hsplit: Hsplit,
    np.vsplit: Vsplit,
    np.dsplit: Dsplit,
    np.pad: Pad,
    np.insert: Insert,
    np.where: Where,
    np.einsum: Einsum
}