Kernels
File size: 1,295 Bytes
e873e70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// 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;
}