dyn_elimination.cpp 10.6 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
//*****************************************************************************
// 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 "ngraph/pass/dyn_elimination.hpp"
#include "gtest/gtest.h"
#include "ngraph/ngraph.hpp"
#include "ngraph/pass/manager.hpp"
#include "util/all_close_f.hpp"
#include "util/test_tools.hpp"

using namespace ngraph;
using namespace std;

TEST(dyn_elimination, transpose)
{
    Shape shape_in{2, 4, 6, 8};
    auto param = make_shared<op::Parameter>(element::boolean, shape_in);

    auto constant_perm =
        make_shared<op::Constant>(element::i64, Shape{4}, vector<int64_t>{2, 3, 1, 0});

    auto transpose = make_shared<op::Transpose>(param, constant_perm);

    auto f = make_shared<Function>(transpose, ParameterVector{param});

    pass::Manager pass_manager;
    pass_manager.register_pass<pass::DynElimination>();
    pass_manager.run_passes(f);

    ASSERT_EQ(count_ops_of_type<op::Transpose>(f), 0);
    ASSERT_EQ(count_ops_of_type<op::Reshape>(f), 1);

    auto new_reshape =
        std::dynamic_pointer_cast<op::Reshape>(f->get_results().at(0)->get_argument(0));
    ASSERT_TRUE(new_reshape);

    ASSERT_EQ(new_reshape->get_input_order(), (AxisVector{2, 3, 1, 0}));
    ASSERT_EQ(new_reshape->output(0).get_shape(), (Shape{6, 8, 4, 2}));
    ASSERT_EQ(new_reshape->get_output_element_type(0), element::boolean);
}

// For now, we can't handle the case where the input has dynamic shapes,
// because the classic Reshape op demands a Shape. Probably won't be able to
// deal with this until/unless we make a "StaticTranspose". Just make sure
// we don't crash or mangle the graph.
TEST(dyn_elimination, transpose_dyn_shape)
{
    PartialShape shape_in{2, 4, Dimension::dynamic(), 8};

    auto param = make_shared<op::Parameter>(element::boolean, shape_in);

    auto constant_perm =
        make_shared<op::Constant>(element::i64, Shape{4}, vector<int64_t>{2, 3, 1, 0});

    auto transpose = make_shared<op::Transpose>(param, constant_perm);

    auto f = make_shared<Function>(transpose, ParameterVector{param});

    pass::Manager pass_manager;
    pass_manager.register_pass<pass::DynElimination>();
    pass_manager.run_passes(f);

    ASSERT_EQ(count_ops_of_type<op::Transpose>(f), 1);
    ASSERT_EQ(count_ops_of_type<op::Constant>(f), 1);

    auto new_transpose =
        std::dynamic_pointer_cast<op::Transpose>(f->get_results().at(0)->get_argument(0));
    ASSERT_TRUE(new_transpose);

    ASSERT_EQ(new_transpose->get_output_element_type(0), element::boolean);
    ASSERT_TRUE(new_transpose->get_output_partial_shape(0).relaxes(
        PartialShape{Dimension::dynamic(), 8, 4, 2}));
}
87 88 89 90 91 92 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

