tvm
Loading...
Searching...
No Matches
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/ir/prim/expr.h>
30#include <tvm/runtime/tensor.h>
31#include <tvm/target/target.h>
32
33namespace tvm {
34namespace s_tir {
35using namespace tvm::prim;
36namespace meta_schedule {
37
39class BuilderInputNode : public ffi::Object {
40 public:
46 ffi::Optional<ffi::Map<ffi::String, runtime::Tensor>> params;
47
48 static void RegisterReflection() {
49 namespace refl = tvm::ffi::reflection;
50 refl::ObjectDef<BuilderInputNode>()
51 .def_ro("mod", &BuilderInputNode::mod)
52 .def_ro("target", &BuilderInputNode::target)
53 .def_ro("params", &BuilderInputNode::params);
54 }
55 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.BuilderInput", BuilderInputNode,
56 ffi::Object);
57};
58
63class BuilderInput : public ffi::ObjectRef {
64 public:
72 IRModule mod, Target target,
73 ffi::Optional<ffi::Map<ffi::String, runtime::Tensor>> params = std::nullopt);
75};
76
78class BuilderResultNode : public ffi::Object {
79 public:
81 ffi::Optional<ffi::String> artifact_path;
83 ffi::Optional<ffi::String> error_msg;
84
85 static void RegisterReflection() {
86 namespace refl = tvm::ffi::reflection;
87 refl::ObjectDef<BuilderResultNode>()
88 .def_ro("artifact_path", &BuilderResultNode::artifact_path)
89 .def_ro("error_msg", &BuilderResultNode::error_msg);
90 }
91 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.BuilderResult", BuilderResultNode,
92 ffi::Object);
93};
94
99class BuilderResult : public ffi::ObjectRef {
100 public:
106 TVM_DLL explicit BuilderResult(ffi::Optional<ffi::String> artifact_path,
107 ffi::Optional<ffi::String> error_msg);
109};
110
112class BuilderNode : public ffi::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, ffi::Object);
131};
132
137class Builder : public ffi::ObjectRef {
138 public:
143 explicit Builder(ffi::ObjectPtr<BuilderNode> data) : ffi::ObjectRef(data) {
144 TVM_FFI_ICHECK(data != nullptr);
145 }
153};
154
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 TVM_FFI_ICHECK(f_build != nullptr) << "PyBuilder's Build method not implemented!";
168 return f_build(build_inputs);
169 }
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:255
Managed reference class to TargetNode.
Definition target.h:134
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
The builder's input, containing an IRModule and the target.
Definition builder.h:39
ffi::Optional< ffi::Map< ffi::String, runtime::Tensor > > params
Parameters for Relax build module.
Definition builder.h:46
static void RegisterReflection()
Definition builder.h:48
IRModule mod
The IRModule to be built.
Definition builder.h:42
Target target
The target to be built for.
Definition builder.h:44
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.BuilderInput", BuilderInputNode, ffi::Object)
Managed reference to BuilderInputNode.
Definition builder.h:63
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(BuilderInput, ffi::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
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.
TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.Builder", BuilderNode, ffi::Object)
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:78
static void RegisterReflection()
Definition builder.h:85
ffi::Optional< ffi::String > artifact_path
The path to the built artifact.
Definition builder.h:81
ffi::Optional< ffi::String > error_msg
The error message if any.
Definition builder.h:83
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.BuilderResult", BuilderResultNode, ffi::Object)
Managed reference to BuilderResultNode.
Definition builder.h:99
BuilderResult(ffi::Optional< ffi::String > artifact_path, ffi::Optional< ffi::String > error_msg)
Constructor of BuilderResult.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(BuilderResult, ffi::ObjectRef, BuilderResultNode)
Managed reference to BuilderNode.
Definition builder.h:137
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Builder, ffi::ObjectRef, BuilderNode)
static Builder PyBuilder(BuilderNode::FBuild f_build)
Create a builder with customized build method on the python-side.
Builder(ffi::ObjectPtr< BuilderNode > data)
Constructor from ffi::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
TIR expressions.
IRModule that holds the functions and type definitions.
Definition builtin.h:25
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
A device-independent managed Tensor abstraction.
Compilation target object.