1/*******************************************************************************
2* Copyright 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
22namespace dnnl {
23
24class memory_desc_test_t : public ::testing::Test {};
25
26HANDLE_EXCEPTIONS_FOR_TEST(memory_desc_test_t, TestQueryDefaultConstructor) {
27 memory::desc md;
28 EXPECT_EQ(md.get_ndims(), 0);
29 EXPECT_EQ(md.get_size(), size_t(0));
30 EXPECT_EQ(md.get_submemory_offset(), 0);
31 EXPECT_EQ(md.get_inner_nblks(), 0);
32 EXPECT_EQ(md.get_format_kind(), memory::format_kind::undef);
33 EXPECT_EQ(md.get_data_type(), memory::data_type::undef);
34
35 EXPECT_TRUE(md.is_zero());
36 EXPECT_TRUE(md.get_dims().empty());
37 EXPECT_TRUE(md.get_padded_dims().empty());
38 EXPECT_TRUE(md.get_padded_offsets().empty());
39 EXPECT_TRUE(md.get_strides().empty());
40 EXPECT_TRUE(md.get_inner_blks().empty());
41 EXPECT_TRUE(md.get_inner_idxs().empty());
42}
43
44HANDLE_EXCEPTIONS_FOR_TEST(memory_desc_test_t, TestQueryBlockedFormat) {
45 const memory::dims dims = {32, 64, 3, 3};
46 const memory::data_type dt = memory::data_type::f32;
47 auto md = memory::desc(dims, dt, memory::format_tag::ABcd16a16b);
48
49 const auto exp_format_kind = memory::format_kind::blocked;
50 const memory::dim exp_submemory_offset = 0;
51 const memory::dims exp_padded_offsets = {0, 0, 0, 0};
52 const memory::dims exp_inner_blks = {16, 16};
53 const memory::dims exp_inner_idxs = {0, 1};
54 const memory::dims exp_strides = {9216, 2304, 768, 256};
55 const size_t exp_size = 73728;
56
57 EXPECT_EQ(md.get_ndims(), (int)dims.size());
58 EXPECT_EQ(md.get_submemory_offset(), exp_submemory_offset);
59 EXPECT_EQ(md.get_inner_nblks(), (int)exp_inner_blks.size());
60 EXPECT_EQ(md.get_format_kind(), exp_format_kind);
61 EXPECT_EQ(md.get_data_type(), dt);
62
63 EXPECT_EQ(md.get_dims(), dims);
64 EXPECT_EQ(md.get_padded_dims(), dims);
65 EXPECT_EQ(md.get_padded_offsets(), exp_padded_offsets);
66 EXPECT_EQ(md.get_strides(), exp_strides);
67 EXPECT_EQ(md.get_inner_blks(), exp_inner_blks);
68 EXPECT_EQ(md.get_inner_idxs(), exp_inner_idxs);
69 EXPECT_EQ(md.get_size(), exp_size);
70
71 EXPECT_TRUE(!md.is_zero());
72}
73
74HANDLE_EXCEPTIONS_FOR_TEST(memory_desc_test_t, TestComparison) {
75 auto zero_md = memory::desc();
76 auto blocked_md = memory::desc({32, 64, 3, 3}, memory::data_type::f32,
77 memory::format_tag::ABcd16a16b);
78 auto plain_md = memory::desc(
79 {32, 64, 3, 3}, memory::data_type::f32, memory::format_tag::abcd);
80
81 EXPECT_EQ(zero_md, memory::desc {});
82 EXPECT_EQ(zero_md, zero_md);
83 EXPECT_EQ(plain_md, plain_md);
84 EXPECT_EQ(blocked_md, blocked_md);
85
86 EXPECT_NE(zero_md, plain_md);
87 EXPECT_NE(zero_md, blocked_md);
88 EXPECT_NE(plain_md, blocked_md);
89}
90
91} // namespace dnnl
92