summaryrefslogtreecommitdiff
path: root/inference-engine/thirdparty/mkl-dnn/src/cpu/cpu_concat.hpp
blob: 477566baedcbb63ce7874a85f539b6ce1ff16417 (plain)
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
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
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
/*******************************************************************************
* Copyright 2016-2018 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.
*******************************************************************************/

#ifndef CPU_CONCAT_HPP
#define CPU_CONCAT_HPP

#include <assert.h>

#include "c_types_map.hpp"
#include "memory_pd.hpp"
#include "type_helpers.hpp"
#include "utils.hpp"
#include "cpu_memory.hpp"
#include "cpu_primitive.hpp"

namespace mkldnn {
namespace impl {
namespace cpu {

#define DECLARE_CPU_CONCAT_PD_t(impl_name, ...) \
    static status_t create(concat_pd_t **concat_pd, \
            const memory_desc_t *output_d, int n, int concat_dim, \
            const memory_pd_t **input_pds, const primitive_attr_t *attr) { \
        auto _pd = new pd_t(output_d, n, concat_dim, \
                (const cpu_memory_pd_t **)input_pds, attr); \
        if (_pd == nullptr) return out_of_memory; \
        if (_pd->init() != success) { delete _pd; return unimplemented; } \
        return safe_ptr_assign<concat_pd_t>(*concat_pd, _pd); \
    } \
    virtual status_t create_primitive(primitive_t **primitive, \
            const primitive_at_t *inputs, \
            const primitive_t **outputs) const override { \
        double ms = get_msec(); \
        primitive_t::input_vector ins(inputs, inputs + n_); \
        primitive_t::output_vector outs(outputs, outputs + 1); \
        auto ret = safe_ptr_assign<primitive_t>(*primitive, \
                new (__VA_ARGS__)(this, ins, outs)); \
        ms = get_msec() - ms; \
        if (mkldnn_verbose()->level >= 2) { \
            printf("mkldnn_verbose,create,%s,%g\n", this->info(), ms); \
            fflush(0); \
        } \
        return ret; \
    } \
    virtual pd_t *clone() const override { return nullptr; } \
    virtual const char *name() const override { return impl_name; }
#define DECLARE_CPU_CONCAT_PD_T(impl_name, ...) \
    DECLARE_CPU_CONCAT_PD_t(impl_name, __VA_ARGS__)

struct cpu_concat_pd_t: public concat_pd_t {
    using cpu_memory_pd_t = cpu_memory_t::pd_t;

    cpu_concat_pd_t(const memory_desc_t *output_d, int n,
            int concat_dim, const cpu_memory_pd_t **input_pds,
            const primitive_attr_t *attr)
        : concat_pd_t(input_pds[0]->engine(), n, concat_dim, attr),
        dst_pd_(input_pds[0]->engine()) {
            for (int i = 0; i < n_; ++i)
                src_pds_.push_back(*input_pds[i]); /* make a copy */
            dst_pd_ = cpu_memory_pd_t(input_pds[0]->engine(), output_d);
        }
    cpu_concat_pd_t(const cpu_concat_pd_t &rhs)
        : concat_pd_t(rhs), src_pds_(rhs.src_pds_)
        , src_image_pds_(rhs.src_image_pds_)
        , dst_pd_(rhs.dst_pd_) {}

    virtual ~cpu_concat_pd_t() {}

    virtual const cpu_memory_pd_t *src_pd(int index = 0) const override
    { return index < this->n_ ? &src_pds_[index] : nullptr; }
    virtual const cpu_memory_pd_t *src_image_pd(int index = 0) const
    { return index < this->n_ ? &src_image_pds_[index] : nullptr; }
    virtual const cpu_memory_pd_t *dst_pd(int index = 0) const override
    { return index == 0 ? &dst_pd_ : nullptr; }

protected:
    nstl::vector<cpu_memory_pd_t> src_pds_;
    nstl::vector<cpu_memory_pd_t> src_image_pds_;
    cpu_memory_pd_t dst_pd_;

    virtual status_t init() {
        bool ok = true
            && set_default_params() == success
            && attr()->has_default_values();
        if (!ok) return unimplemented;

        for (int i = 0; i < n_; ++i) {
            const memory_desc_wrapper i_d(&src_pds_[i]);
            if (i_d.is_wino_desc() || i_d.is_additional_buffer())
                return unimplemented;
        }

        const int ndims = dst_pd_.desc()->ndims;
        int current_concat_dim_offset = 0;
        for (int i = 0; i < n_; ++i) {
            const int dim = src_pds_[i].desc()->dims[concat_dim_];
            dims_t dims, offsets = {};
            utils::array_copy(dims, dst_pd_.desc()->dims, ndims);
            dims[concat_dim_] = dim;
            offsets[concat_dim_] = current_concat_dim_offset;

            cpu_view_t::pd_t v_pd(src_pds_[i].engine());
            status_t status = v_pd.init(&dst_pd_, dims, offsets);
            if (status != success) return status;
            src_image_pds_.push_back(*v_pd.dst_pd());
            current_concat_dim_offset += dim;
        }

        return success;
    }

    virtual status_t set_default_params() {
        if (dst_pd_.desc()->format != memory_format::any)
            return status::success;

        const int ndims = dst_pd_.desc()->ndims;
        const auto fallback_dst_fmt = types::flat_memory_format(ndims);

        /* the stupidest ever heuristics */
        memory_format_t desired_dst_fmt = dst_pd_.desc()->format;
        for (int i = 0; i < n_; ++i)
            desired_dst_fmt = nstl::max(desired_dst_fmt,
                    src_pds_[i].desc()->format);

        /* try to create dst with the desired format */
        status_t status = dst_pd_.set_format(desired_dst_fmt);
        if (status != status::success) {
            /* if fail use fallback flat layout */
            return dst_pd_.set_format(fallback_dst_fmt);
        }

        /* check if we can create view for the dst with the desired format */
        bool desired_format_ok = true;
        int current_concat_dim_offset = 0;
        for (int i = 0; i < n_; ++i) {
            const int dim = src_pds_[i].desc()->dims[concat_dim_];
            dims_t dims, offsets = {};
            utils::array_copy(dims, dst_pd_.desc()->dims, ndims);
            dims[concat_dim_] = dim;
            offsets[concat_dim_] = current_concat_dim_offset;

            cpu_view_t::pd_t v_pd(src_pds_[i].engine());
            if (v_pd.init(&dst_pd_, dims, offsets) != success) {
                desired_format_ok = false;
                break;
            }
            current_concat_dim_offset += dim;
        }

        if (!desired_format_ok) {
            /* if fail use fallback flat layout */
            return dst_pd_.set_format(fallback_dst_fmt);
        }

        return status::success;
    }
};

}
}
}

#endif

// vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s