Docs/vectorphile

vectorphile

v0.3.0 Latest

The singular ML/DL Python library. One import replaces PyTorch + HuggingFace Transformers + scikit-learn + NumPy + Pandas + SciPy + XGBoost + LightGBM + CatBoost + JAX + Matplotlib — with a 5-tier GPU dispatch stack, 50+ production architectures, full tokenizer suite, RL algorithms, GGUF export, and a pretrained model hub. All from first principles.

Overview

vectorphile v0.3.0 reaches production completeness with 41 modules covering every corner of the ML ecosystem. The 5-tier backend dispatch makes it match PyTorch GPU speed when Triton, Numba or CuPy are installed. Every major DL architecture is implemented from scratch — no external DL dependencies required.

ModuleWhat you get
vp (core)Tensor, all ndarray ops, broadcasting, slicing, autograd
vp.backend5-tier GPU dispatch: Triton > PyTorch CUDA > Numba CUDA > im2col+BLAS > NumPy
vp.nnLinear, Conv, Pooling, RNN/LSTM/GRU, Attention, Transformer, Norm, activations, losses
vp.nn.architecturesCNN zoo (ResNet/VGG/EfficientNet/DenseNet), SSM (S4/Mamba), ViT/DeiT, VAE/DCGAN/DDPM, U-Net, GCN/GAT/GIN
vp.nn.flash_attentionO(N) memory tiled attention, causal masking
vp.nn.qatFakeQuantize, QATLinear, QATConv2d, prepare_qat, convert_qat, observers
vp.nn.optimizers_advancedLion, Sophia, Adan, Prodigy, ScheduleFreeAdamW
vp.nn.F50+ functional ops: activations, conv, pool, norm, dropout, all losses, fold/unfold
vp.nn.initxavier_uniform/normal, kaiming_uniform/normal, orthogonal, sparse, trunc_normal, dirac
vp.optimSGD, Adam, AdamW, RMSprop, Adagrad + LR schedulers
vp.autogradReverse-mode autodiff, dynamic computation graph, no_grad, checkpoint
vp.transformsgrad, vmap, jit, pmap, checkpoint, jacobian, hessian, jvp, vjp, eval_shape
vp.tokenizersBytePairEncoding, WordPieceTokenizer, SentencePieceTokenizer, CharacterTokenizer (all from scratch)
vp.textbeam_search, nucleus_sampling, greedy_search, speculative_decoding, apply_chat_template, TextGenerationPipeline
vp.visionCompose, Resize, RandomCrop, ColorJitter, AugMix, ToTensor, Normalize, RandomResizedCrop, make_grid, extract_features
vp.pretrainedload(), list_models(), download(), cache_info() — HuggingFace Hub compatible, safetensors + .bin loader
vp.trainerPyTorch Lightning-style Trainer, TrainableModel, training/validation loop, automatic device placement
vp.callbacksEarlyStopping, ModelCheckpoint, ReduceLROnPlateau, TensorBoardLogger, WandBLogger, CSVLogger, ProgressBar
vp.distributedDataParallel, DDP, GradientAccumulator, MixedPrecisionTrainer, launch
vp.metricsaccuracy, F1, AUC, BLEU, ROUGE, WER, perplexity, NDCG, ECE (all from scratch)
vp.rlDQN/Double/Dueling, REINFORCE, A2C, PPO, SAC, TD3 + CartPole/MountainCar/Pendulum envs
vp.exportto_onnx, ONNXRuntime, benchmark + GGUFReader, GGUFWriter, to_gguf, from_gguf (Q4_0/Q8_0)
vp.mlFull classical ML — 60+ estimators across all categories
vp.dataDataFrame, Series, GroupBy, merge, pivot, melt, read_csv
vp.data.datasetsMNIST, CIFAR10, CIFAR100, TinyShakespeare, IMDb, random_split, WeightedRandomSampler
vp.linalgdet, inv, svd, eig, lstsq, solve, qr, cholesky, pinv, norm
vp.statsmean, std, corr, cov, zscore, t-test, chi2, ANOVA, Pearson/Spearman
vp.integratequad, dblquad, nquad, solve_ivp, odeint, trapezoid, simpson, romberg
vp.interpolateCubicSpline, BSpline, griddata, RBFInterpolator, Akima1D, PCHIP
vp.spatialKDTree, cKDTree, ConvexHull, Delaunay, Voronoi, cdist, pdist
vp.ndimagegaussian_filter, sobel, label, zoom, rotate, morphological ops, distance_transform
vp.fftfft, ifft, rfft, fft2, fftshift
vp.signalstft, convolve, butter, filtfilt, spectrogram
vp.polynomialPolynomial, Chebyshev, Legendre, Laguerre, Hermite, Horner eval, Sturm, Lagrange
vp.maMaskedArray, masked_where/invalid/less/greater, all statistics on masked data
vp.sparsecsr_matrix, csc_matrix, coo_matrix — efficient sparse arithmetic
vp.optimizeminimize, root, curve_fit, differential_evolution
vp.ioMAT, WAV, Parquet, Feather, HDF5, NetCDF4, Matrix Market, NumPy native
vp.viz20+ plot types with themes, palettes — including ML-specific charts
vp.hubsave_model, load_model, Checkpointer, model_card, save/load_weights
vp.quantizationINT8/INT4/FP16 PTQ, QuantizedModel, MinMaxCalibrator, magnitude_prune
41 Modules
Complete ML/DL ecosystem in one package
50+ Architectures
CNN, SSM, ViT, GAN, Diffusion, U-Net, GNN
5-tier GPU
Triton > PyTorch CUDA > Numba > BLAS > NumPy

