mkldnn.cpp 3.52 KB
Newer Older
1
//*****************************************************************************
2
// Copyright 2017-2020 Intel Corporation
3 4 5 6 7 8 9 10 11 12 13 14 15
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
16 17 18

#include <iostream>
#include <mkldnn.hpp>
19
#include <vector>
20 21 22

#include "gtest/gtest.h"

23
static int tensor_volume(const mkldnn::memory::dims& t)
24 25 26 27 28 29 30
{
    int x = 1;
    for (const auto i : t)
        x *= i;
    return x;
}

Robert Kimball's avatar
Robert Kimball committed
31
void test()
32 33 34
{
    using namespace mkldnn;

Robert Kimball's avatar
Robert Kimball committed
35
    auto cpu_engine = engine(engine::cpu, 0);
36

Robert Kimball's avatar
Robert Kimball committed
37 38 39 40 41 42 43 44 45 46 47 48 49
    const int mb = 2;
    const int groups = 2;
    memory::dims input_tz = {mb, 256, 13, 13};
    memory::dims weights_tz = {groups, 384 / groups, 256 / groups, 3, 3};
    memory::dims bias_tz = {384};
    memory::dims strides = {1, 1};
    memory::dims padding = {0, 0};
    memory::dims output_tz = {
        mb,
        384,
        (input_tz[2] + 2 * padding[0] - weights_tz[3]) / strides[0] + 1,
        (input_tz[3] + 2 * padding[1] - weights_tz[4]) / strides[1] + 1,
    };
50

Robert Kimball's avatar
Robert Kimball committed
51 52 53 54
    std::vector<float> input(tensor_volume(input_tz), .0f);
    std::vector<float> weights(tensor_volume(weights_tz), .0f);
    std::vector<float> bias(tensor_volume(bias_tz), .0f);
    std::vector<float> output(tensor_volume(output_tz), .0f);
55

Robert Kimball's avatar
Robert Kimball committed
56 57 58 59 60
    auto c3_src_desc = memory::desc({input_tz}, memory::data_type::f32, memory::format::nchw);
    auto c3_weights_desc =
        memory::desc({weights_tz}, memory::data_type::f32, memory::format::goihw);
    auto c3_bias_desc = memory::desc({bias_tz}, memory::data_type::f32, memory::format::x);
    auto c3_dst_desc = memory::desc({output_tz}, memory::data_type::f32, memory::format::nchw);
61

Robert Kimball's avatar
Robert Kimball committed
62 63 64 65
    auto c3_src = memory({c3_src_desc, cpu_engine}, input.data());
    auto c3_weights = memory({c3_weights_desc, cpu_engine}, weights.data());
    auto c3_bias = memory({c3_bias_desc, cpu_engine}, bias.data());
    auto c3_dst = memory({c3_dst_desc, cpu_engine}, output.data());
66

Robert Kimball's avatar
Robert Kimball committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    auto c3 = convolution_forward(
        convolution_forward::primitive_desc(convolution_forward::desc(prop_kind::forward,
                                                                      algorithm::convolution_direct,
                                                                      c3_src_desc,
                                                                      c3_weights_desc,
                                                                      c3_bias_desc,
                                                                      c3_dst_desc,
                                                                      strides,
                                                                      padding,
                                                                      padding,
                                                                      padding_kind::zero),
                                            cpu_engine),
        c3_src,
        c3_weights,
        c3_bias,
        c3_dst);
83

Robert Kimball's avatar
Robert Kimball committed
84 85
    stream(stream::kind::eager).submit({c3}).wait();
}
86

Robert Kimball's avatar
Robert Kimball committed
87 88 89
TEST(mkldnn, engine)
{
    EXPECT_NO_THROW(test());
90
}