--- base_model: - meta-llama/Llama-Guard-3-8B datasets: - eaddario/imatrix-calibration language: - en license: - llama3.1 pipeline_tag: text-generation tags: - gguf - quant - experimental --- # Experimental GGUF quantized versions of meta-llama/Llama-Guard-3-8B Using [LLaMA C++]() release [b4801]() for quantization. Original model: [meta-llama/Llama-Guard-3-8B](https://huggingface.co./meta-llama/Llama-Guard-3-8B) From the original model creators: > Llama Guard 3 is a Llama-3.1-8B pretrained model, fine-tuned for content safety classification. Similar to previous versions, it can be used to classify content in both LLM inputs (prompt classification) and in LLM responses (response classification). It acts as an LLM – it generates text in its output that indicates whether a given prompt or response is safe or unsafe, and if unsafe, it also lists the content categories violated. > > Llama Guard 3 was aligned to safeguard against the MLCommons standardized hazards taxonomy and designed to support Llama 3.1 capabilities. Specifically, it provides content moderation in 8 languages, and was optimized to support safety and security for search and code interpreter tool calls. # PLEASE READ THIS BEFORE USING THESE EXPERIMENTAL VERSIONS! An area of personal interest is finding ways to optimize the inference performance of LLMs when deployed in resource-constrained environments like commodity hardware, desktops, laptops, edge devices, etc. There are many approaches to accomplish this, including architecture simplification and knowledge distillation, but for now I'm focusing primarily on quantization and pruning. The process of [quantization]() reduces the precision of the model's weights, leading to significant reductions in model size, memory needs and computational requirements (a good thing), but this however comes at the expense of a loss in the model's capabilities and accuracy (a bad thing!). Another approach is to [prune]() the model, that is, to selectively zero-out groups of parameters. Although significant reductions can be achieved this way, the risk of severely degrading the model's performance is markedly higher than when quantizing, as the process requires a deep understanding of the model's architecture in order to identify which tensors can be safely zero'ed. For all means and purposes, pruning is the equivalent of lobotomizing the LLM! A successful outcome is when the overall size is reduced with no, or negligible, loss of capabilities (i.e. language understanding, math and logic problem-solving, conversation, coding, domain-specific knowledge, etc.) compared to the original version. On that regard, the method I'm using seems to yield some modest but encouraging results, and the versions available in this repo are on average **7.5% smaller** than other, high-quality, sources with negligible loss of capability. As I continue to improve the process and develop tools to automate it, I aim to achieve further reductions in the **10-15%** range, maybe more. For testing and comparison I'd normally use models produced by [Unsloth]() ([Daniel and Michael Han]() do some really advanced level stuff!) and [Bartowski]() (see credits below), but they don't provide GGUF versions of this model, so all tests and comparisons are done against naive quantizations obtained by simply running `llama-quantize` with no further optimization. All experimental versions were generated using an appropriate imatrix created from calibration datasets available at [eaddario/imatrix-calibration](). At its core, an Importance Matrix (imatrix) is a table or, more broadly, a structured representation that scores the relative importance of different features or parameters in a machine learning model. It essentially quantifies the "impact" each feature has on a specific outcome, prediction, or relationship being modeled, and it helps to counterbalance the negative effects of quantization and pruning. The process to generate these models is roughly as follows: 1. Convert the the original model's tensors to [GGUF]() F16* 2. Estimate the Perplexity score for the F16 model (baseline) using the [wikitext-2-raw-v1]() dataset, and save the [logits]() 3. Generate an [imatrix]() from selected calibration datasets 4. Quantize & prune versions of the base model 5. Calculate Perplexity, KL Divergence, ARC (Easy+Challenge), HellaSwag, MMLU, Truthful QA and WinoGrande [scores]() for each quantized model 6. Keep versions with the best scores 7. Repeat until all desired quants are created. I find that quantizations below Q3/IQ3 are not fit for my purposes and therefore do not usually generate them, but happy to provide other quants on request. *[BF16]() would be preferred, but Apple's GPUs don't support it yet, and therefore any operations are executed in the CPU, making it unacceptably slow. This is expected to change in the near term but until then, if you are using Apple kit avoid using any models tagged BF16 # Models ### Sizes (in GB) | Model | Naive | Repo | Shrinkage | |-----------------------------------------------------------|------:|-----:|----------:| | [Llama-Guard-3-8B-IQ3_M](./Llama-Guard-3-8B-IQ3_M.gguf) | 3.78 | 3.47 | 8.2% | | [Llama-Guard-3-8B-IQ3_S](./Llama-Guard-3-8B-IQ3_S.gguf) | 3.68 | 3.37 | 8.4% | | [Llama-Guard-3-8B-IQ4_NL](./Llama-Guard-3-8B-IQ4_NL.gguf) | 4.68 | 4.35 | 7.1% | | [Llama-Guard-3-8B-Q3_K_L](./Llama-Guard-3-8B-Q3_K_L.gguf) | 4.32 | 4.01 | 7.2% | | [Llama-Guard-3-8B-Q3_K_M](./Llama-Guard-3-8B-Q3_K_M.gguf) | 4.02 | 3.70 | 8.0% | | [Llama-Guard-3-8B-Q3_K_S](./Llama-Guard-3-8B-Q3_K_S.gguf) | 3.66 | 3.35 | 8.5% | | [Llama-Guard-3-8B-Q4_K_M](./Llama-Guard-3-8B-Q4_K_M.gguf) | 4.92 | 4.59 | 6.7% | | [Llama-Guard-3-8B-Q4_K_S](./Llama-Guard-3-8B-Q4_K_S.gguf) | 4.69 | 4.36 | 7.0% | | [Llama-Guard-3-8B-Q5_K_M](./Llama-Guard-3-8B-Q5_K_M.gguf) | 5.73 | 5.34 | 6.8% | | [Llama-Guard-3-8B-Q5_K_S](./Llama-Guard-3-8B-Q5_K_S.gguf) | 5.60 | 5.21 | 7.0% | | [Llama-Guard-3-8B-Q6_K](./Llama-Guard-3-8B-Q6_K.gguf) | 6.60 | 6.13 | 7.1% | | [Llama-Guard-3-8B-Q8_0](./Llama-Guard-3-8B-Q8_0.gguf) | 8.54 | 7.82 | 8.4% | ### Perplexity and KL Divergence scores | Model | μPPL | 𝜌PPL | μKLD | RMS Δp | |-----------------------------------------------------------|--------------------:|-------:|-------------------:|--------------:| | [Llama-Guard-3-8B-IQ3_M](./Llama-Guard-3-8B-IQ3_M.gguf) | 8.238324 ±0.051597 | 95.64% | 0.223382 ±0.000737 | 14.820 ±0.056 | | [Llama-Guard-3-8B-IQ3_S](./Llama-Guard-3-8B-IQ3_S.gguf) | 8.262126 ±0.052116 | 95.48% | 0.228988 ±0.000778 | 14.837 ±0.057 | | [Llama-Guard-3-8B-IQ4_NL](./Llama-Guard-3-8B-IQ4_NL.gguf) | 7.003106 ±0.043412 | 98.57% | 0.069975 ±0.000336 | 7.869 ±0.046 | | [Llama-Guard-3-8B-Q3_K_L](./Llama-Guard-3-8B-Q3_K_L.gguf) | 7.825878 ±0.050018 | 96.39% | 0.181845 ±0.000636 | 12.836 ±0.052 | | [Llama-Guard-3-8B-Q3_K_M](./Llama-Guard-3-8B-Q3_K_M.gguf) | 7.911577 ±0.050533 | 96.13% | 0.194188 ±0.000679 | 13.302 ±0.054 | | [Llama-Guard-3-8B-Q3_K_S](./Llama-Guard-3-8B-Q3_K_S.gguf) | 9.042611 ±0.058114 | 93.39% | 0.314022 ±0.001100 | 17.945 ±0.066 | | [Llama-Guard-3-8B-Q4_K_M](./Llama-Guard-3-8B-Q4_K_M.gguf) | 6.961140 ±0.043016 | 98.70% | 0.063448 ±0.000302 | 7.468 ±0.043 | | [Llama-Guard-3-8B-Q4_K_S](./Llama-Guard-3-8B-Q4_K_S.gguf) | 6.985735 ±0.043183 | 98.61% | 0.068174 ±0.000323 | 7.787 ±0.044 | | [Llama-Guard-3-8B-Q5_K_M](./Llama-Guard-3-8B-Q5_K_M.gguf) | 6.843617 ±0.042247 | 99.08% | 0.045866 ±0.000199 | 6.306 ±0.034 | | [Llama-Guard-3-8B-Q5_K_S](./Llama-Guard-3-8B-Q5_K_S.gguf) | 6.849948 ±0.042246 | 99.07% | 0.046677 ±0.000201 | 6.388 ±0.034 | | [Llama-Guard-3-8B-Q6_K](./Llama-Guard-3-8B-Q6_K.gguf) | 6.810479 ±0.041963 | 99.17% | 0.041612 ±0.000183 | 5.976 ±0.031 | | [Llama-Guard-3-8B-Q8_0](./Llama-Guard-3-8B-Q8_0.gguf) | 6.796496 ±0.041835 | 99.23% | 0.039114 ±0.000160 | 5.806 ±0.029 | | [Llama-Guard-3-8B-F16](./Llama-Guard-3-8B-F16.gguf) | 6.554978 ±0.040159 | 100% | N/A | N/A | ### ARC, HellaSwag, MMLU, Truthful QA and WinoGrande scores Scores generated using [llama-perplexity]() with 750 tasks per test, and a context size of 768 tokens. Naive (`llama-quantize` with no optimization) Q4_K_M quantization included for comparison. For the test data used in the generation of these scores, follow the appropiate links: [HellaSwag](), [ARC, MMLU, Truthful QA]() and [WinoGrande]() | Model | ARC | HellaSwag | MMLU | Truthful QA | WinoGrande | |-----------------------------------------------------------|----------------:|----------:|----------------:|----------------:|----------------:| | [Llama-Guard-3-8B-IQ3_M](./Llama-Guard-3-8B-IQ3_M.gguf) | 60.2667 ±1.7880 | 79.07 | 38.5333 ±1.7783 | 32.1333 ±1.7063 | 72.9333 ±1.6235 | | [Llama-Guard-3-8B-IQ3_S](./Llama-Guard-3-8B-IQ3_S.gguf) | 62.0000 ±1.7736 | 78.40 | 37.7333 ±1.7711 | 32.4000 ±1.7100 | 73.2000 ±1.6184 | | [Llama-Guard-3-8B-IQ4_NL](./Llama-Guard-3-8B-IQ4_NL.gguf) | 66.5333 ±1.7242 | 81.47 | 37.3333 ±1.7674 | 34.2667 ±1.7342 | 72.6667 ±1.6284 | | [Llama-Guard-3-8B-Q3_K_L](./Llama-Guard-3-8B-Q3_K_L.gguf) | 62.8000 ±1.7661 | 77.46 | 38.5333 ±1.7783 | 30.6667 ±1.6849 | 73.4667 ±1.6132 | | [Llama-Guard-3-8B-Q3_K_M](./Llama-Guard-3-8B-Q3_K_M.gguf) | 62.9333 ±1.7648 | 78.00 | 38.8000 ±1.7805 | 31.2000 ±1.6929 | 72.5333 ±1.6309 | | [Llama-Guard-3-8B-Q3_K_S](./Llama-Guard-3-8B-Q3_K_S.gguf) | 59.3333 ±1.7948 | 77.87 | 36.9333 ±1.7635 | 32.5333 ±1.7119 | 73.3333 ±1.6158 | | [Llama-Guard-3-8B-Q4_K_M](./Llama-Guard-3-8B-Q4_K_M.gguf) | 64.1333 ±1.7525 | 81.47 | 38.0000 ±1.7736 | 33.6000 ±1.7259 | 72.4000 ±1.6334 | | Llama-Guard-3-8B-Q4_K_M (Naive) | 66.0000 ±1.7309 | 80.67 | 38.5333 ±1.7783 | 31.6000 ±1.6988 | 72.2667 ±1.6358 | | [Llama-Guard-3-8B-Q4_K_S](./Llama-Guard-3-8B-Q4_K_S.gguf) | 64.4000 ±1.7496 | 81.73 | 37.6000 ±1.7699 | 32.5333 ±1.7119 | 72.6667 ±1.6284 | | [Llama-Guard-3-8B-Q5_K_M](./Llama-Guard-3-8B-Q5_K_M.gguf) | 66.4000 ±1.7259 | 81.47 | 38.5333 ±1.7783 | 33.4667 ±1.7242 | 73.2000 ±1.6184 | | [Llama-Guard-3-8B-Q5_K_S](./Llama-Guard-3-8B-Q5_K_S.gguf) | 65.7333 ±1.7342 | 81.33 | 38.2667 ±1.7759 | 33.0667 ±1.7190 | 73.4667 ±1.6132 | | [Llama-Guard-3-8B-Q6_K](./Llama-Guard-3-8B-Q6_K.gguf) | 66.9333 ±1.7190 | 82.00 | 38.5333 ±1.7783 | 33.7333 ±1.7276 | 73.4667 ±1.6132 | | [Llama-Guard-3-8B-Q8_0](./Llama-Guard-3-8B-Q8_0.gguf) | 66.0000 ±1.7309 | 81.73 | 37.8667 ±1.7724 | 33.0667 ±1.7190 | 73.7333 ±1.6080 | | [Llama-Guard-3-8B-F16](./Llama-Guard-3-8B-F16.gguf) | 64.9333 ±1.7436 | 81.60 | 38.1333 ±1.7748 | 30.6667 ±1.6849 | 72.8000 ±1.6260 | ### Tokens per Second - Benchmarks Scores generated using [llama-bench](). Naive (`llama-quantize` with no optimization) Q4_K_M quantization included for comparison. | model | size | params | backend | threads | test | t/s | | --------------------------------------------------------- | -------: | -----: | ---------- | ------: | ------------: | ------------: | | [Llama-Guard-3-8B-Q4_K_M](./Llama-Guard-3-8B-Q4_K_M.gguf) | 4.27 GiB | 8.03 B | Metal,BLAS | 6 | pp512 | 329.11 ± 0.74 | | [Llama-Guard-3-8B-Q4_K_M](./Llama-Guard-3-8B-Q4_K_M.gguf) | 4.27 GiB | 8.03 B | Metal,BLAS | 6 | tg128 | 26.59 ± 0.18 | | [Llama-Guard-3-8B-Q4_K_M](./Llama-Guard-3-8B-Q4_K_M.gguf) | 4.27 GiB | 8.03 B | Metal,BLAS | 6 | pp1024+tg1024 | 43.28 ± 0.14 | | Llama-Guard-3-8B-Q4_K_M (Naive) | 4.58 GiB | 8.03 B | Metal,BLAS | 6 | pp512 | 326.68 ± 5.05 | | Llama-Guard-3-8B-Q4_K_M (Naive) | 4.58 GiB | 8.03 B | Metal,BLAS | 6 | tg128 | 26.29 ± 0.30 | | Llama-Guard-3-8B-Q4_K_M (Naive) | 4.58 GiB | 8.03 B | Metal,BLAS | 6 | pp1024+tg1024 | 43.14 ± 0.22 | # Metrics used **[Perplexity]():** one of the key metrics used in NLP evaluation. It measures the quality of a language model by evaluating how well it predicts the next token given a particular sequence of words. A PPL of **1** indicates an exact match between predicted and actual, whereas values greater than one indicate a degree of "surprise" the generated token differs from the expected. **[Kullback–Leibler (KL) Divergence]():** a statistical measure of how much a probability distribution differs from another. When quantizing models (or altering the original tensors in any way for that matter), the closest we can preserve the weights' probability distribution to the orignal model the better, thus the closest to **0** the better. **[AI2 Reasoning Challenge (ARC)]():** a benchmark to evaluate the ability of AI models to answer complex science questions that require logical reasoning beyond pattern matching. **[HellaSwag]():** the Harder Endings, Longer contexts, and Low-shot Activities for Situations With Adversarial Generations (bit of a mouthful!) is a benchmark designed to test commonsense natural language inference. It requires the model to predict the most likely ending of a sentence. **[MMLU]():** the Massive Multitask Language Understanding evaluates LLMs’ general knowledge and problem-solving abilities across 57 subjects, including elementary mathematics, US history, computer science, and law. **[Truthful QA]():** evaluates how well LLMs generate truthful responses to questions. It identifies whether AI models can avoid generating false or misleading information, particularly in areas where human knowledge is prone to misconceptions. **[Winogrande]():** based on the [Winograd Schema Challenge](), is a natural language understanding task requiring models to resolve ambiguities in sentences involving pronoun references. ## Credits A big **Thank You!** to [Colin Kealty]() for the many contributions and for being one of the best sources of high quality quantized models available in Hugginface, and a really big ***Thank You!*** to [Georgi Gerganov]() for his amazing work with **llama.cpp** and the **gguf** file format.