core_fusion.cpp 29 KB
Newer Older
1
//*****************************************************************************
2
// Copyright 2017-2019 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 19 20 21 22 23 24 25 26 27 28

#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"
29
#include "ngraph/op/fused/group_conv.hpp"
30
#include "ngraph/op/relu.hpp"
31 32
#include "ngraph/op/reshape.hpp"
#include "ngraph/op/softmax.hpp"
33
#include "ngraph/pass/batch_fusion.hpp"
34
#include "ngraph/pass/core_fusion.hpp"
35 36 37 38
#include "ngraph/pass/graph_rewrite.hpp"
#include "ngraph/pass/manager.hpp"
#include "ngraph/pattern/matcher.hpp"
#include "ngraph/pattern/op/label.hpp"
39
#include "ngraph/pattern/op/skip.hpp"
40 41
#include "ngraph/serializer.hpp"
#include "ngraph/util.hpp"
42
#include "util/all_close.hpp"
43
#include "util/autodiff/backprop_function.hpp"
44
#include "util/matcher.hpp"
45
#include "util/random.hpp"
46 47 48 49 50 51 52 53 54 55 56 57 58 59
#include "util/test_tools.hpp"

using namespace ngraph;
using namespace std;

TEST(core_fusion, core_fusion_pass_basic)
{
    auto shape_a = Shape{1, 5};
    auto A = op::Constant::create(element::f32, shape_a, {0, 0, 0, 0, 0});
    auto B = make_shared<op::Parameter>(element::f32, shape_a);
    auto max = make_shared<op::Maximum>(A, B);
    auto graph = make_shared<op::Abs>(max);
    pass::Manager pass_manager;
    pass_manager.register_pass<pass::CoreFusion>();
60
    auto func = make_shared<Function>(graph, ParameterVector{B});
61
    pass_manager.run_passes(func);
62
    ASSERT_NE(std::dynamic_pointer_cast<op::Relu>(graph->get_argument(0)), nullptr);
63
}
64

65
#ifndef NGRAPH_JSON_DISABLE
66 67 68 69 70 71 72 73 74 75 76 77 78
TEST(core_fusion, sigmoid_fprop_fusion)
{
    pass::Manager pass_manager;
    pass_manager.register_pass<pass::CoreFusion>();
    const string json_path = file_util::path_join(SERIALIZED_ZOO, "mxnet/Graph_fprop_sigmoid.json");
    const string json_string = file_util::read_file_to_string(json_path);
    stringstream ss(json_string);
    shared_ptr<Function> func = ngraph::deserialize(ss);
    pass_manager.run_passes(func);
    size_t ccg = count_ops_of_type<op::Sigmoid>(func);
    ASSERT_EQ(ccg, 1);
}

79 80 81 82 83 84 85 86 87 88 89 90 91 92
TEST(core_fusion, sigmoid_bprop_fusion)
{
    const string json_path = file_util::path_join(SERIALIZED_ZOO, "mxnet/Graph_fprop_sigmoid.json");
    const string json_string = file_util::read_file_to_string(json_path);
    stringstream ss(json_string);
    shared_ptr<Function> func = ngraph::deserialize(ss);
    auto df = autodiff::backprop_function(func);
    auto backend = runtime::Backend::create("CPU");
    backend->compile(df);
    size_t ccg = count_ops_of_type<op::SigmoidBackprop>(df);
    ASSERT_EQ(ccg, 1);
}
#endif

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
TEST(core_fusion, sigmoid_fprop_fusion_no_broadcast)
{
    auto make_function = []() {
        auto input = std::make_shared<op::Parameter>(element::f32, Shape{3, 4});
        auto neg_input = std::make_shared<op::Negative>(input);
        auto exp_neg_input = std::make_shared<op::Exp>(neg_input);

        auto constant =
            op::Constant::create(element::f32, Shape{3, 4}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});

        auto add_exp = std::make_shared<op::Add>(exp_neg_input, constant);
        auto divide_1_over_exp = std::make_shared<op::Divide>(constant, add_exp);
        return make_shared<Function>(NodeVector{divide_1_over_exp}, ParameterVector{input});
    };
    auto func = make_function();

    // Check fusion happens
    pass::Manager pass_manager;
    pass_manager.register_pass<pass::CoreFusion>();
    pass_manager.run_passes(func);
    size_t ccg = count_ops_of_type<op::Sigmoid>(func);
    ASSERT_EQ(ccg, 1);
}

TEST(core_fusion, sigmoid_fprop_fusion_no_broadcast2)
{
    auto make_function = []() {
        auto input = std::make_shared<op::Parameter>(element::f32, Shape{3, 4});
        auto neg_input = std::make_shared<op::Negative>(input);
        auto exp_neg_input = std::make_shared<op::Exp>(neg_input);

        auto constant =
            op::Constant::create(element::f32, Shape{3, 4}, {1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1});

        auto add_exp = std::make_shared<op::Add>(exp_neg_input, constant);
        auto divide_1_over_exp = std::make_shared<op::Divide>(constant, add_exp);
        return make_shared<Function>(NodeVector{divide_1_over_exp}, ParameterVector{input});
    };
    auto func = make_function();

    pass::Manager pass_manager;
    pass_manager.register_pass<pass::CoreFusion>();
    pass_manager.run_passes(func);
    size_t ccg = count_ops_of_type<op::Sigmoid>(func);
    ASSERT_EQ(ccg, 0);
}

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
TEST(core_fusion, reshape_broadcast)
{
    auto generate_func = []() {
        auto input = make_shared<op::Parameter>(element::f32, Shape{10});
        auto reshape1 = make_shared<op::Reshape>(input, AxisVector{0}, Shape{1, 10, 1});
        auto broadcast =
            make_shared<op::Broadcast>(reshape1, Shape{1, 5, 10, 8, 1, 20}, AxisSet{1, 3, 5});
        auto f = make_shared<Function>(broadcast, ParameterVector{input});
        return f;
    };

    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;
    pass_manager.register_pass<pass::CoreFusion>();
    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)));
}

TEST(core_fusion, reshape_broadcast_graph_optimized)
{
    auto input = make_shared<op::Parameter>(element::f32, Shape{10});
    auto reshape1 = make_shared<op::Reshape>(input, AxisVector{0}, Shape{1, 10, 1});
    auto broadcast =
        make_shared<op::Broadcast>(reshape1, Shape{1, 5, 10, 8, 1, 20}, AxisSet{1, 3, 5});
    auto optimized_f = make_shared<Function>(broadcast, ParameterVector{input});

    pass::Manager pass_manager;
    pass_manager.register_pass<pass::CoreFusion>();
    pass_manager.run_passes(optimized_f);

    auto new_broadcast =
        std::dynamic_pointer_cast<op::Broadcast>(optimized_f->get_results().at(0)->get_argument(0));
    EXPECT_EQ(new_broadcast->get_argument(0), input);
    EXPECT_EQ(new_broadcast->get_broadcast_axes(), (AxisSet{0, 1, 3, 4, 5}));
}

TEST(core_fusion, reshape_broadcast_adds_one)
{
    auto input = make_shared<op::Parameter>(element::f32, Shape{10});
    auto reshape1 = make_shared<op::Reshape>(input, AxisVector{0}, Shape{1, 10, 1});
    auto broadcast =
        make_shared<op::Broadcast>(reshape1, Shape{1, 5, 10, 8, 1, 20, 1}, AxisSet{1, 3, 5, 6});
    auto optimized_f = make_shared<Function>(broadcast, ParameterVector{input});

    pass::Manager pass_manager;
    pass_manager.register_pass<pass::CoreFusion>();
    pass_manager.run_passes(optimized_f);

    auto new_broadcast =
        std::dynamic_pointer_cast<op::Broadcast>(optimized_f->get_results().at(0)->get_argument(0));
    EXPECT_EQ(new_broadcast, broadcast);
    EXPECT_EQ(new_broadcast->get_argument(0), reshape1);
}

TEST(core_fusion, reshape_broadcast_wrong_reshape)
{
    auto input = make_shared<op::Parameter>(element::f32, Shape{10});
    auto reshape1 = make_shared<op::Reshape>(input, AxisVector{0}, Shape{1, 5, 2});
    auto broadcast =
        make_shared<op::Broadcast>(reshape1, Shape{1, 5, 5, 8, 2, 20}, AxisSet{1, 3, 5});
    auto optimized_f = make_shared<Function>(broadcast, ParameterVector{input});

    pass::Manager pass_manager;
    pass_manager.register_pass<pass::CoreFusion>();
    pass_manager.run_passes(optimized_f);

    auto new_broadcast =
        std::dynamic_pointer_cast<op::Broadcast>(optimized_f->get_results().at(0)->get_argument(0));
    EXPECT_EQ(new_broadcast, broadcast);
    EXPECT_EQ(new_broadcast->get_argument(0), reshape1);
}

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
TEST(core_fusion, sparsity_opt_56x56)
{
    Shape win_size_3{1, 1, 3, 3};
    Shape win_size_1{1, 1, 1, 1};
    Strides stride_2{2, 2};
    Strides stride_1{1, 1};
    CoordinateDiff pad_0{0, 0};
    CoordinateDiff pad_1{1, 1};
    auto data_stride3 = std::make_shared<op::Parameter>(element::f32, Shape{1, 64, 56, 56});
    auto weights_stride3 = std::make_shared<op::Parameter>(element::f32, Shape{64, 64, 3, 3});

    auto conv_stride3 = std::make_shared<op::Convolution>(
        data_stride3, weights_stride3, stride_1, stride_1, pad_1, pad_1);
    auto param_broadcast_w3 = std::make_shared<op::Parameter>(element::f32, Shape{64});
    auto broadcast_w3 =
        std::make_shared<op::Broadcast>(param_broadcast_w3, Shape{1, 64, 56, 56}, AxisSet{0, 2, 3});
    auto add_w3 = std::make_shared<op::Add>(conv_stride3, broadcast_w3);
    auto relu_w3 = std::make_shared<op::Relu>(add_w3);
    ///
    auto weights_stride1 = std::make_shared<op::Parameter>(element::f32, Shape{256, 64, 1, 1});
    auto conv_stride1 = std::make_shared<op::Convolution>(relu_w3, weights_stride1);
    auto param_broadcast_w1 = std::make_shared<op::Parameter>(element::f32, Shape{256});
    auto broadcast_w1 = std::make_shared<op::Broadcast>(
        param_broadcast_w1, Shape{1, 256, 56, 56}, AxisSet{0, 2, 3});
    auto add_w1 = std::make_shared<op::Add>(conv_stride1, broadcast_w1);
    ////
    auto other_arg = std::make_shared<op::Parameter>(element::f32, Shape{1, 256, 56, 56});
    auto add_two_convs = std::make_shared<op::Add>(add_w1, other_arg);
    auto relu_two_convs = std::make_shared<op::Relu>(add_two_convs);
    ///
    auto weights_conv_s2 = std::make_shared<op::Parameter>(element::f32, Shape{512, 256, 1, 1});
    auto conv_s2_1 = std::make_shared<op::Convolution>(relu_two_convs, weights_conv_s2, stride_2);
    auto conv_s2_2 = std::make_shared<op::Convolution>(relu_two_convs, weights_conv_s2, stride_2);

    pass::Manager pass_manager;
    pass_manager.register_pass<pass::CoreFusion>();
261 262 263 264 265 266 267
    auto params = ParameterVector{data_stride3,
                                  weights_stride3,
                                  param_broadcast_w3,
                                  weights_stride1,
                                  param_broadcast_w1,
                                  other_arg,
                                  weights_conv_s2};
268 269 270 271 272 273 274 275 276 277 278 279
    auto func = make_shared<Function>(NodeVector{conv_s2_1, conv_s2_2}, params);
    pass_manager.run_passes(func);
    auto results = func->get_results();
    auto t_eltwise_conv1 =
        std::dynamic_pointer_cast<op::Convolution>(results.at(0)->get_argument(0));
    auto t_eltwise_conv2 =
        std::dynamic_pointer_cast<op::Convolution>(results.at(1)->get_argument(0));
    ASSERT_TRUE(t_eltwise_conv1);
    ASSERT_TRUE(t_eltwise_conv2);
    ASSERT_EQ(t_eltwise_conv1->get_window_movement_strides(), stride_1);
    ASSERT_EQ(t_eltwise_conv2->get_window_movement_strides(), stride_1);
}
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315

