Migrating from PyTorch to Gollek
A comprehensive guide for PyTorch developers transitioning to Gollek SDK.
Philosophy
Gollek is designed with PyTorch developers in mind. The API closely mirrors PyTorch’s conventions and patterns, making the transition smooth.
Key Similarities:
- Tensor-first API design
- Module-based architecture
- Autograd with dynamic computational graphs
- Familiar optimizer and loss function names
- Same training loop structure
Key Differences:
- Java-based (JVM, not Python)
- Static typing (more verbose, but safer)
- No dynamic shape inference (shapes declared upfront)
- Built-in model zoo with Java implementations
Quick Reference
Tensor Creation
PyTorch:
import torch
x = torch.randn(10, 20)
y = torch.zeros(5, 5)
z = torch.ones(3, 3)
t = torch.tensor([1.0, 2.0, 3.0])
Gollek:
import tech.kayys.gollek.sdk.core.Tensor;
Tensor x = Tensor.randn(10, 20);
Tensor y = Tensor.zeros(5, 5);
Tensor z = Tensor.ones(3, 3);
Tensor t = Tensor.of(new float[]{1f, 2f, 3f}, 3);
Tensor Operations
PyTorch:
# Slicing
x = tensor[:10, 5:15]
# Concatenation
y = torch.cat([x, z], dim=1)
# Stacking
s = torch.stack([x, y, z], dim=0)
# Gathering
g = torch.gather(x, 1, indices)
# Boolean indexing
mask = x > 0
pos = x[mask]
Gollek:
// Slicing
Tensor x = TensorOps.slice(tensor, 0, 0, 10);
Tensor y = TensorOps.slice(tensor, 1, 5, 15);
// Concatenation
Tensor y = TensorOps.cat(1, List.of(x, z));
// Stacking
Tensor s = TensorOps.stack(0, List.of(x, y, z));
// Gathering
Tensor g = TensorOps.gather(1, x, indices);
// Boolean operations
Tensor mask = TensorOps.gt(x, 0);
Tensor pos = TensorOps.maskedSelect(x, mask);
Basic Arithmetic
PyTorch:
a = x + y
b = x * y
c = x @ y # Matrix multiplication
d = x.T # Transpose
e = x.reshape(-1) # Flatten
Gollek:
Tensor a = x.add(y);
Tensor b = x.multiply(y);
Tensor c = x.matmul(y); // or x.matmul(y)
Tensor d = x.transpose(0, 1);
Tensor e = x.reshape(-1);
Neural Network Modules
PyTorch:
import torch.nn as nn
class MyModel(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(784, 128)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
return x
model = MyModel()
Gollek:
import tech.kayys.gollek.ml.nn.*;
class MyModel extends NNModule {
Linear fc1 = new Linear(784, 128);
ReLU relu = new ReLU();
Linear fc2 = new Linear(128, 10);
@Override
public GradTensor forward(GradTensor x) {
x = fc1.forward(x);
x = relu.forward(x);
x = fc2.forward(x);
return x;
}
}
MyModel model = new MyModel();
Or using Sequential:
PyTorch:
model = nn.Sequential(
nn.Linear(784, 128),
nn.ReLU(),
nn.Linear(128, 10)
)
Gollek:
Module model = new Sequential(
new Linear(784, 128),
new ReLU(),
new Linear(128, 10)
);
Optimizers
PyTorch:
import torch.optim as optim
optimizer = optim.Adam(model.parameters(), lr=0.001)
# or
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
Gollek:
import tech.kayys.gollek.ml.optimize.*;
Optimizer optimizer = new Adam(model.parameters(), 0.001f);
// or
Optimizer optimizer = new SGD(model.parameters(), 0.01f, 0.9f);
Loss Functions
PyTorch:
loss_fn = nn.CrossEntropyLoss()
loss = loss_fn(output, target)
Gollek:
Loss loss_fn = new CrossEntropyLoss();
GradTensor loss = loss_fn.forward(output, target);
Training Loop
PyTorch:
import torch
import torch.nn as nn
from torch.optim import Adam
# Setup
model = MyModel()
optimizer = Adam(model.parameters(), lr=0.001)
loss_fn = nn.CrossEntropyLoss()
# Training
for epoch in range(10):
for batch_x, batch_y in train_loader:
# Forward
output = model(batch_x)
loss = loss_fn(output, batch_y)
# Backward
optimizer.zero_grad()
loss.backward()
optimizer.step()
Gollek:
import tech.kayys.gollek.ml.nn.*;
import tech.kayys.gollek.ml.optimize.*;
// Setup
MyModel model = new MyModel();
Optimizer optimizer = new Adam(model.parameters(), 0.001f);
Loss loss_fn = new CrossEntropyLoss();
// Training
for (int epoch = 0; epoch < 10; epoch++) {
for (Batch batch : train_loader) {
Tensor batch_x = batch.getFeatures();
Tensor batch_y = batch.getLabels();
// Forward
GradTensor output = model.forward(new GradTensor(batch_x));
GradTensor loss = loss_fn.forward(output, new GradTensor(batch_y));
// Backward
optimizer.zeroGrad();
loss.backward();
optimizer.step();
}
}
Autograd
PyTorch:
x = torch.tensor([2.0, 3.0], requires_grad=True)
y = x * 2
z = y.sum()
z.backward()
print(x.grad) # [2., 2.]
Gollek:
Tensor x = Tensor.of(new float[]{2f, 3f}, 2);
GradTensor gx = new GradTensor(x);
GradTensor y = gx.multiply(2);
GradTensor z = y.sum();
z.backward();
System.out.println(gx.getGradient()); // [2., 2.]
Model Saving & Loading
PyTorch:
# Save
torch.save(model.state_dict(), 'model.pt')
# Load
model.load_state_dict(torch.load('model.pt'))
Gollek:
// Save
model.save("model.safetensors");
// Load
model.load("model.safetensors");
Tensor Reshaping
PyTorch:
x = torch.randn(10, 20)
y = x.reshape(-1) # Flatten
z = x.reshape(2, 5, 4, 5) # Reshape
w = x.unsqueeze(0) # Add dimension
v = x.squeeze() # Remove dimension
t = x.transpose(0, 1) # Swap dimensions
Gollek:
Tensor x = Tensor.randn(10, 20);
Tensor y = x.reshape(-1); // Flatten
Tensor z = x.reshape(2, 5, 4, 5); // Reshape
Tensor w = x.unsqueeze(0); // Add dimension
Tensor v = x.squeeze(); // Remove dimension
Tensor t = x.transpose(0, 1); // Swap dimensions
Data Loading
PyTorch:
from torch.utils.data import DataLoader, TensorDataset
# Create dataset
X = torch.randn(1000, 28, 28)
y = torch.randint(0, 10, (1000,))
dataset = TensorDataset(X, y)
# Create loader
loader = DataLoader(dataset, batch_size=32, shuffle=True)
# Iterate
for batch_x, batch_y in loader:
# Process batch
pass
Gollek:
import tech.kayys.gollek.train.data.*;
// Create dataset
Tensor X = Tensor.randn(1000, 28, 28);
Tensor y = Tensor.randint(0, 10, 1000);
TensorDataset dataset = new TensorDataset(X, y);
// Create loader
DataLoader loader = new DataLoader(dataset, 32, true);
// Iterate
for (Batch batch : loader) {
Tensor batch_x = batch.getFeatures();
Tensor batch_y = batch.getLabels();
// Process batch
}
Image Preprocessing (Vision Transforms)
PyTorch:
from torchvision import transforms
pipeline = transforms.Compose([
transforms.Resize(224),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
])
# Apply
transformed = pipeline(image)
Gollek:
import tech.kayys.gollek.ml.vision.transforms.*;
var pipeline = new VisionTransforms.Compose(
new VisionTransforms.Resize(224, 224),
new VisionTransforms.CenterCrop(224, 224),
new VisionTransforms.Normalize(
new float[]{0.485f, 0.456f, 0.406f},
new float[]{0.229f, 0.224f, 0.225f}
)
);
// Apply
Tensor transformed = pipeline.apply(image);
Distributed Training Setup
PyTorch:
from torch.nn import DataParallel, DistributedDataParallel
# Single machine, multiple GPUs
model = DataParallel(model, device_ids=[0, 1, 2])
# Multiple machines
model = DistributedDataParallel(model, device_ids=[0])
Gollek:
import tech.kayys.gollek.ml.distributed.*;
// Single machine, multiple GPUs (coming in v0.3)
// model = new DataParallel(model);
// Multiple machines (coming in v0.3)
// model = new DistributedDataParallel(model);
Common Patterns
Pattern 1: Simple Classification
PyTorch:
class Classifier(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.encoder = nn.Sequential(
nn.Linear(784, 256),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(256, 128),
nn.ReLU()
)
self.head = nn.Linear(128, num_classes)
def forward(self, x):
features = self.encoder(x)
logits = self.head(features)
return logits
model = Classifier(10)
optimizer = optim.Adam(model.parameters(), lr=0.001)
loss_fn = nn.CrossEntropyLoss()
for epoch in range(100):
for batch_x, batch_y in train_loader:
out = model(batch_x)
loss = loss_fn(out, batch_y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
Gollek:
class Classifier extends NNModule {
Module encoder;
Linear head;
public Classifier(int numClasses) {
encoder = new Sequential(
new Linear(784, 256),
new ReLU(),
new Dropout(0.5f),
new Linear(256, 128),
new ReLU()
);
head = new Linear(128, numClasses);
}
@Override
public GradTensor forward(GradTensor x) {
GradTensor features = encoder.forward(x);
return head.forward(features);
}
}
Classifier model = new Classifier(10);
Optimizer optimizer = new Adam(model.parameters(), 0.001f);
Loss loss_fn = new CrossEntropyLoss();
for (int epoch = 0; epoch < 100; epoch++) {
for (Batch batch : train_loader) {
GradTensor out = model.forward(new GradTensor(batch.getFeatures()));
GradTensor loss = loss_fn.forward(out, new GradTensor(batch.getLabels()));
optimizer.zeroGrad();
loss.backward();
optimizer.step();
}
}
Pattern 2: Convolutional Network
PyTorch:
class CNN(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3, 32, 3, padding=1)
self.conv2 = nn.Conv2d(32, 64, 3, padding=1)
self.pool = nn.MaxPool2d(2)
self.fc = nn.Linear(64 * 8 * 8, 10)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
Gollek:
class CNN extends NNModule {
Conv2d conv1 = new Conv2d(3, 32, 3, 1, 1);
Conv2d conv2 = new Conv2d(32, 64, 3, 1, 1);
MaxPool2d pool = new MaxPool2d(2, 2);
Linear fc = new Linear(64 * 8 * 8, 10);
@Override
public GradTensor forward(GradTensor x) {
x = pool.forward(new ReLU().forward(conv1.forward(x)));
x = pool.forward(new ReLU().forward(conv2.forward(x)));
x = x.view(-1);
x = fc.forward(x);
return x;
}
}
Pattern 3: Transfer Learning
PyTorch:
# Load pre-trained model
model = torchvision.models.resnet18(pretrained=True)
# Freeze backbone
for param in model.backbone.parameters():
param.requires_grad = False
# Replace head
model.fc = nn.Linear(512, 10)
# Fine-tune
optimizer = optim.Adam(model.fc.parameters(), lr=0.001)
Gollek:
// Load pre-trained model (coming in v0.3)
// Model model = ModelHub.load("resnet18");
// Freeze backbone (coming in v0.3)
// model.getBackbone().requiresGrad(false);
// Replace head
Linear head = new Linear(512, 10);
// Fine-tune
Optimizer optimizer = new Adam(head.parameters(), 0.001f);
Type System
Key Differences
| Aspect | PyTorch | Gollek |
|---|---|---|
| Type System | Dynamic (duck-typed) | Static (strongly-typed) |
| Shape Specification | Runtime inference | Upfront declaration |
| Tensor Types | torch.Tensor |
Tensor, GradTensor |
| Device | .to(device) |
Implicit via Device enum |
| Dtype | torch.float32 |
Fixed float32 (JVM) |
| Autograd | requires_grad=True |
GradTensor wrapper |
Example: Type Safety
PyTorch (Runtime Error Possible):
x = torch.randn(10, 20)
y = torch.randn(15, 20)
z = x + y # ERROR: shapes don't match - caught at runtime
Gollek (Compile-Time Safety):
Tensor x = Tensor.randn(10, 20);
Tensor y = Tensor.randn(15, 20);
// Tensor z = x.add(y); // ERROR: compile error if shapes wrong
// IDE/compiler catches this before runtime!
Performance Expectations
| Task | PyTorch | Gollek | Status |
|---|---|---|---|
| MNIST Training | 30s/epoch | 45s/epoch | 65% speed (CPU only) |
| Tensor Slicing | 2.1ms | 2.3ms | 91% speed (PyTorch) |
| Matrix Multiply | 5.2ms | 6.1ms | 85% speed |
| Backward Pass | 8.3ms | 12.4ms | 67% speed (autograd) |
Note: Gollek runs on JVM (pure Java) without CUDA. With GPU support (v0.4+), expect 95%+ parity.
Missing Features (Coming Soon)
| Feature | PyTorch | Gollek | Timeline |
|---|---|---|---|
| GPU/CUDA | ✅ | ⚠️ v0.4 | 2-4 weeks |
| Distributed DDP | ✅ | ⚠️ v0.3 | 1-2 weeks |
| Model Zoo | ✅ | ⚠️ v0.4 | 4-8 weeks |
| HF Integration | ✅ | ⚠️ v0.3 | 2-4 weeks |
| Quantization | ✅ | ✅ | Available now |
| ONNX Export | ✅ | ✅ | Available now |
Debugging Tips
Print Shapes
PyTorch:
print(x.shape) # torch.Size([10, 20])
Gollek:
System.out.println(Arrays.toString(x.shape())); // [10, 20]
Inspect Gradients
PyTorch:
loss.backward()
print(model.fc1.weight.grad)
Gollek:
loss.backward();
Tensor grad = model.fc1.getWeight().getGradient();
System.out.println(Arrays.toString(grad.getData()));
Enable Debugging
PyTorch:
torch.autograd.set_detect_anomaly(True)
Gollek:
// Set verbose mode
System.setProperty("gollek.debug", "true");
Getting Help
If you encounter issues migrating from PyTorch:
- Check Gollek Docs - Comprehensive API reference
- Browse Examples Docs - Current example hubs and runnable entrypoints
- Read FAQ - Common questions
- Ask on Discussions - Community support
- File Issue - Bug reports
Summary
Gollek makes it easy for PyTorch developers to build AI/ML applications in Java:
- ✅ Familiar API design (Tensors, Modules, optimizers)
- ✅ Dynamic computation graphs
- ✅ Autograd system
- ✅ Rich NN module library
- ✅ Type-safe Java environment
- ⚠️ CPU-based (GPU coming v0.4)
- ⚠️ Smaller ecosystem (growing)
Ready to get started? See Quick Start Guide.