forked from ccf-ai-infra/GPUCodeForces
133 lines
4.2 KiB
Python
133 lines
4.2 KiB
Python
import torch
|
|
import torch.nn as nn
|
|
from torch.utils.cpp_extension import load_inline
|
|
|
|
class ModelNew(nn.Module):
|
|
def __init__(self, in_features, alpha=0.0):
|
|
super().__init__()
|
|
self.alpha = nn.Parameter(torch.full((in_features,), alpha))
|
|
self._compile_cuda_kernel()
|
|
|
|
def _compile_cuda_kernel(self):
|
|
cpp_source = """
|
|
#include <torch/extension.h>
|
|
torch::Tensor soft_exponential_cuda(torch::Tensor x, torch::Tensor alpha);
|
|
"""
|
|
|
|
cuda_source = """
|
|
#include <cuda_runtime.h>
|
|
|
|
__device__ __forceinline__ float compute_se(float x, float a) {
|
|
if (fabsf(a) < 1e-6f) {
|
|
return x;
|
|
} else if (a > 0.0f) {
|
|
return (expf(a * x) - 1.0f) / a + a;
|
|
} else {
|
|
float val = 1.0f - a * (x + a);
|
|
return (val > 0.0f) ? (-logf(val) / a) : 0.0f;
|
|
}
|
|
}
|
|
|
|
__global__ void soft_exponential_vec4(
|
|
const float* __restrict__ x,
|
|
float* __restrict__ y,
|
|
const float* __restrict__ alpha,
|
|
int total_vecs,
|
|
int spatial_vecs,
|
|
int channels)
|
|
{
|
|
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
|
int stride = gridDim.x * blockDim.x;
|
|
|
|
const float4* x_vec = reinterpret_cast<const float4*>(x);
|
|
float4* y_vec = reinterpret_cast<float4*>(y);
|
|
|
|
for (int i = idx; i < total_vecs; i += stride) {
|
|
float4 v = x_vec[i];
|
|
float4 out;
|
|
|
|
int c = (i / spatial_vecs) % channels;
|
|
float a = alpha[c];
|
|
|
|
out.x = compute_se(v.x, a);
|
|
out.y = compute_se(v.y, a);
|
|
out.z = compute_se(v.z, a);
|
|
out.w = compute_se(v.w, a);
|
|
|
|
y_vec[i] = out;
|
|
}
|
|
}
|
|
|
|
__global__ void soft_exponential_scalar(
|
|
const float* __restrict__ x,
|
|
float* __restrict__ y,
|
|
const float* __restrict__ alpha,
|
|
int total_elements,
|
|
int spatial_size,
|
|
int channels)
|
|
{
|
|
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
|
int stride = gridDim.x * blockDim.x;
|
|
|
|
for (int i = idx; i < total_elements; i += stride) {
|
|
int c = (i / spatial_size) % channels;
|
|
float a = alpha[c];
|
|
y[i] = compute_se(x[i], a);
|
|
}
|
|
}
|
|
|
|
torch::Tensor soft_exponential_cuda(torch::Tensor x, torch::Tensor alpha) {
|
|
auto x_c = x.contiguous();
|
|
auto alpha_c = alpha.contiguous();
|
|
auto output = torch::empty_like(x_c);
|
|
|
|
int total_elements = x_c.numel();
|
|
int N = x_c.size(0);
|
|
int C = x_c.size(1);
|
|
int S = total_elements / (N * C);
|
|
|
|
if (S % 4 == 0) {
|
|
int total_vecs = total_elements / 4;
|
|
int spatial_vecs = S / 4;
|
|
int threads = 256;
|
|
int blocks = (total_vecs + threads - 1) / threads;
|
|
if (blocks > 65535) blocks = 65535;
|
|
|
|
soft_exponential_vec4<<<blocks, threads>>>(
|
|
x_c.data_ptr<float>(),
|
|
output.data_ptr<float>(),
|
|
alpha_c.data_ptr<float>(),
|
|
total_vecs,
|
|
spatial_vecs,
|
|
C
|
|
);
|
|
} else {
|
|
int threads = 256;
|
|
int blocks = (total_elements + threads - 1) / threads;
|
|
if (blocks > 65535) blocks = 65535;
|
|
|
|
soft_exponential_scalar<<<blocks, threads>>>(
|
|
x_c.data_ptr<float>(),
|
|
output.data_ptr<float>(),
|
|
alpha_c.data_ptr<float>(),
|
|
total_elements,
|
|
S,
|
|
C
|
|
);
|
|
}
|
|
|
|
return output;
|
|
}
|
|
"""
|
|
|
|
self.op = load_inline(
|
|
name="soft_exponential_opt",
|
|
cpp_sources=cpp_source,
|
|
cuda_sources=cuda_source,
|
|
functions=["soft_exponential_cuda"],
|
|
extra_cuda_cflags=["-O3"],
|
|
verbose=False
|
|
)
|
|
|
|
def forward(self, x):
|
|
return self.op.soft_exponential_cuda(x, self.alpha) |