static std::shared_ptr<Function> generate_reshape_softmax_reshape()
{
    Shape shape_nchw{10, 20, 30, 40};
    Shape shape_nhwc{10, 30, 40, 20};
    AxisVector to_nhwc{0, 2, 3, 1};
    AxisVector to_nchw{0, 3, 1, 2};
    auto input = make_shared<op::Parameter>(element::f32, shape_nchw);
    auto reshape1 = make_shared<op::Reshape>(input, to_nhwc, shape_nhwc);
    auto softmax = make_shared<op::Softmax>(reshape1, AxisSet{1, 2, 3});
    auto reshape2 = make_shared<op::Reshape>(softmax, to_nchw, shape_nchw);
    auto f = make_shared<Function>(reshape2, ParameterVector{input});
    return f;
}

TEST(core_fusion, reshape_softmax_reshape)
{
    auto baseline_f = generate_reshape_softmax_reshape();
    auto optimized_f = generate_reshape_softmax_reshape();
    auto baseline_input = baseline_f->get_parameters().at(0);

    pass::Manager pass_manager;
    pass_manager.register_pass<pass::CoreFusion>();
    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->get_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)));
}
316

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
TEST(core_fusion, zero_padded_reshaped_conv)
{
    auto X = make_shared<op::Parameter>(element::f32, Shape{1, 2, 2, 1});
    auto F = make_shared<op::Parameter>(element::f32, Shape{1, 1, 1, 1});

    auto pad_value = op::Constant::create<float>(element::f32, Shape{}, std::vector<float>{0.0f});

    auto pad =
        make_shared<op::Pad>(X, pad_value, CoordinateDiff{0, 1, 0, 0}, CoordinateDiff{0, 0, 1, 0});

    auto reshape = make_shared<op::Reshape>(pad, AxisVector{0, 3, 1, 2}, Shape{1, 1, 3, 3});

    auto conv = make_shared<op::Convolution>(reshape,
                                             F,
                                             Strides{1, 1},
                                             Strides{1, 1},
                                             CoordinateDiff{0, 0},
                                             CoordinateDiff{0, 0},
                                             Strides{1, 1});

    auto func = make_shared<Function>(conv, ParameterVector{X, F});

    ASSERT_EQ(count_ops_of_type<op::Pad>(func), 1);

    auto backend = runtime::Backend::create("CPU");
    backend->compile(func);

    ASSERT_EQ(count_ops_of_type<op::Pad>(func), 0);
}

TEST(core_fusion, zero_padded_conv)
{
    auto X = make_shared<op::Parameter>(element::f32, Shape{1, 1, 2, 2});
    auto F = make_shared<op::Parameter>(element::f32, Shape{1, 1, 1, 1});

    auto pad_value = op::Constant::create<float>(element::f32, Shape{}, std::vector<float>{0.0f});

    auto pad =
        make_shared<op::Pad>(X, pad_value, CoordinateDiff{0, 0, 0, 1}, CoordinateDiff{0, 0, 1, 0});

    auto conv = make_shared<op::Convolution>(pad,
                                             F,
                                             Strides{1, 1},
                                             Strides{1, 1},
                                             CoordinateDiff{0, 0},
                                             CoordinateDiff{0, 0},
                                             Strides{1, 1});

    auto func = make_shared<Function>(conv, ParameterVector{X, F});

    ASSERT_EQ(count_ops_of_type<op::Pad>(func), 1);

    auto backend = runtime::Backend::create("CPU");
    backend->compile(func);

    ASSERT_EQ(count_ops_of_type<op::Pad>(func), 0);
}

