mirror of
https://github.com/modelscope/modelscope.git
synced 2025-12-25 20:49:37 +01:00
Merge branch 'master-github' into master-merge-internal20230916
This commit is contained in:
@@ -243,7 +243,8 @@ class HubApi:
|
||||
tag: Optional[str] = None,
|
||||
revision: Optional[str] = DEFAULT_REPOSITORY_REVISION,
|
||||
original_model_id: Optional[str] = None,
|
||||
ignore_file_pattern: Optional[Union[List[str], str]] = None):
|
||||
ignore_file_pattern: Optional[Union[List[str], str]] = None,
|
||||
lfs_suffix: Optional[Union[str, List[str]]] = None):
|
||||
"""Upload model from a given directory to given repository. A valid model directory
|
||||
must contain a configuration.json file.
|
||||
|
||||
@@ -281,6 +282,7 @@ class HubApi:
|
||||
branch and push to it.
|
||||
original_model_id (str, optional): The base model id which this model is trained from
|
||||
ignore_file_pattern (`Union[List[str], str]`, optional): The file pattern to ignore uploading
|
||||
lfs_suffix (`List[str]`, optional): File types to use LFS to manage. examples: '*.safetensors'.
|
||||
|
||||
Raises:
|
||||
InvalidParameter: Parameter invalid.
|
||||
@@ -349,6 +351,10 @@ class HubApi:
|
||||
date = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
|
||||
commit_message = '[automsg] push model %s to hub at %s' % (
|
||||
model_id, date)
|
||||
if lfs_suffix is not None:
|
||||
lfs_suffix_list = [lfs_suffix] if isinstance(lfs_suffix, str) else lfs_suffix
|
||||
for suffix in lfs_suffix_list:
|
||||
repo.add_lfs_type(suffix)
|
||||
repo.push(
|
||||
commit_message=commit_message,
|
||||
local_branch=revision,
|
||||
|
||||
@@ -105,7 +105,7 @@ class Repository:
|
||||
examples '*.safetensors'
|
||||
"""
|
||||
os.system(
|
||||
"printf '%s filter=lfs diff=lfs merge=lfs -text\n'>>%s" %
|
||||
"printf '\n%s filter=lfs diff=lfs merge=lfs -text\n'>>%s" %
|
||||
(file_name_suffix, os.path.join(self.model_dir, '.gitattributes')))
|
||||
|
||||
def push(self,
|
||||
|
||||
@@ -13,7 +13,6 @@ from diffusers import (AutoencoderKL, DDPMScheduler, DiffusionPipeline,
|
||||
utils)
|
||||
from diffusers.models import attention
|
||||
from diffusers.utils import deprecation_utils
|
||||
from swift import AdapterConfig, LoRAConfig, PromptConfig, Swift
|
||||
from transformers import CLIPTextModel, CLIPTokenizer
|
||||
|
||||
from modelscope import snapshot_download
|
||||
@@ -26,6 +25,7 @@ from modelscope.outputs import OutputKeys
|
||||
from modelscope.utils.checkpoint import save_checkpoint, save_configuration
|
||||
from modelscope.utils.config import Config
|
||||
from modelscope.utils.constant import ModelFile, Tasks
|
||||
from modelscope.utils.import_utils import is_swift_available
|
||||
from .control_sd_lora import ControlLoRATuner
|
||||
|
||||
utils.deprecate = lambda *arg, **kwargs: None
|
||||
@@ -34,6 +34,9 @@ attention.deprecate = lambda *arg, **kwargs: None
|
||||
|
||||
__tuner_MAP__ = {'lora': LoRATuner, 'control_lora': ControlLoRATuner}
|
||||
|
||||
if is_swift_available():
|
||||
from swift import AdapterConfig, LoRAConfig, PromptConfig, Swift
|
||||
|
||||
|
||||
@MODELS.register_module(
|
||||
Tasks.efficient_diffusion_tuning,
|
||||
@@ -110,6 +113,10 @@ class EfficientStableDiffusion(TorchModel):
|
||||
self.tuner_name = tuner_name
|
||||
|
||||
if tuner_name == 'swift-lora':
|
||||
if not is_swift_available():
|
||||
raise ValueError(
|
||||
'Please install swift by `pip install ms-swift` to use swift tuners.'
|
||||
)
|
||||
rank = tuner_config[
|
||||
'rank'] if tuner_config and 'rank' in tuner_config else 4
|
||||
lora_config = LoRAConfig(
|
||||
@@ -119,6 +126,10 @@ class EfficientStableDiffusion(TorchModel):
|
||||
use_merged_linear=False)
|
||||
self.unet = Swift.prepare_model(self.unet, lora_config)
|
||||
elif tuner_name == 'swift-adapter':
|
||||
if not is_swift_available():
|
||||
raise ValueError(
|
||||
'Please install swift by `pip install ms-swift` to use swift tuners.'
|
||||
)
|
||||
adapter_length = tuner_config[
|
||||
'adapter_length'] if tuner_config and 'adapter_length' in tuner_config else 10
|
||||
adapter_config = AdapterConfig(
|
||||
@@ -128,6 +139,10 @@ class EfficientStableDiffusion(TorchModel):
|
||||
adapter_length=adapter_length)
|
||||
self.unet = Swift.prepare_model(self.unet, adapter_config)
|
||||
elif tuner_name == 'swift-prompt':
|
||||
if not is_swift_available():
|
||||
raise ValueError(
|
||||
'Please install swift by `pip install ms-swift` to use swift tuners.'
|
||||
)
|
||||
prompt_length = tuner_config[
|
||||
'prompt_length'] if tuner_config and 'prompt_length' in tuner_config else 10
|
||||
prompt_config = PromptConfig(
|
||||
|
||||
@@ -160,6 +160,7 @@ class AutomaticSpeechRecognitionPipeline(Pipeline):
|
||||
token_num_relax=self.cmd['token_num_relax'],
|
||||
decoding_ind=self.cmd['decoding_ind'],
|
||||
decoding_mode=self.cmd['decoding_mode'],
|
||||
fake_streaming=self.cmd['fake_streaming'],
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
@@ -310,13 +311,15 @@ class AutomaticSpeechRecognitionPipeline(Pipeline):
|
||||
'fs': {
|
||||
'model_fs': None,
|
||||
'audio_fs': None
|
||||
}
|
||||
},
|
||||
'fake_streaming': False,
|
||||
}
|
||||
|
||||
frontend_conf = None
|
||||
token_num_relax = None
|
||||
decoding_ind = None
|
||||
decoding_mode = None
|
||||
fake_streaming = False
|
||||
if os.path.exists(outputs['am_model_config']):
|
||||
config_file = open(outputs['am_model_config'], encoding='utf-8')
|
||||
root = yaml.full_load(config_file)
|
||||
@@ -350,6 +353,7 @@ class AutomaticSpeechRecognitionPipeline(Pipeline):
|
||||
cmd['token_num_relax'] = token_num_relax
|
||||
cmd['decoding_ind'] = decoding_ind
|
||||
cmd['decoding_mode'] = decoding_mode
|
||||
cmd['fake_streaming'] = fake_streaming
|
||||
if outputs.__contains__('mvn_file'):
|
||||
cmd['cmvn_file'] = outputs['mvn_file']
|
||||
model_config = self.model_cfg['model_config']
|
||||
@@ -389,6 +393,7 @@ class AutomaticSpeechRecognitionPipeline(Pipeline):
|
||||
'punc_model_file',
|
||||
'punc_infer_config',
|
||||
'param_dict',
|
||||
'fake_streaming',
|
||||
]
|
||||
|
||||
for user_args in user_args_dict:
|
||||
|
||||
@@ -435,24 +435,27 @@ class FilesAstScanning(object):
|
||||
ignored.add(item)
|
||||
return list(set(output) - set(ignored))
|
||||
|
||||
def traversal_files(self, path, check_sub_dir=None):
|
||||
def traversal_files(self, path, check_sub_dir=None, include_init=False):
|
||||
self.file_dirs = []
|
||||
if check_sub_dir is None or len(check_sub_dir) == 0:
|
||||
self._traversal_files(path)
|
||||
self._traversal_files(path, include_init=include_init)
|
||||
else:
|
||||
for item in check_sub_dir:
|
||||
sub_dir = os.path.join(path, item)
|
||||
if os.path.isdir(sub_dir):
|
||||
self._traversal_files(sub_dir)
|
||||
self._traversal_files(sub_dir, include_init=include_init)
|
||||
|
||||
def _traversal_files(self, path):
|
||||
def _traversal_files(self, path, include_init=False):
|
||||
dir_list = os.scandir(path)
|
||||
for item in dir_list:
|
||||
if item.name.startswith('__') or item.name.endswith(
|
||||
'.json') or item.name.endswith('.md'):
|
||||
if item.name == '__init__.py' and not include_init:
|
||||
continue
|
||||
elif (item.name.startswith('__')
|
||||
and item.name != '__init__.py') or item.name.endswith(
|
||||
'.json') or item.name.endswith('.md'):
|
||||
continue
|
||||
if item.is_dir():
|
||||
self._traversal_files(item.path)
|
||||
self._traversal_files(item.path, include_init=include_init)
|
||||
elif item.is_file() and item.name.endswith('.py'):
|
||||
self.file_dirs.append(item.path)
|
||||
elif item.is_file() and 'requirement' in item.name:
|
||||
|
||||
@@ -174,3 +174,9 @@ XFORMERS_IMPORT_ERROR = """
|
||||
{0} requires the timm library but it was not found in your environment. You can install it with pip:
|
||||
`pip install xformers>=0.0.17`
|
||||
"""
|
||||
|
||||
# docstyle-ignore
|
||||
SWIFT_IMPORT_ERROR = """
|
||||
{0} requires the ms-swift library but it was not found in your environment. You can install it with pip:
|
||||
`pip install ms-swift -U`
|
||||
"""
|
||||
|
||||
@@ -310,6 +310,7 @@ REQUIREMENTS_MAAPING = OrderedDict([
|
||||
('open_clip', (is_package_available('open_clip'), OPENCLIP_IMPORT_ERROR)),
|
||||
('taming', (is_package_available('taming'), TAMING_IMPORT_ERROR)),
|
||||
('xformers', (is_package_available('xformers'), XFORMERS_IMPORT_ERROR)),
|
||||
('swift', (is_package_available('swift'), SWIFT_IMPORT_ERROR)),
|
||||
])
|
||||
|
||||
SYSTEM_PACKAGE = set(['os', 'sys', 'typing'])
|
||||
|
||||
@@ -372,7 +372,7 @@ def import_module_from_model_dir(model_dir):
|
||||
"""
|
||||
from pathlib import Path
|
||||
file_scanner = FilesAstScanning()
|
||||
file_scanner.traversal_files(model_dir)
|
||||
file_scanner.traversal_files(model_dir, include_init=True)
|
||||
file_dirs = file_scanner.file_dirs
|
||||
requirements = file_scanner.requirement_dirs
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ datasets>=2.8.0,<=2.13.0
|
||||
einops
|
||||
filelock>=3.3.0
|
||||
gast>=0.2.2
|
||||
ms-swift
|
||||
numpy
|
||||
oss2
|
||||
pandas
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
# Copyright 2022-2023 The Alibaba Fundamental Vision Team Authors. All rights reserved.
|
||||
import os
|
||||
import unittest
|
||||
|
||||
from modelscope.models import Model
|
||||
from modelscope.models.multi_modal import EfficientStableDiffusion
|
||||
from modelscope.pipelines import pipeline
|
||||
from modelscope.utils.constant import Tasks
|
||||
from modelscope.utils.test_utils import test_level
|
||||
@@ -11,6 +11,7 @@ from modelscope.utils.test_utils import test_level
|
||||
class EfficientDiffusionTuningTest(unittest.TestCase):
|
||||
|
||||
def setUp(self) -> None:
|
||||
os.system('pip install ms-swift -U')
|
||||
self.task = Tasks.efficient_diffusion_tuning
|
||||
|
||||
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
||||
@@ -28,6 +29,7 @@ class EfficientDiffusionTuningTest(unittest.TestCase):
|
||||
model_id = 'damo/multi-modal_efficient-diffusion-tuning-lora'
|
||||
model_revision = 'v1.0.2'
|
||||
model = Model.from_pretrained(model_id, model_revision=model_revision)
|
||||
from modelscope.models.multi_modal import EfficientStableDiffusion
|
||||
self.assertTrue(model.__class__ == EfficientStableDiffusion)
|
||||
|
||||
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
||||
@@ -52,6 +54,7 @@ class EfficientDiffusionTuningTest(unittest.TestCase):
|
||||
model_id = 'damo/multi-modal_efficient-diffusion-tuning-control-lora'
|
||||
model_revision = 'v1.0.2'
|
||||
model = Model.from_pretrained(model_id, model_revision=model_revision)
|
||||
from modelscope.models.multi_modal import EfficientStableDiffusion
|
||||
self.assertTrue(model.__class__ == EfficientStableDiffusion)
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
# Copyright 2022-2023 The Alibaba Fundamental Vision Team Authors. All rights reserved.
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
import cv2
|
||||
|
||||
from modelscope.models import Model
|
||||
from modelscope.models.multi_modal import EfficientStableDiffusion
|
||||
from modelscope.pipelines import pipeline
|
||||
from modelscope.utils.constant import Tasks
|
||||
from modelscope.utils.test_utils import test_level
|
||||
@@ -14,6 +14,7 @@ from modelscope.utils.test_utils import test_level
|
||||
class EfficientDiffusionTuningTestSwift(unittest.TestCase):
|
||||
|
||||
def setUp(self) -> None:
|
||||
os.system('pip install ms-swift -U')
|
||||
self.task = Tasks.efficient_diffusion_tuning
|
||||
|
||||
@unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
|
||||
@@ -39,6 +40,7 @@ class EfficientDiffusionTuningTestSwift(unittest.TestCase):
|
||||
model_id = 'damo/multi-modal_efficient-diffusion-tuning-swift-lora'
|
||||
model_revision = 'v1.0.2'
|
||||
model = Model.from_pretrained(model_id, model_revision=model_revision)
|
||||
from modelscope.models.multi_modal import EfficientStableDiffusion
|
||||
self.assertTrue(model.__class__ == EfficientStableDiffusion)
|
||||
|
||||
@unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
|
||||
@@ -64,6 +66,7 @@ class EfficientDiffusionTuningTestSwift(unittest.TestCase):
|
||||
model_id = 'damo/multi-modal_efficient-diffusion-tuning-swift-adapter'
|
||||
model_revision = 'v1.0.2'
|
||||
model = Model.from_pretrained(model_id, model_revision=model_revision)
|
||||
from modelscope.models.multi_modal import EfficientStableDiffusion
|
||||
self.assertTrue(model.__class__ == EfficientStableDiffusion)
|
||||
|
||||
@unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
|
||||
@@ -89,6 +92,7 @@ class EfficientDiffusionTuningTestSwift(unittest.TestCase):
|
||||
model_id = 'damo/multi-modal_efficient-diffusion-tuning-swift-prompt'
|
||||
model_revision = 'v1.0.2'
|
||||
model = Model.from_pretrained(model_id, model_revision=model_revision)
|
||||
from modelscope.models.multi_modal import EfficientStableDiffusion
|
||||
self.assertTrue(model.__class__ == EfficientStableDiffusion)
|
||||
|
||||
|
||||
|
||||
@@ -24,13 +24,31 @@ class AstScaningTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
print(('Testing %s.%s' % (type(self).__name__, self._testMethodName)))
|
||||
self.tmp_dir = tempfile.TemporaryDirectory().name
|
||||
self.tmp_dir2 = tempfile.TemporaryDirectory().name
|
||||
self.test_file = os.path.join(self.tmp_dir, 'test.py')
|
||||
if not os.path.exists(self.tmp_dir):
|
||||
os.makedirs(self.tmp_dir)
|
||||
|
||||
fnames = ['1.py', '2.py', '3.py', '__init__.py']
|
||||
self.folders = ['.', 'a', 'b', 'c']
|
||||
dir_path = self.tmp_dir2
|
||||
folder_dirs = [
|
||||
os.path.join(dir_path, folder) for folder in self.folders
|
||||
]
|
||||
for folder in folder_dirs:
|
||||
os.makedirs(folder, exist_ok=True)
|
||||
for fname in fnames:
|
||||
fpath = os.path.join(folder, fname)
|
||||
with open(fpath, 'w') as f:
|
||||
f.write('hello world')
|
||||
|
||||
for folder in folder_dirs:
|
||||
print(f'folder: {os.listdir(folder)}')
|
||||
|
||||
def tearDown(self):
|
||||
super().tearDown()
|
||||
shutil.rmtree(self.tmp_dir)
|
||||
shutil.rmtree(self.tmp_dir2)
|
||||
|
||||
def test_ast_scaning_class(self):
|
||||
astScaner = AstScanning()
|
||||
@@ -75,6 +93,15 @@ class AstScaningTest(unittest.TestCase):
|
||||
index_0 = list(requirements.keys())[0]
|
||||
self.assertIsInstance(requirements[index_0], list)
|
||||
|
||||
fileScaner.traversal_files(self.tmp_dir2, include_init=False)
|
||||
self.assertTrue(
|
||||
os.path.join(self.tmp_dir2, '__init__.py') not in
|
||||
fileScaner.file_dirs)
|
||||
|
||||
fileScaner.traversal_files(self.tmp_dir2, include_init=True)
|
||||
self.assertTrue(
|
||||
os.path.join(self.tmp_dir2, '__init__.py') in fileScaner.file_dirs)
|
||||
|
||||
def test_file_mtime_md5_method(self):
|
||||
fileScaner = FilesAstScanning()
|
||||
# create first file
|
||||
|
||||
Reference in New Issue
Block a user