初始化项目,由ModelHub XC社区提供模型
Model: mzoelfakar/Al-Khwarizmi-3B Source: Original Platform
This commit is contained in:
38
.gitattributes
vendored
Normal file
38
.gitattributes
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
*.7z filter=lfs diff=lfs merge=lfs -text
|
||||
*.arrow filter=lfs diff=lfs merge=lfs -text
|
||||
*.bin filter=lfs diff=lfs merge=lfs -text
|
||||
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
||||
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
||||
*.ftz filter=lfs diff=lfs merge=lfs -text
|
||||
*.gz filter=lfs diff=lfs merge=lfs -text
|
||||
*.h5 filter=lfs diff=lfs merge=lfs -text
|
||||
*.joblib filter=lfs diff=lfs merge=lfs -text
|
||||
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
||||
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
||||
*.model filter=lfs diff=lfs merge=lfs -text
|
||||
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
||||
*.npy filter=lfs diff=lfs merge=lfs -text
|
||||
*.npz filter=lfs diff=lfs merge=lfs -text
|
||||
*.onnx filter=lfs diff=lfs merge=lfs -text
|
||||
*.ot filter=lfs diff=lfs merge=lfs -text
|
||||
*.parquet filter=lfs diff=lfs merge=lfs -text
|
||||
*.pb filter=lfs diff=lfs merge=lfs -text
|
||||
*.pickle filter=lfs diff=lfs merge=lfs -text
|
||||
*.pkl filter=lfs diff=lfs merge=lfs -text
|
||||
*.pt filter=lfs diff=lfs merge=lfs -text
|
||||
*.pth filter=lfs diff=lfs merge=lfs -text
|
||||
*.rar filter=lfs diff=lfs merge=lfs -text
|
||||
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
||||
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
||||
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
||||
*.tar filter=lfs diff=lfs merge=lfs -text
|
||||
*.tflite filter=lfs diff=lfs merge=lfs -text
|
||||
*.tgz filter=lfs diff=lfs merge=lfs -text
|
||||
*.wasm filter=lfs diff=lfs merge=lfs -text
|
||||
*.xz filter=lfs diff=lfs merge=lfs -text
|
||||
*.zip filter=lfs diff=lfs merge=lfs -text
|
||||
*.zst filter=lfs diff=lfs merge=lfs -text
|
||||
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
||||
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
||||
Al-Khwarizmi-3B-bf16.gguf filter=lfs diff=lfs merge=lfs -text
|
||||
Al-Khwarizmi-3B-Q8_0.gguf filter=lfs diff=lfs merge=lfs -text
|
||||
100
README.md
Normal file
100
README.md
Normal file
@@ -0,0 +1,100 @@
|
||||
---
|
||||
license: apache-2.0
|
||||
base_model: HuggingFaceTB/SmolLM3-3B-Base
|
||||
base_model_relation: finetune
|
||||
tags:
|
||||
- smollm3
|
||||
- fine-tuned
|
||||
- lora
|
||||
- math
|
||||
- conversational
|
||||
- text-generation-inference
|
||||
language:
|
||||
- en
|
||||
datasets:
|
||||
- openai/gsm8k
|
||||
---
|
||||
|
||||
# Al-Khwarizmi-3B
|
||||
|
||||
*An AI math tutor named after Muhammad al-Khwarizmi, the 9th-century mathematician whose name is the origin of the word "algorithm".*
|
||||
|
||||
Fine-tune of **HuggingFaceTB/SmolLM3-3B-Base**, trained in two stages — full fine-tuning followed by LoRA — to solve grade-school math word problems with clear, step-by-step reasoning.
|
||||
|
||||
## Highlights
|
||||
|
||||
- **87.21% mean token accuracy** on held-out validation data — up from 84.75% after the initial full fine-tune, and 82.4% at the very first checkpoint
|
||||
- **Validation loss reduced by ~19%** across the full pipeline (0.678 → 0.462), with training and validation loss tracking closely throughout every stage — no overfitting observed
|
||||
- Trained on the complete GSM8K dataset (both `main` and `socratic` reasoning styles) across two LoRA passes, on top of an initial full fine-tune
|
||||
- Available in this repo as safetensors. **GGUF version with quantization** (BF16 and Q8_0) for efficient usage on CPU and a smaller size [**available here**](https://huggingface.co/mzoelfakar/Al-Khwarizmi-3B-GGUF).
|
||||
|
||||

|
||||

|
||||
|
||||
## Training Details
|
||||
|
||||
**Stage 1 — Full fine-tuning**
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| Dataset | GSM8K (`main`), 1,000 random samples, 90/10 train/val split |
|
||||
| Steps | 450 (1 epoch) |
|
||||
| Learning rate | 5e-5, cosine schedule |
|
||||
| Final validation loss / accuracy | 0.569 / 84.75% |
|
||||
|
||||
**Stage 2 — LoRA fine-tuning**
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| Method | LoRA, r=16, all-linear target modules |
|
||||
| Dataset | Full GSM8K — both `main` and `socratic` reasoning styles |
|
||||
| Steps | 3,550 (combined across two passes) |
|
||||
| Learning rate | 5e-5, cosine schedule |
|
||||
| Final validation loss / accuracy | 0.462 / 87.21% |
|
||||
|
||||
*Run as two consecutive passes over the dataset, with the `main`/`socratic` split swapped between them so every problem was seen in both reasoning styles.*
|
||||
|
||||
## Limitations
|
||||
|
||||
Fine-tuned primarily on GSM8K-style problems (single correct numeric answer, grade-school arithmetic/word problems) — performance on more complex, multi-part, or differently-structured math problems is untested. Occasional arithmetic slips on multi-step problems can still occur, consistent with known limitations of models at this scale.
|
||||
|
||||
## Usage
|
||||
|
||||
```python
|
||||
from transformers import AutoModelForCausalLM, AutoTokenizer
|
||||
import torch
|
||||
|
||||
model_name = "mzoelfakar/Al-Khwarizmi-3B"
|
||||
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
||||
model = AutoModelForCausalLM.from_pretrained(model_name, dtype=torch.bfloat16, device_map="auto")
|
||||
|
||||
messages = [
|
||||
{"role": "system", "content": "You are a math tutor. Solve problems step by step."},
|
||||
{"role": "user", "content": "If a train travels 120 miles in 2 hours, what is its average speed?"}
|
||||
]
|
||||
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
||||
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
||||
outputs = model.generate(**inputs, max_new_tokens=300, temperature=0.7, do_sample=True)
|
||||
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
||||
```
|
||||
|
||||
### Note on raw output formatting
|
||||
|
||||
Because this model was fine-tuned on GSM8K (including the `socratic` reasoning style), raw generations may contain training artifacts not meant for direct display:
|
||||
|
||||
- `<<...>>` — calculator-style intermediate annotations
|
||||
- `**` — separator between a sub-question and its calculation in Socratic-style reasoning (not Markdown bold)
|
||||
- `#### <answer>` — marker preceding the final numeric answer
|
||||
- `*` — used as a multiplication sign (e.g. `8*9`); if two or more appear in the same
|
||||
response, Markdown may pair them as emphasis delimiters, causing text between them
|
||||
to render in italic with the asterisks hidden
|
||||
|
||||
If you're piping output through a Markdown renderer or displaying it in a UI, you'll likely want to strip or reformat these first, since `**` in particular can be misread as Markdown bold syntax if left unescaped.
|
||||
|
||||
## Try it online
|
||||
|
||||
A live chat demo is available via Colab: [Al-Khwarizmi-3B.ipynb](https://colab.research.google.com/github/mzoelfakar/Al-Khwarizmi-3B/blob/main/Al-Khwarizmi-3B.ipynb)
|
||||
|
||||
## Credits
|
||||
|
||||
Fine-tuned by [Mohamed Zoelfakar](https://www.linkedin.com/in/mzoelfakar/), as part of Hugging Face's [smol-course](https://huggingface.co/learn/smol-course/).
|
||||
94
chat_template.jinja
Normal file
94
chat_template.jinja
Normal file
@@ -0,0 +1,94 @@
|
||||
{# ───── defaults ───── #}
|
||||
{%- if enable_thinking is not defined -%}
|
||||
{%- set enable_thinking = true -%}
|
||||
{%- endif -%}
|
||||
|
||||
{# ───── reasoning mode ───── #}
|
||||
{%- if enable_thinking -%}
|
||||
{%- set reasoning_mode = "/think" -%}
|
||||
{%- else -%}
|
||||
{%- set reasoning_mode = "/no_think" -%}
|
||||
{%- endif -%}
|
||||
|
||||
{# ───── header (system message) ───── #}
|
||||
{{- "<|im_start|>system\n" -}}
|
||||
|
||||
{%- if messages[0].role == "system" -%}
|
||||
{%- set system_message = messages[0].content -%}
|
||||
{%- if "/no_think" in system_message -%}
|
||||
{%- set reasoning_mode = "/no_think" -%}
|
||||
{%- elif "/think" in system_message -%}
|
||||
{%- set reasoning_mode = "/think" -%}
|
||||
{%- endif -%}
|
||||
{%- set custom_instructions = system_message.replace("/no_think", "").replace("/think", "").rstrip() -%}
|
||||
{%- endif -%}
|
||||
|
||||
{%- if "/system_override" in system_message -%}
|
||||
{{- custom_instructions.replace("/system_override", "").rstrip() -}}
|
||||
{{- "<|im_end|>\n" -}}
|
||||
{%- else -%}
|
||||
{{- "## Metadata\n\n" -}}
|
||||
{{- "Knowledge Cutoff Date: June 2025\n" -}}
|
||||
{%- set today = strftime_now("%d %B %Y") -%}
|
||||
{{- "Today Date: " ~ today ~ "\n" -}}
|
||||
{{- "Reasoning Mode: " + reasoning_mode + "\n\n" -}}
|
||||
|
||||
{{- "## Custom Instructions\n\n" -}}
|
||||
{%- if custom_instructions -%}
|
||||
{{- custom_instructions + "\n\n" -}}
|
||||
{%- elif reasoning_mode == "/think" -%}
|
||||
{{- "You are a helpful AI assistant named SmolLM, trained by Hugging Face. Your role as an assistant involves thoroughly exploring questions through a systematic thinking process before providing the final precise and accurate solutions. This requires engaging in a comprehensive cycle of analysis, summarizing, exploration, reassessment, reflection, backtracking, and iteration to develop well-considered thinking process. Please structure your response into two main sections: Thought and Solution using the specified format: <think> Thought section </think> Solution section. In the Thought section, detail your reasoning process in steps. Each step should include detailed considerations such as analysing questions, summarizing relevant findings, brainstorming new ideas, verifying the accuracy of the current steps, refining any errors, and revisiting previous steps. In the Solution section, based on various attempts, explorations, and reflections from the Thought section, systematically present the final solution that you deem correct. The Solution section should be logical, accurate, and concise and detail necessary steps needed to reach the conclusion.\n\n" -}}
|
||||
{%- else -%}
|
||||
{{- "You are a helpful AI assistant named SmolLM, trained by Hugging Face.\n\n" -}}
|
||||
{%- endif -%}
|
||||
|
||||
{%- if xml_tools or python_tools or tools -%}
|
||||
{{- "### Tools\n\n" -}}
|
||||
{%- if xml_tools or tools -%}
|
||||
{%- if tools -%}
|
||||
{%- set xml_tools = tools -%}
|
||||
{%- endif -%}
|
||||
{%- set ns = namespace(xml_tool_string="You may call one or more functions to assist with the user query.\nYou are provided with function signatures within <tools></tools> XML tags:\n\n<tools>\n") -%}
|
||||
{%- for tool in xml_tools[:] -%} {# The slicing makes sure that xml_tools is a list #}
|
||||
{%- set ns.xml_tool_string = ns.xml_tool_string ~ (tool | string) ~ "\n" -%}
|
||||
{%- endfor -%}
|
||||
{%- set xml_tool_string = ns.xml_tool_string + "</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call>" -%}
|
||||
{{- xml_tool_string -}}
|
||||
{%- endif -%}
|
||||
{%- if python_tools -%}
|
||||
{%- set ns = namespace(python_tool_string="When you send a message containing Python code between '<code>' and '</code>' tags, it will be executed in a stateful Jupyter notebook environment, and you will then be given the output to continued reasoning in an agentic loop.\n\nYou can use the following tools in your python code like regular functions:\n<tools>\n") -%}
|
||||
{%- for tool in python_tools[:] -%} {# The slicing makes sure that python_tools is a list #}
|
||||
{%- set ns.python_tool_string = ns.python_tool_string ~ (tool | string) ~ "\n" -%}
|
||||
{%- endfor -%}
|
||||
{%- set python_tool_string = ns.python_tool_string + "</tools>\n\nThe state persists between code executions: so variables that you define in one step are still available thereafter." -%}
|
||||
{{- python_tool_string -}}
|
||||
{%- endif -%}
|
||||
{{- "\n\n" -}}
|
||||
{{- "<|im_end|>\n" -}}
|
||||
{%- endif -%}
|
||||
{%- endif -%}
|
||||
{# ───── main loop ───── #}
|
||||
{%- for message in messages -%}
|
||||
{%- set content = message.content if message.content is string else "" -%}
|
||||
{%- if message.role == "user" -%}
|
||||
{{ "<|im_start|>" + message.role + "\n" + content + "<|im_end|>\n" }}
|
||||
{%- elif message.role == "assistant" -%}
|
||||
{% generation %}
|
||||
{%- if reasoning_mode == "/think" -%}
|
||||
{{ "<|im_start|>assistant\n" + content.lstrip("\n") + "<|im_end|>\n" }}
|
||||
{%- else -%}
|
||||
{{ "<|im_start|>assistant\n" + "<think>\n\n</think>\n" + content.lstrip("\n") + "<|im_end|>\n" }}
|
||||
{%- endif -%}
|
||||
{% endgeneration %}
|
||||
{%- elif message.role == "tool" -%}
|
||||
{{ "<|im_start|>" + "user\n" + content + "<|im_end|>\n" }}
|
||||
{%- endif -%}
|
||||
{%- endfor -%}
|
||||
{# ───── generation prompt ───── #}
|
||||
{%- if add_generation_prompt -%}
|
||||
{%- if reasoning_mode == "/think" -%}
|
||||
{{ "<|im_start|>assistant\n" }}
|
||||
{%- else -%}
|
||||
{{ "<|im_start|>assistant\n" + "<think>\n\n</think>\n" }}
|
||||
{%- endif -%}
|
||||
{%- endif -%}
|
||||
111
config.json
Normal file
111
config.json
Normal file
@@ -0,0 +1,111 @@
|
||||
{
|
||||
"architectures": [
|
||||
"SmolLM3ForCausalLM"
|
||||
],
|
||||
"attention_bias": false,
|
||||
"attention_dropout": 0.0,
|
||||
"bos_token_id": null,
|
||||
"dtype": "bfloat16",
|
||||
"eos_token_id": 128012,
|
||||
"hidden_act": "silu",
|
||||
"hidden_size": 2048,
|
||||
"initializer_range": 0.02,
|
||||
"intermediate_size": 11008,
|
||||
"layer_types": [
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention",
|
||||
"full_attention"
|
||||
],
|
||||
"max_position_embeddings": 65536,
|
||||
"max_window_layers": 28,
|
||||
"mlp_bias": false,
|
||||
"model_type": "smollm3",
|
||||
"no_rope_layer_interval": 4,
|
||||
"no_rope_layers": [
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
0
|
||||
],
|
||||
"num_attention_heads": 16,
|
||||
"num_hidden_layers": 36,
|
||||
"num_key_value_heads": 4,
|
||||
"pad_token_id": 128012,
|
||||
"pretraining_tp": 1,
|
||||
"rms_norm_eps": 1e-06,
|
||||
"rope_parameters": {
|
||||
"rope_theta": 5000000.0,
|
||||
"rope_type": "default"
|
||||
},
|
||||
"sliding_window": null,
|
||||
"tie_word_embeddings": true,
|
||||
"transformers_version": "5.14.1",
|
||||
"use_cache": false,
|
||||
"use_sliding_window": false,
|
||||
"vocab_size": 128256
|
||||
}
|
||||
10
generation_config.json
Normal file
10
generation_config.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"do_sample": true,
|
||||
"eos_token_id": [
|
||||
128012
|
||||
],
|
||||
"pad_token_id": 128012,
|
||||
"temperature": 0.6,
|
||||
"top_p": 0.95,
|
||||
"transformers_version": "5.14.1"
|
||||
}
|
||||
BIN
lora_loss_chart.png
Normal file
BIN
lora_loss_chart.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 74 KiB |
BIN
loss_chart.png
Normal file
BIN
loss_chart.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 59 KiB |
3
model.safetensors
Normal file
3
model.safetensors
Normal file
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:eeb76f3014402dfafa57de5586b49d4b6d744fa5044ff14636feac73e65304f0
|
||||
size 6150235096
|
||||
3
tokenizer.json
Normal file
3
tokenizer.json
Normal file
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:7b6a500b662a34eb3f0374db856ba4ad7de4c81040571d78dc0d357238930005
|
||||
size 17208819
|
||||
15
tokenizer_config.json
Normal file
15
tokenizer_config.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"backend": "tokenizers",
|
||||
"bos_token": null,
|
||||
"clean_up_tokenization_spaces": true,
|
||||
"eos_token": "<|im_end|>",
|
||||
"fast": false,
|
||||
"is_local": true,
|
||||
"local_files_only": false,
|
||||
"model_input_names": [
|
||||
"input_ids",
|
||||
"attention_mask"
|
||||
],
|
||||
"model_max_length": 131072,
|
||||
"pad_token": "<|im_end|>"
|
||||
}
|
||||
Reference in New Issue
Block a user