no CPU mode
#1
by rahul7star - opened
sad to see cant run this anymore
import torch
import torch.nn as nn
from huggingface_hub import hf_hub_download
import importlib.util
device = "cpu"
# ---- load kernel from HF ----
kernel_path = hf_hub_download(
repo_id="kernels-community/relu",
filename="relu.py"
)
spec = importlib.util.spec_from_file_location("hf_relu", kernel_path)
hf_relu = importlib.util.module_from_spec(spec)
spec.loader.exec_module(hf_relu)
# assume kernel exposes relu()
hf_relu_fn = hf_relu.relu
# ---- model using HF relu ----
class P(nn.Module):
def __init__(self):
super().__init__()
self.x = nn.Parameter(torch.tensor([1.0, 2.9]))
def forward(self, x):
c = self.x + x
c = hf_relu_fn(c) # ✅ HF kernel relu
return c
In = torch.tensor([1.0, 2.09])
O = torch.tensor([10.0, 30.0])
Mo = P().to(device)
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(Mo.parameters(), lr=0.01)
for epoch in range(2000):
optimizer.zero_grad()
OO = Mo(In)
loss = criterion(OO, O)
loss.backward()
optimizer.step()
print("Learned parameter:", Mo.x)
Even this also
import torch
from kernels import get_kernel
activation = get_kernel("kernels-community/activation", version=1)
device = "cuda" if torch.cuda.is_available() else "cpu"
x = torch.randn((10, 10), dtype=torch.float32, device=device)
y = torch.empty_like(x)
if device == "cuda":
activation.gelu_fast(y, x)
else:
# CPU fallback
y = torch.nn.functional.gelu(x)
print(y)