File size: 14,787 Bytes
f9b3adb
 
 
4b000ec
f9b3adb
4b000ec
f9b3adb
 
 
 
e860caa
f9b3adb
 
57dbe22
2646361
 
4b000ec
 
f9b3adb
4b000ec
f9b3adb
 
4b000ec
f9b3adb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b000ec
f9b3adb
 
 
 
 
4b000ec
f9b3adb
4b000ec
 
f9b3adb
 
 
 
 
 
 
 
 
4b000ec
f9b3adb
4b000ec
f9b3adb
4b000ec
f9b3adb
 
4b000ec
f9b3adb
 
 
 
 
4b000ec
 
 
 
f9b3adb
 
 
 
 
 
 
 
 
 
e860caa
f9b3adb
 
 
 
 
e860caa
 
f9b3adb
 
 
 
 
 
 
e860caa
f9b3adb
 
 
 
 
4b000ec
 
 
 
f9b3adb
 
 
 
 
 
4b000ec
f9b3adb
 
4b000ec
 
f9b3adb
 
 
 
4b000ec
 
 
 
 
 
f9b3adb
 
 
 
 
 
4b000ec
f9b3adb
 
4b000ec
 
f9b3adb
 
 
 
4b000ec
 
 
 
 
 
f9b3adb
2646361
 
 
 
 
 
 
 
 
f9b3adb
 
 
 
 
 
4b000ec
f9b3adb
4b000ec
 
f9b3adb
 
e860caa
95fd08c
 
2646361
 
 
e860caa
 
 
 
 
 
 
 
 
 
 
f9b3adb
 
 
 
 
 
4b000ec
f9b3adb
4b000ec
 
f9b3adb
 
 
95fd08c
 
2646361
 
 
e860caa
 
 
 
2646361
 
 
 
 
 
 
 
e860caa
 
 
 
f9b3adb
 
7c4a80c
2646361
 
 
a709b51
4b000ec
2646361
4b000ec
f9b3adb
7c4a80c
 
 
 
 
4b000ec
 
 
 
 
 
2646361
4b000ec
a709b51
6a92924
a709b51
 
 
 
 
6a92924
 
a709b51
 
2646361
4b000ec
 
 
 
 
 
 
27d23b2
4b000ec
 
 
 
 
e860caa
4434bf3
 
 
 
 
 
 
 
f9b3adb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a709b51
f9b3adb
dc4080e
f9b3adb
a709b51
2646361
57dbe22
2646361
7c4a80c
f9b3adb
4b000ec
f9b3adb
 
 
4b000ec
f9b3adb
4b000ec
 
f9b3adb
 
 
e860caa
7c4a80c
f9b3adb
 
 
 
 
 
 
 
 
 
 
 
 
4b000ec
 
 
 
95fd08c
4b000ec
3afddee
4b000ec
 
 
2646361
 
 
3afddee
 
e860caa
 
4b000ec
3afddee
e860caa
3afddee
e860caa
3afddee
e860caa
95fd08c
3afddee
 
95fd08c
2646361
 
 
a709b51
3afddee
a709b51
3afddee
2646361
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import math
import os
from functools import partial
from typing import Iterator, List, Optional, Tuple, Union

import numpy as np
import torch
import torch.nn.utils.parametrize as parametrize
from torch import nn
from torch.nn import Parameter
from torch.nn import functional as F
from transformers import PretrainedConfig

from .rotary import RotaryEmbedding
from .modeling_xlm_roberta import (XLMRobertaFlashConfig, XLMRobertaModel,
                                   XLMRobertaPreTrainedModel)


def initialized_weights(
    shape: Tuple[int], num_adaptations: int, init: str = "kaiming"
) -> torch.Tensor:
    weight_data = []
    for _ in range(num_adaptations):
        new_adaption = torch.zeros(shape)
        if init == "kaiming":
            nn.init.kaiming_uniform_(new_adaption, a=math.sqrt(5))
        elif init == "normal":
            nn.init.normal_(new_adaption)
        else:
            raise NotImplementedError
        weight_data.append(new_adaption)
    return torch.stack(weight_data, dim=0)


