1 /*******************************************************************************
2 * Copyright 2016-2018 Intel Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
17 #ifndef CPU_CONCAT_HPP
18 #define CPU_CONCAT_HPP
22 #include "c_types_map.hpp"
23 #include "memory_pd.hpp"
24 #include "type_helpers.hpp"
26 #include "cpu_memory.hpp"
27 #include "cpu_primitive.hpp"
33 #define DECLARE_CPU_CONCAT_PD_t(impl_name, ...) \
34 static status_t create(concat_pd_t **concat_pd, \
35 const memory_desc_t *output_d, int n, int concat_dim, \
36 const memory_pd_t **input_pds, const primitive_attr_t *attr) { \
37 auto _pd = new pd_t(output_d, n, concat_dim, \
38 (const cpu_memory_pd_t **)input_pds, attr); \
39 if (_pd == nullptr) return out_of_memory; \
40 if (_pd->init() != success) { delete _pd; return unimplemented; } \
41 return safe_ptr_assign<concat_pd_t>(*concat_pd, _pd); \
43 virtual status_t create_primitive(primitive_t **primitive, \
44 const primitive_at_t *inputs, \
45 const primitive_t **outputs) const override { \
46 double ms = get_msec(); \
47 primitive_t::input_vector ins(inputs, inputs + n_); \
48 primitive_t::output_vector outs(outputs, outputs + 1); \
49 auto ret = safe_ptr_assign<primitive_t>(*primitive, \
50 new (__VA_ARGS__)(this, ins, outs)); \
51 ms = get_msec() - ms; \
52 if (mkldnn_verbose()->level >= 2) { \
53 printf("mkldnn_verbose,create,%s,%g\n", this->info(), ms); \
58 virtual pd_t *clone() const override { return new pd_t(*this); } \
59 virtual const char *name() const override { return impl_name; }
60 #define DECLARE_CPU_CONCAT_PD_T(impl_name, ...) \
61 DECLARE_CPU_CONCAT_PD_t(impl_name, __VA_ARGS__)
63 struct cpu_concat_pd_t: public concat_pd_t {
64 using cpu_memory_pd_t = cpu_memory_t::pd_t;
66 cpu_concat_pd_t(const memory_desc_t *output_d, int n,
67 int concat_dim, const cpu_memory_pd_t **input_pds,
68 const primitive_attr_t *attr)
69 : concat_pd_t(input_pds[0]->engine(), n, concat_dim, attr),
70 dst_pd_(input_pds[0]->engine()) {
71 for (int i = 0; i < n_; ++i)
72 src_pds_.push_back(*input_pds[i]); /* make a copy */
73 dst_pd_ = cpu_memory_pd_t(input_pds[0]->engine(), output_d);
75 cpu_concat_pd_t(const cpu_concat_pd_t &rhs)
76 : concat_pd_t(rhs), src_pds_(rhs.src_pds_)
77 , src_image_pds_(rhs.src_image_pds_)
78 , dst_pd_(rhs.dst_pd_) {}
80 virtual ~cpu_concat_pd_t() {}
82 virtual const cpu_memory_pd_t *src_pd(int index = 0) const override
83 { return index < this->n_ ? &src_pds_[index] : nullptr; }
84 virtual const cpu_memory_pd_t *src_image_pd(int index = 0) const
85 { return index < this->n_ ? &src_image_pds_[index] : nullptr; }
86 virtual const cpu_memory_pd_t *dst_pd(int index = 0) const override
87 { return index == 0 ? &dst_pd_ : nullptr; }
90 nstl::vector<cpu_memory_pd_t> src_pds_;
91 nstl::vector<cpu_memory_pd_t> src_image_pds_;
92 cpu_memory_pd_t dst_pd_;
94 virtual status_t init() {
96 && set_default_params() == success
97 && attr()->has_default_values();
98 if (!ok) return unimplemented;
100 for (int i = 0; i < n_; ++i) {
101 const memory_desc_wrapper i_d(&src_pds_[i]);
102 if (i_d.is_wino_desc() || i_d.is_additional_buffer())
103 return unimplemented;
106 const int ndims = dst_pd_.desc()->ndims;
107 int current_concat_dim_offset = 0;
108 for (int i = 0; i < n_; ++i) {
109 const int dim = src_pds_[i].desc()->dims[concat_dim_];
110 dims_t dims, offsets = {};
111 utils::array_copy(dims, dst_pd_.desc()->dims, ndims);
112 dims[concat_dim_] = dim;
113 offsets[concat_dim_] = current_concat_dim_offset;
115 cpu_view_t::pd_t v_pd(src_pds_[i].engine());
116 status_t status = v_pd.init(&dst_pd_, dims, offsets);
117 if (status != success) return status;
118 src_image_pds_.push_back(*v_pd.dst_pd());
119 current_concat_dim_offset += dim;
125 virtual status_t set_default_params() {
126 if (dst_pd_.desc()->format != memory_format::any)
127 return status::success;
129 const int ndims = dst_pd_.desc()->ndims;
130 const auto fallback_dst_fmt = types::flat_memory_format(ndims);
132 /* the stupidest ever heuristics */
133 memory_format_t desired_dst_fmt = dst_pd_.desc()->format;
134 for (int i = 0; i < n_; ++i)
135 desired_dst_fmt = nstl::max(desired_dst_fmt,
136 src_pds_[i].desc()->format);
138 /* try to create dst with the desired format */
139 status_t status = dst_pd_.set_format(desired_dst_fmt);
140 if (status != status::success) {
141 /* if fail use fallback flat layout */
142 return dst_pd_.set_format(fallback_dst_fmt);
145 /* check if we can create view for the dst with the desired format */
146 bool desired_format_ok = true;
147 int current_concat_dim_offset = 0;
148 for (int i = 0; i < n_; ++i) {
149 const int dim = src_pds_[i].desc()->dims[concat_dim_];
150 dims_t dims, offsets = {};
151 utils::array_copy(dims, dst_pd_.desc()->dims, ndims);
152 dims[concat_dim_] = dim;
153 offsets[concat_dim_] = current_concat_dim_offset;
155 cpu_view_t::pd_t v_pd(src_pds_[i].engine());
156 if (v_pd.init(&dst_pd_, dims, offsets) != success) {
157 desired_format_ok = false;
160 current_concat_dim_offset += dim;
163 if (!desired_format_ok) {
164 /* if fail use fallback flat layout */
165 return dst_pd_.set_format(fallback_dst_fmt);
168 return status::success;
178 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s