初始化项目,由ModelHub XC社区提供模型
Model: Tekimax/granite-ml-coder Source: Original Platform
This commit is contained in:
35
.gitattributes
vendored
Normal file
35
.gitattributes
vendored
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
*.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
|
||||||
104
README.md
Normal file
104
README.md
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
---
|
||||||
|
license: apache-2.0
|
||||||
|
base_model: ibm-granite/granite-3.1-1b-a400m-instruct
|
||||||
|
library_name: transformers
|
||||||
|
pipeline_tag: text-generation
|
||||||
|
tags:
|
||||||
|
- code
|
||||||
|
- machine-learning
|
||||||
|
- data-science
|
||||||
|
- scikit-learn
|
||||||
|
- fine-tuned
|
||||||
|
- granite
|
||||||
|
- mlx
|
||||||
|
- gguf
|
||||||
|
datasets:
|
||||||
|
- iamtarun/python_code_instructions_18k_alpaca
|
||||||
|
language:
|
||||||
|
- en
|
||||||
|
---
|
||||||
|
|
||||||
|
# granite-ml-coder
|
||||||
|
|
||||||
|
A compact **Python / machine-learning coding assistant**, fine-tuned from
|
||||||
|
[`ibm-granite/granite-3.1-1b-a400m-instruct`](https://huggingface.co/ibm-granite/granite-3.1-1b-a400m-instruct). It writes runnable
|
||||||
|
scikit-learn / pandas / NumPy code, explains ML pipeline steps, and reasons about
|
||||||
|
everyday concepts like overfitting, cross-validation, and gradient descent.
|
||||||
|
|
||||||
|
It is small enough to run **fully locally** — on a laptop CPU, via Ollama, or
|
||||||
|
quantized to GGUF — which makes it a good private copilot for data-science work
|
||||||
|
where your data shouldn't leave the machine.
|
||||||
|
|
||||||
|
> Built as the reference model for the **TEKIMAX ML Model Workshop**
|
||||||
|
> (fine-tune → host on HF/Ollama → build a production DL app).
|
||||||
|
|
||||||
|
## Quick start
|
||||||
|
|
||||||
|
### Transformers
|
||||||
|
```python
|
||||||
|
import torch
|
||||||
|
from transformers import AutoModelForCausalLM, AutoTokenizer
|
||||||
|
|
||||||
|
tok = AutoTokenizer.from_pretrained("Tekimax/granite-ml-coder")
|
||||||
|
model = AutoModelForCausalLM.from_pretrained(
|
||||||
|
"Tekimax/granite-ml-coder", dtype=torch.float32, attn_implementation="eager"
|
||||||
|
)
|
||||||
|
|
||||||
|
messages = [
|
||||||
|
{"role": "system", "content": "You are an expert Python machine-learning engineer."},
|
||||||
|
{"role": "user", "content": "Write a scikit-learn pipeline to classify the iris dataset and explain how you avoid overfitting."},
|
||||||
|
]
|
||||||
|
enc = tok.apply_chat_template(messages, add_generation_prompt=True,
|
||||||
|
return_tensors="pt", return_dict=True, enable_thinking=False)
|
||||||
|
out = model.generate(**enc, max_new_tokens=400, do_sample=True, temperature=0.7, top_p=0.9)
|
||||||
|
print(tok.decode(out[0][enc["input_ids"].shape[1]:], skip_special_tokens=True))
|
||||||
|
```
|
||||||
|
|
||||||
|
### Ollama (recommended for local use)
|
||||||
|
```bash
|
||||||
|
ollama run tekimaxllc/granite-ml-coder "Write a Keras autoencoder for network-traffic anomaly detection"
|
||||||
|
```
|
||||||
|
|
||||||
|
### GGUF / llama.cpp
|
||||||
|
A 4-bit `Q4_K_M` build (~378 MB) is available at
|
||||||
|
[`Tekimax/granite-ml-coder-GGUF`](https://huggingface.co/Tekimax/granite-ml-coder-GGUF):
|
||||||
|
```bash
|
||||||
|
llama-cli -m granite-ml-coder-Q4_K_M.gguf -p "Write a sklearn pipeline"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Intended use
|
||||||
|
|
||||||
|
- Drafting Python ML code (scikit-learn, pandas, NumPy, Keras) inside notebooks/IDEs
|
||||||
|
- Explaining ML pipeline steps and concepts (overfitting, gradient descent, model choice)
|
||||||
|
- A private, offline coding copilot for data-science tasks
|
||||||
|
|
||||||
|
## Training
|
||||||
|
|
||||||
|
| | |
|
||||||
|
|---|---|
|
||||||
|
| Base model | `ibm-granite/granite-3.1-1b-a400m-instruct` (Apache-2.0, IBM) — ~1.3B total / 400M active MoE |
|
||||||
|
| Data | [`iamtarun/python_code_instructions_18k_alpaca`](https://huggingface.co/datasets/iamtarun/python_code_instructions_18k_alpaca), filtered to ML/DS rows (≈2,341 examples) |
|
||||||
|
| Method | Full fine-tune, instruction format with the Granite chat template; loss computed on the assistant answer only (prompt tokens masked) |
|
||||||
|
| Schedule | 2 epochs · effective batch size 16 · LR 2e-5 cosine · max_len 512 |
|
||||||
|
| Hardware | Apple M2 Ultra, **CPU** (the MPS/Metal backend was unstable for fine-tuning on torch 2.12 — see the workshop appendix) |
|
||||||
|
| Result | training loss decreasing steadily, no NaN |
|
||||||
|
|
||||||
|
## Limitations
|
||||||
|
|
||||||
|
- **It's a 1B model.** It learns *style, format, and common patterns* well, but
|
||||||
|
is not a frontier model — it can produce incomplete or subtly wrong code, and
|
||||||
|
it won't reliably "pick the best model" for hard problems. Treat its output as
|
||||||
|
a fast first draft and verify before use.
|
||||||
|
- English, Python-focused. Strongest on classic ML (sklearn/pandas); weaker on
|
||||||
|
large, novel, or multi-file tasks.
|
||||||
|
- Inherits any biases/limitations of the base model and the training dataset.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Apache-2.0, inherited from the base model and dataset.
|
||||||
|
|
||||||
|
## Citation / credits
|
||||||
|
|
||||||
|
- Base model: [Granite](https://huggingface.co/ibm-granite/granite-3.1-1b-a400m-instruct)
|
||||||
|
- Dataset: [iamtarun/python_code_instructions_18k_alpaca](https://huggingface.co/datasets/iamtarun/python_code_instructions_18k_alpaca)
|
||||||
|
- Built with Hugging Face `transformers` `Trainer`; quantized with `llama.cpp`.
|
||||||
64
chat_template.jinja
Normal file
64
chat_template.jinja
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
{%- if messages[0]['role'] == 'system' %}
|
||||||
|
{%- set system_message = messages[0]['content'] %}
|
||||||
|
{%- set loop_messages = messages[1:] %}
|
||||||
|
{%- else %}
|
||||||
|
{%- set system_message = "Knowledge Cutoff Date: April 2024.
|
||||||
|
Today's Date: " + strftime_now('%B %d, %Y') + ".
|
||||||
|
You are Granite, developed by IBM." %}
|
||||||
|
{%- if tools and documents %}
|
||||||
|
{%- set system_message = system_message + " You are a helpful AI assistant with access to the following tools. When a tool is required to answer the user's query, respond with <|tool_call|> followed by a JSON list of tools used. If a tool does not exist in the provided list of tools, notify the user that you do not have the ability to fulfill the request.
|
||||||
|
|
||||||
|
Write the response to the user's input by strictly aligning with the facts in the provided documents. If the information needed to answer the question is not available in the documents, inform the user that the question cannot be answered based on the available data." %}
|
||||||
|
{%- elif tools %}
|
||||||
|
{%- set system_message = system_message + " You are a helpful AI assistant with access to the following tools. When a tool is required to answer the user's query, respond with <|tool_call|> followed by a JSON list of tools used. If a tool does not exist in the provided list of tools, notify the user that you do not have the ability to fulfill the request." %}
|
||||||
|
{%- elif documents %}
|
||||||
|
{%- set system_message = system_message + " Write the response to the user's input by strictly aligning with the facts in the provided documents. If the information needed to answer the question is not available in the documents, inform the user that the question cannot be answered based on the available data." %}
|
||||||
|
{%- else %}
|
||||||
|
{%- set system_message = system_message + " You are a helpful AI assistant." %}
|
||||||
|
{%- endif %}
|
||||||
|
{%- if 'citations' in controls and documents %}
|
||||||
|
{%- set system_message = system_message + '
|
||||||
|
|
||||||
|
In your response, use the symbols <co> and </co> to indicate when a fact comes from a document in the search result, e.g <co>0</co> for a fact from document 0. Afterwards, list all the citations with their corresponding documents in an ordered list.' %}
|
||||||
|
{%- endif %}
|
||||||
|
{%- if 'hallucinations' in controls and documents %}
|
||||||
|
{%- set system_message = system_message + '
|
||||||
|
|
||||||
|
Finally, after the response is written, include a numbered list of sentences from the response that are potentially hallucinated and not based in the documents.' %}
|
||||||
|
{%- endif %}
|
||||||
|
{%- set loop_messages = messages %}
|
||||||
|
{%- endif %}
|
||||||
|
{{- '<|start_of_role|>system<|end_of_role|>' + system_message + '<|end_of_text|>
|
||||||
|
' }}
|
||||||
|
{%- if tools %}
|
||||||
|
{{- '<|start_of_role|>tools<|end_of_role|>' }}
|
||||||
|
{{- tools | tojson(indent=4) }}
|
||||||
|
{{- '<|end_of_text|>
|
||||||
|
' }}
|
||||||
|
{%- endif %}
|
||||||
|
{%- if documents %}
|
||||||
|
{{- '<|start_of_role|>documents<|end_of_role|>' }}
|
||||||
|
{%- for document in documents %}
|
||||||
|
{{- 'Document ' + loop.index0 | string + '
|
||||||
|
' }}
|
||||||
|
{{- document['text'] }}
|
||||||
|
{%- if not loop.last %}
|
||||||
|
{{- '
|
||||||
|
|
||||||
|
'}}
|
||||||
|
{%- endif%}
|
||||||
|
{%- endfor %}
|
||||||
|
{{- '<|end_of_text|>
|
||||||
|
' }}
|
||||||
|
{%- endif %}
|
||||||
|
{%- for message in loop_messages %}
|
||||||
|
{{- '<|start_of_role|>' + message['role'] + '<|end_of_role|>' + message['content'] + '<|end_of_text|>
|
||||||
|
' }}
|
||||||
|
{%- if loop.last and add_generation_prompt %}
|
||||||
|
{{- '<|start_of_role|>assistant' }}
|
||||||
|
{%- if controls %}
|
||||||
|
{{- ' ' + controls | tojson()}}
|
||||||
|
{%- endif %}
|
||||||
|
{{- '<|end_of_role|>' }}
|
||||||
|
{%- endif %}
|
||||||
|
{%- endfor %}
|
||||||
37
config.json
Normal file
37
config.json
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
{
|
||||||
|
"architectures": [
|
||||||
|
"GraniteMoeForCausalLM"
|
||||||
|
],
|
||||||
|
"attention_bias": false,
|
||||||
|
"attention_dropout": 0.0,
|
||||||
|
"attention_multiplier": 0.015625,
|
||||||
|
"bos_token_id": 0,
|
||||||
|
"dtype": "float32",
|
||||||
|
"embedding_multiplier": 12.0,
|
||||||
|
"eos_token_id": 0,
|
||||||
|
"hidden_act": "silu",
|
||||||
|
"hidden_size": 1024,
|
||||||
|
"initializer_range": 0.02,
|
||||||
|
"intermediate_size": 512,
|
||||||
|
"logits_scaling": 6.0,
|
||||||
|
"max_position_embeddings": 131072,
|
||||||
|
"model_type": "granitemoe",
|
||||||
|
"num_attention_heads": 16,
|
||||||
|
"num_experts_per_tok": 8,
|
||||||
|
"num_hidden_layers": 24,
|
||||||
|
"num_key_value_heads": 8,
|
||||||
|
"num_local_experts": 32,
|
||||||
|
"output_router_logits": false,
|
||||||
|
"pad_token_id": 0,
|
||||||
|
"residual_multiplier": 0.22,
|
||||||
|
"rms_norm_eps": 1e-06,
|
||||||
|
"rope_parameters": {
|
||||||
|
"rope_theta": 1500000.0,
|
||||||
|
"rope_type": "default"
|
||||||
|
},
|
||||||
|
"router_aux_loss_coef": 0.0,
|
||||||
|
"tie_word_embeddings": true,
|
||||||
|
"transformers_version": "5.9.0",
|
||||||
|
"use_cache": true,
|
||||||
|
"vocab_size": 49155
|
||||||
|
}
|
||||||
9
generation_config.json
Normal file
9
generation_config.json
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"_from_model_config": true,
|
||||||
|
"bos_token_id": 0,
|
||||||
|
"eos_token_id": [
|
||||||
|
0
|
||||||
|
],
|
||||||
|
"pad_token_id": 0,
|
||||||
|
"transformers_version": "5.9.0"
|
||||||
|
}
|
||||||
3
model.safetensors
Normal file
3
model.safetensors
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:d4d22afb98af6731b5ef3ed791c9da790e4a1d733bcc33abe9c8837a42c9b57e
|
||||||
|
size 5338539672
|
||||||
244954
tokenizer.json
Normal file
244954
tokenizer.json
Normal file
File diff suppressed because it is too large
Load Diff
16
tokenizer_config.json
Normal file
16
tokenizer_config.json
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"add_prefix_space": false,
|
||||||
|
"backend": "tokenizers",
|
||||||
|
"bos_token": "<|end_of_text|>",
|
||||||
|
"clean_up_tokenization_spaces": true,
|
||||||
|
"eos_token": "<|end_of_text|>",
|
||||||
|
"errors": "replace",
|
||||||
|
"is_local": false,
|
||||||
|
"local_files_only": false,
|
||||||
|
"model_max_length": 9223372036854775807,
|
||||||
|
"pad_token": "<|end_of_text|>",
|
||||||
|
"padding_side": "left",
|
||||||
|
"tokenizer_class": "TokenizersBackend",
|
||||||
|
"unk_token": "<|end_of_text|>",
|
||||||
|
"vocab_size": 49152
|
||||||
|
}
|
||||||
3
training_args.bin
Normal file
3
training_args.bin
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:b72f4aa65847a2630c3bb470f0ff211e5e66c6543ba73a50172ecb05391e9389
|
||||||
|
size 5265
|
||||||
Reference in New Issue
Block a user