Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,4 +13,5 @@
# limitations under the License.

from .configuration_utils import PretrainedConfig
from .hf_state_dict_utils import BatchNormHFStateDictMixin
from .model_utils import PretrainedModel
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


class BatchNormHFStateDictMixin:
def _get_forward_key_rules(self):
return [
("_mean", "_mean", "running_mean"),
("_variance", "_variance", "running_var"),
]

def _get_reverse_key_rules(self):
return [
("running_mean", "running_mean", "_mean"),
("running_var", "running_var", "_variance"),
]

def get_hf_state_dict(self, *args, **kwargs):

try:
super().get_hf_state_dict(*args, **kwargs)
except NotImplementedError:
pass

model_state_dict = self.state_dict(*args, **kwargs)
hf_state_dict = {}
rules = self._get_forward_key_rules()

for old_key, value in model_state_dict.items():
new_key = old_key
for match_key, old_sub, new_sub in rules:
if match_key in old_key:
new_key = old_key.replace(old_sub, new_sub)
break
hf_state_dict[new_key] = value
return hf_state_dict

def set_hf_state_dict(self, state_dict, *args, **kwargs):

try:
super().set_hf_state_dict(state_dict, *args, **kwargs)
except NotImplementedError:
pass

key_mapping = {}
rules = self._get_reverse_key_rules()

for old_key in list(state_dict.keys()):
for match_key, old_sub, new_sub in rules:
if match_key in old_key:
key_mapping[old_key] = old_key.replace(old_sub, new_sub)
break

for old_key, new_key in key_mapping.items():
state_dict[new_key] = state_dict.pop(old_key)
return self.set_state_dict(state_dict, *args, **kwargs)
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ def _transpose_hf_weight(key, weight):
else:
weight = tp_fn(py_safe_slice_)
else:
weight = py_safe_slice_[:]
# HACK
if len(py_safe_slice_.get_shape()) == 0:
logging.debug("Ignore empty shape this moment")
else:
weight = py_safe_slice_[:]

if not return_numpy and device == "expected":
weight = weight._copy_to(
Expand Down Expand Up @@ -1841,13 +1845,12 @@ def from_pretrained(
):
raise NotImplementedError
else:
try:
transpose_weight_keys = model.get_transpose_weight_keys()
except NotImplementedError:
if convert_from_hf:
raise ValueError("`convert_from_hf=True` is not supported")
else:
transpose_weight_keys = None
transpose_weight_keys = None
if convert_from_hf:
try:
transpose_weight_keys = model.get_transpose_weight_keys()
except NotImplementedError:
pass
state_dict = load_state_dict(
resolved_archive_file,
convert_from_hf=convert_from_hf,
Expand Down
15 changes: 15 additions & 0 deletions paddlex/inference/models/image_unwarping/modeling/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .uvdoc import UVDocNet
Loading