forked from openai/parameter-golf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cpp_muon.py
More file actions
39 lines (32 loc) · 925 Bytes
/
Copy pathtest_cpp_muon.py
File metadata and controls
39 lines (32 loc) · 925 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import torch
from torch.utils.cpp_extension import load_inline
cpp_source = """
#include <torch/extension.h>
torch::Tensor zeropower_via_newtonschulz5_cpp(torch::Tensor G, int steps, double eps) {
double a = 3.4445, b = -4.7750, c = 2.0315;
auto X = G.to(torch::kBFloat16);
X = X / (X.norm() + eps);
bool transposed = G.size(0) > G.size(1);
if (transposed) {
X = X.t();
}
for (int i = 0; i < steps; ++i) {
auto A = torch::matmul(X, X.t());
auto B = b * A + c * torch::matmul(A, A);
X = a * X + torch::matmul(B, X);
}
if (transposed) {
X = X.t();
}
return X;
}
"""
module = load_inline(
name="muon_cpp",
cpp_sources=cpp_source,
functions=["zeropower_via_newtonschulz5_cpp"],
verbose=True,
)
G = torch.randn(128, 128, device='cuda')
out = module.zeropower_via_newtonschulz5_cpp(G, 10, 1e-7)
print(out.shape)