| 1 | // ======================================================================== // |
|---|---|
| 2 | // Copyright 2009-2019 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 | #pragma once |
| 18 | |
| 19 | #include "common/platform.h" |
| 20 | |
| 21 | #include "mkl-dnn/include/mkldnn.hpp" |
| 22 | #include "mkl-dnn/include/mkldnn_debug.h" |
| 23 | #include "mkl-dnn/src/common/mkldnn_thread.hpp" |
| 24 | #include "mkl-dnn/src/common/type_helpers.hpp" |
| 25 | #include "mkl-dnn/src/cpu/jit_generator.hpp" |
| 26 | |
| 27 | #include "common/ref.h" |
| 28 | #include "common/exception.h" |
| 29 | #include "common/thread.h" |
| 30 | // -- GODOT start -- |
| 31 | //#include "common/tasking.h" |
| 32 | // -- GODOT end -- |
| 33 | #include "math.h" |
| 34 | |
| 35 | namespace oidn { |
| 36 | |
| 37 | using namespace mkldnn; |
| 38 | using namespace mkldnn::impl::cpu; |
| 39 | using mkldnn::impl::parallel_nd; |
| 40 | using mkldnn::impl::memory_desc_matches_tag; |
| 41 | |
| 42 | |
| 43 | inline size_t getFormatBytes(Format format) |
| 44 | { |
| 45 | switch (format) |
| 46 | { |
| 47 | case Format::Undefined: return 1; |
| 48 | case Format::Float: return sizeof(float); |
| 49 | case Format::Float2: return sizeof(float)*2; |
| 50 | case Format::Float3: return sizeof(float)*3; |
| 51 | case Format::Float4: return sizeof(float)*4; |
| 52 | } |
| 53 | assert(0); |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | inline memory::dims getTensorDims(const std::shared_ptr<memory>& mem) |
| 59 | { |
| 60 | const mkldnn_memory_desc_t& desc = mem->get_desc().data; |
| 61 | return memory::dims(&desc.dims[0], &desc.dims[desc.ndims]); |
| 62 | } |
| 63 | |
| 64 | inline memory::data_type getTensorType(const std::shared_ptr<memory>& mem) |
| 65 | { |
| 66 | const mkldnn_memory_desc_t& desc = mem->get_desc().data; |
| 67 | return memory::data_type(desc.data_type); |
| 68 | } |
| 69 | |
| 70 | // Returns the number of values in a tensor |
| 71 | inline size_t getTensorSize(const memory::dims& dims) |
| 72 | { |
| 73 | size_t res = 1; |
| 74 | for (int i = 0; i < (int)dims.size(); ++i) |
| 75 | res *= dims[i]; |
| 76 | return res; |
| 77 | } |
| 78 | |
| 79 | inline memory::dims getMaxTensorDims(const std::vector<memory::dims>& dims) |
| 80 | { |
| 81 | memory::dims result; |
| 82 | size_t maxSize = 0; |
| 83 | |
| 84 | for (const auto& d : dims) |
| 85 | { |
| 86 | const size_t size = getTensorSize(d); |
| 87 | if (size > maxSize) |
| 88 | { |
| 89 | result = d; |
| 90 | maxSize = size; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | inline size_t getTensorSize(const std::shared_ptr<memory>& mem) |
| 98 | { |
| 99 | return getTensorSize(getTensorDims(mem)); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | template<int K> |
| 104 | inline int getPadded(int dim) |
| 105 | { |
| 106 | return (dim + (K-1)) & ~(K-1); |
| 107 | } |
| 108 | |
| 109 | template<int K> |
| 110 | inline memory::dims getPadded_nchw(const memory::dims& dims) |
| 111 | { |
| 112 | assert(dims.size() == 4); |
| 113 | memory::dims padDims = dims; |
| 114 | padDims[1] = getPadded<K>(dims[1]); // pad C |
| 115 | return padDims; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | template<int K> |
| 120 | struct BlockedFormat; |
| 121 | |
| 122 | template<> |
| 123 | struct BlockedFormat<8> |
| 124 | { |
| 125 | static constexpr memory::format_tag nChwKc = memory::format_tag::nChw8c; |
| 126 | static constexpr memory::format_tag OIhwKiKo = memory::format_tag::OIhw8i8o; |
| 127 | }; |
| 128 | |
| 129 | template<> |
| 130 | struct BlockedFormat<16> |
| 131 | { |
| 132 | static constexpr memory::format_tag nChwKc = memory::format_tag::nChw16c; |
| 133 | static constexpr memory::format_tag OIhwKiKo = memory::format_tag::OIhw16i16o; |
| 134 | }; |
| 135 | |
| 136 | } // namespace oidn |
| 137 |