Skip to main content
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.
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.
The class names are available on the dataset:

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.

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.
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.
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, which builds features from the bytes and trains a classifier end to end.