rewrite_pooling.cpp 1.39 KB
Newer Older
1
2
3
4
#include <migraphx/rewrite_pooling.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/op/pooling.hpp>
Paul's avatar
Paul committed
5
#include <migraphx/op/reshape.hpp>
6
7
8
9
10
11
12
13
14
15
#include <migraphx/op/reduce_mean.hpp>
#include <migraphx/program.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

void rewrite_pooling::apply(program& prog) const
{
    for(auto ins : iterator_for(prog))
    {
Paul's avatar
Paul committed
16
        if(ins->name() != "pooling")
17
            continue;
Paul's avatar
Paul committed
18
        if(ins->get_shape().lens().size() != 4)
19
            continue;
Paul's avatar
Paul committed
20
        if(ins->inputs().empty())
21
            continue;
Paul's avatar
Paul committed
22
        auto&& s  = ins->inputs().front()->get_shape();
23
        auto&& op = any_cast<op::pooling>(ins->get_operator());
Paul's avatar
Paul committed
24
        if(op.mode != "average")
25
            continue;
Paul's avatar
Paul committed
26
        if(op.padding[0] != 0 and op.padding[1] != 0)
27
            continue;
Paul's avatar
Paul committed
28
        if(op.stride[0] != 1 and op.stride[1] != 1)
29
            continue;
Paul's avatar
Paul committed
30
        if(s.lens()[2] != op.lengths[0] and s.lens()[3] != op.lengths[1])
31
            continue;
Paul's avatar
Paul committed
32
33
34
35
36
        std::int64_t n = s.lens()[0];
        std::int64_t c = s.lens()[1];
        auto reshape = prog.insert_instruction(ins, op::reshape{{n*c, -1}}, ins->inputs().front());
        auto pooling = prog.insert_instruction(ins, op::reduce_mean{{1}}, reshape);
        prog.replace_instruction(ins, op::reshape{{n, c, 1, 1}}, pooling);
37
38
39
40
41
    }
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx