Metadata-Version: 2.5
Name: mangosta
Version: 0.2.0a1
Summary: Lightweight MLOps and model lifecycle management for PyTorch
Author: Mangosta Developers
License: MIT
License-File: LICENSE
Keywords: checkpointing,mlops,model-registry,pytorch,tracking
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Requires-Dist: packaging>=23.0
Requires-Dist: pyyaml>=6.0
Requires-Dist: torch>=2.0.0
Provides-Extra: onnx
Requires-Dist: onnx>=1.14.0; extra == 'onnx'
Description-Content-Type: text/markdown

# mangosta

**Lightweight MLOps and Model Lifecycle Management for PyTorch.**

> *"Simple by default, strict on demand"* - Functional in three lines of code for rapid experimentation, yet robust enough for production pipelines and autonomous AI agents through formal contracts, deterministic fault tolerance, and immutable architecture versioning.

---

## Overview

Modern MLOps frameworks frequently impose heavy operational complexity: daemon background services, external databases, cloud platform dependencies, and brittle networking setups.

`mangosta` is engineered to provide local-first, serverless model lifecycle primitives directly within Python and standard file systems. It eliminates infrastructure overhead while delivering strict guarantees regarding reproducibility, tensor contracts, and crash-consistent persistence.

### Architectural Principles

- **Zero Service Dependencies**: No background daemons, web servers, or remote databases. All operations use direct, atomic file system calls (JSONL, standard ZIP archives, and optional SQLite).
- **Strict Immutability**: Architectures registered in `ModelRegistry` cannot be silently overwritten, preventing silent behavioral divergence between training runs.
- **Contract-Driven Inference**: Non-invasive tensor signature inference captures shapes (including dynamic batch dimensions) and data types, enforcing runtime verification before execution.
- **Deterministic Fault Tolerance**: Checkpoints capture complete training state including optimizer parameters, learning rate schedulers, gradient scalers, and pseudo-random number generator (RNG) states across Python, PyTorch (CPU and CUDA), and NumPy.
- **Decoupled Production Export**: Development artifacts (`.bundle`) remain optimized for training continuity and auditability, while standalone production formats (such as ONNX) are cleanly exported with full user control over dynamic axes, precision, and output restructuring.

---

## Comparison with Existing Tools

| Capability | mangosta | MLflow | Weights & Biases | ClearML |
| :--- | :--- | :--- | :--- | :--- |
| **Infrastructure Overhead** | None (Local files only) | Local/Remote server | Cloud daemon / SaaS | Server / Agent daemon |
| **External Services Required** | Zero | Web server + DB | API backend | Server stack + DB |
| **Persistence Mechanism** | Atomic JSONL & ZIP | SQLite / File store | Cloud storage | MongoDB / MinIO |
| **Fault-Tolerant RNG Restoration** | Native (CPU, CUDA, NumPy, Py) | Manual | Manual | Manual |
| **Tensor Contract Validation** | Native (Pre-inference) | Signature schema | Logged schema only | Logged schema only |
| **Packaging Architecture** | Modular (KISS & SOLID) | Monolithic package | Monolithic client | Complex SDK |
| **Offline Operation** | 100% Offline | Partial | Requires offline mode | Requires local server |

---

## Installation

Install using `uv` (recommended):

```bash
uv add mangosta
```

Or using standard `pip`:

```bash
pip install mangosta
```

### Optional Dependencies

For ONNX graph verification using `onnx.checker`:

```bash
uv add "mangosta[onnx]"
# or
pip install "mangosta[onnx]"
```

---

## Quickstart

### 1. Scaffold a Production Project

Generate an entire Clean Architecture PyTorch project in one command:

```bash
mangosta init my_project
cd my_project
```

This scaffolds:
- `configs/`: Hierarchical configurations (`base.yaml`, experiment overrides)
- `data/`: Raw and processed data partitions
- `src/`: Decoupled `data/`, `models/`, `engine/`, and `utils/`
- Root entrypoints: `train.py`, `infer.py`, `benchmark.py`

### 2. Atomic Local Tracking and Artifact Logging

Track hyperparameters, step-level metrics, and validation artifacts/images with atomic disk flushing using `RunTracker`:

