| """ |
| 2026.5.5 |
| 2026.5.10 |
| 5.5.0 |
| 1.5.1 |
| __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': 12, 'group_fusion': True, 'disable_progress': True, 'verbose_progress': False, 'triton.multi_kernel': 0, 'triton.use_block_ptr': False, 'triton.enable_persistent_tma_matmul': True, 'triton.autotune_at_compile_time': False, 'triton.cooperative_reductions': False, 'cuda.compile_opt_level': '-O2', 'cuda.enable_cuda_lto': True, 'combo_kernels': False, 'benchmark_combo_kernel': True, 'combo_kernel_foreach_dynamic_shapes': True} |
| from torch import Tensor |
| import torch |
| import torch.nn as nn |
| from torch.nn import functional as F |
| from unsloth_zoo.temporary_patches.common import torch_compile |
| from typing import Any, List, Optional, Tuple, Union, Dict, Set, Callable |
| from peft.tuners.lora.variants import (torch) |
|
|
|
|
| torch_addmm = torch.addmm |
| torch_add = torch.add |
| |
| def lora_forward(result, lora_A, lora_B, dropout, x, scaling): |
| |
| |
| target_dtype = result.dtype |
| xA = dropout(x).to(target_dtype) @ lora_A.weight.to(target_dtype).t() |
| |
| shape = result.shape |
| output = torch_addmm( |
| result.view(-1, shape[-1]), |
| xA.view(-1, xA.shape[-1]), |
| lora_B.weight.to(target_dtype).t(), |
| alpha = scaling, |
| beta = 1, |
| ).view(shape) |
|
|
| bias = lora_B.bias |
| if bias is not None: |
| output = torch_add( |
| output, |
| bias.to(target_dtype), |
| alpha = scaling, |
| ) |
| return output |
| pass |
|
|
| def unsloth_forward(self, x: torch.Tensor) -> torch.Tensor: |
| first_dims = x.shape[:-1] |
| if x.dim() != 2: |
| x = x.reshape(-1, x.shape[-1]) |
| B = x.shape[0] |
| nb = self.nblocks |
| m = x.shape[-1] // nb |
| n = self.out_features // nb |
| x = x.reshape(B, nb, m) |
| w = self.weight.view(nb, n, m) |
| out = torch.einsum("bim,inm->bin", x, w) |
| return out.reshape(*first_dims, -1) |
|
|