:param adapter_name: options: 'default' :param reconstr_type: options: 'svd'
(model, peft_config, adapter_name, reconstr_type, reconstruct_config, writer)
| 153 | |
| 154 | |
| 155 | def find_and_initialize(model, peft_config, adapter_name, reconstr_type, reconstruct_config, writer): |
| 156 | """ |
| 157 | :param adapter_name: options: 'default' |
| 158 | :param reconstr_type: options: 'svd' |
| 159 | """ |
| 160 | half_init_dec = reconstruct_config['half_init_dec'] |
| 161 | replacement_module_random_init = reconstruct_config['replacement_module_random_init'] |
| 162 | reconstruction_mode = reconstruct_config['reconstr_mode'] |
| 163 | lora_config = peft_config[adapter_name] |
| 164 | r_squared = reconstruct_config['r_squared'] # whether using r*r matrix between lora_A and lora_B or not |
| 165 | loaded_in_8bit = getattr(model, "is_loaded_in_8bit", False) |
| 166 | if loaded_in_8bit and not is_bnb_available(): |
| 167 | raise ImportError( |
| 168 | "To use Lora with 8-bit quantization, please install the `bitsandbytes` package. " |
| 169 | "You can install it with `pip install bitsandbytes`." |
| 170 | ) |
| 171 | is_target_modules_in_base_model = False |
| 172 | key_list = [key for key, _ in model.named_modules()] |
| 173 | assert (not isinstance(lora_config.target_modules, str)) |
| 174 | print("Iterating through model's specified modules to initialize A/B matrices.") |
| 175 | for key in tqdm(key_list): |
| 176 | target_module_found = any(key.endswith('.' + target_key) for target_key in lora_config.target_modules) |
| 177 | if target_module_found: |
| 178 | if not is_target_modules_in_base_model: |
| 179 | is_target_modules_in_base_model = True |
| 180 | _, target, target_name = _get_submodules(model, key) |
| 181 | |
| 182 | if reconstruction_mode == 'separated': |
| 183 | replacement_encoder_weight, replacement_decoder_weight = get_replacement_module(weight=target.weight.T, |
| 184 | module_name=key, |
| 185 | type=reconstr_type, |
| 186 | writer=writer, |
| 187 | reconstruct_config=reconstruct_config) |
| 188 | |
| 189 | |
| 190 | |
| 191 | if not isinstance(target, peft.tuners.lora.Linear): |
| 192 | raise NotImplementedError('Only initialization for peft.tuners.lora.Linear type is implemented.') |
| 193 | # TODO implement for Linear8bitLt |
| 194 | else: |
| 195 | if half_init_dec: |
| 196 | kaiming_uniform_init_lower_half(replacement_decoder_weight) |
| 197 | if replacement_module_random_init: |
| 198 | kaiming_uniform_init(replacement_encoder_weight) |
| 199 | kaiming_uniform_init(replacement_decoder_weight) |
| 200 | replace_module_weights(target.lora_B.default, replacement_decoder_weight.T) |
| 201 | if r_squared: |
| 202 | target.forward = types.MethodType(forward_latent, target) |
| 203 | target.get_delta_weight = types.MethodType(get_delta_weight, target) |
| 204 | replace_module_weights(target.lora_A.default, replacement_encoder_weight.T) |
| 205 | target.default_lora_latent_mapping = torch.nn.Linear(lora_config.r, lora_config.r, bias=False) |
| 206 | init_module_weights(target.default_lora_latent_mapping, sigma=0.00001) |
| 207 | target.default_lora_latent_mapping.to(target.lora_A.default.weight.device) |
| 208 | |
| 209 | target.lora_A.default.weight.requires_grad = False # only the r*r matrix will be tuned |
| 210 | target.lora_B.default.weight.requires_grad = False # only the r*r matrix will be tuned |
| 211 | |
| 212 | else: |
no test coverage detected