rewrite_pooling.cpp 1.11 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <migraphx/rewrite_pooling.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/op/pooling.hpp>
#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
15
        if(ins->name() != "pooling")
16
            continue;
Paul's avatar
Paul committed
17
        if(ins->get_shape().lens().size() != 4)
18
            continue;
Paul's avatar
Paul committed
19
        if(ins->inputs().empty())
20
            continue;
Paul's avatar
Paul committed
21
        auto&& s  = ins->inputs().front()->get_shape();
22
        auto&& op = any_cast<op::pooling>(ins->get_operator());
Paul's avatar
Paul committed
23
        if(op.mode != "average")
24
            continue;
Paul's avatar
Paul committed
25
        if(op.padding[0] != 0 and op.padding[1] != 0)
26
            continue;
Paul's avatar
Paul committed
27
        if(op.stride[0] != 1 and op.stride[1] != 1)
28
            continue;
Paul's avatar
Paul committed
29
        if(s.lens()[2] != op.lengths[0] and s.lens()[3] != op.lengths[1])
30
31
32
33
34
35
36
            continue;
        prog.replace_instruction(ins, op::reduce_mean{{2, 3}}, ins->inputs().front());
    }
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx