Files
triton/examples/cpp/shift.cpp

78 lines
2.7 KiB
C++
Raw Normal View History

#include <cstring>
#include <cstdio>
2019-06-27 11:37:19 -07:00
#include <sstream>
#include "triton/runtime/jit.h"
#include "triton/driver/backend.h"
#include "triton/driver/stream.h"
#include "triton/tools/bench.hpp"
2019-06-27 11:37:19 -07:00
#include "triton/dnn/shift.h"
2019-06-28 20:22:52 -07:00
#include "triton/external/half.hpp"
2019-07-21 18:11:54 -07:00
double do_bench(triton::driver::context* context,
int32_t R, int32_t S, int32_t B, int32_t F, int32_t H, int32_t W, int32_t C,
triton::dnn::shift::op_t op, triton::dnn::shift::layout_t layout,
std::string numeric_t) {
2019-07-12 20:03:05 -07:00
typedef float NumericT;
// random shifts
std::vector<int32_t> shift_h(C);
std::vector<int32_t> shift_w(C);
for(int32_t c = 0; c < C; c++){
2019-07-21 18:11:54 -07:00
shift_h[c] = rand() % R - R / 2;
shift_w[c] = rand() % S - S / 2;
}
2019-06-27 11:37:19 -07:00
// configuration
2019-07-12 20:03:05 -07:00
triton::dnn::shift shift(B, C, 1, H, W, 1, R, S, F, 1, 1,
shift_h.data(), shift_w.data(),
2019-07-21 18:11:54 -07:00
numeric_t, numeric_t,
2019-07-18 19:39:40 -07:00
op, false, triton::dnn::shift::CHWN);
2019-06-27 11:37:19 -07:00
// host buffers
2019-07-13 21:05:34 -07:00
size_t a_size = B*C*H*W;
size_t b_size = C*F;
size_t c_size = B*F*H*W;
if(op == triton::dnn::shift::BPROP)
std::swap(a_size, c_size);
if(op == triton::dnn::shift::WGRAD){
std::swap(b_size, c_size);
std::swap(a_size, b_size);
}
std::vector<NumericT> ha(a_size);
std::vector<NumericT> hb(b_size);
std::vector<float> hc(c_size);
2019-07-12 20:03:05 -07:00
std::vector<float> rc(hc.size());
2019-06-27 11:37:19 -07:00
// device buffers
triton::driver::buffer* dc = triton::driver::buffer::create(context, hc.size()*4);
2019-06-28 20:22:52 -07:00
triton::driver::buffer* da = triton::driver::buffer::create(context, ha.size()*sizeof(NumericT));
triton::driver::buffer* db = triton::driver::buffer::create(context, hb.size()*sizeof(NumericT));
2019-06-27 11:37:19 -07:00
triton::driver::stream* stream = triton::driver::stream::create(context);
// initialize host
srand(0);
2019-06-27 11:37:19 -07:00
for(size_t i = 0; i < ha.size(); i++)
2019-06-28 20:22:52 -07:00
ha[i] = (NumericT)rand() / RAND_MAX;
for(size_t i = 0; i < hb.size(); i++)
2019-06-28 20:22:52 -07:00
hb[i] = (NumericT)rand() / RAND_MAX;
for(size_t i = 0; i < hc.size(); i++)
hc[i] = 0;
2019-06-27 11:37:19 -07:00
// initialize device
stream->write(da, true, 0, ha);
stream->write(db, true, 0, hb);
stream->write(dc, true, 0, hc);
stream->synchronize();
shift.enqueue(stream, {da, db, dc}, true);
2019-07-21 18:11:54 -07:00
double tns = triton::tools::bench([&]() { shift.enqueue(stream, {da, db, dc}, true);}, stream);
std::cout << tns << std::endl;
}
int main() {
// initialize default compute device
auto context = triton::driver::backend::contexts::get_default();
// shapes
int32_t R = 3, S = 3;
int32_t B = 16, F = 4096;
int32_t H = 32, W = 32;
int32_t C = 4096;
// benchmark
do_bench(context, R, S, B, F, H, W, C, triton::dnn::shift::FPROP, triton::dnn::shift::CHWN, "fp16");
}