Build a function with a signature, generating code for devices coupled with target information. Parameters ---------- mod : Union[PrimFunc, IRModule] The input to be built. target : Optional[Union[str, Target]] The target for compilation. pipeline : Union[Non
(
mod: PrimFunc | IRModule,
target: str | Target | None = None,
pipeline: None | str | tvm.transform.Pass = "default",
)
| 153 | |
| 154 | |
| 155 | def build( |
| 156 | mod: PrimFunc | IRModule, |
| 157 | target: str | Target | None = None, |
| 158 | pipeline: None | str | tvm.transform.Pass = "default", |
| 159 | ): |
| 160 | """Build a function with a signature, generating code for devices |
| 161 | coupled with target information. |
| 162 | |
| 163 | Parameters |
| 164 | ---------- |
| 165 | mod : Union[PrimFunc, IRModule] |
| 166 | The input to be built. |
| 167 | target : Optional[Union[str, Target]] |
| 168 | The target for compilation. |
| 169 | pipeline : Union[None, str, tvm.transform.Pass] |
| 170 | The pipeline to use for compilation. |
| 171 | |
| 172 | Returns |
| 173 | ------- |
| 174 | tvm.runtime.Module |
| 175 | A module combining both host and device code. |
| 176 | """ |
| 177 | # Convert PrimFunc to IRModule |
| 178 | if isinstance(mod, PrimFunc): |
| 179 | mod = tvm.IRModule.from_expr(mod) |
| 180 | else: |
| 181 | assert isinstance(mod, tvm.IRModule) |
| 182 | |
| 183 | # Step 0: Determine the target in environment |
| 184 | # It's used to bind the PrimFunc without target attr to serve as a default target |
| 185 | target_to_bind = Target.current() if target is None else target |
| 186 | if target_to_bind is None: |
| 187 | target_to_bind = "llvm" |
| 188 | assert target_to_bind is not None |
| 189 | target_to_bind = Target(target_to_bind) |
| 190 | |
| 191 | # Step 1: Determine the target to search for tirx pipeline |
| 192 | target = Target.current() if target is None else target |
| 193 | if target is None: |
| 194 | for func in mod.functions.values(): |
| 195 | f_target = func.attrs.get("target", None) |
| 196 | if f_target is not None: |
| 197 | target = f_target |
| 198 | break |
| 199 | if target is not None: |
| 200 | target = Target(target) |
| 201 | |
| 202 | # Step 2: Determine the host target |
| 203 | target_host = "llvm" if tvm.runtime.enabled("llvm") else "c" |
| 204 | if target is not None: |
| 205 | if target.host is not None: |
| 206 | target_host = target.host |
| 207 | elif ( |
| 208 | tvm.device(target.kind.name, 0).dlpack_device_type() == tvm.cpu(0).dlpack_device_type() |
| 209 | ): |
| 210 | target_host = target |
| 211 | target_host = Target(target_host) |
| 212 | target_to_bind = target_to_bind.with_host(target_host) |
nothing calls this directly
no test coverage detected
searching dependent graphs…