Skip to content

Commit 03b17dc

Browse files
A bunch of small features (#944)
1 parent 6e5b58a commit 03b17dc

File tree

8 files changed

+55
-30
lines changed

8 files changed

+55
-30
lines changed

swift/hub/api.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,9 +624,6 @@ def get_cookies():
624624
cookies = pickle.load(f)
625625
for cookie in cookies:
626626
if cookie.is_expired():
627-
logger.warning(
628-
'Authentication has expired, '
629-
'please re-login if you need to access private models or datasets.')
630627
return None
631628
return cookies
632629
return None

swift/hub/errors.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ def handle_http_response(response, logger, cookies, model_id):
8080
response.raise_for_status()
8181
except HTTPError as error:
8282
if cookies is None: # code in [403] and
83-
logger.error(
84-
f'Authentication token does not exist, failed to access model {model_id} which may not exist or may be \
85-
private. Please login first.')
83+
pass
8684
message = _decode_response_error(response)
8785
raise HTTPError('Response details: %s' % message) from error
8886

swift/llm/sft.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ def llm_sft(args: SftArguments) -> Dict[str, Union[str, Any]]:
199199
template, val_dataset, args.max_length, lazy_tokenize=args.lazy_tokenize)
200200
dataset_info = {}
201201
if not args.lazy_tokenize:
202+
td0 = train_dataset[0]
203+
print_example(td0, tokenizer, {})
202204
dataset_info['train_dataset'] = stat_dataset(train_dataset)
203205
if val_dataset is not None:
204206
dataset_info['val_dataset'] = stat_dataset(val_dataset)

swift/trainers/mixin.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,17 @@ def _save(self, output_dir: Optional[str] = None, state_dict=None):
376376
self.model, output_dir, state_dict=state_dict, safe_serialization=save_safetensors)
377377
else:
378378
self.model.save_pretrained(output_dir, state_dict=state_dict, safe_serialization=save_safetensors)
379+
sft_args = getattr(self, 'sft_args', None)
379380
# tokenizer
380-
if self.tokenizer is not None:
381+
from swift import SWIFT_MAPPING
382+
addtional_module_tuners = [
383+
name.lower() for name, (config, cls) in SWIFT_MAPPING.items() if cls.has_additional_modules()
384+
]
385+
if self.tokenizer is not None and sft_args.sft_type not in addtional_module_tuners:
381386
self.tokenizer.save_pretrained(output_dir)
382387
# training_args.bin
383388
torch.save(self.args, os.path.join(output_dir, 'training_args.bin'))
384389
# additional files
385-
sft_args = getattr(self, 'sft_args', None)
386390
if sft_args is not None and sft_args.sft_type == 'full':
387391
additional_files = getattr(self.args, 'additional_saved_files', []) + ['preprocessor_config.json']
388392
if model_dir is not None:

swift/tuners/utils.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import os
66
import shutil
77
import threading
8+
import uuid
89
from dataclasses import asdict, dataclass, field
910
from types import FunctionType
10-
from typing import Dict, List, Optional, OrderedDict, Union
11+
from typing import Dict, Optional, Union
1112

1213
import json
1314
import numpy as np
@@ -176,11 +177,15 @@ def get_activated_adapters(self):
176177

177178
class OffloadHelper:
178179

179-
sub_dir = 'offload_cache'
180-
cache_dir = os.path.join(get_cache_dir(), sub_dir)
181-
shutil.rmtree(cache_dir, ignore_errors=True)
182-
os.makedirs(cache_dir, exist_ok=True)
183-
index = {}
180+
def __init__(self):
181+
sub_dir = os.path.join('offload_cache', str(uuid.uuid4().hex))
182+
self.cache_dir = os.path.join(get_cache_dir(), sub_dir)
183+
shutil.rmtree(self.cache_dir, ignore_errors=True)
184+
os.makedirs(self.cache_dir, exist_ok=True)
185+
self.index = {}
186+
187+
def __del__(self):
188+
shutil.rmtree(self.cache_dir, ignore_errors=True)
184189

185190
@staticmethod
186191
def offload_weight(weight, weight_name, offload_folder, index=None):
@@ -221,26 +226,24 @@ def load_offloaded_weight(weight_file, weight_info):
221226

222227
return weight
223228

224-
@staticmethod
225-
def offload_disk(module: torch.nn.Module, adapter_name, module_key):
229+
def offload_disk(self, module: torch.nn.Module, adapter_name, module_key):
226230
key = adapter_name + ':' + module_key
227231
md5 = hashlib.md5(key.encode('utf-8')).hexdigest()
228-
sub_folder = os.path.join(OffloadHelper.cache_dir, md5)
232+
sub_folder = os.path.join(self.cache_dir, md5)
229233
os.makedirs(sub_folder, exist_ok=True)
230234
state_dict = module.state_dict()
231-
OffloadHelper.index[md5] = {}
235+
self.index[md5] = {}
232236
for key, tensor in state_dict.items():
233-
OffloadHelper.offload_weight(tensor, key, sub_folder, OffloadHelper.index[md5])
237+
OffloadHelper.offload_weight(tensor, key, sub_folder, self.index[md5])
234238

