-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add Upload Guide #1847
Add Upload Guide #1847
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Left some initial comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review, Matt!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
guides/keras_nlp/upload.py
Outdated
""" | ||
|
||
"""shell | ||
pip install -q --upgrade keras-nlp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we might want to add huggingface-hub
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right! Done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, could do this on one line for brevity
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
guides/keras_nlp/upload.py
Outdated
|
||
# Load a user uploaded Classifier from Kaggle Models. | ||
classifier = keras_nlp.models.Classifier.from_preset( | ||
f"kaggle://{kaggle_username}/bert/keras/finetuned_bert" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will this work when running a colab? don't we need a delay before load?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right! I think for creating the guide I should have started from notebook rather than the .py file to catch these kinds of bugs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can add a comment asking the user to make sure the model is uploaded before attempting to load the model.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
guides/keras_nlp/upload.py
Outdated
""" | ||
|
||
"""shell | ||
pip install -q --upgrade keras-nlp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, could do this on one line for brevity
guides/keras_nlp/upload.py
Outdated
causal_lm = keras_nlp.models.CausalLM.from_preset(preset_dir) | ||
|
||
""" | ||
You can also load the `Backbone` and `Tokenizer` objects from this preset directory. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keras_nlp.models.Backbone
and keras_nlp.models.Tokenizer
with backticks. this will trigger auto linking to the docs pages for these classes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
guides/keras_nlp/upload.py
Outdated
""" | ||
|
||
To upload a model we can use `keras_nlp.upload_preset(uri, preset_dir)` API where `uri` has the format of | ||
`kaggle://<KAGGLE_USERNAME>/<MODEL>/<FRAMEWORK>/<VARIATION>` for uploading to Kaggle and `preset_dir` is the directory that the model is saved in. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that for Keras models, the <FRAMEWORK>
should always be keras
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right! Replaced it with Keras
!
|
||
classifier = keras_nlp.models.Classifier.from_preset( | ||
f"kaggle://{kaggle_username}/bert/keras/bert_tiny_imdb" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a follow up PR, and probably is not too urgent, but we might want to add an "advanced" section here on saving a low-level Backbone
and Tokenizer
. I'm not sure what the best training setup to show there is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good! We'll add this later!
guides/keras_nlp/upload.py
Outdated
) | ||
|
||
# Upload to Hugging Face. | ||
keras_nlp.upload_preset(f"hf://{hf_username}/gpt2_imdb", preset_dir) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mattdangerw I added back the HF upload because it can create the delay that we need for the model to be uploaded on Kaggle :D
guides/keras_nlp/upload.py
Outdated
|
||
Running the following uploads the model that is saved in `preset_dir` to Kaggle: | ||
""" | ||
kaggle_username = os.getenv("KAGGLE_USERNAME") # TODO: Assign username. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it to this again to make the autogen run. Kaggle team will have a new release tomorrow with whoami
. I'll update this tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good stuff!
guides/keras_nlp/upload.py
Outdated
# Introduction | ||
|
||
Fine-tuning a machine learning model can yield impressive results for specific tasks. | ||
Uploading your fine-tuned model to a model hub allow you to share it with the broader community. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"hub allow" -> "hub allows"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! Thanks!
guides/keras_nlp/upload.py
Outdated
causal_lm.save_to_preset(preset_dir) | ||
|
||
""" | ||
Let's see what are the files what are the saved files. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: "Let's see the saved files."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed it to your suggestion! Thanks!
guides/keras_nlp/upload.py
Outdated
""" | ||
### Load a Locally Saved Model | ||
|
||
A model that is saved to a local preset, can be loaded using `from_preset`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"preset, can" -> "preset can"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
guides/keras_nlp/upload.py
Outdated
## Upload the Model to a Model Hub | ||
|
||
After saving a preset to a directory, this directory can be uploaded to a model hub such as Kaggle or Hugging Face directly from the KerasNLP library. | ||
To upload the model to Kaggle, the URI should start with `kaggle://` and to upload to Hugging Face, it should start with `hf://`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Is the URI format a requirement? If so, say "... the URI must start..." instead of "should".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is! Replaced "should" with "must"!
guides/keras_nlp/upload.py
Outdated
|
||
""" | ||
To upload a model to Kaggle, first, we need to authenticate with Kaggle. | ||
This can by one of the followings: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"by one of the followings:" -> "in one of the following ways:"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
guides/keras_nlp/upload.py
Outdated
2. Provide a local `~/.kaggle/kaggle.json`. | ||
3. Call `kagglehub.login()`. | ||
|
||
Let's make sure we are logged in before coninuing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"coninuing" -> "continuing"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
guides/keras_nlp/upload.py
Outdated
|
||
""" | ||
To upload a model to Hugging Face, first, we need to authenticate with Hugging Face. | ||
This can by one of the followings: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See previous suggestion.
1. Set environment variables `HF_USERNAME` and `HF_TOKEN`. | ||
2. Call `huggingface_hub.notebook_login()`. | ||
|
||
Let's make sure we are logged in before coninuing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"coninuing" -> "continuing"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review, David!
guides/keras_nlp/upload.py
Outdated
# Introduction | ||
|
||
Fine-tuning a machine learning model can yield impressive results for specific tasks. | ||
Uploading your fine-tuned model to a model hub allow you to share it with the broader community. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! Thanks!
guides/keras_nlp/upload.py
Outdated
causal_lm.save_to_preset(preset_dir) | ||
|
||
""" | ||
Let's see what are the files what are the saved files. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed it to your suggestion! Thanks!
guides/keras_nlp/upload.py
Outdated
""" | ||
### Load a Locally Saved Model | ||
|
||
A model that is saved to a local preset, can be loaded using `from_preset`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
guides/keras_nlp/upload.py
Outdated
## Upload the Model to a Model Hub | ||
|
||
After saving a preset to a directory, this directory can be uploaded to a model hub such as Kaggle or Hugging Face directly from the KerasNLP library. | ||
To upload the model to Kaggle, the URI should start with `kaggle://` and to upload to Hugging Face, it should start with `hf://`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is! Replaced "should" with "must"!
guides/keras_nlp/upload.py
Outdated
|
||
""" | ||
To upload a model to Kaggle, first, we need to authenticate with Kaggle. | ||
This can by one of the followings: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
guides/keras_nlp/upload.py
Outdated
2. Provide a local `~/.kaggle/kaggle.json`. | ||
3. Call `kagglehub.login()`. | ||
|
||
Let's make sure we are logged in before coninuing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
1. Set environment variables `HF_USERNAME` and `HF_TOKEN`. | ||
2. Call `huggingface_hub.notebook_login()`. | ||
|
||
Let's make sure we are logged in before coninuing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
* Upload guide. * KerasNLP upload guide. * Address reviews. * Add classifier example. * Kaggle Hub --> Kaggle Models. * Add model loading. * Replace the toy dataset with IMDB dataset. * Adress reviews. * Some final fixes to make autogen run successful. * Fix classifier name in HF upload. * Reduce batch size. * Convert the code for loading to markdown code block. * Get kaggle username from kagglehub.whoami(). * Run black. * Add notebook and markdown. * Add the guide path. * Address reivews. * Update notebook and markdown files. * Remove upload progress bars from the markdown file. * Remove fine tuning progress bars from the markdown file.
This PR adds a guide to show how to upload to Kaggle and Hugging Face.