Metadata-Version: 2.4
Name: dnaty
Version: 1.0.1
Summary: Evolutionary Neural Architecture Search — compress any PyTorch model with one function call
Author-email: Pedro Vergueiro <pedrol.vergueiro@gmail.com>
License: BSL-1.1
Project-URL: Homepage, https://github.com/pedrovergueiroo/dNATY
Project-URL: Repository, https://github.com/pedrovergueiroo/dNATY
Project-URL: Bug Tracker, https://github.com/pedrovergueiroo/dNATY/issues
Keywords: neural architecture search,neuroevolution,model compression,continual learning,pytorch,evolutionary algorithms,nas,edge ml
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: Developers
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: License :: Other/Proprietary License
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Operating System :: OS Independent
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch>=2.0.0
Requires-Dist: torchvision>=0.15.0
Requires-Dist: numpy>=1.24.0
Requires-Dist: scipy>=1.10.0
Requires-Dist: tqdm>=4.65.0
Provides-Extra: dev
Requires-Dist: pytest>=8.0.0; extra == "dev"
Requires-Dist: matplotlib>=3.7.0; extra == "dev"
Requires-Dist: jupyter; extra == "dev"
Dynamic: license-file

<div align="center">

<img src="logo.png" alt="dNATY logo" width="200" />

# dNATY

### Evolutionary AI Model Compression

**46.5% fewer FLOPs • 1.6× faster inference • 98.85% accuracy retained**

