1 #include "caffe2/mobile/contrib/arm-compute/core/context.h" 2 #include <gtest/gtest.h> 4 #include "caffe2/core/graph.h" 5 #include "caffe2/core/operator.h" 6 #include "caffe2/core/workspace.h" 10 #define DECLARE_OPENGL_OPERATOR(_name) \ 12 _name.mutable_device_option()->set_device_type(OPENGL); 14 #define MAKE_OPENGL_OPERATOR(_op) \ 15 _op->mutable_device_option()->set_device_type(OPENGL); 17 #define ADD_ARG(_op, _name, _type, _val) \ 19 Argument *arg = _op.add_arg(); \ 20 arg->set_name(_name); \ 21 arg->set_##_type(_val); \ 26 template<
typename T =
float>
27 void PopulateCPUBlob(Workspace *ws,
bool random, std::string name,
28 std::vector<int> dims,
int val = 1,
int dist_shift = 0,
float variance = 1) {
29 Blob *blob = ws->CreateBlob(name);
30 auto *tensor = blob->GetMutable<TensorCPU>();
32 T *t_data = tensor->mutable_data<T>();
33 std::random_device rd;
34 std::mt19937 e2(rd());
35 std::normal_distribution<> dist(0 + dist_shift, variance + dist_shift);
36 for (
int i = 0; i < tensor->size(); ++i) {
37 t_data[i] = T(random ? dist(e2) : (val == 1337 ? i : val));
41 template<
typename T = half>
42 void compareNetResult(Workspace& ws,
43 NetDef& cpu_net, NetDef& gpu_net,
44 string cpu_blob=
"ref_Y",
45 string gpu_blob=
"gpu_Y",
47 bool relative=
false) {
48 ws.RunNetOnce(cpu_net);
49 ws.RunNetOnce(gpu_net);
51 Blob *cpu_out = ws.GetBlob(cpu_blob);
52 Blob *gpu_out = ws.GetBlob(gpu_blob);
53 EXPECT_NE(
nullptr, cpu_out);
54 EXPECT_NE(
nullptr, gpu_out);
57 auto& g_ = gpu_out->Get<GLTensor<T>>();
60 auto &t = cpu_out->Get<TensorCPU>();
61 EXPECT_EQ(g.size(), t.size());
63 for (
auto i = 0; i < g.size(); ++i) {
65 EXPECT_NEAR(g.data<
float>()[i], t.data<
float>()[i], tol + tol * std::abs(t.data<
float>()[i])) <<
"at index " << i;
67 EXPECT_NEAR(g.data<
float>()[i], t.data<
float>()[i], tol)
73 template<
typename T = half>
74 void compareNetResult4D(Workspace& ws,
75 NetDef& cpu_net, NetDef& gpu_net,
76 string cpu_blob=
"ref_Y",
77 string gpu_blob=
"gpu_Y",
79 ws.RunNetOnce(cpu_net);
80 ws.RunNetOnce(gpu_net);
82 Blob *cpu_out = ws.GetBlob(cpu_blob);
83 Blob *gpu_out = ws.GetBlob(gpu_blob);
84 auto &g_ = gpu_out->Get<GLTensor<T>>();
86 EXPECT_NE(
nullptr, cpu_out);
87 EXPECT_NE(
nullptr, gpu_out);
90 auto &t = cpu_out->Get<TensorCPU>();
93 char *byte_buffer = (
char *)buffer;
94 auto info = g_.get_underlying()->info();
96 CAFFE_ENFORCE(byte_buffer != NULL);
101 #define get_elem(_a, _b, _c) \ 102 (half *)&byte_buffer[info->offset_element_in_bytes( \ 103 arm_compute::Coordinates(_a, _b, _c))] 104 for (
auto c = 0; c < C; ++c) {
105 for (
auto h = 0; h < H; ++h) {
106 for (
auto w = 0; w < W; ++w) {
107 auto t_elem = t.data<
float>()[(c * H + h) * W + w];
108 auto g_elem = get_elem(w, h, c);
110 if (!isnan(t_elem) && (std::abs(t_elem -
float(*g_elem)) > tol + tol * std::abs(t_elem))) {
113 CHECK(diff_num <= 0.03 * C*H*W);
void Resize(Ts...dim_source)
Resizes a tensor.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...