control_dependencies.cpp 8.63 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 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

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

#include "gtest/gtest.h"
#include "ngraph/autodiff/adjoints.hpp"
#include "ngraph/file_util.hpp"
#include "ngraph/graph_util.hpp"
#include "ngraph/log.hpp"
#include "ngraph/ngraph.hpp"
#include "ngraph/op/batch_norm.hpp"
#include "ngraph/op/get_output_element.hpp"
#include "ngraph/op/parameter.hpp"
#include "ngraph/pass/manager.hpp"
#include "ngraph/pass/visualize_tree.hpp"
#include "ngraph/pattern/matcher.hpp"
#include "ngraph/serializer.hpp"
#include "ngraph/util.hpp"
#include "util/all_close.hpp"
#include "util/autodiff/backprop_function.hpp"
#include "util/autodiff/numeric_compare.hpp"
#include "util/ndarray.hpp"
#include "util/random.hpp"
#include "util/test_tools.hpp"

using namespace ngraph;
using namespace std;

class ControlDependencyOp : public ngraph::op::Op
{
public:
50 51
    static constexpr NodeTypeInfo type_info{"ControlDependencyOp", 0};
    const NodeTypeInfo& get_type_info() const override { return type_info; }
52 53 54
    virtual std::shared_ptr<Node> copy_with_new_args(const NodeVector& new_args) const override
    {
        auto clone = make_shared<ControlDependencyOp>(new_args, std::set<std::shared_ptr<Node>>{});
55
        return move(clone);
56 57 58 59 60 61 62 63 64 65
    }

    ControlDependencyOp(const NodeVector& args, const std::set<std::shared_ptr<Node>>& deps)
        : Op("ControlDependencyOp", args)
    {
        if (args.size() == 0 && deps.size() == 0)
        {
            throw ngraph_error("Expected some arguments or dependencies");
        }

66
        for (auto& node : deps)
67
        {
68
            add_control_dependency(node);
69 70 71 72 73 74 75 76 77 78 79 80 81
        }

        if (args.size() != 0)
        {
            set_output_type(0, args.at(0)->get_element_type(), args.at(0)->get_shape());
        }
        else
        {
            auto dn = *(deps.begin());
            set_output_type(0, dn->get_element_type(), dn->get_shape());
        }
    }
};
82
constexpr NodeTypeInfo ControlDependencyOp::type_info;
83 84 85 86 87 88 89 90 91

TEST(control_dependencies, cdep_ops)
{
    auto A = make_shared<op::Parameter>(element::f32, Shape{});
    auto B = make_shared<op::Parameter>(element::f32, Shape{});
    auto absn = make_shared<op::Abs>(A);
    auto cdop =
        make_shared<ControlDependencyOp>(NodeVector{A}, std::set<std::shared_ptr<Node>>{absn});

92
    auto f = make_shared<Function>(cdop, ParameterVector{A, B});
93
    test_ordered_ops(f, NodeVector{absn});
94 95 96 97 98 99 100 101 102 103 104 105
}

TEST(control_dependencies, two_cdep_ops)
{
    auto A = make_shared<op::Parameter>(element::f32, Shape{});
    auto B = make_shared<op::Parameter>(element::f32, Shape{});
    auto absn = make_shared<op::Abs>(A);
    auto C = make_shared<op::Parameter>(element::f32, Shape{});
    auto absn_c = make_shared<op::Abs>(C);
    auto cdop = make_shared<ControlDependencyOp>(NodeVector{A},
                                                 std::set<std::shared_ptr<Node>>{absn, absn_c});

106
    auto f = make_shared<Function>(cdop, ParameterVector{A, B, C});
107
    test_ordered_ops(f, NodeVector{absn, absn_c});
108 109 110 111 112 113 114 115 116 117 118 119
}

TEST(control_dependencies, two_cdep_ops_op_on_top)
{
    auto A = make_shared<op::Parameter>(element::f32, Shape{});
    auto absn = make_shared<op::Abs>(A);
    auto B = make_shared<op::Parameter>(element::f32, Shape{});
    auto absn_b = make_shared<op::Abs>(B);
    auto cdop = make_shared<ControlDependencyOp>(NodeVector{A},
                                                 std::set<std::shared_ptr<Node>>{absn, absn_b});
    auto absn_cdop = make_shared<op::Abs>(cdop);

120
    auto f = make_shared<Function>(absn_cdop, ParameterVector{A, B});
121
    test_ordered_ops(f, NodeVector{absn, absn_b});
122 123 124 125 126 127 128 129 130
}

TEST(control_dependencies, clone_function_cdop)
{
    auto A = make_shared<op::Parameter>(element::f32, Shape{});
    auto absn = make_shared<op::Abs>(A);
    auto cdop =
        make_shared<ControlDependencyOp>(NodeVector{A}, std::set<std::shared_ptr<Node>>{absn});

131
    auto f = make_shared<Function>(cdop, ParameterVector{A});
132
    test_ordered_ops(f, NodeVector{absn});
133
    auto clone = ngraph::clone_function(*f.get());
134
    auto matcher = std::make_shared<pattern::Matcher>(cdop);
135 136 137 138 139
    auto cdop_clone = clone->get_results().at(0)->get_argument(0);
    ASSERT_TRUE(matcher->match(cdop_clone));
    auto cloned_deps = cdop_clone->get_control_dependencies();
    ASSERT_EQ(cloned_deps.size(), 1);
    auto cloned_abs = *begin(cloned_deps);
140
    ASSERT_TRUE(is_type<op::Abs>(cloned_abs));
141 142 143 144 145 146 147 148 149 150 151 152
}

