Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[XPU] add lora optimization #8527

Merged
merged 3 commits into from
Jul 2, 2024
Merged
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
73 changes: 62 additions & 11 deletions paddlenlp/peft/lora/lora_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,78 @@
from ...utils.distributed import distributed_gather
from ...utils.env import LORA_WEIGHTS_NAME, SAFE_PEFT_WEIGHTS_INDEX_NAME
from ...utils.log import logger
from ...utils.tools import get_env_device
from .lora_config import LoRAConfig

try:
from paddle.distributed.fleet.utils.sequence_parallel_utils import (
ColumnSequenceParallelLinear,
RowSequenceParallelLinear,
)

from .lora_layers import (
ColumnParallelLoRALinear,
ColumnParallelLoRAMergedLinear,
ColumnSequenceParallelLoRALinear,
LoRAConv2D,
LoRALinear,
LoRAMergedLinear,
RowParallelLoRALinear,
RowSequenceParallelLoRALinear,
)
except:
pass


def get_lora_layers():
try:
if get_env_device() == "xpu":
# If paddle_xpu is not installed, just use PaddleNLP's native lora layers
from paddle_xpu.layers.nn.lora_layers import (

Check warning on line 63 in paddlenlp/peft/lora/lora_model.py

View check run for this annotation

Codecov / codecov/patch

paddlenlp/peft/lora/lora_model.py#L63

Added line #L63 was not covered by tests
XPUColumnParallelLoRALinear as ColumnParallelLoRALinear,
)
from paddle_xpu.layers.nn.lora_layers import (

Check warning on line 66 in paddlenlp/peft/lora/lora_model.py

View check run for this annotation

Codecov / codecov/patch

paddlenlp/peft/lora/lora_model.py#L66

Added line #L66 was not covered by tests
XPUColumnSequenceParallelLoRALinear as ColumnSequenceParallelLoRALinear,
)
from paddle_xpu.layers.nn.lora_layers import XPULoRALinear as LoRALinear
from paddle_xpu.layers.nn.lora_layers import (

Check warning on line 70 in paddlenlp/peft/lora/lora_model.py

View check run for this annotation

Codecov / codecov/patch

paddlenlp/peft/lora/lora_model.py#L69-L70

Added lines #L69 - L70 were not covered by tests
XPURowParallelLoRALinear as RowParallelLoRALinear,
)
from paddle_xpu.layers.nn.lora_layers import (

Check warning on line 73 in paddlenlp/peft/lora/lora_model.py

View check run for this annotation

Codecov / codecov/patch

paddlenlp/peft/lora/lora_model.py#L73

Added line #L73 was not covered by tests
XPURowSequenceParallelLoRALinear as RowSequenceParallelLoRALinear,
)

from .lora_layers import (

Check warning on line 77 in paddlenlp/peft/lora/lora_model.py

View check run for this annotation

Codecov / codecov/patch

paddlenlp/peft/lora/lora_model.py#L77

Added line #L77 was not covered by tests
ColumnParallelLoRAMergedLinear,
LoRAConv2D,
LoRAMergedLinear,
)

else:
raise ImportError # Force to use the fallback if not XPU
except ImportError:
from .lora_layers import (
ColumnParallelLoRALinear,
ColumnParallelLoRAMergedLinear,
ColumnSequenceParallelLoRALinear,
LoRAConv2D,
LoRALinear,
LoRAMergedLinear,
RowParallelLoRALinear,
RowSequenceParallelLoRALinear,
)

return {
"ColumnParallelLoRALinear": ColumnParallelLoRALinear,
"ColumnParallelLoRAMergedLinear": ColumnParallelLoRAMergedLinear,
"ColumnSequenceParallelLoRALinear": ColumnSequenceParallelLoRALinear,
"LoRAConv2D": LoRAConv2D,
"LoRALinear": LoRALinear,
"LoRAMergedLinear": LoRAMergedLinear,
"RowParallelLoRALinear": RowParallelLoRALinear,
"RowSequenceParallelLoRALinear": RowSequenceParallelLoRALinear,
}


lora_layers = get_lora_layers()
ColumnParallelLoRALinear = lora_layers["ColumnParallelLoRALinear"]
ColumnParallelLoRAMergedLinear = lora_layers["ColumnParallelLoRAMergedLinear"]
ColumnSequenceParallelLoRALinear = lora_layers["ColumnSequenceParallelLoRALinear"]
LoRAConv2D = lora_layers["LoRAConv2D"]
LoRALinear = lora_layers["LoRALinear"]
LoRAMergedLinear = lora_layers["LoRAMergedLinear"]
RowParallelLoRALinear = lora_layers["RowParallelLoRALinear"]
RowSequenceParallelLoRALinear = lora_layers["RowSequenceParallelLoRALinear"]

try:
from ...quantization.quantization_linear import (
ColumnParallelQuantizationLinear,
Expand Down
Loading