Add BitsAndBytesConfig NF4 quantized Gemma-3-1B-IT model
Browse files- .gitattributes +1 -0
- README.md +86 -0
- chat_template.jinja +47 -0
- config.json +77 -0
- generation_config.json +11 -0
- model.safetensors +3 -0
- special_tokens_map.json +33 -0
- tokenizer.json +3 -0
- tokenizer_config.json +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
license: apache-2.0
|
| 5 |
+
library_name: transformers
|
| 6 |
+
tags:
|
| 7 |
+
- text-generation
|
| 8 |
+
- gemma
|
| 9 |
+
- quantized
|
| 10 |
+
- bnb
|
| 11 |
+
- nf4
|
| 12 |
+
base_model: google/gemma-3-1b-it-qat-int4-unquantized
|
| 13 |
+
pipeline_tag: text-generation
|
| 14 |
+
---
|
| 15 |
+
|
| 16 |
+
# Gemma-3-1B-IT BitsAndBytesConfig NF4 Quantized
|
| 17 |
+
|
| 18 |
+
This model is a quantized version of `google/gemma-3-1b-it-qat-int4-unquantized` using BitsAndBytesConfig with NF4 quantization.
|
| 19 |
+
|
| 20 |
+
## Model Details
|
| 21 |
+
|
| 22 |
+
- **Base Model**: google/gemma-3-1b-it-qat-int4-unquantized
|
| 23 |
+
- **Quantization**: BitsAndBytesConfig NF4 (4-bit)
|
| 24 |
+
- **Quantization Type**: NF4 with double quantization
|
| 25 |
+
- **Compute Dtype**: bfloat16
|
| 26 |
+
- **Storage Dtype**: uint8
|
| 27 |
+
|
| 28 |
+
## Quantization Configuration
|
| 29 |
+
|
| 30 |
+
```python
|
| 31 |
+
from transformers import BitsAndBytesConfig
|
| 32 |
+
|
| 33 |
+
bnb_config = BitsAndBytesConfig(
|
| 34 |
+
load_in_4bit=True,
|
| 35 |
+
bnb_4bit_quant_type="nf4",
|
| 36 |
+
bnb_4bit_use_double_quant=True,
|
| 37 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 38 |
+
bnb_4bit_quant_storage=torch.uint8
|
| 39 |
+
)
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
## Usage
|
| 43 |
+
|
| 44 |
+
```python
|
| 45 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 46 |
+
import torch
|
| 47 |
+
|
| 48 |
+
# Load the quantized model
|
| 49 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 50 |
+
"WaveCut/gemma-3-1b-it-qat-int4-bnb-nf4",
|
| 51 |
+
device_map="auto",
|
| 52 |
+
torch_dtype=torch.bfloat16,
|
| 53 |
+
trust_remote_code=True
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
tokenizer = AutoTokenizer.from_pretrained("WaveCut/gemma-3-1b-it-qat-int4-bnb-nf4")
|
| 57 |
+
|
| 58 |
+
# Generate text
|
| 59 |
+
inputs = tokenizer("Hello, how are you?", return_tensors="pt")
|
| 60 |
+
outputs = model.generate(**inputs, max_length=100)
|
| 61 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 62 |
+
print(response)
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
## Benefits
|
| 66 |
+
|
| 67 |
+
- **Reduced Memory Usage**: ~75% reduction in memory footprint compared to full precision
|
| 68 |
+
- **Faster Inference**: Optimized for inference speed
|
| 69 |
+
- **Maintained Quality**: NF4 quantization preserves model quality effectively
|
| 70 |
+
|
| 71 |
+
## Hardware Requirements
|
| 72 |
+
|
| 73 |
+
- **GPU Memory**: ~3-4GB VRAM (vs ~12GB for FP16)
|
| 74 |
+
- **CUDA Compatible**: Requires CUDA-capable GPU for optimal performance
|
| 75 |
+
- **CPU Fallback**: Can run on CPU with reduced performance
|
| 76 |
+
|
| 77 |
+
## Quantization Details
|
| 78 |
+
|
| 79 |
+
This model uses BitsAndBytesConfig for 4-bit quantization:
|
| 80 |
+
- NF4 (Normal Float 4) quantization for optimal quality/size trade-off
|
| 81 |
+
- Double quantization for additional compression
|
| 82 |
+
- Mixed precision with bfloat16 compute dtype
|
| 83 |
+
|
| 84 |
+
## License
|
| 85 |
+
|
| 86 |
+
This model inherits the Apache 2.0 license from the base model.
|
chat_template.jinja
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{{ bos_token }}
|
| 2 |
+
{%- if messages[0]['role'] == 'system' -%}
|
| 3 |
+
{%- if messages[0]['content'] is string -%}
|
| 4 |
+
{%- set first_user_prefix = messages[0]['content'] + '
|
| 5 |
+
|
| 6 |
+
' -%}
|
| 7 |
+
{%- else -%}
|
| 8 |
+
{%- set first_user_prefix = messages[0]['content'][0]['text'] + '
|
| 9 |
+
|
| 10 |
+
' -%}
|
| 11 |
+
{%- endif -%}
|
| 12 |
+
{%- set loop_messages = messages[1:] -%}
|
| 13 |
+
{%- else -%}
|
| 14 |
+
{%- set first_user_prefix = "" -%}
|
| 15 |
+
{%- set loop_messages = messages -%}
|
| 16 |
+
{%- endif -%}
|
| 17 |
+
{%- for message in loop_messages -%}
|
| 18 |
+
{%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) -%}
|
| 19 |
+
{{ raise_exception("Conversation roles must alternate user/assistant/user/assistant/...") }}
|
| 20 |
+
{%- endif -%}
|
| 21 |
+
{%- if (message['role'] == 'assistant') -%}
|
| 22 |
+
{%- set role = "model" -%}
|
| 23 |
+
{%- else -%}
|
| 24 |
+
{%- set role = message['role'] -%}
|
| 25 |
+
{%- endif -%}
|
| 26 |
+
{{ '<start_of_turn>' + role + '
|
| 27 |
+
' + (first_user_prefix if loop.first else "") }}
|
| 28 |
+
{%- if message['content'] is string -%}
|
| 29 |
+
{{ message['content'] | trim }}
|
| 30 |
+
{%- elif message['content'] is iterable -%}
|
| 31 |
+
{%- for item in message['content'] -%}
|
| 32 |
+
{%- if item['type'] == 'image' -%}
|
| 33 |
+
{{ '<start_of_image>' }}
|
| 34 |
+
{%- elif item['type'] == 'text' -%}
|
| 35 |
+
{{ item['text'] | trim }}
|
| 36 |
+
{%- endif -%}
|
| 37 |
+
{%- endfor -%}
|
| 38 |
+
{%- else -%}
|
| 39 |
+
{{ raise_exception("Invalid content type") }}
|
| 40 |
+
{%- endif -%}
|
| 41 |
+
{{ '<end_of_turn>
|
| 42 |
+
' }}
|
| 43 |
+
{%- endfor -%}
|
| 44 |
+
{%- if add_generation_prompt -%}
|
| 45 |
+
{{'<start_of_turn>model
|
| 46 |
+
'}}
|
| 47 |
+
{%- endif -%}
|
config.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"Gemma3ForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"attention_bias": false,
|
| 6 |
+
"attention_dropout": 0.0,
|
| 7 |
+
"attn_logit_softcapping": null,
|
| 8 |
+
"bos_token_id": 2,
|
| 9 |
+
"cache_implementation": "hybrid",
|
| 10 |
+
"eos_token_id": 1,
|
| 11 |
+
"final_logit_softcapping": null,
|
| 12 |
+
"head_dim": 256,
|
| 13 |
+
"hidden_activation": "gelu_pytorch_tanh",
|
| 14 |
+
"hidden_size": 1152,
|
| 15 |
+
"initializer_range": 0.02,
|
| 16 |
+
"intermediate_size": 6912,
|
| 17 |
+
"layer_types": [
|
| 18 |
+
"sliding_attention",
|
| 19 |
+
"sliding_attention",
|
| 20 |
+
"sliding_attention",
|
| 21 |
+
"sliding_attention",
|
| 22 |
+
"sliding_attention",
|
| 23 |
+
"full_attention",
|
| 24 |
+
"sliding_attention",
|
| 25 |
+
"sliding_attention",
|
| 26 |
+
"sliding_attention",
|
| 27 |
+
"sliding_attention",
|
| 28 |
+
"sliding_attention",
|
| 29 |
+
"full_attention",
|
| 30 |
+
"sliding_attention",
|
| 31 |
+
"sliding_attention",
|
| 32 |
+
"sliding_attention",
|
| 33 |
+
"sliding_attention",
|
| 34 |
+
"sliding_attention",
|
| 35 |
+
"full_attention",
|
| 36 |
+
"sliding_attention",
|
| 37 |
+
"sliding_attention",
|
| 38 |
+
"sliding_attention",
|
| 39 |
+
"sliding_attention",
|
| 40 |
+
"sliding_attention",
|
| 41 |
+
"full_attention",
|
| 42 |
+
"sliding_attention",
|
| 43 |
+
"sliding_attention"
|
| 44 |
+
],
|
| 45 |
+
"max_position_embeddings": 32768,
|
| 46 |
+
"model_type": "gemma3_text",
|
| 47 |
+
"num_attention_heads": 4,
|
| 48 |
+
"num_hidden_layers": 26,
|
| 49 |
+
"num_key_value_heads": 1,
|
| 50 |
+
"pad_token_id": 0,
|
| 51 |
+
"quantization_config": {
|
| 52 |
+
"_load_in_4bit": true,
|
| 53 |
+
"_load_in_8bit": false,
|
| 54 |
+
"bnb_4bit_compute_dtype": "bfloat16",
|
| 55 |
+
"bnb_4bit_quant_storage": "uint8",
|
| 56 |
+
"bnb_4bit_quant_type": "nf4",
|
| 57 |
+
"bnb_4bit_use_double_quant": true,
|
| 58 |
+
"llm_int8_enable_fp32_cpu_offload": false,
|
| 59 |
+
"llm_int8_has_fp16_weight": false,
|
| 60 |
+
"llm_int8_skip_modules": null,
|
| 61 |
+
"llm_int8_threshold": 6.0,
|
| 62 |
+
"load_in_4bit": true,
|
| 63 |
+
"load_in_8bit": false,
|
| 64 |
+
"quant_method": "bitsandbytes"
|
| 65 |
+
},
|
| 66 |
+
"query_pre_attn_scalar": 256,
|
| 67 |
+
"rms_norm_eps": 1e-06,
|
| 68 |
+
"rope_local_base_freq": 10000,
|
| 69 |
+
"rope_scaling": null,
|
| 70 |
+
"rope_theta": 1000000,
|
| 71 |
+
"sliding_window": 512,
|
| 72 |
+
"sliding_window_pattern": 6,
|
| 73 |
+
"torch_dtype": "bfloat16",
|
| 74 |
+
"transformers_version": "4.53.0",
|
| 75 |
+
"use_cache": true,
|
| 76 |
+
"vocab_size": 262144
|
| 77 |
+
}
|
generation_config.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cache_implementation": "hybrid",
|
| 3 |
+
"do_sample": true,
|
| 4 |
+
"eos_token_id": [
|
| 5 |
+
1,
|
| 6 |
+
106
|
| 7 |
+
],
|
| 8 |
+
"top_k": 64,
|
| 9 |
+
"top_p": 0.95,
|
| 10 |
+
"transformers_version": "4.53.0"
|
| 11 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:55993432430feb93d1ed1ada0f26aa021372e5309304c2c647ae0169d180ef82
|
| 3 |
+
size 964577521
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"boi_token": "<start_of_image>",
|
| 3 |
+
"bos_token": {
|
| 4 |
+
"content": "<bos>",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false
|
| 9 |
+
},
|
| 10 |
+
"eoi_token": "<end_of_image>",
|
| 11 |
+
"eos_token": {
|
| 12 |
+
"content": "<eos>",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false
|
| 17 |
+
},
|
| 18 |
+
"image_token": "<image_soft_token>",
|
| 19 |
+
"pad_token": {
|
| 20 |
+
"content": "<pad>",
|
| 21 |
+
"lstrip": false,
|
| 22 |
+
"normalized": false,
|
| 23 |
+
"rstrip": false,
|
| 24 |
+
"single_word": false
|
| 25 |
+
},
|
| 26 |
+
"unk_token": {
|
| 27 |
+
"content": "<unk>",
|
| 28 |
+
"lstrip": false,
|
| 29 |
+
"normalized": false,
|
| 30 |
+
"rstrip": false,
|
| 31 |
+
"single_word": false
|
| 32 |
+
}
|
| 33 |
+
}
|
tokenizer.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4667f2089529e8e7657cfb6d1c19910ae71ff5f28aa7ab2ff2763330affad795
|
| 3 |
+
size 33384568
|
tokenizer_config.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|