235-
@staticmethod
236-
def load_disk(module: torch.nn.Module, adapter_name, module_key):
239+
def load_disk(self, module: torch.nn.Module, adapter_name, module_key):
237240
key = adapter_name + ':' + module_key
238241
md5 = hashlib.md5(key.encode('utf-8')).hexdigest()
239-
sub_folder = os.path.join(OffloadHelper.cache_dir, md5)
242+
sub_folder = os.path.join(self.cache_dir, md5)
240243
state_dict = {}
241-
for key, value in OffloadHelper.index[md5].items():
244+
for key, value in self.index[md5].items():
242245
file = os.path.join(sub_folder, f'{key}.dat')
243-
state_dict[key] = OffloadHelper.load_offloaded_weight(file, OffloadHelper.index[md5][key])
246+
state_dict[key] = OffloadHelper.load_offloaded_weight(file, self.index[md5][key])
244247
if version.parse(torch.__version__) >= version.parse('2.1.0'):
245248
module.load_state_dict(state_dict, assign=True)
246249
else:
@@ -264,6 +267,8 @@ def load_disk(module: torch.nn.Module, adapter_name, module_key):
264267

265268
class SwiftAdapter:
266269

270+
offload_helper = OffloadHelper()
271+
267272
@staticmethod
268273
def prepare_model(model: torch.nn.Module, config: SwiftConfig, adapter_name: str) -> SwiftOutput:
269274
raise NotImplementedError
@@ -294,7 +299,7 @@ def offload(module: torch.nn.Module, adapter_name, module_key, offload: str):
294299
module.to('cpu')
295300
elif offload == 'meta':
296301
if str(device) != 'meta':
297-
OffloadHelper.offload_disk(module, adapter_name=adapter_name, module_key=module_key)
302+
SwiftAdapter.offload_helper.offload_disk(module, adapter_name=adapter_name, module_key=module_key)
298303
module.to('meta')
299304
else:
300305
raise NotImplementedError
@@ -309,7 +314,7 @@ def load(module: torch.nn.Module, adapter_name, module_key):
309314
module.to(module.origin_device)
310315
delattr(module, 'origin_device')
311316
elif str(device) == 'meta':
312-
OffloadHelper.load_disk(module, adapter_name=adapter_name, module_key=module_key)
317+
SwiftAdapter.offload_helper.load_disk(module, adapter_name=adapter_name, module_key=module_key)
313318
module.to(module.origin_device)
314319
delattr(module, 'origin_device')
315320

swift/ui/app.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,23 @@
1212
locale_dict = {
1313
'title': {
1414
'zh': '🚀SWIFT: 轻量级大模型训练推理框架',
15-
'en': '🚀SWIFT: Scalable lightWeight Infrastructure for Fine-Tuning'
15+
'en': '🚀SWIFT: Scalable lightWeight Infrastructure for Fine-Tuning and Inference'
1616
},
1717
'sub_title': {
1818
'zh':
1919
'请查看 <a href=\"https://github.com/modelscope/swift/tree/main/docs/source\" target=\"_blank\">'
2020
'SWIFT 文档</a>来查看更多功能',
2121
'en':
22-
'Please check <a href=\"https://github.com/modelscope/swift/tree/main/docs/source\" target=\"_blank\">'
22+
'Please check <a href=\"https://github.com/modelscope/swift/tree/main/docs/source_en\" target=\"_blank\">'
2323
'SWIFT Documentation</a> for more usages',
2424
},
25+
'star_beggar': {
26+
'zh':
27+
'喜欢<a href=\"https://github.com/modelscope/swift\" target=\"_blank\">SWIFT</a>就动动手指给我们加个star吧🥺 ',
28+
'en':
29+
'If you like <a href=\"https://github.com/modelscope/swift\" target=\"_blank\">SWIFT</a>, '
30+
'please take a few seconds to star us🥺 '
31+
},
2532
}
2633

2734

@@ -31,6 +38,7 @@ def run_ui():
3138
with gr.Blocks(title='SWIFT WebUI') as app:
3239
gr.HTML(f"<h1><center>{locale_dict['title'][lang]}</center></h1>")
3340
gr.HTML(f"<h3><center>{locale_dict['sub_title'][lang]}</center></h3>")
41+
gr.HTML(f"<h3><center>{locale_dict['star_beggar'][lang]}</center></h3>")
3442
with gr.Tabs():
3543
LLMTrain.build_ui(LLMTrain)
3644
LLMInfer.build_ui(LLMInfer)

swift/ui/llm_train/dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class Dataset(BaseUI):
3737
'en': 'Custom train dataset path'
3838
},
3939
'info': {
40-
'zh': '输入自定义的训练数据集路径,逗号分隔',
41-
'en': 'Extra train files, split by comma'
40+
'zh': '输入自定义的训练数据集路径,空格分隔',
41+
'en': 'Extra train files, split by blank'
4242
}
4343
},
4444
'custom_val_dataset_path': {

swift/ui/llm_train/quantization.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ class Quantization(BaseUI):
1616
'en': 'Quantization'
1717
},
1818
},
19+
'quant_method': {
20+
'label': {
21+
'zh': '量化方式',
22+
'en': 'Quantization method'
23+
},
24+
'info': {
25+
'zh': '如果制定了量化位数,本参数默认为bnb',
26+
'en': 'Default is bnb if quantization_bit is specified'
27+
}
28+
},
1929
'quantization_bit': {
2030
'label': {
2131
'zh': '量化bit数',
@@ -51,6 +61,7 @@ def do_build_ui(cls, base_tab: Type['BaseUI']):
5161
with gr.Accordion(elem_id='quantization_tab', open=False):
5262
with gr.Row():
5363
gr.Dropdown(elem_id='quantization_bit')
64+
gr.Dropdown(elem_id='quant_method')
5465
gr.Dropdown(elem_id='bnb_4bit_comp_dtype')
5566
gr.Dropdown(elem_id='bnb_4bit_quant_type')
5667
gr.Checkbox(elem_id='bnb_4bit_use_double_quant')

0 commit comments

Comments
 (0)