Skip to main content



SWIFT(Scalable lightWeight Infrastructure for Fine-Tuning)

Introduction

SWIFT (Scalable lightWeight Infrastructure for Fine-Tuning) is an extensible lightweight tool designed for model fine-tuning. Its core focus lies in implementing various efficient fine-tuning methods, including parameter-efficient, memory-efficient, and time-efficient approaches. SWIFT is capable of supporting efficient fine-tuning of models from diverse domains within the ModelScope community, with primary emphasis on vision-based task models and large language models in NLP. Additionally, SWIFT is fully compatible with Peft, enabling users to directly utilize Peft’s interface for fine-tuning models stored in the ModelScope hub.

Supported methods:

  1. LoRA: LORA: LOW-RANK ADAPTATION OF LARGE LANGUAGE MODELS

  2. Adapter: Parameter-Efficient Transfer Learning for NLP

  3. Prompt Tuning: Visual Prompt Tuning

  4. All the tuners supported by Peft can be used here.

Supported features:

  1. All the model-id passed into SWIFT will be downloaded from the model hub of ModelScope to increase the speed.
  2. Tuners provided by SWIFT can be used together, which means you can use multiple tuners at a time.

Check the usable LLM examples here: SFT and Inference examples

git clone https://github.com/modelscope/swift.git
cd swift/examples/pytorch/llm
# sft
bash run_sft.sh
# inference
bash run_infer.sh
  • Supported models: baichuan-7b, baichuan-13b, chatglm2-6b, llama2-7b, llama2-13b, openbuddy-llama2-13b, ...

  • Supported datasets: alpaca-en, alpaca-zh, ...

  • Supported sft methods: lora, full, ...

  • Continuously updated...

Getting started

SWIFT supports multiple tuners, and also tuners provided by Peft. To use the these tuners, please call:

from swift import Swift
model = Swift.prepare_model(model, config, extra_state_keys=['...'])

The code above gives you a model with a randomly initialized tuner. The input model is an instance of torch.nn.Module, config is a subclass instance of SwiftConfig or PeftConfig. extra_state_keys is the extra module weights(like the linear head) to be trained and stored in the output dir.

If you want to use multiple tuners simultaneously, please call:

from swift import Swift, LoRAConfig, PromptConfig
model = Swift.prepare_model(model, {'lora': LoRAConfig(...), 'prompt': PromptConfig(...)})

You can all save_pretrained and push_to_hub after finetuning:

from swift import push_to_hub
model.save_pretrained('some-output-folder')
push_to_hub('my-group/some-repo-id-modelscope', 'some-output-folder', token='some-ms-token')

Assume my-group/some-repo-id-modelscope is the model-id in the hub, and some-ms-token is the token for uploading.

Using the model-id to do later inference:

from swift import Swift
model = Swift.from_pretrained(model, 'my-group/some-repo-id-modelscope')

Here shows a runnable example:

import os
import tempfile

# Please install modelscope by `pip install modelscope`
from modelscope import Model

from swift import LoRAConfig, SwiftModel, Swift, push_to_hub

tmp_dir = tempfile.TemporaryDirectory().name
if not os.path.exists(tmp_dir):
    os.makedirs(tmp_dir)


model = Model.from_pretrained('modelscope/Llama-2-7b-ms', device_map='auto')
lora_config = LoRAConfig(target_modules=['q_proj', 'k_proj', 'v_proj'])
model: SwiftModel = Swift.prepare_model(model, lora_config)
# Do some finetuning here
model.save_pretrained(tmp_dir)

push_to_hub('my-group/swift_llama2', output_dir=tmp_dir)
model = Model.from_pretrained('modelscope/Llama-2-7b-ms', device_map='auto')
model = SwiftModel.from_pretrained(model, 'my-group/swift_llama2', device_map='auto')

This is a example that uses transformers for model creation uses SWIFT for efficient tuning.

from swift import Swift, LoRAConfig, AdapterConfig, PromptConfig
from transformers import AutoModelForImageClassification

# init vit model
model = AutoModelForImageClassification.from_pretrained("google/vit-base-patch16-224")

# init lora tuner config
lora_config = LoRAConfig(
    r=10,	# the rank of the LoRA module
    target_modules=['query', 'key', 'value'],	# the modules to be replaced with the end of the module name
    merge_weights=False	# whether to merge weights
)

# init adapter tuner config
adapter_config = AdapterConfig(
    dim=768,	# the dimension of the hidden states
    hidden_pos=0,	# the position of the hidden state to passed into the adapter
    target_modules=r'.*attention.output.dense$',	# the modules to be replaced with regular expression
    adapter_length=10	# the length of the adapter length
)

# init prompt tuner config
prompt_config = PromptConfig(
    dim=768,	# the dimension of the hidden states
    target_modules=r'.*layer\.\d+$',	# the modules to be replaced with regular expression
    embedding_pos=0,	# the position of the embedding tensor
    prompt_length=10,	# the length of the prompt tokens
    attach_front=False	# Whether prompt is attached in front of the embedding
)

# create model with swift. In practice, you can use any of these tuners or a combination of them.
model = Swift.prepare_model(model, {"lora_tuner": lora_config, "adapter_tuner": adapter_config, "prompt_tuner": prompt_config})

# get the trainable parameters of model
model.get_trainable_parameters()
# 'trainable params: 838,776 || all params: 87,406,432 || trainable%: 0.9596273189597764'

You can use the features offered by Peft in SWIFT:

from swift import LoraConfig, Swift
from peft import TaskType
lora_config = LoraConfig(target_modules=['query', 'key', 'value'], task_type=TaskType.CAUSAL_LM)
model_wrapped = Swift.prepare_model(model, lora_config)

# or call from_pretrained to load weights in the modelhub
model_wrapped = Swift.from_pretrained(model, 'some-id-in-the-modelscope-modelhub')

or:

from swift import LoraConfig, get_peft_model, PeftModel
from peft import TaskType
lora_config = LoraConfig(target_modules=['query', 'key', 'value'], task_type=TaskType.CAUSAL_LM)
model_wrapped = get_peft_model(model, lora_config)

# or call from_pretrained to load weights in the modelhub
model_wrapped = PeftModel.from_pretrained(model, 'some-id-in-the-modelscope-modelhub')

The saving strategy between Swift tuners and Peft tuners are slightly different. You can name a tuner of a SWIFT by:

model = Swift.prepare_model(model, {'default': LoRAConfig(...)})
model.save_pretrained('./output')

In the output dir, you will have a dir structure like this:

output
    |-- default
        |-- adapter_config.json
        |-- adapter_model.bin
    |-- adapter_config.json
    |-- adapter_model.bin

The config/weights stored in the output dir is the config of extra_state_keys and the weights of it. This is different from Peft, which stores the weights and config of the default tuner.

Installation

SWIFT is running in Python environment. Please make sure your python version is higher than 3.8.

Please install SWIFT by the pip command:

pip install swift -U

If you want to install SWIFT by source code, please run:

git clone https://github.com/modelscope/swift.git
cd swift
pip install -e .

If you are using source code, please remember install requirements by:

pip install -r requirements/framework.txt

SWIFT requires torch>=1.13.

We also recommend to use SWIFT in our docker image:

docker pull registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu20.04-cuda11.7.1-py38-torch2.0.1-tf1.15.5-1.8.0

Learn More

License

This project is licensed under the Apache License (Version 2.0).

Release files for ms-swift 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ms-swift 1.0.0
File Size Uploaded
ms-swift-1.0.0.tar.gz 48.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ms-swift 1.0.0
File Interpreter ABI Platform
ms_swift-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 111.9 kB

Release files / ms-swift-1.0.0.tar.gz

Download URL ms-swift-1.0.0.tar.gz
Size 48.7 kB
Tags Source
SHA-256 checksum
How to use checksums
d38b43213cd4ca9e951fc7203bb4e2f9d66a19aa52345b4d9cb2a3a2fbcb7a24
BLAKE2b-256 checksum
How to use checksums
51bdf4503b1f4f7b87bd01a0c2d58d16fcd60ed85b0ad2ce4ccd4ba5dc07ca78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.7.13

Release files / ms_swift-1.0.0-py3-none-any.whl

Download URL ms_swift-1.0.0-py3-none-any.whl
Size 63.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3d17494d9ed6567dffdb26338dc0b88d8c5b9e35c6b22e8e0e903b674b58f345
BLAKE2b-256 checksum
How to use checksums
3eb430bc9f59e64042a190920433700d63d5a5cc133f6b36d0bdf24576c0bd69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.7.13

Release history Release notifications | RSS feed

4.5.3

2 release files

4.5.2

2 release files

4.5.1

2 release files

4.5.0

2 release files

4.4.2

2 release files

4.4.1

2 release files

4.4.0

2 release files

4.3.2

2 release files

4.3.1

2 release files

4.3.0

2 release files

4.2.3

2 release files

4.2.2

2 release files

4.2.1

2 release files

4.2.0

2 release files

4.1.3

2 release files

4.1.2

2 release files

4.1.1

2 release files

4.1.0

2 release files

4.0.4

2 release files

4.0.3

2 release files

4.0.2

2 release files

4.0.1

2 release files

4.0.0

2 release files

3.12.6

2 release files

3.12.5

2 release files

3.12.3

2 release files

3.12.2

2 release files

3.12.0

2 release files

3.11.3

2 release files

3.11.2

2 release files

3.11.1

2 release files

3.10.3

2 release files

3.10.2

2 release files

3.10.1

2 release files

3.10.0

2 release files

3.9.3

2 release files

3.9.2

2 release files

3.9.1

2 release files

3.9.0

2 release files

3.8.3

2 release files

3.8.2

2 release files

3.8.1

2 release files

3.8.0

2 release files

3.7.3

2 release files

3.7.2

2 release files

3.7.1

2 release files

3.7.0

2 release files

3.6.4

2 release files

3.6.3

2 release files

3.6.2

2 release files

3.6.1

2 release files

3.6.0

2 release files

3.5.3

2 release files

3.5.2

2 release files

3.5.1

2 release files

3.5.0

2 release files

3.4.1

2 release files

3.4.0

2 release files

3.3.1

2 release files

3.3.0

2 release files

3.2.2

2 release files

3.2.1

2 release files

3.2.0

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.3

2 release files

3.0.2

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.6.1

2 release files

2.6.0

2 release files

2.5.2

2 release files

2.5.1

2 release files

2.4.2

2 release files

2.4.1

2 release files

2.4.0

2 release files

2.3.2

2 release files

2.3.1

2 release files

2.3.0

2 release files

2.2.5

2 release files

2.2.4

2 release files

2.2.3

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.5

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.7.3

2 release files

1.7.2

2 release files

1.7.1

2 release files

1.7.0

2 release files

1.6.3

2 release files

1.6.2

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.4

2 release files

1.5.3

2 release files

1.5.2

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page