Add comprehensive model card for CSC-SQL

#1
by nielsr HF Staff - opened
Files changed (1) hide show
  1. README.md +107 -0
README.md ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: cc-by-nc-4.0
3
+ pipeline_tag: text-generation
4
+ library_name: transformers
5
+ tags:
6
+ - text-to-sql
7
+ - reinforcement-learning
8
+ - qwen2
9
+ - code-generation
10
+ datasets:
11
+ - cycloneboy/bird_train
12
+ ---
13
+
14
+ # CSC-SQL: Corrective Self-Consistency in Text-to-SQL via Reinforcement Learning
15
+
16
+ This repository contains the models and code for the paper [CSC-SQL: Corrective Self-Consistency in Text-to-SQL via Reinforcement Learning](https://arxiv.org/abs/2505.13271).
17
+
18
+ πŸ“– [Paper on arXiv](https://arxiv.org/abs/2505.13271) | πŸ’» [GitHub Repository](https://github.com/CycloneBoy/csc_sql)
19
+
20
+ ## Introduction
21
+
22
+ 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%.
23
+
24
+ ![CSC-SQL Framework](https://github.com/CycloneBoy/csc_sql/raw/main/data/image/csc_sql_framework.png)
25
+
26
+ ## Main Results
27
+
28
+ Performance Comparison of different Text-to-SQL methods on BIRD dev and test dataset. On the BIRD private test set, our 7B model achieves 71.72% execution accuracy, while the 32B model achieves 73.67%.
29
+
30
+ <img src="https://github.com/CycloneBoy/csc_sql/raw/main/data/image/csc_sql_result_main.png" height="500" alt="CSC-SQL Main Results">
31
+
32
+ ## Model Checkpoints
33
+
34
+ The available models are fine-tuned on Qwen2.5-Coder and are accessible on Hugging Face:
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
+ ## Quickstart
46
+
47
+ You can use this model with the `transformers` library. The `config.json` indicates `Qwen2ForCausalLM` as its architecture.
48
+
49
+ ```python
50
+ import torch
51
+ from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
52
+
53
+ model_name = "cycloneboy/CscSQL-Grpo-Qwen2.5-Coder-7B-Instruct"
54
+
55
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
56
+ model = AutoModelForCausalLM.from_pretrained(
57
+ model_name,
58
+ torch_dtype=torch.bfloat16,
59
+ device_map="auto"
60
+ )
61
+ model.eval()
62
+
63
+ # Example usage for text-to-SQL generation
64
+ question = "List the names of all employees."
65
+ db_schema = "CREATE TABLE employees (id INT, name TEXT, salary INT);"
66
+ prompt = f"Question: {question}
67
+ Schema: {db_schema}
68
+ SQL: "
69
+
70
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
71
+
72
+ generation_config = GenerationConfig(
73
+ max_new_tokens=128,
74
+ do_sample=True,
75
+ temperature=0.7,
76
+ top_k=20,
77
+ top_p=0.8,
78
+ repetition_penalty=1.05,
79
+ eos_token_id=tokenizer.eos_token_id,
80
+ pad_token_id=tokenizer.pad_token_id,
81
+ )
82
+
83
+ with torch.no_grad():
84
+ outputs = model.generate(
85
+ **inputs,
86
+ generation_config=generation_config
87
+ )
88
+
89
+ response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
90
+ print(f"Generated SQL: {response}")
91
+ ```
92
+
93
+ ## Citation
94
+
95
+ If you find our work helpful or inspiring, please feel free to cite our paper:
96
+
97
+ ```bibtex
98
+ @misc{sheng2025cscsqlcorrectiveselfconsistencytexttosql,
99
+ title={CSC-SQL: Corrective Self-Consistency in Text-to-SQL via Reinforcement Learning},
100
+ author={Lei Sheng and Shuai-Shuai Xu},
101
+ year={2025},
102
+ eprint={2505.13271},
103
+ archivePrefix={arXiv},
104
+ primaryClass={cs.CL},
105
+ url={https://arxiv.org/abs/2505.13271},
106
+ }
107
+ ```