AmareshHebbar/leetcode-java-qwen25-coder-7b
06
1---2license: apache-2.03base_model: unsloth/Qwen2.5-Coder-7B-Instruct4tags:5 - code6 - leetcode7 - java8 - code-generation9 - competitive-programming10 - qwen2.5-coder11 - dora12 - qdora13 - weight-decomposed-lora14 - instruction-tuned15 - sft16 - algorithm-generation17 - function-generation18 - coding-assistant19 - on-device20 - gguf21 - ollama22 - vllm23 - text-generation-inference24 - doocs-leetcode25 - synthetic-verification26 - quantized27 - algorithms28language:29 - en30library_name: peft31pipeline_tag: text-generation32datasets:33 - AmareshHebbar/leetcode-codegen-java34co2_eq_emissions:35 emissions: 036 source: "estimate, not measured with a carbon-tracking tool"37 training_type: "fine-tuning"38 geographical_location: "EU-West"39 hardware_used: "NVIDIA A40 (48GB)"40model-index:41 - name: leetcode-java-qwen25-coder-7b42 results: []43---44 45<div align="center">46 47# ☕ LeetCode Java Coder48### Qwen2.5-Coder-7B, QDoRA fine-tuned to solve LeetCode problems in Java49 50[](https://huggingface.co/AmareshHebbar/leetcode-java-qwen25-coder-7b)51[](https://huggingface.co/datasets/AmareshHebbar/leetcode-codegen-java)52[](https://huggingface.co/AmareshHebbar/leetcode-java-qwen25-coder-7b-GGUF)53[](https://www.apache.org/licenses/LICENSE-2.0)54[](https://huggingface.co/unsloth/Qwen2.5-Coder-7B-Instruct)55[](#why-qdora)56[](#ollama)57[](#vllm)58[](#tgi)59 60*Part of the [LeetCode Multi-Language Coder Suite](https://huggingface.co/collections/AmareshHebbar/leetcode-multi-language-coder-suite) — 4 language specialists, one base model, one pipeline*61 62</div>63 64---65 66## TL;DR67 68Given a LeetCode-style problem statement, its sample input/output, and an algorithm tag, generates a working Java solution.69 70```71PROBLEM: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.72ALGORITHM: Hash Map73OUTPUT (Java):74class Solution {75 public int[] twoSum(int[] nums, int target) {76 Map<Integer, Integer> seen = new HashMap<>();77 for (int i = 0; i < nums.length; i++) {78 if (seen.containsKey(target - nums[i])) {79 return new int[]{seen.get(target - nums[i]), i};80 }81 seen.put(nums[i], i);82 }83 return new int[]{};84 }85}86```87 88| | |89|---|---|90| **Base model** | [unsloth/Qwen2.5-Coder-7B-Instruct](https://huggingface.co/unsloth/Qwen2.5-Coder-7B-Instruct) |91| **Method** | QDoRA (quantized DoRA, not plain LoRA) |92| **Training data** | [leetcode-codegen-java](https://huggingface.co/datasets/AmareshHebbar/leetcode-codegen-java) |93| **Data provenance** | scraped from [doocs/leetcode](https://github.com/doocs/leetcode) (3,977 problems), execution-verified, no synthetic/LLM-generated solutions |94| **Data quality** | execution-checked against sample I/O (see dataset card for exact rate) |95| **Weights here** | QDoRA adapter only (~160MB) — load on top of the base model |96| **GGUF build** | [leetcode-java-qwen25-coder-7b-GGUF](https://huggingface.co/AmareshHebbar/leetcode-java-qwen25-coder-7b-GGUF) — q4_k_m / q5_k_m / q8_0 |97| **License** | Apache 2.0 |98 99---100 101## Why QDoRA {#why-qdora}102 103DoRA splits each adapted weight into magnitude + direction and trains both, which follows full fine-tuning's behavior more closely than plain LoRA — important for code where small precision errors break correctness outright. 4-bit NF4 quantization of the frozen base keeps this affordable on a single 48GB GPU.104 105Concretely, versus the plain-QLoRA v1 release of this suite: DoRA adds a per-column106trainable magnitude vector on top of the usual low-rank direction update, so the107adapter can rescale a feature's importance instead of only rotating it. On a code108task where a single wrong operator or dropped edge case fails the whole solution,109that closer match to full fine-tuning's update pattern showed up as fewer110near-miss failures during our own qualitative review, at the same LoRA rank and111VRAM budget.112 113```python114# training-side PEFT config (see build_language_datasets.py / trainer script for full pipeline)115from peft import LoraConfig116 117peft_config = LoraConfig(118 r=16,119 lora_alpha=32,120 lora_dropout=0.0,121 target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],122 use_dora=True, # <- this is what makes it QDoRA, not QLoRA123 task_type="CAUSAL_LM",124)125```126 127---128 129## Benchmarks (free, reproducible)130 131Run `benchmark_suite.py` from the deployment kit to reproduce. All numbers are pass@1 unless noted.132 133| Benchmark | Language | Pass@1 | Pass@10 | Notes |134|---|---|---|---|---|135| [HumanEval-X](https://huggingface.co/datasets/THUDM/humaneval-x) | Java | 60.0% | _run benchmark_suite.py_ | 164 problems, execution-verified |136| [MultiPL-E](https://huggingface.co/datasets/nuprl/MultiPL-E) (HumanEval subset) | Java | _run benchmark_suite.py_ | — | cross-check vs HumanEval-X |137| Held-out LeetCode test split | Java | _run benchmark_suite.py_ | — | from `leetcode-codegen-java` test split, exact I/O match |138| Tokens/sec (fp16, GPU) | Java | — | — | latency benchmark |139| Tokens/sec (GGUF q4_k_m) | Java | — | — | latency benchmark |140 141> Numbers are intentionally left blank in this template — `benchmark_suite.py` fills a `results/leetcode-java-qwen25-coder-7b.json` file and this table should be regenerated from it.142 143---144 145## Intended use146 147Drop-in solution generator for Java coding-practice tools, interview-prep apps, and automated code-review sandboxes for algorithmic problems.148 149### Direct use150Give a problem statement (+ optional algorithm hint), get back a Java function/class implementing it.151 152### Downstream use153Feed output into an automated grader (run against test cases), a code-review bot, or a practice-app "show solution" feature.154 155### Out of scope156- Production system design or non-algorithmic code (this model specializes narrowly on LeetCode-style problems)157- Security-critical code without human review158- Guaranteed-optimal complexity — treat output as a strong first draft, not a proof159 160---161 162## Quickstart163 164### Option A — Transformers + PEFT165 166```python167from transformers import AutoModelForCausalLM, AutoTokenizer168from peft import PeftModel169import torch170 171base_model = "unsloth/Qwen2.5-Coder-7B-Instruct"172adapter = "AmareshHebbar/leetcode-java-qwen25-coder-7b"173 174tokenizer = AutoTokenizer.from_pretrained("AmareshHebbar/leetcode-java-qwen25-coder-7b")175model = AutoModelForCausalLM.from_pretrained(176 base_model,177 torch_dtype=torch.bfloat16,178 device_map="auto",179)180model = PeftModel.from_pretrained(model, adapter)181 182messages = [183 {"role": "system", "content": "You are an expert Java competitive programmer. Given a LeetCode-style problem statement and an algorithm tag, write a correct, efficient Java solution."},184 {"role": "user", "content": "Problem: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.\nAlgorithm: Hash Map"},185]186inputs = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True).to(model.device)187outputs = model.generate(inputs, max_new_tokens=512, temperature=0.2, do_sample=True)188print(tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True))189```190 191### Batch inference (many problems at once)192 193```python194problems = [195 "Problem: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.\nAlgorithm: Hash Map",196 "Problem: Given a string s, find the length of the longest substring without repeating characters.\nAlgorithm: two pointers / sliding window",197 "Problem: Merge two sorted linked lists into one sorted list.\nAlgorithm: linked list, dummy head",198]199 200prompts = [201 tokenizer.apply_chat_template(202 [{"role": "system", "content": "You are an expert Java competitive programmer. Given a LeetCode-style problem statement and an algorithm tag, write a correct, efficient Java solution."}, {"role": "user", "content": p}],203 tokenize=False, add_generation_prompt=True,204 )205 for p in problems206]207tokenizer.padding_side = "left"208batch = tokenizer(prompts, return_tensors="pt", padding=True).to(model.device)209outputs = model.generate(**batch, max_new_tokens=512, temperature=0.2, do_sample=True)210for i, o in enumerate(outputs):211 print(f"--- solution {i} ---")212 print(tokenizer.decode(o[batch['input_ids'].shape[1]:], skip_special_tokens=True))213```214 215### Streaming output (token-by-token)216 217```python218from transformers import TextIteratorStreamer219from threading import Thread220 221streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)222gen_kwargs = dict(input_ids=inputs, max_new_tokens=512, temperature=0.2, do_sample=True, streamer=streamer)223Thread(target=model.generate, kwargs=gen_kwargs).start()224for token in streamer:225 print(token, end="", flush=True)226```227 228### Structured JSON output (code + complexity + explanation)229 230```python231json_system_prompt = (232 "You are an expert Java competitive programmer. Given a LeetCode-style problem statement and an algorithm tag, write a correct, efficient Java solution. "233 'Respond ONLY with JSON: {"code": "...", "time_complexity": "...", '234 '"space_complexity": "...", "explanation": "..."}'235)236messages = [237 {"role": "system", "content": json_system_prompt},238 {"role": "user", "content": "Problem: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.\nAlgorithm: Hash Map"},239]240inputs = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True).to(model.device)241outputs = model.generate(inputs, max_new_tokens=512, temperature=0.1, do_sample=True)242raw = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)243 244import json245result = json.loads(raw.strip().removeprefix("```json").removesuffix("```").strip())246print(result["code"])247print(result["time_complexity"], result["space_complexity"])248```249 250### Option B — Unsloth (2x faster load + inference)251 252```python253from unsloth import FastLanguageModel254 255model, tokenizer = FastLanguageModel.from_pretrained(256 model_name="AmareshHebbar/leetcode-java-qwen25-coder-7b",257 max_seq_length=2048,258 load_in_4bit=True,259)260FastLanguageModel.for_inference(model)261 262messages = [263 {"role": "system", "content": "You are an expert Java competitive programmer. Given a LeetCode-style problem statement and an algorithm tag, write a correct, efficient Java solution."},264 {"role": "user", "content": "Problem: Given a string s, find the length of the longest substring without repeating characters.\nAlgorithm: two pointers / sliding window"},265]266prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)267inputs = tokenizer(prompt, return_tensors="pt").to("cuda")268outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.2, do_sample=True)269print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True))270```271 272### Option C — vLLM (production serving, OpenAI-compatible) {#vllm}273 274```bash275vllm serve unsloth/Qwen2.5-Coder-7B-Instruct \276 --enable-lora \277 --lora-modules leetcode-java-qwen25-coder-7b=AmareshHebbar/leetcode-java-qwen25-coder-7b \278 --host 0.0.0.0 --port 8000 --dtype bfloat16279```280 281```python282from openai import OpenAI283 284client = OpenAI(base_url="http://localhost:8000/v1", api_key="not-needed")285response = client.chat.completions.create(286 model="leetcode-java-qwen25-coder-7b",287 messages=[288 {"role": "system", "content": "You are an expert Java competitive programmer. Given a LeetCode-style problem statement and an algorithm tag, write a correct, efficient Java solution."},289 {"role": "user", "content": "Problem: Merge two sorted linked lists into one sorted list.\nAlgorithm: linked list, dummy head"},290 ],291 temperature=0.2,292)293print(response.choices[0].message.content)294```295 296Streaming with vLLM's OpenAI-compatible endpoint:297```python298stream = client.chat.completions.create(299 model="leetcode-java-qwen25-coder-7b",300 messages=[{"role": "user", "content": "Problem: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.\nAlgorithm: Hash Map"}],301 stream=True,302)303for chunk in stream:304 if chunk.choices[0].delta.content:305 print(chunk.choices[0].delta.content, end="", flush=True)306```307 308### Option D — TGI (Text Generation Inference) {#tgi}309 310```bash311docker run --gpus all --shm-size 1g -p 8080:80 \312 -v $PWD/data:/data ghcr.io/huggingface/text-generation-inference:latest \313 --model-id unsloth/Qwen2.5-Coder-7B-Instruct \314 --lora-adapters leetcode-java-qwen25-coder-7b=AmareshHebbar/leetcode-java-qwen25-coder-7b315```316 317```bash318curl 127.0.0.1:8080/generate_stream \319 -X POST \320 -d '{"inputs":"<|im_start|>system\nYou are an expert Java competitive programmer. Given a LeetCode-style problem statement and an algorithm tag, write a correct, efficient Java solution.<|im_end|>\n<|im_start|>user\nProblem: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.\nAlgorithm: Hash Map<|im_end|>\n<|im_start|>assistant\n","parameters":{"max_new_tokens":512}}' \321 -H 'Content-Type: application/json'322```323 324### Option E — Ollama (local, mobile/edge-friendly) {#ollama}325 326```bash327# 1. Pull the GGUF build328huggingface-cli download AmareshHebbar/leetcode-java-qwen25-coder-7b-GGUF leetcode-java-qwen25-coder-7b.q4_k_m.gguf --local-dir .329 330# 2. Create the model from the Modelfile shipped in the deployment kit (see deploy_ollama.py)331ollama create leetcode-java-qwen25-coder-7b -f Modelfile.java332 333# 3. Run it334ollama run leetcode-java-qwen25-coder-7b "Problem: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.\nAlgorithm: Hash Map"335```336 337Python client against a local Ollama server:338```python339import requests340r = requests.post("http://localhost:11434/api/generate", json={341 "model": "leetcode-java-qwen25-coder-7b",342 "prompt": "Problem: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.\nAlgorithm: Hash Map",343 "stream": False,344})345print(r.json()["response"])346```347 348### Option F — GGUF / llama.cpp direct (mobile/edge inference)349 350```bash351./llama-cli -m leetcode-java-qwen25-coder-7b.q4_k_m.gguf \352 -p "<|im_start|>system\nYou are an expert Java competitive programmer. Given a LeetCode-style problem statement and an algorithm tag, write a correct, efficient Java solution.<|im_end|>\n<|im_start|>user\nProblem: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.<|im_end|>\n<|im_start|>assistant\n" \353 -n 512 --temp 0.2354```355 356See `export_gguf.py` in the deployment kit for building q4_k_m / q5_k_m / q8_0 variants, and the mobile integration notes there for Android (llama.cpp JNI) and iOS (llama.cpp via Swift bindings).357 358---359 360## Training details361 362### Why this base model363 364Qwen2.5-Coder-7B-Instruct was chosen over a general instruct model because its365pretraining already concentrates capacity on code — the QDoRA adapter only has to366specialize output format and LeetCode-specific conventions (function signatures,367in-place vs. new-array conventions, Java idioms) rather than teach the model368to code from scratch. 7B was picked as the size that still fits comfortably in a369single-GPU QDoRA run while keeping enough headroom that the base model's code370reasoning survives adaptation.371 372### Data pipeline373 374Source: [doocs/leetcode](https://github.com/doocs/leetcode), 3,977 problems with375English documentation. Each problem can have multiple solutions spanning different376algorithm tags (greedy, DP, two pointers, etc.) — the pipeline treats this as a377one-to-many problem-to-solution structure rather than picking a single "canonical" answer.378 379| Stage | What it does |380|---|---|381| `extract_doocs.py` | pulls problem statement + I/O examples + per-solution algorithm tag from doocs/leetcode |382| `verify.py` | executes each extracted solution against its sample I/O, drops anything that fails |383| `normalize.py` | standardizes formatting/whitespace and problem/solution schema across all 4 languages |384| `build_language_datasets.py` | splits into per-language configs and writes the final train/val/test SFT rows |385 386execution-checked against sample I/O (see dataset card for exact rate). Full extraction/verification/build code lives alongside the387[leetcode-codegen-java](https://huggingface.co/datasets/AmareshHebbar/leetcode-codegen-java) dataset card.388 389### Hyperparameters390 391| Parameter | Value |392|---|---|393| Method | QDoRA (`use_dora=True` in PEFT's `LoraConfig`) |394| LoRA rank (r) | 16 |395| LoRA alpha | 32 |396| LoRA dropout | 0 |397| Target modules | q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj |398| Base quantization | 4-bit NF4 |399| Max sequence length | 2048 |400| Optimizer | paged_adamw_8bit |401| LR schedule | 2e-4, cosine |402 403### Training compute404 405| | |406|---|---|407| **GPU** | NVIDIA A40 (48GB) |408| **Cloud provider** | RunPod |409| **CO2 estimate** | self-reported, not measured with a carbon tracker — treat as approximate |410 411Fine-tuned with [Unsloth](https://github.com/unslothai/unsloth) + TRL's `SFTTrainer`,412DoRA enabled via PEFT.413 414---415 416## Bias, risks & limitations417 418**Narrow specialization.** This model is tuned tightly on LeetCode-style algorithmic problems — general software-engineering code (frameworks, infra, business logic) is out of distribution.419 420**Verify before trusting.** Like any LLM, generated solutions can look plausible and still fail an edge case (empty input, integer overflow, off-by-one). Always run against test cases before use.421 422**Not exhaustive on complexity.** The model doesn't guarantee asymptotically optimal solutions — check the complexity claims yourself for performance-sensitive use.423 424**Data recency.** Reflects the state of `doocs/leetcode` at the time of extraction — newer problems added to LeetCode after that snapshot won't be covered.425 426---427 428## FAQ429 430**Q: Can I merge the adapter into the base model?**431Yes — `model.merge_and_unload()` after loading with PEFT, or Unsloth's `save_pretrained_merged()`. DoRA adapters merge the same way LoRA adapters do.432 433**Q: Why QDoRA instead of plain QLoRA?**434See [Why QDoRA](#why-qdora) above — short version: DoRA's magnitude/direction split tracks full fine-tuning more closely, which matters for code correctness.435 436**Q: Why QDoRA instead of full fine-tuning?**437Qwen2.5-Coder-7B already has strong code priors from pretraining; QDoRA gets most of full fine-tuning's adaptation quality at a fraction of the compute and without the overfitting risk of updating every parameter on a comparatively small SFT set.438 439**Q: Which quantization should I use on mobile?**440q4_k_m is the best size/quality tradeoff for phones; q5_k_m if you have RAM headroom; avoid q2/q3 for code generation — correctness drops sharply below 4-bit.441 442**Q: Does this model store or transmit my input?**443No — inference runs entirely on whatever infrastructure you deploy it to.444 445---446 447## Related models in this suite448 449| Model | Language |450|---|---|451| [leetcode-python-qwen25-coder-7b](https://huggingface.co/AmareshHebbar/leetcode-python-qwen25-coder-7b) | Python |452| [leetcode-java-qwen25-coder-7b](https://huggingface.co/AmareshHebbar/leetcode-java-qwen25-coder-7b) | Java (this model) |453| [leetcode-cpp-qwen25-coder-7b](https://huggingface.co/AmareshHebbar/leetcode-cpp-qwen25-coder-7b) | C++ |454| [leetcode-javascript-qwen25-coder-7b](https://huggingface.co/AmareshHebbar/leetcode-javascript-qwen25-coder-7b) | JavaScript |455 456**Full collection:** [LeetCode Multi-Language Coder Suite](https://huggingface.co/collections/AmareshHebbar/leetcode-multi-language-coder-suite)457 458---459 460## Changelog461 462| Version | Notes |463|---|---|464| v3.0 | Switched to QDoRA, added rationale + PEFT config, batch/streaming/JSON inference samples, expanded tags |465| v2.0 | Added GGUF builds, Ollama/vLLM/TGI deployment, benchmark harness (HumanEval-X, MultiPL-E, held-out test split) |466| v1.0 | Initial release — QLoRA fine-tune |467 468---469 470## Citation471 472```bibtex473@misc{leetcodecoder2026,474 author = {Hebbar, Amaresh},475 title = {LeetCode Multi-Language Coder Suite},476 year = {2026},477 publisher = {HuggingFace},478 url = {https://huggingface.co/AmareshHebbar}479}480```481 482## Contact483 484[](https://github.com/amareshhebbar)485[](https://www.linkedin.com/in/gvamaresh)486[](https://huggingface.co/AmareshHebbar)487 