TEST(dyn_elimination, slice)
{
    // input has shape [2,4,6,8,2,2,2]
    // slice in numpy syntax is [0:,:4,2:6:2,7:3:-2,np.newaxis,...,1]
    // shape should be [2,4,2,2,1,2,2] (so sayeth numpy!)
    Shape shape_in{2, 4, 6, 8, 2, 2, 2};
    auto input = make_shared<op::Parameter>(element::f32, shape_in);
    auto constant_lb =
        make_shared<op::Constant>(element::i64, Shape{7}, vector<int64_t>{0, 3, 2, 7, 0, 0, 1});
    auto constant_ub =
        make_shared<op::Constant>(element::i64, Shape{7}, vector<int64_t>{0, 4, 6, 3, 0, 0, 0});
    auto constant_strides =
        make_shared<op::Constant>(element::i64, Shape{7}, vector<int64_t>{1, 1, 2, -2, 0, 0, 0});
    AxisSet lower_bounds_mask{1};
    AxisSet upper_bounds_mask{0};
    AxisSet new_axis_mask{4};
    AxisSet shrink_mask{6};
    AxisSet ellipsis_mask{5};

    auto sl = make_shared<op::DynSlice>(input,
                                        constant_lb,
                                        constant_ub,
                                        constant_strides,
                                        lower_bounds_mask,
                                        upper_bounds_mask,
                                        new_axis_mask,
                                        shrink_mask,
                                        ellipsis_mask);

    ASSERT_EQ(sl->get_element_type(), element::f32);
    ASSERT_EQ(sl->get_shape(), (Shape{2, 4, 2, 2, 1, 2, 2}));

    auto f = make_shared<Function>(sl, ParameterVector{input});

    pass::Manager pass_manager;
    pass_manager.register_pass<pass::DynElimination>();
    pass_manager.run_passes(f);

    ASSERT_EQ(count_ops_of_type<op::DynSlice>(f), 0);
    ASSERT_EQ(count_ops_of_type<op::Slice>(f), 1);
    ASSERT_EQ(count_ops_of_type<op::Reshape>(f), 1);
    ASSERT_EQ(count_ops_of_type<op::Reverse>(f), 1);

    ASSERT_EQ(f->get_results().at(0)->get_element_type(), element::f32);
    ASSERT_EQ(f->get_results().at(0)->get_shape(), (Shape{2, 4, 2, 2, 1, 2, 2}));
}
134 135 136 137 138 139 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

TEST(dyn_elimination, replace_slice)
{
    // input has shape [2,4,6,8,2,2,2]
    // slice in numpy syntax is [0:,:4,2:6:2,7:3:-2,np.newaxis,...,1]
    // slice shape should be [2,4,2,2,1,2,2] (so sayeth numpy!)
    Shape shape_in{2, 4, 6, 8, 2, 2, 2};
    Shape shape_slice{2, 4, 2, 2, 1, 2, 2};
    auto input = make_shared<op::Parameter>(element::f32, shape_in);
    auto replacement = make_shared<op::Parameter>(element::f32, shape_slice);
    auto constant_lb =
        make_shared<op::Constant>(element::i64, Shape{7}, vector<int64_t>{0, 3, 2, 7, 0, 0, 1});
    auto constant_ub =
        make_shared<op::Constant>(element::i64, Shape{7}, vector<int64_t>{0, 4, 6, 3, 0, 0, 0});
    auto constant_strides =
        make_shared<op::Constant>(element::i64, Shape{7}, vector<int64_t>{1, 1, 2, -2, 0, 0, 0});
    AxisSet lower_bounds_mask{1};
    AxisSet upper_bounds_mask{0};
    AxisSet new_axis_mask{4};
    AxisSet shrink_mask{6};
    AxisSet ellipsis_mask{5};

    auto rsl = make_shared<op::DynReplaceSlice>(input,
                                                replacement,
                                                constant_lb,
                                                constant_ub,
                                                constant_strides,
                                                lower_bounds_mask,
                                                upper_bounds_mask,
                                                new_axis_mask,
                                                shrink_mask,
                                                ellipsis_mask);

    ASSERT_EQ(rsl->get_element_type(), element::f32);
    ASSERT_EQ(rsl->get_shape(), (Shape{2, 4, 6, 8, 2, 2, 2}));

    auto f = make_shared<Function>(rsl, ParameterVector{input, replacement});

    pass::Manager pass_manager;
    pass_manager.register_pass<pass::DynElimination>();
    pass_manager.run_passes(f);

    ASSERT_EQ(count_ops_of_type<op::DynReplaceSlice>(f), 0);
    ASSERT_EQ(count_ops_of_type<op::ReplaceSlice>(f), 1);
    ASSERT_EQ(count_ops_of_type<op::Reshape>(f), 1);
    ASSERT_EQ(count_ops_of_type<op::Reverse>(f), 1);

    ASSERT_EQ(f->get_results().at(0)->get_element_type(), element::f32);
    ASSERT_EQ(f->get_results().at(0)->get_shape(), (Shape{2, 4, 6, 8, 2, 2, 2}));
}
184

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
TEST(dyn_elimination, reshape)
{
    auto input_arg = make_shared<op::Parameter>(element::f32, Shape{2, 4, 6, 8});
    auto shape_arg = make_shared<op::Constant>(element::i64, Shape{3}, vector<int64_t>{0, 6, -1});

    auto r = make_shared<op::DynReshape>(input_arg, shape_arg, true);

    ASSERT_EQ(r->get_element_type(), element::f32);
    ASSERT_EQ(r->get_shape(), (Shape{2, 6, 32}));

    auto f = make_shared<Function>(r, ParameterVector{input_arg});

    pass::Manager pass_manager;
    pass_manager.register_pass<pass::DynElimination>();
    pass_manager.run_passes(f);

    ASSERT_EQ(count_ops_of_type<op::DynReshape>(f), 0);
    ASSERT_EQ(count_ops_of_type<op::Constant>(f), 0);
    ASSERT_EQ(count_ops_of_type<op::Reshape>(f), 1);

    ASSERT_EQ(f->get_results().at(0)->get_element_type(), element::f32);
    ASSERT_EQ(f->get_results().at(0)->get_shape(), (Shape{2, 6, 32}));
}
208

