Instructions to use albertkingdom/deepseek-coder-7b-text2sql-magicoder-lora with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use albertkingdom/deepseek-coder-7b-text2sql-magicoder-lora with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("deepseek-ai/deepseek-coder-7b-instruct-v1.5") model = PeftModel.from_pretrained(base_model, "albertkingdom/deepseek-coder-7b-text2sql-magicoder-lora") - Notebooks
- Google Colab
- Kaggle
Rewrite model card: correct repo references, mixed-training results, intended use & limitations
Browse files
README.md
CHANGED
|
@@ -1,51 +1,133 @@
|
|
| 1 |
---
|
| 2 |
base_model: deepseek-ai/deepseek-coder-7b-instruct-v1.5
|
| 3 |
-
library_name:
|
| 4 |
-
model_name: deepseek-sql-
|
| 5 |
tags:
|
| 6 |
-
-
|
| 7 |
-
-
|
| 8 |
- sft
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
# Model Card for deepseek-sql-
|
| 13 |
|
| 14 |
-
|
| 15 |
-
It has been trained using [TRL](https://github.com/huggingface/trl).
|
| 16 |
|
| 17 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
```python
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
```
|
| 27 |
|
| 28 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
### Framework versions
|
| 36 |
|
|
|
|
| 37 |
- TRL: 0.26.2
|
| 38 |
- Transformers: 4.57.3
|
| 39 |
-
- Pytorch: 2.13.0
|
| 40 |
- Datasets: 4.4.2
|
| 41 |
-
- Tokenizers: 0.22.2
|
| 42 |
|
| 43 |
## Citations
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
Cite TRL as:
|
| 48 |
-
|
| 49 |
```bibtex
|
| 50 |
@misc{vonwerra2022trl,
|
| 51 |
title = {{TRL: Transformer Reinforcement Learning}},
|
|
@@ -55,4 +137,4 @@ Cite TRL as:
|
|
| 55 |
publisher = {GitHub},
|
| 56 |
howpublished = {\url{https://github.com/huggingface/trl}}
|
| 57 |
}
|
| 58 |
-
```
|
|
|
|
| 1 |
---
|
| 2 |
base_model: deepseek-ai/deepseek-coder-7b-instruct-v1.5
|
| 3 |
+
library_name: peft
|
| 4 |
+
model_name: deepseek-coder-7b-instruct-sql-create-context-lora
|
| 5 |
tags:
|
| 6 |
+
- base_model:adapter:deepseek-ai/deepseek-coder-7b-instruct-v1.5
|
| 7 |
+
- lora
|
| 8 |
- sft
|
| 9 |
+
- text-to-sql
|
| 10 |
+
- trl
|
| 11 |
+
license: apache-2.0
|
| 12 |
+
pipeline_tag: text-generation
|
| 13 |
+
datasets:
|
| 14 |
+
- b-mc2/sql-create-context
|
| 15 |
+
- ise-uiuc/Magicoder-OSS-Instruct-75K
|
| 16 |
---
|
| 17 |
|
| 18 |
+
# Model Card for deepseek-coder-7b-instruct-sql-create-context-lora
|
| 19 |
|
| 20 |
+
LoRA adapter fine-tuned from [deepseek-ai/deepseek-coder-7b-instruct-v1.5](https://huggingface.co/deepseek-ai/deepseek-coder-7b-instruct-v1.5) for text-to-SQL generation, trained with [TRL](https://github.com/huggingface/trl) SFTTrainer.
|
|
|
|
| 21 |
|
| 22 |
+
## Intended Use & Limitations
|
| 23 |
+
|
| 24 |
+
**Intended use**: generating a single SQL query from a natural-language question given a `CREATE TABLE` schema, in English, for single or lightly-joined relational databases similar in style to Spider / sql-create-context schemas.
|
| 25 |
+
|
| 26 |
+
**Limitations**:
|
| 27 |
+
- Not evaluated on production/adversarial inputs, non-English questions, or dialects outside SQLite-compatible syntax.
|
| 28 |
+
- Execution accuracy on Spider (60.8%) means roughly 2 in 5 generated queries on unseen schemas are still wrong — **always validate generated SQL before running it against a real database**, especially for destructive statements (this adapter was only trained/evaluated on read (`SELECT`) queries).
|
| 29 |
+
- Code capability (HumanEval+) is retained better than the pure-SQL version but still ~4pp below the un-finetuned base model — for general-purpose coding tasks unrelated to SQL, the base model remains the stronger choice. See Results below.
|
| 30 |
+
|
| 31 |
+
## What's on `main` vs `pure-sql`
|
| 32 |
+
|
| 33 |
+
- **`main` (this version)** — trained on a 50/50 mix of SQL and general code-instruction data. Better SQL generalization to unseen schemas *and* better retention of general code ability than the pure-SQL version.
|
| 34 |
+
- **`pure-sql`** branch — the original version trained on 100% SQL data. Higher in-distribution SQL accuracy on the training-like distribution, but noticeably worse code capability retention and worse generalization to unseen database schemas.
|
| 35 |
|
| 36 |
```python
|
| 37 |
+
# to load the pure-SQL version instead:
|
| 38 |
+
PeftModel.from_pretrained(model, adapter_id, revision="pure-sql")
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
## Why mix in code data
|
| 42 |
+
|
| 43 |
+
The pure-SQL version showed catastrophic forgetting of general code generation ability:
|
| 44 |
+
|
| 45 |
+
| Metric | Base | Pure-SQL SFT | Δ |
|
| 46 |
+
|---|---|---|---|
|
| 47 |
+
| HumanEval pass@1 | 52.0% | 40.0% | -12.0pp |
|
| 48 |
+
| HumanEval+ (999 edge cases) | 46.0% | 34.0% | -12.0pp |
|
| 49 |
+
|
| 50 |
+
Training data was rebalanced to 50% [b-mc2/sql-create-context](https://huggingface.co/datasets/b-mc2/sql-create-context) + 50% [ise-uiuc/Magicoder-OSS-Instruct-75K](https://huggingface.co/datasets/ise-uiuc/Magicoder-OSS-Instruct-75K) (interleaved batch-wise via `datasets.interleave_datasets`), 1 epoch.
|
| 51 |
+
|
| 52 |
+
## Results
|
| 53 |
+
|
| 54 |
+
### Code capability retention (n=50, HumanEval/HumanEval+)
|
| 55 |
+
|
| 56 |
+
| Metric | Base | Pure-SQL SFT | **This version (mixed)** |
|
| 57 |
+
|---|---|---|---|
|
| 58 |
+
| HumanEval pass@1 | 52.0% | 40.0% (-12.0pp) | **44.0% (-8.0pp)** |
|
| 59 |
+
| HumanEval+ (plus) | 46.0% | 34.0% (-12.0pp) | **42.0% (-4.0pp)** |
|
| 60 |
+
|
| 61 |
+
### SQL generalization on unseen schemas (Spider 1.0, n=1034, real databases + official eval)
|
| 62 |
+
|
| 63 |
+
| Metric | Base | Pure-SQL SFT | **This version (mixed)** |
|
| 64 |
+
|---|---|---|---|
|
| 65 |
+
| Official Execution Accuracy | 39.9% | 50.4% | **60.8%** |
|
| 66 |
+
| Official Exact Match (structural) | 32.1% | 37.4% | **47.3%** |
|
| 67 |
+
|
| 68 |
+
This version beats the pure-SQL version on *every* axis measured — code capability retention, SQL generalization to unseen schemas, and in-distribution SQL accuracy (see below). Mixing in code data appears to act as a regularizer against overfitting to the narrow single-domain SQL distribution.
|
| 69 |
+
|
| 70 |
+
### In-distribution SQL accuracy (b-mc2/sql-create-context, same distribution as training)
|
| 71 |
+
|
| 72 |
+
| Model | Exact Match |
|
| 73 |
+
|------|-------------|
|
| 74 |
+
| Base | 4% (2/50) |
|
| 75 |
+
| Pure-SQL SFT | 78% (39/50) |
|
| 76 |
+
| This version (mixed) | 70% (14/20, different eval run — see repo `text-to-sql/docs/experiment_log.md` for full methodology) |
|
| 77 |
+
|
| 78 |
+
## LoRA configuration
|
| 79 |
|
| 80 |
+
```
|
| 81 |
+
r: 16
|
| 82 |
+
lora_alpha: 32
|
| 83 |
+
lora_dropout: 0.05
|
| 84 |
+
target_modules: [q_proj, k_proj, v_proj, o_proj]
|
| 85 |
+
quantization: 4-bit NF4 (QLoRA), bf16 compute
|
| 86 |
```
|
| 87 |
|
| 88 |
+
## Quick start
|
| 89 |
+
|
| 90 |
+
```python
|
| 91 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 92 |
+
from peft import PeftModel
|
| 93 |
+
|
| 94 |
+
base_model_id = "deepseek-ai/deepseek-coder-7b-instruct-v1.5"
|
| 95 |
+
adapter_id = "albertkingdom/deepseek-coder-7b-instruct-sql-create-context-lora"
|
| 96 |
|
| 97 |
+
model = AutoModelForCausalLM.from_pretrained(base_model_id, device_map="auto")
|
| 98 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_id)
|
| 99 |
+
model = PeftModel.from_pretrained(model, adapter_id) # main = mixed training version
|
| 100 |
|
| 101 |
+
messages = [{
|
| 102 |
+
"role": "user",
|
| 103 |
+
"content": """Given the database schema below, write a SQL query that answers the user's question.
|
| 104 |
+
Only output the SQL query. Do not add any explanation.
|
| 105 |
|
| 106 |
+
### Schema
|
| 107 |
+
CREATE TABLE users (id INT, name VARCHAR(100), email VARCHAR(100))
|
| 108 |
+
|
| 109 |
+
### Question
|
| 110 |
+
Find all users with gmail addresses"""
|
| 111 |
+
}]
|
| 112 |
+
|
| 113 |
+
inputs = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True).to(model.device)
|
| 114 |
+
outputs = model.generate(inputs, max_new_tokens=200)
|
| 115 |
+
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
## Training procedure
|
| 119 |
+
|
| 120 |
+
Trained with SFT (TRL `SFTTrainer`) on an interleaved SQL + code instruction dataset, 1 epoch, effective batch size 16, bf16, 4-bit QLoRA, on a rented RTX 5090.
|
| 121 |
|
| 122 |
### Framework versions
|
| 123 |
|
| 124 |
+
- PEFT 0.18.0
|
| 125 |
- TRL: 0.26.2
|
| 126 |
- Transformers: 4.57.3
|
|
|
|
| 127 |
- Datasets: 4.4.2
|
|
|
|
| 128 |
|
| 129 |
## Citations
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
```bibtex
|
| 132 |
@misc{vonwerra2022trl,
|
| 133 |
title = {{TRL: Transformer Reinforcement Learning}},
|
|
|
|
| 137 |
publisher = {GitHub},
|
| 138 |
howpublished = {\url{https://github.com/huggingface/trl}}
|
| 139 |
}
|
| 140 |
+
```
|