Add comprehensive model card for CSC-SQL (#1)
Browse files- Add comprehensive model card for CSC-SQL (316a73fb71716c82144143ee197f7510b8fab841)
Co-authored-by: Niels Rogge <[email protected]>
README.md
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: cc-by-nc-4.0
|
3 |
+
pipeline_tag: text-generation
|
4 |
+
library_name: transformers
|
5 |
+
tags:
|
6 |
+
- text-to-sql
|
7 |
+
- sql-generation
|
8 |
+
- reinforcement-learning
|
9 |
+
- qwen
|
10 |
+
---
|
11 |
+
|
12 |
+
# CSC-SQL: Corrective Self-Consistency in Text-to-SQL via Reinforcement Learning
|
13 |
+
|
14 |
+
The model presented in the paper [CSC-SQL: Corrective Self-Consistency in Text-to-SQL via Reinforcement Learning](https://huggingface.co/papers/2505.13271).
|
15 |
+
|
16 |
+
**Abstract:** Large language models (LLMs) have demonstrated strong capabilities in translating natural language questions about relational databases into SQL queries. In particular, test-time scaling techniques such as Self-Consistency and Self-Correction can enhance SQL generation accuracy by increasing computational effort during inference. However, these methods have notable limitations: Self-Consistency may select suboptimal outputs despite majority votes, while Self-Correction typically addresses only syntactic errors. To leverage the strengths of both approaches, we propose CSC-SQL, a novel method that integrates Self-Consistency and Self-Correction. CSC-SQL selects the two most frequently occurring outputs from parallel sampling and feeds them into a merge revision model for correction. Additionally, we employ the Group Relative Policy Optimization (GRPO) algorithm to fine-tune both the SQL generation and revision models via reinforcement learning, significantly enhancing output quality. Experimental results confirm the effectiveness and generalizability of CSC-SQL. On the BIRD private test set, our 7B model achieves 71.72% execution accuracy, while the 32B model achieves 73.67%. The code has been open sourced at this https URL.
|
17 |
+
|
18 |
+
**Code:** The code for CSC-SQL is open-sourced at [https://github.com/CycloneBoy/csc_sql](https://github.com/CycloneBoy/csc_sql).
|
19 |
+
|
20 |
+
## Introduction
|
21 |
+
|
22 |
+
CSC-SQL is a novel method that integrates Self-Consistency and Self-Correction for improved Text-to-SQL generation. It addresses limitations of prior methods by selecting optimal outputs and handling both syntactic and semantic errors. The approach employs Group Relative Policy Optimization (GRPO) to fine-tune SQL generation and revision models, leading to significant enhancements in output quality.
|
23 |
+
|
24 |
+

|
25 |
+
|
26 |
+
## Main Results
|
27 |
+
|
28 |
+
Performance Comparison of different Text-to-SQL methods on BIRD dev and test dataset.
|
29 |
+
|
30 |
+

|
31 |
+
|
32 |
+
## Models
|
33 |
+
|
34 |
+
A collection of CSC-SQL models can be found on Hugging Face: [CSC-SQL Hugging Face Collection](https://huggingface.co/collections/cycloneboy/csc-sql-6835c4a52da10c54bbe14f8e).
|
35 |
+
|
36 |
+
| **Model and Dataset** | HuggingFace |
|
37 |
+
|---------------------------------------|--------------------------------------------------------------------------------------------|
|
38 |
+
| CscSQL-Merge-Qwen2.5-Coder-3B-Instruct | [🤗 HuggingFace](https://huggingface.co/cycloneboy/CscSQL-Merge-Qwen2.5-Coder-3B-Instruct) |
|
39 |
+
| CscSQL-Merge-Qwen2.5-Coder-7B-Instruct | [🤗 HuggingFace](https://huggingface.co/cycloneboy/CscSQL-Merge-Qwen2.5-Coder-7B-Instruct) |
|
40 |
+
| CscSQL-Grpo-Qwen2.5-Coder-3B-Instruct | [🤗 HuggingFace](https://huggingface.co/cycloneboy/CscSQL-Grpo-Qwen2.5-Coder-3B-Instruct) |
|
41 |
+
| CscSQL-Grpo-XiYanSQL-QwenCoder-3B-2502 | [🤗 HuggingFace](https://huggingface.co/cycloneboy/CscSQL-Grpo-XiYanSQL-QwenCoder-3B-2502) |
|
42 |
+
| CscSQL-Grpo-Qwen2.5-Coder-7B-Instruct | [🤗 HuggingFace](https://huggingface.co/cycloneboy/CscSQL-Grpo-Qwen2.5-Coder-7B-Instruct) |
|
43 |
+
| CscSQL-Grpo-XiYanSQL-QwenCoder-7B-2502 | [🤗 HuggingFace](https://huggingface.co/cycloneboy/CscSQL-Grpo-XiYanSQL-QwenCoder-7B-2502) |
|
44 |
+
|
45 |
+
## Dataset
|
46 |
+
|
47 |
+
The BIRD training and development datasets used can be found here: [BIRD Train Dataset](https://huggingface.co/datasets/cycloneboy/bird_train).
|
48 |
+
|
49 |
+
## Quickstart
|
50 |
+
|
51 |
+
This section provides instructions on how to use the pre-trained CSC-SQL models.
|
52 |
+
|
53 |
+
```python
|
54 |
+
import torch
|
55 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
56 |
+
|
57 |
+
model_dir = "cycloneboy/CscSQL-Grpo-Qwen2.5-Coder-7B-Instruct" # Or other released models
|
58 |
+
|
59 |
+
def load_model_tokenizer(model_path):
|
60 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
61 |
+
tokenizer.eos_token = "<|im_end|>"
|
62 |
+
tokenizer.pad_token = "<|endoftext|>"
|
63 |
+
tokenizer.eos_token_id = tokenizer.convert_tokens_to_ids(tokenizer.eos_token)
|
64 |
+
tokenizer.pad_token_id = tokenizer.convert_tokens_to_ids(tokenizer.pad_token)
|
65 |
+
tokenizer.padding_side = "left"
|
66 |
+
|
67 |
+
model = AutoModelForCausalLM.from_pretrained(model_path, device_map='auto',torch_dtype=torch.bfloat16, trust_remote_code=True)
|
68 |
+
return model, tokenizer
|
69 |
+
|
70 |
+
# Example usage for a natural language question (Text-to-SQL)
|
71 |
+
# Make sure your input string ends with "<|im_start|>assistant
|
72 |
+
" for generation
|
73 |
+
text_list = ["""
|
74 |
+
<|im_start|>user
|
75 |
+
Your task is to write a SQLite query given a natural language question and a database schema.
|
76 |
+
You need to generate the SQL query that answers the question correctly.
|
77 |
+
|
78 |
+
For example, to find out the names of all the songs, given:
|
79 |
+
CREATE TABLE songs (
|
80 |
+
song_id INTEGER PRIMARY KEY,
|
81 |
+
song_name TEXT
|
82 |
+
);
|
83 |
+
Question: What are the names of all the songs?
|
84 |
+
SQL: SELECT song_name FROM songs
|
85 |
+
|
86 |
+
To find the artist of the song 'Yesterday', given:
|
87 |
+
CREATE TABLE songs (
|
88 |
+
song_id INTEGER PRIMARY KEY,
|
89 |
+
song_name TEXT,
|
90 |
+
artist_id INTEGER
|
91 |
+
);
|
92 |
+
CREATE TABLE artists (
|
93 |
+
artist_id INTEGER PRIMARY KEY,
|
94 |
+
artist_name TEXT
|
95 |
+
);
|
96 |
+
Question: Who is the artist of the song 'Yesterday'?
|
97 |
+
SQL: SELECT T2.artist_name FROM songs AS T1 JOIN artists AS T2 ON T1.artist_id = T2.artist_id WHERE T1.song_name = 'Yesterday'
|
98 |
+
|
99 |
+
Now, answer the following question.
|
100 |
+
Question: How many records are there in the table 'songs'?
|
101 |
+
SQL:
|
102 |
+
<|im_end|>
|
103 |
+
<|im_start|>assistant
|
104 |
+
"""]
|
105 |
+
|
106 |
+
model, tokenizer = load_model_tokenizer(model_dir)
|
107 |
+
inputs = tokenizer(text_list, return_tensors='pt', padding=True, add_special_tokens=False).to('cuda')
|
108 |
+
input_ids = inputs["input_ids"]
|
109 |
+
attention_mask = inputs["attention_mask"]
|
110 |
+
generation_config = GenerationConfig(
|
111 |
+
eos_token_id=tokenizer.eos_token_id,
|
112 |
+
pad_token_id=tokenizer.pad_token_id,
|
113 |
+
temperature=0.1,
|
114 |
+
max_new_tokens=512,
|
115 |
+
num_return_sequences=1,
|
116 |
+
num_beams=1,
|
117 |
+
top_p=0.95,
|
118 |
+
do_sample=False
|
119 |
+
)
|
120 |
+
outputs = model.generate(
|
121 |
+
inputs= input_ids,
|
122 |
+
attention_mask=attention_mask,
|
123 |
+
**generation_config.to_dict()
|
124 |
+
)
|
125 |
+
gen_text = tokenizer.batch_decode(outputs[:, input_ids.shape[1]:], skip_special_tokens=True)
|
126 |
+
print(gen_text[0])
|
127 |
+
|
128 |
+
# Expected output: SELECT count(*) FROM songs
|
129 |
+
```
|
130 |
+
|
131 |
+
## Citation
|
132 |
+
|
133 |
+
If you find our work useful or helpful for your R&D works, please feel free to cite our paper as below.
|
134 |
+
|
135 |
+
```bibtex
|
136 |
+
@misc{sheng2025cscsqlcorrectiveselfconsistencytexttosql,
|
137 |
+
title={CSC-SQL: Corrective Self-Consistency in Text-to-SQL via Reinforcement Learning},
|
138 |
+
author={Lei Sheng and Shuai-Shuai Xu},
|
139 |
+
year={2025},
|
140 |
+
eprint={2505.13271},
|
141 |
+
archivePrefix={arXiv},
|
142 |
+
primaryClass={cs.CL},
|
143 |
+
url={https://arxiv.org/abs/2505.13271},
|
144 |
+
}
|
145 |
+
```
|