Autotuning with micro TVM tutorial

Hi, sorry for posting many questions.

I’m now in trouble running this demo, and it fails when flashing to the board.

I specified ‘target’ and ‘board’ like below:

TARGET = tvm.target.target.micro("stm32f746xx")
BOARD = "nucleo_f746zg"

I didn’t make any change except the above part, but when I ran below:

################
# Run Autotuning
################
# Now we can run autotuning separately on each extracted task.

num_trials = 10
for task in tasks:
    tuner = tvm.autotvm.tuner.GATuner(task)
    tuner.tune(
        n_trial=num_trials,
        measure_option=measure_option,
        callbacks=[
            tvm.autotvm.callback.log_to_file("microtvm_autotune.log.txt"),
            tvm.autotvm.callback.progress_bar(num_trials, si_prefix="M"),
        ],
        si_prefix="M",
    )

I got the error below 10 times.

 Current/Best:    0.00/   0.00 MFLOPS | Progress: (0/10) | 0.00 s
ERROR:root:Project Generate Error: calling method generate_project: JSON-RPC error # -32000: calling method generate_project
Traceback (most recent call last):
  File "/home/mamo/workspace/tvm/tvm/python/tvm/micro/project_api/server.py", line 481, in serve_one_request  # <--- Outermost server-side stack frame
    self._dispatch_request(request)
  File "/home/mamo/workspace/tvm/tvm/python/tvm/micro/project_api/server.py", line 593, in _dispatch_request
    return_value = dispatch_method(**params)
  File "/home/mamo/workspace/tvm/tvm/python/tvm/micro/project_api/server.py", line 621, in _dispatch_generate_project
    return self._handler.generate_project(
  File "/home/mamo/workspace/tvm/tvm/build/microtvm_template_projects/zephyr/microtvm_api_server.py", line 417, in generate_project
    version = self._get_platform_version(get_zephyr_base(options))
  File "/home/mamo/workspace/tvm/tvm/build/microtvm_template_projects/zephyr/microtvm_api_server.py", line 325, in get_zephyr_base
    assert zephyr_base, "'zephyr_base' option not passed and not found by default!"
AssertionError: 'zephyr_base' option not passed and not found by default!

ERROR:root:Project Generate Error: calling method generate_project: JSON-RPC error # -32000: calling method generate_project
Traceback (most recent call last):
  File "/home/mamo/workspace/tvm/tvm/python/tvm/micro/project_api/server.py", line 481, in serve_one_request  # <--- Outermost server-side stack frame
    self._dispatch_request(request)
  File "/home/mamo/workspace/tvm/tvm/python/tvm/micro/project_api/server.py", line 593, in _dispatch_request
    return_value = dispatch_method(**params)
  File "/home/mamo/workspace/tvm/tvm/python/tvm/micro/project_api/server.py", line 621, in _dispatch_generate_project
    return self._handler.generate_project(
  File "/home/mamo/workspace/tvm/tvm/build/microtvm_template_projects/zephyr/microtvm_api_server.py", line 417, in generate_project
    version = self._get_platform_version(get_zephyr_base(options))
  File "/home/mamo/workspace/tvm/tvm/build/microtvm_template_projects/zephyr/microtvm_api_server.py", line 325, in get_zephyr_base
    assert zephyr_base, "'zephyr_base' option not passed and not found by default!"
AssertionError: 'zephyr_base' option not passed and not found by default!

ERROR:root:Project Generate Error: calling method generate_project: JSON-RPC error # -32000: calling method generate_project
Traceback (most recent call last):
  File "/home/mamo/workspace/tvm/tvm/python/tvm/micro/project_api/server.py", line 481, in serve_one_request  # <--- Outermost server-side stack frame
    self._dispatch_request(request)
  File "/home/mamo/workspace/tvm/tvm/python/tvm/micro/project_api/server.py", line 593, in _dispatch_request
    return_value = dispatch_method(**params)
  File "/home/mamo/workspace/tvm/tvm/python/tvm/micro/project_api/server.py", line 621, in _dispatch_generate_project
    return self._handler.generate_project(
  File "/home/mamo/workspace/tvm/tvm/build/microtvm_template_projects/zephyr/microtvm_api_server.py", line 417, in generate_project
    version = self._get_platform_version(get_zephyr_base(options))
  File "/home/mamo/workspace/tvm/tvm/build/microtvm_template_projects/zephyr/microtvm_api_server.py", line 325, in get_zephyr_base
    assert zephyr_base, "'zephyr_base' option not passed and not found by default!"
AssertionError: 'zephyr_base' option not passed and not found by default!

ERROR:root:Project Generate Error: calling method generate_project: JSON-RPC error # -32000: calling method generate_project
Traceback (most recent call last):
  File "/home/mamo/workspace/tvm/tvm/python/tvm/micro/project_api/server.py", line 481, in serve_one_request  # <--- Outermost server-side stack frame
    self._dispatch_request(request)
  File "/home/mamo/workspace/tvm/tvm/python/tvm/micro/project_api/server.py", line 593, in _dispatch_request
    return_value = dispatch_method(**params)
  File "/home/mamo/workspace/tvm/tvm/python/tvm/micro/project_api/server.py", line 621, in _dispatch_generate_project
    return self._handler.generate_project(
  File "/home/mamo/workspace/tvm/tvm/build/microtvm_template_projects/zephyr/microtvm_api_server.py", line 417, in generate_project
    version = self._get_platform_version(get_zephyr_base(options))
  File "/home/mamo/workspace/tvm/tvm/build/microtvm_template_projects/zephyr/microtvm_api_server.py", line 325, in get_zephyr_base
    assert zephyr_base, "'zephyr_base' option not passed and not found by default!"
AssertionError: 'zephyr_base' option not passed and not found by default!

I’m not sure what the backend is doing here, but maybe it uses west command and tries to flash the microcontrollers right?

I connected the correct board to PC, and also checked if zephyr is installed properly by trying blinky project. west command is recognized properly in current directory so I can’t find why it doesn’t work.

Could anyone help me on this?

Hi @sho,

this means it can’t find your Zephyr installation. Is the ZEPHYR_BASE environment variable set? If not, you could either set it or set it in the project options:

module_loader = tvm.micro.AutoTvmModuleLoader(
    template_project_dir=pathlib.Path(tvm.micro.get_microtvm_template_projects("crt")),
    project_options={"verbose": False, "zephyr_base": "/path/to/zephyr"},  # <-- note I changed this line
)

let me know if this helps. It should work ok on the microTVM reference vm.

Hi @areusch,

I’m also unfamiliar with zephyr so I didn’t know I needed to set ZEPHYR_BASE. Thanks to you, the system can now find zephyr.

However, I’ve got a new error that says

/tmp/tmpv3hbsr6h/generated-project/crt/src/runtime/crt/common/crt_runtime_api.c:210: undefined reference to TVMSystemLibEntryPoint’`

The whole log:

-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/tmpupdbpk2k/generated-project/build
[  1%] Generating misc/generated/syscalls.json, misc/generated/struct_tags.json
[  2%] Built target parse_syscalls_target
[  3%] Generating include/generated/kobj-types-enum.h, include/generated/otype-to-str.h
[  4%] Generating include/generated/syscall_dispatch.c, include/generated/syscall_list.h
[  4%] Built target syscall_list_h_target
[  4%] Built target kobj_types_h_target
[  5%] Generating include/generated/driver-validation.h
[  5%] Built target driver_validation_h_target
[  5%] Building C object zephyr/CMakeFiles/offsets.dir/arch/arm/core/offsets/offsets.c.obj
[  6%] Building CXX object CMakeFiles/microtvm_rpc_server.dir/crt/src/runtime/crt/microtvm_rpc_server/rpc_server.cc.obj
[  6%] Built target offsets
[  7%] Building C object CMakeFiles/microtvm_rpc_common.dir/crt/src/runtime/crt/microtvm_rpc_common/crcccitt.c.obj
[  8%] Building CXX object CMakeFiles/microtvm_rpc_common.dir/crt/src/runtime/crt/microtvm_rpc_common/frame_buffer.cc.obj
[  9%] Linking CXX static library libmicrotvm_rpc_server.a
[  9%] Building CXX object CMakeFiles/microtvm_rpc_common.dir/crt/src/runtime/crt/microtvm_rpc_common/framing.cc.obj
[  9%] Built target microtvm_rpc_server
[ 10%] Building C object CMakeFiles/common.dir/crt/src/runtime/crt/common/crt_backend_api.c.obj
[ 10%] Building C object CMakeFiles/common.dir/crt/src/runtime/crt/common/crt_runtime_api.c.obj
[ 11%] Building CXX object CMakeFiles/microtvm_rpc_common.dir/crt/src/runtime/crt/microtvm_rpc_common/session.cc.obj
[ 12%] Building CXX object CMakeFiles/microtvm_rpc_common.dir/crt/src/runtime/crt/microtvm_rpc_common/write_stream.cc.obj
[ 13%] Building C object CMakeFiles/common.dir/crt/src/runtime/crt/common/func_registry.c.obj
[ 13%] Linking CXX static library libmicrotvm_rpc_common.a
[ 13%] Built target microtvm_rpc_common
[ 14%] Building C object CMakeFiles/tvm_model.dir/model/codegen/host/src/lib0.c.obj
[ 15%] Building C object CMakeFiles/common.dir/crt/src/runtime/crt/common/ndarray.c.obj
[ 15%] Linking C static library libtvm_model.a
[ 15%] Built target tvm_model
[ 15%] Building C object CMakeFiles/common.dir/crt/src/runtime/crt/common/packed_func.c.obj
[ 16%] Generating include/generated/offsets.h
[ 16%] Built target offsets_h
[ 17%] Linking C static library libcommon.a
[ 17%] Built target zephyr_generated_headers
[ 17%] Built target common
[ 18%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/cbprintf.c.obj
[ 19%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/crc32_sw.c.obj
[ 19%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/device.c.obj
[ 19%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/crc16_sw.c.obj
[ 20%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/crc8_sw.c.obj
[ 21%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/crc7_sw.c.obj
[ 21%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/dec.c.obj
[ 22%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/fdtable.c.obj
[ 23%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/errno.c.obj
[ 24%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/fatal.c.obj
[ 25%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/hex.c.obj
[ 25%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/notify.c.obj
[ 25%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/idle.c.obj
[ 26%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/printk.c.obj
[ 27%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/init.c.obj
[ 28%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/onoff.c.obj
[ 29%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/kheap.c.obj
[ 29%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/rb.c.obj
[ 29%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/mailbox.c.obj
[ 30%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/sem.c.obj
[ 31%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/mem_slab.c.obj
[ 32%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/thread_entry.c.obj
[ 33%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/msg_q.c.obj
[ 33%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/timeutil.c.obj
[ 34%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/work_q.c.obj
[ 34%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/mutex.c.obj
[ 35%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/heap.c.obj
[ 36%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/pipes.c.obj
[ 36%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/heap-validate.c.obj
[ 37%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/queue.c.obj
[ 38%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/cbprintf_complete.c.obj
[ 38%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/sched.c.obj
[ 38%] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/ring_buffer.c.obj
[ 39%] Building C object zephyr/CMakeFiles/zephyr.dir/misc/generated/configs.c.obj
[ 40%] Building C object zephyr/CMakeFiles/zephyr.dir/soc/arm/st_stm32/stm32f7/soc.c.obj
[ 41%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/sem.c.obj
[ 41%] Building C object zephyr/CMakeFiles/zephyr.dir/soc/arm/st_stm32/common/stm32cube_hal.c.obj
[ 42%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/stack.c.obj
[ 43%] Building C object zephyr/CMakeFiles/zephyr.dir/subsys/cpp/cpp_init_array.c.obj
[ 44%] Building C object zephyr/CMakeFiles/zephyr.dir/subsys/cpp/cpp_ctors.c.obj
[ 44%] Building C object zephyr/CMakeFiles/zephyr.dir/subsys/cpp/cpp_dtors.c.obj
[ 45%] Building C object zephyr/CMakeFiles/zephyr.dir/subsys/cpp/cpp_virtual.c.obj
[ 46%] Building CXX object zephyr/CMakeFiles/zephyr.dir/subsys/cpp/cpp_vtable.cpp.obj
[ 46%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/system_work_q.c.obj
[ 46%] Building CXX object zephyr/CMakeFiles/zephyr.dir/subsys/cpp/cpp_new.cpp.obj
[ 47%] Building C object zephyr/CMakeFiles/zephyr.dir/subsys/power/reboot.c.obj
[ 48%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/thread.c.obj
[ 50%] Building C object zephyr/CMakeFiles/zephyr.dir/drivers/interrupt_controller/intc_exti_stm32.c.obj
[ 51%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/thread_abort.c.obj
[ 51%] Building C object zephyr/CMakeFiles/zephyr.dir/drivers/clock_control/clock_stm32_ll_common.c.obj
[ 51%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/version.c.obj
[ 52%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/work_q.c.obj
[ 53%] Building C object zephyr/CMakeFiles/zephyr.dir/drivers/clock_control/clock_stm32f2_f4_f7.c.obj
[ 54%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/condvar.c.obj
[ 55%] Building C object zephyr/CMakeFiles/zephyr.dir/drivers/pinmux/stm32/pinmux_stm32.c.obj
[ 55%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/smp.c.obj
[ 55%] Building C object zephyr/CMakeFiles/zephyr.dir/drivers/timer/sys_clock_init.c.obj
[ 56%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/banner.c.obj
[ 57%] Building C object zephyr/CMakeFiles/zephyr.dir/drivers/timer/cortex_m_systick.c.obj
[ 57%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/xip.c.obj
[ 58%] Linking CXX static library libzephyr.a
[ 58%] Built target zephyr
Scanning dependencies of target linker_script_target
[ 58%] Generating linker.cmd
[ 58%] Built target linker_script_target
[ 59%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/timeout.c.obj
[ 60%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/timer.c.obj
[ 60%] Building C object zephyr/kernel/CMakeFiles/kernel.dir/mempool.c.obj
[ 61%] Building C object zephyr/arch/common/CMakeFiles/isr_tables.dir/isr_tables.c.obj
[ 62%] Linking C static library libkernel.a
[ 63%] Linking C static library libisr_tables.a
[ 63%] Built target kernel
[ 63%] Built target isr_tables
Scanning dependencies of target arch__arm__core__aarch32__cortex_m
Scanning dependencies of target arch__arm__core__aarch32
[ 64%] Building ASM object zephyr/arch/arch/arm/core/aarch32/cortex_m/CMakeFiles/arch__arm__core__aarch32__cortex_m.dir/vector_table.S.obj
[ 65%] Building C object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/swap.c.obj
[ 66%] Building ASM object zephyr/arch/arch/arm/core/aarch32/cortex_m/CMakeFiles/arch__arm__core__aarch32__cortex_m.dir/reset.S.obj
[ 66%] Building ASM object zephyr/arch/arch/arm/core/aarch32/cortex_m/CMakeFiles/arch__arm__core__aarch32__cortex_m.dir/fault_s.S.obj
[ 67%] Building C object zephyr/arch/arch/arm/core/aarch32/cortex_m/CMakeFiles/arch__arm__core__aarch32__cortex_m.dir/fault.c.obj
[ 68%] Building ASM object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/swap_helper.S.obj
[ 68%] Building C object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/irq_manage.c.obj
[ 68%] Building ASM object zephyr/arch/arch/arm/core/aarch32/cortex_m/CMakeFiles/arch__arm__core__aarch32__cortex_m.dir/exc_exit.S.obj
[ 69%] Building C object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/thread.c.obj
[ 70%] Building C object zephyr/arch/arch/arm/core/aarch32/cortex_m/CMakeFiles/arch__arm__core__aarch32__cortex_m.dir/scb.c.obj
[ 71%] Building C object zephyr/arch/arch/arm/core/aarch32/cortex_m/CMakeFiles/arch__arm__core__aarch32__cortex_m.dir/irq_init.c.obj
[ 72%] Building ASM object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/cpu_idle.S.obj
[ 72%] Building C object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/fatal.c.obj
[ 72%] Building C object zephyr/arch/arch/arm/core/aarch32/cortex_m/CMakeFiles/arch__arm__core__aarch32__cortex_m.dir/thread_abort.c.obj
[ 73%] Building C object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/nmi.c.obj
[ 74%] Linking C static library libarch__arm__core__aarch32__cortex_m.a
[ 74%] Built target arch__arm__core__aarch32__cortex_m
[ 75%] Building C object zephyr/arch/arch/arm/core/aarch32/cortex_m/mpu/CMakeFiles/arch__arm__core__aarch32__cortex_m__mpu.dir/arm_core_mpu.c.obj
[ 76%] Building ASM object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/nmi_on_reset.S.obj
[ 76%] Building C object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/prep_c.c.obj
[ 76%] Building C object zephyr/arch/arch/arm/core/aarch32/cortex_m/mpu/CMakeFiles/arch__arm__core__aarch32__cortex_m__mpu.dir/arm_mpu.c.obj
[ 77%] Building ASM object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/isr_wrapper.S.obj
[ 78%] Building C object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/__aeabi_atexit.c.obj
[ 78%] Linking C static library libarch__arm__core__aarch32.a
[ 78%] Built target arch__arm__core__aarch32
[ 78%] Building C object zephyr/lib/libc/newlib/CMakeFiles/lib__libc__newlib.dir/libc-hooks.c.obj
[ 79%] Linking C static library libarch__arm__core__aarch32__cortex_m__mpu.a
[ 79%] Built target arch__arm__core__aarch32__cortex_m__mpu
[ 80%] Building C object zephyr/lib/posix/CMakeFiles/lib__posix.dir/pthread_common.c.obj
[ 81%] Linking C static library liblib__libc__newlib.a
[ 81%] Built target lib__libc__newlib
[ 81%] Building C object zephyr/soc/arm/common/cortex_m/CMakeFiles/soc__arm__common__cortex_m.dir/arm_mpu_regions.c.obj
[ 81%] Building C object zephyr/lib/posix/CMakeFiles/lib__posix.dir/nanosleep.c.obj
[ 82%] Linking C static library libsoc__arm__common__cortex_m.a
[ 82%] Built target soc__arm__common__cortex_m
[ 83%] Building C object zephyr/subsys/random/CMakeFiles/subsys__random.dir/rand32_entropy_device.c.obj
[ 84%] Linking C static library liblib__posix.a
[ 84%] Built target lib__posix
[ 85%] Building C object zephyr/drivers/gpio/CMakeFiles/drivers__gpio.dir/gpio_stm32.c.obj
[ 85%] Linking C static library libsubsys__random.a
[ 85%] Built target subsys__random
[ 85%] Building C object zephyr/drivers/serial/CMakeFiles/drivers__serial.dir/uart_stm32.c.obj
[ 86%] Linking C static library libdrivers__gpio.a
[ 86%] Built target drivers__gpio
[ 87%] Building C object zephyr/drivers/entropy/CMakeFiles/drivers__entropy.dir/entropy_stm32.c.obj
[ 88%] Linking C static library libdrivers__serial.a
[ 88%] Built target drivers__serial
[ 88%] Building C object modules/stm32/stm32cube/CMakeFiles/..__modules__hal__stm32__stm32cube.dir/stm32f7xx/soc/system_stm32f7xx.c.obj
[ 88%] Linking C static library libdrivers__entropy.a
[ 88%] Built target drivers__entropy
[ 89%] Building C object modules/stm32/stm32cube/CMakeFiles/..__modules__hal__stm32__stm32cube.dir/stm32f7xx/drivers/src/stm32f7xx_hal.c.obj
[ 89%] Building C object modules/stm32/stm32cube/CMakeFiles/..__modules__hal__stm32__stm32cube.dir/stm32f7xx/drivers/src/stm32f7xx_hal_rcc.c.obj
[ 90%] Building C object modules/stm32/stm32cube/CMakeFiles/..__modules__hal__stm32__stm32cube.dir/stm32f7xx/drivers/src/stm32f7xx_hal_rcc_ex.c.obj
[ 91%] Building C object CMakeFiles/app.dir/src/main.c.obj
[ 92%] Building C object modules/stm32/stm32cube/CMakeFiles/..__modules__hal__stm32__stm32cube.dir/stm32f7xx/drivers/src/stm32f7xx_ll_rng.c.obj
[ 92%] Building C object modules/stm32/stm32cube/CMakeFiles/..__modules__hal__stm32__stm32cube.dir/stm32f7xx/drivers/src/stm32f7xx_ll_utils.c.obj
[ 92%] Linking CXX static library app/libapp.a
[ 92%] Built target app
[ 93%] Building C object zephyr/arch/common/CMakeFiles/arch__common.dir/sw_isr_common.c.obj
[ 94%] Linking C static library lib..__modules__hal__stm32__stm32cube.a
[ 94%] Built target ..__modules__hal__stm32__stm32cube
[ 94%] Linking C static library libarch__common.a
[ 94%] Built target arch__common
[ 95%] Building C object zephyr/CMakeFiles/zephyr_prebuilt.dir/misc/empty_file.c.obj
[ 96%] Linking CXX executable zephyr_prebuilt.elf
/opt/zephyr-sdk-0.12.3/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/10.2.0/../../../../arm-zephyr-eabi/bin/ld: ../libcommon.a(crt_runtime_api.c.obj): in function `SystemLibraryCreate':
/tmp/tmpupdbpk2k/generated-project/crt/src/runtime/crt/common/crt_runtime_api.c:210: undefined reference to `TVMSystemLibEntryPoint'
collect2: error: ld returned 1 exit status
make[2]: *** [zephyr/CMakeFiles/zephyr_prebuilt.dir/build.make:120: zephyr/zephyr_prebuilt.elf] Error 1
make[1]: *** [CMakeFiles/Makefile2:2529: zephyr/CMakeFiles/zephyr_prebuilt.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
INFO:__main__:run (in cwd: /tmp/tmpb__5wipg/generated-project/build): cmake .. -DWEST=west -DBOARD:STRING=nucleo_f746zg
Including boilerplate (Zephyr base): /home/mamo/zephyrproject/zephyr/cmake/app/boilerplate.cmake
CMake Deprecation Warning at /home/mamo/zephyrproject/zephyr/cmake/app/boilerplate.cmake:37 (cmake_policy):
  The OLD behavior for policy CMP0079 will be removed from a future version
  of CMake.

  The cmake-policies(7) manual explains that the OLD behaviors of all
  policies are deprecated and that a policy should be set to OLD only under
  specific short-term circumstances.  Projects should be ported to the NEW
  behavior and not rely on setting a policy to OLD.
Call Stack (most recent call first):
  /home/mamo/zephyrproject/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:24 (include)
  /home/mamo/zephyrproject/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:35 (include_boilerplate)
  CMakeLists.txt:26 (find_package)

I know the reference vm has all the tools set properly to run this tutorial, but for some reason, my VM doesn’t work now.

I just built TVM from source, enabling MICRO, LLVM, installed zephyr and SDKs of same version just like written in tvm/apps/microtvm/reference-vm/zephyr/base-box/base_box_setup.sh.

Do I need to modify other parts in config.cmake when I build TVM?

Hi @areusch,

Please let me ask you a bit more about AutoTVM.

I’m browsing the whole TVM project to find how the workflow in the backend changes if you specify a zephyr board here, so that I would know how to do AutoTuning without depending on zephyr.

image

As a user going though the tutorial, it looks if you want to run host-driven or AutoTVM, it is mandatory to use zephyr and west to tune parameters, flashing the memory and running repeatedly. I’d like to know if it’s possible for users to run host-driven or AutoTVM on MCUs which are not supported by zephyr.

  1. Must I use zephyr to run host-driven or AutoTVM for the moment?

  2. If yes, do you have any plan to enable users to use boards not supported by zephyr?

  3. If no, do I still need to customize TVM to run host-driven or AutoTVM on whatever MCUs? For example, in the AutoTVM tutorial, the workflow looks seamless because TVM Python APIs work as a user interface to run west commands. We can build zephyr projects and flash them onto MCUs by Python APIs. However, if we don’t use zephyr, this is not the case. So it is probably necessary to modify or add some TVM c++ code?

  4. I was following how project_options flows into Python and C++ code and how it is used, which unfortunately failed. Could you tell me the part where the program branches, one is for zephyr and the other is for non zephyr. I guess this part is exactly where I can know how to go without zephyr maybe?

I think my question is very similar to the link below.

He is mentioning

zephyr is limited in the flashing capabilities, which would be a problem for autoTVM.

This is what I exactly want to say. It looked difficult to customize the AutoTVM so that we can be independent of zephyr. Please share if there has been any updates on this.

@sho it looks like you’re just about there. I think I see what’s happening here–we recently merged a change in the way we construct Target strings, and while we fixed the host-side example, we didn’t fix the “on-device” one. I think it would work if you make the following change:

# Compiling for physical hardware
# --------------------------------------------------------------------------
#    module_loader = tvm.micro.AutoTvmModuleLoader(
# ...
#    builder = tvm.autotvm.LocalBuilder(
#        n_parallel=1,
#        build_kwargs={"build_option": {"tir.disable_vectorize": True}},
#        do_fork=False,
#        build_func=tvm.micro.autotvm_build_func,
#        runner=RUNNER,   # <-- note changed line
#    )

Apologies I don’t have time to try this right now–we’re all off work right now on vacation. Hopefully this can point you in the right direction.

1 Like

It is! You just need to create your own implementation of Project API. The use of Zephyr in autotuning is configured on this line:

# Compiling for physical hardware
# --------------------------------------------------------------------------
#    module_loader = tvm.micro.AutoTvmModuleLoader(
#        template_project_dir=pathlib.Path(tvm.micro.get_microtvm_template_projects("zephyr")),  # <-- points AutoTVM at the Zephyr Project API server. you can replace this with the path to your own template project with a builtin Project API server.
#        project_options={  # <-- these are options given to the Zephyr Project API server
#            "zephyr_board": BOARD,
#            "west_cmd": "west",
#            "verbose": False,
#            "project_type": "host_driven",
#        },
#    )

You can see an example for Zephyr (quite complex), Arduino, or just a simple make flow.

You can test your Project API server using tvmc micro subcommand. I’d suggest you do this before trying to use the AutoTVM flow (e.g. try to do the typical flow of tvmc micro create-project, tvmc micro build, tvmc micro flash using your API server impl). You should also be able to use this to reproduce the ZEPHYR_BASE problem you had above. Once the TVMCon recordings/slides are released, you can also checkout Leandro Nunes’ (@leandron) and Gustavo Romero’s (@gromero) tutorial on tvmc micro.

The thread you pointed to is now dated–Project API is implemented, and this is how we currently (and for the foreseeable future) interface between microTVM and TVM automation flows such as AutoTVM (and later AutoScheduling/AutoTIR).

Please let me know if this answers all your questions. I didn’t respond to them individually because I think they are all answered by looking into Project API above.

@areusch, I am getting the exact same error as @sho; here is the error log:

[ 96%] Linking CXX executable zephyr_prebuilt.elf /home/vagrant/zephyr-sdk_backup/arm-zephyr-eabi/bin/…/lib/gcc/arm-zephyr-eabi/10.2.0/…/…/…/…/arm-zephyr-eabi/bin/ld: …/libcommon.a(crt_runtime_api.c.obj): in function SystemLibraryCreate': /home/vagrant/tvm/apps/microtvm/zephyr/template_project/project/crt/src/runtime/crt/common/crt_runtime_api.c:210: undefined reference to TVMSystemLibEntryPoint’ collect2: error: ld returned 1 exit status make[2]: *** [zephyr/zephyr_prebuilt.elf] Error 1 make[1]: *** [zephyr/CMakeFiles/zephyr_prebuilt.dir/all] Error 2 make: *** [all] Error 2 zephyr/CMakeFiles/zephyr_prebuilt.dir/build.make:119: recipe for target ‘zephyr/zephyr_prebuilt.elf’ failed CMakeFiles/Makefile2:2528: recipe for target ‘zephyr/CMakeFiles/zephyr_prebuilt.dir/all’ failed Makefile:90: recipe for target ‘all’ failed

Here is the code snippet I am using:

def _make_session(temp_dir, zephyr_board, west_cmd, mod):

config_main_stack_size = None

project_options = {
    "project_type": "host_driven",
    "west_cmd": west_cmd,
    "verbose": False,
    "zephyr_board": zephyr_board,
}
if config_main_stack_size is not None:
    project_options["config_main_stack_size"] = config_main_stack_size

project = tvm.micro.generate_project(
    temp_dir,
    mod,
    temp_dir + "/project",
    project_options,
)
project.build()
project.flash()
return tvm.micro.Session(project.transport())

TARGET = tvm.target.target.micro(“stm32f746xx”) # Specifying target hardware architecture BOARD = “nucleo_f746zg” # or “stm32f746g_disco#” # Specifying exact board name, used by zephyr later to create binary

repo_root = subprocess.check_output([“git”, “rev-parse”, “–show-toplevel”], encoding=‘utf-8’).strip() project_dir = os.path.join(repo_root, “apps”, “microtvm”, “zephyr”, “template_project”) temp_file_directory = repo_root

def build_flash(mod,TARGET,params,input_mcu,node,iteration):

 with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}):
     c_mod = relay.build(mod, target=TARGET, params=params)
 with _make_session(project_dir, BOARD, "west", c_mod) as session:
    graph_mod = tvm.micro.create_local_debug_executor(graph, session.get_system_lib(), session.device, dump_root= temp_file_directory)
    graph_mod.set_input(**c_mod.get_params())    
    graph_mod.run() 
    tvm_output = graph_mod.get_output(0).asnumpy()

What might be the reason for this?

Nevermind, I realised following code was missing:

RUNTIME = Runtime(“crt”, {“system-lib”: True})

c_mod = relay.build(mod, target=TARGET, runtime=RUNTIME, params=params)

@asnecemnnit glad you were able to solve it. we intend to properly document these new APIs and fix any lingering backwards-compat bugs by next release.

Thank you @areusch and @asnecemnnit very much for your information. (Also very sorry for my late reply)

I was able to run the host-driven case as well. But as @asnecemnnit suggested, runtime=RUNTIME is the one that works fine maybe? I couldn’t find runner option in LocalBuilder.

@sho @asnecemnnit my apologies, runtime=RUNTIME is the correct fix here. I mixed them up in my earlier responses :sweat_smile: Glad you were able to figure it out!

I think to properly get the example to pick up the tuned schedules, you should also pass this option to LocalBuilder:

# Compiling for physical hardware
# --------------------------------------------------------------------------
#    module_loader = tvm.micro.AutoTvmModuleLoader(
# ...
#    builder = tvm.autotvm.LocalBuilder(
#        n_parallel=1,
#        build_kwargs={"build_option": {"tir.disable_vectorize": True}},
#        do_fork=False,
#        build_func=tvm.micro.autotvm_build_func,
#        runtime=RUNTIME,   # <-- note changed line
#    )