|
| 1 | +--- |
| 2 | +title: SafeTensors to GGUF |
| 3 | +weight: 3 |
| 4 | +bookToc: true |
| 5 | +--- |
| 6 | + |
| 7 | +# SafeTensors to GGUF Conversion |
| 8 | + |
| 9 | +This guide covers converting SafeTensors models (typically BERT and RoBERTa) to GGUF format using zonnx. SafeTensors is HuggingFace's preferred serialization format for model weights. |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +- zonnx installed (`go install github.com/zerfoo/zonnx/cmd/zonnx@latest`) |
| 14 | +- A HuggingFace model directory containing `config.json` and `model.safetensors` |
| 15 | + |
| 16 | +## Directory Structure |
| 17 | + |
| 18 | +zonnx expects a directory as input for SafeTensors conversion. The directory must contain: |
| 19 | + |
| 20 | +``` |
| 21 | +model-dir/ |
| 22 | + config.json # required -- model configuration |
| 23 | + model.safetensors # required -- model weights |
| 24 | +``` |
| 25 | + |
| 26 | +The `config.json` provides architecture metadata (hidden size, layer count, attention heads, etc.) that zonnx maps to GGUF metadata keys. The `model.safetensors` file contains the weight tensors. |
| 27 | + |
| 28 | +## Step 1: Download a Model |
| 29 | + |
| 30 | +Download a model from HuggingFace. For example, to get [FinBERT](https://huggingface.co/ProsusAI/finbert) for financial sentiment analysis: |
| 31 | + |
| 32 | +```bash |
| 33 | +# Create a directory for the model |
| 34 | +mkdir -p ./models/finbert |
| 35 | + |
| 36 | +# Download config.json and model.safetensors |
| 37 | +# (use the HuggingFace CLI, git clone, or manual download) |
| 38 | +huggingface-cli download ProsusAI/finbert \ |
| 39 | + --include config.json model.safetensors \ |
| 40 | + --local-dir ./models/finbert |
| 41 | +``` |
| 42 | + |
| 43 | +Verify the directory contents: |
| 44 | + |
| 45 | +```bash |
| 46 | +ls ./models/finbert/ |
| 47 | +# config.json model.safetensors |
| 48 | +``` |
| 49 | + |
| 50 | +## Step 2: Convert to GGUF |
| 51 | + |
| 52 | +Run the `convert` command with `--format safetensors` and the appropriate `--arch`: |
| 53 | + |
| 54 | +```bash |
| 55 | +zonnx convert \ |
| 56 | + --format safetensors \ |
| 57 | + --arch bert \ |
| 58 | + --output ./models/finbert.gguf \ |
| 59 | + ./models/finbert/ |
| 60 | +``` |
| 61 | + |
| 62 | +Note that the input argument is the **directory** path, not the `.safetensors` file path. |
| 63 | + |
| 64 | +## config.json Fields and Metadata Mapping |
| 65 | + |
| 66 | +zonnx reads `config.json` and maps fields to GGUF metadata. For BERT and RoBERTa models, the following fields are mapped: |
| 67 | + |
| 68 | +### Standard Fields (All Architectures) |
| 69 | + |
| 70 | +| config.json field | GGUF key | |
| 71 | +|-------------------|----------| |
| 72 | +| `hidden_size` | `{arch}.embedding_length` | |
| 73 | +| `num_hidden_layers` | `{arch}.block_count` | |
| 74 | +| `num_attention_heads` | `{arch}.attention.head_count` | |
| 75 | +| `num_key_value_heads` | `{arch}.attention.head_count_kv` | |
| 76 | +| `intermediate_size` | `{arch}.feed_forward_length` | |
| 77 | +| `vocab_size` | `{arch}.vocab_size` | |
| 78 | +| `max_position_embeddings` | `{arch}.context_length` | |
| 79 | + |
| 80 | +### BERT/RoBERTa-Specific Fields |
| 81 | + |
| 82 | +| config.json field | GGUF key | |
| 83 | +|-------------------|----------| |
| 84 | +| `layer_norm_eps` | `{arch}.attention.layer_norm_epsilon` | |
| 85 | +| `num_labels` | `{arch}.num_labels` | |
| 86 | +| (auto) | `{arch}.pooler_type` = `"cls"` | |
| 87 | + |
| 88 | +If `num_labels` is not present in `config.json` but `id2label` is, zonnx derives the label count from the `id2label` mapping. |
| 89 | + |
| 90 | +## Supported Data Types |
| 91 | + |
| 92 | +zonnx handles these SafeTensors data types: |
| 93 | + |
| 94 | +| SafeTensors dtype | GGUF dtype | |
| 95 | +|-------------------|------------| |
| 96 | +| `F32` | Float32 | |
| 97 | +| `F16` | Float16 | |
| 98 | +| `BF16` | BFloat16 | |
| 99 | + |
| 100 | +Non-float tensors (e.g., `position_ids` with int64 dtype) are skipped automatically during conversion. |
| 101 | + |
| 102 | +## End-to-End Example: FinBERT |
| 103 | + |
| 104 | +This example converts [ProsusAI/finbert](https://huggingface.co/ProsusAI/finbert), a BERT model fine-tuned for financial sentiment classification. |
| 105 | + |
| 106 | +### 1. Download the Model |
| 107 | + |
| 108 | +```bash |
| 109 | +mkdir -p ./models/finbert |
| 110 | +huggingface-cli download ProsusAI/finbert \ |
| 111 | + --include config.json model.safetensors \ |
| 112 | + --local-dir ./models/finbert |
| 113 | +``` |
| 114 | + |
| 115 | +### 2. Inspect config.json |
| 116 | + |
| 117 | +A typical FinBERT `config.json` contains: |
| 118 | + |
| 119 | +```json |
| 120 | +{ |
| 121 | + "architectures": ["BertForSequenceClassification"], |
| 122 | + "hidden_size": 768, |
| 123 | + "num_hidden_layers": 12, |
| 124 | + "num_attention_heads": 12, |
| 125 | + "intermediate_size": 3072, |
| 126 | + "vocab_size": 30522, |
| 127 | + "max_position_embeddings": 512, |
| 128 | + "layer_norm_eps": 1e-12, |
| 129 | + "id2label": { |
| 130 | + "0": "positive", |
| 131 | + "1": "negative", |
| 132 | + "2": "neutral" |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +zonnx maps these fields to GGUF metadata keys like `bert.embedding_length`, `bert.block_count`, `bert.attention.head_count`, etc. The three labels in `id2label` produce `bert.num_labels = 3`. |
| 138 | + |
| 139 | +### 3. Convert |
| 140 | + |
| 141 | +```bash |
| 142 | +zonnx convert \ |
| 143 | + --format safetensors \ |
| 144 | + --arch bert \ |
| 145 | + --output ./models/finbert.gguf \ |
| 146 | + ./models/finbert/ |
| 147 | +``` |
| 148 | + |
| 149 | +### 4. Verify |
| 150 | + |
| 151 | +```bash |
| 152 | +zonnx inspect --pretty ./models/finbert.gguf |
| 153 | +``` |
| 154 | + |
| 155 | +The output should show GGUF metadata with `bert.*` keys and all encoder layer tensors. |
| 156 | + |
| 157 | +### 5. Use with Zerfoo |
| 158 | + |
| 159 | +```bash |
| 160 | +zerfoo predict ./models/finbert.gguf --input "Revenue exceeded expectations this quarter" |
| 161 | +``` |
| 162 | + |
| 163 | +## RoBERTa Models |
| 164 | + |
| 165 | +RoBERTa conversion follows the same steps. Use `--arch roberta`: |
| 166 | + |
| 167 | +```bash |
| 168 | +zonnx convert \ |
| 169 | + --format safetensors \ |
| 170 | + --arch roberta \ |
| 171 | + --output ./models/roberta.gguf \ |
| 172 | + ./models/roberta-dir/ |
| 173 | +``` |
| 174 | + |
| 175 | +RoBERTa uses the same encoder layer structure as BERT. The `--arch` flag ensures tensor names are mapped using the `roberta.encoder.layer.N.*` prefix pattern. |
0 commit comments