Quickstart Guide
This guide provides a rapid introduction to defining a neural network, performing forward passes, backpropagation, and updating weights using AMEVA-Forge on CPU and WebGPU.
1. Tensor Creation and Operations
AMEVA-Forge's fundamental data structure is the Tensor. Tensor operations are recorded dynamically for automatic differentiation.
import forge as torch
# Define standard tensors with autograd enabled
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]], requires_grad=True)
y = torch.tensor([[5.0, 6.0], [7.0, 8.0]])
# Matrix multiplication on CPU or GPU
z = x @ y
print("Forward Result:\n", z.numpy())
2. Reverse-Mode Autograd
Calling .backward() computes gradients for all descendant tensors where requires_grad=True.
# Compute a scalar loss
loss = z.sum()
# Execute reverse accumulation
loss.backward()
print("Gradient of x:\n", x.grad.numpy())
3. Building Neural Networks with forge.nn
import forge.nn as nn
import forge.optim as optim
class SimpleMLP(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(2, 4)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(4, 1)
def forward(self, x):
return self.fc2(self.relu(self.fc1(x)))
model = SimpleMLP()
print(model)
4. Browser WebGPU Training Loop
Move the entire model to WebGPU using .to("gpu") and utilize async methods for non-blocking browser execution.
model = SimpleMLP().to("gpu")
optimizer = optim.SGD(model.parameters(), lr=0.05)
criterion = nn.MSELoss()
inputs = torch.randn((4, 2), device="gpu")
targets = torch.tensor([[0.0], [1.0], [1.0], [0.0]], device="gpu")
for step in range(50):
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, targets)
# Read loss scalar value asynchronously
loss_val = await loss.numpy_async()
loss.backward()
await optimizer.step_async()
if step % 10 == 0:
print(f"Step {step:02d} | GPU Loss: {float(loss_val):.5f}")