tvm
space_generator.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 #ifndef TVM_S_TIR_META_SCHEDULE_SPACE_GENERATOR_H_
20 #define TVM_S_TIR_META_SCHEDULE_SPACE_GENERATOR_H_
21 
22 #include <tvm/ffi/container/array.h>
23 #include <tvm/ffi/function.h>
24 #include <tvm/ffi/reflection/registry.h>
25 #include <tvm/ir/module.h>
26 #include <tvm/runtime/object.h>
31 #include <tvm/target/target.h>
32 
33 namespace tvm {
34 namespace s_tir {
35 namespace meta_schedule {
36 
37 // Forward declaration
38 class TuneContext;
39 class SpaceGenerator;
40 
77 class SpaceGeneratorNode : public runtime::Object {
78  public:
80  ffi::Optional<ffi::Array<ScheduleRule>> sch_rules;
82  ffi::Optional<ffi::Array<Postproc>> postprocs;
84  ffi::Optional<ffi::Map<Mutator, FloatImm>> mutator_probs;
85 
86  static void RegisterReflection() {
87  namespace refl = tvm::ffi::reflection;
88  refl::ObjectDef<SpaceGeneratorNode>()
89  .def_ro("sch_rules", &SpaceGeneratorNode::sch_rules)
90  .def_ro("postprocs", &SpaceGeneratorNode::postprocs)
91  .def_ro("mutator_probs", &SpaceGeneratorNode::mutator_probs);
92  }
93 
95  virtual ~SpaceGeneratorNode() = default;
96 
102  virtual void InitializeWithTuneContext(const TuneContext& context);
103 
109  virtual ffi::Array<s_tir::Schedule> GenerateDesignSpace(const IRModule& mod) = 0;
110 
115  virtual SpaceGenerator Clone() const = 0;
116 
117  static constexpr const bool _type_mutable = true;
118  TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.SpaceGenerator", SpaceGeneratorNode, Object);
119 };
120 
125 class SpaceGenerator : public runtime::ObjectRef {
126  public:
131  explicit SpaceGenerator(ObjectPtr<SpaceGeneratorNode> data) : ObjectRef(data) {
132  TVM_FFI_ICHECK(data != nullptr);
133  }
138  using FInitializeWithTuneContext = ffi::TypedFunction<void(const TuneContext&)>;
144  using FGenerateDesignSpace = ffi::TypedFunction<ffi::Array<s_tir::Schedule>(const IRModule&)>;
149  using FClone = ffi::TypedFunction<SpaceGenerator()>;
150 
151  protected:
152  SpaceGenerator() = default;
153 
154  public:
166  ffi::Optional<ffi::Array<ScheduleRule>> sch_rules,
167  ffi::Optional<ffi::Array<Postproc>> postprocs,
168  ffi::Optional<ffi::Map<Mutator, FloatImm>> mutator_probs,
169  FInitializeWithTuneContext f_initialize_with_tune_context,
170  FGenerateDesignSpace f_generate_design_space, FClone f_clone);
181  TVM_DLL static SpaceGenerator ScheduleFn(
182  ffi::Function schedule_fn, ffi::Optional<ffi::Array<ScheduleRule>> sch_rules,
183  ffi::Optional<ffi::Array<Postproc>> postprocs,
184  ffi::Optional<ffi::Map<Mutator, FloatImm>> mutator_probs);
194  ffi::Array<SpaceGenerator, void> space_generators,
195  ffi::Optional<ffi::Array<ScheduleRule>> sch_rules,
196  ffi::Optional<ffi::Array<Postproc>> postprocs,
197  ffi::Optional<ffi::Map<Mutator, FloatImm>> mutator_probs);
208  ffi::Function f_block_filter, ffi::Optional<ffi::Array<ScheduleRule>> sch_rules,
209  ffi::Optional<ffi::Array<Postproc>> postprocs,
210  ffi::Optional<ffi::Map<Mutator, FloatImm>> mutator_probs);
212 };
213 
216  public:
226 
227  static void RegisterReflection() {
228  // `f_initialize_with_tune_context` is not registered
229  // `f_generate_design_space` is not registered
230  // `f_clone` is not registered
231  namespace refl = tvm::ffi::reflection;
232  refl::ObjectDef<PySpaceGeneratorNode>();
233  }
234 
235  void InitializeWithTuneContext(const TuneContext& context) final;
236  ffi::Array<s_tir::Schedule> GenerateDesignSpace(const IRModule& mod) final;
237  SpaceGenerator Clone() const final;
238  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.PySpaceGenerator", PySpaceGeneratorNode,
240 };
241 
242 } // namespace meta_schedule
243 } // namespace s_tir
244 } // namespace tvm
245 
246 #endif // TVM_S_TIR_META_SCHEDULE_SPACE_GENERATOR_H_
Managed reference class to IRModuleNode.
Definition: module.h:256
The design space generator with customized methods on the python-side.
Definition: space_generator.h:215
void InitializeWithTuneContext(const TuneContext &context) final
Initialize the design space generator with tuning context.
SpaceGenerator::FClone FClone
Definition: space_generator.h:219
FInitializeWithTuneContext f_initialize_with_tune_context
The packed function to the InitializeWithTuneContext function.
Definition: space_generator.h:221
FClone f_clone
The packed function to the Clone function.
Definition: space_generator.h:225
SpaceGenerator::FInitializeWithTuneContext FInitializeWithTuneContext
Definition: space_generator.h:217
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.PySpaceGenerator", PySpaceGeneratorNode, SpaceGeneratorNode)
SpaceGenerator::FGenerateDesignSpace FGenerateDesignSpace
Definition: space_generator.h:218
SpaceGenerator Clone() const final
Clone the space generator.
FGenerateDesignSpace f_generate_design_space
The packed function to the GenerateDesignSpace function.
Definition: space_generator.h:223
static void RegisterReflection()
Definition: space_generator.h:227
ffi::Array< s_tir::Schedule > GenerateDesignSpace(const IRModule &mod) final
Generate design spaces given a module.
The abstract class for design space generation.
Definition: space_generator.h:77
static constexpr const bool _type_mutable
Definition: space_generator.h:117
ffi::Optional< ffi::Map< Mutator, FloatImm > > mutator_probs
The probability of using certain mutator.
Definition: space_generator.h:84
virtual ~SpaceGeneratorNode()=default
Default destructor.
static void RegisterReflection()
Definition: space_generator.h:86
virtual ffi::Array< s_tir::Schedule > GenerateDesignSpace(const IRModule &mod)=0
Generate design spaces given a module.
ffi::Optional< ffi::Array< ScheduleRule > > sch_rules
The schedule rules.
Definition: space_generator.h:80
ffi::Optional< ffi::Array< Postproc > > postprocs
The postprocessors.
Definition: space_generator.h:82
virtual void InitializeWithTuneContext(const TuneContext &context)
Initialize the design space generator with tuning context.
virtual SpaceGenerator Clone() const =0
Clone the space generator.
TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.SpaceGenerator", SpaceGeneratorNode, Object)
Managed reference to SpaceGeneratorNode.
Definition: space_generator.h:125
ffi::TypedFunction< SpaceGenerator()> FClone
The function type of Clone method.
Definition: space_generator.h:149
SpaceGenerator(ObjectPtr< SpaceGeneratorNode > data)
Constructor from ObjectPtr<SpaceGeneratorNode>.
Definition: space_generator.h:131
ffi::TypedFunction< void(const TuneContext &)> FInitializeWithTuneContext
The function type of InitializeWithTuneContext method.
Definition: space_generator.h:138
static SpaceGenerator PySpaceGenerator(ffi::Optional< ffi::Array< ScheduleRule >> sch_rules, ffi::Optional< ffi::Array< Postproc >> postprocs, ffi::Optional< ffi::Map< Mutator, FloatImm >> mutator_probs, FInitializeWithTuneContext f_initialize_with_tune_context, FGenerateDesignSpace f_generate_design_space, FClone f_clone)
Create a design space generator with customized methods on the python-side.
ffi::TypedFunction< ffi::Array< s_tir::Schedule >(const IRModule &)> FGenerateDesignSpace
The function type of GenerateDesignSpace method.
Definition: space_generator.h:144
static SpaceGenerator SpaceGeneratorUnion(ffi::Array< SpaceGenerator, void > space_generators, ffi::Optional< ffi::Array< ScheduleRule >> sch_rules, ffi::Optional< ffi::Array< Postproc >> postprocs, ffi::Optional< ffi::Map< Mutator, FloatImm >> mutator_probs)
Create a design space generator that is union of multiple design space generators.
static SpaceGenerator ScheduleFn(ffi::Function schedule_fn, ffi::Optional< ffi::Array< ScheduleRule >> sch_rules, ffi::Optional< ffi::Array< Postproc >> postprocs, ffi::Optional< ffi::Map< Mutator, FloatImm >> mutator_probs)
Create a design space generator with customized schedule function.
static SpaceGenerator PostOrderApply(ffi::Function f_block_filter, ffi::Optional< ffi::Array< ScheduleRule >> sch_rules, ffi::Optional< ffi::Array< Postproc >> postprocs, ffi::Optional< ffi::Map< Mutator, FloatImm >> mutator_probs)
Create a design space generator that generates design spaces by applying schedule rules to blocks in ...
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(SpaceGenerator, ObjectRef, SpaceGeneratorNode)
Managed reference to TuneContextNode.
Definition: tune_context.h:99
IRModule that holds the functions and type definitions.
Definition: repr_printer.h:91
tvm::relax::Function Function
Definition: transform.h:38
tvm::PrimExpr mod(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:308
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
A managed object in the TVM runtime.
Compilation target object.