michaelbenayoun HF Staff commited on
Commit
e3838bb
·
verified ·
1 Parent(s): 2034760

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. 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.core_model_loading import WeightRenaming
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
- Drop-in replacement for LlamaRMSNorm with a different parameter layout.
17
 
18
- Stores the learnable gain as ``self.scale`` instead of ``self.weight``.
19
- The ``conversion_mapping`` class attribute tells the loader to rename
20
- checkpoint keys so that ``*.weight`` ``*.scale`` for any RMSNorm path.
 
21
 
22
- This kernel exists to test the ``register_kernel_replacements`` path:
23
- single-layer replacement where the kernel defines ``__init__`` and
24
- carries a ``conversion_mapping``.
25
  """
26
 
27
- conversion_mapping = 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