class LoRAParametrization(nn.Module):
    """
    This LoRA implementation was inspired by  https://github.com/cccntu/minLoRA
    The MIT License (MIT) Copyright (c) 2020 Andrej Karpathy
    Permission is hereby granted, free of charge, to any person obtaining a copy of this software
    and associated documentation files (the "Software"), to deal in the Software without restriction,
    including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
    subject to the following conditions:
    The above copyright notice and this permission notice shall be included in all copies or substantial
    portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
    LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    """

    def __init__(
        self,
        fan_in: int,
        fan_out: int,
        layer_type: str = "linear",
        num_adaptations: int = 1,
        rank: int = 4,
        dropout_p: float = 0.0,
        alpha: float = 1,
    ):
        super().__init__()
        # if weight is stored as (fan_out, fan_in), the memory layout of A & B follows (W + BA)x
        # otherwise, it's x(W + AB). This allows us to tie the weights between linear layers and embeddings
        fan_in_fan_out = layer_type == "embedding"
        self.swap = (lambda x: (x[1], x[0])) if fan_in_fan_out else (lambda x: x)

        if layer_type == "linear":
            self.lora_A = nn.Parameter(
                initialized_weights((rank, fan_in), num_adaptations, init="kaiming")
            )
            self.lora_B = nn.Parameter(torch.zeros((num_adaptations, fan_out, rank)))
        elif layer_type == "embedding":
            self.lora_A = nn.Parameter(torch.zeros((num_adaptations, fan_in, rank)))
            self.lora_B = nn.Parameter(
                initialized_weights(
                    (rank, fan_out), num_adaptations=num_adaptations, init="normal"
                )
            )
        else:
            raise NotImplementedError

        self.lora_alpha, self.rank = alpha, rank
        self.scaling = alpha / rank
        self.lora_dropout = nn.Dropout(p=dropout_p) if dropout_p > 0 else lambda x: x
        self.dropout_fn = self._dropout if dropout_p > 0 else lambda x: x
        self.register_buffer(
            "lora_dropout_mask",
            torch.ones(self.swap((1, fan_in)), dtype=self.lora_A.dtype),
            persistent=False,
        )

    def _dropout(self, A):
        # to mimic the original implementation: A @ dropout(x), we do (A * dropout(ones)) @ x
        return A * self.lora_dropout(self.lora_dropout_mask)

    def lora_forward(self, X, current_task):
        return (
            X
            + torch.matmul(
                *self.swap(
                    (
                        self.lora_B[current_task],
                        self.dropout_fn(self.lora_A[current_task]),
                    )
                )
            ).view(X.shape)
            * self.scaling
        )

    def forward(self, X):
        return X

    @classmethod
    def from_linear(
        cls,
        layer: nn.Module,
        num_adaptations: int,
        rank: int,
        dropout_p: float,
        alpha: float,
    ):
        assert isinstance(layer, nn.Linear)
        fan_out, fan_in = layer.weight.shape
        return cls(
            fan_in,
            fan_out,
            num_adaptations=num_adaptations,
            layer_type="linear",
            rank=rank,
            dropout_p=dropout_p,
            alpha=alpha,
        )

    @classmethod
    def from_embedding(
        cls,
        layer: nn.Module,
        num_adaptations: int,
        rank: int,
        dropout_p: float,
        alpha: float,
    ):
        assert isinstance(layer, nn.Embedding)
        fan_in, fan_out = layer.weight.shape
        return cls(
            fan_in,
            fan_out,
            num_adaptations=num_adaptations,
            layer_type="embedding",
            rank=rank,
            dropout_p=dropout_p,
            alpha=alpha,
        )

    @classmethod
    def add_to_layer(
        cls,
        layer: nn.Module,
        num_adaptations: int,
        rank: int,
        dropout_p: float,
        alpha: float,
    ):
        """
        Registering LoRA adapters to all embedding and linear layers.
        Additionally, we implement a custom forward function for LoRA parametrization.
        This function modifies the layer's forward pass to optionally use task-specific
        parameters. When a `task_id` is provided, it employs a LoRA parametrization
        to modify the original weights according to the specific task. This allows
        the layer to adapt dynamically to different tasks at runtime. If no `task_id`
        is specified, the layer uses its original weights.
        """
        if isinstance(layer, nn.Linear):
            parametrize.register_parametrization(
                layer,
                "weight",
                cls.from_linear(
                    layer,
                    num_adaptations=num_adaptations,
                    rank=rank,
                    dropout_p=dropout_p,
                    alpha=alpha,
                ),
            )

            def new_forward(self, input, task_id=None, residual=False):
                if task_id is not None:
                    weights = self.parametrizations.weight[0].lora_forward(
                        self.weight, current_task=task_id
                    )
                else:
                    weights = self.weight

                out = F.linear(input, weights, self.bias)

                if residual:
                    return out, input
                return out

            layer.forward = new_forward.__get__(layer, layer.__class__)

        elif isinstance(layer, nn.Embedding):
            parametrize.register_parametrization(
                layer,
                "weight",
                cls.from_embedding(
                    layer,
                    num_adaptations=num_adaptations,
                    rank=rank,
                    dropout_p=dropout_p,
                    alpha=alpha,
                ),
            )

            def new_forward(self, input, task_id=None):
                if task_id is not None:
                    weights = self.parametrizations.weight[0].lora_forward(
                        self.weight, current_task=task_id
                    )
                else:
                    weights = self.weight

                out = F.embedding(
                    input,
                    weights,
                    self.padding_idx,
                    self.max_norm,
                    self.norm_type,
                    self.scale_grad_by_freq,
                    self.sparse,
                )

                return out

            layer.forward = new_forward.__get__(layer, layer.__class__)


