concat_fusion.cpp 12.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
//*****************************************************************************
// Copyright 2017-2019 Intel Corporation
//
// 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.
//*****************************************************************************

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <list>
#include <memory>

#include "gtest/gtest.h"

#include "ngraph/file_util.hpp"
#include "ngraph/graph_util.hpp"
#include "ngraph/log.hpp"
#include "ngraph/ngraph.hpp"
#include "ngraph/op/broadcast.hpp"
#include "ngraph/op/concat.hpp"
#include "ngraph/op/parameter.hpp"
#include "ngraph/op/reshape.hpp"
#include "ngraph/pass/concat_fusion.hpp"
#include "ngraph/pass/graph_rewrite.hpp"
#include "ngraph/pass/manager.hpp"
#include "ngraph/pass/visualize_tree.hpp"
#include "ngraph/pattern/matcher.hpp"
#include "ngraph/pattern/op/label.hpp"
#include "ngraph/pattern/op/skip.hpp"
#include "ngraph/serializer.hpp"
#include "ngraph/util.hpp"
#include "util/all_close.hpp"
#include "util/autodiff/backprop_function.hpp"
#include "util/matcher.hpp"
#include "util/random.hpp"
#include "util/test_tools.hpp"

using namespace ngraph;
using namespace std;

TEST(concat_fusion, single_branch)
{
53
    Shape shape_a{12, 8, 1, 1};
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    auto generate_func = [shape_a]() {
        auto A = make_shared<op::Parameter>(element::f32, shape_a);

        auto concat_1 = make_shared<op::Concat>(NodeVector{A}, 2);
        auto concat_2 = make_shared<op::Concat>(NodeVector{concat_1}, 2);
        auto concat_3 = make_shared<op::Concat>(
            NodeVector{concat_2, concat_2, concat_2, concat_2, concat_2, concat_2, concat_2}, 2);
        auto concat_4 = make_shared<op::Concat>(
            NodeVector{concat_3, concat_3, concat_3, concat_3, concat_3, concat_3, concat_3}, 3);
        auto f_concat_1 = make_shared<Function>(NodeVector{concat_4}, ParameterVector{A});
        return f_concat_1;
    };

    auto baseline_f = generate_func();
    auto optimized_f = generate_func();
    auto baseline_input_shape = baseline_f->get_parameters().at(0)->get_shape();

    pass::Manager pass_manager;
72
    pass_manager.register_pass<pass::VisualizeTree>("before_single_branch.png");
73 74
    pass_manager.register_pass<pass::ConcatElimination>();
    pass_manager.register_pass<pass::SelfConcatFusion>();
75
    pass_manager.register_pass<pass::VisualizeTree>("after_single_branch.png");
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    pass_manager.run_passes(optimized_f);

    test::Uniform<float> rng(0.0f, 100.0f);
    vector<vector<float>> args;
    vector<float> tensor_val(shape_size(baseline_input_shape));
    rng.initialize(tensor_val);
    args.push_back(tensor_val);

    auto baseline_results = execute(baseline_f, args, "INTERPRETER");
    auto optimized_results = execute(optimized_f, args, "INTERPRETER");

    EXPECT_TRUE(test::all_close(baseline_results.at(0), optimized_results.at(0)));
    size_t num_reshapes_optimized = count_ops_of_type<op::Reshape>(optimized_f);
    size_t num_broadcast_optimzed = count_ops_of_type<op::Broadcast>(optimized_f);

    ASSERT_EQ(num_reshapes_optimized, 1);
    ASSERT_EQ(num_broadcast_optimzed, 1);
}

TEST(concat_fusion, multiple_branches_1)
{
97
    Shape shape_a{16, 8, 1, 1};
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    auto generate_func = [shape_a]() {
        auto A = make_shared<op::Parameter>(element::f32, shape_a);

        auto concat_1 = make_shared<op::Concat>(NodeVector{A}, 2);
        auto concat_2 = make_shared<op::Concat>(NodeVector{concat_1}, 2);
        auto concat_3 = make_shared<op::Concat>(
            NodeVector{concat_2, concat_2, concat_2, concat_2, concat_2, concat_2, concat_2}, 2);
        auto concat_4 = make_shared<op::Concat>(
            NodeVector{concat_3, concat_3, concat_3, concat_3, concat_3, concat_3, concat_3}, 3);

        auto concat_5 = make_shared<op::Concat>(NodeVector{A, A}, 2);
        auto concat_6 = make_shared<op::Concat>(NodeVector{concat_5, concat_5, concat_5}, 3);
        auto f_concat_1 = make_shared<Function>(NodeVector{concat_4, concat_6}, ParameterVector{A});
        return f_concat_1;
    };

    auto baseline_f = generate_func();
    auto optimized_f = generate_func();
    auto baseline_input_shape = baseline_f->get_parameters().at(0)->get_shape();

    pass::Manager pass_manager;
119
    pass_manager.register_pass<pass::VisualizeTree>("before_multiple_branches_1.png");
120 121
    pass_manager.register_pass<pass::ConcatElimination>();
    pass_manager.register_pass<pass::SelfConcatFusion>();
122
    pass_manager.register_pass<pass::VisualizeTree>("after_multiple_branches_1.png");
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    pass_manager.run_passes(optimized_f);

    test::Uniform<float> rng(0.0f, 100.0f);
    vector<vector<float>> args;
    vector<float> tensor_val(shape_size(baseline_input_shape));
    rng.initialize(tensor_val);
    args.push_back(tensor_val);

    auto baseline_results = execute(baseline_f, args, "INTERPRETER");
    auto optimized_results = execute(optimized_f, args, "INTERPRETER");

    EXPECT_TRUE(test::all_close(baseline_results.at(0), optimized_results.at(0)));

    size_t num_reshapes_optimized = count_ops_of_type<op::Reshape>(optimized_f);
    size_t num_broadcast_optimzed = count_ops_of_type<op::Broadcast>(optimized_f);

    ASSERT_EQ(num_reshapes_optimized, 2);
    ASSERT_EQ(num_broadcast_optimzed, 2);
}

