Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Step 2.5 Fine Tune model #140

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions notebooks/demo-nm/fine-tune-model.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Fine Tune Model"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Neural Magic hosts many models, including BERT models sparsified on an English text corpus with a masked language modeling training scheme. These sparse models can then finetune to your dataset like you normally would with a dense model using SparseML and recipes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This import will install a sparsification compatible version of transformers for SparseML.\n",
"from sparseml.transformers import question_answering, export_onnx"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load Dataset"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#TODO: Load dataset."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Fine Tune Model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"question_answering(\n",
" output_dir=\"sparse_quantized_bert-question_answering_squad\",\n",
" model_name_or_path=\"zoo:nlp/masked_language_modeling/bert-base/pytorch/huggingface/wikipedia_bookcorpus/12layer_pruned80_quant-none-vnni\", #noqa: E501\n",
" recipe=\"zoo:nlp/masked_language_modeling/bert-base/pytorch/huggingface/wikipedia_bookcorpus/12layer_pruned80_quant-none-vnni?recipe_type=transfer-question_answering\", #noqa: E501\n",
" distill_teacher=\"path/to/dense-teacher-trained\",\n",
" dataset_name=\"custom-dataset\",\n",
" do_train=True,\n",
" do_eval=True,\n",
" evaluation_strategy=\"epoch\",\n",
" fp16=True,\n",
" seed=21636,\n",
" per_device_train_batch_size=16,\n",
" per_device_eval_batch_size=24,\n",
" preprocessing_num_workers=6,\n",
" save_strategy=\"epoch\",\n",
" save_total_limit=1\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Store model in ONNX format for deepsparse engine"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"export_onnx(\n",
" model_path=\"sparse_quantized_bert-text_classification_squad\",\n",
" task='question-answering',\n",
" sequence_length=128\n",
")"
]
}
],
"metadata": {
"language_info": {
"name": "python"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
15 changes: 15 additions & 0 deletions overlays/nm-fine-tune/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
sparsezoo = "*"
sparseml = "*"
torch = "<1.9"
boto3 = "*"

[dev-packages]

[requires]
python_version = "3.8"
Loading