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

# PyTorch

> Load `.serva` files into a PyTorch training loop with `ServaDataset`.

**`ServaDataset`** reads a folder of **`.serva`** files and returns each file's raw bytes as a tensor. There is no decoding: the model trains on the encoded bytes directly. It behaves like `torchvision's` `ImageFolder`, so you point it at a directory and go.

```python theme={null}
from serva.torch import ServaDataset
from torch.utils.data import DataLoader

ds = ServaDataset("data/train")
loader = DataLoader(ds, batch_size=32, shuffle=True)

for batch in loader:
    ...   # your model, your training step
```

> [!NOTE]
>
> PyTorch ships with the `serva` package, so `ServaDataset` works after a plain
> `pip install serva`.

## Folder layout

How you lay out the directory decides what each item returns.

* **Class subfolders** give you `(tensor, label)`, ready for a classifier. The label comes from the folder name.
* **A flat folder** of `.serva` files gives you just the tensor, for text or any other unlabeled data.

```text theme={null}
data/train/
  ants/
    img_00001.serva
    ...
  bees/
    img_00001.serva
    ...
```

The class names are available on the dataset:

```python theme={null}
ds = ServaDataset("data/train")
print(ds.classes)   # ['ants', 'bees']
```

## Custom labels

When the label does not live in the folder name, for example a regression target or a value from a CSV, pass `label_fn`. It is any function from a file path to a label.

```python theme={null}
scores = {"sample_01": 4.7, "sample_02": 2.1}   # loaded from your own file
ds = ServaDataset("data/train", label_fn=lambda path: scores[path.stem])
```

## Sample length

Every `.serva` file has a different byte length, but a batch needs every sample the same size to stack. By default `ServaDataset` pads or truncates each file to a fixed `length` (262144 bytes) so batches stack with no extra work.

```python theme={null}
ds = ServaDataset("data/train", length=4096)   # fixed size, small
```

For sequence models that need each file's real length, pass `length=None` and batch with `pad_collate`. It pads each batch only to its own longest file and returns a mask marking the real bytes, so the model can ignore the padding.

```python theme={null}
from serva.torch import ServaDataset, pad_collate

ds = ServaDataset("data/train", length=None)
loader = DataLoader(ds, batch_size=32, collate_fn=pad_collate)

for bytes, mask, labels in loader:
    ...   # pass mask to your model so padded positions are ignored
```

> [!TIP]
>
> `length=None` with `pad_collate` keeps every byte of every file and adds the
> least padding possible. Give the mask to your model and the padding has no
> effect on the result.

## How it fits your model

`ServaDataset` has one job turn a `.serva` file into a tensor, plus a label when there is one. It knows nothing about your model. Anything model specific, such as reshaping or retyping the tensor and how batches are formed, is plain PyTorch that you control:

* **`transform`** a function you pass to reshape or retype each tensor.
* **`collate_fn`** on the DataLoader, how items are grouped into a batch.

Both are ordinary PyTorch, so anyone who knows the framework can adapt the dataset without learning anything new. For a full worked example, see the [ResNet style tutorial](https://servamind.mintlify.site/tutorials/training/serva-resnet-tutorial.md), which builds features from the bytes and trains a classifier end to end.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.