File size: 5,337 Bytes
614f056
970f3b3
 
614f056
5c1fd92
970f3b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
614f056
 
970f3b3
614f056
970f3b3
614f056
970f3b3
 
 
614f056
970f3b3
614f056
970f3b3
 
 
614f056
970f3b3
 
614f056
970f3b3
 
614f056
970f3b3
 
 
614f056
970f3b3
614f056
970f3b3
 
614f056
970f3b3
 
 
614f056
970f3b3
 
 
614f056
970f3b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
614f056
970f3b3
614f056
970f3b3
 
 
614f056
970f3b3
 
 
 
 
 
614f056
970f3b3
 
 
614f056
 
970f3b3
614f056
970f3b3
 
 
 
 
 
 
 
 
 
 
 
 
 
614f056
970f3b3
614f056
970f3b3
614f056
 
 
970f3b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
---
license: bsd-3-clause
base_model: Salesforce/codet5p-220m
library_name: transformers
pipeline_tag: text-generation
language:
- en
datasets:
- gretelai/synthetic_text_to_sql
tags:
- sql
- sql-to-text
- code
- codet5p
- text2text-generation
model-index:
- name: codet5p-sql2text
  results:
  - task:
      type: text2text-generation
      name: SQL-to-Text
    dataset:
      name: gretelai/synthetic_text_to_sql
      type: gretelai/synthetic_text_to_sql
      split: test
    metrics:
    - type: bleu
      name: BLEU
      value: 33.1003
    - type: rouge1
      name: ROUGE-1
      value: 66.8589
    - type: rouge2
      name: ROUGE-2
      value: 45.1124
    - type: rougel
      name: ROUGE-L
      value: 57.0609
---

# SQL-to-Text (Salesforce/codet5p-220m)

`Salesforce/codet5p-220m` fine-tuned to explain a SQL query in plain English.

The direction is **SQL -> natural language**: the model takes a query (and,
optionally, the DDL of the tables it touches) and returns a sentence describing
what that query does. It does *not* generate SQL from a question.

## Prompt format

Inputs follow one fixed template; training, evaluation and inference all build
it with the same function, so they cannot drift apart. The schema block is
dropped when no DDL is supplied, and when it is supplied only `CREATE TABLE ...` statements are kept.

```
Explain the following SQL query.

Schema:
<CREATE TABLE statements>

SQL:
<the query>
```

## Usage

```python
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer

model_id = "thealper2/codet5p-sql2text"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)

schema = "CREATE TABLE employees (id INT, name TEXT, salary INT, dept_id INT);"
sql = "SELECT dept_id, AVG(salary) FROM employees GROUP BY dept_id;"
prompt = f"Explain the following SQL query.\n\nSchema:\n{schema}\n\nSQL:\n{sql}"

inputs = tokenizer(
    prompt,
    return_tensors="pt",
    truncation=True,
    max_length=256,
)
outputs = model.generate(
    **inputs,
    num_beams=4,
    max_new_tokens=128,
    min_new_tokens=5,
    early_stopping=True,
)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
```

## Training data

[`gretelai/synthetic_text_to_sql`](https://huggingface.co/datasets/gretelai/synthetic_text_to_sql), mapping
`sql` + `sql_context`
to `sql_explanation`.

Preprocessing drops rows that are too short to be a real explanation, removes
exact duplicates and duplicate *inputs*, and removes any training row whose
input also appears in the official test split, so the reported test scores are
not inflated by leakage. The validation split is
3% of the cleaned train
split (seed 42).

Sequence lengths were chosen from the measured token-length distribution:
source 256 tokens, target
128 tokens.


## Training procedure

| Hyper-parameter | Value |
| --- | --- |
| Epochs | 3.00 |
| Learning rate | 0.0001 |
| LR schedule | linear |
| Warmup ratio | 0.0500 |
| Weight decay | 0.0100 |
| Optimiser | adamw_torch |
| Per-device train batch size | 16 |
| Gradient accumulation | 4 |
| Max gradient norm | 1.00 |
| Model selection | eval_rougeL |
| Seed | 42 |
| Effective batch size | 64 |

Trained on a single NVIDIA GeForce RTX 5060 Ti (15.9 GB) with torch 2.11.0+cu128, bf16 mixed precision.

Wall-clock training time: 84 minutes.

## Evaluation

Scored by `evaluate.py` on the full splits with beam search (num_beams=4).

### Generation quality

| Metric | Validation | Test | _meta |
| --- | --- | --- | --- |
| Examples | 2989 | 5850 | - |
| BLEU | 33.65 | 33.10 | - |
| ROUGE-1 | 67.15 | 66.86 | - |
| ROUGE-2 | 45.76 | 45.11 | - |
| ROUGE-L | 57.58 | 57.06 | - |
| Mean generated length | 36.37 | 35.69 | - |
| Loss | 0.5829 | 0.5923 | - |

### SQL-aware faithfulness

Recall metrics ask whether the explanation mentions what the query actually does; the *rate* metrics are error rates, where lower is better -- they measure claims the query does not support.

| Metric | Validation | Test |
| --- | --- | --- |
| Examples | 2989 | 5850 |
| Operation recall | 98.53 | 98.40 |
| Aggregation recall | 98.72 | 98.57 |
| Join mention recall | 98.21 | 98.74 |
| Join table coverage | 98.34 | 98.56 |
| Condition column coverage | 82.98 | 81.99 |
| Condition value coverage | 86.18 | 87.24 |
| Operation over-claim rate | 5.47 | 5.29 |
| Unsupported number rate | 3.98 | 3.18 |
| Unsupported quoted-string rate | 3.20 | 3.18 |
| Unsupported entity rate | 1.38 | 1.72 |

## Limitations

* Trained on synthetic queries and synthetic explanations, so the phrasing
  reflects that generator's style rather than how a particular team documents
  its own queries.
* Explanations are grounded in the query text, not in the data: the model
  cannot know what a column means beyond its name.
* Condition coverage is the weakest area -- long `WHERE` clauses lose some
  columns and literals -- so an explanation may describe a filter less
  precisely than the query applies it. Do not rely on it as an audit of what a
  query returns.
* Inputs are truncated past the configured source length, so very large schemas
  are only partially visible to the model.
* English only.

## Reproduction

```bash
make preprocess
make train
make evaluate
```

Base model: [`Salesforce/codet5p-220m`](https://huggingface.co/Salesforce/codet5p-220m).