Download unsloth_compiled_cache/Linear4bit_peft_forward.py from RebecaLeyva/Parra_Postpartum_STT: direct link, hf CLI and curl.
- Browser
- Download file 3.66 kB
-
https://huggingface.co/RebecaLeyva/Parra_Postpartum_STT/resolve/main/unsloth_compiled_cache/Linear4bit_peft_forward.py
- Command line
-
hf download hf://RebecaLeyva/Parra_Postpartum_STT/unsloth_compiled_cache/Linear4bit_peft_forward.py
-
curl -L -o Linear4bit_peft_forward.py https://huggingface.co/RebecaLeyva/Parra_Postpartum_STT/resolve/main/unsloth_compiled_cache/Linear4bit_peft_forward.py
3.66 kB
| """ | |
| 2025.5.7 | |
| 2025.5.6 | |
| 4.51.3 | |
| 0.15.2 | |
| __UNSLOTH_VERSIONING__ | |
| """ | |
| torch_compile_options = {'epilogue_fusion': True, 'max_autotune': False, 'shape_padding': True, 'trace.enabled': False, 'triton.cudagraphs': False, 'debug': False, 'dce': True, 'memory_planning': True, 'coordinate_descent_tuning': False, 'trace.graph_diagram': False, 'compile_threads': 24, 'combo_kernels': False, 'group_fusion': True, 'disable_progress': True, 'verbose_progress': False, 'triton.multi_kernel': False, 'triton.use_block_ptr': False, 'triton.enable_persistent_tma_matmul': True, 'triton.autotune_at_compile_time': True} | |
| from torch import Tensor | |
| import torch | |
| import torch.nn as nn | |
| from torch.nn import functional as F | |
| from peft.tuners.lora.bnb import (torch) | |
| torch_addmm = torch.addmm | |
| torch_add = torch.add | |
| # @torch.compile(fullgraph = False, dynamic = True, options = torch_compile_options) | |
| def lora_forward(result, lora_A, lora_B, dropout, x, scaling): | |
| xA = dropout(x) @ lora_A.weight.t() | |
| # output = result + scaling * xA @ lora_B.weight.t() | |
| shape = result.shape | |
| output = torch_addmm( | |
| result.view(-1, shape[-1]), | |
| xA.view(-1, xA.shape[-1]), | |
| lora_B.weight.t(), | |
| alpha = scaling, | |
| beta = 1, | |
| ).view(shape) | |
| bias = lora_B.bias | |
| if bias is not None: | |
| output = torch_add( | |
| output, | |
| bias, | |
| alpha = scaling, | |
| ) | |
| return output | |
| pass | |
| def unsloth_forward(self, x: torch.Tensor, *args, **kwargs) -> torch.Tensor: | |
| adapter_names = kwargs.pop("adapter_names", None) | |
| if self.disable_adapters: | |
| if self.merged: | |
| self.unmerge() | |
| result = self.base_layer(x, *args, **kwargs) | |
| elif adapter_names is not None: | |
| result = self._mixed_batch_forward(x, *args, adapter_names=adapter_names, **kwargs) | |
| elif self.merged: | |
| result = self.base_layer(x, *args, **kwargs) | |
| else: | |
| result = self.base_layer(x, *args, **kwargs) | |
| # As per Tim Dettmers, for 4bit, we need to defensively clone here. | |
| # The reason is that in some cases, an error can occur that backprop | |
| # does not work on a manipulated view. This issue may be solved with | |
| # newer PyTorch versions but this would need extensive testing to be | |
| # sure. | |
| for active_adapter in self.active_adapters: | |
| if active_adapter not in self.lora_A.keys(): | |
| continue | |
| lora_A = self.lora_A[active_adapter] | |
| lora_B = self.lora_B[active_adapter] | |
| dropout = self.lora_dropout[active_adapter] | |
| scaling = self.scaling[active_adapter] | |
| requires_conversion = not torch.is_autocast_enabled() | |
| if requires_conversion: | |
| expected_dtype = result.dtype | |
| x = self._cast_input_dtype(x, lora_A.weight.dtype) | |
| if not self.use_dora[active_adapter]: | |
| return lora_forward(result, lora_A, lora_B, dropout, x, scaling) | |
| else: | |
| if isinstance(dropout, torch.nn.Identity) or not self.training: | |
| base_result = result | |
| else: | |
| x = dropout(x) | |
| base_result = None | |
| output = self.lora_magnitude_vector[active_adapter]( | |
| x, | |
| lora_A=lora_A, | |
| lora_B=lora_B, | |
| scaling=scaling, | |
| base_layer=self.get_base_layer(), | |
| base_result=base_result, | |
| ) | |
| if requires_conversion: | |
| output = output.to(expected_dtype) | |
| result = result + output | |
| return result | |