commit 69da5f5c3e18f0f239335825e9c9678eee8d3b62 Author: ModelHub XC Date: Mon Oct 5 20:06:17 2026 +0800 初始化项目,由ModelHub XC社区提供模型 Model: tkeskin/qwen2.5-coder-1.5b-code-translation Source: Original Platform diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..52373fe --- /dev/null +++ b/.gitattributes @@ -0,0 +1,36 @@ +*.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 diff --git a/Modelfile b/Modelfile new file mode 100644 index 0000000..2ebf950 --- /dev/null +++ b/Modelfile @@ -0,0 +1,16 @@ +# ollama modelfile auto-generated by llamafactory + +FROM . + +TEMPLATE """{{ if .System }}<|im_start|>system +{{ .System }}<|im_end|> +{{ end }}{{ range .Messages }}{{ if eq .Role "user" }}<|im_start|>user +{{ .Content }}<|im_end|> +<|im_start|>assistant +{{ else if eq .Role "assistant" }}{{ .Content }}<|im_end|> +{{ end }}{{ end }}""" + +SYSTEM """You are Qwen, created by Alibaba Cloud. You are a helpful assistant.""" + +PARAMETER stop "<|im_end|>" +PARAMETER num_ctx 4096 diff --git a/README.md b/README.md new file mode 100644 index 0000000..55144d8 --- /dev/null +++ b/README.md @@ -0,0 +1,99 @@ +--- +base_model: Qwen/Qwen2.5-Coder-1.5B-Instruct +license: apache-2.0 +datasets: + - tkeskin/leetcode-solutions +language: + - en +pipeline_tag: text-generation +library_name: transformers +tags: + - lora + - llama-factory + - code + - code-translation + - qwen + - qwen2 + - code +--- + +# qwen2.5-coder-1.5b-code-translation + +A fine-tuned version of [Qwen/Qwen2.5-Coder-1.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-Coder-1.5B-Instruct) for translating code between **C++**, **Java**, and **Python**. + +## Training + +- **Base model:** Qwen/Qwen2.5-Coder-1.5B-Instruct +- **Method:** LoRA (Low-Rank Adaptation) via [LLaMA-Factory](https://github.com/hiyouga/LLaMA-Factory) +- **Dataset:** [tkeskin/leetcode-solutions](https://huggingface.co/datasets/tkeskin/leetcode-solutions) (`instruct` config) — directed C++/Java/Python translation pairs derived from LeetCode solutions +- **Hardware:** AMD MI210 (ROCm) / NVIDIA CUDA, `flash_attn: sdpa` +- **LoRA target:** all linear layers (`lora_target: all`) +- **Precision:** bf16 + +## Evaluation + +Evaluated with an **execution-based** translation benchmark: each held-out `evaluation`-config payload from [tkeskin/leetcode-solutions](https://huggingface.co/datasets/tkeskin/leetcode-solutions) is a directed source→target translation whose output is compiled and run against the problem's input/output pairs. The eval split is held out from training (no leakage). Metric is **pass@1** (all test cases pass), n-weighted over 3,336 payloads. + +| | Base (Qwen2.5-Coder-1.5B-Instruct) | This model | Δ | +|---|---|---|---| +| **pass@1** | 29.3% | **61.9%** | **+32.6** | +| **compile rate** | 59.6% | **84.5%** | **+24.9** | + +pass@1 by language pair × difficulty (%): + +| source | target | difficulty | base | this model | +|---|---|---|---|---| +| cpp | java | Easy | 41.4 | 81.4 | +| cpp | java | Hard | 12.7 | 47.5 | +| cpp | java | Medium | 27.9 | 69.4 | +| cpp | python | Easy | 40.7 | 76.7 | +| cpp | python | Hard | 29.8 | 45.0 | +| cpp | python | Medium | 38.6 | 66.6 | +| java | cpp | Easy | 39.5 | 85.0 | +| java | cpp | Hard | 32.8 | 47.1 | +| java | cpp | Medium | 40.0 | 68.5 | +| java | python | Easy | 18.6 | 78.5 | +| java | python | Hard | 15.3 | 45.8 | +| java | python | Medium | 22.7 | 66.6 | +| python | cpp | Easy | 25.9 | 72.1 | +| python | cpp | Hard | 14.3 | 22.7 | +| python | cpp | Medium | 25.9 | 57.8 | +| python | java | Easy | 44.1 | 62.8 | +| python | java | Hard | 10.2 | 24.6 | +| python | java | Medium | 28.7 | 54.7 | + +The base model also redefined the harness-provided `ListNode`/`TreeNode` helper types on ~6% of problems (a compile error); this fine-tune does so on **none**, having learned the dataset's convention. Full methodology is in the [llm-fine-tune](https://github.com/tkeskin/llm-fine-tune) repo (Stage 5). + +### Standard benchmarks and the specialization trade-off + +Translation specialization has a measurable cost on standard benchmarks (base → this model): + +- **Held-out perplexity** on the translation test set drops **1.29 → 1.07** — training fit the target distribution. +- **General ability is preserved** — MMLU is flat (**0.512 → 0.514**); lm-eval reasoning tasks unchanged within noise. +- **Code generation _from a natural-language spec_ regresses** — HumanEval pass@1 (Python) **75% → 53%**. + +That regression is mostly **output-format specialization, not lost ability**. Trained on LeetCode solutions — which wrap every answer in `class Solution { ... }` with camelCase methods — the model now answers HumanEval-style prompts in that same idiom, e.g. emitting `class Solution { bool hasCloseElements(...) }` instead of the requested free `has_close_elements(...)`. The logic is frequently correct but mismatches the benchmark's free-function contract (C++ class-wrapping rises from **0% in the base to 78%** here). The model improved at its trained format and regressed on unfamiliar ones, while keeping general knowledge intact. + +## Intended use + +Given source code in one of C++, Java, or Python, the model generates a translation into the target language, following the same logic and structure. The Qwen2.5-Coder base model includes code-specific pre-training across C, C++, Java, Python, and many other languages, giving it a stronger prior for code structure. + +## Usage + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer + +model_id = "tkeskin/qwen2.5-coder-1.5b-code-translation" +tokenizer = AutoTokenizer.from_pretrained(model_id) +model = AutoModelForCausalLM.from_pretrained(model_id) + +messages = [ + { + "role": "user", + "content": "Translate the following C++ code to Python:\n\nint add(int a, int b) { return a + b; }" + } +] +inputs = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True) +outputs = model.generate(inputs, max_new_tokens=256) +print(tokenizer.decode(outputs[0][inputs.shape[-1]:], skip_special_tokens=True)) +``` diff --git a/chat_template.jinja b/chat_template.jinja new file mode 100644 index 0000000..bdf7919 --- /dev/null +++ b/chat_template.jinja @@ -0,0 +1,54 @@ +{%- if tools %} + {{- '<|im_start|>system\n' }} + {%- if messages[0]['role'] == 'system' %} + {{- messages[0]['content'] }} + {%- else %} + {{- 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.' }} + {%- endif %} + {{- "\n\n# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within XML tags:\n" }} + {%- for tool in tools %} + {{- "\n" }} + {{- tool | tojson }} + {%- endfor %} + {{- "\n\n\nFor each function call, return a json object with function name and arguments within XML tags:\n\n{\"name\": , \"arguments\": }\n<|im_end|>\n" }} +{%- else %} + {%- if messages[0]['role'] == 'system' %} + {{- '<|im_start|>system\n' + messages[0]['content'] + '<|im_end|>\n' }} + {%- else %} + {{- '<|im_start|>system\nYou are Qwen, created by Alibaba Cloud. You are a helpful assistant.<|im_end|>\n' }} + {%- endif %} +{%- endif %} +{%- for message in messages %} + {%- if (message.role == "user") or (message.role == "system" and not loop.first) or (message.role == "assistant" and not message.tool_calls) %} + {{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }} + {%- elif message.role == "assistant" %} + {{- '<|im_start|>' + message.role }} + {%- if message.content %} + {{- '\n' + message.content }} + {%- endif %} + {%- for tool_call in message.tool_calls %} + {%- if tool_call.function is defined %} + {%- set tool_call = tool_call.function %} + {%- endif %} + {{- '\n\n{"name": "' }} + {{- tool_call.name }} + {{- '", "arguments": ' }} + {{- tool_call.arguments | tojson }} + {{- '}\n' }} + {%- endfor %} + {{- '<|im_end|>\n' }} + {%- elif message.role == "tool" %} + {%- if (loop.index0 == 0) or (messages[loop.index0 - 1].role != "tool") %} + {{- '<|im_start|>user' }} + {%- endif %} + {{- '\n\n' }} + {{- message.content }} + {{- '\n' }} + {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %} + {{- '<|im_end|>\n' }} + {%- endif %} + {%- endif %} +{%- endfor %} +{%- if add_generation_prompt %} + {{- '<|im_start|>assistant\n' }} +{%- endif %} diff --git a/config.json b/config.json new file mode 100644 index 0000000..c20be7f --- /dev/null +++ b/config.json @@ -0,0 +1,61 @@ +{ + "architectures": [ + "Qwen2ForCausalLM" + ], + "attention_dropout": 0.0, + "bos_token_id": 151643, + "dtype": "bfloat16", + "eos_token_id": 151645, + "hidden_act": "silu", + "hidden_size": 1536, + "initializer_range": 0.02, + "intermediate_size": 8960, + "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" + ], + "max_position_embeddings": 32768, + "max_window_layers": 28, + "model_type": "qwen2", + "num_attention_heads": 12, + "num_hidden_layers": 28, + "num_key_value_heads": 2, + "pad_token_id": null, + "rms_norm_eps": 1e-06, + "rope_parameters": { + "rope_theta": 1000000.0, + "rope_type": "default" + }, + "sliding_window": null, + "tie_word_embeddings": true, + "transformers_version": "5.6.0", + "use_cache": true, + "use_sliding_window": false, + "vocab_size": 151936 +} diff --git a/generation_config.json b/generation_config.json new file mode 100644 index 0000000..a4b1a8a --- /dev/null +++ b/generation_config.json @@ -0,0 +1,14 @@ +{ + "bos_token_id": 151643, + "do_sample": true, + "eos_token_id": [ + 151645, + 151643 + ], + "pad_token_id": 151643, + "repetition_penalty": 1.1, + "temperature": 0.7, + "top_k": 20, + "top_p": 0.8, + "transformers_version": "5.6.0" +} diff --git a/model.safetensors b/model.safetensors new file mode 100644 index 0000000..a87caea --- /dev/null +++ b/model.safetensors @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:64c9bc629d82111783bde5fc07a635fcb5326b2c29e9a3dc8d6bb777935ffd47 +size 3087467144 diff --git a/tokenizer.json b/tokenizer.json new file mode 100644 index 0000000..34510ff --- /dev/null +++ b/tokenizer.json @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3fd169731d2cbde95e10bf356d66d5997fd885dd8dbb6fb4684da3f23b2585d8 +size 11421892 diff --git a/tokenizer_config.json b/tokenizer_config.json new file mode 100644 index 0000000..29ee655 --- /dev/null +++ b/tokenizer_config.json @@ -0,0 +1,31 @@ +{ + "add_prefix_space": false, + "backend": "tokenizers", + "bos_token": null, + "clean_up_tokenization_spaces": false, + "eos_token": "<|im_end|>", + "errors": "replace", + "extra_special_tokens": [ + "<|im_start|>", + "<|im_end|>", + "<|object_ref_start|>", + "<|object_ref_end|>", + "<|box_start|>", + "<|box_end|>", + "<|quad_start|>", + "<|quad_end|>", + "<|vision_start|>", + "<|vision_end|>", + "<|vision_pad|>", + "<|image_pad|>", + "<|video_pad|>" + ], + "is_local": false, + "local_files_only": false, + "model_max_length": 32768, + "pad_token": "<|endoftext|>", + "padding_side": "left", + "split_special_tokens": false, + "tokenizer_class": "Qwen2Tokenizer", + "unk_token": null +}