```python
from mangosta import RunTracker

# RunTracker provides native context management and clean static typing
with RunTracker(experiment="vision", run_name="baseline_resnet") as run:
    run.log_params({"learning_rate": 1e-3, "batch_size": 32, "weight_decay": 1e-4})
    run.set_dataset("data/processed_images.parquet")
    
    for epoch in range(10):
        # Training iteration ...
        run.log_metric("train_loss", 0.342, step=epoch)
        run.log_metric("val_accuracy", 0.912, step=epoch)

    # Log validation images (supports PyTorch tensors, NumPy arrays, or file paths)
    run.log_image("val_samples", sample_tensor, step=10)

    # Log arbitrary training artifacts (e.g. confusion matrix, predictions CSV)
    run.log_artifact("outputs/confusion_matrix.json")
```

*(Note: `quick_track(...)` is also available as a shorthand alias).*

### 3. Architecture Registration

Register neural network architectures under a family name and semantic version:

```python
import torch
import torch.nn as nn
from mangosta import register_model

@register_model(family="classifier", version="1.0.0")
class ConvClassifier(nn.Module):
    def __init__(self, in_channels: int = 3, num_classes: int = 10) -> None:
        super().__init__()
        self.encoder = nn.Sequential(
            nn.Conv2d(in_channels, 16, kernel_size=3, padding=1),
            nn.ReLU(),
            nn.AdaptiveAvgPool2d((1, 1)),
        )
        self.head = nn.Linear(16, num_classes)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.head(self.encoder(x).flatten(1))
```

### 3. Checkpointing and Fault-Tolerant Resumption

Save complete training state including optimizer, scheduler, and all random generator states:

```python
from mangosta import save_bundle, resume_training, ModelRegistry

# Save crash-consistent checkpoint
save_bundle(
    output_path="checkpoints/epoch_5.bundle",
    model=model,
    family="classifier",
    version="1.0.0",
    init_args={"in_channels": 3, "num_classes": 10},
    sample_input=torch.randn(1, 3, 32, 32),
    is_checkpoint=True,
    optimizer=optimizer,
    scheduler=scheduler,
    epoch=5,
    step=1250,
)

# Resume deterministically following an interruption
fresh_model = ModelRegistry.build("classifier", version="1.0.0", in_channels=3, num_classes=10)
fresh_optimizer = torch.optim.Adam(fresh_model.parameters(), lr=1e-3)
fresh_scheduler = torch.optim.lr_scheduler.StepLR(fresh_optimizer, step_size=5)

state = resume_training(
    bundle_path="checkpoints/epoch_5.bundle",
    model=fresh_model,
    optimizer=fresh_optimizer,
    scheduler=fresh_scheduler,
    restore_rng=True,
)

start_epoch = state.epoch + 1
start_step = state.step
```

### 4. Decoupled Production Inference

Load models dynamically without importing the underlying model class in your serving scripts:

```python
import torch
from mangosta import load_bundle

# Reconstructs architecture via registry and mounts weights
bundle = load_bundle("artifacts/model_final.bundle")

# Inference with automated input signature validation
input_tensor = torch.randn(4, 3, 32, 32)
predictions = bundle.predict(input_tensor)
```

### 5. Standalone ONNX Export

Export directly to standard ONNX with automatic dictionary-to-tuple output unrolling and float16 precision:

```python
from mangosta import export_onnx

export_onnx(
    model_or_bundle="artifacts/model_final.bundle",
    output_path="exports/model.onnx",
    sample_input_shape=(1, 3, 512, 512),
    output_mapping=["semantic_mask", "radius_map", "gradient_map"],
    input_names=["input0"],
    output_names=["output0", "output1", "output2"],
    dynamic_axes={
        "input0": {0: "batch", 2: "height", 3: "width"},
        "output0": {0: "batch", 2: "height", 3: "width"},
        "output1": {0: "batch", 2: "height", 3: "width"},
        "output2": {0: "batch", 2: "height", 3: "width"},
    },
    float16=True,
    opset_version=18,
    verify=True,
)
```

---

## Command Line Interface (CLI)

`mangosta` provides a zero-daemon CLI utility:

```bash
# Scaffold a structured PyTorch project
mangosta init my_project

# Launch the zero-daemon local experiment and artifact dashboard
# Opens your default browser automatically to view runs, metric curves, and logged images
mangosta view

# View a specific experiment directory or bundle
mangosta view ./experiments --port 8501

# Inspect internal bundle contents, tensor signatures, and metadata
mangosta inspect checkpoints/epoch_5.bundle

# Generate a self-contained, offline interactive HTML report
mangosta report experiments/vision/baseline_resnet -o report.html
mangosta report checkpoints/epoch_5.bundle -o report.html

# Export a bundle directly to ONNX from the terminal
mangosta export-onnx checkpoints/epoch_5.bundle -o model.onnx --fp16 --input-shape 1 3 32 32 -m models.classifier
```

---

## Package Architecture

The library follows Single Responsibility (SRP) and Open-Closed (OCP) design principles:

```text
src/mangosta/
|-- __init__.py           # Unified high-level public API
|-- exceptions.py         # Domain-specific exception hierarchy
|-- core/                 # Architecture registry and tensor contracts
|   |-- registry.py       # ModelRegistry, @register_model, dynamic SemVer resolution
|   `-- schema.py         # TensorSpec, ModelSchema, infer_schema
|-- artifacts/            # Persistence, atomic packaging, and loading
|   |-- bundle.py         # Atomic ZIP creation, RNG serialization, checkpointing
|   `-- loader.py         # Decoupled reconstruction, LoadedBundle, resume_training
|-- tracking/             # Experiment tracking, artifacts, and visualization
|   |-- tracker.py        # RunTracker, quick_track, log_image, log_artifact
|   |-- viewer.py         # Zero-daemon local SPA browser viewer and REST API
|   `-- report.py         # Offline interactive HTML report generator
|-- export/               # Standalone production exports
|   `-- onnx.py           # ONNX exporter, dictionary unrolling, precision handling
`-- cli/                  # Command-line interface
    |-- main.py           # CLI entrypoint (init, inspect, report, view, export-onnx)
    `-- init_project.py   # Project scaffolding engine
```

---

## AI Agent Integration (Gemini / Antigravity Skill)

`mangosta` provides a production-grade Agent Skill designed for LLMs (Google Gemini, Antigravity CLI, Claude, OpenAI). The skill equips AI coding agents with the exact runbooks, API contracts, and defensive patterns needed to manage models autonomously without hallucinating commands or corrupting checkpoint states.

### Installing the Skill in Your Project

To make your local AI agent an expert in `mangosta`, copy the public skill specification to your workspace customization folder:

```bash
mkdir -p .agents/skills/mangosta
cp skills/mangosta/SKILL.md .agents/skills/mangosta/SKILL.md
```

Once installed, AI coding agents automatically discover the skill via its standard YAML frontmatter:
- **Scope**: Automated project scaffolding (`mangosta init`), architecture registration, contract validation, checkpoint recovery, artifact & image logging, zero-daemon visual inspection (`mangosta view`), and ONNX exports.
- **Location in repository**: [`skills/mangosta/SKILL.md`](file:///home/yeriel/Workspace/mangosta/skills/mangosta/SKILL.md)

### Agent Prompting Examples

With the skill installed, you can instruct your coding agent directly with natural language:
- *"Scaffold a new PyTorch computer vision project using mangosta."* -> Agent runs `mangosta init` and configures Clean Architecture components.
- *"Add checkpointing to our training loop with full RNG and optimizer recovery."* -> Agent implements `save_bundle(..., is_checkpoint=True)`.
- *"Log validation predictions as images and launch the local viewer."* -> Agent integrates `run.log_image()` and guides you to run `mangosta view`.
- *"Export our final trained bundle to ONNX with float16 precision."* -> Agent executes `mangosta export-onnx` or `export_onnx()`.

---

## Development

Run tests and verification suites locally:

```bash
# Sync virtual environment
uv sync

# Run automated tests with coverage
uv run pytest -v --cov=mangosta

# Run linter and formatting checks
uv run ruff check src tests examples
uv run ruff format --check src tests examples

# Build package distributions (sdist and wheel)
uv build
```

---

## License

MIT License.
