vllm-npu 0.4.2__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- vllm/__init__.py +23 -0
- vllm/_custom_ops.py +251 -0
- vllm/attention/__init__.py +13 -0
- vllm/attention/backends/__init__.py +0 -0
- vllm/attention/backends/abstract.py +127 -0
- vllm/attention/backends/flash_attn.py +271 -0
- vllm/attention/backends/flashinfer.py +220 -0
- vllm/attention/backends/rocm_flash_attn.py +374 -0
- vllm/attention/backends/torch_sdpa.py +250 -0
- vllm/attention/backends/xformers.py +393 -0
- vllm/attention/layer.py +56 -0
- vllm/attention/ops/__init__.py +0 -0
- vllm/attention/ops/paged_attn.py +216 -0
- vllm/attention/ops/prefix_prefill.py +792 -0
- vllm/attention/ops/triton_flash_attention.py +810 -0
- vllm/attention/selector.py +91 -0
- vllm/block.py +84 -0
- vllm/config.py +1225 -0
- vllm/core/__init__.py +0 -0
- vllm/core/block/__init__.py +0 -0
- vllm/core/block/block_table.py +295 -0
- vllm/core/block/common.py +199 -0
- vllm/core/block/cpu_gpu_block_allocator.py +228 -0
- vllm/core/block/interfaces.py +205 -0
- vllm/core/block/naive_block.py +318 -0
- vllm/core/block/prefix_caching_block.py +606 -0
- vllm/core/block_manager_v1.py +625 -0
- vllm/core/block_manager_v2.py +258 -0
- vllm/core/evictor_v1.py +105 -0
- vllm/core/evictor_v2.py +127 -0
- vllm/core/interfaces.py +113 -0
- vllm/core/policy.py +45 -0
- vllm/core/scheduler.py +1163 -0
- vllm/distributed/__init__.py +3 -0
- vllm/distributed/communication_op.py +237 -0
- vllm/distributed/device_communicators/__init__.py +0 -0
- vllm/distributed/device_communicators/custom_all_reduce.py +274 -0
- vllm/distributed/device_communicators/pynccl.py +287 -0
- vllm/distributed/device_communicators/pynccl_utils.py +66 -0
- vllm/distributed/parallel_state.py +339 -0
- vllm/distributed/utils.py +136 -0
- vllm/engine/__init__.py +0 -0
- vllm/engine/arg_utils.py +649 -0
- vllm/engine/async_llm_engine.py +737 -0
- vllm/engine/llm_engine.py +784 -0
- vllm/engine/metrics.py +368 -0
- vllm/engine/output_processor/__init__.py +0 -0
- vllm/engine/output_processor/interfaces.py +76 -0
- vllm/engine/output_processor/multi_step.py +142 -0
- vllm/engine/output_processor/single_step.py +284 -0
- vllm/engine/output_processor/stop_checker.py +101 -0
- vllm/engine/output_processor/util.py +19 -0
- vllm/entrypoints/__init__.py +0 -0
- vllm/entrypoints/api_server.py +119 -0
- vllm/entrypoints/llm.py +259 -0
- vllm/entrypoints/openai/__init__.py +0 -0
- vllm/entrypoints/openai/api_server.py +186 -0
- vllm/entrypoints/openai/cli_args.py +115 -0
- vllm/entrypoints/openai/protocol.py +460 -0
- vllm/entrypoints/openai/serving_chat.py +392 -0
- vllm/entrypoints/openai/serving_completion.py +347 -0
- vllm/entrypoints/openai/serving_engine.py +234 -0
- vllm/envs.py +217 -0
- vllm/executor/__init__.py +0 -0
- vllm/executor/cpu_executor.py +152 -0
- vllm/executor/distributed_gpu_executor.py +115 -0
- vllm/executor/executor_base.py +115 -0
- vllm/executor/gpu_executor.py +150 -0
- vllm/executor/multiproc_worker_utils.py +263 -0
- vllm/executor/neuron_executor.py +91 -0
- vllm/executor/ray_gpu_executor.py +327 -0
- vllm/executor/ray_utils.py +119 -0
- vllm/logger.py +153 -0
- vllm/logging/__init__.py +5 -0
- vllm/logging/formatter.py +15 -0
- vllm/lora/__init__.py +0 -0
- vllm/lora/fully_sharded_layers.py +262 -0
- vllm/lora/layers.py +1181 -0
- vllm/lora/lora.py +167 -0
- vllm/lora/models.py +645 -0
- vllm/lora/punica.py +213 -0
- vllm/lora/request.py +32 -0
- vllm/lora/utils.py +98 -0
- vllm/lora/worker_manager.py +251 -0
- vllm/model_executor/__init__.py +7 -0
- vllm/model_executor/guided_decoding/__init__.py +25 -0
- vllm/model_executor/guided_decoding/lm_format_enforcer_decoding.py +70 -0
- vllm/model_executor/guided_decoding/outlines_decoding.py +130 -0
- vllm/model_executor/guided_decoding/outlines_logits_processors.py +184 -0
- vllm/model_executor/layers/__init__.py +0 -0
- vllm/model_executor/layers/activation.py +173 -0
- vllm/model_executor/layers/fused_moe/__init__.py +7 -0
- vllm/model_executor/layers/fused_moe/configs/E=16,N=1344,device_name=NVIDIA_A100-SXM4-40GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=16,N=1344,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=16,N=1344,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=16,N=2688,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=16,N=2688,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=1792,device_name=NVIDIA_A100-SXM4-40GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=1792,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=1792,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=2048,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=2048,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=3584,device_name=NVIDIA_A100-SXM4-40GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=3584,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=3584,device_name=NVIDIA_H100_80GB_HBM3,dtype=float8.json +140 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=3584,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=4096,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=4096,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=7168,device_name=NVIDIA_A100-SXM4-80GB.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=7168,device_name=NVIDIA_H100_80GB_HBM3,dtype=float8.json +146 -0
- vllm/model_executor/layers/fused_moe/configs/E=8,N=7168,device_name=NVIDIA_H100_80GB_HBM3.json +146 -0
- vllm/model_executor/layers/fused_moe/fused_moe.py +479 -0
- vllm/model_executor/layers/layernorm.py +71 -0
- vllm/model_executor/layers/linear.py +709 -0
- vllm/model_executor/layers/logits_processor.py +115 -0
- vllm/model_executor/layers/ops/__init__.py +0 -0
- vllm/model_executor/layers/ops/rand.py +157 -0
- vllm/model_executor/layers/ops/sample.py +406 -0
- vllm/model_executor/layers/quantization/__init__.py +35 -0
- vllm/model_executor/layers/quantization/aqlm.py +376 -0
- vllm/model_executor/layers/quantization/awq.py +175 -0
- vllm/model_executor/layers/quantization/base_config.py +97 -0
- vllm/model_executor/layers/quantization/fp8.py +265 -0
- vllm/model_executor/layers/quantization/gptq.py +224 -0
- vllm/model_executor/layers/quantization/gptq_marlin.py +438 -0
- vllm/model_executor/layers/quantization/marlin.py +227 -0
- vllm/model_executor/layers/quantization/schema.py +84 -0
- vllm/model_executor/layers/quantization/squeezellm.py +137 -0
- vllm/model_executor/layers/rejection_sampler.py +405 -0
- vllm/model_executor/layers/rotary_embedding.py +525 -0
- vllm/model_executor/layers/sampler.py +1051 -0
- vllm/model_executor/layers/vocab_parallel_embedding.py +155 -0
- vllm/model_executor/model_loader/__init__.py +30 -0
- vllm/model_executor/model_loader/loader.py +362 -0
- vllm/model_executor/model_loader/neuron.py +136 -0
- vllm/model_executor/model_loader/tensorizer.py +368 -0
- vllm/model_executor/model_loader/utils.py +41 -0
- vllm/model_executor/model_loader/weight_utils.py +372 -0
- vllm/model_executor/models/__init__.py +119 -0
- vllm/model_executor/models/baichuan.py +410 -0
- vllm/model_executor/models/bloom.py +327 -0
- vllm/model_executor/models/chatglm.py +386 -0
- vllm/model_executor/models/commandr.py +373 -0
- vllm/model_executor/models/dbrx.py +413 -0
- vllm/model_executor/models/decilm.py +122 -0
- vllm/model_executor/models/deepseek.py +438 -0
- vllm/model_executor/models/falcon.py +444 -0
- vllm/model_executor/models/gemma.py +393 -0
- vllm/model_executor/models/gpt2.py +266 -0
- vllm/model_executor/models/gpt_bigcode.py +274 -0
- vllm/model_executor/models/gpt_j.py +281 -0
- vllm/model_executor/models/gpt_neox.py +295 -0
- vllm/model_executor/models/internlm2.py +323 -0
- vllm/model_executor/models/jais.py +333 -0
- vllm/model_executor/models/llama.py +442 -0
- vllm/model_executor/models/llava.py +239 -0
- vllm/model_executor/models/minicpm.py +531 -0
- vllm/model_executor/models/mixtral.py +583 -0
- vllm/model_executor/models/mixtral_quant.py +404 -0
- vllm/model_executor/models/mpt.py +295 -0
- vllm/model_executor/models/olmo.py +356 -0
- vllm/model_executor/models/opt.py +349 -0
- vllm/model_executor/models/orion.py +319 -0
- vllm/model_executor/models/phi.py +300 -0
- vllm/model_executor/models/qwen.py +284 -0
- vllm/model_executor/models/qwen2.py +367 -0
- vllm/model_executor/models/qwen2_moe.py +447 -0
- vllm/model_executor/models/stablelm.py +301 -0
- vllm/model_executor/models/starcoder2.py +302 -0
- vllm/model_executor/models/xverse.py +366 -0
- vllm/model_executor/sampling_metadata.py +588 -0
- vllm/model_executor/utils.py +35 -0
- vllm/outputs.py +150 -0
- vllm/py.typed +2 -0
- vllm/sampling_params.py +340 -0
- vllm/sequence.py +766 -0
- vllm/spec_decode/__init__.py +0 -0
- vllm/spec_decode/batch_expansion.py +397 -0
- vllm/spec_decode/interfaces.py +73 -0
- vllm/spec_decode/metrics.py +191 -0
- vllm/spec_decode/multi_step_worker.py +203 -0
- vllm/spec_decode/ngram_worker.py +176 -0
- vllm/spec_decode/spec_decode_worker.py +472 -0
- vllm/spec_decode/top1_proposer.py +200 -0
- vllm/spec_decode/util.py +228 -0
- vllm/test_utils.py +41 -0
- vllm/transformers_utils/__init__.py +0 -0
- vllm/transformers_utils/config.py +58 -0
- vllm/transformers_utils/configs/__init__.py +16 -0
- vllm/transformers_utils/configs/chatglm.py +68 -0
- vllm/transformers_utils/configs/dbrx.py +278 -0
- vllm/transformers_utils/configs/falcon.py +87 -0
- vllm/transformers_utils/configs/jais.py +236 -0
- vllm/transformers_utils/configs/mpt.py +178 -0
- vllm/transformers_utils/detokenizer.py +313 -0
- vllm/transformers_utils/tokenizer.py +149 -0
- vllm/transformers_utils/tokenizer_group/__init__.py +33 -0
- vllm/transformers_utils/tokenizer_group/base_tokenizer_group.py +55 -0
- vllm/transformers_utils/tokenizer_group/ray_tokenizer_group.py +169 -0
- vllm/transformers_utils/tokenizer_group/tokenizer_group.py +78 -0
- vllm/transformers_utils/tokenizers/__init__.py +5 -0
- vllm/transformers_utils/tokenizers/baichuan.py +255 -0
- vllm/usage/__init__.py +0 -0
- vllm/usage/usage_lib.py +209 -0
- vllm/utils.py +677 -0
- vllm/worker/__init__.py +0 -0
- vllm/worker/cache_engine.py +105 -0
- vllm/worker/cpu_model_runner.py +346 -0
- vllm/worker/cpu_worker.py +321 -0
- vllm/worker/model_runner.py +1168 -0
- vllm/worker/neuron_model_runner.py +196 -0
- vllm/worker/neuron_worker.py +98 -0
- vllm/worker/worker.py +345 -0
- vllm/worker/worker_base.py +146 -0
- vllm_npu-0.4.2.dist-info/LICENSE +201 -0
- vllm_npu-0.4.2.dist-info/METADATA +173 -0
- vllm_npu-0.4.2.dist-info/RECORD +219 -0
- vllm_npu-0.4.2.dist-info/WHEEL +5 -0
- vllm_npu-0.4.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,262 @@
|
|
1
|
+
# pylint: disable=unused-argument
|
2
|
+
from typing import TYPE_CHECKING, List, Optional
|
3
|
+
|
4
|
+
import torch
|
5
|
+
import torch.nn as nn
|
6
|
+
from transformers import PretrainedConfig
|
7
|
+
|
8
|
+
from vllm.config import LoRAConfig
|
9
|
+
from vllm.distributed.communication_op import (
|
10
|
+
tensor_model_parallel_all_gather, tensor_model_parallel_all_reduce)
|
11
|
+
from vllm.distributed.parallel_state import get_tensor_model_parallel_rank
|
12
|
+
from vllm.lora.layers import (ColumnParallelLinearWithLoRA,
|
13
|
+
MergedColumnParallelLinearWithLoRA,
|
14
|
+
MergedQKVParallelLinearWithLora,
|
15
|
+
RowParallelLinearWithLoRA)
|
16
|
+
from vllm.lora.punica import bgmv, dispatch_bgmv_low_level
|
17
|
+
|
18
|
+
if TYPE_CHECKING:
|
19
|
+
pass
|
20
|
+
|
21
|
+
|
22
|
+
def _fully_sharded_can_replace(can_replace):
|
23
|
+
"""
|
24
|
+
decorator which adds the condition of fully sharded loras
|
25
|
+
intended to wrap can_replace_layer()
|
26
|
+
"""
|
27
|
+
|
28
|
+
def dec(*args, **kwargs):
|
29
|
+
return (can_replace(*args, **kwargs)
|
30
|
+
and kwargs['lora_config'].fully_sharded_loras)
|
31
|
+
|
32
|
+
return dec
|
33
|
+
|
34
|
+
|
35
|
+
# these layers are based on the tensor parallelism strategy given in
|
36
|
+
# Y. Sheng et al., S-LoRA: Serving Thousands of Concurrent LoRA Adapters. 2023,
|
37
|
+
# https://arxiv.org/abs/2311.03285.
|
38
|
+
|
39
|
+
|
40
|
+
class ColumnParallelLinearWithShardedLoRA(ColumnParallelLinearWithLoRA):
|
41
|
+
"""
|
42
|
+
Differs from ColumnParallelLinearWithLoRA by slicing LoRA A also.
|
43
|
+
|
44
|
+
Based on S-LoRA, slicing happens along the rank dim.
|
45
|
+
"""
|
46
|
+
|
47
|
+
def slice_lora_a(self, lora_a: torch.Tensor) -> torch.Tensor:
|
48
|
+
tp_rank = get_tensor_model_parallel_rank()
|
49
|
+
shard_size = self.lora_a_stacked.shape[2]
|
50
|
+
start_idx = tp_rank * shard_size
|
51
|
+
lora_a = lora_a[:, start_idx:start_idx + shard_size]
|
52
|
+
return lora_a
|
53
|
+
|
54
|
+
def apply_weights(self, x: torch.Tensor,
|
55
|
+
bias: Optional[torch.Tensor]) -> torch.Tensor:
|
56
|
+
output = self.base_layer.linear_method.apply_weights(
|
57
|
+
self.base_layer, x, bias)
|
58
|
+
|
59
|
+
x = x.view(-1, x.shape[-1])
|
60
|
+
output, out_orig_shape = output.view(-1,
|
61
|
+
output.shape[-1]), output.shape
|
62
|
+
buffer = torch.zeros((x.shape[0], self.lora_a_stacked.shape[2]),
|
63
|
+
dtype=torch.float32,
|
64
|
+
device=x.device)
|
65
|
+
|
66
|
+
bgmv(buffer, x, self.lora_a_stacked,
|
67
|
+
self.indices[:self.indices_len[0]], 0, 1.0)
|
68
|
+
buffer = tensor_model_parallel_all_gather(buffer)
|
69
|
+
bgmv(output, buffer, self.lora_b_stacked,
|
70
|
+
self.indices[:self.indices_len[0]], 0, 1.0)
|
71
|
+
# now have column partitioned output
|
72
|
+
|
73
|
+
output = output.view(*out_orig_shape)
|
74
|
+
return output
|
75
|
+
|
76
|
+
@classmethod
|
77
|
+
@_fully_sharded_can_replace
|
78
|
+
def can_replace_layer(cls, source_layer: nn.Module,
|
79
|
+
lora_config: LoRAConfig, packed_modules_list: List,
|
80
|
+
model_config: Optional[PretrainedConfig]) -> bool:
|
81
|
+
# specifying kwargs so they can be easily accessed in decorator
|
82
|
+
return super().can_replace_layer(
|
83
|
+
source_layer=source_layer,
|
84
|
+
lora_config=lora_config,
|
85
|
+
packed_modules_list=packed_modules_list,
|
86
|
+
model_config=model_config,
|
87
|
+
decorate=False,
|
88
|
+
)
|
89
|
+
|
90
|
+
|
91
|
+
def _mcp_apply_weights(x, bias, layer):
|
92
|
+
"""
|
93
|
+
MergedColumnParallelLinearWithShardedLoRA and
|
94
|
+
QKVParallelLinearWithShardedLora share the same
|
95
|
+
LoRa weight application method.
|
96
|
+
|
97
|
+
The main difference is the step by shard_size for lora_b which can
|
98
|
+
vary for QKVParallelLinearWithShardedLora but is constant for
|
99
|
+
MergedColumnParallelLinearWithShardedLoRA.
|
100
|
+
"""
|
101
|
+
# expecting 2 for column parallel and 3 for qkv
|
102
|
+
n = len(layer.lora_a_stacked)
|
103
|
+
output = layer.base_layer.linear_method.apply_weights(
|
104
|
+
layer.base_layer, x, bias)
|
105
|
+
|
106
|
+
x = x.view(-1, x.shape[-1])
|
107
|
+
output, out_orig_shape = output.view(-1, output.shape[-1]), output.shape
|
108
|
+
buffers = torch.zeros((n, x.shape[0], layer.lora_a_stacked[0].shape[2]),
|
109
|
+
dtype=torch.float32,
|
110
|
+
device=x.device)
|
111
|
+
for idx in range(n):
|
112
|
+
bgmv(buffers[idx], x, layer.lora_a_stacked[idx],
|
113
|
+
layer.indices[:layer.indices_len[0]], 0, 1.0)
|
114
|
+
|
115
|
+
buffers = tensor_model_parallel_all_gather(buffers)
|
116
|
+
left_offset = 0
|
117
|
+
for idx in range(n):
|
118
|
+
shard_size = layer.lora_b_stacked[idx].shape[2]
|
119
|
+
dispatch_bgmv_low_level(output, buffers[idx],
|
120
|
+
layer.lora_b_stacked[idx],
|
121
|
+
layer.indices[:layer.indices_len[0]], 0, 1.0,
|
122
|
+
left_offset, shard_size)
|
123
|
+
left_offset += shard_size
|
124
|
+
|
125
|
+
output = output.view(*out_orig_shape)
|
126
|
+
# now have column partitioned and packed output
|
127
|
+
return output
|
128
|
+
|
129
|
+
|
130
|
+
class MergedColumnParallelLinearWithShardedLoRA(
|
131
|
+
MergedColumnParallelLinearWithLoRA):
|
132
|
+
"""
|
133
|
+
Differs from MergedColumnParallelLinearWithLoRA by slicing the
|
134
|
+
LoRA A's also.
|
135
|
+
|
136
|
+
Based on S-LoRA, slicing happens along the rank dim.
|
137
|
+
"""
|
138
|
+
|
139
|
+
def slice_lora_a(self, lora_a: List[torch.Tensor]) -> List[torch.Tensor]:
|
140
|
+
output_shard_size = self.lora_a_stacked[0].shape[2]
|
141
|
+
output_start_idx = self.tp_rank * output_shard_size
|
142
|
+
lora_a = [
|
143
|
+
lora_a[i][:, output_start_idx:output_start_idx + output_shard_size]
|
144
|
+
for i in range(2)
|
145
|
+
]
|
146
|
+
return lora_a
|
147
|
+
|
148
|
+
def apply_weights(self, x: torch.Tensor,
|
149
|
+
bias: Optional[torch.Tensor]) -> torch.Tensor:
|
150
|
+
return _mcp_apply_weights(x, bias, self)
|
151
|
+
|
152
|
+
@classmethod
|
153
|
+
@_fully_sharded_can_replace
|
154
|
+
def can_replace_layer(cls, source_layer: nn.Module,
|
155
|
+
lora_config: LoRAConfig, packed_modules_list: List,
|
156
|
+
model_config: Optional[PretrainedConfig]) -> bool:
|
157
|
+
# specifying kwargs so they can be easily accessed in decorator
|
158
|
+
return super().can_replace_layer(
|
159
|
+
source_layer=source_layer,
|
160
|
+
lora_config=lora_config,
|
161
|
+
packed_modules_list=packed_modules_list,
|
162
|
+
model_config=model_config,
|
163
|
+
decorate=False,
|
164
|
+
)
|
165
|
+
|
166
|
+
|
167
|
+
class MergedQKVParallelLinearWithShardedLora(MergedQKVParallelLinearWithLora):
|
168
|
+
"""
|
169
|
+
Differs from QKVParallelLinearWithLora by slicing the
|
170
|
+
LoRA A's also.
|
171
|
+
|
172
|
+
Based on S-LoRA, slicing happens along the rank dim.
|
173
|
+
"""
|
174
|
+
|
175
|
+
def slice_lora_a(self, lora_a: List[torch.Tensor]) -> List[torch.Tensor]:
|
176
|
+
shard_size = [self.lora_a_stacked[i].shape[2] for i in range(3)]
|
177
|
+
start_idx = [self.tp_rank * shard_size[i] for i in range(3)]
|
178
|
+
lora_a = [
|
179
|
+
lora_a[i][:, start_idx[i]:start_idx[i] +
|
180
|
+
shard_size[i]] if lora_a[i] is not None else None
|
181
|
+
for i in range(3)
|
182
|
+
]
|
183
|
+
return lora_a
|
184
|
+
|
185
|
+
def apply_weights(self, x: torch.Tensor,
|
186
|
+
bias: Optional[torch.Tensor]) -> torch.Tensor:
|
187
|
+
return _mcp_apply_weights(x, bias, self)
|
188
|
+
|
189
|
+
@classmethod
|
190
|
+
@_fully_sharded_can_replace
|
191
|
+
def can_replace_layer(cls, source_layer: nn.Module,
|
192
|
+
lora_config: LoRAConfig, packed_modules_list: List,
|
193
|
+
model_config: Optional[PretrainedConfig]) -> bool:
|
194
|
+
# specifying kwargs so they can be easily accessed in decorator
|
195
|
+
return super().can_replace_layer(
|
196
|
+
source_layer=source_layer,
|
197
|
+
lora_config=lora_config,
|
198
|
+
packed_modules_list=packed_modules_list,
|
199
|
+
model_config=model_config,
|
200
|
+
decorate=False,
|
201
|
+
)
|
202
|
+
|
203
|
+
|
204
|
+
class RowParallelLinearWithShardedLoRA(RowParallelLinearWithLoRA):
|
205
|
+
"""
|
206
|
+
Differs from RowParallelLinearWithLoRA by slicing the
|
207
|
+
LoRA B's also.
|
208
|
+
|
209
|
+
Based on S-LoRA, slicing happens along the output dim.
|
210
|
+
This yields a combined partial sum from the row parallel base
|
211
|
+
layer and column partitioned output from the LoRA.
|
212
|
+
"""
|
213
|
+
|
214
|
+
def slice_lora_b(self, lora_b: torch.Tensor) -> torch.Tensor:
|
215
|
+
shard_size = self.lora_b_stacked.shape[2]
|
216
|
+
start_idx = self.tp_rank * shard_size
|
217
|
+
end_idx = (self.tp_rank + 1) * shard_size
|
218
|
+
lora_b = lora_b[:, start_idx:end_idx]
|
219
|
+
return lora_b
|
220
|
+
|
221
|
+
def apply_weights(self, x: torch.Tensor) -> torch.Tensor:
|
222
|
+
output = self.base_layer.linear_method.apply_weights(
|
223
|
+
self.base_layer, x)
|
224
|
+
|
225
|
+
x = x.view(-1, x.shape[-1])
|
226
|
+
output, out_orig_shape = output.view(-1,
|
227
|
+
output.shape[-1]), output.shape
|
228
|
+
buffer = torch.zeros((x.shape[0], self.lora_a_stacked.shape[2]),
|
229
|
+
dtype=torch.float32,
|
230
|
+
device=x.device)
|
231
|
+
bgmv(buffer, x, self.lora_a_stacked,
|
232
|
+
self.indices[:self.indices_len[0]], 0, 1.0)
|
233
|
+
buffer = tensor_model_parallel_all_reduce(buffer)
|
234
|
+
|
235
|
+
# following S-LoRA, allows the fusing of all_gather and all_reduce
|
236
|
+
# by adding the column partitioned lora output to a slice of output
|
237
|
+
# tensor, which is a partial sum due to row parallel. All that
|
238
|
+
# remains is a standard all_reduce. User should be aware though that
|
239
|
+
# the output is not the same as a normal row_parallel, it should be
|
240
|
+
# reduced before being used
|
241
|
+
shard_size = self.lora_b_stacked.shape[2]
|
242
|
+
start_idx = self.tp_rank * shard_size
|
243
|
+
dispatch_bgmv_low_level(output, buffer, self.lora_b_stacked,
|
244
|
+
self.indices[:self.indices_len[0]], 0, 1.0,
|
245
|
+
start_idx, shard_size)
|
246
|
+
|
247
|
+
output = output.view(*out_orig_shape)
|
248
|
+
return output
|
249
|
+
|
250
|
+
@classmethod
|
251
|
+
@_fully_sharded_can_replace
|
252
|
+
def can_replace_layer(cls, source_layer: nn.Module,
|
253
|
+
lora_config: LoRAConfig, packed_modules_list: List,
|
254
|
+
model_config: Optional[PretrainedConfig]) -> bool:
|
255
|
+
# specifying kwargs so they can be easily accessed in decorator
|
256
|
+
return super().can_replace_layer(
|
257
|
+
source_layer=source_layer,
|
258
|
+
lora_config=lora_config,
|
259
|
+
packed_modules_list=packed_modules_list,
|
260
|
+
model_config=model_config,
|
261
|
+
decorate=False,
|
262
|
+
)
|