Kernels
relu / bench /sycl_relu.cpp
superexpai's picture
Optimized relu: cpu/cuda/xpu, 1.2-1.85x faster on RTX 4090, benchmarked vs upstream and torch.relu
e873e70 verified
Raw
History Blame Contribute Delete
1.3 kB
// kernels-community/relu XPU backend kernel (SYCL parallel_for), torch-free.
// Runs on whatever SYCL device is present (here: the i9 CPU via OpenCL).
#include <sycl/sycl.hpp>
#include <cstdio>
#include <cstdlib>
#include <chrono>
#include <algorithm>
using namespace sycl;
int main(){
queue q{default_selector_v};
printf("SYCL device: %s\n", q.get_device().get_info<info::device::name>().c_str());
const size_t n = 256ull*1024*1024; // 1 GB/array
float* in = malloc_shared<float>(n, q);
float* out = malloc_shared<float>(n, q);
for(size_t i=0;i<n;++i) in[i]=((i&1)?-1.f:1.f)*float(i%97);
double gb = 2.0*n*sizeof(float)/1e9;
// same body as relu_xpu/relu.cpp
auto run=[&](){ q.parallel_for(range<1>(n),[=](id<1> i){ out[i]=in[i]>0.f?in[i]:0.f; }).wait(); };
for(int w=0;w<3;w++) run();
bool ok=true; for(size_t i=0;i<n&&ok;i++){ float e=in[i]>0?in[i]:0; if(out[i]!=e) ok=false; }
double best=1e30;
for(int r=0;r<8;r++){
auto t0=std::chrono::high_resolution_clock::now(); run();
auto t1=std::chrono::high_resolution_clock::now();
best=std::min(best,std::chrono::duration<double>(t1-t0).count());
}
printf("SYCL relu (xpu backend): %.1f GB/s (%.2f ms) %s\n", gb/best, best*1e3, ok?"OK":"FAIL");
free(in,q); free(out,q);
return 0;
}