|
| 1 | +/*! |
| 2 | +* Copyright (c) 2017 by Contributors |
| 3 | +* \file build_module.h |
| 4 | +* \brief Functions for compiling ops. |
| 5 | +*/ |
| 6 | +#ifndef TVM_BUILD_MODULE_H_ |
| 7 | +#define TVM_BUILD_MODULE_H_ |
| 8 | + |
| 9 | +#include <string> |
| 10 | +#include <vector> |
| 11 | +#include "./tvm/runtime/packed_func.h" |
| 12 | +#include "./tvm/schedule_pass.h" |
| 13 | +#include "./tvm/lowered_func.h" |
| 14 | + |
| 15 | +namespace tvm { |
| 16 | + |
| 17 | +/*! |
| 18 | +* \brief Container for target device information. |
| 19 | +* Use target::llvm, target::cuda etc functions instead of constructing directly. |
| 20 | +*/ |
| 21 | +struct Target { |
| 22 | + /*! \brief The name of the target device */ |
| 23 | + std::string target_name; |
| 24 | + /*! \brief The type of the target device */ |
| 25 | + DLDeviceType device_type; |
| 26 | + /*! \brief The maximum threads that a schedule should use for this device */ |
| 27 | + int max_num_threads = 1; |
| 28 | + /*! \brief The warp size that should be used by the LowerThreadAllreduce pass */ |
| 29 | + int thread_warp_size = 1; |
| 30 | + /*! \brief Keys for this target */ |
| 31 | + std::unordered_set<std::string> keys; |
| 32 | + /*! \brief Options for this target */ |
| 33 | + std::vector<std::string> options; |
| 34 | + |
| 35 | + Target(const std::string& target_name, |
| 36 | + DLDeviceType device_type, |
| 37 | + int max_num_threads, |
| 38 | + int thread_warp_size, |
| 39 | + const std::unordered_set<std::string>& keys, |
| 40 | + const std::vector<std::string>& options) : |
| 41 | + target_name(target_name), |
| 42 | + device_type(device_type), |
| 43 | + max_num_threads(max_num_threads), |
| 44 | + thread_warp_size(thread_warp_size), |
| 45 | + keys(keys), |
| 46 | + options(options) { |
| 47 | + } |
| 48 | + |
| 49 | + /*! \return the full device string to pass to codegen::Build */ |
| 50 | + EXPORT std::string str() const; |
| 51 | + |
| 52 | + /*! |
| 53 | + * \brief Create a Target given a string |
| 54 | + * \param target_str the string to parse |
| 55 | + */ |
| 56 | + EXPORT static Target create(const std::string& target_str); |
| 57 | +}; |
| 58 | + |
| 59 | +/*! \brief This namespace provides functions to construct Target instances */ |
| 60 | +namespace target { |
| 61 | +/*! \return A target for LLVM */ |
| 62 | +EXPORT Target llvm(); |
| 63 | + |
| 64 | +/*! \return A target for CUDA */ |
| 65 | +EXPORT Target cuda(); |
| 66 | + |
| 67 | +/*! \return A target for ROCm */ |
| 68 | +EXPORT Target rocm(); |
| 69 | + |
| 70 | +/*! \return A target for Metal */ |
| 71 | +EXPORT Target metal(); |
| 72 | + |
| 73 | +/*! \return A target for rasp */ |
| 74 | +EXPORT Target rasp(); |
| 75 | + |
| 76 | +/*! \return A target for stackvm */ |
| 77 | +EXPORT Target stackvm(); |
| 78 | + |
| 79 | +} // namespace target |
| 80 | + |
| 81 | +/*! |
| 82 | +* \brief Container for build configuration options |
| 83 | +*/ |
| 84 | +struct BuildConfig { |
| 85 | + /*! |
| 86 | + * \brief The data alignment to use when constructing buffers. If this is set to |
| 87 | + * -1, then TVM's internal default will be used |
| 88 | + */ |
| 89 | + int data_alignment = -1; |
| 90 | + /*! |
| 91 | + * \brief The offset factor to use when constructing buffers. If this is set to |
| 92 | + * 0, then the offset field is not used. |
| 93 | + */ |
| 94 | + int offset_factor = 0; |
| 95 | + |
| 96 | + /*! |
| 97 | + * \brief Splitting factor for loop splitting. If this is set to zero, no splitting will be |
| 98 | + * done. Otherwise, a split will be done with this factor and the inner loop will be unrolled. |
| 99 | + */ |
| 100 | + int double_buffer_split_loop = 1; |
| 101 | + /*! \brief Threshold of number of steps in the loop to be automatically unrolled */ |
| 102 | + int auto_unroll_max_step = 0; |
| 103 | + /*! \brief The maximum nested level of loops that can be automatically unrolled */ |
| 104 | + int auto_unroll_max_depth = 8; |
| 105 | + /*! \brief The maximum extent of loop that will be unrolled */ |
| 106 | + int auto_unroll_max_extent = 0; |
| 107 | + /*! |
| 108 | + * \brief Whether to explicitly unroll the loop. If set to false, the unroll hint will |
| 109 | + * be passed to the CodeGen phase. Set to true if CodeGen supports unroll pragma. |
| 110 | + */ |
| 111 | + bool unroll_explicit = true; |
| 112 | + |
| 113 | + /*! \brief Set to true if buffer arguments do not overlap. This enables more optimization. */ |
| 114 | + bool restricted_func = true; |
| 115 | + |
| 116 | + /*! \brief Whether to detect global barrier */ |
| 117 | + bool detect_global_barrier = false; |
| 118 | + |
| 119 | + BuildConfig() { |
| 120 | + } |
| 121 | +}; |
| 122 | + |
| 123 | +/*! |
| 124 | +* \brief Build a LoweredFunc given a schedule, args and binds |
| 125 | +* \param sch The schedule to lower. |
| 126 | +* \param args The arguments to the function. |
| 127 | +* \param name The name of the lowered function. |
| 128 | +* \param binds Buffer assignments. |
| 129 | +* \param config The build configuration. |
| 130 | +* \return The lowered function. |
| 131 | +*/ |
| 132 | +EXPORT Array<LoweredFunc> lower(Schedule sch, |
| 133 | + const Array<Tensor>& args, |
| 134 | + const std::string& name, |
| 135 | + const std::unordered_map<Tensor, Buffer>& binds, |
| 136 | + const BuildConfig& config); |
| 137 | + |
| 138 | +/*! |
| 139 | +* \brief Build a device and host module for a specific target from an array of lowered functions. |
| 140 | +* \param funcs The functions to be built. |
| 141 | +* \param target The target device to build for. |
| 142 | +* \param target_host The target for building host code. If null, a suitable default will be used. |
| 143 | +* \param config The build configuration. |
| 144 | +* \return The built module. |
| 145 | +*/ |
| 146 | +EXPORT runtime::Module build(const Array<LoweredFunc>& funcs, |
| 147 | + const Target& target, |
| 148 | + Target* target_host, |
| 149 | + const BuildConfig& config); |
| 150 | + |
| 151 | +} // namespace tvm |
| 152 | + |
| 153 | +#endif // TVM_BUILD_MODULE_H_ |
0 commit comments