1 | /******************************************************************************* |
2 | * Copyright 2020-2022 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 | #include "src/common/primitive_cache.hpp" |
22 | |
23 | namespace dnnl { |
24 | |
25 | void fill_primitive_cache(int n) { |
26 | using tag = memory::format_tag; |
27 | using dt = memory::data_type; |
28 | |
29 | engine eng(get_test_engine_kind(), 0); |
30 | for (int i = 0; i < n; i++) { |
31 | // fill primitive cache with n primitives |
32 | auto md = memory::desc({i, 1, 1, 1}, dt::f32, tag::nchw); |
33 | auto relu_pd = eltwise_forward::primitive_desc(eng, |
34 | prop_kind::forward_inference, algorithm::eltwise_relu, md, md, |
35 | 0.f, 0.f); |
36 | auto relu = eltwise_forward(relu_pd); |
37 | } |
38 | } |
39 | |
40 | TEST(primitive_cache_test, TestDefaultCapacity) { |
41 | auto default_capacity = get_primitive_cache_capacity(); |
42 | #ifndef DNNL_DISABLE_PRIMITIVE_CACHE |
43 | ASSERT_EQ(default_capacity, 1024); |
44 | #else |
45 | ASSERT_EQ(default_capacity, 0); |
46 | #endif |
47 | } |
48 | |
49 | #ifndef DNNL_DISABLE_PRIMITIVE_CACHE |
50 | |
51 | TEST(primitive_cache_test, TestInitState) { |
52 | ASSERT_EQ(get_primitive_cache_size(), 0); |
53 | } |
54 | |
55 | TEST(primitive_cache_test, TestSetCapacity) { |
56 | set_primitive_cache_capacity(18); |
57 | ASSERT_EQ(get_primitive_cache_capacity(), 18); |
58 | } |
59 | |
60 | TEST(primitive_cache_test, TestClearCache) { |
61 | set_primitive_cache_capacity(8); |
62 | fill_primitive_cache(8); |
63 | ASSERT_EQ(get_primitive_cache_size(), 8); |
64 | |
65 | set_primitive_cache_capacity(0); |
66 | ASSERT_EQ(get_primitive_cache_size(), 0); |
67 | } |
68 | |
69 | TEST(primitive_cache_test, TestEviction) { |
70 | set_primitive_cache_capacity(0); |
71 | set_primitive_cache_capacity(22); |
72 | fill_primitive_cache(30); |
73 | ASSERT_EQ(get_primitive_cache_size(), 22); |
74 | } |
75 | |
76 | TEST(primitive_cache_test, TestSizeLessCapacity) { |
77 | set_primitive_cache_capacity(0); |
78 | set_primitive_cache_capacity(15); |
79 | fill_primitive_cache(12); |
80 | set_primitive_cache_capacity(13); |
81 | ASSERT_EQ(get_primitive_cache_size(), 12); |
82 | } |
83 | |
84 | TEST(primitive_cache_test, TestSizeGreaterCapacity) { |
85 | set_primitive_cache_capacity(0); |
86 | set_primitive_cache_capacity(15); |
87 | fill_primitive_cache(12); |
88 | set_primitive_cache_capacity(10); |
89 | ASSERT_EQ(get_primitive_cache_size(), 10); |
90 | } |
91 | |
92 | TEST(primitive_cache_test, TestCacheHit) { |
93 | set_primitive_cache_capacity(0); |
94 | set_primitive_cache_capacity(2); |
95 | fill_primitive_cache(1); |
96 | fill_primitive_cache(1); |
97 | |
98 | #if DNNL_CPU_RUNTIME != DNNL_RUNTIME_SYCL |
99 | if (get_test_engine_kind() == engine::kind::cpu) { |
100 | // Regular CPU engines are always considered equal. |
101 | ASSERT_EQ(get_primitive_cache_size(), 1); |
102 | return; |
103 | } |
104 | #endif |
105 | ASSERT_EQ(get_primitive_cache_size(), 2); |
106 | } |
107 | #endif |
108 | |
109 | } // namespace dnnl |
110 | |