Installation

Requires Python 3.9 or later. NumPy and SciPy are the only required dependencies. All GPU acceleration is optional — vectorphile runs on CPU with NumPy by default and auto-upgrades when faster backends are detected.

TerminalPython
pip install vectorphile                    # core (numpy + scipy)
pip install "vectorphile[viz]"              # + matplotlib
pip install "vectorphile[io]"               # + pyarrow, h5py, netCDF4
pip install "vectorphile[cuda]"             # + cupy (GPU via CuPy)
pip install "vectorphile[numba]"            # + numba CUDA kernels
pip install "vectorphile[triton]"           # + OpenAI Triton fused kernels
pip install "vectorphile[pretrained]"       # + safetensors, huggingface-hub
pip install "vectorphile[onnx]"             # + onnx, onnxruntime
pip install "vectorphile[rl]"               # + gymnasium
pip install "vectorphile[full]"             # all extras
Python
>= 3.9
NumPy
>= 1.24
SciPy
>= 1.10
License
Apache-2.0
*

From source: Clone github.com/vectorphile/vectorphile and run pip install -e ".[dev]" for editable install with test dependencies.

Quick Start

A complete training loop using the Lightning-style Trainer API.

quickstart.pyPython
import vectorphile as vp
from vectorphile.trainer import Trainer, TrainableModel
from vectorphile.data.datasets import MNIST

# 1. Data
train_ds, val_ds = MNIST(root='./data').split(val_ratio=0.1)

# 2. Model
class MyNet(TrainableModel):
    def __init__(self):
        super().__init__()
        self.net = vp.nn.Sequential(
            vp.nn.Linear(784, 256),
            vp.nn.GELU(),
            vp.nn.Linear(256, 10),
        )
    def forward(self, x):
        return self.net(x)
    def training_step(self, batch):
        x, y = batch
        loss = vp.nn.CrossEntropyLoss()(self(x), y)
        return loss

# 3. Train
trainer = Trainer(
    max_epochs=10,
    optimizer='adamw', lr=1e-3,
    callbacks=['early_stopping', 'model_checkpoint'],
    device='auto',
)
trainer.fit(MyNet(), train_ds, val_ds)

Tensors

vp.core

The Tensor is the central primitive. Set requires_grad=True for gradient tracking. Operations dispatch automatically to the fastest available backend.

tensor_ops.pyPython
import vectorphile as vp

x = vp.randn(3, 4); y = vp.ones(4, 5)
z = x @ y                        # (3, 5) matmul

a = vp.zeros(5, 1) + vp.randn(1, 8)
b = a.reshape(5, 2, 4)
c = b.permute(2, 0, 1)

m = z.mean(); s = z.sum(axis=1, keepdims=True); mx, idx = z.max(axis=0)
x_f16 = x.astype('float16')
arr = x.numpy(); t = vp.from_numpy(arr)

Autograd

vp.autograd

Reverse-mode automatic differentiation. Dynamic computation graph supports arbitrary control flow. v0.3.0 adds jacobian, hessian, vmap, and gradient checkpointing.

autograd.pyPython
x = vp.tensor([2.0, 3.0], requires_grad=True)
w = vp.tensor([[1.0, 0.5], [0.5, 1.0]])
h = x @ w
y = (h ** 2 + vp.log(h.abs() + 1)).sum()
y.backward()
print(x.grad)

from vectorphile.autograd import jacobian, hessian
J = jacobian(lambda v: (v ** 2).sum(), x)
H = hessian(lambda v: (v ** 2).sum(), x)

with vp.no_grad():
    logits = model(X_test)

Transforms

vp.transforms

JAX-inspired functional transforms. vmap vectorizes any function over a batch dimension without loops.

gradvalue_and_gradjacobianhessianvmapjitpmapcheckpointjvpvjplinearizeeval_shape

Backend Dispatch

vp.backend

5-tier automatic dispatch selects the fastest available compute backend at runtime. When Triton fused kernels are available, vectorphile matches PyTorch performance. Falls back gracefully all the way to NumPy on CPU-only machines.

backend.pyPython
import vectorphile as vp
from vectorphile.backend import available_backends

# Auto-dispatch (5-tier priority):
#   Triton > PyTorch CUDA > Numba CUDA > im2col+BLAS > NumPy
print(available_backends())
# ['triton', 'torch_cuda', 'numba_cuda', 'numpy']

x = vp.randn(1024, 1024)
y = vp.randn(1024, 1024)
z = x @ y                    # dispatches to fastest available backend

# Explicit backend override
with vp.backend.use('numpy'):
    z_cpu = x @ y
