|
| 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 | +#ifndef TVM_META_SCHEDULE_COST_MODEL_H_ |
| 21 | +#define TVM_META_SCHEDULE_COST_MODEL_H_ |
| 22 | + |
| 23 | +#include <tvm/meta_schedule/search_strategy.h> |
| 24 | + |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +namespace tvm { |
| 28 | +namespace meta_schedule { |
| 29 | + |
| 30 | +class TuneContext; |
| 31 | + |
| 32 | +/*! \brief Cost model. */ |
| 33 | +class CostModelNode : public runtime::Object { |
| 34 | + public: |
| 35 | + /*! \brief Virtual destructor. */ |
| 36 | + virtual ~CostModelNode() = default; |
| 37 | + |
| 38 | + void VisitAttrs(tvm::AttrVisitor* v) {} |
| 39 | + |
| 40 | + /*! |
| 41 | + * \brief Load the cost model from given file location. |
| 42 | + * \param path The file path. |
| 43 | + */ |
| 44 | + virtual void Load(const String& path) = 0; |
| 45 | + |
| 46 | + /*! |
| 47 | + * \brief Save the cost model to given file location. |
| 48 | + * \param path The file path. |
| 49 | + */ |
| 50 | + virtual void Save(const String& path) = 0; |
| 51 | + |
| 52 | + /*! |
| 53 | + * \brief Update the cost model given running results. |
| 54 | + * \param tune_context The tuning context. |
| 55 | + * \param candidates The measure candidates. |
| 56 | + * \param results The running results of the measure candidates. |
| 57 | + */ |
| 58 | + virtual void Update(const TuneContext& tune_context, const Array<MeasureCandidate>& candidates, |
| 59 | + const Array<RunnerResult>& results) = 0; |
| 60 | + |
| 61 | + /*! |
| 62 | + * \brief Predict the normalized score (the larger the better) of given measure candidates. |
| 63 | + * \param tune_context The tuning context. |
| 64 | + * \param candidates The measure candidates. |
| 65 | + * \return The predicted normalized score. |
| 66 | + */ |
| 67 | + virtual std::vector<double> Predict(const TuneContext& tune_context, |
| 68 | + const Array<MeasureCandidate>& candidates) = 0; |
| 69 | + |
| 70 | + static constexpr const char* _type_key = "meta_schedule.CostModel"; |
| 71 | + TVM_DECLARE_BASE_OBJECT_INFO(CostModelNode, Object); |
| 72 | +}; |
| 73 | + |
| 74 | +/*! \brief The cost model with customized methods on the python-side. */ |
| 75 | +class PyCostModelNode : public CostModelNode { |
| 76 | + public: |
| 77 | + /*! |
| 78 | + * \brief Load the cost model from given file location. |
| 79 | + * \param path The file path. |
| 80 | + */ |
| 81 | + using FLoad = runtime::TypedPackedFunc<void(String)>; |
| 82 | + /*! |
| 83 | + * \brief Save the cost model to given file location. |
| 84 | + * \param path The file path. |
| 85 | + */ |
| 86 | + using FSave = runtime::TypedPackedFunc<void(String)>; |
| 87 | + /*! |
| 88 | + * \brief Update the cost model given running results. |
| 89 | + * \param tune_context The tuning context. |
| 90 | + * \param candidates The measure candidates. |
| 91 | + * \param results The running results of the measure candidates. |
| 92 | + * \return Whether cost model was updated successfully. |
| 93 | + */ |
| 94 | + using FUpdate = runtime::TypedPackedFunc<void(const TuneContext&, const Array<MeasureCandidate>&, |
| 95 | + const Array<RunnerResult>&)>; |
| 96 | + /*! |
| 97 | + * \brief Predict the running results of given measure candidates. |
| 98 | + * \param tune_context The tuning context. |
| 99 | + * \param candidates The measure candidates. |
| 100 | + * \param p_addr The address to save the the estimated running results. |
| 101 | + */ |
| 102 | + using FPredict = runtime::TypedPackedFunc<void(const TuneContext&, const Array<MeasureCandidate>&, |
| 103 | + void* p_addr)>; |
| 104 | + /*! |
| 105 | + * \brief Get the cost model as string with name. |
| 106 | + * \return The string representation of the cost model. |
| 107 | + */ |
| 108 | + using FAsString = runtime::TypedPackedFunc<String()>; |
| 109 | + |
| 110 | + /*! \brief The packed function to the `Load` function. */ |
| 111 | + FLoad f_load; |
| 112 | + /*! \brief The packed function to the `Save` function. */ |
| 113 | + FSave f_save; |
| 114 | + /*! \brief The packed function to the `Update` function. */ |
| 115 | + FUpdate f_update; |
| 116 | + /*! \brief The packed function to the `Predict` function. */ |
| 117 | + FPredict f_predict; |
| 118 | + /*! \brief The packed function to the `AsString` function. */ |
| 119 | + FAsString f_as_string; |
| 120 | + |
| 121 | + void VisitAttrs(tvm::AttrVisitor* v) { |
| 122 | + // `f_load` is not visited |
| 123 | + // `f_save` is not visited |
| 124 | + // `f_update` is not visited |
| 125 | + // `f_predict` is not visited |
| 126 | + // `f_as_string` is not visited |
| 127 | + } |
| 128 | + |
| 129 | + void Load(const String& path) { |
| 130 | + ICHECK(f_load != nullptr) << "PyCostModel's Load method not implemented!"; |
| 131 | + f_load(path); |
| 132 | + } |
| 133 | + |
| 134 | + void Save(const String& path) { |
| 135 | + ICHECK(f_save != nullptr) << "PyCostModel's Save method not implemented!"; |
| 136 | + f_save(path); |
| 137 | + } |
| 138 | + void Update(const TuneContext& tune_context, const Array<MeasureCandidate>& candidates, |
| 139 | + const Array<RunnerResult>& results) { |
| 140 | + ICHECK(f_update != nullptr) << "PyCostModel's Update method not implemented!"; |
| 141 | + f_update(tune_context, candidates, results); |
| 142 | + } |
| 143 | + |
| 144 | + std::vector<double> Predict(const TuneContext& tune_context, |
| 145 | + const Array<MeasureCandidate>& candidates) { |
| 146 | + ICHECK(f_predict != nullptr) << "PyCostModel's Predict method not implemented!"; |
| 147 | + std::vector<double> result(candidates.size(), 0.0); |
| 148 | + f_predict(tune_context, candidates, result.data()); |
| 149 | + return result; |
| 150 | + } |
| 151 | + |
| 152 | + static constexpr const char* _type_key = "meta_schedule.PyCostModel"; |
| 153 | + TVM_DECLARE_FINAL_OBJECT_INFO(PyCostModelNode, CostModelNode); |
| 154 | +}; |
| 155 | + |
| 156 | +/*! |
| 157 | + * \brief Managed reference to CostModelNode |
| 158 | + * \sa CostModelNode |
| 159 | + */ |
| 160 | +class CostModel : public runtime::ObjectRef { |
| 161 | + public: |
| 162 | + /*! |
| 163 | + * \brief Create a feature extractor with customized methods on the python-side. |
| 164 | + * \param f_load The packed function of `Load`. |
| 165 | + * \param f_save The packed function of `Save`. |
| 166 | + * \param f_update The packed function of `Update`. |
| 167 | + * \param f_predict The packed function of `Predict`. |
| 168 | + * \param f_as_string The packed function of `AsString`. |
| 169 | + * \return The feature extractor created. |
| 170 | + */ |
| 171 | + TVM_DLL static CostModel PyCostModel(PyCostModelNode::FLoad f_load, // |
| 172 | + PyCostModelNode::FSave f_save, // |
| 173 | + PyCostModelNode::FUpdate f_update, // |
| 174 | + PyCostModelNode::FPredict f_predict, // |
| 175 | + PyCostModelNode::FAsString f_as_string); |
| 176 | + TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(CostModel, ObjectRef, CostModelNode); |
| 177 | +}; |
| 178 | + |
| 179 | +} // namespace meta_schedule |
| 180 | +} // namespace tvm |
| 181 | + |
| 182 | +#endif // TVM_META_SCHEDULE_COST_MODEL_H_ |
0 commit comments