|
| 1 | + |
| 2 | +/* |
| 3 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 4 | + * or more contributor license agreements. See the NOTICE file |
| 5 | + * distributed with this work for additional information |
| 6 | + * regarding copyright ownership. The ASF licenses this file |
| 7 | + * to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance |
| 9 | + * with the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, |
| 14 | + * software distributed under the License is distributed on an |
| 15 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | + * KIND, either express or implied. See the License for the |
| 17 | + * specific language governing permissions and limitations |
| 18 | + * under the License. |
| 19 | + */ |
| 20 | +#ifndef TVM_META_SCHEDULE_BUILDER_H_ |
| 21 | +#define TVM_META_SCHEDULE_BUILDER_H_ |
| 22 | + |
| 23 | +#include <tvm/ir/module.h> |
| 24 | +#include <tvm/target/target.h> |
| 25 | + |
| 26 | +namespace tvm { |
| 27 | +namespace meta_schedule { |
| 28 | + |
| 29 | +/*! \brief The builder's input. */ |
| 30 | +class BuilderInputNode : public runtime::Object { |
| 31 | + public: |
| 32 | + /*! \brief The IRModule to be built. */ |
| 33 | + IRModule mod; |
| 34 | + /*! \brief The target to be built for. */ |
| 35 | + Target target; |
| 36 | + |
| 37 | + void VisitAttrs(tvm::AttrVisitor* v) { |
| 38 | + v->Visit("mod", &mod); |
| 39 | + v->Visit("target", &target); |
| 40 | + } |
| 41 | + |
| 42 | + static constexpr const char* _type_key = "meta_schedule.BuilderInput"; |
| 43 | + TVM_DECLARE_FINAL_OBJECT_INFO(BuilderInputNode, runtime::Object); |
| 44 | +}; |
| 45 | + |
| 46 | +/*! |
| 47 | + * \brief Managed reference to BuilderInputNode |
| 48 | + * \sa BuilderInputNode |
| 49 | + */ |
| 50 | +class BuilderInput : public runtime::ObjectRef { |
| 51 | + public: |
| 52 | + /*! |
| 53 | + * \brief Constructor of BuilderInput. |
| 54 | + * \param mod The IRModule to be built. |
| 55 | + * \param target The target to be built for. |
| 56 | + */ |
| 57 | + TVM_DLL explicit BuilderInput(IRModule mod, Target target); |
| 58 | + TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(BuilderInput, runtime::ObjectRef, BuilderInputNode); |
| 59 | +}; |
| 60 | + |
| 61 | +/*! \brief The builder's output. */ |
| 62 | +class BuilderResultNode : public runtime::Object { |
| 63 | + public: |
| 64 | + /*! \brief The path to the built artifact. */ |
| 65 | + Optional<String> artifact_path; |
| 66 | + /*! \brief The error message if any. */ |
| 67 | + Optional<String> error_msg; |
| 68 | + |
| 69 | + void VisitAttrs(tvm::AttrVisitor* v) { |
| 70 | + v->Visit("artifact_path", &artifact_path); |
| 71 | + v->Visit("error_msg", &error_msg); |
| 72 | + } |
| 73 | + |
| 74 | + static constexpr const char* _type_key = "meta_schedule.BuilderResult"; |
| 75 | + TVM_DECLARE_FINAL_OBJECT_INFO(BuilderResultNode, runtime::Object); |
| 76 | +}; |
| 77 | + |
| 78 | +/*! |
| 79 | + * \brief Managed reference to BuilderResultNode |
| 80 | + * \sa BuilderResultNode |
| 81 | + */ |
| 82 | +class BuilderResult : public runtime::ObjectRef { |
| 83 | + public: |
| 84 | + /*! |
| 85 | + * \brief Constructor of BuilderResult. |
| 86 | + * \param artifact_path The path to the built artifact. |
| 87 | + * \param error_msg The error message if any. |
| 88 | + */ |
| 89 | + TVM_DLL explicit BuilderResult(Optional<String> artifact_path, Optional<String> error_msg); |
| 90 | + TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(BuilderResult, runtime::ObjectRef, BuilderResultNode); |
| 91 | +}; |
| 92 | + |
| 93 | +/*! \brief The abstract builder interface. */ |
| 94 | +class BuilderNode : public runtime::Object { |
| 95 | + public: |
| 96 | + /*! \brief Default destructor */ |
| 97 | + virtual ~BuilderNode() = default; |
| 98 | + /*! |
| 99 | + * \brief Generate the build results from build inputs. |
| 100 | + * \param build_inputs The inputs to be built. |
| 101 | + * \return The build results. |
| 102 | + */ |
| 103 | + virtual Array<BuilderResult> Build(const Array<BuilderInput>& build_inputs) = 0; |
| 104 | + /*! |
| 105 | + * \brief The function type of `Build` method. |
| 106 | + * \param build_inputs The inputs to be built. |
| 107 | + * \return The build results. |
| 108 | + */ |
| 109 | + using FBuild = runtime::TypedPackedFunc<Array<BuilderResult>(const Array<BuilderInput>&)>; |
| 110 | + |
| 111 | + static constexpr const char* _type_key = "meta_schedule.Builder"; |
| 112 | + TVM_DECLARE_BASE_OBJECT_INFO(BuilderNode, runtime::Object); |
| 113 | +}; |
| 114 | + |
| 115 | +/*! |
| 116 | + * \brief Managed reference to BuilderNode |
| 117 | + * \sa BuilderNode |
| 118 | + */ |
| 119 | +class Builder : public runtime::ObjectRef { |
| 120 | + public: |
| 121 | + /*! |
| 122 | + * \brief Create a builder with customized build method on the python-side. |
| 123 | + * \param f_build The packed function to the `Build` function.. |
| 124 | + * \return The Builder created. |
| 125 | + */ |
| 126 | + static Builder PyBuilder(BuilderNode::FBuild f_build); |
| 127 | + TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(Builder, runtime::ObjectRef, BuilderNode); |
| 128 | +}; |
| 129 | + |
| 130 | +/*! \brief An abstract builder with customized build method on the python-side. */ |
| 131 | +class PyBuilderNode : public BuilderNode { |
| 132 | + public: |
| 133 | + /*! \brief The packed function to the `Build` function. */ |
| 134 | + FBuild f_build; |
| 135 | + |
| 136 | + void VisitAttrs(tvm::AttrVisitor* v) { |
| 137 | + // `f_build` is not visited |
| 138 | + } |
| 139 | + |
| 140 | + Array<BuilderResult> Build(const Array<BuilderInput>& build_inputs) final { |
| 141 | + return f_build(build_inputs); |
| 142 | + } |
| 143 | + |
| 144 | + static constexpr const char* _type_key = "meta_schedule.PyBuilder"; |
| 145 | + TVM_DECLARE_FINAL_OBJECT_INFO(PyBuilderNode, BuilderNode); |
| 146 | +}; |
| 147 | + |
| 148 | +} // namespace meta_schedule |
| 149 | +} // namespace tvm |
| 150 | + |
| 151 | +#endif // TVM_META_SCHEDULE_BUILDER_H_ |
0 commit comments