tvm
cost_model.h
Go to the documentation of this file.
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/ffi/container/array.h>
24 #include <tvm/ffi/function.h>
25 #include <tvm/ffi/reflection/registry.h>
26 #include <tvm/ffi/string.h>
30 #include <tvm/runtime/object.h>
32 
33 #include <vector>
34 
35 namespace tvm {
36 namespace meta_schedule {
37 
38 class TuneContext;
39 
41 class CostModelNode : public runtime::Object {
42  public:
44  virtual ~CostModelNode() = default;
45 
50  virtual void Load(const String& path) = 0;
51 
56  virtual void Save(const String& path) = 0;
57 
64  virtual void Update(const TuneContext& context, const Array<MeasureCandidate>& candidates,
65  const Array<RunnerResult>& results) = 0;
66 
73  virtual std::vector<double> Predict(const TuneContext& context,
74  const Array<MeasureCandidate>& candidates) = 0;
75 
76  static constexpr const char* _type_key = "meta_schedule.CostModel";
78 };
79 
82  public:
87  using FLoad = ffi::TypedFunction<void(String)>;
92  using FSave = ffi::TypedFunction<void(String)>;
100  using FUpdate = ffi::TypedFunction<void(const TuneContext&, const Array<MeasureCandidate>&,
101  const Array<RunnerResult>&)>;
108  using FPredict =
109  ffi::TypedFunction<void(const TuneContext&, const Array<MeasureCandidate>&, void* p_addr)>;
114  using FAsString = ffi::TypedFunction<String()>;
115 
126 
127  void Load(const String& path);
128  void Save(const String& path);
129  void Update(const TuneContext& context, const Array<MeasureCandidate>& candidates,
130  const Array<RunnerResult>& results);
131  std::vector<double> Predict(const TuneContext& context,
132  const Array<MeasureCandidate>& candidates);
133 
134  static constexpr const char* _type_key = "meta_schedule.PyCostModel";
136 };
137 
142 class CostModel : public runtime::ObjectRef {
143  public:
153  TVM_DLL static CostModel PyCostModel(PyCostModelNode::FLoad f_load, //
154  PyCostModelNode::FSave f_save, //
155  PyCostModelNode::FUpdate f_update, //
156  PyCostModelNode::FPredict f_predict, //
157  PyCostModelNode::FAsString f_as_string);
159 };
160 
161 } // namespace meta_schedule
162 } // namespace tvm
163 
164 #endif // TVM_META_SCHEDULE_COST_MODEL_H_
Cost model.
Definition: cost_model.h:41
virtual void Save(const String &path)=0
Save the cost model to given file location.
virtual void Update(const TuneContext &context, const Array< MeasureCandidate > &candidates, const Array< RunnerResult > &results)=0
Update the cost model given running results.
virtual std::vector< double > Predict(const TuneContext &context, const Array< MeasureCandidate > &candidates)=0
Predict the normalized score (the larger the better) of given measure candidates.
virtual ~CostModelNode()=default
Virtual destructor.
TVM_DECLARE_BASE_OBJECT_INFO(CostModelNode, Object)
static constexpr const char * _type_key
Definition: cost_model.h:76
virtual void Load(const String &path)=0
Load the cost model from given file location.
Managed reference to CostModelNode.
Definition: cost_model.h:142
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(CostModel, ObjectRef, CostModelNode)
static CostModel PyCostModel(PyCostModelNode::FLoad f_load, PyCostModelNode::FSave f_save, PyCostModelNode::FUpdate f_update, PyCostModelNode::FPredict f_predict, PyCostModelNode::FAsString f_as_string)
Create a feature extractor with customized methods on the python-side.
The cost model with customized methods on the python-side.
Definition: cost_model.h:81
FPredict f_predict
The packed function to the Predict function.
Definition: cost_model.h:123
void Load(const String &path)
Load the cost model from given file location.
FLoad f_load
The packed function to the Load function.
Definition: cost_model.h:117
void Save(const String &path)
Save the cost model to given file location.
ffi::TypedFunction< void(const TuneContext &, const Array< MeasureCandidate > &, void *p_addr)> FPredict
Predict the running results of given measure candidates.
Definition: cost_model.h:109
void Update(const TuneContext &context, const Array< MeasureCandidate > &candidates, const Array< RunnerResult > &results)
Update the cost model given running results.
TVM_DECLARE_FINAL_OBJECT_INFO(PyCostModelNode, CostModelNode)
ffi::TypedFunction< String()> FAsString
Get the cost model as string with name.
Definition: cost_model.h:114
FSave f_save
The packed function to the Save function.
Definition: cost_model.h:119
std::vector< double > Predict(const TuneContext &context, const Array< MeasureCandidate > &candidates)
Predict the normalized score (the larger the better) of given measure candidates.
static constexpr const char * _type_key
Definition: cost_model.h:134
ffi::TypedFunction< void(String)> FLoad
Load the cost model from given file location.
Definition: cost_model.h:87
ffi::TypedFunction< void(const TuneContext &, const Array< MeasureCandidate > &, const Array< RunnerResult > &)> FUpdate
Update the cost model given running results.
Definition: cost_model.h:101
FUpdate f_update
The packed function to the Update function.
Definition: cost_model.h:121
ffi::TypedFunction< void(String)> FSave
Save the cost model to given file location.
Definition: cost_model.h:92
FAsString f_as_string
The packed function to the AsString function.
Definition: cost_model.h:125
Managed reference to TuneContextNode.
Definition: tune_context.h:98
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
A managed object in the TVM runtime.