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_S_TIR_META_SCHEDULE_COST_MODEL_H_
21 #define TVM_S_TIR_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>
27 #include <tvm/runtime/base.h>
32 
33 #include <vector>
34 
35 namespace tvm {
36 namespace s_tir {
37 namespace meta_schedule {
38 
39 class TuneContext;
40 
42 class CostModelNode : public ffi::Object {
43  public:
45  virtual ~CostModelNode() = default;
46 
51  virtual void Load(const ffi::String& path) = 0;
52 
57  virtual void Save(const ffi::String& path) = 0;
58 
65  virtual void Update(const TuneContext& context, const ffi::Array<MeasureCandidate>& candidates,
66  const ffi::Array<RunnerResult>& results) = 0;
67 
74  virtual std::vector<double> Predict(const TuneContext& context,
75  const ffi::Array<MeasureCandidate>& candidates) = 0;
76 
77  static constexpr const bool _type_mutable = true;
78  TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.CostModel", CostModelNode, ffi::Object);
79 };
80 
83  public:
88  using FLoad = ffi::TypedFunction<void(ffi::String)>;
93  using FSave = ffi::TypedFunction<void(ffi::String)>;
101  using FUpdate = ffi::TypedFunction<void(const TuneContext&, const ffi::Array<MeasureCandidate>&,
102  const ffi::Array<RunnerResult>&)>;
109  using FPredict = ffi::TypedFunction<void(const TuneContext&, const ffi::Array<MeasureCandidate>&,
110  void* p_addr)>;
119 
120  void Load(const ffi::String& path);
121  void Save(const ffi::String& path);
122  void Update(const TuneContext& context, const ffi::Array<MeasureCandidate>& candidates,
123  const ffi::Array<RunnerResult>& results);
124  std::vector<double> Predict(const TuneContext& context,
125  const ffi::Array<MeasureCandidate>& candidates);
126  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.PyCostModel", PyCostModelNode,
127  CostModelNode);
128 };
129 
134 class CostModel : public ffi::ObjectRef {
135  public:
144  TVM_DLL static CostModel PyCostModel(PyCostModelNode::FLoad f_load, //
145  PyCostModelNode::FSave f_save, //
146  PyCostModelNode::FUpdate f_update, //
147  PyCostModelNode::FPredict f_predict);
149 };
150 
151 } // namespace meta_schedule
152 } // namespace s_tir
153 } // namespace tvm
154 
155 #endif // TVM_S_TIR_META_SCHEDULE_COST_MODEL_H_
Cost model.
Definition: cost_model.h:42
static constexpr const bool _type_mutable
Definition: cost_model.h:77
virtual void Save(const ffi::String &path)=0
Save the cost model to given file location.
TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.CostModel", CostModelNode, ffi::Object)
virtual std::vector< double > Predict(const TuneContext &context, const ffi::Array< MeasureCandidate > &candidates)=0
Predict the normalized score (the larger the better) of given measure candidates.
virtual ~CostModelNode()=default
Virtual destructor.
virtual void Update(const TuneContext &context, const ffi::Array< MeasureCandidate > &candidates, const ffi::Array< RunnerResult > &results)=0
Update the cost model given running results.
virtual void Load(const ffi::String &path)=0
Load the cost model from given file location.
Managed reference to CostModelNode.
Definition: cost_model.h:134
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(CostModel, ffi::ObjectRef, CostModelNode)
static CostModel PyCostModel(PyCostModelNode::FLoad f_load, PyCostModelNode::FSave f_save, PyCostModelNode::FUpdate f_update, PyCostModelNode::FPredict f_predict)
Create a cost model with customized methods on the python-side.
The cost model with customized methods on the python-side.
Definition: cost_model.h:82
void Update(const TuneContext &context, const ffi::Array< MeasureCandidate > &candidates, const ffi::Array< RunnerResult > &results)
Update the cost model given running results.
void Save(const ffi::String &path)
Save the cost model to given file location.
ffi::TypedFunction< void(const TuneContext &, const ffi::Array< MeasureCandidate > &, void *p_addr)> FPredict
Predict the running results of given measure candidates.
Definition: cost_model.h:110
FUpdate f_update
The packed function to the Update function.
Definition: cost_model.h:116
ffi::TypedFunction< void(const TuneContext &, const ffi::Array< MeasureCandidate > &, const ffi::Array< RunnerResult > &)> FUpdate
Update the cost model given running results.
Definition: cost_model.h:102
ffi::TypedFunction< void(ffi::String)> FSave
Save the cost model to given file location.
Definition: cost_model.h:93
FSave f_save
The packed function to the Save function.
Definition: cost_model.h:114
std::vector< double > Predict(const TuneContext &context, const ffi::Array< MeasureCandidate > &candidates)
Predict the normalized score (the larger the better) of given measure candidates.
void Load(const ffi::String &path)
Load the cost model from given file location.
FLoad f_load
The packed function to the Load function.
Definition: cost_model.h:112
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.PyCostModel", PyCostModelNode, CostModelNode)
FPredict f_predict
The packed function to the Predict function.
Definition: cost_model.h:118
ffi::TypedFunction< void(ffi::String)> FLoad
Load the cost model from given file location.
Definition: cost_model.h:88
Managed reference to TuneContextNode.
Definition: tune_context.h:99
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40