Tier 1
Triton
Fused CUDA kernels (softmax, layernorm, cross-entropy)
Tier 2
PyTorch CUDA
Full GPU via cuBLAS / cuDNN
Tier 3
Numba CUDA
Custom GPU kernels (tiled matmul, conv, attention)
Tier 4
im2col+BLAS
CPU BLAS via NumPy with im2col convolution
Tier 5
NumPy
Pure Python fallback, always available

Neural Networks

vp.nn

Composable layer primitives. All inherit from Module. v0.3.0 adds LR schedulers, einops rearrange/reduce, and forward/backward hooks.

model.pyPython
model = vp.nn.Sequential(
    vp.nn.Linear(784, 512),
    vp.nn.BatchNorm1d(512),
    vp.nn.GELU(),
    vp.nn.Dropout(p=0.3),
    vp.nn.Linear(512, 10),
)

# Einops-style tensor manipulation
from vectorphile.nn.einops import rearrange, reduce, repeat
x = rearrange(x, 'b c h w -> b (c h w)')
x = reduce(x, 'b c h w -> b c', reduction='mean')

# Hooks for feature extraction
from vectorphile.nn.hooks import FeatureExtractor
extractor = FeatureExtractor(model, layer_names=['net.0', 'net.3'])
feats = extractor(input_tensor)

RNN / Attention / Transformer

vp.nn

Full sequence modelling: RNN, LSTM, GRU with bidirectional and multi-layer support; MultiheadAttention; full Encoder-Decoder Transformer with sinusoidal PositionalEncoding.

sequence_models.pyPython
# LSTM (bidirectional)
lstm = vp.nn.LSTM(input_size=64, hidden_size=128, num_layers=2,
                   bidirectional=True, batch_first=True, dropout=0.2)
out, (h, c) = lstm(vp.randn(32, 50, 64))

# Full Transformer
transformer = vp.nn.Transformer(
    d_model=256, nhead=8,
    num_encoder_layers=6, num_decoder_layers=6,
    dim_feedforward=1024, dropout=0.1,
)
out = transformer(src, tgt)

# Advanced attention (from vp.nn.architectures)
from vectorphile.nn.architectures import GroupedQueryAttention, SlidingWindowAttention
gqa = GroupedQueryAttention(d_model=512, num_heads=8, num_kv_heads=2)
swa = SlidingWindowAttention(d_model=512, num_heads=8, window_size=512)

Architecture Zoo

vp.nn.architectures

50+ production-quality DL architectures implemented from scratch. No PyTorch or HuggingFace required.

FamilyModels
CNNLeNet5, VGG (11/13/16/19), ResNet (18/34/50/101/152), DenseNet (121/169/201/264), MobileNetV2, EfficientNet (B0-B7)
State Space ModelS4 (HiPPO-LegT + bilinear ZOH), S4Layer, Mamba (selective scan), MambaBlock
Vision TransformerViT (tiny/small/base/large/huge), DeiT (distillation token, dual-head)
GenerativeVAE (ELBO), DCGANGenerator, DCGANDiscriminator, DCGAN (gradient penalty), DDPM (linear/cosine schedule)
SegmentationUNet, ResUNet (residual blocks), AttentionUNet (gated attention gates)
Graph NNGCN (symmetric normalization), GAT (multi-head attention), GraphSAGE (mean/max aggregation), GIN (WL-test)
Transformer / LLMGPT2, BERT, BERTForMLM, T5, LLaMA (SwiGLU + RoPE), Mistral (sliding window + GQA)
Attention VariantsRotaryEmbedding (RoPE), ALiBi, GroupedQueryAttention, MultiQueryAttention, SlidingWindowAttention, FlashAttention

CNNs

vp.nn.architectures.cnn

Complete CNN zoo with BasicBlock, Bottleneck, MBConv, SEBlock, InvertedResidual, DenseBlock, ConvBNReLU helpers.

cnn_architectures.pyPython
from vectorphile.nn.architectures import ResNet, EfficientNet, DenseNet, VGG, MobileNetV2

# ResNet family (18/34/50/101/152)
model = ResNet.resnet50(num_classes=1000)
model = ResNet.resnet18(num_classes=10)

# EfficientNet family (B0-B7)
model = EfficientNet.efficientnet_b0(num_classes=1000)
model = EfficientNet.efficientnet_b7(num_classes=1000)

# DenseNet family (121/169/201/264)
model = DenseNet.densenet121(num_classes=1000)

# VGG family (11/13/16/19, with/without BN)
model = VGG.vgg16_bn(num_classes=1000)

# MobileNetV2
model = MobileNetV2(num_classes=1000, width_mult=1.0)

out = model(vp.randn(4, 3, 224, 224))   # (4, 1000)

State Space Models (S4 / Mamba)

vp.nn.architectures.ssm

S4 uses HiPPO-LegT initialization, bilinear ZOH discretization, and FFT-based convolution for O(L log L) sequence modelling. Mamba adds selectivity via input-dependent delta/B/C projections and hardware-aware sequential scan.

ssm_architectures.pyPython
from vectorphile.nn.architectures import S4, Mamba

# S4 — HiPPO-LegT matrix, bilinear ZOH, FFT convolution
model = S4(d_model=256, n_layers=6, d_state=64, dropout=0.1)
out = model(vp.randn(2, 1024, 256))    # (batch, seq, d_model)

