35 Commits

Author SHA1 Message Date
Tianqi Chen 8cbb0b11bb [CMAKE][REFACTOR] Split libtvm.so into libtvm_runtime.so and libtvm_compiler.so (#19444)
## Motivation

Historically TVM ships a single monolithic `libtvm.so` that bundles both
the
runtime and the compiler/LLVM-heavy code paths. Deployment scenarios
that only
need the runtime end up paying the full compiler footprint (LLVM-static
dominates
the binary size), and the layout makes it awkward to install the project
under a
single Python package directory the way
`tvm_ffi`/`libinfo.load_lib_ctypes`
expects.

This PR splits the single shared library into two:

- `libtvm_runtime.so` — runtime-only symbols (loaded `RTLD_GLOBAL`).
- `libtvm_compiler.so` — compiler / LLVM / codegen, links
`libtvm_runtime.so`
  publicly (loaded `RTLD_LOCAL`).

## Target restructure

- New CMake target `tvm_compiler` replaces the old `tvm` SHARED target.
- `tvm_compiler` depends on `tvm_runtime` via `target_link_libraries(...
PUBLIC tvm_runtime)`,
so anything that linked the old `tvm` now picks up the runtime
transitively.
- `tvm_libinfo_objs` (build-info TU) moved from `tvm_runtime` into
`tvm_compiler`
  — it is compiler-side metadata and the runtime no longer needs it.
- All `target_link_libraries` / `target_compile_*` /
`set_target_properties` /
  `tvm_ffi_add_apple_dsymutil` callsites have been rewired.
- The separate `libtvm_allvisible.so` target is **removed** (was only
consumed
  by cpptests). Cpptests with private-symbol deps are deleted; remaining
  cpptests now link directly against `libtvm_compiler.so` /
`libtvm_runtime.so`. `src/support/hexdump.cc` is folded into the header.
- `BUILD_DUMMY_LIBTVM` and the `BUILD_FOR_HEXAGON + USE_HEXAGON_GTEST`
  cpp-test wiring are removed.

## Output and install layout

- All artifacts now go to `build/lib/` (was `build/`):
  - `build/lib/libtvm_runtime.so`
  - `build/lib/libtvm_compiler.so`
- Install layout is now `<package>/lib/` so
`tvm_ffi.libinfo.load_lib_ctypes`
  with `package="tvm"` finds the libs in the wheel.
- CI Jenkins stash paths and `apps/hexagon_*` paths updated to the new
  `build/lib/...` location.

## Python loader change

`python/tvm/base.py` now resolves the libs directly via a small
`package_lib_paths()` helper in `python/tvm/libinfo.py` (anchored on
`python/tvm/__file__`, returning the wheel `lib/`,
`<worktree>/build/lib`, and
`<worktree>/lib` candidates). Module-level `_LIB_RUNTIME`, `_LIB`, and
`_RUNTIME_ONLY` are set inline at import time:

- `libtvm_runtime.{so,dylib,dll}` loaded `RTLD_GLOBAL`.
- `libtvm_compiler.{so,dylib,dll}` loaded `RTLD_LOCAL`.
- `TVM_USE_RUNTIME_LIB` (parsed strictly: `1`/`true`/`yes`) selects
  runtime-only at the loader level.
- When the compiler lib is absent, `_RUNTIME_ONLY` is set to True
  automatically and `_LIB is _LIB_RUNTIME`.

## Non-obvious build-integration fixes

Three issues surfaced once both libs are loaded into the same process
and are
worth calling out:

1. **`fpA_intB_gemm` double-registration.** `fpA_intB_gemm_tvm` is an
OBJECT
library that registers a global `fastertransformer.gemm_fp16_int` at
static
   init. Linking it into both `tvm_runtime` and `tvm_compiler` made the
registration run twice and trip the duplicate-registration check. Fix:
link
it (and the other runtime-only externals — `flash_attn`, NCCL, NVSHMEM,
RCCL) only into `tvm_runtime`. `tvm_compiler` picks them up via the
PUBLIC
   `tvm_runtime` link.

2. **`-Wl,--no-as-needed` for minrpc.** `python/tvm/rpc/minrpc.py`
defaults
   to `runtime="libtvm_runtime"` and passes `-Wl,--no-as-needed` so the
   runtime static initializers actually run in the spawned minrpc binary
   (without it, the linker drops the lib because no symbol is referenced
   directly from the minrpc TU). minrpc does **not** link
   `libtvm_compiler.so`.

3. **`testing.GetShape{Elem,Size}` moved to runtime.** Those two test
helpers
(the only `testing.*` symbols the minrpc test exercises) were registered
in
`src/support/ffi_testing.cc` (compiler-side). They are now registered in
`src/runtime/rpc/testing.cc` under `rpc.testing.GetShape{Elem,Size}` so
   the minrpc server binary — runtime-only — can resolve them.

## Deprecations and breaking changes

- `BUILD_DUMMY_LIBTVM` is **removed** (option, libinfo entry, and CMake
  wiring). Downstream consumers that built the dummy variant should link
  `libtvm_runtime.so` directly.
- **Breaking change for downstream consumers** that read `libtvm.so` by
name:
there is no longer a `libtvm.so`. Replace with `libtvm_compiler.so`
(full)
or `libtvm_runtime.so` (runtime-only). The Vulkan device comment and a
few
  test/CI comments have been updated accordingly.
- `libtvm_allvisible.so` is **removed**. Cpptests that depended on
private
out-of-line symbols have been deleted; the remaining cpp-test contract
is
  documented as "public API or private header-only API only" (see
  `tests/cpp/`).
- `tests/cpp-runtime/` (Hexagon + OpenCL backend tests) is **removed**
until
  TVM moves to a plugin-mode backend architecture where each backend can
  ship its own test harness with its own visibility scope.

## Tested

- `ninja` build: `build/lib/libtvm_runtime.so`,
`build/lib/libtvm_compiler.so`;
  no `build/libtvm.so`, no `build/lib/libtvm_allvisible.so`.
  `ldd build/lib/libtvm_compiler.so` links `libtvm_runtime.so`,
  `libtvm_ffi.so`, `libfpA_intB_gemm.so`, `libflash_attn.so`.
- `ldd build/cpptest`: only `libtvm_compiler.so` + `libtvm_runtime.so` +
  `libtvm_ffi.so` (no `libtvm_allvisible.so`).
- `./build/cpptest`: 144 / 144 tests pass across 29 suites.
- Smoke imports: full and `TVM_USE_RUNTIME_LIB=1` — both pass.
`TVM_USE_RUNTIME_LIB=0` correctly disables runtime-only mode (strict
parse).
- `tests/python/all-platform-minimal-test`: 75 passed, 77 skipped.
- `tests/python/runtime/`: 81 passed, 2 skipped (incl.
`test_rpc_return_remote_object` exercising the minrpc executable
end-to-end
  via `rpc.testing.GetShape{Elem,Size}`).
- `tests/python/relax/test_vm_*.py`: 150 passed, 3 deselected
(`test_vm_multi_device.py` requires 3+ GPUs; host has 2 — env, not
regression),
  2 xfailed.
- `tests/python/tirx-base/`: 273 passed, 2 skipped.
- `pre-commit` on edited files: green.

Closes #19443.
2026-04-26 07:37:01 -04:00
Tianqi Chen ff29817d57 [LLVM] Bump minimum LLVM version to 15 (#18853)
## Summary

Bump the minimum required LLVM version from 6.0 to 15.0, removing all
compatibility code for older LLVM versions.

- Update CMake minimum version checks in `FindLLVM.cmake` and
`LLVM.cmake`
- Remove ~90 dead `#if TVM_LLVM_VERSION` preprocessor branches across 15
source files
- Update documentation references in `from_source.rst` and
`config.cmake`
- 531 lines of dead code removed, no behavioral changes

All changes are strictly dead-code removal — no live code paths were
modified.

## Test plan

- [x] Build with LLVM 15 (`-DUSE_LLVM=ON`): passed
- [x] `test_target_codegen_llvm.py`: 40/40 passed
- [x] `pre-commit run --all-files`: passed
2026-02-28 10:50:30 -05:00
Tianqi Chen e5f483cd5f [REFACTOR][NODE] Remove node redirect headers (#18829) 2026-02-27 06:36:57 -05:00
Tianqi Chen f533d0b3c1 [REFACTOR] Migrate CHECK macros to tvm-ffi ones (#18803) 2026-02-21 09:00:44 -05:00
Tianqi Chen 0460d82169 [REFACTOR][TARGET] Cleanup target config (#18788) 2026-02-17 15:52:16 -05:00
Tianqi Chen 2030db36e4 [REFACTOR][TARGET] Phase out legacy target string in favor of json (#18785)
This PR phases out legacy target string format in favor of the json
style format that is more well formed. It also simplfies our overall
code in handling multiple formats.
2026-02-16 16:21:35 -05:00
Tianqi Chen 349df2bc26 [FFI][REFACTOR] Cleanup namespace (#18280)
* [FFI][REFACTOR] Cleanup namespace

This PR cleansup the namespace to ensure all ffi classes
are accessed through ffi:: namespace.
It will helps to cleanup the ffi package before isolation.

* fix hexagon
2025-09-08 08:58:30 -04:00
Balint Cristian 6790af8322 [LLVM] Fixes up to the latest LLVM21 (#18204)
This PR fix TVM use with the latest LLVM version 21.

- At this time LLVM21 is available as a release candidate.
- Double checks for backward compatibility down to LLVM10
2025-08-12 04:59:23 +03:00
Balint Cristian 789e0b87a9 [LLVM][CPPTEST] Small fixes for LLVM >= 20 (#18202)
This PR updates cpptest to run on LLVM >= 20
2025-08-10 18:27:34 +03:00
Tianqi Chen a531d170b9 [REFACTOR] Phase out relay c++ components (#17660)
* cleanup relay c++

* [REFACTOR] Phase out relay c++ components

This PR phases out the relay C++ components and
simplifies the overall codegen runtime logic.

---------

Co-authored-by: Siyuan Feng <hzfengsy@sjtu.edu.cn>
2025-02-17 22:21:37 +08:00
Luke Hutton c20cdafcbc [SME] Target parser support for SME (#16794)
This commit adds support for recognising when the SME architecture
feature is available based on the target string. A python user can
use `target.features.has_sme` to check availability.
2024-04-02 13:54:15 +01:00
Luke Hutton 726a141649 [Target] Use LLVM target parser for determining Arm(R) A-Profile Architecture features (#16425)
Currently, target features are determined by a set of fixed checks on
the target string. This works well for checking support of a small
number of simple features, but it doesn't scale. Some problems include:
- There are many non-trivial conditions for which a feature may(not) be
  available. It is easy to miss these with the current implementation.
- The inclusion of some features in a target string can imply other
  features. For example, "+sve" implies "+neon". This currently isn't
  taken into account.
- The tests in tests/cpp/target/parsers/aprofile_test.c suggest that
  targets such as "llvm -mcpu=cortex-a+neon" and "llvm -mattr=+noneon"
  are supported target strings. The features will be correctly parsed in
  TVM, however, they are not valid in LLVM. Therefore, it's possible
  that TVM and LLVM have different understanding of the features
  available.

This commit uses the more robust LLVM target parser to determine support
for the features in TVM. It leverages previous infrastructure added to
TVM for obtaining a list of all supported features given an input
target, and uses this to check the existance of certain features we're
interested in. It should be trivial to grow this list over time. As a
result of this change, the problems mentioned above are solved.

In the current form, this commit drops support for target strings such
as "llvm -mcpu=cortex-a+neon" and "llvm -mattr=+noneon". A scan of the
codebase suggests this functionality is not in use (only in test cases).
Should we feel the need to support them, or have a smoother migration
for downstream users of TVM we can add a translator to the parser to
convert these into LLVM compatible targets.
2024-03-27 15:53:46 +00:00
Andrei Hutu e53a8bcfb9 [TOPI][Target] Add fp16 SIMD support for conv2d on arm_cpu targets (#16383)
Optimised fp16 conv2d matrix tiling for Arm(R) Neon(TM) instructions and exposed `+fullfp16` as a target feature for Arm(R) Cortex(R) A-Profile CPUs.

Also, a target test was added to `cpptest` for Arm(R) Cortex(R) A-Profile CPUs which checks that the `has_fp16_simd` flag is set exclusively when the user explicitly passes the `+fullfp16` or `+sve` attributes and a supporting architecture version at target creation.
2024-01-11 09:17:35 +00:00
Jack Frankland 14be4bfc0d [TOPI][Target] Add SVE specific convolution (#14483)
* Expose SVE as a target feature for Arm(R) Cortex(R) A-Profile CPUs.
* Update the compute definition of `conv2d_spatial_pack_nhwc` to defer
  to the LLVM backend for vectorization when compiling on an SVE enabled
  target for data tensors with unit width and height since this has been
  shown to be performant for wide vector architectures.
* Add a target test to `cpptest` for Arm(R) Cortex(R) A-Profile CPUs
  which tests that the `has_sve` flag is set when the user explicitly
  passes the `"+sve"` attribute on target creation. Because SVE is
  optional on architecture versions 8.0 and later the test checks
  whether it is optionally set.
* Make some pre-existing read only target properties constant.
2023-05-17 14:37:57 +01:00
AndrewZhaoLuo 5878f6090b [Target] Make key=arm_cpu --> key=arm_cpu,cpu on AArch64 (#13775)
* arm cpu is cpu

* init commit

* fix test
2023-01-12 16:12:56 -08:00
fPecc 5a58c581f5 Added macro generation in MLF export (#12789)
The generated MLF header files for each module contain the struct definition to use as input and outputs to call the generated function. If we want to call this tvmgen_default_run, we need to allocate space (statically or dynamically) for the input and output tensors. This generates macros that define the size of each input and output in bytes, this allows us to reference this new macros to statically or dynamically allocate vectors to store the inputs and outputs of the tvmgen_default_run function.


Co-authored-by: Federico Peccia <peccia@fzi.de>
Co-authored-by: Christopher Sidebottom <chris.sidebottom@arm.com>
2022-12-07 11:11:00 +00:00
Christopher Sidebottom 308c20a4ca [Target] Add Target Parser for Arm(R) Cortex(R) A-Profile CPUs (#12454)
This implements an initial Target Parser which aims to consolidate architecture feature detection from a few different places:
* https://github.com/apache/tvm/blob/d2db9cb0d839e32778f461b77e59f6418282a511/python/tvm/topi/arm_cpu/arm_utils.py#L24-L70
* https://github.com/apache/tvm/blob/02fbaf0ed9120a8f95155e63de42459f230584aa/python/tvm/relay/qnn/op/legalizations.py#L350-L359
* https://github.com/apache/tvm/blob/b542724873140bb051492530d97a78b9b7b7983d/python/tvm/relay/op/strategy/arm_cpu.py#L232

A further patch will remove all of the above and replace usages with the `.features` map.
2022-10-20 10:24:44 +01:00
Krzysztof Parzyszek 6def53aeaa [Target] Only append default keys if target doesn't have any yet (#12474)
* [Target] Only append default keys if target doesn't have any yet

This allows target parsers to provide their own target keys. Without this
change, the default keys would always be appended, which may or may not
be desirable.

* Add "cpu" to ARM CPU keys

* Add "cpu" to the keys in the mprofile target parser

* Restore the mprofile cpptest, since the "cpu" key is back

* So the -device attribute is actually needed...
2022-08-18 18:11:23 -05:00
Christopher Sidebottom 1de32b587b [Target] Add Target Parser for Arm(R) Cortex(R) M-Profile CPUs (#12319)
This implements an initial Target Parser which uses the same logic as
the CMSIS-NN compiler flags to update the features and keys of the `c`
and `llvm` `Target`s.

Refactoring of the CMSIS-NN logic will be in a separate patch.
2022-08-12 16:31:34 +01:00
Mark Shields 0d70f690ea [Relay] Allow partial virtual device annotations. (#12107)
* [Relay] Allow partial virtual device annotations.

Previously CompilationConfig::CanonicalVirtualDevice required
the argument virtual device to contain a device type. However
now that virtual devices may contain memory scopes that's
unnecessarily strict.

With this change it is possible to write virtual device
annotations with just memory scopes, and let PlanDevices
flow those constraints along with the usual device constraints.

* - Make sure CanonicalVirtualDevice reuses FullyUnconstrained
2022-07-15 14:34:28 -07:00
Mark Shields 8f208adae2 [Relay] Allow Primitive functions to carry virtual device annotations in PlanDevices (#12095)
* [Relay] Allow Primitive function to carry virtual device annotations in PlanDevices

Previously Primitive=1 functions not analyzed and calls to such were completely
unconstrained. With this change at least any virtual device annotation on the function
are respected and accounted for in calls, even though the body is not analyzed.

This may help with piggy-backing on PlanDevices for doing memory scope analysis, since
it is now possible to express cross-scope functions on Primitive functions. However
I believe there are other issues to deal with in addition to this one.

* - comments

* - also canonicalize targets

When including virtual device annotations in test relay programs the
annotation will typically use a target which was used as an input to
the make_compilation_config helper, but due to various canonicalization
make not be pointer equal to the final structurally equal target which ends
up inside the constructed CompilationConfig. However VirtualDevices use
pointer equality when comparing their target field.

So make sure the notion of CanonicalVirtualDevice also accounts for canonical
targets.

* - update unit test to reflect the Ardreno example

* - trivial cleanup
2022-07-15 08:27:35 -07:00
Dmitriy Smirnov 08723d0ce6 [usmp] U3 use case (#11015)
* U3

Change-Id: Ibc088f19ad1dc9466fc368f8523baa30ee88b7d0

* addressed upstream comments

* Unit test added

Added unit test for InterfaceCNode::EmitConstantPool method
2022-06-28 12:02:21 +01:00
Dmitriy Smirnov c80da037d8 [USMP] Adding support for U1 usecase for constant pools (#10189)
* [TIR.Constant] U1 usecase

Constants are now aggregated into one struct and initialized in default_lib0.c
file

Change-Id: I34d61f8139c8a92c06944fe990ba892a660476fd

Unit test fixed

Change-Id: I436e7b6d6b3064b3f8bbfbb048d4296b63a6b69c

* Refactored

Addressed:
* PoolInfo splitted to WorkspacePoolInfo and ConstantPoolInfo
* workspace_byte_alignment moved to ExecutorCodegenMetadata
* getModuleAlignment -> GetModuleAlignment
* GenerateInternalWorkspaceBuffers refactored
* reverted format change of src/tir/transforms/legalize_packed_calls.cc
* addressed comments for src/tir/usmp/analysis/extract_buffer_info.cc
* removed commented code from include/tvm/tir/usmp/utils.h

Change-Id: I7d1b32884b0e5992e2e00c7838c85e425d9c25fd

* more unit test fixes

Change-Id: I573a05fa1cb4037ae83691f7dff2c2724b1d7700

* More refactoring and unit test fixes

Added ConstantMemoryPools

Change-Id: If1e391c631575980564bca790ba33748c82d907f

* bugfix

Change-Id: Iacc7a9d734a505dfa0d8d32d23ea3f57e6de8582

* refactoring. added constant_alignment

added constant_alignment
unit tests updated

Change-Id: I378193cb9e675e352c61d96ff4e09655090053e1

* unit-test bugix

Change-Id: Ia4411d59c4a376c01326fed366cdb196a432899e

* unit test fix

Change-Id: Ia2077bdeb1d2c6c9827eeef90ab410ae31b8c4a4

* Added support for c++ runtime

* refactored

* renamed pools and consts

renamed pools and consts to workspace_pools and constant_pools

* addressed upstream comments

* addressed upstream comments-2

* addressed upstream comments-3
2022-06-22 16:41:53 -07:00
Manupa Karunaratne 119afda634 [microNPU] add E2E tests with cascader wo striping (#11410)
This commit adds end-to-end tests using the cascader
w/o striping. It needed few adjustments to the order
in which the arugments are provided to the entry point
function in AoT when both memory pools and devices
are present.

Change-Id: I37e04afd635add895e317586f628a62cae75f3fa
2022-05-30 16:31:23 +01:00
Mark Shields d1467777c6 Finish support for list-of-targets (#11382)
* Finish support for list-of-targets

This finishes the work started in https://github.com/apache/tvm/pull/11173 to support
'external codegen' targets in the N build-like API surfaces.

 - It turns out it's ok if a build is given only a single 'external codegen' target, so remove that check
   in CompilationConfig::Init. When Collage builds a 'candidate partition' it does so for a single target.
   As far as Collage is concerned it does not care whether the target is regular (eg Target("cuda")), or
   for a specific external codegen (eg Target("cutlass")), it just passes the target into the build.

 - Add CompilationConfig::FindPrimitiveTargetForKind which I'll later need to retrieve
   the external codegen Target instance corresponding to a "Compiler" attribute value.

 - Target.update_target_host_consist was supporting three API styles:
    - single target
    - map from device type to target
    - map from target to IRModule (for the ir_to_runtime API)
   I replaced all those calls with a more specialized 'canonicalize' call:
    - Target.canonicalize_target_and_host
    - Target.canonicalize_multi_targets_and_host
    - Target.canonicalize_target_map_and_host
   In particular, all the tuning interfaces (task extraction, tuning, tuning records) all explicitly
   *do not* support multiple targets since the underlying code just doesn't support that.

* - Lints
- Revert unintended changes

* - more lints

* - Fix model_library_format handling of target.
- Improve comments in compilation_config.h

* - Lints
- Update target/target_host params documentation

* - Fix micro library format tests
- Rev micro library format from 5 to 6
- Use Target.current() in a few places

* - eta contract comprehension

* - Woops, one more device: target map left
- Handle host already being in Target

* - lint

* - lint

* - Bug with append
- Take device type from target

* - Fix hexagon
2022-05-23 09:14:28 -07:00
Mark Shields 521b80af21 [Relay] Support 'external codegen targets'. (#11173)
* [Relay] Support 'external codegen targets'.

(Part of Collage, https://github.com/apache/tvm-rfcs/blob/main/rfcs/0062-collage.md)

This change prepares the VM and Relay target handling machinery to support
external codegen targets in addition to 'regular' targets. This allows us
to configure the build with Collage as follows:
```
    host_target = tvm.target.Target("llvm")
    targets = [tvm.target.Target("cuda", host_target),
               tvm.target.Target("cutlass", host_target),
               tvm.target.Target("cudnn", host_target)]
    with tvm.transform.PassContext(...):
        exe = tvm.relay.vm.compile(module, target=targets)
```

Four changes are required:
1. I introduce four new target kinds for the external codegens currently supported
   by Collage. Others can be added as they are vetted for use by Collage. These
   are given a device type matching the external codegen's assumption (ie just CUDA
   currently), and given a target kind attribute "is_external_codegen" of True. The
   latter is needed by Collage to signal the target kind name represents and external
   codegen 'compiler' name. See the RFC for specifics.
2. I introduce the binary relation Target::IsExternalCodegenFor so that
   external codegen targets can be related back to the 'underlying' targets
   they are implicitly using in their codegen.
3. I rework the VMCompiler and BuildModule interfaces to accept an Array<Target> of
   'raw targets' instead of a Map<Integer, Target>. This more general representation
   is needed because we may now have multiple targets of the same device type
   active simultaneously. I add new static methods on the Python Target to
   convert to this form in a way that mimics check_and_update_host_consist.
4. I rework CompilationConfig to work from Array<Target> directly, to not depend
   on the host_target argument (since dealt with on the Python side), and to
   understand that if we have two targets for the same device type the non-external
   codegen target takes precedence.

The change to CompilationConfig seems neutral with respect to the recent discussions
on compilation configuration representation and tvmc.

I made a few attempts to remove Target.check_and_update_host_const entirely in favor
of using CompilationConfig as the definitive target handling choke point but backed
out once they became too large.

* - Working on unit tests

* - Fix two Debug-only failures

* - Use Array<Target> in GraphExecutorCodegen/AOTExecutorCodegen ifaces instead
  of CompilationConfig (don't want to bake it into any official APIs).
- Started unit tests.

* - Lints

* - Moar Lints

* - Fix some unit tests

* - Fix last unit test failures

* - whitespace

* - Address Eric's comments.
  CI likely to fail due to stricter FindPrimitiveTargetOrFail but let's see.

* - Comment adjustments.
- Unit test for new Target members.
2022-05-04 13:57:21 -07:00
Manupa Karunaratne ce29f02f4c [USMP] Adding support for U4 usecase (#10785)
* [USMP] Adding support for U4 usecase

This commit adds support for placing I/O
tensors within the workspace buffer.

This is enabled using PassConfig option
tir.usmp.use_workspace_io. Once it is enabled,
it will remove the I/O tensors from the TIR
main PrimFunc and replace them with Allocate
nodes that is annotated to contain Input and
Output tensors.

The USMP will plan memory for them accordingly.
(i.e. it will re-use space used by them for
intermediaries depending on the liveness).

This will only be supported with C Interface API.
Thus, this commit produces two functions to the
metadata sources to obtain input and output structs
that points to location inside the workspace struct.

Change-Id: I4c7e750ead9a880ba900602c17f53a125f97dbf9

* fixup! [USMP] Adding support for U4 usecase

Change-Id: I78f03d36b12b4a5e8eae8d11701f51019489defc

* fixup! [USMP] Adding support for U4 usecase

Change-Id: I857f3d0ba7bc192d56d750c44b232998b2876e7a
2022-04-25 17:37:59 -07:00
Manupa Karunaratne 55849e651e [USMP] adding support for U2 and U3 usecases (#10193)
This commit adds a MemoryPools argument for
the compilation flow according to RFC0029.

Moreover, it is used to provide support for
external pools from the application layer
that could be pinned for different memories
and/or be reused between multiple inferences
of a model.
2022-02-14 09:54:06 +00:00
Mark Shields bd61d18c19 [Relay] s/SEScope/VirtualDevice/g (#9759)
* [Relay] s/SEScope/VirtualDevice/g

Nobody liked 'SEScope', and 'DeviceMcDeviceFace' is too verbose, so it
seems 'VirtualDevice' has the popular vote.
2021-12-17 09:53:07 -05:00
Mark Shields 289bd90cfc Prepare for switching VM to LowerTEPass. (#9550)
This is a grab bag of fallout changes from switching the VM to use LoweTEPass
which can be easily split out of the main #9483 PR.

- AnnotateSpans can be used from C++ (though, unfortunately, it didn't help
  me with debugging since spans are universally dropped in most passes).
- Can get a human readable dump of the VM's PackedFunc names and indexes for
  debugging.
- If TVM_LOG_DEBUG defined then include types and ids of GlobalVars. I had
  a lot of difficulty tracking down where duplicate GlobalVars for the same
  name_hint were getting created and propagated.
- GetCallLoweredProps follows same API as GetDeviceCopy and GetOnDevice
  where will return 'null' properties if call/expr is not of call_lowered
  form. Mildly more convenient, though switching all the above to ICHECK
  and push 'if (op == the relevant op)' into all use sites would also be just
  fine.
- Misc VLOG improvements made while tracking down issues in #9483.
2021-11-24 17:09:53 +00:00
Grant Watson 8e1425d4d0 Expose workspace size in tvmgen_default.h (#9510)
This PR exposes the workspace size as a macro 
TVMGEN_DEFAULT_WORKSPACE_SIZE in tvmgen_default.h 
(or TVMGEN_<MODEL_NAME>_WORKSPACE_SIZE in 
tvmgen_<model_name>.h in the case that the model name is not default).

This functionality is useful for microTVM/AOT use cases 
where it's useful to know the workspace size at compile time.
2021-11-22 14:35:18 +00:00
Christopher Sidebottom 67714c64f2 [1/3][AOT][DeviceAPI] Connecting devices structure to relevant operators (#9395)
* [AOT][DeviceAPI] Connecting devices structure to relevant operators

This patch adds support for passing the device context via the unpacked API in AOT, generating an additional struct if necessary:

```c
/*!
 * \brief Device context pointers for TVM module "default"
 */
struct tvmgen_default_devices {
  void* npu;
};
```

Which is then added as an argument to the entry function:
```c
/*!
 * \brief entrypoint function for TVM module "default"
 * \param inputs Input tensors for the module
 * \param outputs Output tensors for the module
 * \param devices Device context pointers for the module
 */
int32_t tvmgen_default_run(
  struct tvmgen_default_inputs* inputs,
  struct tvmgen_default_outputs* outputs,
  struct tvmgen_default_devices* devices
);
```

I've temporarily added the collection of external code generators to the TE compiler pending proper annotation of the eventual functions.

Co-authored-by: Grant Watson <grant.watson@arm.com>

* Correct "use_device_api" attribute name on Target

Co-authored-by: Grant Watson <grant.watson@arm.com>
2021-11-12 12:16:11 -08:00
Mark Shields 675f348522 Better host handling in CompilationConfig & debug printing (#9460)
(This is a bit of a grab bag in preparation for #9326
which I'm trying to minimize)

While switching the device planner to use SEScopes I had a lot
of trouble with Target's not matching up.
- If no explicit host target is given but the given
  TargetMap has targets with hosts, try to use those
  to establish the host_target.
- Make sure both the 'legacy' TargetMap representation
  and the newer representation agree to pointer equality on
  their targets.
- Make sure the Interpreter uses the target from CompilationConfig
  since it's been normalized.

To debug the above:
- When in pretty printing with show_meta_data_ false give as much
  detail on SEScopes, Targets and call attributes as possible.
  That needed some rework in the relay_text_printer.cc.
- Ditto for critical 'target' attribute on PrimFuncs.
- Also added a Target::ToDebugString so I could see the
  host fields along with everything else since a lot of problems
  were caused by a mismatch of 'the same' Target with and without
  a host. (Tried using that for the ReprPrinter but broken unit
  tests.)

Note that the codebase assumes Targets are compared by ObjectPtrEquality,
yet CheckAndUpdateHostConsistency (I count 65 call sites) changes the targets.
Ultimately CompilationConfig or it's ultimate replacement should ensure we munge
targets only once at the 'main' entry points.
2021-11-09 08:44:21 -08:00
Mark Shields a6c948ac64 Adds SEScope (Storage/Execution Scope) for use as new unit of planning in 'device' planning. (#9313)
[Target] Adds SEScope (Storage/Execution Scope) for use as new unit of planning in 'device' planning

This is the first step in https://github.com/apache/tvm-rfcs/pull/38 to bring devices
and targets together when doing device planning. I've gone ahead and also included a
memory scope in this object since we will also need to propagate memory scopes across
Relay expressions once this basic preparation is in place. In the meantime that field will be
left as "".

Once device planning works in units of SEScopes it will be possible to directly read off
the device and target for any Relay sub-expression without the need for TargetMaps ort
the construction of default Targets.

SEScopes also support 'Join' and 'Default' operations needed when constraint solving in
the device planner. You can see those in use in my scratchpad branch:
  https://github.com/mbs-octoml/mbs-tvm/tree/mbs-scopes

This PR also brings some duplicated and the ad-hoc 'default target' handling logic
together into a CompilationConfig class. (Again, see the scratchpad branch for how that
will end up being used). I've placed that next to SEScope since it's main purpose is to
  a) establish the default SEScope for primitive ops
  b) establish the SEScope for the 'host'
  c) feed a definitive vector of Targets into device planning so it can resolve all
     "on_device" and "device_copy" device references to their full SEScope form.

* Reworked to avoid global SEScopeCache.

Realized while working through unit tests in the sequel that it's reasonable
for folks to call build multiple times with distinct Target objects, in which
case the global cache would grow without bound.

So instead placed the cache in the CompilationConfig class. Since that class
now has everything the device planner needs to do its job, promoted it to
be an FFI-able Object, which is now in compilation_config.{h,cc}.

I think we can do much better with CompilationConfig, but for now keeping it
to the minimum I needed to prepare for device planning from all the executor
compilation codepaths.
2021-11-03 12:37:28 -04:00
Christopher Sidebottom b53472c7b6 Migrate C Interface API Generation to C++ (#9106)
Using the new name transformations added in #9088, the C interface API is now generated in C++ rather than in Python. This is intended to be a no-op for the actual users of this change and thus I've undone some of my overzealous sanitizing to match that expectation.

Follow up PRs will clean up any remaining name transformation inconsistencies.

Fixes #8792
2021-10-08 07:40:07 +01:00