2 // Copyright (c) 2016 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 #include <gtest/gtest.h>
19 #include "api/CPP/memory.hpp"
20 #include <api/CPP/input_layout.hpp>
21 #include "api/CPP/convolution.hpp"
22 #include "api/CPP/eltwise.hpp"
23 #include "api/CPP/reorder.hpp"
24 #include <api/CPP/topology.hpp>
25 #include <api/CPP/network.hpp>
26 #include <api/CPP/engine.hpp>
27 #include "test_utils/test_utils.h"
28 #include <api/CPP/data.hpp>
31 #include <gmock/gmock.h>
34 using namespace cldnn;
35 using namespace tests;
36 using namespace testing;
38 TEST(fused_conv_eltwise, basic_0)
40 const auto& engine = get_test_engine();
42 auto input = memory::allocate(engine, { data_types::f32, format::bfyx, { 1, 1, 4, 5 } });
43 auto weights = memory::allocate(engine, { data_types::f32, format::bfyx, { 1, 1, 1, 1 } });
46 1.0f, 2.0f, -15.f, 3.0f, 4.0f, -15.f, 5.0f, 6.0f, -15.f, 7.0f,
47 -15.f, 0.0f, 0.0f, -15.f, 0.5f, -0.5f, -15.f, 8.0f, 1.5f, 5.2f
51 input_layout("input", input.get_layout()),
52 data("weights", weights),
53 convolution("conv", "input", { "weights" }),
54 eltwise("eltwise", "input", "conv", eltwise_mode::sum),
55 reorder("out", "eltwise", format::bfyx, data_types::f32));
58 opt.set_option(build_option::optimize_data(true));
59 network network(engine, topology, opt);
60 network.set_input_data("input", input);
62 auto outputs = network.execute();
63 EXPECT_EQ(outputs.size(), size_t(1));
64 EXPECT_EQ(outputs.begin()->first, "out");
66 auto output = outputs.begin()->second.get_memory();
67 auto&& out_layout = output.get_layout();
69 EXPECT_EQ(out_layout.format, format::bfyx);
70 EXPECT_EQ(out_layout.size.batch[0], 1);
71 EXPECT_EQ(out_layout.size.feature[0], 1);
72 EXPECT_EQ(out_layout.size.spatial[0], 4);
73 EXPECT_EQ(out_layout.size.spatial[1], 5);
77 TEST(fused_conv_eltwise, dont_fuse_if_conv_elt_are_outputs)
79 const auto& engine = get_test_engine();
81 auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 1, 4, 5 } });
82 auto weights = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 1, 1, 1 } });
85 1.0f, 2.0f, -15.f, 3.0f, 4.0f, -15.f, 5.0f, 6.0f, -15.f, 7.0f,
86 -15.f, 0.0f, 0.0f, -15.f, 0.5f, -0.5f, -15.f, 8.0f, 1.5f, 5.2f
90 input_layout("input", input.get_layout()),
91 data("weights", weights),
92 convolution("conv", "input", { "weights" }),
93 eltwise("out", "input", "conv", eltwise_mode::sum));
96 opt.set_option(build_option::optimize_data(true));
97 network network(engine, topology, opt);
98 network.set_input_data("input", input);
100 auto outputs = network.execute();
101 EXPECT_EQ(outputs.size(), size_t(1));
102 EXPECT_EQ(outputs.begin()->first, "out");
104 auto output = outputs.begin()->second.get_memory();
105 auto&& out_layout = output.get_layout();
107 EXPECT_EQ(out_layout.format, format::bfyx);
108 EXPECT_EQ(out_layout.size.batch[0], 1);
109 EXPECT_EQ(out_layout.size.feature[0], 1);
110 EXPECT_EQ(out_layout.size.spatial[0], 4);
111 EXPECT_EQ(out_layout.size.spatial[1], 5);