TEST(core_fusion, non_zero_padded_conv)
{
    auto X = make_shared<op::Parameter>(element::f32, Shape{1, 1, 2, 2});
    auto F = make_shared<op::Parameter>(element::f32, Shape{1, 1, 1, 1});

    auto pad_value = op::Constant::create<float>(element::f32, Shape{}, std::vector<float>{1.0f});

    auto pad =
        make_shared<op::Pad>(X, pad_value, CoordinateDiff{0, 0, 0, 1}, CoordinateDiff{0, 0, 1, 0});

    auto conv = make_shared<op::Convolution>(pad,
                                             F,
                                             Strides{1, 1},
                                             Strides{1, 1},
                                             CoordinateDiff{0, 0},
                                             CoordinateDiff{0, 0},
                                             Strides{1, 1});

    auto func = make_shared<Function>(conv, ParameterVector{X, F});

    ASSERT_EQ(count_ops_of_type<op::Pad>(func), 1);

    auto backend = runtime::Backend::create("CPU");
    backend->compile(func);

    ASSERT_EQ(count_ops_of_type<op::Pad>(func), 1);
}

TEST(core_fusion, zero_padded_conv_backprop_filters)
{
    auto X = make_shared<op::Parameter>(element::f32, Shape{1, 1, 2, 2});
    auto F = make_shared<op::Parameter>(element::f32, Shape{1, 1, 2, 2});

    auto pad_value = op::Constant::create<float>(element::f32, Shape{}, std::vector<float>{0.0f});

    auto pad =
        make_shared<op::Pad>(X, pad_value, CoordinateDiff{0, 0, 0, 1}, CoordinateDiff{0, 0, 1, 0});

    auto conv = make_shared<op::ConvolutionBackpropFilters>(pad,
                                                            Shape{1, 1, 2, 2},
                                                            F,
                                                            Strides{1, 1},
                                                            Strides{1, 1},
                                                            CoordinateDiff{0, 0},
                                                            CoordinateDiff{0, 0},
                                                            Strides{1, 1});

    auto func = make_shared<Function>(conv, ParameterVector{X, F});

    ASSERT_EQ(count_ops_of_type<op::Pad>(func), 1);

    auto backend = runtime::Backend::create("CPU");
    backend->compile(func);

    ASSERT_EQ(count_ops_of_type<op::Pad>(func), 0);
}

432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
TEST(core_fusion, conv_bias)
{
    auto gen_f = [](bool with_fused_op) {
        auto data = make_shared<op::Parameter>(element::f32, Shape{2, 3, 4, 5});
        auto weights = make_shared<op::Parameter>(element::f32, Shape{4, 3, 2, 2});
        auto bias = make_shared<op::Parameter>(element::f32, Shape{4});
        if (with_fused_op)
        {
            return make_shared<Function>(make_shared<op::ConvolutionBias>(data, weights, bias),
                                         ParameterVector{data, weights, bias});
        }
        else
        {
            auto conv = make_shared<op::Convolution>(data, weights);
            auto conv_bias =
                conv + make_shared<op::Broadcast>(bias, conv->get_shape(), AxisSet{0, 2, 3});
            return make_shared<Function>(conv_bias, ParameterVector{data, weights, bias});
449 450 451 452 453 454 455 456
        }
    };

    auto fused_f = gen_f(true);
    auto decomp_f1 = gen_f(false);
    auto decomp_f2 = gen_f(false);

    pass::Manager pass_manager;
457
    pass_manager.register_pass<pass::CoreFusion>(ngraph::pass::FusionType::ALL_FUSIONS);
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
    pass_manager.run_passes(decomp_f1);
    ASSERT_EQ(count_ops_of_type<op::ConvolutionBias>(decomp_f1), 1);

    test::Uniform<float> rng(0.0f, 1.0f);
    vector<vector<float>> args;

    for (shared_ptr<op::Parameter> param : fused_f->get_parameters())
    {
        vector<float> tensor_val(shape_size(param->get_shape()));
        rng.initialize(tensor_val);
        args.push_back(tensor_val);
    }
    auto fused_r = execute(fused_f, args, "INTERPRETER");
    auto decomp_r1 = execute(decomp_f1, args, "INTERPRETER");
    auto decomp_r2 = execute(decomp_f2, args, "INTERPRETER");

    for (size_t i = 0; i < fused_r.size(); i++)
    {
        EXPECT_TRUE(test::all_close(fused_r.at(i), decomp_r1.at(i)));
        EXPECT_TRUE(test::all_close(fused_r.at(i), decomp_r2.at(i)));
    }
}