TEST(concat_fusion, multiple_branches_2)
{
145
    Shape shape_a{16, 8, 1, 1};
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    auto generate_func = [shape_a]() {
        auto A = make_shared<op::Parameter>(element::f32, shape_a);
        auto concat_3 = make_shared<op::Concat>(NodeVector{A, A, A, A, A, A, A}, 2);
        auto concat_4 = make_shared<op::Concat>(
            NodeVector{concat_3, concat_3, concat_3, concat_3, concat_3, concat_3, concat_3}, 3);

        auto concat_6 = make_shared<op::Concat>(NodeVector{A, A, A}, 3);
        auto f_concat_1 = make_shared<Function>(NodeVector{concat_4, concat_6}, ParameterVector{A});
        return f_concat_1;
    };

    auto baseline_f = generate_func();
    auto optimized_f = generate_func();
    auto baseline_input_shape = baseline_f->get_parameters().at(0)->get_shape();

    pass::Manager pass_manager;
162
    pass_manager.register_pass<pass::VisualizeTree>("before_multiple_branches_2.png");
163 164
    pass_manager.register_pass<pass::ConcatElimination>();
    pass_manager.register_pass<pass::SelfConcatFusion>();
165
    pass_manager.register_pass<pass::VisualizeTree>("after_multiple_branches_2.png");
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    pass_manager.run_passes(optimized_f);

    test::Uniform<float> rng(0.0f, 100.0f);
    vector<vector<float>> args;
    vector<float> tensor_val(shape_size(baseline_input_shape));
    rng.initialize(tensor_val);
    args.push_back(tensor_val);

    auto baseline_results = execute(baseline_f, args, "INTERPRETER");
    auto optimized_results = execute(optimized_f, args, "INTERPRETER");

    EXPECT_TRUE(test::all_close(baseline_results.at(0), optimized_results.at(0)));

    size_t num_reshapes_optimized = count_ops_of_type<op::Reshape>(optimized_f);
    size_t num_broadcast_optimzed = count_ops_of_type<op::Broadcast>(optimized_f);

    ASSERT_EQ(num_reshapes_optimized, 1);
    ASSERT_EQ(num_broadcast_optimzed, 1);
}

