neurondb commited on
Commit
915cc00
·
verified ·
1 Parent(s): 5fcb16c

Initial release: postgres-llm-qlora

Browse files

QLoRA adapter for PostgreSQL SQL/text-to-SQL. Base: Qwen/Qwen2.5-Coder-3B-Instruct. Trained on neurondb/neurondb-postgresql-sql (37k steps, 3 epochs). Includes adapter weights, tokenizer, and model card.

.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ tokenizer.json filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,3 +1,129 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ base_model: Qwen/Qwen2.5-Coder-3B-Instruct
3
+ library_name: peft
4
+ tags:
5
+ - sql
6
+ - postgresql
7
+ - text-to-sql
8
+ - code
9
+ - lora
10
+ - qlora
11
+ - qwen2
12
+ license: apache-2.0
13
+ pipeline_tag: text-generation
14
+ ---
15
+
16
+ # postgres-llm-qlora
17
+
18
+ QLoRA fine-tuned adapter for **PostgreSQL SQL / text-to-SQL** on [Qwen/Qwen2.5-Coder-3B-Instruct](https://huggingface.co/Qwen/Qwen2.5-Coder-3B-Instruct). Trained on the [neurondb/neurondb-postgresql-sql](https://huggingface.co/datasets/neurondb/neurondb-postgresql-sql) dataset. Use this adapter with the base model to generate PostgreSQL-compatible SQL from natural language instructions. Use this adapter with the base model to generate PostgreSQL-compatible SQL from natural language instructions.
19
+
20
+ ## Model details
21
+
22
+ - **Base model:** [Qwen/Qwen2.5-Coder-3B-Instruct](https://huggingface.co/Qwen/Qwen2.5-Coder-3B-Instruct)
23
+ - **Training dataset:** [neurondb/neurondb-postgresql-sql](https://huggingface.co/datasets/neurondb/neurondb-postgresql-sql) (~212k rows: question, schema, sql, with sources including PostgreSQL regression tests, docs, contrib, pgTAP, plpgsql, sql_create_context, community SQL datasets)
24
+ - **Method:** QLoRA (4-bit base + LoRA), rank 64, alpha 128
25
+ - **Training:** 37,299 steps, 3 epochs on the dataset train split
26
+ - **Final metrics:** ~0.34 loss, ~89% mean token accuracy
27
+
28
+ ## Usage
29
+
30
+ ### With transformers + PEFT (recommended on 8GB GPU: load base in 4-bit)
31
+
32
+ ```python
33
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
34
+ from peft import PeftModel
35
+ import torch
36
+
37
+ adapter_path = "YOUR_USER/postgres-llm-qlora" # or local path
38
+ base_model = "Qwen/Qwen2.5-Coder-3B-Instruct"
39
+
40
+ bnb_config = BitsAndBytesConfig(
41
+ load_in_4bit=True,
42
+ bnb_4bit_compute_dtype=torch.bfloat16,
43
+ bnb_4bit_quant_type="nf4",
44
+ bnb_4bit_use_double_quant=True,
45
+ )
46
+ model = AutoModelForCausalLM.from_pretrained(
47
+ base_model,
48
+ quantization_config=bnb_config,
49
+ device_map="auto",
50
+ trust_remote_code=True,
51
+ )
52
+ model = PeftModel.from_pretrained(model, adapter_path)
53
+ tokenizer = AutoTokenizer.from_pretrained(adapter_path, trust_remote_code=True)
54
+ model.eval()
55
+
56
+ prompt = "Create a table users with columns id (serial primary key), name (text), email (text);"
57
+ text = f"### Instruction:\n{prompt}\n\n### Response:\n"
58
+ inputs = tokenizer(text, return_tensors="pt").to(model.device)
59
+ with torch.no_grad():
60
+ out = model.generate(
61
+ **inputs,
62
+ max_new_tokens=256,
63
+ do_sample=False,
64
+ pad_token_id=tokenizer.eos_token_id,
65
+ )
66
+ response = tokenizer.decode(out[0][inputs["input_ids"].size(1):], skip_special_tokens=True)
67
+ print(response)
68
+ ```
69
+
70
+ ### With pipeline (after loading model as above)
71
+
72
+ ```python
73
+ # After loading model + tokenizer as above
74
+ from transformers import pipeline
75
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=model.device)
76
+ out = pipe("### Instruction:\nList all tables.\n\n### Response:\n", max_new_tokens=128, do_sample=False)
77
+ print(out[0]["generated_text"])
78
+ ```
79
+
80
+ ## Prompt format
81
+
82
+ Use the same instruction format as in training:
83
+
84
+ ```
85
+ ### Instruction:
86
+ <your natural language or task>
87
+
88
+ ### Response:
89
+ ```
90
+
91
+ The model will generate SQL (and optionally an explanation) after `### Response:`.
92
+
93
+ ## Training data
94
+
95
+ This model was fine-tuned on **[neurondb/neurondb-postgresql-sql](https://huggingface.co/datasets/neurondb/neurondb-postgresql-sql)** (~212k instruction pairs). The dataset includes:
96
+
97
+ - **Sources:** PostgreSQL regression tests, official docs, contrib modules, pgTAP tests, PL/pgSQL source, sql_create_context, community SQL datasets (e.g. Spider/WikiSQL-derived), synthetic text-to-SQL
98
+ - **Content:** `question`, optional `schema` (DDL), `sql` (PostgreSQL/PL-pgSQL), optional `explanation`
99
+ - **Splits:** train / validation / test; training used the train split only
100
+
101
+ See the [dataset card](https://huggingface.co/datasets/neurondb/neurondb-postgresql-sql) for schema, difficulty distribution, categories, and license.
102
+
103
+ ## License
104
+
105
+ Apache 2.0. Base model (Qwen2.5-Coder) follows its original license.
106
+
107
+ ## Citation
108
+
109
+ If you use this adapter, please cite the **training dataset** and the base model / PEFT:
110
+
111
+ **Dataset:**
112
+ ```bibtex
113
+ @dataset{neurondb_postgresql_sql_2026,
114
+ title={NeuronDB PostgreSQL SQL & PL/pgSQL Instruction Dataset},
115
+ author={NeuronDB Team},
116
+ year={2026},
117
+ url={https://huggingface.co/datasets/neurondb/neurondb-postgresql-sql},
118
+ }
119
+ ```
120
+
121
+ **PEFT:**
122
+ ```bibtex
123
+ @software{peft,
124
+ title = {PEFT: State-of-the-art Parameter-Efficient Fine-Tuning},
125
+ author = {Hugging Face},
126
+ year = {2023},
127
+ url = {https://github.com/huggingface/peft}
128
+ }
129
+ ```
adapter_config.json ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "alora_invocation_tokens": null,
3
+ "alpha_pattern": {},
4
+ "arrow_config": null,
5
+ "auto_mapping": null,
6
+ "base_model_name_or_path": "Qwen/Qwen2.5-Coder-3B-Instruct",
7
+ "bias": "none",
8
+ "corda_config": null,
9
+ "ensure_weight_tying": false,
10
+ "eva_config": null,
11
+ "exclude_modules": null,
12
+ "fan_in_fan_out": false,
13
+ "inference_mode": true,
14
+ "init_lora_weights": true,
15
+ "layer_replication": null,
16
+ "layers_pattern": null,
17
+ "layers_to_transform": null,
18
+ "loftq_config": {},
19
+ "lora_alpha": 128,
20
+ "lora_bias": false,
21
+ "lora_dropout": 0.05,
22
+ "megatron_config": null,
23
+ "megatron_core": "megatron.core",
24
+ "modules_to_save": null,
25
+ "peft_type": "LORA",
26
+ "peft_version": "0.18.1",
27
+ "qalora_group_size": 16,
28
+ "r": 64,
29
+ "rank_pattern": {},
30
+ "revision": null,
31
+ "target_modules": [
32
+ "k_proj",
33
+ "up_proj",
34
+ "gate_proj",
35
+ "o_proj",
36
+ "down_proj",
37
+ "v_proj",
38
+ "q_proj"
39
+ ],
40
+ "target_parameters": null,
41
+ "task_type": "CAUSAL_LM",
42
+ "trainable_token_indices": null,
43
+ "use_dora": false,
44
+ "use_qalora": false,
45
+ "use_rslora": false
46
+ }
adapter_model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b2e5a8710d4cd6c53c6fc851e8e027b0f7f757f0166bc606a71458e4ca2fe288
3
+ size 479005064
chat_template.jinja ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- if tools %}
2
+ {{- '<|im_start|>system\n' }}
3
+ {%- if messages[0]['role'] == 'system' %}
4
+ {{- messages[0]['content'] }}
5
+ {%- else %}
6
+ {{- 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.' }}
7
+ {%- endif %}
8
+ {{- "\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 <tools></tools> XML tags:\n<tools>" }}
9
+ {%- for tool in tools %}
10
+ {{- "\n" }}
11
+ {{- tool | tojson }}
12
+ {%- endfor %}
13
+ {{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}
14
+ {%- else %}
15
+ {%- if messages[0]['role'] == 'system' %}
16
+ {{- '<|im_start|>system\n' + messages[0]['content'] + '<|im_end|>\n' }}
17
+ {%- else %}
18
+ {{- '<|im_start|>system\nYou are Qwen, created by Alibaba Cloud. You are a helpful assistant.<|im_end|>\n' }}
19
+ {%- endif %}
20
+ {%- endif %}
21
+ {%- for message in messages %}
22
+ {%- if (message.role == "user") or (message.role == "system" and not loop.first) or (message.role == "assistant" and not message.tool_calls) %}
23
+ {{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }}
24
+ {%- elif message.role == "assistant" %}
25
+ {{- '<|im_start|>' + message.role }}
26
+ {%- if message.content %}
27
+ {{- '\n' + message.content }}
28
+ {%- endif %}
29
+ {%- for tool_call in message.tool_calls %}
30
+ {%- if tool_call.function is defined %}
31
+ {%- set tool_call = tool_call.function %}
32
+ {%- endif %}
33
+ {{- '\n<tool_call>\n{"name": "' }}
34
+ {{- tool_call.name }}
35
+ {{- '", "arguments": ' }}
36
+ {{- tool_call.arguments | tojson }}
37
+ {{- '}\n</tool_call>' }}
38
+ {%- endfor %}
39
+ {{- '<|im_end|>\n' }}
40
+ {%- elif message.role == "tool" %}
41
+ {%- if (loop.index0 == 0) or (messages[loop.index0 - 1].role != "tool") %}
42
+ {{- '<|im_start|>user' }}
43
+ {%- endif %}
44
+ {{- '\n<tool_response>\n' }}
45
+ {{- message.content }}
46
+ {{- '\n</tool_response>' }}
47
+ {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
48
+ {{- '<|im_end|>\n' }}
49
+ {%- endif %}
50
+ {%- endif %}
51
+ {%- endfor %}
52
+ {%- if add_generation_prompt %}
53
+ {{- '<|im_start|>assistant\n' }}
54
+ {%- endif %}
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3fd169731d2cbde95e10bf356d66d5997fd885dd8dbb6fb4684da3f23b2585d8
3
+ size 11421892
tokenizer_config.json ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_prefix_space": false,
3
+ "backend": "tokenizers",
4
+ "bos_token": null,
5
+ "clean_up_tokenization_spaces": false,
6
+ "eos_token": "<|im_end|>",
7
+ "errors": "replace",
8
+ "extra_special_tokens": [
9
+ "<|im_start|>",
10
+ "<|im_end|>",
11
+ "<|object_ref_start|>",
12
+ "<|object_ref_end|>",
13
+ "<|box_start|>",
14
+ "<|box_end|>",
15
+ "<|quad_start|>",
16
+ "<|quad_end|>",
17
+ "<|vision_start|>",
18
+ "<|vision_end|>",
19
+ "<|vision_pad|>",
20
+ "<|image_pad|>",
21
+ "<|video_pad|>"
22
+ ],
23
+ "is_local": false,
24
+ "model_max_length": 32768,
25
+ "pad_token": "<|im_end|>",
26
+ "split_special_tokens": false,
27
+ "tokenizer_class": "Qwen2Tokenizer",
28
+ "unk_token": null
29
+ }