TEST(core_fusion, conv_bias_bcast_reshape)
{
    // PaddlePaddle pattern
    auto gen_f = [](bool with_fused_op) {
        auto data = make_shared<op::Parameter>(element::f32, Shape{2, 3, 4, 5});
        auto weights = make_shared<op::Parameter>(element::f32, Shape{4, 3, 2, 2});
        auto bias = make_shared<op::Parameter>(element::f32, Shape{4});
        if (with_fused_op)
        {
            return make_shared<Function>(make_shared<op::ConvolutionBias>(data, weights, bias),
                                         ParameterVector{data, weights, bias});
        }
        else
        {
            auto conv = make_shared<op::Convolution>(data, weights);
            auto bias_bcast = make_shared<op::Broadcast>(bias, Shape{2, 4, 12}, AxisSet{0, 2});
            auto conv_bias =
                conv + make_shared<op::Reshape>(bias_bcast, AxisVector{0, 1, 2}, conv->get_shape());
            return make_shared<Function>(conv_bias, ParameterVector{data, weights, bias});
500 501 502 503 504 505 506 507
        }
    };

    auto fused_f = gen_f(true);
    auto decomp_f1 = gen_f(false);
    auto decomp_f2 = gen_f(false);

    pass::Manager pass_manager;
508
    pass_manager.register_pass<pass::CoreFusion>(ngraph::pass::FusionType::ALL_FUSIONS);
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
    pass_manager.run_passes(decomp_f1);
    ASSERT_EQ(count_ops_of_type<op::ConvolutionBias>(decomp_f1), 1);

    test::Uniform<float> rng(0.0f, 1.0f);
    vector<vector<float>> args;

    for (shared_ptr<op::Parameter> param : fused_f->get_parameters())
    {
        vector<float> tensor_val(shape_size(param->get_shape()));
        rng.initialize(tensor_val);
        args.push_back(tensor_val);
    }
    auto fused_r = execute(fused_f, args, "INTERPRETER");
    auto decomp_r1 = execute(decomp_f1, args, "INTERPRETER");
    auto decomp_r2 = execute(decomp_f2, args, "INTERPRETER");

    for (size_t i = 0; i < fused_r.size(); i++)
    {
        EXPECT_TRUE(test::all_close(fused_r.at(i), decomp_r1.at(i)));
        EXPECT_TRUE(test::all_close(fused_r.at(i), decomp_r2.at(i)));
    }
}

