tvm
mutator.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_MUTATOR_H_
21 #define TVM_META_SCHEDULE_MUTATOR_H_
22 
23 #include <tvm/ffi/function.h>
24 #include <tvm/ffi/optional.h>
25 #include <tvm/ffi/reflection/registry.h>
26 #include <tvm/runtime/object.h>
29 #include <tvm/tir/schedule/trace.h>
30 
31 namespace tvm {
32 namespace meta_schedule {
33 
34 class TuneContext;
35 class Mutator;
36 
38 class MutatorNode : public runtime::Object {
39  public:
41  virtual ~MutatorNode() = default;
42 
43  static void RegisterReflection() {
44  namespace refl = tvm::ffi::reflection;
45  refl::ObjectDef<MutatorNode>();
46  }
47 
53  virtual void InitializeWithTuneContext(const TuneContext& context) = 0;
54 
61  virtual ffi::Optional<tir::Trace> Apply(
62  const tir::Trace& trace, support::LinearCongruentialEngine::TRandState* rand_state) = 0;
63 
68  virtual Mutator Clone() const = 0;
69 
70  static constexpr const bool _type_mutable = true;
71  TVM_FFI_DECLARE_OBJECT_INFO("meta_schedule.Mutator", MutatorNode, Object);
72 };
73 
78 class Mutator : public runtime::ObjectRef {
79  public:
84  using FInitializeWithTuneContext = ffi::TypedFunction<void(const TuneContext&)>;
90  using FApply = ffi::TypedFunction<ffi::Optional<tir::Trace>(
96  using FClone = ffi::TypedFunction<Mutator()>;
101  using FAsString = ffi::TypedFunction<ffi::String()>;
103  TVM_DLL static Mutator MutateTileSize();
109  TVM_DLL static Mutator MutateParallel(int64_t max_jobs_per_core);
114  TVM_DLL static Mutator MutateUnroll();
119  TVM_DLL static Mutator MutateComputeLocation();
124  TVM_DLL static Mutator MutateThreadBinding();
133  TVM_DLL static Mutator PyMutator(FInitializeWithTuneContext f_initialize_with_tune_context,
134  FApply f_apply, FClone f_clone, FAsString f_as_string);
136  TVM_DLL static ffi::Map<Mutator, FloatImm, void> DefaultLLVM();
138  TVM_DLL static ffi::Map<Mutator, FloatImm, void> DefaultCUDA();
140  TVM_DLL static ffi::Map<Mutator, FloatImm, void> DefaultCUDATensorCore();
142  TVM_DLL static ffi::Map<Mutator, FloatImm, void> DefaultHexagon();
143 
145 };
146 
148 class PyMutatorNode : public MutatorNode {
149  public:
162 
163  static void RegisterReflection() {
164  // `f_initialize_with_tune_context` is not registered
165  // `f_apply` is not registered
166  // `f_clone` is not registered
167  // `f_as_string` is not registered
168  }
169 
170  void InitializeWithTuneContext(const TuneContext& context) final;
171  ffi::Optional<tir::Trace> Apply(const tir::Trace& trace,
173  Mutator Clone() const final;
175 };
176 
177 } // namespace meta_schedule
178 } // namespace tvm
179 
180 #endif // TVM_META_SCHEDULE_MUTATOR_H_
Mutator is designed to mutate the trace to explore the design space.
Definition: mutator.h:38
TVM_FFI_DECLARE_OBJECT_INFO("meta_schedule.Mutator", MutatorNode, Object)
virtual ~MutatorNode()=default
Virtual destructor.
virtual void InitializeWithTuneContext(const TuneContext &context)=0
Initialize the design space generator with tuning context.
virtual ffi::Optional< tir::Trace > Apply(const tir::Trace &trace, support::LinearCongruentialEngine::TRandState *rand_state)=0
Apply the mutator function to the given trace.
static constexpr const bool _type_mutable
Definition: mutator.h:70
virtual Mutator Clone() const =0
Clone the mutator.
static void RegisterReflection()
Definition: mutator.h:43
Managed reference to MutatorNode.
Definition: mutator.h:78
static Mutator MutateThreadBinding()
Create a Mutator that mutates auto thread binding.
ffi::TypedFunction< Mutator()> FClone
Clone the mutator.
Definition: mutator.h:96
static Mutator MutateComputeLocation()
Create a Mutator that mutates the outcome of SampleComputeLocation.
static Mutator MutateUnroll()
Create a Mutator that mutates auto unroll step.
static ffi::Map< Mutator, FloatImm, void > DefaultCUDATensorCore()
Create default mutators for CUDA with TensorCore.
ffi::TypedFunction< ffi::String()> FAsString
Get the mutator as string with name.
Definition: mutator.h:101
ffi::TypedFunction< ffi::Optional< tir::Trace >(const tir::Trace &, support::LinearCongruentialEngine::TRandState rand_state)> FApply
Apply the mutator function to the given trace.
Definition: mutator.h:91
static ffi::Map< Mutator, FloatImm, void > DefaultLLVM()
Create default mutators for LLVM.
static Mutator MutateTileSize()
Create a Mutator that mutates the decision of instruction Sample-Perfect-Tile.
static Mutator MutateParallel(int64_t max_jobs_per_core)
Create a Mutator that mutates the parallel extent.
static Mutator PyMutator(FInitializeWithTuneContext f_initialize_with_tune_context, FApply f_apply, FClone f_clone, FAsString f_as_string)
Create a mutator with customized methods on the python-side.
static ffi::Map< Mutator, FloatImm, void > DefaultHexagon()
Create default mutators for Hexagon.
static ffi::Map< Mutator, FloatImm, void > DefaultCUDA()
Create default mutators for CUDA.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Mutator, ObjectRef, MutatorNode)
ffi::TypedFunction< void(const TuneContext &)> FInitializeWithTuneContext
The function type of InitializeWithTuneContext method.
Definition: mutator.h:84
The mutator with customized methods on the python-side.
Definition: mutator.h:148
Mutator::FApply FApply
Definition: mutator.h:151
FInitializeWithTuneContext f_initialize_with_tune_context
The packed function to the InitializeWithTuneContext function.
Definition: mutator.h:155
Mutator::FClone FClone
Definition: mutator.h:152
static void RegisterReflection()
Definition: mutator.h:163
FAsString f_as_string
The packed function to the AsString function.
Definition: mutator.h:161
Mutator::FAsString FAsString
Definition: mutator.h:153
FApply f_apply
The packed function to the Apply function.
Definition: mutator.h:157
void InitializeWithTuneContext(const TuneContext &context) final
Initialize the design space generator with tuning context.
Mutator Clone() const final
Clone the mutator.
FClone f_clone
The packed function to the Clone function.
Definition: mutator.h:159
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("meta_schedule.PyMutator", PyMutatorNode, MutatorNode)
ffi::Optional< tir::Trace > Apply(const tir::Trace &trace, support::LinearCongruentialEngine::TRandState *rand_state) final
Apply the mutator function to the given trace.
Mutator::FInitializeWithTuneContext FInitializeWithTuneContext
Definition: mutator.h:150
Managed reference to TuneContextNode.
Definition: tune_context.h:98
int64_t TRandState
Definition: random_engine.h:46
Managed reference to TraceNode.
Definition: trace.h:143
Definition: repr_printer.h:91
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
A managed object in the TVM runtime.
Random number generator. It provides a generic interface consistent with std::uniform_random_bit_gene...