TEST(concat_fusion, non_fusable_self_concat)
{
188 189
    Shape shape_a{32, 1, 1, 1};
    Shape shape_b{32, 1, 1};
190 191 192 193 194 195 196 197 198 199 200 201
    auto generate_func = [shape_a, shape_b]() {
        auto A = make_shared<op::Parameter>(element::f32, shape_a);
        auto B = make_shared<op::Parameter>(element::f32, shape_b);

        auto concat_1 = make_shared<op::Concat>(NodeVector{A, A, A, A}, 1);
        auto concat_2 = make_shared<op::Concat>(
            NodeVector{concat_1, concat_1, concat_1, concat_1, concat_1, concat_1, concat_1}, 2);
        auto concat_3 = make_shared<op::Concat>(NodeVector{concat_2, concat_2}, 1);
        auto concat_4 = make_shared<op::Concat>(NodeVector{concat_3, concat_3, concat_3}, 3);

        auto concat_5 = make_shared<op::Concat>(NodeVector{B, B, B, B, B, B, B}, 1);
        auto concat_6 = make_shared<op::Concat>(NodeVector{concat_5, concat_5, concat_5}, 2);
202
        auto broadcast = make_shared<op::Broadcast>(concat_6, Shape{32, 8, 7, 3}, AxisSet{1});
203 204 205 206 207 208 209 210 211 212 213
        auto add = make_shared<op::Add>(concat_4, broadcast);
        auto f_concat_1 = make_shared<Function>(NodeVector{add}, ParameterVector{A, B});
        return f_concat_1;
    };

    auto baseline_f = generate_func();
    auto optimized_f = generate_func();
    auto baseline_input_shape_1 = baseline_f->get_parameters().at(0)->get_shape();
    auto baseline_input_shape_2 = baseline_f->get_parameters().at(1)->get_shape();

    pass::Manager pass_manager;
214
    pass_manager.register_pass<pass::VisualizeTree>("before_non_fusable_self_concat.png");
215 216
    pass_manager.register_pass<pass::ConcatElimination>();
    pass_manager.register_pass<pass::SelfConcatFusion>();
217
    pass_manager.register_pass<pass::VisualizeTree>("after_non_fusable_self_concat.png");
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    pass_manager.run_passes(optimized_f);

    test::Uniform<float> rng(0.0f, 100.0f);
    vector<vector<float>> args;
    vector<float> tensor_val_1(shape_size(baseline_input_shape_1));
    vector<float> tensor_val_2(shape_size(baseline_input_shape_2));
    rng.initialize(tensor_val_1);
    rng.initialize(tensor_val_2);
    args.push_back(tensor_val_1);
    args.push_back(tensor_val_2);

    auto baseline_results = execute(baseline_f, args, "INTERPRETER");
    auto optimized_results = execute(optimized_f, args, "INTERPRETER");

    EXPECT_TRUE(test::all_close(baseline_results.at(0), optimized_results.at(0)));

    size_t num_reshapes_optimized = count_ops_of_type<op::Reshape>(optimized_f);
    size_t num_broadcast_optimzed = count_ops_of_type<op::Broadcast>(optimized_f);

    ASSERT_EQ(num_reshapes_optimized, 2);
    ASSERT_EQ(num_broadcast_optimzed, 3);
}

TEST(concat_fusion, self_concat_with_fan_out)
{
    Shape shape_a{8, 1, 1, 1};
    Shape shape_b{8, 4, 1, 1};
    auto generate_func = [shape_a, shape_b]() {
        auto A = make_shared<op::Parameter>(element::f32, shape_a);
        auto B = make_shared<op::Parameter>(element::f32, shape_b);

        auto concat_1 = make_shared<op::Concat>(NodeVector{A, A, A, A, A, A, A}, 2);
        auto concat_2 =
            make_shared<op::Concat>(NodeVector{concat_1, concat_1, concat_1, concat_1}, 1);
        auto concat_3 =
            make_shared<op::Concat>(NodeVector{concat_2, concat_2, concat_2, concat_2}, 3);

        auto concat_4 = make_shared<op::Concat>(NodeVector{B, B, B, B, B, B, B}, 2);
        auto concat_5 = make_shared<op::Concat>(NodeVector{concat_4, concat_4, concat_4}, 3);
        auto concat_6 = make_shared<op::Concat>(NodeVector{concat_2, concat_4}, 3);
        auto f_concat_1 =
            make_shared<Function>(NodeVector{concat_3, concat_6}, ParameterVector{A, B});
        return f_concat_1;
    };

    auto baseline_f = generate_func();
    auto optimized_f = generate_func();
    auto baseline_input_shape_1 = baseline_f->get_parameters().at(0)->get_shape();
    auto baseline_input_shape_2 = baseline_f->get_parameters().at(1)->get_shape();

    pass::Manager pass_manager;
269
    pass_manager.register_pass<pass::VisualizeTree>("before_self_concat_with_fan_out.png");
270 271
    pass_manager.register_pass<pass::ConcatElimination>();
    pass_manager.register_pass<pass::SelfConcatFusion>();
272
    pass_manager.register_pass<pass::VisualizeTree>("after_self_concat_with_fan_out.png");
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    pass_manager.run_passes(optimized_f);

    test::Uniform<float> rng(0.0f, 100.0f);
    vector<vector<float>> args;
    vector<float> tensor_val_1(shape_size(baseline_input_shape_1));
    vector<float> tensor_val_2(shape_size(baseline_input_shape_2));
    rng.initialize(tensor_val_1);
    rng.initialize(tensor_val_2);
    args.push_back(tensor_val_1);
    args.push_back(tensor_val_2);

    auto baseline_results = execute(baseline_f, args, "INTERPRETER");
    auto optimized_results = execute(optimized_f, args, "INTERPRETER");

    EXPECT_TRUE(test::all_close(baseline_results.at(0), optimized_results.at(0)));

    size_t num_reshapes_optimized = count_ops_of_type<op::Reshape>(optimized_f);
    size_t num_broadcast_optimzed = count_ops_of_type<op::Broadcast>(optimized_f);

    ASSERT_EQ(num_reshapes_optimized, 1);
    ASSERT_EQ(num_broadcast_optimzed, 1);
}