|
| 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 | +/*! |
| 21 | + * \file tvm/target/target.h |
| 22 | + * \brief Compilation target object. |
| 23 | + */ |
| 24 | +#ifndef TVM_TARGET_TARGET_H_ |
| 25 | +#define TVM_TARGET_TARGET_H_ |
| 26 | + |
| 27 | +#include <tvm/support/with.h> |
| 28 | +#include <tvm/node/container.h> |
| 29 | +#include <tvm/ir/expr.h> |
| 30 | + |
| 31 | +#include <string> |
| 32 | +#include <vector> |
| 33 | +#include <unordered_set> |
| 34 | + |
| 35 | +namespace tvm { |
| 36 | +/*! |
| 37 | + * \brief Compilation target. |
| 38 | + * \note Use target::llvm, target::cuda etc functions. |
| 39 | + * \sa Target |
| 40 | + */ |
| 41 | +class TargetNode : public Object { |
| 42 | + public: |
| 43 | + /*! \brief The name of the target device */ |
| 44 | + std::string target_name; |
| 45 | + /*! \brief The name of the target device */ |
| 46 | + std::string device_name; |
| 47 | + /*! \brief The type of the target device */ |
| 48 | + int device_type; |
| 49 | + /*! \brief The maximum threads that a schedule should use for this device */ |
| 50 | + int max_num_threads = 1; |
| 51 | + /*! \brief The warp size that should be used by the LowerThreadAllreduce pass */ |
| 52 | + int thread_warp_size = 1; |
| 53 | + /*! \brief Keys for this target */ |
| 54 | + Array<PrimExpr> keys_array; |
| 55 | + /*! \brief Options for this target */ |
| 56 | + Array<PrimExpr> options_array; |
| 57 | + /*! \brief Collection of imported libs */ |
| 58 | + Array<PrimExpr> libs_array; |
| 59 | + |
| 60 | + /*! \return the full device string to pass to codegen::Build */ |
| 61 | + TVM_DLL const std::string& str() const; |
| 62 | + |
| 63 | + void VisitAttrs(AttrVisitor* v) { |
| 64 | + v->Visit("target_name", &target_name); |
| 65 | + v->Visit("device_name", &device_name); |
| 66 | + v->Visit("device_type", &device_type); |
| 67 | + v->Visit("max_num_threads", &max_num_threads); |
| 68 | + v->Visit("thread_warp_size", &thread_warp_size); |
| 69 | + v->Visit("keys_array", &keys_array); |
| 70 | + v->Visit("options_array", &options_array); |
| 71 | + v->Visit("libs_array", &libs_array); |
| 72 | + } |
| 73 | + |
| 74 | + /*! \brief Get the keys for this target as a vector of string */ |
| 75 | + TVM_DLL std::vector<std::string> keys() const; |
| 76 | + |
| 77 | + /*! \brief Get the options for this target as a vector of string */ |
| 78 | + TVM_DLL std::vector<std::string> options() const; |
| 79 | + |
| 80 | + /*! \brief Get the keys for this target as an unordered_set of string */ |
| 81 | + TVM_DLL std::unordered_set<std::string> libs() const; |
| 82 | + |
| 83 | + static constexpr const char* _type_key = "Target"; |
| 84 | + TVM_DECLARE_FINAL_OBJECT_INFO(TargetNode, Object); |
| 85 | + |
| 86 | + private: |
| 87 | + /*! \brief Internal string repr. */ |
| 88 | + mutable std::string str_repr_; |
| 89 | +}; |
| 90 | + |
| 91 | +/*! |
| 92 | + * \brief Managed reference class to TargetNode. |
| 93 | + * \sa TargetNode |
| 94 | + */ |
| 95 | +class Target : public ObjectRef { |
| 96 | + public: |
| 97 | + Target() {} |
| 98 | + explicit Target(ObjectPtr<Object> n) : ObjectRef(n) {} |
| 99 | + /*! |
| 100 | + * \brief Create a Target given a string |
| 101 | + * \param target_str the string to parse |
| 102 | + */ |
| 103 | + TVM_DLL static Target Create(const std::string& target_str); |
| 104 | + /*! |
| 105 | + * \brief Get the current target context from thread local storage. |
| 106 | + * \param allow_not_defined If the context stack is empty and this is set to true, an |
| 107 | + * undefined Target will be returned. Otherwise, an empty context stack will cause a |
| 108 | + * runtime error. |
| 109 | + * \return The target that is the current context. The target may not be defined if |
| 110 | + * allow_not_defined is true. |
| 111 | + */ |
| 112 | + TVM_DLL static tvm::Target Current(bool allow_not_defined = true); |
| 113 | + |
| 114 | + const TargetNode* operator->() const { |
| 115 | + return static_cast<const TargetNode*>(get()); |
| 116 | + } |
| 117 | + |
| 118 | + using ContainerType = TargetNode; |
| 119 | + class Internal; |
| 120 | + private: |
| 121 | + // enable with syntax. |
| 122 | + friend class Internal; |
| 123 | + friend class With<Target>; |
| 124 | + /*! |
| 125 | + * \brief Push a new target context onto the thread local stack. |
| 126 | + * The Target on top of the stack is used to determine which |
| 127 | + * specialization to use when invoking a GenericFunc. |
| 128 | + */ |
| 129 | + TVM_DLL void EnterWithScope(); |
| 130 | + /*! |
| 131 | + * \brief Pop a target off the thread local context stack, |
| 132 | + * restoring the previous target as the current context. |
| 133 | + */ |
| 134 | + TVM_DLL void ExitWithScope(); |
| 135 | +}; |
| 136 | + |
| 137 | +/*! \brief This namespace provides functions to construct Target instances */ |
| 138 | +namespace target { |
| 139 | + |
| 140 | +/*! \return A target for LLVM */ |
| 141 | +TVM_DLL Target llvm(const std::vector<std::string>& options = |
| 142 | + std::vector<std::string>()); |
| 143 | + |
| 144 | +/*! \return A target for CUDA */ |
| 145 | +TVM_DLL Target cuda(const std::vector<std::string>& options = |
| 146 | + std::vector<std::string>()); |
| 147 | + |
| 148 | +/*! \return A target for ROCm */ |
| 149 | +TVM_DLL Target rocm(const std::vector<std::string>& options = |
| 150 | + std::vector<std::string>()); |
| 151 | + |
| 152 | +/*! \return A target for OpenCL */ |
| 153 | +TVM_DLL Target opencl(const std::vector<std::string>& options = |
| 154 | + std::vector<std::string>()); |
| 155 | + |
| 156 | +/*! \return A target for Metal */ |
| 157 | +TVM_DLL Target metal(const std::vector<std::string>& options = |
| 158 | + std::vector<std::string>()); |
| 159 | + |
| 160 | +/*! \return A target for rasp */ |
| 161 | +TVM_DLL Target rasp(const std::vector<std::string>& options = |
| 162 | + std::vector<std::string>()); |
| 163 | + |
| 164 | +/*! \return A target for Mali */ |
| 165 | +TVM_DLL Target mali(const std::vector<std::string>& options = |
| 166 | + std::vector<std::string>()); |
| 167 | + |
| 168 | +/*! \return A target for Intel Graphics */ |
| 169 | +TVM_DLL Target intel_graphics(const std::vector<std::string>& options = |
| 170 | + std::vector<std::string>()); |
| 171 | + |
| 172 | +/*! \return A target for stackvm */ |
| 173 | +TVM_DLL Target stackvm(const std::vector<std::string>& options = |
| 174 | + std::vector<std::string>()); |
| 175 | + |
| 176 | +/*! \return A target for external device */ |
| 177 | +TVM_DLL Target ext_dev(const std::vector<std::string>& options = |
| 178 | + std::vector<std::string>()); |
| 179 | +} // namespace target |
| 180 | +} // namespace tvm |
| 181 | +#endif // TVM_TARGET_TARGET_H_ |
0 commit comments