TEST(core_fusion, conv_bias_add)
{
    auto gen_f = [](bool with_fused_op) {
        auto data = make_shared<op::Parameter>(element::f32, Shape{2, 3, 4, 5});
        auto weights = make_shared<op::Parameter>(element::f32, Shape{4, 3, 2, 2});
        auto bias = make_shared<op::Parameter>(element::f32, Shape{4});
        auto add = make_shared<op::Parameter>(element::f32, Shape{2, 4, 3, 4});
        if (with_fused_op)
        {
            auto conv_bias = make_shared<op::ConvolutionBias>(data, weights, bias);
            return make_shared<Function>(make_shared<op::ConvolutionBiasAdd>(conv_bias, add),
                                         ParameterVector{data, weights, bias, add});
        }
        else
        {
            auto conv = make_shared<op::Convolution>(data, weights);
            auto conv_bias =
                conv + make_shared<op::Broadcast>(bias, conv->get_shape(), AxisSet{0, 2, 3});
            return make_shared<Function>(conv_bias + add,
                                         ParameterVector{data, weights, bias, add});
        }
    };

    auto fused_f = gen_f(true);
    auto decomp_f1 = gen_f(false);
    auto decomp_f2 = gen_f(false);

    pass::Manager pass_manager;
560
    pass_manager.register_pass<pass::CoreFusion>(ngraph::pass::FusionType::ALL_FUSIONS);
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
    pass_manager.run_passes(decomp_f1);
    ASSERT_EQ(count_ops_of_type<op::ConvolutionBiasAdd>(decomp_f1), 1);

    test::Uniform<float> rng(0.0f, 1.0f);
    vector<vector<float>> args;

    for (shared_ptr<op::Parameter> param : fused_f->get_parameters())
    {
        vector<float> tensor_val(shape_size(param->get_shape()));
        rng.initialize(tensor_val);
        args.push_back(tensor_val);
    }
    auto fused_r = execute(fused_f, args, "INTERPRETER");
    auto decomp_r1 = execute(decomp_f1, args, "INTERPRETER");
    auto decomp_r2 = execute(decomp_f2, args, "INTERPRETER");

    for (size_t i = 0; i < fused_r.size(); i++)
    {
        EXPECT_TRUE(test::all_close(fused_r.at(i), decomp_r1.at(i)));
        EXPECT_TRUE(test::all_close(fused_r.at(i), decomp_r2.at(i)));
    }
}

// TODO: Enable once fusion is moved to core
TEST(core_fusion, DISABLED_conv_bias_bprop)
{
    auto gen_f = [](bool with_fused_op) {
        auto data = make_shared<op::Parameter>(element::f32, Shape{2, 3, 4, 5});
        auto weights = make_shared<op::Parameter>(element::f32, Shape{4, 3, 2, 2});
        auto bias = make_shared<op::Parameter>(element::f32, Shape{4});
        auto delta = make_shared<op::Parameter>(element::f32, Shape{2, 4, 3, 4});
        if (with_fused_op)
        {
            auto conv_bprop =
                make_shared<op::ConvolutionBiasBackpropFiltersBias>(data,
                                                                    weights->get_shape(),
                                                                    bias->get_shape(),
                                                                    delta,
                                                                    Strides{1, 1},
                                                                    Strides{1, 1},
                                                                    CoordinateDiff{0, 0},
                                                                    CoordinateDiff{0, 0},
                                                                    Strides{1, 1});
            auto goe0 = make_shared<op::GetOutputElement>(conv_bprop, 0);
            auto goe1 = make_shared<op::GetOutputElement>(conv_bprop, 1);
            return make_shared<Function>(NodeVector{goe0, goe1}, ParameterVector{data, delta});
        }
        else
        {
            auto conv_bprop = make_shared<op::ConvolutionBackpropFilters>(data,
                                                                          weights->get_shape(),
                                                                          delta,
                                                                          Strides{1, 1},
                                                                          Strides{1, 1},
                                                                          CoordinateDiff{0, 0},
                                                                          CoordinateDiff{0, 0},
                                                                          Strides{1, 1});
            auto bias_bprop = make_shared<op::Sum>(delta, AxisSet{0, 2, 3});
            return make_shared<Function>(NodeVector{conv_bprop, bias_bprop},
                                         ParameterVector{data, delta});
        }
    };

    auto fused_f = gen_f(true);
    auto decomp_f1 = gen_f(false);
    auto decomp_f2 = gen_f(false);

    pass::Manager pass_manager;
629
    pass_manager.register_pass<pass::CoreFusion>(ngraph::pass::FusionType::ALL_FUSIONS);
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
    pass_manager.run_passes(decomp_f1);
    ASSERT_EQ(count_ops_of_type<op::ConvolutionBiasBackpropFiltersBias>(decomp_f1), 1);

    test::Uniform<float> rng(0.0f, 1.0f);
    vector<vector<float>> args;

    for (shared_ptr<op::Parameter> param : fused_f->get_parameters())
    {
        vector<float> tensor_val(shape_size(param->get_shape()));
        rng.initialize(tensor_val);
        args.push_back(tensor_val);
    }
    auto fused_r = execute(fused_f, args, "INTERPRETER");
    auto decomp_r1 = execute(decomp_f1, args, "INTERPRETER");
    auto decomp_r2 = execute(decomp_f2, args, "INTERPRETER");

    for (size_t i = 0; i < fused_r.size(); i++)
    {
        EXPECT_TRUE(test::all_close(fused_r.at(i), decomp_r1.at(i)));
        EXPECT_TRUE(test::all_close(fused_r.at(i), decomp_r2.at(i)));
    }
}
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693

