![](https://private-user-images.githubusercontent.com/786476/279227345-8353bb33-bda7-47fe-bbc2-0214ce1e2395.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQwNTk0OTMsIm5iZiI6MTcyNDA1OTE5MywicGF0aCI6Ii83ODY0NzYvMjc5MjI3MzQ1LTgzNTNiYjMzLWJkYTctNDdmZS1iYmMyLTAyMTRjZTFlMjM5NS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwODE5JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDgxOVQwOTE5NTNaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0yMDg4YWM0ZDVmMWRlNDU4ZWQzYmVmNTE1MjM4NWRhZWY2OWZiNGVjNjkyYWU4YTY2OGQ2YzkxODM5NzI1MjQ4JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.h5r7fazp5bZlnMIxW7wKLQ7Yg2KiTA6antiUHN-55YQ)
This repository includes code for fine-tuning a Language Model for text-to-SQL tasks and for generating SQL queries with the fine-tuned model. Both the fine-tuning and generation processes leverage QLoRA, a Quantized Low-Rank Parameter Efficient finetuning method, enabled by Intel's BigDL library on Intel GPUs.
- Python 3.x
- PyTorch
- Transformers library
- Datasets library
- Intel Extension for PyTorch (IPEX)
- Intel BigDL-LLM[XPU]
- Clone this repo.
git clone https://github.com/your_username/your_repository.git
- Install required python packages
pip install -r requirements
- Install Intel BigDL llm package
pip install --pre --upgrade bigdl-llm[xpu] -f https://developer.intel.com/ipex-whl-stable-xpu
- finetune.py : Contains code for fine-tuning a pre-trained Language Model on text-to-SQL tasks.
- generate.py : Contains code for generating SQL queries using a fine-tuned model.
To finetune a model, run the finetune.py
script
python finetune.py
============================================================
Training Parameters:
Foundation model: NousResearch/CodeLlama-7b-hf
Model save path: ./final_model
Device used: xpu
Intel GPU: Intel(R) Data Center GPU Max 1100
Batch size per device: 32
Gradient accum. steps: 4
Warmup steps: 100
Save steps: 20
Evaluation steps: 20
Max steps: 300
Learning rate: 0.0003
Max gradient norm: 0.3
Save total limit: 3
Logging steps: 20
============================================================
![](https://private-user-images.githubusercontent.com/786476/279217587-225935e6-b36a-4633-8bb6-b2ab8c32ef6a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQwNTk0OTMsIm5iZiI6MTcyNDA1OTE5MywicGF0aCI6Ii83ODY0NzYvMjc5MjE3NTg3LTIyNTkzNWU2LWIzNmEtNDYzMy04YmI2LWIyYWI4YzMyZWY2YS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwODE5JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDgxOVQwOTE5NTNaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1lNDQwZWM1MWIzMGRjMmU3NTI3MDk5YmNlNDEyN2ZjNGE2YzFhNDg3Y2QwYjQwZmNkNDMxZGY4NzkyOTdiNzg2JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.KDs1SeYW_fG9xRbGq6MYKoWoojHH1e29dtnmLjLYyv8)
Here is how the loss chart looks at the end of 300 steps of finetuning:
As you can see the loss has a big drop in the intial steps and training loss gradually tapers to around 0.6:
![loss_chart](https://private-user-images.githubusercontent.com/786476/287599049-0c86bf02-93d6-47da-be34-b09d39e6ffea.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQwNTk0OTMsIm5iZiI6MTcyNDA1OTE5MywicGF0aCI6Ii83ODY0NzYvMjg3NTk5MDQ5LTBjODZiZjAyLTkzZDYtNDdkYS1iZTM0LWIwOWQzOWU2ZmZlYS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwODE5JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDgxOVQwOTE5NTNaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1jMDAwYTdjYTVkNWYxZDhkNzFhYjU5MDZmOGIwZDI2NWUyYWJkYTNmMWE3MzRlMzI3MTg5YTZhZjk5NDMwOGU1JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.vIISGHyDkKrSPk6GeGmsczZbZternVehevGKhuAzj1s)
- Downloads a pre-trained model based on the given base model ID.
- Tokenizes the input questions, context, and answers.
- Fine-tunes the model using the tokenized data and qLoRA.
- Saves the fine-tuned model.
- BASE_MODEL: The pre-trained model to use for fine-tuning.
- MODEL_PATH: Path to save the fine-tuned model.
- DEVICE: Device to run the model on.
To generate SQL queries using the fine-tuned model, run the generate.py script.
- Uses either the base model or a fine-tuned model for SQL query generation.
- Loads sample data and generates SQL queries for each sample.
- BASE_MODEL: The base model to use for inference.
- MODEL_PATH: Path to the fine-tuned model.
- LORA_CHECKPOINT: Latest checkpoint for the fine-tuned model.
- TEST_DATA: Path to the test data file.
Following a 15-minute training session, the finetuned model demonstrates enhanced proficiency in generating SQL queries that more accurately reflect the given questions, compared to the base model. With additional training steps, we can anticipate further improvements in the model's response accuracy:
Finetuned model generation:
![](https://private-user-images.githubusercontent.com/786476/287599792-1edf44ea-557b-4156-be2b-bf2716e2b4a5.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQwNTk0OTMsIm5iZiI6MTcyNDA1OTE5MywicGF0aCI6Ii83ODY0NzYvMjg3NTk5NzkyLTFlZGY0NGVhLTU1N2ItNDE1Ni1iZTJiLWJmMjcxNmUyYjRhNS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwODE5JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDgxOVQwOTE5NTNaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0wZjI1OTViZTM4NTA0OGYzODQ0OTMwYzJlMjVjNzQ5ZTdiOTk2NGM3NDFkM2ZmN2U1NzY2NDIxNTg3YWJmNGM0JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.6Azj8fIMjFNKL2oMmie8RFEN2BVVOAAdWhE98o5nWrc)
Base model generation:
![](https://private-user-images.githubusercontent.com/786476/287599827-f873f868-73e9-4bcf-861f-94cdefe34fc1.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQwNTk0OTMsIm5iZiI6MTcyNDA1OTE5MywicGF0aCI6Ii83ODY0NzYvMjg3NTk5ODI3LWY4NzNmODY4LTczZTktNGJjZi04NjFmLTk0Y2RlZmUzNGZjMS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwODE5JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDgxOVQwOTE5NTNaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT05NTdiMzJmMDUzZDgxYWE0OGMxM2FkMWI5NGZhNTZjMjdiNDQ1OGI3ZDdkNmRjOWNiMTFkYmJiM2UyNDgyNmU4JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.3uhW5pF9JUMwKQ_HCQuIov_rj6s0sbawDkwvINwuxjk)
- Default base model for fine-tuning: openlm-research/open_llama_3b
- Model path for saving the fine-tuned LoRA adaptor (incase of interruptions):
./saved_model
- Path for saving task based (here it is text to sql) LoRA adaptors:
./lora_models
- Default dataset for fine-tuning: b-mc2/sql-create-context
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.