A Fill-in-the-Middle (FIM) fine-tuned version of deepseek-ai/deepseek-coder-6.7b-base, trained with QLoRA to complete missing code segments given both a prefix and a suffix — exactly how modern IDE autocomplete works.
Standard language models generate code left-to-right. This model is trained with the Fill-in-the-Middle objective, which teaches it to reason about both sides of a cursor position and generate the code that belongs in between.
Given a prefix (code before the cursor) and a suffix (code after the cursor), the model generates a contextually accurate middle segment — from a single line to a full function body.
The base model, deepseek-coder-6.7b-base, was pre-trained with FIM natively, making it the ideal starting point. Its tokenizer includes native FIM special tokens (<|fim▁begin|>, <|fim▁end|>, <|fim▁hole|>) which this fine-tune fully exploits.
FIM format
This model uses DeepSeek-Coder's native FIM token format:
The model then generates the middle segment and stops at <|EOT|>.
Usage
python
1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
34MODEL_NAME ="AbdoSaad24/fim_deepseek-coder-6.7b-code-autoCompletion-finetuned"56FIM_PREFIX ="<|fim▁begin|>"7FIM_SUFFIX ="<|fim▁end|>"8FIM_MIDDLE ="<|fim▁hole|>"9EOS_TOKEN ="<|EOT|>"1011tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)12model = AutoModelForCausalLM.from_pretrained(13 MODEL_NAME,14 torch_dtype=torch.float16,15 device_map="auto",16 trust_remote_code=True,17)1819deffim_complete(prefix:str, suffix:str="", max_new_tokens:int=150, temperature:float=0.2)->str:20"""Generate the code segment that fills the gap between prefix and suffix."""21 prompt =f"{FIM_PREFIX}{prefix}{FIM_SUFFIX}{suffix}{FIM_MIDDLE}"22 inputs = tokenizer(prompt, return_tensors="pt").to(model.device)2324with torch.no_grad():25 outputs = model.generate(26**inputs,27 max_new_tokens=max_new_tokens,28 do_sample=temperature >0,29 temperature=temperature if temperature >0else1.0,30 top_p=0.95,31 eos_token_id=tokenizer.convert_tokens_to_ids(EOS_TOKEN),32 pad_token_id=tokenizer.eos_token_id,33)3435 generated_ids = outputs[0][inputs["input_ids"].shape[1]:]36return tokenizer.decode(generated_ids, skip_special_tokens=True)
Example: complete a binary search
python
1prefix =(2"def binary_search(arr: list, target: int) -> int:\n"3" \"\"\"Return index of target in sorted arr, or -1 if not found.\"\"\"\n"4" left, right = 0, len(arr) - 1\n"5" while left <= right:\n"6" mid = (left + right) // 2\n"7)8suffix =(9" elif arr[mid] < target:\n"10" left = mid + 1\n"11" else:\n"12" right = mid - 1\n"13" return -1\n"14)1516print(fim_complete(prefix, suffix))17# → if arr[mid] == target:18# → return mid
Example: complete error handling
python
1prefix =(2"def read_json_file(filepath: str) -> dict:\n"3" \"\"\"Read and parse a JSON file safely.\"\"\"\n"4" try:\n"5" with open(filepath, 'r', encoding='utf-8') as f:\n"6)7suffix =(8" except FileNotFoundError:\n"9" raise FileNotFoundError(f\"File not found: {filepath}\")\n"10)1112print(fim_complete(prefix, suffix))13# → return json.load(f)
Training details
Base model
deepseek-ai/deepseek-coder-6.7b-base — chosen specifically because it was pre-trained with FIM objectives and already understands FIM special tokens natively. Using the instruct variant would have degraded FIM performance.
Dataset
FIM training examples were generated from two Python code sources:
Source
Snippets extracted
FIM examples generated
sahil2801/CodeAlpaca-20k
~15,000
~30,000
iamtarun/python_code_instructions_18k_alpaca
~11,400
~11,400
Total (capped)
—
10,000
Each raw code snippet was split into prefix / middle / suffix at a random cut point (20–80% of file length), snapped to the nearest newline, and repeated N_AUGMENTS=2 times per snippet to create variety. Only examples where the middle section was at least 10 characters were kept.
The final 10,000 examples were split 99/1 into train (9,900) and validation (100).
Fine-tuning method: QLoRA via LLaMA-Factory
Training used the pre-training (pt) stage — not the SFT stage — because FIM is a raw completion objective with no instruction template.
After training, LoRA adapters were merged into the base model weights using LLaMA-Factory's export pipeline and pushed as a single standalone model.
Intended use
This model is designed for Python code autocompletion tasks where both prefix and suffix context is available:
IDE plugins that complete mid-function code
Jupyter / notebook inline suggestions
Coding assistants with cursor-aware context
Educational tools that help complete partially written algorithms
Out-of-scope use
Languages other than Python (performance will degrade significantly)
Instruction following or chat (use an instruct model instead)
Production use without human review of generated code
Limitations
Optimised for Python; other languages are not supported
Context window is limited to 1024 tokens — very long files may lose coherence
Generated code should always be reviewed before execution
The model may generate plausible-looking but incorrect completions for complex algorithmic logic
Training data was capped at 10,000 examples; broader coverage may improve quality
Citation
If you use this model, please cite the original DeepSeek-Coder work:
bibtex
1@misc{guo2024deepseekcoderlargelanguagemodel,
2 title={DeepSeek-Coder: When the Large Language Model Meets Programming},
3 author={Daya Guo et al.},
4 year={2024},
5 eprint={2401.14196},
6 archivePrefix={arXiv}
7}