TEST(control_dependencies, clone_function_cdop_abs)
{
    auto A = make_shared<op::Parameter>(element::f32, Shape{});
    auto absn = make_shared<op::Abs>(A);
    auto B = make_shared<op::Parameter>(element::f32, Shape{});
    auto absn_b = make_shared<op::Abs>(B);
    auto cdop = make_shared<ControlDependencyOp>(NodeVector{A},
                                                 std::set<std::shared_ptr<Node>>{absn, absn_b});
    auto absn_cdop = make_shared<op::Abs>(cdop);

153
    auto f = make_shared<Function>(absn_cdop, ParameterVector{A, B});
154
    auto clone = ngraph::clone_function(*f.get());
155
    auto matcher = std::make_shared<pattern::Matcher>(cdop);
156 157 158 159 160 161
    auto cdop_clone = clone->get_results().at(0)->get_argument(0)->get_argument(0);
    ASSERT_TRUE(matcher->match(cdop_clone));
    auto cloned_deps = cdop_clone->get_control_dependencies();
    ASSERT_EQ(cloned_deps.size(), 2);
    for (auto ccdep : cloned_deps)
    {
162
        ASSERT_TRUE(is_type<op::Abs>(ccdep));
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
static size_t count_control_dependencies(const shared_ptr<Node>& node,
                                         const shared_ptr<Node>& dependency)
{
    auto& dependencies = node->get_control_dependencies();
    return count(dependencies.begin(), dependencies.end(), dependency);
}

TEST(control_dependencies, replace_node)
{
    Shape shape{2, 2};
    auto A = make_shared<op::Parameter>(element::f32, shape);
    auto B = make_shared<op::Parameter>(element::f32, shape);
    auto MUL_AB = A * B;
    auto MUL_BA = B * A;
    auto ADD = A + B;
    auto SUM = MUL_AB + ADD;
    ADD->add_control_dependency(MUL_AB);
    ASSERT_TRUE(1 == count_control_dependencies(ADD, MUL_AB));
    ASSERT_TRUE(0 == count_control_dependencies(ADD, MUL_BA));
    replace_node(MUL_AB, MUL_BA);
    ASSERT_TRUE(0 == count_control_dependencies(ADD, MUL_AB));
    ASSERT_TRUE(1 == count_control_dependencies(ADD, MUL_BA));
}

190
#ifndef NGRAPH_JSON_DISABLE
191 192 193 194 195 196
TEST(control_dependencies, serialize_cdop)
{
    auto A = make_shared<op::Parameter>(element::f32, Shape{});
    auto absn = make_shared<op::Abs>(A);
    auto cdop = make_shared<op::Negative>(A);
    cdop->add_control_dependency(absn);
197
    auto f = make_shared<Function>(cdop, ParameterVector{A});
198 199 200 201

    string js = serialize(f, 4);
    shared_ptr<Function> clone = deserialize(js);

202
    auto matcher = std::make_shared<pattern::Matcher>(cdop);
203 204 205 206 207
    auto cdop_clone = clone->get_results().at(0)->get_argument(0);
    ASSERT_TRUE(matcher->match(cdop_clone));
    auto cloned_deps = cdop_clone->get_control_dependencies();
    ASSERT_EQ(cloned_deps.size(), 1);
    auto cloned_abs = *begin(cloned_deps);
208
    ASSERT_TRUE(is_type<op::Abs>(cloned_abs));
209 210 211 212 213 214 215 216 217 218 219 220 221
}

TEST(control_dependencies, serialize_cdop_abs)
{
    auto A = make_shared<op::Parameter>(element::f32, Shape{});
    auto absn = make_shared<op::Abs>(A);
    auto B = make_shared<op::Parameter>(element::f32, Shape{});
    auto absn_b = make_shared<op::Abs>(B);
    auto cdop = make_shared<op::Negative>(A);
    cdop->add_control_dependency(absn);
    cdop->add_control_dependency(absn_b);
    auto absn_cdop = make_shared<op::Abs>(cdop);

222
    auto f = make_shared<Function>(absn_cdop, ParameterVector{A, B});
223 224 225

    string js = serialize(f, 4);
    shared_ptr<Function> clone = deserialize(js);
226
    auto matcher = std::make_shared<pattern::Matcher>(cdop);
227 228 229 230 231 232
    auto cdop_clone = clone->get_results().at(0)->get_argument(0)->get_argument(0);
    ASSERT_TRUE(matcher->match(cdop_clone));
    auto cloned_deps = cdop_clone->get_control_dependencies();
    ASSERT_EQ(cloned_deps.size(), 2);
    for (auto ccdep : cloned_deps)
    {
233
        ASSERT_TRUE(is_type<op::Abs>(ccdep));
234 235
    }
}
236
#endif