[![PyPI version](https://img.shields.io/pypi/v/dnaty.svg)](https://pypi.org/project/dnaty/)
[![Python 3.10+](https://img.shields.io/badge/python-3.10+-3776ab.svg?logo=python&logoColor=white)](https://www.python.org/)
[![PyTorch 2.0+](https://img.shields.io/badge/PyTorch-2.0+-ee4c2c.svg?logo=pytorch&logoColor=white)](https://pytorch.org/)
[![License: BSL-1.1](https://img.shields.io/badge/License-BSL--1.1-orange.svg)](LICENSE)

Automated model compression using multi-objective evolutionary search. Zero manual engineering. One function call: `compress(model, dataset)`.

![dNATY Architecture](figure_1.png)

</div>

---

## Why dNATY?

**Problem:** Most production models are oversized — too slow for real-time inference, too expensive to run.

**Existing solutions require:**
- Manual architecture tuning
- Days of hyperparameter search
- Accuracy/speed trade-offs

**dNATY solves this** with episodic memory-guided evolutionary search — operators that worked before are tried more often. Finds Pareto-optimal solutions in minutes.

### Proven Results (CIFAR-100)

| Model | FLOPs Reduction | Speedup | Accuracy | Time |
|-------|-----------------|---------|----------|------|
| ResNet-50 | **-46.5%** | 1.6× | 98.85% | 5min |
| EfficientNet-B0 | **-40%** | 1.4× | 98.85% | 6min |
| MobileNetV3-Large | **-98%** | 1.8× | 97.2% | 4min |

---

## Project Structure

```
dNATY/
├── dnaty/              # Core compression framework
├── dnaty_saas/         # Production API (FastAPI)
├── frontend/           # Web UI (React + TypeScript)
├── notebooks/          # Experiments & benchmarks
├── scripts/            # Demo & utilities
├── tests/              # Unit tests
└── pyproject.toml      # Package config
```

---

## Quick Start

```bash
pip install dnaty
```

```python
from dnaty import compress
from dnaty.experiments.fast_dataset import FastDataset

# Your existing model (any PyTorch model with Linear layers)
model = your_trained_model

# Your data
ds = FastDataset("MNIST", device="cpu", train_subset=10_000)

# Compress
result = compress(model, ds, target_flops=0.5, n_generations=30)

print(result.summary())
# CompressResult | arch=[128, 64] | FLOPs -46.5% (327680 -> 175104) |
#   params -52.3% (328K -> 156K) | acc=0.9821
```

---

## How It Works

dNATY runs a population of candidate architectures through an evolution loop:

1. **Mutate** — apply structural operators (add/remove neurons, merge layers, etc.)
2. **Train** — locally train each candidate for a few epochs
3. **Select** — NSGA-II Pareto selection: maximize accuracy, minimize FLOPs
4. **Remember** — episodic memory records which operators helped most; they get picked more often next round

The memory mechanism is dNATY's core innovation. Over generations, the search becomes smarter — not random.

---

## API

### `compress(model, train_data, **kwargs) -> CompressResult`

| Parameter | Default | Description |
|---|---|---|
| `model` | required | Any `nn.Module` with Linear layers |
| `train_data` | required | `DataLoader` or `FastDataset` |
| `target_flops` | `0.5` | Target fraction of original FLOPs (0.5 = 50% less) |
| `n_generations` | `30` | Evolutionary generations |
| `n_pop` | `15` | Population size |
| `device` | auto | `'cpu'` or `'cuda'` |
| `seed` | `None` | Fix for reproducibility |

### `CompressResult`

```python
result.model              # compressed nn.Module, ready to use
result.accuracy           # validation accuracy
result.flops_reduction    # e.g. 0.465 = 46.5% fewer FLOPs
result.flops_reduction_pct  # same as percentage
result.params_reduction_pct
result.arch               # hidden layer sizes found  [128, 64]
result.summary()          # one-line human-readable summary
```

---

## SaaS API

dNATY ships with a production-ready FastAPI backend.

```bash
cd dnaty_saas
cp .env.example .env   # fill DATABASE_URL, JWT_SECRET, ANTHROPIC_API_KEY
uvicorn main:app --reload
```

### `POST /api/v1/compress`

```json
{
  "description": "classifica defeitos em pecas, precisa rodar no Raspberry Pi",
  "dataset": "MNIST",
  "target_flops": 0.5,
  "n_generations": 30
}
```

Response `202`:
```json
{ "job_id": "a3f2c1b0", "status": "queued", "message": "..." }
```

### `GET /api/v1/compress/{job_id}`

```json
{
  "status": "completed",
  "result": {
    "accuracy": 0.9821,
    "flops_reduction": 0.465,
    "arch": [128, 64],
    "explanation": "...",        // Claude-generated explanation
    "deployment_code": "..."     // ready-to-use Python code
  }
}
```

> Set `ANTHROPIC_API_KEY` in `.env` to enable Claude explanations.  
> Without it, the endpoint still works — returns template text instead.

---

## Getting Started

### Web UI
```bash
cd frontend
npm install
npm run dev
```
Open [http://localhost:5173](http://localhost:5173)

### Jupyter Notebooks
- **CIFAR-100 Baseline** — [notebooks/colab_cifar100_notebook.ipynb](notebooks/colab_cifar100_notebook.ipynb)
- **ImageNet Benchmark** — [notebooks/colab_imagenet_simple_FIXED.ipynb](notebooks/colab_imagenet_simple_FIXED.ipynb)
- **CPU Latency** — [notebooks/benchmark_cpu_latency.py](notebooks/benchmark_cpu_latency.py)

### CLI Demo
```bash
python scripts/demo_compress.py           # 20 gens, MNIST (~5 min CPU)
python scripts/demo_compress.py --full    # 30 gens, more accurate
python scripts/demo_compress.py --dataset FashionMNIST
```

---

## Benchmarks

| Metric | Value |
|---|---|
| FLOPs reduction vs. initial arch | -46.5% |
| FLOPs reduction vs. RandomNAS | better in Pareto front |
| Speedup to target accuracy | 1.6x fewer generations |
| CL: BWT vs. EWC | 6.9x less forgetting |

All numbers reproducible with `python scripts/prove_it.py`.

---

## License

[BSL 1.1](LICENSE) — free for non-commercial use; contact pedrol.vergueiro@gmail.com for commercial licensing.
