Upload 10 files
Browse files- README.md +53 -3
- chat_template.jinja +24 -0
- config.json +41 -0
- config.yaml +45 -0
- generation_config.json +6 -0
- model.safetensors +3 -0
- special_tokens_map.json +23 -0
- tokenizer.json +0 -0
- tokenizer.model +3 -0
- tokenizer_config.json +44 -0
README.md
CHANGED
|
@@ -1,3 +1,53 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Mistral + LoRA Fine-Tuning
|
| 2 |
+
|
| 3 |
+
Lightweight fine-tuning setup for adapting a Mistral-architecture model using LoRA.
|
| 4 |
+
The project keeps everything modular: base model, adapters, training config, and generation settings.
|
| 5 |
+
|
| 6 |
+
## Features
|
| 7 |
+
- LoRA adapters applied to attention and MLP blocks
|
| 8 |
+
- Minimal training overhead with notebook-friendly resource use
|
| 9 |
+
- Drop-in loading for inference or further fine-tuning
|
| 10 |
+
|
| 11 |
+
## Requirements
|
| 12 |
+
```bash
|
| 13 |
+
pip install transformers accelerate peft datasets bitsandbytes
|
| 14 |
+
```
|
| 15 |
+
|
| 16 |
+
## Training
|
| 17 |
+
|
| 18 |
+
Start training with:
|
| 19 |
+
|
| 20 |
+
```bash
|
| 21 |
+
accelerate launch train.py --config config.yaml
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
## Inference
|
| 25 |
+
|
| 26 |
+
```python
|
| 27 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 28 |
+
from peft import PeftModel
|
| 29 |
+
|
| 30 |
+
base = "mistral-base"
|
| 31 |
+
lora = "./lora-out"
|
| 32 |
+
|
| 33 |
+
tok = AutoTokenizer.from_pretrained(base)
|
| 34 |
+
model = AutoModelForCausalLM.from_pretrained(base, device_map="auto")
|
| 35 |
+
model = PeftModel.from_pretrained(model, lora)
|
| 36 |
+
|
| 37 |
+
prompt = "Your prompt here"
|
| 38 |
+
out = model.generate(
|
| 39 |
+
**tok(prompt, return_tensors="pt").to(model.device),
|
| 40 |
+
max_new_tokens=256
|
| 41 |
+
)
|
| 42 |
+
print(tok.decode(out[0]))
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
## Files
|
| 46 |
+
- `config.yaml` – training + LoRA configuration
|
| 47 |
+
- `train.py` – simple training harness
|
| 48 |
+
- `lora-out/` – adapter weights after training
|
| 49 |
+
- `generation_config.json` – generation defaults
|
| 50 |
+
- `tokenizer_config.json` / `special_tokens_map.json` – tokenizer metadata
|
| 51 |
+
|
| 52 |
+
## License
|
| 53 |
+
MIT
|
chat_template.jinja
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{%- if messages[0]['role'] == 'system' %}
|
| 2 |
+
{%- set system_message = messages[0]['content'] %}
|
| 3 |
+
{%- set loop_messages = messages[1:] %}
|
| 4 |
+
{%- else %}
|
| 5 |
+
{%- set loop_messages = messages %}
|
| 6 |
+
{%- endif %}
|
| 7 |
+
|
| 8 |
+
{{- bos_token }}
|
| 9 |
+
{%- for message in loop_messages %}
|
| 10 |
+
{%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}
|
| 11 |
+
{{- raise_exception('After the optional system message, conversation roles must alternate user/assistant/user/assistant/...') }}
|
| 12 |
+
{%- endif %}
|
| 13 |
+
{%- if message['role'] == 'user' %}
|
| 14 |
+
{%- if loop.first and system_message is defined %}
|
| 15 |
+
{{- ' [INST] ' + system_message + '\n\n' + message['content'] + ' [/INST]' }}
|
| 16 |
+
{%- else %}
|
| 17 |
+
{{- ' [INST] ' + message['content'] + ' [/INST]' }}
|
| 18 |
+
{%- endif %}
|
| 19 |
+
{%- elif message['role'] == 'assistant' %}
|
| 20 |
+
{{- ' ' + message['content'] + eos_token}}
|
| 21 |
+
{%- else %}
|
| 22 |
+
{{- raise_exception('Only user and assistant roles are supported, with the exception of an initial optional system message!') }}
|
| 23 |
+
{%- endif %}
|
| 24 |
+
{%- endfor %}
|
config.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"MistralForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"attention_dropout": 0.0,
|
| 6 |
+
"bos_token_id": 1,
|
| 7 |
+
"dtype": "float16",
|
| 8 |
+
"eos_token_id": 2,
|
| 9 |
+
"head_dim": null,
|
| 10 |
+
"hidden_act": "silu",
|
| 11 |
+
"hidden_size": 4096,
|
| 12 |
+
"initializer_range": 0.02,
|
| 13 |
+
"intermediate_size": 14336,
|
| 14 |
+
"max_position_embeddings": 32768,
|
| 15 |
+
"model_type": "mistral",
|
| 16 |
+
"num_attention_heads": 32,
|
| 17 |
+
"num_hidden_layers": 32,
|
| 18 |
+
"num_key_value_heads": 8,
|
| 19 |
+
"quantization_config": {
|
| 20 |
+
"_load_in_4bit": true,
|
| 21 |
+
"_load_in_8bit": false,
|
| 22 |
+
"bnb_4bit_compute_dtype": "float16",
|
| 23 |
+
"bnb_4bit_quant_storage": "uint8",
|
| 24 |
+
"bnb_4bit_quant_type": "nf4",
|
| 25 |
+
"bnb_4bit_use_double_quant": false,
|
| 26 |
+
"llm_int8_enable_fp32_cpu_offload": false,
|
| 27 |
+
"llm_int8_has_fp16_weight": false,
|
| 28 |
+
"llm_int8_skip_modules": null,
|
| 29 |
+
"llm_int8_threshold": 6.0,
|
| 30 |
+
"load_in_4bit": true,
|
| 31 |
+
"load_in_8bit": false,
|
| 32 |
+
"quant_method": "bitsandbytes"
|
| 33 |
+
},
|
| 34 |
+
"rms_norm_eps": 1e-05,
|
| 35 |
+
"rope_theta": 1000000.0,
|
| 36 |
+
"sliding_window": null,
|
| 37 |
+
"tie_word_embeddings": false,
|
| 38 |
+
"transformers_version": "4.57.1",
|
| 39 |
+
"use_cache": true,
|
| 40 |
+
"vocab_size": 32000
|
| 41 |
+
}
|
config.yaml
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Base model + tokenizer
|
| 2 |
+
model_name: "mistral-base"
|
| 3 |
+
tokenizer_name: "mistral-base"
|
| 4 |
+
|
| 5 |
+
lora:
|
| 6 |
+
r: 32
|
| 7 |
+
alpha: 64
|
| 8 |
+
dropout: 0.05
|
| 9 |
+
target_modules:
|
| 10 |
+
- "q_proj"
|
| 11 |
+
- "k_proj"
|
| 12 |
+
- "v_proj"
|
| 13 |
+
- "o_proj"
|
| 14 |
+
- "gate_proj"
|
| 15 |
+
- "up_proj"
|
| 16 |
+
- "down_proj"
|
| 17 |
+
|
| 18 |
+
training:
|
| 19 |
+
output_dir: "./lora-out"
|
| 20 |
+
batch_size: 4
|
| 21 |
+
micro_batch_size: 1
|
| 22 |
+
gradient_accumulation_steps: 4
|
| 23 |
+
learning_rate: 2e-4
|
| 24 |
+
weight_decay: 0.0
|
| 25 |
+
warmup_ratio: 0.03
|
| 26 |
+
num_epochs: 3
|
| 27 |
+
max_grad_norm: 1.0
|
| 28 |
+
fp16: true
|
| 29 |
+
|
| 30 |
+
dataset:
|
| 31 |
+
path: "./data"
|
| 32 |
+
field: "text"
|
| 33 |
+
shuffle: true
|
| 34 |
+
num_workers: 4
|
| 35 |
+
|
| 36 |
+
generation:
|
| 37 |
+
bos_token_id: 1
|
| 38 |
+
eos_token_id: 2
|
| 39 |
+
max_new_tokens: 256
|
| 40 |
+
temperature: 0.7
|
| 41 |
+
top_p: 0.9
|
| 42 |
+
|
| 43 |
+
logging:
|
| 44 |
+
steps: 10
|
| 45 |
+
save_steps: 200
|
generation_config.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"bos_token_id": 1,
|
| 4 |
+
"eos_token_id": 2,
|
| 5 |
+
"transformers_version": "4.57.1"
|
| 6 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a1acc43f03937c7664bdf5f560e1e9992b44fd4066e6b289d8fed990e9c722f0
|
| 3 |
+
size 4450837496
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token": {
|
| 3 |
+
"content": "<s>",
|
| 4 |
+
"lstrip": false,
|
| 5 |
+
"normalized": false,
|
| 6 |
+
"rstrip": false,
|
| 7 |
+
"single_word": false
|
| 8 |
+
},
|
| 9 |
+
"eos_token": {
|
| 10 |
+
"content": "</s>",
|
| 11 |
+
"lstrip": false,
|
| 12 |
+
"normalized": false,
|
| 13 |
+
"rstrip": false,
|
| 14 |
+
"single_word": false
|
| 15 |
+
},
|
| 16 |
+
"unk_token": {
|
| 17 |
+
"content": "<unk>",
|
| 18 |
+
"lstrip": false,
|
| 19 |
+
"normalized": false,
|
| 20 |
+
"rstrip": false,
|
| 21 |
+
"single_word": false
|
| 22 |
+
}
|
| 23 |
+
}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer.model
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:dadfd56d766715c61d2ef780a525ab43b8e6da4de6865bda3d95fdef5e134055
|
| 3 |
+
size 493443
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_bos_token": true,
|
| 3 |
+
"add_eos_token": false,
|
| 4 |
+
"add_prefix_space": null,
|
| 5 |
+
"added_tokens_decoder": {
|
| 6 |
+
"0": {
|
| 7 |
+
"content": "<unk>",
|
| 8 |
+
"lstrip": false,
|
| 9 |
+
"normalized": false,
|
| 10 |
+
"rstrip": false,
|
| 11 |
+
"single_word": false,
|
| 12 |
+
"special": true
|
| 13 |
+
},
|
| 14 |
+
"1": {
|
| 15 |
+
"content": "<s>",
|
| 16 |
+
"lstrip": false,
|
| 17 |
+
"normalized": false,
|
| 18 |
+
"rstrip": false,
|
| 19 |
+
"single_word": false,
|
| 20 |
+
"special": true
|
| 21 |
+
},
|
| 22 |
+
"2": {
|
| 23 |
+
"content": "</s>",
|
| 24 |
+
"lstrip": false,
|
| 25 |
+
"normalized": false,
|
| 26 |
+
"rstrip": false,
|
| 27 |
+
"single_word": false,
|
| 28 |
+
"special": true
|
| 29 |
+
}
|
| 30 |
+
},
|
| 31 |
+
"additional_special_tokens": [],
|
| 32 |
+
"bos_token": "<s>",
|
| 33 |
+
"clean_up_tokenization_spaces": false,
|
| 34 |
+
"eos_token": "</s>",
|
| 35 |
+
"extra_special_tokens": {},
|
| 36 |
+
"legacy": false,
|
| 37 |
+
"model_max_length": 1000000000000000019884624838656,
|
| 38 |
+
"pad_token": null,
|
| 39 |
+
"sp_model_kwargs": {},
|
| 40 |
+
"spaces_between_special_tokens": false,
|
| 41 |
+
"tokenizer_class": "LlamaTokenizer",
|
| 42 |
+
"unk_token": "<unk>",
|
| 43 |
+
"use_default_system_prompt": false
|
| 44 |
+
}
|