class XLMRobertaLoRA(XLMRobertaPreTrainedModel):
    """
    A wrapper class around the Jina XLM-RoBERTa model that integrates LoRA (Low-Rank Adaptation) adapters.
    """

    def __init__(
        self, config: XLMRobertaFlashConfig, roberta: Optional[XLMRobertaModel] = None
    ):
        super().__init__(config)
        if roberta is None:
            self.roberta = XLMRobertaModel(config)
        else:
            self.roberta = roberta

        self._lora_adaptations = config.lora_adaptations
        if (
            not isinstance(self._lora_adaptations, list)
            or len(self._lora_adaptations) < 1
        ):
            raise ValueError(
                f"`lora_adaptations` must be a list and contain at least one element"
            )
        self._task_instructions = config.task_instructions
        if (
            not isinstance(self._task_instructions, dict)
            or len(self._task_instructions) != len(self._lora_adaptations)
            or not all(
                [v in self._lora_adaptations for v in self._task_instructions.keys()]
            )
        ):
            raise ValueError(
                f"`task_instructions` must be a dict and contain the same number of elements "
                f"as `lora_adaptations` with all keys in `task_instructions` present in `lora_adaptations`."
            )
        self._adaptation_map = {
            name: idx for idx, name in enumerate(self._lora_adaptations)
        }
        self._rank = config.lora_rank
        self._dropout_p = config.lora_dropout_p
        self._alpha = config.lora_alpha
        self._register_lora(
            num_adaptations=len(self._lora_adaptations),
            rank=self._rank,
            dropout_p=self._dropout_p,
            alpha=self._alpha,
        )
        self.main_params_trainable = config.lora_main_params_trainable

    @property
    def rotary_emb_base(self):
        return self.roberta.rotary_emb_base

    @rotary_emb_base.setter
    def rotary_emb_base(self, base):
        self.roberta.rotary_emb_base = base

    @property
    def main_params_trainable(self):
        return self._main_params_trainable

    @main_params_trainable.setter
    def main_params_trainable(self, val: bool):
        """Whether the main parameters (i.e. those that are not LoRA) should be trainable.
        This method sets the `requires_grad_` attribute of the main weights
        and controls which parameters are returned in `self.parameters()`.
        :param val: Whether or not to make the parameters trainable.
        :return: None
        """
        self._main_params_trainable = val
        for name, param in super().named_parameters():
            if "lora" not in name:
                param.requires_grad_(val)

    @classmethod
    def from_pretrained(
        cls,
        pretrained_model_name_or_path: Optional[Union[str, os.PathLike]],
        *model_args,
        config: Optional[Union[PretrainedConfig, str, os.PathLike]] = None,
        cache_dir: Optional[Union[str, os.PathLike]] = None,
        ignore_mismatched_sizes: bool = False,
        force_download: bool = False,
        local_files_only: bool = False,
        token: Optional[Union[str, bool]] = None,
        revision: str = "main",
        use_safetensors: bool = None,
        **kwargs,
    ):
        if config.load_trained_adapters:  # checkpoint already contains LoRA adapters
            return super().from_pretrained(
                pretrained_model_name_or_path, *model_args, use_flash_attn=config.use_flash_attn, **kwargs
            )
        else:  # initializing new adapters
            roberta = XLMRobertaModel.from_pretrained(
                pretrained_model_name_or_path, *model_args, use_flash_attn=config.use_flash_attn, **kwargs
            )
            return cls(config, roberta=roberta)

    def _register_lora(self, num_adaptations, rank, dropout_p, alpha):
        self.apply(
            partial(
                LoRAParametrization.add_to_layer,
                num_adaptations=num_adaptations,
                rank=rank,
                dropout_p=dropout_p,
                alpha=alpha,
            )
        )

    def forward(self, *args, **kwargs):
        return self.roberta(*args, **kwargs)

    def parameters(self, recurse: bool = True) -> Iterator[Parameter]:
        for _, param in self.named_parameters(recurse=recurse):
            yield param

    def named_parameters(
        self, prefix: str = "", recurse: bool = True, remove_duplicate: bool = True
    ) -> Iterator[Tuple[str, Parameter]]:
        for name, param in super().named_parameters(
            prefix=prefix, recurse=recurse, remove_duplicate=remove_duplicate
        ):
            if "lora" in name or self.main_params_trainable:
                yield name, param

    @torch.inference_mode()
    def encode(
        self,
        sentences: Union[str, List[str]],
        *args,
        task: Optional[str] = None,
        **kwargs,
    ) -> Union[List[torch.Tensor], np.ndarray, torch.Tensor]:
        """
        Computes sentence embeddings.
        sentences(`str` or `List[str]`):
            Sentence or sentences to be encoded
        task(`str`, *optional*, defaults to `None`):
            Specifies the task for which the encoding is intended. If `task` is not provided,
            all LoRA adapters are disabled, and the model reverts to its original,
            general-purpose weights.
        """
        if task and task not in self._lora_adaptations:
            raise ValueError(
                f"Unsupported task '{task}'. "
                f"Supported tasks are: {', '.join(self.config.lora_adaptations)}."
                f"Alternatively, don't pass the `task` argument to disable LoRA."
            )
        adapter_mask = None
        if task:
            task_id = self._adaptation_map[task]
            num_examples = 1 if isinstance(sentences, str) else len(sentences)
            adapter_mask = torch.full(
                (num_examples,), task_id, dtype=torch.int32, device=self.device
            )
            if isinstance(sentences, str):
                sentences = self._task_instructions[task] + sentences
            else:
                sentences = [self._task_instructions[task] + sentence for sentence in sentences]
        return self.roberta.encode(
            sentences, *args, adapter_mask=adapter_mask, **kwargs
        )