> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cirron.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PyTorch

> PyTorch project template.

# PyTorch Template

The PyTorch template scaffolds a production-ready ML project with PyTorch, torchvision, and common ML dependencies. Two variants are available:

* **`pytorch`**: basic inference template for deployment
* **`pytorch-train`**: complete training pipeline with checkpoints, validation, and metrics

## Quick Start

```bash theme={null}
# Inference project
cirron init my-pytorch-model --template pytorch

# Training project
cirron init my-training-project --template pytorch-train
```

The model type (classification, regression, etc.) is selected interactively during `cirron init`.

## Project Structure

```
my-pytorch-model/
├── src/
│   ├── model.py           # Model architecture (ClassificationModel / RegressionModel)
│   ├── data_loader.py     # Dataset + DataLoader utilities
│   ├── train.py           # Training script (pytorch-train only)
│   └── inference.py       # Inference / prediction
├── data/                  # Data directory
├── checkpoints/           # Saved during training
├── requirements.txt
├── Dockerfile
└── cirron.yaml
```

For shared files (`Dockerfile`, `.gitignore`, `README.md`, `.env.example`, tests), see [Common files](/cli/templates/common).

## Cirron-specific bits

### cirron.yaml

The generated config wires the training entrypoint and runtime:

```yaml theme={null}
framework: pytorch
model_type: classification
train_entrypoint: src/train.py
inference_entrypoint: src/inference.py
```

### Profiling hooks

The generated `train.py` and `inference.py` integrate with the Cirron SDK. Call `ci.profile()` once at module top so traces show up in `cirron traces`:

```python theme={null}
import cirron as ci

ci.profile()
```

The PyTorch hooks then auto-instrument forward/backward/optimizer steps. For explicit regions the hooks don't cover, use `ci.scope` as a context manager:

```python theme={null}
with ci.scope("validate"):
    val_loss, accuracy = self.validate()
```

### requirements.txt

```txt theme={null}
torch>=2.0.0
torchvision>=0.15.0
numpy>=1.21.0
scikit-learn>=1.3.0
matplotlib>=3.5.0
tqdm>=4.64.0
Pillow>=9.0.0
```

## Model Types

* **Classification**: `nn.CrossEntropyLoss`, softmax output. Default architecture is a small CNN.
* **Regression**: `nn.MSELoss`, linear output. Default architecture is a fully-connected MLP.

The interactive `cirron init` prompt selects which one is generated into `src/model.py`.

## Usage

```bash theme={null}
# Train
python src/train.py

# Build container
cirron build

# Deploy
cirron deploy
```

## GPU Support

The generated code automatically detects CUDA via `torch.cuda.is_available()` and falls back to CPU. To target a GPU build, set `runtime: gpu` in `cirron.yaml`.

## Next Steps

<CardGroup cols={2}>
  <Card title="TensorFlow Template" href="/cli/templates/tensorflow">
    Alternative deep learning framework
  </Card>

  <Card title="scikit-learn Template" href="/cli/templates/sklearn">
    Traditional ML algorithms
  </Card>

  <Card title="Common files" href="/cli/templates/common">
    Dockerfile, tests, and shared scaffolding
  </Card>

  <Card title="Deployment" href="/cli/commands/deploy">
    Deploy your trained model
  </Card>
</CardGroup>
