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

[Flux] Port Flux Core Model #1864

Open
wants to merge 19 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
Empty file.
44 changes: 44 additions & 0 deletions keras_hub/src/models/flux/convert_weights.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
def convert_mlpembedder_weights(pytorch_model, keras_model):
"""
Convert weights from PyTorch MLPEmbedder to Keras MLPEmbedderKeras.
"""
pytorch_in_layer_weight = (
pytorch_model.in_layer.weight.detach().cpu().numpy()
)
pytorch_in_layer_bias = pytorch_model.in_layer.bias.detach().cpu().numpy()

pytorch_out_layer_weight = (
pytorch_model.out_layer.weight.detach().cpu().numpy()
)
pytorch_out_layer_bias = pytorch_model.out_layer.bias.detach().cpu().numpy()

keras_model.in_layer.set_weights(
[pytorch_in_layer_weight.T, pytorch_in_layer_bias]
)
keras_model.out_layer.set_weights(
[pytorch_out_layer_weight.T, pytorch_out_layer_bias]
)


def convert_selfattention_weights(pytorch_model, keras_model):
"""
Convert weights from PyTorch SelfAttention to Keras SelfAttentionKeras.
"""

# Extract PyTorch weights
pytorch_qkv_weight = pytorch_model.qkv.weight.detach().cpu().numpy()
pytorch_qkv_bias = (
pytorch_model.qkv.bias.detach().cpu().numpy()
if pytorch_model.qkv.bias is not None
else None
)

pytorch_proj_weight = pytorch_model.proj.weight.detach().cpu().numpy()
pytorch_proj_bias = pytorch_model.proj.bias.detach().cpu().numpy()

# Set Keras weights (Dense layers use [weight, bias] format)
keras_model.qkv.set_weights(
[pytorch_qkv_weight.T]
+ ([pytorch_qkv_bias] if pytorch_qkv_bias is not None else [])
)
keras_model.proj.set_weights([pytorch_proj_weight.T, pytorch_proj_bias])
Loading
Loading