Skip to main content

Export PyTorch modules to Scratch sprites

Project description

cattorch

Export PyTorch neural networks to Scratch sprites.

cattorch transpiles a torch.nn.Module into a .sprite3 file that can be imported directly into any Scratch project. The generated sprite uses only standard Scratch blocks, so no extensions or modifications are required.

cattorch does not export training scripts, you will need to train your model with torch before exporting to a Scratch sprite.

Install

pip install cattorch

Requires Python 3.10+ and PyTorch 2.0+.

Usage

import torch
import torch.nn as nn
from cattorch import transpile

class TwoLayerNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(4, 8)
        self.fc2 = nn.Linear(8, 3)

    def forward(self, x):
        return self.fc2(torch.relu(self.fc1(x)))

model = TwoLayerNet()
# train your model first! then:
# transpile(model, example input, sprite name)
transpile(model, torch.randn(1, 4), "two_layer_net")
# => two_layer_net.sprite3

# optionally reduce file size by rounding weights
transpile(model, torch.randn(1, 4), "two_layer_net", sig_figs=6)

cattorch uses torch.export under the hood, which requires a single code path with no data-dependent control flow. If your model has conditional returns (e.g. returning loss during training), add an inference-only forward method:

# won't work: conditional return
def forward(self, x, targets=None):
    logits = self.head(x)
    if targets is None:
        return logits
    return logits, F.cross_entropy(logits, targets)

# will work: single return path
def forward_inference(self, x):
    return self.head(x)

model.eval()
model.forward = model.forward_inference
transpile(model, example_input, "my_model")

Some modules (e.g. HuggingFace transformer blocks) return tuples instead of plain tensors. torch.export will fail if a downstream layer receives a tuple where it expects a tensor. Unpack the output in your wrapper's forward method:

# won't work: block returns (hidden_states, attention_weights, ...)
x = block(x)

# will work: extract the tensor you need
x = block(x)[0]

In Scratch, the sprite reads its input from a list called input and writes results to a list called output. It is up to you to add logic to fill the input tensor and run the generated code blocks.

If the model takes multiple input tensors, the additional inputs are named input_1, input_2, etc.

Supported operations

Category Operations
Linear layers nn.Linear (with and without bias)
Matrix multiply @ / torch.matmul
Activations F.relu, torch.sigmoid, torch.tanh, F.gelu (tanh approx. only), F.silu, F.leaky_relu, F.elu
Normalization nn.LayerNorm, nn.RMSNorm, torch.rsqrt
Softmax F.softmax (any dim)
Embedding nn.Embedding
Masking masked_fill (for causal attention masks via register_buffer)
Arithmetic +, -, * (tensor and scalar), / (scalar), unary -, torch.pow
Reduction torch.mean (along a dim)
Tensor creation torch.arange, torch.ones, torch.zeros, torch.full, torch.ones_like, torch.zeros_like
Shape view, reshape, flatten, contiguous, clone (no-ops on flat data)
Transpose transpose, permute, .T (arbitrary dimensions)
Split / Chunk split, split_with_sizes, chunk
Concatenation torch.cat (any dim, any number of inputs)
Slice tensor[:n] style slicing along any dimension

These are sufficient for architectures like MLPs and transformer LLMs, including multi-head attention, combined QKV projections, rotary position embeddings (RoPE), causal masking, pre-norm blocks with residual connections, and SwiGLU-style gating. CNN and RNN support is planned for the future.

Scratch limits

  • Project size: Scratch limits projects to 5 MB. cattorch warns at 4 MB and errors at 5 MB.
  • List length: Scratch lists can hold at most 200,000 items. cattorch raises an error if any weight tensor or intermediate list exceeds this.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cattorch-0.2.1.tar.gz (46.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cattorch-0.2.1-py3-none-any.whl (65.0 kB view details)

Uploaded Python 3

File details

Details for the file cattorch-0.2.1.tar.gz.

File metadata

  • Download URL: cattorch-0.2.1.tar.gz
  • Upload date:
  • Size: 46.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cattorch-0.2.1.tar.gz
Algorithm Hash digest
SHA256 02a06a84b9288ebb9f4d2c630aec7c7cbd29a6c7f32a293d68439c3f787ca6c4
MD5 c00d082455649991a4fdf0a3c68da73e
BLAKE2b-256 da45ca772f6f7b125c219f414d9c1aebf42b5c47fb9d3f81c59c9f0f6b098d29

See more details on using hashes here.

Provenance

The following attestation bundles were made for cattorch-0.2.1.tar.gz:

Publisher: publish.yml on NormallyNormal/cattorch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cattorch-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: cattorch-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 65.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cattorch-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 37afc6619a802d7a0baf11ab559364194810d11c1d237283291be689c4d3e83e
MD5 57c3696338af76cc8e1b9103f6b35dd7
BLAKE2b-256 151fef9982260d1f057ffc935f7e2ce2736d1826098b41ae4d6f91531abfb313

See more details on using hashes here.

Provenance

The following attestation bundles were made for cattorch-0.2.1-py3-none-any.whl:

Publisher: publish.yml on NormallyNormal/cattorch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page