tvm
Loading...
Searching...
No Matches
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_S_TIR_META_SCHEDULE_MUTATOR_H_
21#define TVM_S_TIR_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/ir/prim/expr.h>
27#include <tvm/runtime/base.h>
31
32namespace tvm {
33namespace s_tir {
34namespace meta_schedule {
35
36class TuneContext;
37class Mutator;
38
40class MutatorObj : public ffi::Object {
41 public:
43 virtual ~MutatorObj() = default;
44
45 static void RegisterReflection() {
46 namespace refl = tvm::ffi::reflection;
47 refl::ObjectDef<MutatorObj>();
48 }
49
55 virtual void InitializeWithTuneContext(const TuneContext& context) = 0;
56
63 virtual ffi::Optional<s_tir::Trace> Apply(const s_tir::Trace& trace,
65
70 virtual Mutator Clone() const = 0;
71
72 static constexpr const bool _type_mutable = true;
73 TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.Mutator", MutatorObj, ffi::Object);
74};
75
80class Mutator : public ffi::ObjectRef {
81 public:
82 explicit Mutator(ffi::UnsafeInit tag, ffi::ObjectPtr<MutatorObj> data) : ffi::ObjectRef(tag) {
83 data_ = std::move(data);
84 }
85
90 using FInitializeWithTuneContext = ffi::TypedFunction<void(const TuneContext&)>;
96 using FApply = ffi::TypedFunction<ffi::Optional<s_tir::Trace>(
102 using FClone = ffi::TypedFunction<Mutator()>;
104 TVM_DLL static Mutator MutateTileSize();
110 TVM_DLL static Mutator MutateParallel(int64_t max_jobs_per_core);
115 TVM_DLL static Mutator MutateUnroll();
125 TVM_DLL static Mutator MutateThreadBinding();
133 TVM_DLL static Mutator PyMutator(FInitializeWithTuneContext f_initialize_with_tune_context,
134 FApply f_apply, FClone f_clone);
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
148class PyMutatorObj : public MutatorObj {
149 public:
159
160 static void RegisterReflection() {
161 namespace refl = tvm::ffi::reflection;
162 refl::ObjectDef<PyMutatorObj>();
163 // `f_initialize_with_tune_context` is not registered
164 // `f_apply` is not registered
165 // `f_clone` is not registered
166 }
167
168 void InitializeWithTuneContext(const TuneContext& context) final;
169 ffi::Optional<s_tir::Trace> Apply(const s_tir::Trace& trace,
170 LinearCongruentialEngine::TRandState* rand_state) final;
171 Mutator Clone() const final;
173};
174
175} // namespace meta_schedule
176} // namespace s_tir
177} // namespace tvm
178
179#endif // TVM_S_TIR_META_SCHEDULE_MUTATOR_H_
int64_t TRandState
Definition random_engine.h:47
Managed reference to TraceNode.
Definition trace.h:145
Mutator is designed to mutate the trace to explore the design space.
Definition mutator.h:40
static void RegisterReflection()
Definition mutator.h:45
virtual ffi::Optional< s_tir::Trace > Apply(const s_tir::Trace &trace, LinearCongruentialEngine::TRandState *rand_state)=0
Apply the mutator function to the given trace.
static constexpr const bool _type_mutable
Definition mutator.h:72
TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.Mutator", MutatorObj, ffi::Object)
virtual ~MutatorObj()=default
Virtual destructor.
virtual Mutator Clone() const =0
Clone the mutator.
virtual void InitializeWithTuneContext(const TuneContext &context)=0
Initialize the design space generator with tuning context.
Managed reference to MutatorObj.
Definition mutator.h:80
static ffi::Map< Mutator, FloatImm, void > DefaultCUDATensorCore()
Create default mutators for CUDA with TensorCore.
ffi::TypedFunction< ffi::Optional< s_tir::Trace >(const s_tir::Trace &, LinearCongruentialEngine::TRandState rand_state)> FApply
Apply the mutator function to the given trace.
Definition mutator.h:97
static ffi::Map< Mutator, FloatImm, void > DefaultHexagon()
Create default mutators for Hexagon.
ffi::TypedFunction< Mutator()> FClone
Clone the mutator.
Definition mutator.h:102
static Mutator MutateThreadBinding()
Create a Mutator that mutates auto thread binding.
static Mutator MutateParallel(int64_t max_jobs_per_core)
Create a Mutator that mutates the parallel extent.
static ffi::Map< Mutator, FloatImm, void > DefaultLLVM()
Create default mutators for LLVM.
Mutator(ffi::UnsafeInit tag, ffi::ObjectPtr< MutatorObj > data)
Definition mutator.h:82
static ffi::Map< Mutator, FloatImm, void > DefaultCUDA()
Create default mutators for CUDA.
static Mutator PyMutator(FInitializeWithTuneContext f_initialize_with_tune_context, FApply f_apply, FClone f_clone)
Create a mutator with customized methods on the python-side.
static Mutator MutateUnroll()
Create a Mutator that mutates auto unroll step.
static Mutator MutateComputeLocation()
Create a Mutator that mutates the outcome of SampleComputeLocation.
ffi::TypedFunction< void(const TuneContext &)> FInitializeWithTuneContext
The function type of InitializeWithTuneContext method.
Definition mutator.h:90
static Mutator MutateTileSize()
Create a Mutator that mutates the decision of instruction Sample-Perfect-Tile.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Mutator, ffi::ObjectRef, MutatorObj)
The mutator with customized methods on the python-side.
Definition mutator.h:148
FClone f_clone
The packed function to the Clone function.
Definition mutator.h:158
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.PyMutator", PyMutatorObj, MutatorObj)
ffi::Optional< s_tir::Trace > Apply(const s_tir::Trace &trace, LinearCongruentialEngine::TRandState *rand_state) final
Apply the mutator function to the given trace.
Mutator::FInitializeWithTuneContext FInitializeWithTuneContext
Definition mutator.h:150
FInitializeWithTuneContext f_initialize_with_tune_context
The packed function to the InitializeWithTuneContext function.
Definition mutator.h:154
Mutator::FClone FClone
Definition mutator.h:152
Mutator Clone() const final
Clone the mutator.
void InitializeWithTuneContext(const TuneContext &context) final
Initialize the design space generator with tuning context.
static void RegisterReflection()
Definition mutator.h:160
Mutator::FApply FApply
Definition mutator.h:151
FApply f_apply
The packed function to the Apply function.
Definition mutator.h:156
Managed reference to TuneContextObj.
Definition tune_context.h:99
TIR expressions.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
Random number generator. It provides a generic interface consistent with std::uniform_random_bit_gene...