-
Notifications
You must be signed in to change notification settings - Fork 310
Gpt2safetensors #2459
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
base: master
Are you sure you want to change the base?
Gpt2safetensors #2459
Conversation
Summary of ChangesHello @LakshmiKalaKadali, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly extends the model export capabilities by enabling the conversion of Keras GPT-2 models to the Hugging Face Safetensors format. This allows for seamless interoperability between Keras-based GPT-2 implementations and the broader Hugging Face ecosystem, facilitating easier model sharing and deployment. The changes involve adding specific conversion logic for GPT-2's architecture and tokenizer, integrating this into the existing export framework, and providing a robust testing mechanism to ensure the fidelity of the converted models. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request adds functionality to convert Keras-based GPT-2 models to the Hugging Face safetensors format. This involves a new export script for GPT-2, updates to the generic Hugging Face exporter, and modifications to the GPT-2 tokenizer and checkpoint conversion script. The changes are generally well-structured, but there are some critical issues. I've identified violations of the backend-agnostic principle, a potential runtime error in the exporter logic, and some maintainability concerns. My feedback includes suggestions to use keras.ops for backend-agnostic tensor operations, fix a critical bug, and improve code style and dependency management.
| if ( | ||
| "lm_head.weight" in weights_dict_torch | ||
| and "transformer.wte.weight" in weights_dict_torch | ||
| ): | ||
| wte = weights_dict_torch["transformer.wte.weight"] | ||
| lm = weights_dict_torch["lm_head.weight"] | ||
|
|
||
| if wte.data_ptr() == lm.data_ptr(): | ||
| weights_dict_torch["lm_head.weight"] = lm.clone().contiguous() |
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.
There is a potential NameError here. The variables wte and lm are defined within an if block but used in a subsequent, un-nested if block on line 106. If the condition on line 99 is false, wte and lm will not be defined, causing a runtime error. The second if statement should be nested inside the first.
| if ( | |
| "lm_head.weight" in weights_dict_torch | |
| and "transformer.wte.weight" in weights_dict_torch | |
| ): | |
| wte = weights_dict_torch["transformer.wte.weight"] | |
| lm = weights_dict_torch["lm_head.weight"] | |
| if wte.data_ptr() == lm.data_ptr(): | |
| weights_dict_torch["lm_head.weight"] = lm.clone().contiguous() | |
| if ( | |
| "lm_head.weight" in weights_dict_torch | |
| and "transformer.wte.weight" in weights_dict_torch | |
| ): | |
| wte = weights_dict_torch["transformer.wte.weight"] | |
| lm = weights_dict_torch["lm_head.weight"] | |
| if wte.data_ptr() == lm.data_ptr(): | |
| weights_dict_torch["lm_head.weight"] = lm.clone().contiguous() |
| num_params = PRESET_MAP[FLAGS.preset][0] | ||
| hf_model_name = PRESET_MAP[FLAGS.preset][1] | ||
|
|
||
| os.system("pip install requests") |
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.
| import json | ||
| import os | ||
| import shutil |
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.
| q_w = keras_model.get_layer( | ||
| f"transformer_layer_{i}" | ||
| )._self_attention_layer._query_dense.kernel | ||
| k_w = keras_model.get_layer( | ||
| f"transformer_layer_{i}" | ||
| )._self_attention_layer._key_dense.kernel | ||
| v_w = keras_model.get_layer( | ||
| f"transformer_layer_{i}" | ||
| )._self_attention_layer._value_dense.kernel |
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.
Accessing private layer attributes like _self_attention_layer and its sub-layers makes this code brittle. If the internal structure of TransformerDecoder changes, this export script will break. It would be more robust to expose these weights via a public API on the layer to create a more stable interface.
|
Resolve the gemini suggested changes and mark them as resolved for the respective comments, make the changes as backend agnostic, not specific to tf. |
|
Sure Sachin, I am working on it. Thank You
…On Wed, Nov 26, 2025 at 5:37 AM Sachin Prasad ***@***.***> wrote:
*sachinprasadhs* left a comment (keras-team/keras-hub#2459)
<#2459 (comment)>
Resolve the gemini suggested changes and mark them as resolved for the
respective comments, make the changes as backend agnostic, not specific to
tf.
Once these comments are addressed, I will go thorough the files in detail.
—
Reply to this email directly, view it on GitHub
<#2459 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BDVX3HLEK7VQA2OGD4ODON336TVKRAVCNFSM6AAAAACNAUC4WOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTKNZYGEZTOMJYGQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
GPT2 model conversion from keras to hf safetensors format. colab gist