Core Components
The core module contains the fundamental building blocks of FIT.
Tensor
Core tensor implementation with automatic differentiation support.
- class fit.core.tensor.Tensor(data, requires_grad: bool = False)[source]
Bases:
objectCore tensor class with automatic differentiation capabilities.
A tensor stores data and tracks operations for gradient computation.
- __init__(data, requires_grad: bool = False)[source]
Initialize a tensor.
- Parameters:
data – The data array (numpy array or compatible)
requires_grad – Whether to track operations for gradient computation
- sum(axis=None, keepdims=False)[source]
Sum tensor elements along specified axis.
- Parameters:
axis – Axis along which to sum
keepdims – Whether to keep the summed dimensions
- Returns:
A new tensor with summed values
- mean(axis=None, keepdims=False)[source]
Calculate the mean of tensor elements along specified axis.
- property shape
Return the shape of the tensor data.
- property ndim
Return the number of dimensions.
- property size
Return the total number of elements.
- property T
Return the transpose of the tensor.
Autograd
This module implements the autograd engine for automatic differentiation.
The autograd engine tracks computations and builds a directed acyclic graph for efficient backpropagation.
- class fit.core.autograd.Node(requires_grad: bool = False)[source]
Bases:
objectRepresents a node in the computational graph.
Each node represents a value in the computation graph and tracks its dependencies (parents) as well as the backward function to compute gradients with respect to its inputs.
- class fit.core.autograd.Function[source]
Bases:
objectBase class for autograd functions.
Each function represents an operation in the computation and defines how to compute the forward pass and the backward pass (gradient computation).
- static apply(ctx: Dict[str, Any], *inputs: Any) Any[source]
Apply the function to inputs.
- Parameters:
ctx – Context dictionary to store data for the backward pass
*inputs – Input values
- Returns:
Output value(s)
- static backward(ctx: Dict[str, Any], grad_output: ndarray) Tuple[ndarray | None, ...][source]
Compute gradients with respect to inputs.
- Parameters:
ctx – Context dictionary with data stored during the forward pass
grad_output – Gradient with respect to the output
- Returns:
Tuple of gradients with respect to inputs
- class fit.core.autograd.Add[source]
Bases:
FunctionAddition function.
- static apply(ctx: Dict[str, Any], a: ndarray, b: ndarray) ndarray[source]
Apply the function to inputs.
- Parameters:
ctx – Context dictionary to store data for the backward pass
*inputs – Input values
- Returns:
Output value(s)
- static backward(ctx: Dict[str, Any], grad_output: ndarray) Tuple[ndarray, ndarray][source]
Compute gradients with respect to inputs.
- Parameters:
ctx – Context dictionary with data stored during the forward pass
grad_output – Gradient with respect to the output
- Returns:
Tuple of gradients with respect to inputs
- class fit.core.autograd.Multiply[source]
Bases:
FunctionElement-wise multiplication function.
- static apply(ctx: Dict[str, Any], a: ndarray, b: ndarray) ndarray[source]
Apply the function to inputs.
- Parameters:
ctx – Context dictionary to store data for the backward pass
*inputs – Input values
- Returns:
Output value(s)
- static backward(ctx: Dict[str, Any], grad_output: ndarray) Tuple[ndarray, ndarray][source]
Compute gradients with respect to inputs.
- Parameters:
ctx – Context dictionary with data stored during the forward pass
grad_output – Gradient with respect to the output
- Returns:
Tuple of gradients with respect to inputs
- class fit.core.autograd.MatMul[source]
Bases:
FunctionMatrix multiplication function.
- static apply(ctx: Dict[str, Any], a: ndarray, b: ndarray) ndarray[source]
Apply the function to inputs.
- Parameters:
ctx – Context dictionary to store data for the backward pass
*inputs – Input values
- Returns:
Output value(s)
- static backward(ctx: Dict[str, Any], grad_output: ndarray) Tuple[ndarray, ndarray][source]
Compute gradients with respect to inputs.
- Parameters:
ctx – Context dictionary with data stored during the forward pass
grad_output – Gradient with respect to the output
- Returns:
Tuple of gradients with respect to inputs
- class fit.core.autograd.Sum[source]
Bases:
FunctionSum reduction function.
- static apply(ctx: Dict[str, Any], a: ndarray, axis: int | None = None, keepdims: bool = False) ndarray[source]
Apply the function to inputs.
- Parameters:
ctx – Context dictionary to store data for the backward pass
*inputs – Input values
- Returns:
Output value(s)
- static backward(ctx: Dict[str, Any], grad_output: ndarray) Tuple[ndarray, None, None][source]
Compute gradients with respect to inputs.
- Parameters:
ctx – Context dictionary with data stored during the forward pass
grad_output – Gradient with respect to the output
- Returns:
Tuple of gradients with respect to inputs
- class fit.core.autograd.Mean[source]
Bases:
FunctionMean reduction function.
- static apply(ctx: Dict[str, Any], a: ndarray, axis=None, keepdims=False) ndarray[source]
Apply the function to inputs.
- Parameters:
ctx – Context dictionary to store data for the backward pass
*inputs – Input values
- Returns:
Output value(s)
- static backward(ctx: Dict[str, Any], grad_output: ndarray) Tuple[ndarray, None, None][source]
Compute gradients with respect to inputs.
- Parameters:
ctx – Context dictionary with data stored during the forward pass
grad_output – Gradient with respect to the output
- Returns:
Tuple of gradients with respect to inputs
- class fit.core.autograd.Exp[source]
Bases:
FunctionExponential function.
- static apply(ctx: Dict[str, Any], a: ndarray) ndarray[source]
Apply the function to inputs.
- Parameters:
ctx – Context dictionary to store data for the backward pass
*inputs – Input values
- Returns:
Output value(s)
- static backward(ctx: Dict[str, Any], grad_output: ndarray) Tuple[ndarray][source]
Compute gradients with respect to inputs.
- Parameters:
ctx – Context dictionary with data stored during the forward pass
grad_output – Gradient with respect to the output
- Returns:
Tuple of gradients with respect to inputs
- class fit.core.autograd.Log[source]
Bases:
FunctionNatural logarithm function.
- static apply(ctx: Dict[str, Any], a: ndarray) ndarray[source]
Apply the function to inputs.
- Parameters:
ctx – Context dictionary to store data for the backward pass
*inputs – Input values
- Returns:
Output value(s)
- static backward(ctx: Dict[str, Any], grad_output: ndarray) Tuple[ndarray][source]
Compute gradients with respect to inputs.
- Parameters:
ctx – Context dictionary with data stored during the forward pass
grad_output – Gradient with respect to the output
- Returns:
Tuple of gradients with respect to inputs
- class fit.core.autograd.Reshape[source]
Bases:
FunctionReshape function.
- static apply(ctx: Dict[str, Any], a: ndarray, shape: Tuple[int, ...]) ndarray[source]
Apply the function to inputs.
- Parameters:
ctx – Context dictionary to store data for the backward pass
*inputs – Input values
- Returns:
Output value(s)
- static backward(ctx: Dict[str, Any], grad_output: ndarray) Tuple[ndarray, None][source]
Compute gradients with respect to inputs.
- Parameters:
ctx – Context dictionary with data stored during the forward pass
grad_output – Gradient with respect to the output
- Returns:
Tuple of gradients with respect to inputs
- class fit.core.autograd.ReLU[source]
Bases:
FunctionRectified Linear Unit activation function.
- static apply(ctx: Dict[str, Any], a: ndarray) ndarray[source]
Apply the function to inputs.
- Parameters:
ctx – Context dictionary to store data for the backward pass
*inputs – Input values
- Returns:
Output value(s)
- static backward(ctx: Dict[str, Any], grad_output: ndarray) Tuple[ndarray][source]
Compute gradients with respect to inputs.
- Parameters:
ctx – Context dictionary with data stored during the forward pass
grad_output – Gradient with respect to the output
- Returns:
Tuple of gradients with respect to inputs
- fit.core.autograd.get_function(name: str) Function[source]
Get a function by name from the registry.
- Parameters:
name – Name of the function
- Returns:
Function class
- Raises:
ValueError – If the function is not found
Operations
This module implements specialized operations for tensors.
These operations build on the core tensor capabilities and autograd engine to provide higher-level functionality.
- fit.core.ops.matmul(a: Tensor, b: Tensor) Tensor[source]
Perform matrix multiplication: a @ b.
- Parameters:
a – First tensor
b – Second tensor
- Returns:
Result tensor
- fit.core.ops.transpose(x: Tensor, axes: Tuple[int, ...] | None = None) Tensor[source]
Transpose a tensor.
- Parameters:
x – Input tensor
axes – Permutation of dimensions (default is to reverse dimensions)
- Returns:
Transposed tensor
- fit.core.ops.einsum(equation: str, *tensors: Tensor) Tensor[source]
Einstein summation for tensors.
- Parameters:
equation – Summation equation in Einstein notation
*tensors – Input tensors
- Returns:
Result tensor
- fit.core.ops.sigmoid(x: Tensor) Tensor[source]
Apply sigmoid activation: 1 / (1 + exp(-x)).
- Parameters:
x – Input tensor
- Returns:
Output tensor
- fit.core.ops.softmax(x: Tensor, axis: int = -1) Tensor[source]
Apply softmax activation along specified axis.
- Parameters:
x – Input tensor
axis – Axis along which to apply softmax
- Returns:
Output tensor
- fit.core.ops.tanh(x: Tensor) Tensor[source]
Apply hyperbolic tangent activation: (exp(x) - exp(-x)) / (exp(x) + exp(-x)).
- Parameters:
x – Input tensor
- Returns:
Output tensor
- fit.core.ops.abs(x: Tensor) Tensor[source]
Compute the absolute value of a tensor.
- Parameters:
x – Input tensor
- Returns:
Output tensor
- fit.core.ops.sqrt(x: Tensor) Tensor[source]
Compute the square root of a tensor.
- Parameters:
x – Input tensor
- Returns:
Output tensor
- fit.core.ops.sin(x: Tensor) Tensor[source]
Compute the sine of a tensor (in radians).
- Parameters:
x – Input tensor
- Returns:
Output tensor
- fit.core.ops.cos(x: Tensor) Tensor[source]
Compute the cosine of a tensor (in radians).
- Parameters:
x – Input tensor
- Returns:
Output tensor
- fit.core.ops.zeros(shape: Tuple[int, ...], requires_grad: bool = False) Tensor[source]
Create a tensor filled with zeros.
- Parameters:
shape – Shape of the tensor
requires_grad – Whether the tensor requires gradient computation
- Returns:
Tensor filled with zeros
- fit.core.ops.ones(shape: Tuple[int, ...], requires_grad: bool = False) Tensor[source]
Create a tensor filled with ones.
- Parameters:
shape – Shape of the tensor
requires_grad – Whether the tensor requires gradient computation
- Returns:
Tensor filled with ones
- fit.core.ops.randn(shape: Tuple[int, ...], requires_grad: bool = False) Tensor[source]
Create a tensor filled with random values from a standard normal distribution.
- Parameters:
shape – Shape of the tensor
requires_grad – Whether the tensor requires gradient computation
- Returns:
Tensor filled with random values
- fit.core.ops.concatenate(tensors: List[Tensor], axis: int = 0) Tensor[source]
Concatenate tensors along specified axis.
- Parameters:
tensors – List of tensors to concatenate
axis – Axis along which to concatenate
- Returns:
Concatenated tensor
- fit.core.ops.std(x: Tensor, axis: int | None = None, keepdims: bool = False) Tensor[source]
Compute the standard deviation of a tensor.
- Parameters:
x – Input tensor
axis – Axis along which to compute standard deviation
keepdims – Whether to keep the reduced dimensions
- Returns:
Standard deviation tensor
- fit.core.ops.var(x: Tensor, axis: int | None = None, keepdims: bool = False) Tensor[source]
Compute the variance of a tensor.
- Parameters:
x – Input tensor
axis – Axis along which to compute variance
keepdims – Whether to keep the reduced dimensions
- Returns:
Variance tensor
- fit.core.ops.argmax(x: Tensor, axis: int | None = None) Tensor[source]
Return the indices of the maximum values along specified axis.
- Parameters:
x – Input tensor
axis – Axis along which to find maximum values
- Returns:
Indices tensor
- fit.core.ops.logsumexp(x: Tensor, axis: int | None = None, keepdims: bool = False) Tensor[source]
Compute the log of the sum of exponentials of input elements.
This is a numerically stable version of log(sum(exp(x))).
- Parameters:
x – Input tensor
axis – Axis along which to perform the operation
keepdims – Whether to keep the reduced dimensions
- Returns:
Result tensor
- fit.core.ops.mse_loss(predictions: Tensor, targets: Tensor, reduction: str = 'mean') Tensor[source]
Compute Mean Squared Error loss.
- Parameters:
predictions – Predicted values
targets – Target values
reduction – Reduction method (‘mean’ or ‘sum’)
- Returns:
Loss tensor
- fit.core.ops.binary_cross_entropy(predictions: Tensor, targets: Tensor, reduction: str = 'mean') Tensor[source]
Compute Binary Cross Entropy loss.
- Parameters:
predictions – Predicted probabilities (between 0 and 1)
targets – Binary target values (0 or 1)
reduction – Reduction method (‘mean’ or ‘sum’)
- Returns:
Loss tensor
- fit.core.ops.softmax_cross_entropy(logits: Tensor, targets: Tensor, reduction: str = 'mean') Tensor[source]
Compute Softmax Cross Entropy loss.
- Parameters:
logits – Unnormalized predictions (logits)
targets – Target class indices or one-hot encoded targets
reduction – Reduction method (‘mean’ or ‘sum’)
- Returns:
Loss tensor
- fit.core.ops.cosine_similarity(x1: Tensor, x2: Tensor, dim: int = 1, eps: float = 1e-08) Tensor[source]
Compute cosine similarity between tensors along specified dimension.
- Parameters:
x1 – First tensor
x2 – Second tensor
dim – Dimension along which to compute similarity
eps – Small value to avoid division by zero
- Returns:
Similarity tensor
- fit.core.ops.pairwise_distance(x1: Tensor, x2: Tensor, p: float = 2.0, eps: float = 1e-06) Tensor[source]
Compute pairwise distance between tensors.
- Parameters:
x1 – First tensor (batch_size x D)
x2 – Second tensor (batch_size x D)
p – p-norm to use for distance calculation
eps – Small value to avoid division by zero
- Returns:
Distance tensor (batch_size)