1 | /******************************************************************************* |
2 | * Copyright 2019-2022 Intel Corporation |
3 | * |
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 |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
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 | *******************************************************************************/ |
16 | |
17 | #include <assert.h> |
18 | #include "oneapi/dnnl/dnnl.h" |
19 | #include "opdesc.hpp" |
20 | #include "primitive_desc_iface.hpp" |
21 | |
22 | #include "c_types_map.hpp" |
23 | #include "type_helpers.hpp" |
24 | #include "utils.hpp" |
25 | |
26 | using namespace dnnl::impl; |
27 | using namespace dnnl::impl::utils; |
28 | using namespace dnnl::impl::status; |
29 | using namespace dnnl::impl::prop_kind; |
30 | using namespace dnnl::impl::alg_kind; |
31 | using namespace dnnl::impl::types; |
32 | |
33 | namespace { |
34 | status_t resampling_desc_init(resampling_desc_t *resampling_desc, |
35 | prop_kind_t prop_kind, alg_kind_t alg_kind, const float *factors, |
36 | const memory_desc_t *src_desc, const memory_desc_t *dst_desc) { |
37 | bool args_ok = true |
38 | && one_of(alg_kind, resampling_nearest, resampling_linear) |
39 | && src_desc && IMPLICATION(dst_desc == nullptr, factors) |
40 | && utils::one_of(src_desc->ndims, 3, 4, 5); |
41 | if (!args_ok) return invalid_arguments; |
42 | |
43 | const bool is_fwd = one_of(prop_kind, forward_training, forward_inference); |
44 | if (is_fwd) { |
45 | args_ok = args_ok && src_desc->format_kind != format_kind::any; |
46 | if (!args_ok) return invalid_arguments; |
47 | } |
48 | |
49 | auto rd = resampling_desc_t(); |
50 | rd.primitive_kind = primitive_kind::resampling; |
51 | rd.prop_kind = prop_kind; |
52 | rd.alg_kind = alg_kind; |
53 | |
54 | bool runtime_dims_or_strides |
55 | = memory_desc_wrapper(src_desc).has_runtime_dims_or_strides() |
56 | || (dst_desc |
57 | && memory_desc_wrapper(dst_desc) |
58 | .has_runtime_dims_or_strides()); |
59 | if (runtime_dims_or_strides) return unimplemented; |
60 | |
61 | auto fill_dst_md = [](const memory_desc_t *i_md, const float *factors, |
62 | memory_desc_t *o_md) { |
63 | o_md->ndims = i_md->ndims; |
64 | o_md->data_type = i_md->data_type; |
65 | utils::array_copy(o_md->dims, i_md->dims, 2); |
66 | for (int i = 0; i < o_md->ndims - 2; i++) |
67 | o_md->dims[2 + i] = (dim_t)(i_md->dims[2 + i] * factors[i]); |
68 | o_md->format_kind = format_kind::any; |
69 | }; |
70 | |
71 | (prop_kind == backward_data ? rd.diff_src_desc : rd.src_desc) = *src_desc; |
72 | if (dst_desc) |
73 | (is_fwd ? rd.dst_desc : rd.diff_dst_desc) = *dst_desc; |
74 | else { |
75 | dst_desc = (is_fwd ? &rd.dst_desc : &rd.diff_dst_desc); |
76 | fill_dst_md( |
77 | src_desc, factors, (is_fwd ? &rd.dst_desc : &rd.diff_dst_desc)); |
78 | } |
79 | |
80 | /* User provided factors are used only to compute destination dimensions. |
81 | Implementation uses true scaling factors from source to destination */ |
82 | for (int i = 0; i < src_desc->ndims - 2; i++) |
83 | rd.factors[i] = (float)((double)dst_desc->dims[2 + i] |
84 | / src_desc->dims[2 + i]); |
85 | |
86 | bool consistency = src_desc->ndims == dst_desc->ndims |
87 | && src_desc->dims[0] == dst_desc->dims[0] |
88 | && src_desc->dims[1] == dst_desc->dims[1]; |
89 | |
90 | if (!consistency) return invalid_arguments; |
91 | |
92 | *resampling_desc = rd; |
93 | return success; |
94 | } |
95 | } // namespace |
96 | |
97 | status_t dnnl_resampling_forward_primitive_desc_create( |
98 | primitive_desc_iface_t **primitive_desc_iface, engine_t *engine, |
99 | prop_kind_t prop_kind, alg_kind_t alg_kind, const float *factors, |
100 | const memory_desc_t *src_desc, const memory_desc_t *dst_desc, |
101 | const primitive_attr_t *attr) { |
102 | if (!one_of(prop_kind, forward_training, forward_inference)) |
103 | return invalid_arguments; |
104 | |
105 | auto resampling_desc = resampling_desc_t(); |
106 | CHECK(resampling_desc_init(&resampling_desc, prop_kind, alg_kind, factors, |
107 | src_desc, dst_desc)); |
108 | return primitive_desc_create(primitive_desc_iface, engine, |
109 | (const op_desc_t *)&resampling_desc, nullptr, attr); |
110 | } |
111 | |
112 | status_t dnnl_resampling_backward_primitive_desc_create( |
113 | primitive_desc_iface_t **primitive_desc_iface, engine_t *engine, |
114 | alg_kind_t alg_kind, const float *factors, |
115 | const memory_desc_t *diff_src_desc, const memory_desc_t *diff_dst_desc, |
116 | const primitive_desc_iface_t *hint_fwd_pd, |
117 | const primitive_attr_t *attr) { |
118 | |
119 | auto resampling_desc = resampling_desc_t(); |
120 | CHECK(resampling_desc_init(&resampling_desc, backward_data, alg_kind, |
121 | factors, diff_src_desc, diff_dst_desc)); |
122 | return primitive_desc_create(primitive_desc_iface, engine, |
123 | (const op_desc_t *)&resampling_desc, hint_fwd_pd, attr); |
124 | } |
125 | // vim: et ts=4 sw=4 cindent cino+=l0,\:4,N-s |
126 | |