Zheliu commited on
Commit
e07ddb2
·
1 Parent(s): 3997e58

refactor RMSNorm by kernels-builder

Browse files
build/torch-universal/RMSNorm/{layers.py → RMSNorm.py} RENAMED
@@ -1,17 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import torch
2
  import torch_npu
 
3
 
4
 
5
- class RMSNorm(torch.nn.Module):
6
- def forward(self, x):
 
7
  """
8
  Forward pass through the RMSNorm layer.
9
-
10
  Args:
11
  x (torch.Tensor): The input tensor.
12
-
13
  Returns:
14
  torch.Tensor: The output tensor after applying RMSNorm.
15
-
16
  """
17
  return torch_npu.npu_rms_norm(x, self.weight, epsilon=self.variance_epsilon)[0]
 
1
+ # Licensed under the Apache License, Version 2.0 (the "License");
2
+ # you may not use this file except in compliance with the License.
3
+ # You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+
14
  import torch
15
  import torch_npu
16
+ from ._ops import add_op_namespace_prefix
17
 
18
 
19
+ @torch.library.custom_op(add_op_namespace_prefix("RMSNorm"), mutates_args=())
20
+ class _RMSNorm(torch.nn.Module):
21
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
22
  """
23
  Forward pass through the RMSNorm layer.
 
24
  Args:
25
  x (torch.Tensor): The input tensor.
 
26
  Returns:
27
  torch.Tensor: The output tensor after applying RMSNorm.
 
28
  """
29
  return torch_npu.npu_rms_norm(x, self.weight, epsilon=self.variance_epsilon)[0]
build/torch-universal/RMSNorm/__init__.py CHANGED
@@ -1,8 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import torch
2
- import torch_npu
3
- from . import layers
 
 
 
 
 
 
 
 
 
 
 
4
 
5
 
6
- __all__ = [
7
- "layers",
8
- ]
 
1
+ # Licensed under the Apache License, Version 2.0 (the "License");
2
+ # you may not use this file except in compliance with the License.
3
+ # You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+
14
  import torch
15
+ from ._ops import ops
16
+ from .RMSNorm import _RMSNorm
17
+
18
+
19
+ def RMSNorm(x: torch.Tensor) -> torch.Tensor:
20
+ """
21
+ Apply RMSNorm to the input tensor.
22
+ Args:
23
+ x (torch.Tensor): The input tensor.
24
+ Returns:
25
+ torch.Tensor: The output tensor after applying RMSNorm.
26
+ """
27
+ return ops.RMSNorm(x)
28
 
29
 
30
+ __all__ = ["RMSNorm"]
 
 
build/torch-universal/RMSNorm/_ops.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ ops = torch.ops._RMSNorm_20251112073655
3
+
4
+ def add_op_namespace_prefix(op_name: str):
5
+ """
6
+ Prefix op by namespace.
7
+ """
8
+ return f"_RMSNorm_20251112073655::{op_name}"