Commit
·
85f64e2
1
Parent(s):
76705e2
feat: implement stateless adapter switching [wip]
Browse filesSigned-off-by: jupyterjazz <[email protected]>
- custom_lora_module.py +317 -0
- modeling_jina_embeddings_v4.py +10 -10
- qwen2_5_vl.py +0 -0
custom_lora_module.py
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import math
|
| 4 |
+
import warnings
|
| 5 |
+
from typing import Any, Optional, Union
|
| 6 |
+
|
| 7 |
+
import torch
|
| 8 |
+
import torch.nn as nn
|
| 9 |
+
import torch.nn.functional as F
|
| 10 |
+
from accelerate.utils.imports import is_xpu_available
|
| 11 |
+
from torch import svd_lowrank
|
| 12 |
+
from transformers.pytorch_utils import Conv1D
|
| 13 |
+
|
| 14 |
+
from peft.tuners.tuners_utils import BaseTunerLayer, check_adapters_to_merge
|
| 15 |
+
from peft.utils.integrations import (
|
| 16 |
+
dequantize_module_weight,
|
| 17 |
+
gather_params_ctx,
|
| 18 |
+
get_bnb_param_type,
|
| 19 |
+
skip_init_on_device,
|
| 20 |
+
)
|
| 21 |
+
from peft.utils.other import transpose
|
| 22 |
+
from peft.tuners.lora import LoraLayer
|
| 23 |
+
|
| 24 |
+
class Linear(nn.Module, LoraLayer):
|
| 25 |
+
# Lora implemented in a dense layer
|
| 26 |
+
def __init__(
|
| 27 |
+
self,
|
| 28 |
+
base_layer,
|
| 29 |
+
adapter_name: str,
|
| 30 |
+
r: int = 0,
|
| 31 |
+
lora_alpha: int = 1,
|
| 32 |
+
lora_dropout: float = 0.0,
|
| 33 |
+
fan_in_fan_out: bool = False, # Set this to True if the layer to replace stores weight like (fan_in, fan_out)
|
| 34 |
+
is_target_conv_1d_layer: bool = False,
|
| 35 |
+
init_lora_weights: Union[bool, str] = True,
|
| 36 |
+
use_rslora: bool = False,
|
| 37 |
+
use_dora: bool = False,
|
| 38 |
+
lora_bias: bool = False,
|
| 39 |
+
**kwargs,
|
| 40 |
+
) -> None:
|
| 41 |
+
super().__init__()
|
| 42 |
+
LoraLayer.__init__(self, base_layer, **kwargs)
|
| 43 |
+
self.fan_in_fan_out = fan_in_fan_out
|
| 44 |
+
|
| 45 |
+
self._active_adapter = adapter_name
|
| 46 |
+
self.update_layer(
|
| 47 |
+
adapter_name,
|
| 48 |
+
r,
|
| 49 |
+
lora_alpha=lora_alpha,
|
| 50 |
+
lora_dropout=lora_dropout,
|
| 51 |
+
init_lora_weights=init_lora_weights,
|
| 52 |
+
use_rslora=use_rslora,
|
| 53 |
+
use_dora=use_dora,
|
| 54 |
+
lora_bias=lora_bias,
|
| 55 |
+
)
|
| 56 |
+
self.is_target_conv_1d_layer = is_target_conv_1d_layer
|
| 57 |
+
|
| 58 |
+
def merge(self, safe_merge: bool = False, adapter_names: Optional[list[str]] = None) -> None:
|
| 59 |
+
"""
|
| 60 |
+
Merge the active adapter weights into the base weights
|
| 61 |
+
|
| 62 |
+
Args:
|
| 63 |
+
safe_merge (`bool`, *optional*):
|
| 64 |
+
If True, the merge operation will be performed in a copy of the original weights and check for NaNs
|
| 65 |
+
before merging the weights. This is useful if you want to check if the merge operation will produce
|
| 66 |
+
NaNs. Defaults to `False`.
|
| 67 |
+
adapter_names (`list[str]`, *optional*):
|
| 68 |
+
The list of adapter names that should be merged. If None, all active adapters will be merged. Defaults
|
| 69 |
+
to `None`.
|
| 70 |
+
"""
|
| 71 |
+
adapter_names = check_adapters_to_merge(self, adapter_names)
|
| 72 |
+
if not adapter_names:
|
| 73 |
+
# no adapter to merge
|
| 74 |
+
return
|
| 75 |
+
|
| 76 |
+
for active_adapter in adapter_names:
|
| 77 |
+
if active_adapter in self.lora_A.keys():
|
| 78 |
+
base_layer = self.get_base_layer()
|
| 79 |
+
if safe_merge:
|
| 80 |
+
# Note that safe_merge will be slower than the normal merge
|
| 81 |
+
# because of the copy operation.
|
| 82 |
+
orig_weights = base_layer.weight.data.clone()
|
| 83 |
+
delta_weight = self.get_delta_weight(active_adapter)
|
| 84 |
+
if not self.use_dora[active_adapter]:
|
| 85 |
+
orig_weights += delta_weight
|
| 86 |
+
else:
|
| 87 |
+
# handle dora
|
| 88 |
+
# since delta_weight already includes scaling, set it to 1 here
|
| 89 |
+
weight_norm = (
|
| 90 |
+
self.lora_magnitude_vector[active_adapter]
|
| 91 |
+
.get_weight_norm(orig_weights, transpose(delta_weight, self.fan_in_fan_out), scaling=1)
|
| 92 |
+
.detach()
|
| 93 |
+
)
|
| 94 |
+
# We need to cache weight_norm because it has to be based on the original weights. We
|
| 95 |
+
# cannot calculate it on the fly based on the merged weights when unmerging because its a
|
| 96 |
+
# different value
|
| 97 |
+
self._cache_store(f"{active_adapter}-weight_norm", weight_norm)
|
| 98 |
+
dora_factor = self.lora_magnitude_vector[active_adapter].weight / weight_norm
|
| 99 |
+
dora_factor = transpose(dora_factor.view(-1, 1), self.fan_in_fan_out)
|
| 100 |
+
orig_weights = dora_factor * (orig_weights + delta_weight)
|
| 101 |
+
|
| 102 |
+
if not torch.isfinite(orig_weights).all():
|
| 103 |
+
raise ValueError(
|
| 104 |
+
f"NaNs detected in the merged weights. The adapter {active_adapter} seems to be broken"
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
base_layer.weight.data = orig_weights
|
| 108 |
+
|
| 109 |
+
if self.lora_bias[active_adapter]:
|
| 110 |
+
new_bias = base_layer.bias + self.lora_B[active_adapter].bias
|
| 111 |
+
if not torch.isfinite(new_bias).all():
|
| 112 |
+
raise ValueError(
|
| 113 |
+
f"NaNs detected in the merged weights. The adapter {active_adapter} seems to be broken"
|
| 114 |
+
)
|
| 115 |
+
base_layer.bias.data = new_bias
|
| 116 |
+
|
| 117 |
+
else:
|
| 118 |
+
delta_weight = self.get_delta_weight(active_adapter)
|
| 119 |
+
if not self.use_dora[active_adapter]:
|
| 120 |
+
base_layer.weight.data += delta_weight
|
| 121 |
+
else:
|
| 122 |
+
# handle dora
|
| 123 |
+
# since delta_weight already includes scaling, set it to 1 here
|
| 124 |
+
weight_norm = (
|
| 125 |
+
self.lora_magnitude_vector[active_adapter]
|
| 126 |
+
.get_weight_norm(
|
| 127 |
+
base_layer.weight, transpose(delta_weight, self.fan_in_fan_out), scaling=1
|
| 128 |
+
)
|
| 129 |
+
.detach()
|
| 130 |
+
)
|
| 131 |
+
# We need to cache weight_norm because it has to be based on the original weights. We
|
| 132 |
+
# cannot calculate it on the fly based on the merged weights when unmerging because its a
|
| 133 |
+
# different value
|
| 134 |
+
self._cache_store(f"{active_adapter}-weight_norm", weight_norm)
|
| 135 |
+
dora_factor = self.lora_magnitude_vector[active_adapter].weight / weight_norm
|
| 136 |
+
dora_factor = transpose(dora_factor.view(-1, 1), self.fan_in_fan_out)
|
| 137 |
+
new_weight = dora_factor * (base_layer.weight.data + delta_weight)
|
| 138 |
+
base_layer.weight.data = new_weight
|
| 139 |
+
|
| 140 |
+
if self.lora_bias[active_adapter]:
|
| 141 |
+
base_layer.bias.data += self.lora_B[active_adapter].bias
|
| 142 |
+
|
| 143 |
+
self.merged_adapters.append(active_adapter)
|
| 144 |
+
|
| 145 |
+
def unmerge(self) -> None:
|
| 146 |
+
"""
|
| 147 |
+
This method unmerges all merged adapter layers from the base weights.
|
| 148 |
+
"""
|
| 149 |
+
if not self.merged:
|
| 150 |
+
warnings.warn("Already unmerged. Nothing to do.")
|
| 151 |
+
return
|
| 152 |
+
while len(self.merged_adapters) > 0:
|
| 153 |
+
active_adapter = self.merged_adapters.pop()
|
| 154 |
+
if active_adapter in self.lora_A.keys():
|
| 155 |
+
weight = self.get_base_layer().weight
|
| 156 |
+
delta_weight = self.get_delta_weight(active_adapter)
|
| 157 |
+
if not self.use_dora[active_adapter]:
|
| 158 |
+
weight.data -= delta_weight
|
| 159 |
+
else:
|
| 160 |
+
weight_norm = self._cache_pop(f"{active_adapter}-weight_norm")
|
| 161 |
+
dora_factor = self.lora_magnitude_vector[active_adapter].weight / weight_norm
|
| 162 |
+
weight_orig = weight.data / dora_factor.view(-1, 1) - delta_weight
|
| 163 |
+
weight.data = weight_orig
|
| 164 |
+
|
| 165 |
+
if self.lora_bias[active_adapter]:
|
| 166 |
+
self.get_base_layer().bias.data -= self.lora_B[active_adapter].bias
|
| 167 |
+
|
| 168 |
+
def get_delta_weight(self, adapter) -> torch.Tensor:
|
| 169 |
+
"""
|
| 170 |
+
Compute the delta weight for the given adapter.
|
| 171 |
+
|
| 172 |
+
Args:
|
| 173 |
+
adapter (str):
|
| 174 |
+
The name of the adapter for which the delta weight should be computed.
|
| 175 |
+
"""
|
| 176 |
+
device = self.lora_B[adapter].weight.device
|
| 177 |
+
dtype = self.lora_B[adapter].weight.dtype
|
| 178 |
+
|
| 179 |
+
# In case users wants to merge the adapter weights that are in
|
| 180 |
+
# (b)float16 while being on CPU, we need to cast the weights to float32, perform the merge and then cast back to
|
| 181 |
+
# (b)float16 because some CPUs have slow bf16/fp16 matmuls.
|
| 182 |
+
cast_to_fp32 = device.type == "cpu" and (dtype == torch.float16 or dtype == torch.bfloat16)
|
| 183 |
+
|
| 184 |
+
weight_A = self.lora_A[adapter].weight
|
| 185 |
+
weight_B = self.lora_B[adapter].weight
|
| 186 |
+
|
| 187 |
+
if cast_to_fp32:
|
| 188 |
+
weight_A = weight_A.float()
|
| 189 |
+
weight_B = weight_B.float()
|
| 190 |
+
|
| 191 |
+
output_tensor = transpose(weight_B @ weight_A, self.fan_in_fan_out) * self.scaling[adapter]
|
| 192 |
+
|
| 193 |
+
if cast_to_fp32:
|
| 194 |
+
output_tensor = output_tensor.to(dtype=dtype)
|
| 195 |
+
|
| 196 |
+
# cast back the weights
|
| 197 |
+
self.lora_A[adapter].weight.data = weight_A.to(dtype)
|
| 198 |
+
self.lora_B[adapter].weight.data = weight_B.to(dtype)
|
| 199 |
+
|
| 200 |
+
return output_tensor
|
| 201 |
+
|
| 202 |
+
def forward(self, x: torch.Tensor, *args: Any, **kwargs: Any) -> torch.Tensor:
|
| 203 |
+
self._check_forward_args(x, *args, **kwargs)
|
| 204 |
+
adapter_names = kwargs.pop("adapter_names", None)
|
| 205 |
+
|
| 206 |
+
if self.disable_adapters:
|
| 207 |
+
if self.merged:
|
| 208 |
+
self.unmerge()
|
| 209 |
+
result = self.base_layer(x, *args, **kwargs)
|
| 210 |
+
elif adapter_names is not None:
|
| 211 |
+
result = self._mixed_batch_forward(x, *args, adapter_names=adapter_names, **kwargs)
|
| 212 |
+
elif self.merged:
|
| 213 |
+
result = self.base_layer(x, *args, **kwargs)
|
| 214 |
+
else:
|
| 215 |
+
result = self.base_layer(x, *args, **kwargs)
|
| 216 |
+
torch_result_dtype = result.dtype
|
| 217 |
+
|
| 218 |
+
lora_A_keys = self.lora_A.keys()
|
| 219 |
+
for active_adapter in self.active_adapters:
|
| 220 |
+
if active_adapter not in lora_A_keys:
|
| 221 |
+
continue
|
| 222 |
+
|
| 223 |
+
lora_A = self.lora_A[active_adapter]['default']
|
| 224 |
+
lora_B = self.lora_B[active_adapter]['default']
|
| 225 |
+
dropout = self.lora_dropout[active_adapter]
|
| 226 |
+
scaling = self.scaling[active_adapter]
|
| 227 |
+
x = self._cast_input_dtype(x, lora_A.weight.dtype)
|
| 228 |
+
|
| 229 |
+
if not self.use_dora[active_adapter]:
|
| 230 |
+
result = result + lora_B(lora_A(dropout(x))) * scaling
|
| 231 |
+
else:
|
| 232 |
+
if isinstance(dropout, nn.Identity) or not self.training:
|
| 233 |
+
base_result = result
|
| 234 |
+
else:
|
| 235 |
+
x = dropout(x)
|
| 236 |
+
base_result = None
|
| 237 |
+
|
| 238 |
+
result = result + self.lora_magnitude_vector[active_adapter](
|
| 239 |
+
x,
|
| 240 |
+
lora_A=lora_A,
|
| 241 |
+
lora_B=lora_B,
|
| 242 |
+
scaling=scaling,
|
| 243 |
+
base_layer=self.get_base_layer(),
|
| 244 |
+
base_result=base_result,
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
+
result = result.to(torch_result_dtype)
|
| 248 |
+
|
| 249 |
+
return result
|
| 250 |
+
|
| 251 |
+
def __repr__(self) -> str:
|
| 252 |
+
rep = super().__repr__()
|
| 253 |
+
return "lora." + rep
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
def update_layer(
|
| 257 |
+
self,
|
| 258 |
+
adapter_name,
|
| 259 |
+
r,
|
| 260 |
+
lora_alpha,
|
| 261 |
+
lora_dropout,
|
| 262 |
+
init_lora_weights,
|
| 263 |
+
use_rslora,
|
| 264 |
+
use_dora: bool = False,
|
| 265 |
+
lora_bias: bool = False,
|
| 266 |
+
):
|
| 267 |
+
# This code works for linear layers, override for other layer types
|
| 268 |
+
if r <= 0:
|
| 269 |
+
raise ValueError(f"`r` should be a positive integer value but the value passed is {r}")
|
| 270 |
+
|
| 271 |
+
self.r[adapter_name] = r
|
| 272 |
+
self.lora_alpha[adapter_name] = lora_alpha
|
| 273 |
+
if lora_dropout > 0.0:
|
| 274 |
+
lora_dropout_layer = nn.Dropout(p=lora_dropout)
|
| 275 |
+
else:
|
| 276 |
+
lora_dropout_layer = nn.Identity()
|
| 277 |
+
|
| 278 |
+
self.lora_dropout.update(nn.ModuleDict({adapter_name: lora_dropout_layer}))
|
| 279 |
+
# Actual trainable parameters
|
| 280 |
+
self.lora_A[adapter_name] = nn.ModuleDict({
|
| 281 |
+
"default": nn.Linear(self.in_features, r, bias=False),
|
| 282 |
+
"second_adapter": nn.Linear(self.in_features, r, bias=False)
|
| 283 |
+
})
|
| 284 |
+
self.lora_B[adapter_name] = nn.ModuleDict({
|
| 285 |
+
"default": nn.Linear(r, self.out_features, bias=lora_bias),
|
| 286 |
+
"second_adapter": nn.Linear(r, self.out_features, bias=lora_bias)
|
| 287 |
+
})
|
| 288 |
+
self.lora_bias[adapter_name] = lora_bias
|
| 289 |
+
|
| 290 |
+
if use_rslora:
|
| 291 |
+
self.scaling[adapter_name] = lora_alpha / math.sqrt(r)
|
| 292 |
+
else:
|
| 293 |
+
self.scaling[adapter_name] = lora_alpha / r
|
| 294 |
+
|
| 295 |
+
self.reset_lora_parameters(adapter_name, init_lora_weights)
|
| 296 |
+
self._move_adapter_to_device_of_base_layer(adapter_name)
|
| 297 |
+
self.use_dora[adapter_name] = False
|
| 298 |
+
self.set_adapter(self.active_adapters)
|
| 299 |
+
|
| 300 |
+
def reset_lora_parameters(self, adapter_name, init_lora_weights):
|
| 301 |
+
if init_lora_weights is False:
|
| 302 |
+
return
|
| 303 |
+
if init_lora_weights is True:
|
| 304 |
+
# initialize A the same way as the default for nn.Linear and B to zero
|
| 305 |
+
# https://github.com/microsoft/LoRA/blob/a0a92e0f26c067cf94747bdbf1ce73793fa44d19/loralib/layers.py#L124
|
| 306 |
+
nn.init.kaiming_uniform_(self.lora_A[adapter_name]['default'].weight, a=math.sqrt(5))
|
| 307 |
+
nn.init.kaiming_uniform_(self.lora_A[adapter_name]['second_adapter'].weight, a=math.sqrt(5))
|
| 308 |
+
elif init_lora_weights.lower() == "gaussian":
|
| 309 |
+
nn.init.normal_(self.lora_A[adapter_name]['default'].weight, std=1 / self.r[adapter_name])
|
| 310 |
+
nn.init.normal_(self.lora_A[adapter_name]['second_adapter'].weight, std=1 / self.r[adapter_name])
|
| 311 |
+
else:
|
| 312 |
+
raise ValueError(f"Unknown initialization {init_lora_weights=}")
|
| 313 |
+
nn.init.zeros_(self.lora_B[adapter_name]['default'].weight)
|
| 314 |
+
nn.init.zeros_(self.lora_B[adapter_name]['second_adapter'].weight)
|
| 315 |
+
if self.lora_bias[adapter_name]:
|
| 316 |
+
nn.init.zeros_(self.lora_B[adapter_name]['default'].bias)
|
| 317 |
+
nn.init.zeros_(self.lora_B[adapter_name]['second_adapter'].bias)
|
modeling_jina_embeddings_v4.py
CHANGED
|
@@ -10,18 +10,17 @@ from typing import Any, Callable, ClassVar, Dict, List, Optional, Union, cast
|
|
| 10 |
import numpy as np
|
| 11 |
import torch
|
| 12 |
from huggingface_hub import snapshot_download
|
| 13 |
-
from peft import PeftModel
|
| 14 |
from peft.utils.hotswap import hotswap_adapter
|
| 15 |
from PIL import Image
|
| 16 |
from torch import nn
|
| 17 |
from torch.utils.data import DataLoader
|
| 18 |
from tqdm import tqdm
|
| 19 |
from transformers import BatchFeature
|
| 20 |
-
from
|
| 21 |
-
Qwen2_5_VLProcessor)
|
| 22 |
-
|
| 23 |
from .configuration_jina_embeddings_v4 import JinaEmbeddingsV4Config
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
class PromptType(str, Enum):
|
| 27 |
query = "query"
|
|
@@ -32,6 +31,7 @@ class TaskType(str, Enum):
|
|
| 32 |
retrieval = "retrieval"
|
| 33 |
code = "code"
|
| 34 |
text_matching = "text-matching"
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
PREFIX_DICT = {"query": "Query", "passage": "Passage"}
|
|
@@ -173,7 +173,6 @@ class JinaEmbeddingsV4Model(Qwen2_5_VLForConditionalGeneration):
|
|
| 173 |
)
|
| 174 |
|
| 175 |
kwargs["output_hidden_states"] = True
|
| 176 |
-
|
| 177 |
outputs = super().forward(
|
| 178 |
input_ids,
|
| 179 |
attention_mask,
|
|
@@ -270,7 +269,6 @@ class JinaEmbeddingsV4Model(Qwen2_5_VLForConditionalGeneration):
|
|
| 270 |
hidden_states = self.get_last_hidden_states(
|
| 271 |
input_ids=input_ids, attention_mask=attention_mask, **kwargs
|
| 272 |
) # (batch_size, seq_length, hidden_size)
|
| 273 |
-
|
| 274 |
# Compute the embeddings
|
| 275 |
single_vec_emb = self.project_to_single_vector_embeddings(
|
| 276 |
hidden_states, attention_mask, input_ids=input_ids
|
|
@@ -465,7 +463,7 @@ class JinaEmbeddingsV4Model(Qwen2_5_VLForConditionalGeneration):
|
|
| 465 |
if "torch_dtype" not in kwargs:
|
| 466 |
kwargs["torch_dtype"] = "auto"
|
| 467 |
|
| 468 |
-
task_value = kwargs.pop("task", "
|
| 469 |
try:
|
| 470 |
task = TaskType(task_value)
|
| 471 |
except ValueError:
|
|
@@ -490,11 +488,13 @@ class JinaEmbeddingsV4Model(Qwen2_5_VLForConditionalGeneration):
|
|
| 490 |
base_model.adapter_dir = adapter_dir
|
| 491 |
base_model.task = task
|
| 492 |
|
|
|
|
|
|
|
| 493 |
# Create the PEFT model with the requested task adapter
|
| 494 |
peft_model = PeftModel.from_pretrained(
|
| 495 |
-
base_model, os.path.join(adapter_dir, task.value)
|
| 496 |
)
|
| 497 |
-
|
| 498 |
# Add set_task method to the PEFT model instance
|
| 499 |
def set_task_method(self, task: Union[str, TaskType]):
|
| 500 |
"""
|
|
|
|
| 10 |
import numpy as np
|
| 11 |
import torch
|
| 12 |
from huggingface_hub import snapshot_download
|
| 13 |
+
from peft import PeftModel, LoraConfig
|
| 14 |
from peft.utils.hotswap import hotswap_adapter
|
| 15 |
from PIL import Image
|
| 16 |
from torch import nn
|
| 17 |
from torch.utils.data import DataLoader
|
| 18 |
from tqdm import tqdm
|
| 19 |
from transformers import BatchFeature
|
| 20 |
+
from .qwen2_5_vl import Qwen2_5_VLForConditionalGeneration, Qwen2_5_VLProcessor
|
|
|
|
|
|
|
| 21 |
from .configuration_jina_embeddings_v4 import JinaEmbeddingsV4Config
|
| 22 |
+
import peft
|
| 23 |
+
from .custom_lora_module import Linear
|
| 24 |
|
| 25 |
class PromptType(str, Enum):
|
| 26 |
query = "query"
|
|
|
|
| 31 |
retrieval = "retrieval"
|
| 32 |
code = "code"
|
| 33 |
text_matching = "text-matching"
|
| 34 |
+
test = "test"
|
| 35 |
|
| 36 |
|
| 37 |
PREFIX_DICT = {"query": "Query", "passage": "Passage"}
|
|
|
|
| 173 |
)
|
| 174 |
|
| 175 |
kwargs["output_hidden_states"] = True
|
|
|
|
| 176 |
outputs = super().forward(
|
| 177 |
input_ids,
|
| 178 |
attention_mask,
|
|
|
|
| 269 |
hidden_states = self.get_last_hidden_states(
|
| 270 |
input_ids=input_ids, attention_mask=attention_mask, **kwargs
|
| 271 |
) # (batch_size, seq_length, hidden_size)
|
|
|
|
| 272 |
# Compute the embeddings
|
| 273 |
single_vec_emb = self.project_to_single_vector_embeddings(
|
| 274 |
hidden_states, attention_mask, input_ids=input_ids
|
|
|
|
| 463 |
if "torch_dtype" not in kwargs:
|
| 464 |
kwargs["torch_dtype"] = "auto"
|
| 465 |
|
| 466 |
+
task_value = kwargs.pop("task", "test")
|
| 467 |
try:
|
| 468 |
task = TaskType(task_value)
|
| 469 |
except ValueError:
|
|
|
|
| 488 |
base_model.adapter_dir = adapter_dir
|
| 489 |
base_model.task = task
|
| 490 |
|
| 491 |
+
lora_config = LoraConfig.from_pretrained(os.path.join(adapter_dir, task.value))
|
| 492 |
+
lora_config._custom_modules = {torch.nn.modules.linear.Linear: Linear}
|
| 493 |
# Create the PEFT model with the requested task adapter
|
| 494 |
peft_model = PeftModel.from_pretrained(
|
| 495 |
+
model=base_model, model_id=os.path.join(adapter_dir, task.value), config=lora_config
|
| 496 |
)
|
| 497 |
+
|
| 498 |
# Add set_task method to the PEFT model instance
|
| 499 |
def set_task_method(self, task: Union[str, TaskType]):
|
| 500 |
"""
|
qwen2_5_vl.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|