-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from younesbelkada/add-push-to-hub
[`core`] Add hub utils
- Loading branch information
Showing
8 changed files
with
426 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# coding=utf-8 | ||
# Copyright 2023-present the HuggingFace Inc. team. | ||
# | ||
# 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. | ||
WEIGHTS_NAME = "adapter_model.bin" | ||
CONFIG_NAME = "adapter_config.json" | ||
|
||
# TODO: add automapping and superclass here? |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# coding=utf-8 | ||
# Copyright 2023-present the HuggingFace Inc. team. | ||
# | ||
# 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. | ||
import unittest | ||
import tempfile | ||
import os | ||
|
||
from peft import LoraConfig, PromptEncoderConfig, PrefixTuningConfig, PromptTuningConfig | ||
|
||
class PeftConfigTestMixin: | ||
all_config_classes = ( | ||
LoraConfig, | ||
PromptEncoderConfig, | ||
PrefixTuningConfig, | ||
PromptTuningConfig, | ||
) | ||
|
||
|
||
class PeftConfigTester(unittest.TestCase, PeftConfigTestMixin): | ||
def test_methods(self): | ||
r""" | ||
Test if all configs have the expected methods. Here we test | ||
- to_dict | ||
- save_pretrained | ||
- from_pretrained | ||
- from_json_file | ||
""" | ||
# test if all configs have the expected methods | ||
for config_class in self.all_config_classes: | ||
config = config_class() | ||
self.assertTrue(hasattr(config, "to_dict")) | ||
self.assertTrue(hasattr(config, "save_pretrained")) | ||
self.assertTrue(hasattr(config, "from_pretrained")) | ||
self.assertTrue(hasattr(config, "from_json_file")) | ||
|
||
def test_task_type(self): | ||
for config_class in self.all_config_classes: | ||
# assert this will not fail | ||
_ = config_class(task_type="test") | ||
|
||
|
||
def test_save_pretrained(self): | ||
r""" | ||
Test if the config is correctly saved and loaded using | ||
- save_pretrained | ||
""" | ||
for config_class in self.all_config_classes: | ||
config = config_class() | ||
with tempfile.TemporaryDirectory() as tmp_dirname: | ||
config.save_pretrained(tmp_dirname) | ||
|
||
config_from_pretrained = config_class.from_pretrained(tmp_dirname) | ||
self.assertEqual(config.to_dict(), config_from_pretrained.to_dict()) | ||
|
||
def test_from_json_file(self): | ||
for config_class in self.all_config_classes: | ||
config = config_class() | ||
with tempfile.TemporaryDirectory() as tmp_dirname: | ||
config.save_pretrained(tmp_dirname) | ||
|
||
config_from_json = config_class.from_json_file(os.path.join(tmp_dirname, "adapter_config.json")) | ||
self.assertEqual(config.to_dict(), config_from_json) | ||
|
||
|
||
def test_to_dict(self): | ||
r""" | ||
Test if the config can be correctly converted to a dict using: | ||
- to_dict | ||
- __dict__ | ||
""" | ||
for config_class in self.all_config_classes: | ||
config = config_class() | ||
self.assertEqual(config.to_dict(), config.__dict__) | ||
self.assertTrue(isinstance(config.to_dict(), dict)) | ||
|
||
|
||
def test_set_attributes(self): | ||
# manually set attributes and check if they are correctly written | ||
for config_class in self.all_config_classes: | ||
config = config_class(peft_type="test") | ||
|
||
# save pretrained | ||
with tempfile.TemporaryDirectory() as tmp_dirname: | ||
config.save_pretrained(tmp_dirname) | ||
|
||
config_from_pretrained = config_class.from_pretrained(tmp_dirname) | ||
self.assertEqual(config.to_dict(), config_from_pretrained.to_dict()) |
Oops, something went wrong.