TEST(batch_fusion, group_convolution_fusion)
{
    Shape shape_a{1, 32, 2, 2};
    auto A = make_shared<op::Parameter>(element::f32, shape_a);
    Shape shape_b{2, 16, 1, 1};
    auto B = make_shared<op::Parameter>(element::f32, shape_b);
    Shape shape_r{1, 2, 2, 2};

    auto a_slice0 = std::make_shared<op::Slice>(A, Coordinate{0, 0, 0, 0}, Coordinate{1, 16, 2, 2});
    auto a_slice1 =
        std::make_shared<op::Slice>(A, Coordinate{0, 16, 0, 0}, Coordinate{1, 32, 2, 2});

    auto b_slice0 = std::make_shared<op::Slice>(B, Coordinate{0, 0, 0, 0}, Coordinate{1, 16, 1, 1});
    auto b_slice1 = std::make_shared<op::Slice>(B, Coordinate{1, 0, 0, 0}, Coordinate{2, 16, 1, 1});

    auto conv_lower = make_shared<op::Convolution>(a_slice0,
                                                   b_slice0,
                                                   Strides{1, 1},
                                                   Strides{1, 1},
                                                   CoordinateDiff{0, 0},
                                                   CoordinateDiff{0, 0},
                                                   Strides{1, 1});

    auto conv_upper = make_shared<op::Convolution>(a_slice1,
                                                   b_slice1,
                                                   Strides{1, 1},
                                                   Strides{1, 1},
                                                   CoordinateDiff{0, 0},
                                                   CoordinateDiff{0, 0},
                                                   Strides{1, 1});

    auto concat = make_shared<op::Concat>(NodeVector{conv_lower, conv_upper}, 1);

    auto f = make_shared<Function>(NodeVector{concat}, ParameterVector{A, B});
    pass::Manager pass_manager;
    pass_manager.register_pass<pass::BatchFusion>();
    pass_manager.run_passes(f);
    auto gc =
        std::dynamic_pointer_cast<op::GroupConvolution>(f->get_results().at(0)->get_argument(0));
    ASSERT_TRUE(gc);
}
694 695 696 697 698 699 700 701 702 703 704 705 706 707

TEST(core_fusion, pass_property)
{
    auto pass = std::make_shared<ngraph::pass::CoreFusion>();
    ASSERT_EQ(false, pass->get_property(pass::PassProperty::REQUIRE_STATIC_SHAPE));
    ASSERT_EQ(false, pass->get_property(pass::PassProperty::CHANGE_DYNAMIC_STATE));
}

TEST(batch_fusion, pass_property)
{
    auto pass = std::make_shared<ngraph::pass::BatchFusion>();
    ASSERT_EQ(true, pass->get_property(pass::PassProperty::REQUIRE_STATIC_SHAPE));
    ASSERT_EQ(false, pass->get_property(pass::PassProperty::CHANGE_DYNAMIC_STATE));
}