tvm
builder.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_BUILDER_H_
20 #define TVM_S_TIR_META_SCHEDULE_BUILDER_H_
21 
22 #include <tvm/ffi/container/array.h>
23 #include <tvm/ffi/container/map.h>
24 #include <tvm/ffi/function.h>
25 #include <tvm/ffi/optional.h>
26 #include <tvm/ffi/reflection/registry.h>
27 #include <tvm/ffi/string.h>
28 #include <tvm/ir/module.h>
29 #include <tvm/runtime/object.h>
30 #include <tvm/runtime/tensor.h>
31 #include <tvm/target/target.h>
32 
33 namespace tvm {
34 namespace s_tir {
35 namespace meta_schedule {
36 
38 class BuilderInputNode : public runtime::Object {
39  public:
45  ffi::Optional<ffi::Map<ffi::String, runtime::Tensor>> params;
46 
47  static void RegisterReflection() {
48  namespace refl = tvm::ffi::reflection;
49  refl::ObjectDef<BuilderInputNode>()
50  .def_ro("mod", &BuilderInputNode::mod)
51  .def_ro("target", &BuilderInputNode::target)
52  .def_ro("params", &BuilderInputNode::params);
53  }
54  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.BuilderInput", BuilderInputNode,
55  runtime::Object);
56 };
57 
62 class BuilderInput : public runtime::ObjectRef {
63  public:
70  TVM_DLL explicit BuilderInput(
71  IRModule mod, Target target,
72  ffi::Optional<ffi::Map<ffi::String, runtime::Tensor>> params = std::nullopt);
74 };
75 
77 class BuilderResultNode : public runtime::Object {
78  public:
80  ffi::Optional<ffi::String> artifact_path;
82  ffi::Optional<ffi::String> error_msg;
83 
84  static void RegisterReflection() {
85  namespace refl = tvm::ffi::reflection;
86  refl::ObjectDef<BuilderResultNode>()
87  .def_ro("artifact_path", &BuilderResultNode::artifact_path)
88  .def_ro("error_msg", &BuilderResultNode::error_msg);
89  }
90  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.BuilderResult", BuilderResultNode,
91  runtime::Object);
92 };
93 
98 class BuilderResult : public runtime::ObjectRef {
99  public:
105  TVM_DLL explicit BuilderResult(ffi::Optional<ffi::String> artifact_path,
106  ffi::Optional<ffi::String> error_msg);
109 };
110 
112 class BuilderNode : public runtime::Object {
113  public:
115  virtual ~BuilderNode() = default;
121  virtual ffi::Array<BuilderResult> Build(const ffi::Array<BuilderInput>& build_inputs) = 0;
127  using FBuild = ffi::TypedFunction<ffi::Array<BuilderResult>(const ffi::Array<BuilderInput>&)>;
128 
129  static constexpr const bool _type_mutable = true;
130  TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.Builder", BuilderNode, runtime::Object);
131 };
132 
137 class Builder : public runtime::ObjectRef {
138  public:
143  explicit Builder(ObjectPtr<BuilderNode> data) : ObjectRef(data) {
144  TVM_FFI_ICHECK(data != nullptr);
145  }
153 };
154 
156 class PyBuilderNode : public BuilderNode {
157  public:
160 
161  static void RegisterReflection() {
162  namespace refl = tvm::ffi::reflection;
163  refl::ObjectDef<PyBuilderNode>().def_ro("f_build", &PyBuilderNode::f_build);
164  }
165 
166  ffi::Array<BuilderResult> Build(const ffi::Array<BuilderInput>& build_inputs) final {
167  ICHECK(f_build != nullptr) << "PyBuilder's Build method not implemented!";
168  return f_build(build_inputs);
169  }
170  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.PyBuilder", PyBuilderNode, BuilderNode);
171 };
172 
173 } // namespace meta_schedule
174 } // namespace s_tir
175 } // namespace tvm
176 
177 #endif // TVM_S_TIR_META_SCHEDULE_BUILDER_H_
Managed reference class to IRModuleNode.
Definition: module.h:256
Managed reference class to TargetNode.
Definition: target.h:192
The builder's input, containing an IRModule and the target.
Definition: builder.h:38
ffi::Optional< ffi::Map< ffi::String, runtime::Tensor > > params
Parameters for Relax build module.
Definition: builder.h:45
static void RegisterReflection()
Definition: builder.h:47
IRModule mod
The IRModule to be built.
Definition: builder.h:41
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.BuilderInput", BuilderInputNode, runtime::Object)
Target target
The target to be built for.
Definition: builder.h:43
Managed reference to BuilderInputNode.
Definition: builder.h:62
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(BuilderInput, runtime::ObjectRef, BuilderInputNode)
BuilderInput(IRModule mod, Target target, ffi::Optional< ffi::Map< ffi::String, runtime::Tensor >> params=std::nullopt)
Constructor of BuilderInput.
The abstract builder interface.
Definition: builder.h:112
TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.Builder", BuilderNode, runtime::Object)
static constexpr const bool _type_mutable
Definition: builder.h:129
virtual ffi::Array< BuilderResult > Build(const ffi::Array< BuilderInput > &build_inputs)=0
Generate the build results from build inputs.
ffi::TypedFunction< ffi::Array< BuilderResult >(const ffi::Array< BuilderInput > &)> FBuild
The function type of Build method.
Definition: builder.h:127
virtual ~BuilderNode()=default
Default destructor.
The builder's output, containing the artifact path or error message if any.
Definition: builder.h:77
static void RegisterReflection()
Definition: builder.h:84
ffi::Optional< ffi::String > artifact_path
The path to the built artifact.
Definition: builder.h:80
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.BuilderResult", BuilderResultNode, runtime::Object)
ffi::Optional< ffi::String > error_msg
The error message if any.
Definition: builder.h:82
Managed reference to BuilderResultNode.
Definition: builder.h:98
BuilderResult(ffi::Optional< ffi::String > artifact_path, ffi::Optional< ffi::String > error_msg)
Constructor of BuilderResult.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(BuilderResult, runtime::ObjectRef, BuilderResultNode)
Managed reference to BuilderNode.
Definition: builder.h:137
static Builder PyBuilder(BuilderNode::FBuild f_build)
Create a builder with customized build method on the python-side.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Builder, runtime::ObjectRef, BuilderNode)
Builder(ObjectPtr< BuilderNode > data)
Constructor from ObjectPtr<BuilderNode>.
Definition: builder.h:143
An abstract builder with customized build method on the python-side.
Definition: builder.h:156
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.PyBuilder", PyBuilderNode, BuilderNode)
FBuild f_build
The packed function to the Build function.
Definition: builder.h:159
ffi::Array< BuilderResult > Build(const ffi::Array< BuilderInput > &build_inputs) final
Generate the build results from build inputs.
Definition: builder.h:166
static void RegisterReflection()
Definition: builder.h:161
IRModule that holds the functions and type definitions.
Definition: repr_printer.h:91
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.
A device-independent managed Tensor abstraction.
Compilation target object.