# Mamba — selective scan with input-dependent dt/B/C projections
model = Mamba(d_model=256, n_layers=12, d_state=16, d_conv=4, expand=2)
out = model(vp.randn(2, 2048, 256))

Vision Transformers (ViT / DeiT)

vp.nn.architectures.vision_transformer

ViT with patch embedding, CLS token, learnable positional encoding, and all official size configs. DeiT adds a distillation token and dual classification heads for teacher-student training.

vit_architectures.pyPython
from vectorphile.nn.architectures import ViT, DeiT

# Vision Transformer — tiny/small/base/large/huge
model = ViT.from_config('base',   image_size=224, patch_size=16, num_classes=1000)
model = ViT.from_config('tiny',   image_size=224, patch_size=16, num_classes=100)

# DeiT — distillation token, dual heads
model = DeiT.from_config('small', image_size=224, patch_size=16, num_classes=1000)
img = vp.randn(4, 3, 224, 224)
logits, dist_logits = model(img, training=True)
logits              = model(img, training=False)

Generative Models (VAE / DCGAN / DDPM)

vp.nn.architectures.generative

VAE with ELBO loss and reparameterization trick. DCGAN with optional gradient penalty (WGAN-GP style). DDPM with linear and cosine noise schedules, epsilon-prediction, and ancestral sampling.

generative_architectures.pyPython
from vectorphile.nn.architectures import VAE, DCGAN, DDPM

# VAE — ELBO loss = recon + KL divergence
vae = VAE(input_dim=784, latent_dim=32, hidden_dim=512)
x_hat, mu, logvar = vae(x)
loss = vae.elbo_loss(x, x_hat, mu, logvar)
samples = vae.sample(n=16)

# DCGAN — with optional gradient penalty (WGAN-GP)
gan = DCGAN(latent_dim=100, image_size=64, channels=3)
real_loss, fake_loss = gan.discriminator_loss(real_imgs, fake_imgs)
gen_loss = gan.generator_loss(fake_imgs)

# DDPM — linear/cosine noise schedule + epsilon-prediction
ddpm = DDPM(image_size=32, channels=3, timesteps=1000, schedule='cosine')
noise_loss = ddpm(x_0)
samples = ddpm.sample(n=16, steps=50)

U-Net (Segmentation)

vp.nn.architectures.unet

Standard U-Net encoder-decoder with skip connections, ResU-Net with residual blocks in each stage, and AttentionU-Net with learnable gated attention at every skip connection.

unet_architectures.pyPython
from vectorphile.nn.architectures import UNet, ResUNet, AttentionUNet

# Standard U-Net (encoder-decoder with skip connections)
model = UNet(in_channels=1, out_channels=2, features=[64, 128, 256, 512])

# ResU-Net (residual blocks in encoder/decoder)
model = ResUNet(in_channels=3, out_channels=1, features=[32, 64, 128, 256])

# Attention U-Net (gated attention at each skip connection)
model = AttentionUNet(in_channels=1, out_channels=2, features=[64, 128, 256, 512])

mask = model(vp.randn(4, 1, 256, 256))   # (4, out_channels, 256, 256)

Graph Neural Networks

vp.nn.architectures.graph

GCN with symmetric normalization. GAT with multi-head attention and masking. GraphSAGE with mean/max aggregation for inductive learning. GIN achieves the theoretical WL-test expressiveness upper bound.

gnn_architectures.pyPython
from vectorphile.nn.architectures import GCN, GAT, GraphSAGE, GIN

# GCN — symmetric normalization
model = GCN(in_features=16, hidden_features=64, out_features=7, num_layers=3)
out = model(node_feats, adj_matrix)

# GAT — multi-head attention
model = GAT(in_features=16, hidden_features=64, out_features=7,
            num_heads=8, num_layers=3, dropout=0.6)

# GraphSAGE — inductive (mean/max aggregation)
model = GraphSAGE(in_features=16, hidden_features=64, out_features=7,
                  aggregation='mean', num_layers=3)

# GIN — WL-test expressive power
model = GIN(in_features=16, hidden_features=64, out_features=2, num_layers=5)

Functional API & Initializers

vp.nn.F · vp.nn.init

vp.nn.F exposes 50+ operations as stateless functions.vp.nn.init provides all standard weight initialization schemes.

xavier_uniform_xavier_normal_kaiming_uniform_kaiming_normal_orthogonal_sparse_trunc_normal_dirac_

Optimizers & Schedulers

vp.optim · vp.nn.lr_schedulers

All classic optimizers plus 10 LR schedulers including WarmupCosineDecay and OneCycleLR.

optim.pyPython
opt_sgd   = vp.optim.SGD(model.parameters(), lr=0.01, momentum=0.9, nesterov=True)
opt_adam  = vp.optim.Adam(model.parameters(), lr=1e-3)
opt_adamw = vp.optim.AdamW(model.parameters(), lr=1e-3, weight_decay=1e-2)

sched = vp.nn.lr_schedulers.WarmupCosineDecay(opt_adamw,
    warmup_steps=500, total_steps=10000, min_lr=1e-6)

sched = vp.nn.lr_schedulers.get_scheduler('cosine_with_warmup',
    optimizer=opt_adamw, num_warmup_steps=500, num_training_steps=10000)