209 210 211 212 213 214 215 216 217 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 269 270
TEST(dyn_elimination, range)
{
    auto constant_start = make_shared<op::Constant>(element::i64, Shape{}, vector<int64_t>{0});
    auto constant_stop = make_shared<op::Constant>(element::i64, Shape{}, vector<int64_t>{5});
    auto constant_step = make_shared<op::Constant>(element::i64, Shape{}, vector<int64_t>{2});

    auto range = make_shared<op::Range>(constant_start, constant_stop, constant_step);

    ASSERT_EQ(range->get_element_type(), element::i64);
    ASSERT_EQ(range->get_shape(), (Shape{3}));

    auto f = make_shared<Function>(range, ParameterVector{});

    pass::Manager pass_manager;
    pass_manager.register_pass<pass::DynElimination>();
    pass_manager.run_passes(f);

    ASSERT_EQ(count_ops_of_type<op::Range>(f), 0);
    ASSERT_EQ(count_ops_of_type<op::Constant>(f), 1);

    auto replacement = dynamic_pointer_cast<op::Constant>(f->get_results().at(0)->get_argument(0));

    ASSERT_NE(replacement, nullptr);
    ASSERT_EQ(replacement->get_element_type(), element::i64);
    ASSERT_EQ(replacement->get_shape(), (Shape{3}));

    auto vals = replacement->get_vector<int64_t>();

    ASSERT_EQ(vals, (vector<int64_t>{0, 2, 4}));
}

TEST(dyn_elimination, range_f64)
{
    auto constant_start = make_shared<op::Constant>(element::f64, Shape{}, vector<double>{-0.5});
    auto constant_stop = make_shared<op::Constant>(element::f64, Shape{}, vector<double>{2});
    auto constant_step = make_shared<op::Constant>(element::f64, Shape{}, vector<double>{0.25});

    auto range = make_shared<op::Range>(constant_start, constant_stop, constant_step);

    ASSERT_EQ(range->get_element_type(), element::f64);
    ASSERT_EQ(range->get_shape(), (Shape{10}));

    auto f = make_shared<Function>(range, ParameterVector{});

    pass::Manager pass_manager;
    pass_manager.register_pass<pass::DynElimination>();
    pass_manager.run_passes(f);

    ASSERT_EQ(count_ops_of_type<op::Range>(f), 0);
    ASSERT_EQ(count_ops_of_type<op::Constant>(f), 1);

    auto replacement = dynamic_pointer_cast<op::Constant>(f->get_results().at(0)->get_argument(0));

    ASSERT_NE(replacement, nullptr);
    ASSERT_EQ(replacement->get_element_type(), element::f64);
    ASSERT_EQ(replacement->get_shape(), (Shape{10}));

    auto vals = replacement->get_vector<double>();

    ASSERT_TRUE(test::all_close_f(
        vals, vector<double>{-0.5, -0.25, 0, 0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75}));
}