-
Notifications
You must be signed in to change notification settings - Fork 634
[pooling]Fix weight loading for st_projector in embed models #4297
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
Open
bukejiyu
wants to merge
5
commits into
PaddlePaddle:develop
Choose a base branch
from
bukejiyu:fix_st_proj
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
# limitations under the License. | ||
""" | ||
|
||
import os | ||
from collections.abc import Iterable | ||
from typing import Optional, TypeVar | ||
|
||
|
@@ -40,36 +41,30 @@ def _load_dense_weights(linear: nn.Linear, folder: str, model_config: "ModelConf | |
from fastdeploy.model_executor.utils import default_weight_loader | ||
|
||
filename = "model.safetensors" | ||
file_path = f"{folder}/{filename}" if folder else filename | ||
file_path = f"{model_config.model}/{folder}/{filename}" if folder else filename | ||
|
||
try: | ||
file_bytes = get_hf_file_to_dict(file_path, model_config.model, model_config.revision) | ||
if not file_bytes: | ||
print(file_path) | ||
if not os.path.exists(file_path): | ||
return False | ||
|
||
state_dict = {} | ||
if filename.endswith(".safetensors"): | ||
import io | ||
# only safetensor now | ||
from safetensors.numpy import load_file | ||
|
||
from safetensors.numpy import load as load_safetensors | ||
|
||
numpy_tensors = load_safetensors(io.BytesIO(file_bytes)) | ||
for key, numpy_array in numpy_tensors.items(): | ||
state_dict[key] = paddle.to_tensor(numpy_array) | ||
else: | ||
import io | ||
|
||
state_dict = paddle.load(io.BytesIO(file_bytes)) | ||
numpy_tensors = load_file(file_path) | ||
for key, numpy_array in numpy_tensors.items(): | ||
state_dict[key] = paddle.to_tensor(numpy_array) | ||
|
||
weight_keys = ["weight", "linear.weight", "dense.weight"] | ||
|
||
for weight_key in weight_keys: | ||
if weight_key in state_dict: | ||
weight_loader = getattr(linear.weight, "weight_loader", default_weight_loader) | ||
weight_loader = getattr(linear.weight, "weight_loader", default_weight_loader()) | ||
weight_loader(linear.weight, state_dict[weight_key].astype(paddle.float32)) | ||
bias_key = weight_key.replace("weight", "bias") | ||
if linear.bias is not None and bias_key in state_dict: | ||
bias_loader = getattr(linear.bias, "weight_loader", default_weight_loader) | ||
bias_loader = getattr(linear.bias, "weight_loader", default_weight_loader()) | ||
bias_loader(linear.bias, state_dict[bias_key].astype(paddle.float32)) | ||
return True | ||
except Exception as e: | ||
|
@@ -80,6 +75,7 @@ def _load_dense_weights(linear: nn.Linear, folder: str, model_config: "ModelConf | |
|
||
def _load_st_projector(model_config: "ModelConfig") -> Optional[nn.Layer]: | ||
try: | ||
print("Loading ST Projector...") | ||
|
||
modules = get_hf_file_to_dict("modules.json", model_config.model, model_config.revision) | ||
if not modules: | ||
return None | ||
|
@@ -98,13 +94,15 @@ def _load_st_projector(model_config: "ModelConfig") -> Optional[nn.Layer]: | |
layer_config = get_hf_file_to_dict(config_path, model_config.model, model_config.revision) | ||
if not layer_config: | ||
continue | ||
bias_attr = paddle.ParamAttr(name="linear_bias", initializer=paddle.nn.initializer.Constant(0)) | ||
linear = nn.Linear( | ||
layer_config.get("in_features", 768), | ||
layer_config.get("out_features", 768), | ||
bias=layer_config.get("bias", True), | ||
layer_config.get("in_features", 768), layer_config.get("out_features", 768), bias_attr=bias_attr | ||
) | ||
if linear.weight._is_initialized: | ||
linear.weight.initialize() | ||
if linear.bias._is_initialized: | ||
linear.bias.initialize() | ||
linear = linear.astype(paddle.float32) | ||
|
||
if not _load_dense_weights(linear, folder, model_config): | ||
continue | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
调试的print删掉吧