Advanced Optimizers

vp.nn.optimizers_advanced

5 state-of-the-art optimizers from recent research: Lion (sign momentum), Sophia (second-order), Adan (Nesterov momentum), Prodigy (automatic LR), and ScheduleFreeAdamW.

advanced_optimizers.pyPython
from vectorphile.nn.optimizers_advanced import (
    Lion, Sophia, Adan, Prodigy, ScheduleFreeAdamW
)

# Lion — sign momentum (~3x memory savings vs Adam)
opt = Lion(model.parameters(), lr=1e-4, weight_decay=1e-2)

# Sophia — second-order with Hutchinson Hessian estimator
opt = Sophia(model.parameters(), lr=2e-4, rho=0.04)

# Adan — adaptive Nesterov momentum
opt = Adan(model.parameters(), lr=1e-3, betas=(0.98, 0.92, 0.99))

# Prodigy — automatic learning rate (D-Adaptation)
opt = Prodigy(model.parameters(), lr=1.0)

# ScheduleFreeAdamW — no LR scheduler needed
opt = ScheduleFreeAdamW(model.parameters(), lr=5e-4)
opt.train()   # switch modes before train/eval
opt.eval()

Flash Attention

vp.nn.flash_attention

Tiled attention with O(N) memory — no full N×N attention matrix is ever materialized. Supports causal masking for autoregressive models.

flash_attention.pyPython
from vectorphile.nn.flash_attention import FlashAttention

# O(N) memory tiled attention (no full NxN matrix stored)
attn = FlashAttention(
    head_dim=64, num_heads=8,
    block_size=64,
    dropout=0.0,
    causal=True,       # causal mask for autoregressive models
)
out = attn(q, k, v)   # q/k/v: (batch, heads, seq, head_dim)

Quantization-Aware Training

vp.nn.qat

Simulate quantization during training using the straight-through estimator. Produces INT8 models that are more accurate than post-training quantization.

qat.pyPython
from vectorphile.nn.qat import prepare_qat, convert_qat

# 1. Insert FakeQuantize ops
model_qat = prepare_qat(model,
    weight_bits=8, activation_bits=8,
    per_channel=True, symmetric=True)

# 2. Fine-tune with fake quantization active
for epoch in range(5):
    for x, y in loader:
        loss = criterion(model_qat(x), y)
        loss.backward()
        optimizer.step()
        optimizer.zero_grad()

# 3. Freeze observers after epoch 3
model_qat.apply(lambda m: m.observer_enabled.fill_(0))

# 4. Convert to real quantized weights
model_int8 = convert_qat(model_qat)

DataLoader & Datasets

vp.utils.data · vp.data.datasets

v0.3.0 adds built-in datasets and advanced sampler support.

datasets.pyPython
from vectorphile.utils.data import DataLoader, WeightedRandomSampler
from vectorphile.data.datasets import MNIST, CIFAR10, CIFAR100, TinyShakespeare, IMDb, random_split

train_ds = MNIST(root='./data', train=True, transform=transform)
sampler  = WeightedRandomSampler(weights=class_weights, num_samples=len(train_ds))
loader   = DataLoader(train_ds, batch_size=64, sampler=sampler)

train_ds, val_ds = random_split(train_ds, lengths=[0.9, 0.1])

ds = TinyShakespeare(root='./data', seq_len=256)
ds = IMDb(root='./data', split='train')

Trainer

vp.trainer

PyTorch Lightning-style training loop. Override training_step and validation_step. Everything else is automatic.

trainer.pyPython
from vectorphile.trainer import Trainer, TrainableModel
from vectorphile.callbacks import EarlyStopping, ModelCheckpoint, WandBLogger

class MyModel(TrainableModel):
    def __init__(self):
        super().__init__()
        self.net = vp.nn.Linear(784, 10)

    def forward(self, x):
        return self.net(x)

    def training_step(self, batch):
        x, y = batch
        return vp.nn.CrossEntropyLoss()(self(x), y)

    def validation_step(self, batch):
        x, y = batch
        loss = vp.nn.CrossEntropyLoss()(self(x), y)
        acc  = (self(x).argmax(-1) == y).float().mean()
        return {'val_loss': loss, 'val_acc': acc}

trainer = Trainer(
    max_epochs=50,
    optimizer='adamw', lr=1e-3, weight_decay=1e-2,
    gradient_clip_val=1.0,
    callbacks=[
        EarlyStopping(monitor='val_loss', patience=5),
        ModelCheckpoint(dirpath='./ckpts', monitor='val_acc', mode='max'),
        WandBLogger(project='my-project'),
    ],
    accelerator='auto',
    precision='fp16',
)
trainer.fit(MyModel(), train_loader, val_loader)

Callbacks

vp.callbacks
EarlyStoppingStop training when metric stops improving
ModelCheckpointSave top-k best checkpoints by metric
ReduceLROnPlateauReduce LR when metric plateaus
TensorBoardLoggerLog metrics to TensorBoard
WandBLoggerLog metrics to Weights & Biases
CSVLoggerLog metrics to a CSV file
ProgressBartqdm-style training progress
GradientMonitorTrack gradient norms per layer
LRFinderAutomatic learning rate range test

