Converting TinyStories-33M to ONNX

Quick way (recommended)

Alternative: via the universal converter (scripts/universal_onnx_converter.py)

# Install dependencies (if not already installed)
pip install torch transformers onnxruntime onnxruntime-tools huggingface_hub

# Basic conversion of TinyStories-33M from Hugging Face
python3 ../scripts/universal_onnx_converter.py roneneldan/TinyStories-33M -o tinystories-33m.onnx --opset 14 -l 256

# Quantize to INT8 and save the quantized model
python3 ../scripts/universal_onnx_converter.py roneneldan/TinyStories-33M -o tinystories-33m.onnx --opset 14 -l 256 -q

# Search for models on Hugging Face (optional)
python3 ../scripts/universal_onnx_converter.py --search tinystories

1. Install dependencies

pip install torch transformers onnxruntime

2. Run the conversion

python convert_tinystories_optimized.py

This will create:

  • tinystories-33m-quantized.onnx (~50–80 MB) — quantized model
  • tokenizer/ — tokenizer files

3. Copy the model into the app

mkdir -p ~/Documents/models
cp tinystories-33m-quantized.onnx ~/Documents/models/

Detailed way

Step 1: Prepare the environment

# Create a virtual environment (optional)
python -m venv onnx_env
source onnx_env/bin/activate  # Linux/Mac
# or
onnx_env\Scripts\activate  # Windows

# Install dependencies
pip install -r requirements_convert.txt

Step 2: Convert the model

# Basic conversion
python convert_tinystories.py

# Or optimized with quantization
python convert_tinystories_optimized.py

Step 3: Validate the result

# Check file sizes
ls -lh *.onnx

# Test the model
python -c "
import onnxruntime as ort
session = ort.InferenceSession('tinystories-33m-quantized.onnx')
print('Model loaded successfully!')
print('Inputs:', [input.name for input in session.get_inputs()])
print('Outputs:', [output.name for output in session.get_outputs()])
"

Model sizes

Model Original Quantized Reduction
TinyStories-33M ~291 MB ~50–80 MB ~70–80%