1 /*******************************************************************************
2 * Copyright 2020 Intel Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16
17 #include "dnnl_test_common.hpp"
18 #include "gtest/gtest.h"
19
20 #include "oneapi/dnnl/dnnl.hpp"
21
22 namespace dnnl {
23
24 using dt = memory::data_type;
25 using tag = memory::format_tag;
26
27 // This test checks that globally defined primitive_t object will be
28 // successfully destroyed after finishing the program despite the order of
29 // internal objects destruction.
30 // The cause was thread-local non-trivially-constructed object in
31 // global_scratchpad_t object which got destroyed before global_scratchpad_t
32 // causing a crash.
33 class global_scratchpad_t : public ::testing::Test {};
34
35 struct conv_ctx_t {
conv_ctx_tdnnl::conv_ctx_t36 conv_ctx_t() : eng_(engine::kind::cpu, 0) {}
37
38 struct conv_t {
39 conv_t() = default;
40
41 memory::desc src_md;
42 memory::desc wei_md;
43 memory::desc dst_md;
44 convolution_forward::primitive_desc pd;
45 memory src_mem;
46 memory wei_mem;
47 memory dst_mem;
48 primitive prim;
49 };
50
Setupdnnl::conv_ctx_t51 void Setup(const memory::dims &src_dims, const memory::dims &wei_dims,
52 const memory::dims &dst_dims, const memory::dims &strides_dims,
53 const memory::dims &dilations_dims,
54 const memory::dims &padding_left,
55 const memory::dims &padding_right) {
56 c_.src_md = memory::desc(src_dims, dt::f32, tag::any);
57 c_.wei_md = memory::desc(wei_dims, dt::f32, tag::any);
58 c_.dst_md = memory::desc(dst_dims, dt::f32, tag::any);
59
60 auto desc = convolution_forward::desc(prop_kind::forward,
61 algorithm::convolution_direct, c_.src_md, c_.wei_md, c_.dst_md,
62 strides_dims, dilations_dims, padding_left, padding_right);
63
64 c_.pd = convolution_forward::primitive_desc(desc, eng_);
65
66 c_.src_mem = test::make_memory(c_.pd.src_desc(), eng_);
67 c_.wei_mem = test::make_memory(c_.pd.weights_desc(), eng_);
68 c_.dst_mem = test::make_memory(c_.pd.dst_desc(), eng_);
69
70 c_.prim = convolution_forward(c_.pd);
71 }
72
73 engine eng_;
74 struct conv_t c_;
75 };
76
77 conv_ctx_t global_conv_ctx1;
78 conv_ctx_t global_conv_ctx2;
79
HANDLE_EXCEPTIONS_FOR_TEST(global_scratchpad_t,TestGlobalScratchpad)80 HANDLE_EXCEPTIONS_FOR_TEST(global_scratchpad_t, TestGlobalScratchpad) {
81 #if defined(DNNL_WITH_SYCL) && defined(TEST_DNNL_DPCPP_BUFFER)
82 // It seems static USM data doesn't get along with OpenCL runtime.
83 // TODO: investigate.
84 if (get_test_engine_kind() == engine::kind::gpu) return;
85 #endif
86
87 memory::dims src1 = {1, 1, 3, 4};
88 memory::dims wei1 = {1, 1, 3, 3};
89 memory::dims dst1 = {1, 1, 8, 5};
90 memory::dims str1 = {1, 1};
91 memory::dims dil1 = {0, 0};
92 memory::dims pad_l1 = {3, 1};
93 memory::dims pad_r1 = {4, 2};
94 global_conv_ctx1.Setup(src1, wei1, dst1, str1, dil1, pad_l1, pad_r1);
95
96 memory::dims src2 = {256, 3, 227, 227};
97 memory::dims wei2 = {96, 3, 11, 11};
98 memory::dims dst2 = {256, 96, 55, 55};
99 memory::dims str2 = {4, 4};
100 memory::dims dil2 = {0, 0};
101 memory::dims pad_l2 = {0, 0};
102 memory::dims pad_r2 = {0, 0};
103 global_conv_ctx2.Setup(src2, wei2, dst2, str2, dil2, pad_l2, pad_r2);
104
105 // if something goes wrong, test should return 139 on Linux.
106 };
107
108 } // namespace dnnl
109