Distributed Training

vp.distributed
distributed.pyPython
from vectorphile.distributed import DataParallel, DDP, MixedPrecisionTrainer, launch

# DataParallel (single-node multi-GPU)
model = DataParallel(model, device_ids=[0, 1, 2, 3])

# DistributedDataParallel (multi-node)
model = DDP(model, device_id=local_rank)

# Gradient accumulation (simulate larger batch)
from vectorphile.distributed import GradientAccumulator
accum = GradientAccumulator(optimizer, accumulate_steps=4)
for batch in loader:
    loss = model(batch)
    accum.backward(loss)
accum.step()

# Mixed precision training
mp_trainer = MixedPrecisionTrainer(model, optimizer, dtype='fp16')
with mp_trainer.autocast():
    loss = model(batch)
mp_trainer.backward(loss)
mp_trainer.step()

Metrics

vp.metrics

All metrics implemented from scratch. No scikit-learn or NLTK required.

metrics.pyPython
from vectorphile.metrics import (
    accuracy, f1_score, auc_roc,
    bleu_score, rouge_score, word_error_rate, perplexity,
    ndcg, expected_calibration_error,
)

acc  = accuracy(y_pred, y_true)
f1   = f1_score(y_pred, y_true, average='macro')
auc  = auc_roc(y_scores, y_true)

bleu = bleu_score(hypotheses, references)
r    = rouge_score(hypothesis, reference)
wer  = word_error_rate(hypothesis, reference)
ppl  = perplexity(model, tokenized_text)

ece  = expected_calibration_error(probs, y_true, n_bins=15)

Tokenizers

vp.tokenizers

Full tokenizer suite from scratch — BPE (GPT-2 style), WordPiece (BERT style), SentencePiece (T5/LLaMA style). All support train, encode, decode, save and load.

tokenizers.pyPython
from vectorphile.tokenizers import BytePairEncoding, WordPieceTokenizer, SentencePieceTokenizer

# BPE (GPT-2 style)
bpe = BytePairEncoding(vocab_size=50257)
bpe.train(corpus_texts)
tokens = bpe.encode("Hello, world!")
text   = bpe.decode(tokens)
bpe.save('bpe_tokenizer.json')

# WordPiece (BERT style)
wp = WordPieceTokenizer(vocab_size=30522)
wp.train(corpus_texts)

# SentencePiece (T5/LLaMA style)
sp = SentencePieceTokenizer(vocab_size=32000, model_type='bpe')
sp.train(corpus_texts)

Text Generation

vp.text

Complete text generation toolkit: greedy, beam search with n-gram blocking, nucleus sampling, contrastive search, speculative decoding for fast inference, and chat template formatting.

text_generation.pyPython
from vectorphile.text import TextGenerationPipeline, beam_search, nucleus_sampling

# High-level pipeline
pipe = TextGenerationPipeline(model=llm, tokenizer=tok)
out = pipe("Once upon a time", max_new_tokens=200, strategy='beam_search', num_beams=5)

# Beam search with n-gram blocking
ids = beam_search(model, input_ids, num_beams=5, max_new_tokens=100,
                  no_repeat_ngram_size=3)

# Nucleus (top-p) sampling
ids = nucleus_sampling(model, input_ids, top_p=0.95, temperature=0.8,
                        max_new_tokens=200)

# Speculative decoding (fast inference with draft model)
from vectorphile.text import speculative_decoding
ids = speculative_decoding(target_model=llm, draft_model=small_llm,
                            input_ids=input_ids, gamma=5)

Vision Transforms

vp.vision
vision_transforms.pyPython
from vectorphile.vision import (
    Compose, Resize, RandomCrop, ColorJitter, AugMix,
    ToTensor, Normalize, RandomResizedCrop, make_grid, extract_features,
)

