keepcalculator

06 Numbers and Powers

Hugging Face Models: Runtime & Format Guide

Card No. 06·30258 · Updated · Standard formula, shown below

The calculator on this card is switched off while it is checked. The formula is below, and the worked example shows how to do the sum by hand.

Choosing the right runtime and format for a Hugging Face model determines how fast it runs, how much memory it uses, and whether it works on your hardware. This guide explains the main options—PyTorch, TensorFlow, ONNX, and the safetensors file format—and gives you a clear decision path for CPU, GPU, and edge devices.

Formula

The estimated memory needed for a model is roughly:

Memory ≈ Model size × 1.2 × (32 / quantization bits)

  • Model size (GB) – the size of the weights file on disk.
  • 1.2 – a safety factor for activations, optimizer states, and overhead during inference.
  • Quantization bits – 32 for full FP32, 16 for FP16, 8 for INT8, 4 for INT4.

The output Fits in RAM? is 1 if the estimated memory is ≤ available RAM, else 0.

Worked Example

Using the defaults: a 1.5 GB model, 8 GB RAM, 8-bit quantization.

  1. Memory = 1.5 × 1.2 × (32 / 8) = 1.5 × 1.2 × 4 = 7.2 GB
  2. 7.2 GB ≤ 8 GB → fits = 1

The calculator shows 7.2 GB and 1 (fits).

Runtimes: PyTorch vs TensorFlow vs ONNX

Hugging Face models are primarily trained and distributed in PyTorch. The transformers library defaults to PyTorch, and most community models are uploaded with .bin or .safetensors weights that load directly into PyTorch. TensorFlow is supported for many architectures, but you often need to convert or use the TF version of the model. ONNX (Open Neural Network Exchange) is a format that runs on multiple runtimes—ONNX Runtime, TensorRT, CoreML—and is ideal for production deployment where you want to avoid a heavy framework dependency.

File Formats: .bin vs .safetensors vs .onnx

The .bin format is the traditional PyTorch serialization (pickle). It can execute arbitrary code when loaded, which is a security risk if you download untrusted models. .safetensors is a newer format that stores tensors in a flat, secure structure—no code execution—and loads faster because it memory-maps the file. ONNX files are a single graph that can be optimized and run without Python. For new projects, prefer .safetensors for safety and speed.

How to Choose: CPU, GPU, or Edge

  • CPU-only – use ONNX Runtime with quantization (INT8) to get the best speed. PyTorch on CPU works but is slower.
  • GPU (NVIDIA) – PyTorch with FP16 or BF16 is the default. For larger models, use 8-bit or 4-bit quantization via bitsandbytes.
  • Apple Silicon – use PyTorch with MPS (Metal Performance Shaders) or convert to CoreML for production.
  • Edge/embedded – ONNX or TensorFlow Lite; keep the model small with quantization.

Common Problems and Solutions

Out-of-memory errors – reduce batch size, use gradient checkpointing, or quantize to 8-bit. Slow inference – try ONNX Runtime with graph optimizations, or use torch.compile on GPU. Model won’t load – check that the model architecture matches the library version; sometimes you need to install a specific transformers version. Missing ops in ONNX – some models use custom operators; you may need to use the optimum library to export correctly.

Quantization: When and How Much

Quantization reduces precision to fit memory and speed up inference. INT8 is a safe default—most models lose less than 1% accuracy. INT4 can halve memory again but may degrade quality noticeably, especially for text generation. Use 8-bit for general use, 4-bit only when you need to run a model that otherwise wouldn’t fit. Tools like bitsandbytes support load-time quantization without retraining.

Cost and Performance Trade-offs

Larger runtimes (PyTorch) give you the most flexibility but use more memory and CPU overhead. ONNX Runtime is lighter and often 2–3× faster on CPU. Quantization reduces memory but increases latency slightly due to dequantization steps. For cloud deployment, the cost is proportional to memory and compute time—so a quantized ONNX model can cut bills significantly. For local use, the trade-off is between setup effort and speed.

Safety and Security

Always download models from trusted sources. The .bin format can contain malicious pickle code; prefer .safetensors. When using ONNX, ensure the runtime is up to date to avoid known vulnerabilities. Do not run untrusted models on systems with sensitive data. For production, pin the exact model version and hash.

Frequently Asked Questions

Should I use PyTorch or TensorFlow for Hugging Face?

PyTorch is the default and best supported. TensorFlow works but often requires extra conversion steps. Choose PyTorch unless you have a strong reason to stay in TensorFlow.

What is the difference between .bin and .safetensors?

.bin is a pickle serialization that can execute code; .safetensors is a secure, faster-loading format. Use .safetensors for safety and performance.

How do I convert a model to ONNX?

Use the optimum library: optimum-cli export onnx --model bert-base-uncased bert-base-uncased-onnx/. This handles most architectures automatically.

Can I run a model without GPU?

Yes, but it will be slower. Use ONNX Runtime with INT8 quantization for best CPU performance, or use a smaller model.

What is the best quantization for a 7B model?

For a 7B model (about 14 GB in FP16), use 8-bit to fit in 12 GB RAM, or 4-bit to fit in 6 GB. Test quality to ensure it meets your needs.

Why is my model loading slowly?

Large .bin files take time to deserialize. Switch to .safetensors for faster loading, or use memory mapping.

Related

Formula used
in t | Model size (GB) | GB | 1.5 | 0.1 | 100
in m | RAM available (GB) | GB | 8 | 0.5 | 512
in q | Quantization bits | bits | 8 | 4 | 32
out mem | Estimated memory needed (GB) | GB | t * 1.2 * (32 / q) | 2
out fits | Fits in RAM? | - | (t * 1.2 * (32 / q)

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →