|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/*! |
| 21 | + * \file src/contrib/ethosu/cascader/common.h |
| 22 | + * \brief Common functions used in the NPU cascader |
| 23 | + */ |
| 24 | +#ifndef TVM_CONTRIB_ETHOSU_CASCADER_COMMON_H_ |
| 25 | +#define TVM_CONTRIB_ETHOSU_CASCADER_COMMON_H_ |
| 26 | + |
| 27 | +#include <tvm/ir/expr.h> |
| 28 | +#include <tvm/runtime/container/array.h> |
| 29 | + |
| 30 | +#include <vector> |
| 31 | + |
| 32 | +namespace tvm { |
| 33 | +namespace contrib { |
| 34 | +namespace ethosu { |
| 35 | +namespace cascader { |
| 36 | + |
| 37 | +/*! |
| 38 | + * \brief Make a tvm::Array<Integer> from an int vector. |
| 39 | + * \param vec The int vector. |
| 40 | + * \return The Integer Array. |
| 41 | + * \note Array<Integer>(std::vector<int>) doesn't work as this implicit |
| 42 | + * type conversion fails. This is why this helper is required. |
| 43 | + */ |
| 44 | +inline Array<Integer> make_array(const std::vector<int>& vec) { |
| 45 | + Array<Integer> arr; |
| 46 | + arr.resize(vec.size()); |
| 47 | + for (unsigned int i = 0; i < vec.size(); ++i) { |
| 48 | + arr.Set(i, Integer(vec[i])); |
| 49 | + } |
| 50 | + return arr; |
| 51 | +} |
| 52 | + |
| 53 | +/*! |
| 54 | + * \brief Make a tvm::Array<FloatImm> from an float vector. |
| 55 | + * \param vec The float vector. |
| 56 | + * \return The FloatImm Array. |
| 57 | + */ |
| 58 | +inline Array<FloatImm> make_array(const std::vector<float>& vec) { |
| 59 | + Array<FloatImm> arr; |
| 60 | + arr.resize(vec.size()); |
| 61 | + for (unsigned int i = 0; i < vec.size(); ++i) { |
| 62 | + arr.Set(i, FloatImm(DataType::Float(32), static_cast<double>(vec[i]))); |
| 63 | + } |
| 64 | + return arr; |
| 65 | +} |
| 66 | + |
| 67 | +/*! |
| 68 | + * \brief Make a vector from a tvm::Array. |
| 69 | + * \param arr The Array. |
| 70 | + * \return The vector. |
| 71 | + */ |
| 72 | +template <class T, class tvm_T> |
| 73 | +inline std::vector<T> make_vector(const Array<tvm_T>& arr) { |
| 74 | + std::vector<T> vec(arr.size()); |
| 75 | + for (unsigned int i = 0; i < arr.size(); ++i) { |
| 76 | + vec[i] = arr[i]->value; |
| 77 | + } |
| 78 | + return vec; |
| 79 | +} |
| 80 | + |
| 81 | +/*! |
| 82 | + * \brief Create a combined hash. |
| 83 | + * \param seed The current hash value. |
| 84 | + * \param v The value to combine into the hash. |
| 85 | + * \return The combined hash. |
| 86 | + */ |
| 87 | +template <class T> |
| 88 | +inline void hash_combine(std::size_t* seed, T const& v) { |
| 89 | + *seed ^= std::hash<T>()(v) + 0x9e3779b9 + (*seed << 6) + (*seed >> 2); |
| 90 | +} |
| 91 | + |
| 92 | +/*! |
| 93 | + * \brief Hash a vector. |
| 94 | + * \param vec The vector to hash. |
| 95 | + * \return The hash. |
| 96 | + */ |
| 97 | +template <class T> |
| 98 | +inline std::size_t hash_vector(const std::vector<T>& vec) { |
| 99 | + std::size_t seed = vec.size(); |
| 100 | + for (const auto& elem : vec) { |
| 101 | + hash_combine(&seed, elem); |
| 102 | + } |
| 103 | + return seed; |
| 104 | +} |
| 105 | + |
| 106 | +} // namespace cascader |
| 107 | +} // namespace ethosu |
| 108 | +} // namespace contrib |
| 109 | +} // namespace tvm |
| 110 | + |
| 111 | +#endif // TVM_CONTRIB_ETHOSU_CASCADER_COMMON_H_ |
0 commit comments