transform = Compose([
    RandomResizedCrop(size=224, scale=(0.08, 1.0)),
    ColorJitter(brightness=0.4, contrast=0.4, saturation=0.2, hue=0.1),
    AugMix(severity=3, mixture_width=3),
    ToTensor(),
    Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

img_tensor = transform(pil_image)

# Feature extraction from named layers
feats = extract_features(model=resnet, images=img_batch,
                          layer_names=['layer3', 'layer4'])

Pretrained Hub

vp.pretrained

Load pretrained weights for 20+ popular models. Compatible with HuggingFace Hub. Supports safetensors and pytorch .bin formats via a pure-Python urllib downloader.

pretrained.pyPython
from vectorphile import pretrained

pretrained.list_models()
# ['gpt2', 'gpt2-medium', 'bert-base-uncased', 'bert-large-uncased',
#  't5-small', 't5-base', 'vit-base-patch16-224', 'llama-2-7b', ...]

model = pretrained.load('gpt2')
model = pretrained.load('bert-base-uncased')
model = pretrained.load('vit-base-patch16-224')

info = pretrained.cache_info()
print(f"Cached: {info['num_models']} models, {info['total_size_mb']:.0f} MB")

Reinforcement Learning

vp.rl

Complete RL suite: value-based (DQN, Double DQN, Dueling DQN), policy gradient (REINFORCE, A2C, PPO), actor-critic (SAC, TD3). Includes built-in environments and prioritized replay buffers.

rl_algorithms.pyPython
from vectorphile.rl import PPO, SAC, DQN, REINFORCE, A2C, TD3
from vectorphile.rl import CartPoleEnv, MountainCarEnv, PendulumEnv

# PPO — Proximal Policy Optimization
env   = CartPoleEnv()
agent = PPO(obs_dim=4, act_dim=2, hidden_dim=64, clip_eps=0.2)
agent.train(env, total_steps=500_000)

# SAC — Soft Actor-Critic (continuous actions, automatic entropy)
env   = PendulumEnv()
agent = SAC(obs_dim=3, act_dim=1, hidden_dim=256, automatic_entropy=True)

# DQN / Double DQN / Dueling DQN
from vectorphile.rl import DoubleDQN
agent = DoubleDQN(obs_dim=4, act_dim=2, hidden_dim=128)

# TD3 — Twin Delayed DDPG
agent = TD3(obs_dim=11, act_dim=3, max_act=1.0)

ONNX Export & Runtime

vp.export
export_onnx.pyPython
from vectorphile.export import to_onnx, ONNXRuntime, benchmark

to_onnx(model, dummy_input=vp.randn(1, 3, 224, 224),
        output_path='model.onnx', opset=17)

rt  = ONNXRuntime('model.onnx')
out = rt(input_array)

result = benchmark(model, rt, input_shape=(1, 3, 224, 224), n_runs=100)
print(result)  # {'vp_ms': 4.2, 'onnx_ms': 1.8, 'speedup': 2.3}

GGUF Format

vp.export

Read and write GGUF files (llama.cpp binary format). Supports Q4_0, Q8_0, F16, and F32 precision. Export any LLM to GGUF for use with llama.cpp, LM Studio, Ollama, etc.

gguf_export.pyPython
from vectorphile.export import to_gguf, from_gguf

# Export LLM to GGUF (llama.cpp compatible)
to_gguf(model, tokenizer, output_path='model.gguf',
        quantization='Q4_0')   # Q4_0 | Q8_0 | F16 | F32

# Load GGUF for inference
model, metadata = from_gguf('model.gguf')
print(metadata['general.architecture'])

Quantization (PTQ)

vp.quantization

Post-training quantization for inference efficiency. INT8, INT4 (group quantization), and FP16.

quantization.pyPython
from vectorphile import quantization as Q

q_model = Q.quantize(model, scheme='int8', symmetric=True)
q_model_4 = Q.quantize(model, scheme='int4', group_size=128)
print(f"Compression: {q_model_4.compression_ratio(model):.1f}x")

masks = Q.magnitude_prune(model, sparsity=0.5)
Q.apply_masks(model, masks)

Classical ML

vp.ml

60+ estimators across every classical ML category.

classical_ml.pyPython
from vectorphile import ml

X_tr, X_te, y_tr, y_te = ml.train_test_split(X, y, test_size=0.2, stratify=y)
scaler = ml.StandardScaler()
X_tr   = scaler.fit_transform(X_tr)

clf    = ml.RandomForestClassifier(n_estimators=200)
clf.fit(X_tr, y_tr)

scores = ml.cross_val_score(clf, X, y, cv=5, scoring='f1_weighted')
grid   = ml.GridSearchCV(ml.SVC(kernel='rbf'), param_grid={'C': [0.1, 1, 10]}, cv=5)

Gradient Boosting

vp.ml
boosting.pyPython
lgbm = ml.LGBMClassifier(n_estimators=300, learning_rate=0.05,
    num_leaves=63, early_stopping_rounds=20)
lgbm.fit(X_tr, y_tr, eval_set=(X_val, y_val))

cat  = ml.CatBoostClassifier(n_estimators=300, cat_features=[0, 2, 5])
cat.fit(X_tr, y_tr)

Manifold Learning

vp.ml
manifold.pyPython
tsne = ml.TSNE(n_components=2, perplexity=30)
emb  = tsne.fit_transform(X)

umap = ml.UMAP(n_components=2, n_neighbors=15, min_dist=0.1)
emb  = umap.fit_transform(X)

Pipelines & Feature Selection

vp.ml
pipeline.pyPython
pipe = ml.Pipeline([
    ('scaler', ml.StandardScaler()),
    ('pca',    ml.PCA(n_components=50)),
    ('clf',    ml.RandomForestClassifier(n_estimators=100)),
])
pipe.fit(X_train, y_train)
rfe = ml.RFECV(ml.LogisticRegression(), cv=5, scoring='accuracy')
rfe.fit(X_train, y_train)

DataFrames

vp.data
dataframe.pyPython
df = vp.read_csv('dataset.csv')
df['margin'] = (df['revenue'] - df['cost']) / df['revenue']
monthly = df.groupby(['year', 'month']).agg({'revenue': 'sum'})
result  = df.merge(metadata, on='product_id', how='left')

Linear Algebra

vp.linalg
linalg.pyPython
A = vp.randn(50, 50)
U, S, Vt   = vp.linalg.svd(A)
vals, vecs = vp.linalg.eig(A)
x          = vp.linalg.solve(A.T @ A, b)
print(vp.linalg.det(A), vp.linalg.norm(A, ord='fro'))

Statistics

vp.stats
stats.pyPython
from vectorphile import stats
r_p, p_p = stats.pearsonr(x, y)
t, p      = stats.ttest_ind(group_a, group_b, equal_var=False)
chi2, p, dof, expected = stats.chi2_contingency(table)
z         = stats.zscore(x)

Signal Processing

vp.fft · vp.signal
signal.pyPython
from vectorphile import fft, signal

X     = fft.fft(x)
freqs = fft.fftfreq(len(x), d=1/fs)

b, a     = signal.butter(order=4, cutoff=0.1, btype='low')
filtered = signal.filtfilt(b, a, noisy)
f, t, Sxx = signal.spectrogram(x, fs=fs)

File I/O

vp.io
NumPy .npy/.npz
save, load, savez, savez_compressed, savetxt, loadtxt
Parquet / Feather
read_parquet, write_parquet, read_feather, write_feather
HDF5
read_hdf, write_hdf
MATLAB .mat
loadmat, savemat
WAV audio
wavfile_read, wavfile_write
Matrix Market
mmread, mmwrite, mminfo
NetCDF4
read_netcdf, write_netcdf
Arrow CSV
read_csv_arrow

Visualization

vp.viz

Matplotlib-backed, theme-aware plotting. Three themes (dark, light, paper) and five palettes. Includes ML-specific charts for confusion matrices, ROC curves, training history, and feature importances.

plotscatterbarhistogramareaheatmapboxplotviolinplotkdeplotpairplotconfusion_matrix_plotroc_curve_plottraining_historyfeature_importance_plotresiduals_plot

Hub & Checkpointing

vp.hub
hub.pyPython
from vectorphile import hub

hub.save_model(model, name='my_model', version='1.0.0',
               metadata={'accuracy': 0.943})
model = hub.load_model('my_model', version='1.0.0')

ckpt = hub.Checkpointer('./ckpts', monitor='val_loss', mode='min', save_top_k=3)
ckpt(model, epoch=5, metrics={'val_loss': 0.12})
best = ckpt.load_best()

Architecture

41 modules. Flat, explicit, no hidden dispatch tables outside vp.backend.

directory structure (v0.3.0)Python
vectorphile/
+-- __init__.py               <- top-level API (v0.3.0, 41 modules)
+-- core/                     <- Tensor, ops
+-- autograd/                 <- reverse-mode autodiff, jacobian, hessian, vmap
+-- backend/                  <- 5-tier GPU dispatch (Triton > torch > numba > blas > numpy)
+-- cuda/                     <- Numba CUDA kernels + Triton fused kernels
+-- nn/                       <- layers, activations, losses, attention, norm
|   +-- architectures/        <- CNN, SSM, ViT, Generative, U-Net, GNN, LLMs
|   +-- flash_attention.py    <- O(N) tiled attention
|   +-- qat.py                <- quantization-aware training
|   +-- optimizers_advanced.py<- Lion, Sophia, Adan, Prodigy, ScheduleFreeAdamW
|   +-- lr_schedulers.py      <- 10 LR schedulers
|   +-- einops.py             <- rearrange, reduce, repeat, pack, unpack
|   +-- hooks.py              <- forward/backward hooks, FeatureExtractor
|   +-- functional.py         <- 50+ stateless ops
|   +-- init.py               <- weight initialization
+-- optim/                    <- SGD, Adam, AdamW, RMSprop, Adagrad
+-- transforms/               <- grad, vmap, jit, jacobian, hessian
+-- tokenizers/               <- BPE, WordPiece, SentencePiece (all from scratch)
+-- text/                     <- beam_search, nucleus_sampling, speculative_decoding
+-- vision/                   <- Compose, RandomCrop, ColorJitter, AugMix, make_grid
+-- pretrained/               <- load(), HF-compatible, safetensors + .bin
+-- trainer/                  <- Lightning-style Trainer, TrainableModel
+-- callbacks/                <- EarlyStopping, ModelCheckpoint, Loggers
+-- distributed/              <- DataParallel, DDP, GradientAccumulator, MixedPrecision
+-- metrics/                  <- accuracy, F1, AUC, BLEU, ROUGE, WER, perplexity
+-- rl/                       <- DQN, PPO, SAC, TD3, REINFORCE, A2C + envs
+-- export/                   <- to_onnx, ONNXRuntime + GGUFReader/Writer
+-- ml/                       <- 60+ ML estimators
+-- data/                     <- DataFrame, Series, GroupBy + datasets (MNIST, CIFAR10...)
+-- linalg/, stats/, random/, fft/
+-- signal/, sparse/, optimize/, amp/
+-- integrate/, interpolate/, spatial/
+-- ndimage/, polynomial/, ma/
+-- io/, viz/, hub/, quantization/
+-- utils/data/               <- DataLoader, WeightedRandomSampler
Phase 1 — current
Pure Python + NumPy/SciPy backend. 5-tier GPU dispatch with optional Triton/Numba/CuPy. 41 modules, 50+ architectures.
Phase 2 — C + CUDA
C extensions for core tensor ops. cuBLAS, cuDNN, cuFFT, real fp16/bf16 AMP. jit compiles to native code.