Parallel-MetaLearn: A Functional, Vectorized Meta-Learning Framework in PyTorch
Overview
Parallel-MetaLearn is a modular PyTorch framework designed for gradient-based and metric-based meta-learning research. By leveraging the functional transformation primitives of torch.func (specifically vmap, grad, and functional_call), the framework parallelizes task-level inner adaptation loops across the meta-batch dimension.
Standard meta-learning implementations typically iterate sequentially over tasks within a meta-batch using explicit Python loops, causing suboptimal GPU utilization, or require rewriting model architectures into non-standard functional forms. Parallel-MetaLearn preserves standard object-oriented PyTorch nn.Module definitions while vectorizing inner-loop optimization paths via stateless execution.
Key Methodological Features
- Task-Level Vectorization (
torch.func.vmap): Inner adaptation steps across independent tasks within an episode are evaluated in parallel, significantly reducing dispatch overhead. - Standard
nn.ModuleCompatibility: Model definitions use standard PyTorch layers without manual functional parameter passing inforward(). - Stateful Buffer Tracking: Supports per-step running statistics (e.g., in
BatchNorm) and prototype tracking across both first-order and second-order derivative passes. - Support for Task Imbalance & Dynamic Masking: Includes a masking and padding engine allowing variable support/query shot allocations per episode without violating vectorization constraints.
- Ghost Graph Suppression: Incorporates early weight detachment and explicit graph truncation in first-order modes (e.g., FOMAML, Reptile) and evaluation routines to prevent memory leakage.
- Modular Extensibility: Clean decoupling between data sampling, model wrappers, inner optimizers, and loss modules.
⚠️ Computational Trade-offs: VRAM Consumption & Chunk Size
While vectorizing task execution via vmap provides theoretical and wall-clock speedups, it alters the memory scaling profile:
$$\text{Memory Overhead} \propto B_{\text{meta}} \times N_{\text{inner_steps}} \times \text{Activation Size}$$
-
Second-Order Derivatives & Activation Footprint:
In higher-order optimization (e.g., Full MAML, ProtoMAML), computation graphs across all inner adaptation steps for all parallel tasks must reside in VRAM simultaneously. On consumer GPUs with limited VRAM, large meta-batch sizes can quickly lead to Out-Of-Memory (OOM) errors. -
Chunked Gradient Accumulation (
chunk_size):
To mitigate memory pressure,Parallel-MetaLearnimplements chunked task processing (chunk_size).- When
chunk_sizeequals the meta-batch size, full vectorization is achieved. - If VRAM is constrained, decreasing
chunk_sizedivides the meta-batch into smaller sub-batches and accumulates gradients sequentially. - Note: In extreme scenarios where
chunk_size = 1, memory usage drops to its minimum, but runtime performance converges to standard sequential iteration. Researchers should tunechunk_sizeto balance available hardware memory against parallelism throughput.
- When
Installation
From PyPI
pip install parallel-metalearn
For Local Development
git clone [https://github.com/your-username/parallel-metalearn.git](https://github.com/your-username/parallel-metalearn.git)
cd parallel-metalearn
pip install -e .
Supported Algorithms
| Algorithm | Paradigm | Derivative Order | Domain Motivation & Origin | Key Reference |
|---|---|---|---|---|
| MAML | Gradient-based | 1st & 2nd Order | General Meta-Learning | Finn et al. (2017) |
| FOMAML | Gradient-based | 1st Order | General Meta-Learning | Finn et al. (2017) |
| ANIL | Representation-based | 1st & 2nd Order | General Meta-Learning | Raghu et al. (2019) |
| BOIL | Body-Only Inner Loop | 1st & 2nd Order | General Meta-Learning | Oh et al. (2020) |
| Meta-SGD | Learnable Step Sizes | 1st & 2nd Order | General Meta-Learning | Li et al. (2017) |
| MAML++ | Multi-Step Loss & MSL | 1st & 2nd Order | General Meta-Learning | Antoniou et al. (2019) |
| ProtoMAML (v1 & v2) | Metric + Gradient Hybrid | 1st & 2nd Order | General Few-Shot Learning | Triantafillou et al. (2019) |
| Prototypical Networks | Metric-based | Non-parametric | General Metric Learning | Snell et al. (2017) |
| Reptile | First-order Directional | 1st Order | General Meta-Learning | Nichol et al. (2018) |
| TAGML | Task-Unbiased Gradient-based | 1st & 2nd Order | Mechanical Signal Diagnosis | Yang et al. (2023) |
| PTFM | Time-Frequency Metric-based | Non-parametric | Rotational Machinery Dynamics | Wang et al. (2025) |
Note on Cross-Domain Generality:
While TAGML and PTFM were originally introduced and evaluated in the mechanical engineering literature (specifically for vibration-based few-shot fault diagnosis in bearing and transmission systems), their algorithmic formulations are implemented in a strictly domain-agnostic manner within this framework. Specifically, the Task-Agnostic Regularization (entropy-reduction penalty) of TAGML and the dual-branch Time-Frequency mixing mechanism of PTFM are decoupled from specific sensor physical setups, allowing them to serve as generic meta-optimizers and backbone pipelines for arbitrary sequence and multi-modal few-shot tasks.
Minimal Working Example
Below is a standard workflow demonstrating model initialization, loss configuration, and meta-training:
import torch
from metalearn.model_wrappers import MAML_Model
from metalearn.loss import LabelEncoder, CrossEntropy, CategoricalAccuracy
from metalearn.inner_optimizers import InnerSGD
from metalearn.algorithms import MAML
from metalearn.train import MetaTrain
# 1. Standard PyTorch architecture definition
backbone = MyFeatureExtractor()
head = MyLinearClassifier()
model = MAML_Model(backbone=backbone, head=head)
# 2. Label encoding and loss setup
label_encoder = LabelEncoder(num_classes=10, max_n_way=3, shuffle=True)
loss_fn = CrossEntropy(metric_fn=CategoricalAccuracy())
# 3. Optimization setup
inner_optimizer = InnerSGD(
initial_fast_weights=model.get_fast_weights(),
inner_lr=0.01,
first_order=False
)
outer_optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
# 4. Meta-Learner initialization
algorithm = MAML(
model=model,
optimizer=outer_optimizer,
inner_optimizer=inner_optimizer,
support_loss_fn=loss_fn,
inner_steps=3,
chunk_size=8, # Balances VRAM overhead and vectorization speed
)
# 5. Training execution
trainer = MetaTrain(
TrainLoader=train_loader,
ValLoader=val_loader,
algorithm=algorithm
)
history, best_metric, best_loss = trainer.train(
epochs=100,
check_idx=10,
log_checkpoint_path="checkpoints"
)
Research Applications & Extensions
The framework is decoupled via standardized input/output mappings (out_dict, targets), allowing straightforward application to various meta-learning paradigms:
- Multi-Task Meta-Learning (MTL): Extend
targetsto return multiple supervisory signals and define composite objectives inBaseLoss. - Domain Generalization & Shift: Implement alignment objectives (e.g., MMD, Wasserstein loss) using features extracted from
out_dict["features"]. - Simulated Federated Meta-Learning: Utilize
vmapto execute localized client updates concurrently before applying server aggregation rules (e.g., FedAvg). - Zero-Shot to Few-Shot Transition: Models automatically switch from metric-based zero-shot priors to few-shot gradient adaptation depending on support set availability.
📊 Scaling Analysis: Vectorized (vmap) vs. Sequential (for-loop) Execution
To evaluate the empirical speedup and scaling profile of functional task vectorization, MAML was benchmarked across a wide spectrum of meta-batch sizes ($B_{\text{meta}} \in [1, 200]$) under identical architectural, loss, and optimization constraints. Each configuration was evaluated over 10 full meta-training epochs to compute the average execution latency per epoch.
| Meta-Batch Size (Tasks) | Sequential for-loop (ms/epoch) |
Vectorized vmap (ms/epoch) |
Speedup Factor |
|---|---|---|---|
| 1 | 79.43 ms | 260.29 ms | 0.31x |
| 2 | 241.40 ms | 226.36 ms | 1.07x |
| 3 | 86.57 ms | 104.86 ms | 0.83x |
| 5 | 143.71 ms | 75.97 ms | 1.89x |
| 10 | 253.73 ms | 79.87 ms | 3.18x |
| 20 | 594.35 ms | 113.24 ms | 5.25x |
| 30 | 760.44 ms | 149.40 ms | 5.09x |
| 40 | 1063.92 ms | 224.18 ms | 4.75x |
| 50 | 1555.70 ms | 232.85 ms | 6.68x |
| 70 | 1863.73 ms | 305.81 ms | 6.09x |
| 100 | 2920.59 ms | 386.20 ms | 7.56x |
| 120 | 3259.52 ms | 447.91 ms | 7.28x |
| 200 | 5490.08 ms | 791.53 ms | 6.94x |
🔍 Performance & Hardware Bottleneck Analysis
-
Vectorization Overhead at Small Batches ($B_{\text{meta}} \le 3$):
For very small task counts, the initial compilation and dispatch overhead oftorch.funcfunctional transformations dominates, resulting in lower throughput than native sequential iteration. -
Sub-linear Scaling & Core Occupancy ($B_{\text{meta}} = 5 \to 100$):
As the number of concurrent tasks increases,torch.func.vmapmaximizes Streaming Multiprocessor (SM) occupancy on the GPU. While the sequential execution latency grows strictly linearly ($\mathcal{O}(N)$), the vectorized pipeline scales sub-linearly, reaching a peak acceleration of $\approx 7.56\times$ at 100 tasks. -
Speedup Saturation & Amdahl's Law ($B_{\text{meta}} > 100$):
The empirical speedup plateaus between $7\times$ and $7.5\times$ rather than scaling indefinitely. This saturation is governed by fundamental hardware constraints:- Compute & Memory Bandwidth Saturation: Once GPU CUDA cores reach full occupancy, additional tasks are queued by the hardware warp scheduler rather than executed with true instantaneous concurrency. Additionally, tracking multiple computation graphs under second-order derivatives shifts the bottleneck from compute throughput to GPU memory bandwidth.
- Amdahl's Law: Non-vectorizable sequential operations (e.g., CPU data batching, host-to-device memory copies, outer-loop global parameter reduction, and outer optimizer updates) place an asymptotic upper bound on theoretical end-to-end acceleration.
-
Hardware Context & Colab Constraints:
💡 Benchmark Hardware Note:
These benchmarks were conducted on a standard free-tier Google Colab instance (NVIDIA Tesla T4 GPU with ~15 GB VRAM). In this virtualized environment, physical GPU compute units and memory bandwidth are shared across multiple concurrent user sessions (typically allocating only a fraction of total hardware throughput to each runtime). On dedicated research-grade hardware (e.g., NVIDIA A100/H100 GPUs with high-bandwidth HBM3 memory), higher saturation thresholds and absolute throughput are expected.
⚙️ Execution Backends & Memory Management (vmap vs. sequential)
Parallel-MetaLearn implements a Dual-Backend Execution Engine allowing seamless switching between maximum throughput parallelism and strict $O(1)$ memory-capped sequential iteration without changing the training loop API.
1. Dual-Backend Dispatcher (backend="vmap" vs backend="sequential")
| Feature / Metric | backend="vmap" (Default) |
backend="sequential" |
|---|---|---|
| Primary Goal | Maximum Training Throughput | Zero-OOM Scaling & Massive Models |
| Execution Paradigm | Vectorized batching via torch.func.vmap |
Sequential task iteration via torch.unbind |
| Memory Footprint (VRAM) | $\mathcal{O}(B_{\text{meta}} \times \text{Activations} \times (N_{\text{steps}} \text{ if 2nd-order or multi-step loss else } 1))$ | $\mathcal{O}(1 \times \text{Activations} \times (N_{\text{steps}} \text{ if 2nd-order else } 1))$ |
| Mathematical Accuracy | Exact meta-batch average | Exact meta-batch average (Linear accumulation) |
| Multi-Step Loss (MSL) | Full graph accumulation & batched backward | Immediate per-step backward & instant graph purge |
| Cache Management | Chunked clearing (torch.cuda.empty_cache()) |
Immediate per-task deallocation & cache flush |
chunk_size Parameter |
Active & Essential (Controls sub-batch sizes) | Ignored / Inactive (Task-by-task $O(1)$ execution) |
| Recommended Use Cases | Standard CNNs/MLPs, $B_{\text{meta}} \ge 5$, $N_{\text{steps}} \le 3$ | Large Backbones (ResNet/ViT), $B_{\text{meta}} \le 3$, $N_{\text{steps}} \ge 5$ |
2. When to Use Which Backend?
🚀 Use backend="vmap" When:
- Training lightweight to medium feature extractors (e.g., 1D-CNNs, 4-Conv backbones).
- Meta-batch size is moderate to large ($B_{\text{meta}} \ge 5$).
- Inner adaptation steps are small ($N_{\text{steps}} \in [1, 3]$).
- Hardware has sufficient GPU memory to exploit SM parallelism.
- Tuning
chunk_size: When runningvmap, tunechunk_sizeto fit maximum tasks per sub-batch on your GPU.
🛡️ Use backend="sequential" When:
- Large Neural Architectures: Working with memory-heavy backbones (e.g., ResNet-50, Transformers/ViTs).
- Deep Inner Loops: Executing high adaptation step counts ($N_{\text{steps}} \ge 5, 10, 20$) with Second-Order derivatives without risking GPU OOM.
- Small Meta-Batches ($B_{\text{meta}} \le 3$): Avoiding the initial
vmaptracing overhead when batch sizes are minimal. - Exact Mathematical Equivalency: In
sequentialmode, gradients are mathematically accumulated as: $$\nabla_{\theta} \mathcal{L}{\text{meta}} = \frac{1}{B} \sum{i=1}^{B} \nabla_{\theta} \mathcal{L}_{\text{task}}^{(i)}$$ Each task immediately executes a scaledbackward()and destroys its forward/backward computation graphs and cached activations before processing the next task.
3. Usage Example
# 1. High-throughput Parallel Vectorization (Default)
algorithm_fast = MAML(
model=model,
optimizer=outer_optimizer,
inner_optimizer=inner_optimizer,
support_loss_fn=loss_fn,
inner_steps=3,
backend="vmap", # Vectorized task-level execution
chunk_size=8 # Active: Slices batch into chunks of 8 tasks
)
# 2. Memory-Safe Sequential Execution (For Heavy Models / Deep Steps)
algorithm_safe = MAML(
model=model,
optimizer=outer_optimizer,
inner_optimizer=inner_optimizer,
support_loss_fn=loss_fn,
inner_steps=10, # Deep adaptation without OOM
backend="sequential" # Sequential O(1) memory execution (chunk_size is ignored)
)
Empirical Benchmark (Fault Diagnosis Domain Shift)
To evaluate empirical convergence, algorithms were evaluated on the CWRU Vibration Dataset under strict file-level stratified partitioning (evaluating generalization under domain shift across distinct physical bearing loads).
Setup
- Signal Segmentation: 2-channel vibration windows ($L=2048$, $75%$ overlap).
- Data Split: $20%$ of physical data files used for Meta-Training; $80%$ reserved exclusively for Out-Of-Distribution Meta-Validation.
- Task Protocol: 3-Way 5-Shot Support ($K_s=5$), 15-Shot Query ($K_q=15$).
- Batch Configuration: Meta-Batch Size = $24$, evaluated over 200 epochs.
Results
| Algorithm | Inner Loop Protocol | Peak Validation Accuracy | Empirical Characteristics |
|---|---|---|---|
| ProtoMAML v2 | Prototypical Head + Adapted Backbone (3 Steps) | 100.00% | Stable convergence; lower variance under domain shift. |
| ProtoMAML v1 | Prototype Initialization + Joint SGD (1 Step) | 99.44% | Fast adaptation; consistent loss minimization. |
| MAML++ | Per-Layer LRs + Multi-Step Loss (3 Steps) | 98.89% | Significant variance reduction over Vanilla MAML. |
| Prototypical Net | Non-parametric Distance Metric | 86.11% | Fast computation; susceptible to representational underfitting. |
| MAML (Vanilla) | Second-Order SGD (3 Steps) | 82.22% | Higher gradient variance across adaptation steps. |
| Reptile | First-Order Directional Update (3 Steps) | 70.56% | Minimal VRAM footprint; requires more adaptation steps. |
License
Distributed under the MIT License.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file parallel_metalearn-0.5.6.tar.gz.
File metadata
- Download URL: parallel_metalearn-0.5.6.tar.gz
- Upload date:
- Size: 108.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aebf3c257add59f04c24bf2dc1bef9546769a8599021c3f9ae9d3473bddc1136
|
|
| MD5 |
71f862341459494d7e7222e813181701
|
|
| BLAKE2b-256 |
abdf8dbc7cba5efbbeb7589272efc6526683e2ecb30e52b0b9da908866260d2a
|
File details
Details for the file parallel_metalearn-0.5.6-py3-none-any.whl.
File metadata
- Download URL: parallel_metalearn-0.5.6-py3-none-any.whl
- Upload date:
- Size: 132.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8fda72f23a12107218a992ef6b948c60f3b43b64ad1ba0d39246aee96844cb52
|
|
| MD5 |
250465c3cb1034085a2d7841e3061701
|
|
| BLAKE2b-256 |
e2fbdd4dd08bdf1c91b85b7b1038c622fd821bac7d048b7a266397b3e9b3fafa
|