⚠️ DO NOT USE IN PRODUCTIONThis project is currently in v0.x and is under active development. The API, behavior, and output format may change at any time. Expect breaking changes and bugs.
A Differentiable, Constraint-Oriented Programming Language
Website | Documentation | Get Started
Delta is a compiled language that transforms learning intent into optimized tensor programs. Instead of manually writing loss functions and training loops, you declare what you want your model to learn through constraints. Delta compiles this into efficient PyTorch code.
param weights = randn(784, 10);
obs images: Tensor[Float];
obs labels: Tensor[Int];
fn predict(x: Tensor[Float]) -> Tensor[Float] {
softmax(x @ weights)
}
let pred = predict(images);
require argmax(pred) == labels;
This compiles to an optimized PyTorch FX graph with automatic differentiation, constraint-based loss computation, and mode-specific specialization.
Define what your model should learn, not how. Delta compiles constraints into differentiable objectives automatically.
// Hard constraint: predictions must match labels
require pred == target;
// Soft constraint: prefer smaller weights (regularization)
prefer norm(weights) < 1.0 weight 0.01;
Explicitly mark learnable parameters vs. observed data. Delta tracks gradient flow and validates differentiability at compile time.
param theta = randn(10, 5); // Learnable - gradients flow here
obs x: Tensor[Float]; // Fixed input - no gradients
const scale = 0.1; // Compile-time constant
Write once, get optimized code for training, inference, and analysis:
train {
// Only runs during training
let dropout_mask = rand() > 0.5;
}
infer {
// Only runs during inference - dropout disabled
let dropout_mask = ones();
}
Conditionals compile to soft, differentiable operations by default:
// Compiles to: sigmoid((x - 0) / temperature) * a + (1 - sigmoid(...)) * b
if x > 0 temperature 0.1 { a } else { b }
pip install delta-lang- Python 3.10+
- PyTorch 2.0+ (installed automatically)
git clone https://github.com/deltalanguage/delta.git
cd delta
pip install -e .Create model.delta:
let model = Linear(10, 5);
obs x: Tensor[Float];
obs y: Tensor[Float];
fn forward(input: Tensor[Float]) -> Tensor[Float] {
relu(model(input))
}
let pred = forward(x);
require sum((pred - y) ** 2) == 0;
import delta
import torch
# Compile the Delta program
model = delta.compile("model.delta")
# Prepare data
x_train = torch.randn(100, 10)
y_train = torch.randn(100, 5)
# Train
model.fit(
{"x": x_train, "y": y_train},
epochs=100,
lr=0.01
)
# Inference
model.eval()
x_test = torch.randn(10, 10) # Test data with 10 samples
predictions = model(x_test)
print(predictions)// model.delta
let fc1 = Linear(784, 256);
let fc2 = Linear(256, 128);
let fc3 = Linear(128, 10);
fn forward(x: Tensor) -> Tensor {
fc3(relu(fc2(relu(fc1(x)))))
}
import delta
import torch
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
# Flatten images to 784-dim vectors
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda x: x.view(-1))
])
train_data = datasets.MNIST('./data', train=True, download=True, transform=transform)
train_loader = DataLoader(train_data, batch_size=64, shuffle=True)
model = delta.compile("model.delta")
model.fit(train_loader, epochs=10, lr=0.001)See example_projects/transformer/ for a complete implementation of a GPT-style language model in Delta.
| Type | Description |
|---|---|
Int |
Integer |
Float |
Floating-point |
Bool |
Boolean |
Tensor[T] |
Tensor with element type T |
Tensor[Float, N, M] |
Tensor with shape |
param name = initializer; // Learnable parameter
obs name: Type; // Observed input
const name = value; // Compile-time constant
let name = expr; // Local binding
require expr == target; // Hard equality
require expr > 0; // Hard inequality
prefer expr < 1.0 weight 0.5; // Soft constraint
prefer expr == target weight 1.0 slack s; // With slack variable
learn {
require forward(x) == y;
} with epochs = 10, batch_size = 32, optimizer = Adam(lr=1e-3);
Learn blocks define training objectives and configurations. The Delta compiler synthesizes a training loop loss function from the constraints.
if condition { then_expr } else { else_expr }
if condition temperature 0.1 { ... } // Soft/differentiable
for i in range(n) { ... }
// Note: while loops and recursion are currently not supported
train { ... } // Training-only code
infer { ... } // Inference-only code
analyze { ... } // Analysis-only code
non_diff { ... } // Non-differentiable (hard control flow allowed)
Delta is a staged compiler that:
- Parses Delta source into an AST
- Infers types and validates roles/effects
- Lowers to SIR (Semantic IR) — a differentiable-aware representation
- Relaxes hard operations into soft, differentiable alternatives
- Compiles constraints into penalty terms
- Specializes for the execution mode (train/infer)
- Lowers to PyTorch FX for execution
Delta never interprets tensor operations—it compiles everything to optimized PyTorch graphs.
Delta Source → AST → Typed AST → SIR → Optimized SIR → PyTorch FX → Execution
delta/
├── frontend/ # Lexer, parser, AST
├── types/ # Type system and inference
├── ir/ # Semantic IR (SIR) definition
├── passes/ # Compiler passes (relaxation, optimization)
├── backend/ # PyTorch FX lowering
├── runtime/ # Execution context and model wrapper
├── stdlib/ # Standard library (nn, optim, tensor ops)
└── compiler.py # Main compiler orchestration
Core tensor operations: zeros, ones, randn, cat, stack, reshape, transpose, etc.
zeros,ones,randn,fullsupport dynamic shapes via list literals.catandstacksupport both variadic argumentscat(y1, y2, dim=0)and list literalscat([y1, y2], dim=0).reshapesupports variadic arguments and list literals:reshape(x, -1, 10, 2)orreshape(x, [-1, 10, 2]).
Neural network layers: Linear, Conv2d, LSTM, MultiheadAttention, ReLU, Softmax, etc.
LSTMandMultiheadAttentionreturn tuples and support destructuring:let (out, hidden) = rnn(x);.MultiheadAttentionsupports 3 inputs:let (out, weights) = attn(query, key, value);.
Probability distributions: Normal, Bernoulli, Categorical, Uniform, etc.
Support keyword arguments: Bernoulli(probs=p).
Support the ~ operator for observations: require y ~ Bernoulli(probs=p);.
Optimizers and schedulers:
Adam,AdamW,SGD,RMSprop,Adagrad,LBFGS- Support
lr,weight_decay, and specific hyperparameters (e.g.,betas,momentum).
Data loading utilities: DataLoader, Dataset
# Run all tests
pytest tests/
# Run specific test file
pytest tests/test_parser.py -v
# Run with coverage
pytest tests/ --cov=delta- Lexer and parser
- Type inference
- SIR (Semantic IR)
- PyTorch FX backend
- Basic constraint compilation
- Mode specialization (train/infer)
- Standard library
- Probabilistic inference
- Advanced debugging (
whycommand)
Contributions are welcome!
- Fork the repository at github.com/deltalanguage/delta
- Create a feature branch:
git checkout -b feature/my-feature - Make your changes
- Run tests:
pytest tests/ - Submit a pull request
MIT License. See LICENSE for details.
Delta builds on ideas from:
- Differentiable programming (JAX, PyTorch)
- Probabilistic programming (Stan, Pyro)
- Constraint-based learning research
- Compiler design (MLIR, XLA)