Skip to main content

A Hardware-Accelerated Tensor Computation and Autograd Engine

Project description

DepthTensor: A Hardware-Accelerated Tensor Computation and Autograd Engine

DepthTensor is a light-weight, high-performance library for reverse-mode automatic differentiation (AD). It is useful in building the mathematical foundation of deep learning frameworks, with the use of a Tensor object which dynamically builds computational graphs and computes gradients using Vector-Jacobian Products (VJP), generalized for tensors of abritrary rank.

Note: This is the core autograd and tensor computation engine. For the full deep learning framework, refer to DepthML.

1. Mathematical Foundation

The goal is to compute the gradient of scalar field $L$ with respect to an input tensor $X$, denoted as the adjoint $\bar{X}$.

1.1. Generalized VJPs via Tensor Contractions

Let $X$ be a tensor of shape $\mathcal{I} = (i_1, \dots, i_n)$ and $Y = f(X)$ be a tensor of shape $\mathcal{J} = (j_1, \dots, j_m)$.

Mathematically, the Jacobian is the tensor of rank $n + m$ which contains all the partial derivatives $\frac{\partial Y_{\mathcal{J}}}{\partial X_{\mathcal{I}}}$. DepthTensor does not compute this object, but, rather, it computes the contraction of the incoming adjoint $\bar{Y}$ with the local derivative:

$$ \bar{X}{i_1 \dots i_n} = \sum{j_1 \dots j_m} \bar{Y}{j_1 \dots j_m} \frac{\partial Y{j_1 \dots j_m}}{\partial X_{i_1 \dots i_n}} $$

If X and Y are scalars, indices vanish, and this reduces to:

$$ \bar{x} = \bar{y} \cdot f'(x) $$

If X and Y are matrices, this reduces to:

$$ \bar{X} = \bar{Y} W^{T} $$

1.2. Graph Topology and Gradient Accumulation

Gradients are accumulated using a Depth-First Search (DFS) topological sort, which ensures that for any node $X$ with multiple gradient flow streams ${Y^{(1)}, \dots, Y^{k}}$, the gradient sum is the sum of contractions:

$$ \bar{X} = \sum_{k} \mathrm{VJP}(Y^{(k)}, X) $$

All gradients are aggregated from all gradient downstreams.

2. Architecture and System Design

2.1. The differentiable Primitive (Tensor)

The Tensor class acts like a node in the Directed Acyclic Graph (DAG).

Operations are automatically dispatched to backend computers, which consists of numpy (CPU) and cupy (GPU).

Computational graphs are built dynamically at runtime.

3. Empirical Validation

We will verify the tensor engine by minimizing the Rosenbrock function, which is a non-convex optimization benchmark:

$$ f(x, y) = (a - x)^2 + b(y - x^2)^2 $$

import depthtensor as dt

# Initialize tensors
x = dt.Tensor([1.2], device="gpu", requires_grad=True)
y = dt.Tensor([1.2], device="gpu", requires_grad=True)

a, b = dt.Tensor([1], device="gpu"), dt.Tensor(
    [100], device="gpu"
)

# 2. Optimization Loop
lr = 0.001
for i in range(500):
    # Rosenbrock: f(x,y) = (a-x)^2 + b(y-x^2)^2
    loss = (a - x) ** 2 + b * (y - x**2) ** 2

    # Backpward pass
    dt.differentiate(loss)

    # Gradient Descent
    x.data -= lr * x.grad  # type: ignore
    y.data -= lr * y.grad  # type: ignore

    # Zero grads
    x.zeros_grad()
    y.zeros_grad()

    if i % 10 == 0:
        print(loss.item())

print(f"Converged: ({x.data}, {y.data})")
# Target: (1.0, 1.0)

4. Installation

Requirements:

  • numpy
  • cupy (optional: for NVIDIA GPU acceleration)

4.1. Bash

git clone https://github.com/YourUsername/DepthTensor.git
pip install -e .

4.2. Pip

pip install depthtensor

Author: Le Hong Ha

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

depthtensor-2.4.0.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

depthtensor-2.4.0-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file depthtensor-2.4.0.tar.gz.

File metadata

  • Download URL: depthtensor-2.4.0.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for depthtensor-2.4.0.tar.gz
Algorithm Hash digest
SHA256 7a78ca5e7dfefc00e0fa516530780146481308b3f90d6aed722646d9b8dfbe9e
MD5 7fd5265fd63775674f1b69c9e9d4f62e
BLAKE2b-256 be3162dff5ffb1a979e6d4527901a0f785540838afc43589198e6d6a6243b1cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for depthtensor-2.4.0.tar.gz:

Publisher: publish.yml on l-h-ha/DepthTensor

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file depthtensor-2.4.0-py3-none-any.whl.

File metadata

  • Download URL: depthtensor-2.4.0-py3-none-any.whl
  • Upload date:
  • Size: 20.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for depthtensor-2.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a38047444d9a45aa54a17938f6a8c45d98df65cc8db0575b7aa098e46722a17c
MD5 d18e9780338402c7b3f889f6f1696b0b
BLAKE2b-256 0b0a708fef81b5048ffb3997d4b7ee33fc701ae8e38ddf5d476e80d41a636589

See more details on using hashes here.

Provenance

The following attestation bundles were made for depthtensor-2.4.0-py3-none-any.whl:

Publisher: publish.yml on l-h-ha/DepthTensor

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page