|
|
""" |
|
|
2025.3.17 |
|
|
2025.3.19 |
|
|
4.50.0 |
|
|
0.15.2 |
|
|
__UNSLOTH_VERSIONING__ |
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import os |
|
|
import importlib.util |
|
|
if importlib.util.find_spec("unsloth_studio") is None: |
|
|
UNSLOTH_STUDIO_ENABLED = False |
|
|
else: |
|
|
UNSLOTH_STUDIO_ENABLED = os.environ.get("UNSLOTH_STUDIO_DISABLED", "0") == "0" |
|
|
pass |
|
|
from typing import List, Dict, Tuple, Optional, Any, Callable |
|
|
import math |
|
|
|
|
|
torch_compile_options = {'epilogue_fusion': True, 'max_autotune': False, 'shape_padding': True, 'trace.enabled': False, 'triton.cudagraphs': False} |
|
|
from torch import Tensor |
|
|
import torch |
|
|
import torch.nn as nn |
|
|
from torch.nn import functional as F |
|
|
from transformers.models.gemma3.modeling_gemma3 import (nn) |
|
|
|
|
|
def forward(self, input: Tensor) -> Tensor: |
|
|
self._check_input_dim(input) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.momentum is None: |
|
|
exponential_average_factor = 0.0 |
|
|
else: |
|
|
exponential_average_factor = self.momentum |
|
|
|
|
|
if self.training and self.track_running_stats: |
|
|
|
|
|
if self.num_batches_tracked is not None: |
|
|
self.num_batches_tracked.add_(1) |
|
|
if self.momentum is None: |
|
|
exponential_average_factor = 1.0 / float(self.num_batches_tracked) |
|
|
else: |
|
|
exponential_average_factor = self.momentum |
|
|
|
|
|
r""" |
|
|
Decide whether the mini-batch stats should be used for normalization rather than the buffers. |
|
|
Mini-batch stats are used in training mode, and in eval mode when buffers are None. |
|
|
""" |
|
|
if self.training: |
|
|
bn_training = True |
|
|
else: |
|
|
bn_training = (self.running_mean is None) and (self.running_var is None) |
|
|
|
|
|
r""" |
|
|
Buffers are only updated if they are to be tracked and we are in training mode. Thus they only need to be |
|
|
passed when the update should occur (i.e. in training mode when they are tracked), or when buffer stats are |
|
|
used for normalization (i.e. in eval mode when buffers are not None). |
|
|
""" |
|
|
return F.batch_norm( |
|
|
input, |
|
|
|
|
|
self.running_mean |
|
|
if not self.training or self.track_running_stats |
|
|
else None, |
|
|
self.running_var if not self.training or self.track_running_stats else None, |
|
|
self.weight, |
|
|
self.bias, |
|
|
bn_training, |
|
|
exponential_average_factor, |
|
|
self.eps, |
|
|
).to(input.dtype).to(input.dtype) |
|
|
|