UMA & from_keras: putting it all together

Hi,

I’ve been trying to go beyond the vanilla example trying to compile a simple keras model consisting of one Conv2D - I couldn’t get it to work.

Here is the code for run_model.py (EDIT:: edited to match the following edit comments):

from tvm.micro.testing.aot_test_utils import AOT_DEFAULT_RUNNER
import tvm
from tvm import relay
from backend import VanillaAcceleratorBackend
from tvm.relay import transform
from collections import OrderedDict
import numpy as np


from tvm.testing.aot import (
    AOTTestModel as AOTModel,
    AOTTestRunner as AOTRunner,
    generate_ref_data,
    compile_and_run,
)

import tensorflow as tf
from tensorflow.keras.applications.resnet50 import preprocess_input, ResNet50
from tensorflow.keras.applications.mobilenet import MobileNet

from PIL import Image
from tvm.contrib.download import download_testdata


def create_keras_model(input_shape=[1, 224, 224, 3], runner=AOT_DEFAULT_RUNNER,):
    model = tf.keras.Sequential([tf.keras.Input(input_shape[1:], input_shape[0], name='input_1'), tf.keras.layers.Conv2D(8, 3, use_bias=False)])

    img_url = "https://github.com/dmlc/mxnet.js/blob/main/data/cat.png?raw=true"
    img_path = download_testdata(img_url, "cat.png", module="data")
    img = Image.open(img_path).resize((224, 224))
    data = np.array(img)[np.newaxis, :].astype("float32")
    data = preprocess_input(data)

    shape_dict = {"input_1": data.shape}
    mod, params = relay.frontend.from_keras(model, shape_dict, layout='NHWC')
    mod = transform.InferType()(mod)
    output_list = generate_ref_data(mod, params)

    pass_config = {"tir.usmp.enable": True}
    runner = AOTRunner(
        makefile=runner.makefile,
        prologue=runner.prologue,
        epilogue=runner.epilogue,
        includes=runner.includes,
        parameters=runner.parameters,
        pass_config=pass_config,
    )

    inputs = {key: val.numpy() for key, val in params.items()}
    inputs["input_1"] = data

    return mod, inputs, output_list, runner

def main():
    mod, inputs, output_list, runner = create_keras_model()

    uma_backend = VanillaAcceleratorBackend()
    uma_backend.register()
    mod = uma_backend.partition(mod)
    target = tvm.target.Target("vanilla_accelerator", host=tvm.target.Target("c"))

    export_directory = tvm.contrib.utils.tempdir(keep_for_debug=True).path
    print(f"Generated files are in {export_directory}")
    compile_and_run(
        AOTModel(module=mod, inputs=inputs, outputs=output_list),
        runner,
        interface_api="c",
        use_unpacked_api=True,
        target=target,
        test_dir=str(export_directory),
    )


if __name__ == "__main__":
    main()

And the error:

❯ python run_model.py
conv2d NHWC layout is not optimized for x86 with autotvm.
conv2d NHWC layout is not optimized for x86 with autotvm.
Generated files are in /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T10-52-46___0zmcx_lr/00000
conv2d is not optimized for this platform.
Traceback (most recent call last):
  File "run_model.py", line 68, in <module>
    main()
  File "run_model.py", line 57, in main
    compile_and_run(
  File "/Users/username/tvm/python/tvm/testing/aot.py", line 878, in compile_and_run
    compiled_test_mods = compile_models(
  File "/Users/username/tvm/python/tvm/testing/aot.py", line 668, in compile_models
    executor_factory = tvm.relay.build(
  File "/Users/username/tvm/python/tvm/relay/build_module.py", line 364, in build
    graph_json, runtime_mod, params = bld_mod.build(
  File "/Users/username/tvm/python/tvm/relay/build_module.py", line 161, in build
    self._build(
  File "/Users/username/tvm/python/tvm/_ffi/_ctypes/packed_func.py", line 237, in __call__
    raise get_last_ffi_error()
tvm._ffi.base.TVMError: Traceback (most recent call last):
  [bt] (8) 9   libtvm.dylib                        0x000000015d38bde0 tvm::relay::ExprMutator::VisitExpr(tvm::RelayExpr const&) + 264
  [bt] (7) 8   libtvm.dylib                        0x000000015d04c2f8 tvm::relay::transform::DeviceAwareExprMutator::VisitExpr_(tvm::relay::CallNode const*) + 100
  [bt] (6) 7   libtvm.dylib                        0x000000015d2bb398 tvm::relay::tec::LowerTensorExprMutator::DeviceAwareVisitExpr_(tvm::relay::CallNode const*) + 4056
  [bt] (5) 6   libtvm.dylib                        0x000000015d2a7b9c tvm::relay::tec::TECompilerImpl::Lower(tvm::relay::tec::CCacheKey const&) + 56
  [bt] (4) 5   libtvm.dylib                        0x000000015d2ada98 tvm::relay::tec::TECompilerImpl::LowerInternal(tvm::relay::tec::CCacheKey const&, tvm::GlobalVarSupply) + 3508
  [bt] (3) 4   libtvm.dylib                        0x000000015d2c5b10 tvm::relay::tec::PrimFuncFor(tvm::relay::Function const&, tvm::Target const&, tvm::GlobalVarSupply, tvm::NameSupply) + 144
  [bt] (2) 3   libtvm.dylib                        0x000000015d2c82e4 tvm::relay::tec::ScheduleBuilder::Create(tvm::relay::Function const&, tvm::GlobalVarSupply, tvm::NameSupply) + 9828
  [bt] (1) 2   libtvm.dylib                        0x000000015d3b7954 tvm::relay::OpImplementation::Schedule(tvm::Attrs const&, tvm::runtime::Array<tvm::te::Tensor, void> const&, tvm::Target const&) + 
188
  [bt] (0) 1   libtvm.dylib                        0x000000015d64e5c0 tvm::runtime::PackedFuncObj::Extractor<tvm::runtime::PackedFuncSubObj<TVMFuncCreateFromCFunc::$_2> >::Call(tvm::runtime::PackedFunc
Obj const*, tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*) + 156
  File "/Users/username/tvm/python/tvm/_ffi/_ctypes/packed_func.py", line 81, in cfun
    rv = local_pyfunc(*pyargs)
  File "/Users/username/tvm/python/tvm/relay/op/strategy/generic.py", line 107, in schedule_injective
    return topi.generic.schedule_injective(outs)
  File "/Users/username/tvm/python/tvm/topi/generic/injective.py", line 60, in schedule_injective
    raise RuntimeError("schedule_injective not registered for '%s'" % target)
RuntimeError: schedule_injective not registered for 'vanilla_accelerator '

Note the extra space in the target name in the error message, in 'vanilla_accelerator ', this could be a bug.

I am also running on apple M1 so I might also be doing something wrong to get conv2d NHWC layout is not optimized for x86 with autotvm..

EDIT I believe I solved this issue, the problem was in the actual model definition which uses keras Conv2D with use_bias=True by default, putting use_bias=False seems to help, but only up to the next error - which is:

Traceback (most recent call last):
  File "run_model.py", line 83, in <module>
    main()
  File "run_model.py", line 72, in main
    compile_and_run(
  File "/Users/username/tvm/python/tvm/testing/aot.py", line 891, in compile_and_run
    run_and_check(
  File "/Users/username/tvm/python/tvm/testing/aot.py", line 841, in run_and_check
    run_and_check_body(test_dir)
  File "/Users/username/tvm/python/tvm/testing/aot.py", line 750, in run_and_check_body
    _create_header_file(
  File "/Users/username/tvm/python/tvm/testing/aot.py", line 562, in _create_header_file
    header_file.write(f"const size_t {tensor_name}_len = {npy_data.size};\n")
AttributeError: 'NDArray' object has no attribute 'size'

So what I did is that I created a new inputs dict from params where the values are the numpy equivalent, I also added input_1 to it. Now onto the next error:

conv2d NHWC layout is not optimized for x86 with autotvm.
conv2d NHWC layout is not optimized for x86 with autotvm.
Generated files are in /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000
conv2d is not optimized for this platform.
Traceback (most recent call last):
  File "run_model.py", line 83, in <module>
    main()
  File "run_model.py", line 72, in main
    compile_and_run(
  File "/Users/username/tvm/python/tvm/testing/aot.py", line 891, in compile_and_run
    run_and_check(
  File "/Users/username/tvm/python/tvm/testing/aot.py", line 841, in run_and_check
    run_and_check_body(test_dir)
  File "/Users/username/tvm/python/tvm/testing/aot.py", line 817, in run_and_check_body
    _subprocess_check_log_output(compile_command, ".", compile_log_path)
  File "/Users/username/tvm/python/tvm/testing/aot.py", line 179, in _subprocess_check_log_output
    raise RuntimeError(f"Subprocess failed: {cmd}\nstdout:\n{stdout}")
RuntimeError: Subprocess failed: make -f /Users/username/tvm/python/tvm/testing/../../../tests/python/relay/aot/default.mk build_dir
=/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build CFLAGS='-DTVM_RUNTIME_ALLOC_ALIGN
MENT_BYTES=8  -DTVM_RUNTIME_CONST_ALLOC_ALIGNMENT_BYTES=8 ' TVM_ROOT=/Users/username/tvm/python/tvm/testing/../../.. AOT_TEST_ROOT=/
Users/username/tvm/python/tvm/testing/../../../tests/python/relay/aot CODEGEN_ROOT=/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/
tvm-debug-mode-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/codegen STANDALONE_CRT_DIR=/Users/username/tvm/build/standalone_crt FVP
_DIR=/opt/arm/FVP_Corstone_SSE-300_Ethos-U55/models/Linux64_GCC-6.4/ aot_test_runner
stdout:

--------------------------------------------------------------------------------2023-01-26 14:02:12: Execute (.): make -f /Users/nicholasscottodiperto
/work/tvm/python/tvm/testing/../../../tests/python/relay/aot/default.mk build_dir=/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-temp
dirs/2023-01-26T14-02-10___nh85qb5e/00000/build CFLAGS='-DTVM_RUNTIME_ALLOC_ALIGNMENT_BYTES=8  -DTVM_RUNTIME_CONST_ALLOC_ALIGNMENT_BYTES=8 ' TVM_ROOT=
/Users/username/tvm/python/tvm/testing/../../.. AOT_TEST_ROOT=/Users/username/tvm/python/tvm/testing/../../../test
s/python/relay/aot CODEGEN_ROOT=/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/codegen 
STANDALONE_CRT_DIR=/Users/username/tvm/build/standalone_crt FVP_DIR=/opt/arm/FVP_Corstone_SSE-300_Ethos-U55/models/Linux64_GCC-6.4/ 
aot_test_runner
--------------------------------------------------------------------------------
mkdir -p /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build
gcc -DTVM_RUNTIME_ALLOC_ALIGNMENT_BYTES=8  -DTVM_RUNTIME_CONST_ALLOC_ALIGNMENT_BYTES=8  -c -g -I/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-d
ebug-mode-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build/../include -I/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/20
23-01-26T14-02-10___nh85qb5e/00000/codegen/host/include -isystem/Users/username/tvm/build/standalone_crt/include -o /var/folders/62/
f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build/stack_allocator.o  /Users/nicholasscottodiperto/wo
rk/tvm/build/standalone_crt/src/runtime/crt/memory/stack_allocator.c 
mkdir -p /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build
gcc -DTVM_RUNTIME_ALLOC_ALIGNMENT_BYTES=8  -DTVM_RUNTIME_CONST_ALLOC_ALIGNMENT_BYTES=8  -c -g -I/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-d
ebug-mode-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build/../include -I/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/20
23-01-26T14-02-10___nh85qb5e/00000/codegen/host/include -isystem/Users/username/tvm/build/standalone_crt/include -o /var/folders/62/
f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build/crt_backend_api.o  /Users/nicholasscottodiperto/wo
rk/tvm/build/standalone_crt/src/runtime/crt/common/crt_backend_api.c 
mkdir -p /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build
gcc -DTVM_RUNTIME_ALLOC_ALIGNMENT_BYTES=8  -DTVM_RUNTIME_CONST_ALLOC_ALIGNMENT_BYTES=8  -g -I/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debu
g-mode-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build/../include -I/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-
01-26T14-02-10___nh85qb5e/00000/codegen/host/include -isystem/Users/username/tvm/build/standalone_crt/include -o /var/folders/62/f38
_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build/aot_test_runner /var/folders/62/f38_r8614ds2b85ln1m0z
r0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build/test.c /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-
tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build/../codegen/host/src/default_lib0.c /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode
-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build/../codegen/host/src/default_lib1.c /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mod
e-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build/../codegen/host/src/default_lib2.c /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mo
de-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build/stack_allocator.o /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023
-01-26T14-02-10___nh85qb5e/00000/build/crt_backend_api.o    -lm
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build/test.c:30:89: warning: non-void fu
nction does not return a value [-Wreturn-type]
TVM_DLL int TVMFuncRegisterGlobal(const char* name, TVMFunctionHandle f, int override) {}
                                                                                        ^
1 warning generated.
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build/../codegen/host/src/default_lib0.c
:5:24: error: argument to 'section' attribute is not valid for this target: mach-o section specifier requires a segment and section separated by a com
ma
__attribute__((section(".bss.noinit.tvm"), aligned(8)))
                       ^
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build/../codegen/host/src/default_lib0.c
:9:190: warning: incompatible pointer types passing 'uint8_t (*)[602112]' to parameter of type 'uint8_t *' (aka 'unsigned char *') [-Wincompatible-poi
nter-types]
int32_t tvmgen_default_run(struct tvmgen_default_inputs* inputs,struct tvmgen_default_outputs* outputs) {return tvmgen_default___tvm_main__(inputs->in
put_1,inputs->_param_1,outputs->output,&global_workspace);
                                                                                                                                                      
                                       ^~~~~~~~~~~~~~~~~
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build/../codegen/host/src/default_lib0.c
:8:97: note: passing argument to parameter 'global_workspace_0_var' here
TVM_DLL int32_t tvmgen_default___tvm_main__(void* input_1,void* _param_1,void* output0,uint8_t* global_workspace_0_var);
                                                                                                ^
1 warning and 1 error generated.
make: *** [/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-10___nh85qb5e/00000/build/aot_test_runner] Error 
1

conv2d NHWC layout is not optimized for x86 with autotvm.
conv2d NHWC layout is not optimized for x86 with autotvm.
- _param_1: <class 'numpy.ndarray'>
- input_1: <class 'numpy.ndarray'>
Generated files are in /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000
conv2d is not optimized for this platform.
Traceback (most recent call last):
  File "run_model.py", line 83, in <module>
    main()
  File "run_model.py", line 72, in main
    compile_and_run(
  File "/Users/username/tvm/python/tvm/testing/aot.py", line 891, in compile_and_run
    run_and_check(
  File "/Users/username/tvm/python/tvm/testing/aot.py", line 841, in run_and_check
    run_and_check_body(test_dir)
  File "/Users/username/tvm/python/tvm/testing/aot.py", line 817, in run_and_check_body
    _subprocess_check_log_output(compile_command, ".", compile_log_path)
  File "/Users/username/tvm/python/tvm/testing/aot.py", line 179, in _subprocess_check_log_output
    raise RuntimeError(f"Subprocess failed: {cmd}\nstdout:\n{stdout}")
RuntimeError: Subprocess failed: make -f /Users/username/tvm/python/tvm/testing/../../../tests/python/relay/aot/default.mk build_dir
=/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build CFLAGS='-DTVM_RUNTIME_ALLOC_ALIGN
MENT_BYTES=8  -DTVM_RUNTIME_CONST_ALLOC_ALIGNMENT_BYTES=8 ' TVM_ROOT=/Users/username/tvm/python/tvm/testing/../../.. AOT_TEST_ROOT=/
Users/username/tvm/python/tvm/testing/../../../tests/python/relay/aot CODEGEN_ROOT=/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/
tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/codegen STANDALONE_CRT_DIR=/Users/username/tvm/build/standalone_crt FVP
_DIR=/opt/arm/FVP_Corstone_SSE-300_Ethos-U55/models/Linux64_GCC-6.4/ aot_test_runner
stdout:

--------------------------------------------------------------------------------2023-01-26 14:02:19: Execute (.): make -f /Users/nicholasscottodiperto
/work/tvm/python/tvm/testing/../../../tests/python/relay/aot/default.mk build_dir=/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-temp
dirs/2023-01-26T14-02-18___0m0dh9q4/00000/build CFLAGS='-DTVM_RUNTIME_ALLOC_ALIGNMENT_BYTES=8  -DTVM_RUNTIME_CONST_ALLOC_ALIGNMENT_BYTES=8 ' TVM_ROOT=
/Users/username/tvm/python/tvm/testing/../../.. AOT_TEST_ROOT=/Users/username/tvm/python/tvm/testing/../../../test
s/python/relay/aot CODEGEN_ROOT=/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/codegen 
STANDALONE_CRT_DIR=/Users/username/tvm/build/standalone_crt FVP_DIR=/opt/arm/FVP_Corstone_SSE-300_Ethos-U55/models/Linux64_GCC-6.4/ 
aot_test_runner
--------------------------------------------------------------------------------
mkdir -p /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build
gcc -DTVM_RUNTIME_ALLOC_ALIGNMENT_BYTES=8  -DTVM_RUNTIME_CONST_ALLOC_ALIGNMENT_BYTES=8  -c -g -I/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-d
ebug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include -I/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/20
23-01-26T14-02-18___0m0dh9q4/00000/codegen/host/include -isystem/Users/username/tvm/build/standalone_crt/include -o /var/folders/62/
f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/stack_allocator.o  /Users/nicholasscottodiperto/wo
rk/tvm/build/standalone_crt/src/runtime/crt/memory/stack_allocator.c 
mkdir -p /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build
gcc -DTVM_RUNTIME_ALLOC_ALIGNMENT_BYTES=8  -DTVM_RUNTIME_CONST_ALLOC_ALIGNMENT_BYTES=8  -c -g -I/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-d
ebug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include -I/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/20
23-01-26T14-02-18___0m0dh9q4/00000/codegen/host/include -isystem/Users/username/tvm/build/standalone_crt/include -o /var/folders/62/
f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/crt_backend_api.o  /Users/nicholasscottodiperto/wo
rk/tvm/build/standalone_crt/src/runtime/crt/common/crt_backend_api.c 
mkdir -p /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build
gcc -DTVM_RUNTIME_ALLOC_ALIGNMENT_BYTES=8  -DTVM_RUNTIME_CONST_ALLOC_ALIGNMENT_BYTES=8  -g -I/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debu
g-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include -I/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-
01-26T14-02-18___0m0dh9q4/00000/codegen/host/include -isystem/Users/username/tvm/build/standalone_crt/include -o /var/folders/62/f38
_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/aot_test_runner /var/folders/62/f38_r8614ds2b85ln1m0z
r0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/test.c /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-
tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../codegen/host/src/default_lib0.c /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode
-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../codegen/host/src/default_lib1.c /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mod
e-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../codegen/host/src/default_lib2.c /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mo
de-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/stack_allocator.o /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023
-01-26T14-02-18___0m0dh9q4/00000/build/crt_backend_api.o    -lm
In file included from /var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/test.c:10:
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include/tvmgen_default_expected
_output_data_output.h:5:4427: error: use of undeclared identifier 'inf'
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include/tvmgen_default_expected
_output_data_output.h:5:4602: error: use of undeclared identifier 'inf'
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include/tvmgen_default_expected
_output_data_output.h:5:6455: error: use of undeclared identifier 'inf'
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include/tvmgen_default_expected
_output_data_output.h:5:6988: error: use of undeclared identifier 'inf'
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include/tvmgen_default_expected
_output_data_output.h:5:8931: error: use of undeclared identifier 'inf'
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include/tvmgen_default_expected
_output_data_output.h:5:9107: error: use of undeclared identifier 'inf'
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include/tvmgen_default_expected
_output_data_output.h:5:12302: error: use of undeclared identifier 'inf'
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include/tvmgen_default_expected
_output_data_output.h:5:12478: error: use of undeclared identifier 'inf'
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include/tvmgen_default_expected
_output_data_output.h:5:12867: error: use of undeclared identifier 'inf'
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include/tvmgen_default_expected
_output_data_output.h:5:13044: error: use of undeclared identifier 'inf'
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include/tvmgen_default_expected
_output_data_output.h:5:13050: error: use of undeclared identifier 'inf'
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include/tvmgen_default_expected
_output_data_output.h:5:18601: error: use of undeclared identifier 'inf'
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include/tvmgen_default_expected
_output_data_output.h:5:18777: error: use of undeclared identifier 'inf'
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include/tvmgen_default_expected
_output_data_output.h:5:22860: error: use of undeclared identifier 'inf'
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include/tvmgen_default_expected
_output_data_output.h:5:23035: error: use of undeclared identifier 'inf'
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include/tvmgen_default_expected
_output_data_output.h:5:25017: error: use of undeclared identifier 'inf'
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include/tvmgen_default_expected
_output_data_output.h:5:26049: error: use of undeclared identifier 'inf'
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include/tvmgen_default_expected
_output_data_output.h:5:26223: error: use of undeclared identifier 'inf'
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../include/tvmgen_default_expected
_output_data_output.h:5:34512: error: use of undeclared identifier 'inf'
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../codegen/host/src/default_lib0.c
:5:24: error: argument to 'section' attribute is not valid for this target: mach-o section specifier requires a segment and section separated by a com
ma
__attribute__((section(".bss.noinit.tvm"), aligned(8)))
                       ^
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../codegen/host/src/default_lib0.c
:9:190: warning: incompatible pointer types passing 'uint8_t (*)[602112]' to parameter of type 'uint8_t *' (aka 'unsigned char *') [-Wincompatible-poi
nter-types]
int32_t tvmgen_default_run(struct tvmgen_default_inputs* inputs,struct tvmgen_default_outputs* outputs) {return tvmgen_default___tvm_main__(inputs->in
put_1,inputs->_param_1,outputs->output,&global_workspace);
                                                                                                                                                      
                                       ^~~~~~~~~~~~~~~~~
/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/../codegen/host/src/default_lib0.c
:8:97: note: passing argument to parameter 'global_workspace_0_var' here
TVM_DLL int32_t tvmgen_default___tvm_main__(void* input_1,void* _param_1,void* output0,uint8_t* global_workspace_0_var);
                                                                                                ^
1 warning and 1 error generated.
make: *** [/var/folders/62/f38_r8614ds2b85ln1m0zr0c0000gp/T/tvm-debug-mode-tempdirs/2023-01-26T14-02-18___0m0dh9q4/00000/build/aot_test_runner] Error 
1

Hi @slai-nick

I got your test to work by adjusting it according to the mobilenet test: https://github.com/apache/tvm/blob/main/tests/python/contrib/test_uma/test_uma_pipeline.py#L111

  • pass the params to the AOTModel
  • set the target to [target_c, target]

The last remaining errors you are seeing seem to be related to your target architecture and compiler. Probably, AOT is not yet supported for the M1.

1 Like

I managed to get it to compile on an arm64 linux docker image, I used the tensorflow one from arm (I used armswdev/tensorflow-arm-neoverse:r22.09-tf-2.10.0-eigen, it uses python 3.8) - I am still in the process of building the arm image in the tvm repo for later work but it’s painfully slow on apple m1.

I share the final script if it can help - I basically followed @r.stahl advice.

from PIL import Image
import numpy as np

import tensorflow as tf
from tensorflow.keras.applications.resnet50 import preprocess_input, ResNet50

from tvm.micro.testing.aot_test_utils import AOT_DEFAULT_RUNNER
import tvm
from tvm import relay
from backend import VanillaAcceleratorBackend
from tvm.relay import transform

from tvm.testing.aot import (
    AOTTestModel as AOTModel,
    generate_ref_data,
    compile_and_run,
)
from tvm.contrib.download import download_testdata


def load_test_image(size=224):
    img_url = "https://github.com/dmlc/mxnet.js/blob/main/data/cat.png?raw=true"
    img_path = download_testdata(img_url, "cat.png", module="data")
    img = Image.open(img_path).resize((size, size))
    return img


def main():
    export_directory = tvm.contrib.utils.tempdir(keep_for_debug=True).path
    print(f"Exporting build to {export_directory}")
    use_unpacked_api = True
    interface_api = "c"
    test_runner = AOT_DEFAULT_RUNNER

    # We need to fix the model input shape before importing to tvm
    keras_model = tf.keras.Sequential([
        tf.keras.Input([224, 224, 3], 1, name="input"),
        ResNet50(),
    ])
    shape_dict = dict(input0=(1, 224, 224, 3))
    mod, params = relay.frontend.from_keras(keras_model, shape_dict, layout='NHWC')
    mod = transform.InferType()(mod)

    uma_backend = VanillaAcceleratorBackend()
    uma_backend.register()
    target = tvm.target.Target("vanilla_accelerator", host=tvm.target.Target("c"))
    target_c = tvm.target.Target("c")

    img = np.array(load_test_image())[np.newaxis, :].astype("float32")
    data = preprocess_input(img)
    input_list = {"input0": data}
    output_list = generate_ref_data(mod, input_list, params)
    mod = uma_backend.partition(mod)
    aot_test_model = AOTModel(module=mod, inputs=input_list, outputs=output_list, params=params)

    compile_and_run(
        aot_test_model,
        test_runner,
        interface_api,
        use_unpacked_api,
        workspace_byte_alignment=1,
        debug_calculated_workspaces=False,
        target=[target_c, target],
        test_dir=export_directory,
    )
    print(f"Build exported to {export_directory}")



if __name__ == "__main__":
    main()

One thing that bugged me for a while is that I was trying with a fresh instance of Conv2D and random input data as an example, however this was producing error because compile_and_run will record the original model output and compare it to the compiled model output, and in this random case the original output had nan and this was creating issues during the testing.

I will try again on native macOS to see if I can get it to work now and post updates here, hopefully soon, after which this post could be closed.