Upload folder using huggingface_hub
Browse files- build/torch-cuda/__init__.py +24 -17
build/torch-cuda/__init__.py
CHANGED
|
@@ -1,36 +1,43 @@
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
|
| 4 |
-
from transformers.
|
| 5 |
|
| 6 |
-
conversion_mapping = [
|
| 7 |
-
WeightRenaming(
|
| 8 |
-
source_patterns=r"(.*(?:input_layernorm|post_attention_layernorm|norm)\.)weight",
|
| 9 |
-
target_patterns=r"\1scale",
|
| 10 |
-
),
|
| 11 |
-
]
|
| 12 |
|
| 13 |
-
|
| 14 |
-
class CustomRMSNorm(nn.Module):
|
| 15 |
"""
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
single-layer replacement where the kernel defines ``__init__`` and
|
| 24 |
-
carries a ``conversion_mapping``.
|
| 25 |
"""
|
| 26 |
|
| 27 |
-
conversion_mapping =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def __init__(self, hidden_size: int, eps: float = 1e-6):
|
| 30 |
super().__init__()
|
| 31 |
self.scale = nn.Parameter(torch.ones(hidden_size))
|
| 32 |
self.variance_epsilon = eps
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
| 35 |
print("This dummy kernel is used")
|
| 36 |
input_dtype = hidden_states.dtype
|
|
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
|
| 4 |
+
from transformers.conversion_mapping import WeightRenaming
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
class CustomRMSNormLayout(nn.Module):
|
|
|
|
| 8 |
"""
|
| 9 |
+
Layout class for the CustomRMSNorm kernel.
|
| 10 |
|
| 11 |
+
Transformers instantiates this in place of the original RMSNorm, passing
|
| 12 |
+
the same constructor arguments (hidden_size, eps). The weight is stored
|
| 13 |
+
as ``self.scale`` instead of ``self.weight``; the conversion_mapping below
|
| 14 |
+
handles the checkpoint key rename at load time.
|
| 15 |
|
| 16 |
+
kernelize then replaces this module's forward with CustomRMSNorm.forward.
|
|
|
|
|
|
|
| 17 |
"""
|
| 18 |
|
| 19 |
+
conversion_mapping = [
|
| 20 |
+
WeightRenaming(
|
| 21 |
+
source_patterns=r"(.*(?:input_layernorm|post_attention_layernorm|norm)\.)weight",
|
| 22 |
+
target_patterns=r"\1scale",
|
| 23 |
+
),
|
| 24 |
+
]
|
| 25 |
|
| 26 |
def __init__(self, hidden_size: int, eps: float = 1e-6):
|
| 27 |
super().__init__()
|
| 28 |
self.scale = nn.Parameter(torch.ones(hidden_size))
|
| 29 |
self.variance_epsilon = eps
|
| 30 |
|
| 31 |
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
| 32 |
+
pass # replaced at runtime by kernelize
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class CustomRMSNorm(nn.Module):
|
| 36 |
+
"""
|
| 37 |
+
Forward-only kernel class. kernelize discovers this via kernel_layer_name
|
| 38 |
+
set on CustomRMSNormLayout and replaces the layout instance's forward at runtime.
|
| 39 |
+
"""
|
| 40 |
+
|
| 41 |
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
| 42 |
print("This dummy kernel is used")
|
| 43 |
input_dtype = hidden_states.dtype
|