Qwen3 H5 exporting to HF safetensor format#2571
Qwen3 H5 exporting to HF safetensor format#2571LakshmiKalaKadali wants to merge 1 commit intokeras-team:masterfrom
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 enhances the KerasHub export pipeline by adding full support for the Qwen3 model family. It allows users to convert KerasHub Qwen3 models, including their configurations and weights, into the Hugging Face safetensors format, facilitating interoperability with the broader Hugging Face ecosystem. The changes include new conversion logic for model parameters and tokenizer settings, along with robust testing to ensure accurate transformation. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
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.
Code Review
This pull request introduces support for exporting KerasHub Qwen3 models to the Hugging Face safetensors format. The implementation is clean and follows the existing structure for model exporters. The new conversion logic in keras_hub/src/utils/transformers/export/qwen3.py is well-written, and the addition of keras_hub/src/utils/transformers/export/qwen3_test.py provides a thorough integration test that validates the end-to-end export process. The changes are a valuable extension to the KerasHub export pipeline. I have a few minor suggestions to improve code style and consistency.
| # 2. BPE Models (Qwen) | ||
| elif tokenizer_type == "QwenTokenizer": | ||
| # 2. BPE Models (Qwen/Qwen3) | ||
| elif tokenizer_type in ["QwenTokenizer","Qwen3Tokenizer"]: |
There was a problem hiding this comment.
For better readability and to adhere to standard Python formatting conventions, please add a space after the comma in the list. This is a minor style issue that a formatter like ruff would typically correct.
| elif tokenizer_type in ["QwenTokenizer","Qwen3Tokenizer"]: | |
| elif tokenizer_type in ["QwenTokenizer", "Qwen3Tokenizer"]: |
References
- The style guide requires using
rufffor code formatting. This change aligns with commonruffconfigurations for list formatting. (link)
| "unk_token": None, | ||
| "model_max_length": 32768, | ||
| } | ||
|
No newline at end of file |
There was a problem hiding this comment.
Please add a newline at the end of the file. It's a standard convention for text files and is often enforced by formatters like ruff to prevent issues with file concatenation and some tools.
References
- The style guide requires using
rufffor code formatting. Standardruffconfigurations enforce a final newline in files. (link)
| } | ||
| keras_logits = keras_model(keras_inputs) | ||
|
|
||
| import torch |
| keras_logits_np = ops.convert_to_numpy(keras_logits) | ||
| hf_logits_np = hf_logits.detach().cpu().numpy() | ||
|
|
||
| self.assertAllClose(keras_logits_np, hf_logits_np, atol=1e-3, rtol=1e-3) No newline at end of file |
There was a problem hiding this comment.
Please add a newline at the end of the file. It's a standard convention for text files and is often enforced by formatters like ruff to prevent issues with file concatenation and some tools.
References
- The style guide requires using
rufffor code formatting. Standardruffconfigurations enforce a final newline in files. (link)
This PR extends the KerasHub export pipeline to support the Qwen3 model family. It enables users to convert KerasHub Qwen3Backbone models into the Hugging Face safetensors format, allowing for seamless integration with the transformers ecosystem.
The following changes has done:
New Conversion Logic (qwen3.py)
Config Mapping: Implemented get_qwen3_config which maps KerasHub configuration to Hugging Face's Qwen2Config.
Weight Mapping: Implemented get_qwen3_weights_map to handle:
Transposition: Converting Keras EinsumDense kernels (Input, Output) to Hugging Face Linear weights (Output, Input).
Bias Flattening: Reshaping Attention biases from (Heads, Dim) to (Hidden,).