TVM fails to compile Metal lib on MacOS due to version?

trying to run a metal relay model with the MacOS metal runtime. it builds the model just fine but when I go to run it I get errors. i can run the exact same model with the llvm cpu runtime without issue.

to create the model i do:

mod, params = relay.frontend.from_tflite(tflite_model, shape_dict=shape_dict, dtype_dict=dtype_dict)
targetm = "metal" #"metal-macos-metal2.1"
with transform.PassContext(opt_level=3):
    libm = relay.build(mod, targetm, params=params)
module_gpu = runtime.GraphModule(libm["default"](tvm.metal()))

module_gpu.set_input(0, dummy_data)
module_gpu.set_input(1, dummy_state)

module_gpu.run()

at which point i get this error.

  File "/Users/marko/code/tvm/src/runtime/metal/metal_module.mm", line 100
  File "/Users/marko/code/tvm/src/runtime/library_module.cc", line 78
TVMError: ---------------------------------------------------------------
An internal invariant was violated during the execution of TVM.
Please read TVM's error reporting guidelines.
More details can be found here: https://discuss.tvm.ai/t/error-reporting/7793.
---------------------------------------------------------------

  Check failed: ret == 0: TVMError: Fail to compile metal lib:Compilation failed:
error: invalid value 'macos-metal2.3' in '-std=macos-metal2.3'
note: use 'ios-metal1.0' for 'Metal 1.0 (iOS)' standard
note: use 'ios-metal1.1' for 'Metal 1.1 (iOS)' standard
note: use 'ios-metal1.2' for 'Metal 1.2 (iOS)' standard
note: use 'ios-metal2.0' for 'Metal 2.0 (iOS)' standard
note: use 'ios-metal2.1' for 'Metal 2.1 (iOS)' standard
note: use 'ios-metal2.2' for 'Metal 2.2 (iOS)' standard
note: use 'osx-metal1.0' for 'Metal 1.0 (OSX)' standard
note: use 'macos-metal1.1' or 'osx-metal1.1' for 'Metal 1.1 (macOS)' standard
note: use 'macos-metal1.2' or 'osx-metal1.2' for 'Metal 1.2 (macOS)' standard
note: use 'macos-metal2.0' or 'osx-metal2.0' for 'Metal 2.0 (macOS)' standard
note: use 'macos-metal2.1' for 'Metal 2.1 (macOS)' standard
note: use 'macos-metal2.2' for 'Metal 2.2 (macOS)' standard

i’m currently running MacOS 10.15.7 and it seems I may be running metal 2.1 but my question is how do i go about specifying the Metal version? do i have to recompile? and if so what file should i point the CMakeLists.txt or tvm_config to?

Hello @markostam !

Did you find a solution to this issue? I’m also having problems with Metal-based models every now and then and it’s hard for me to keep track on what changes have an impact on the situation

The original issue seems relate to mismatch of metal kit required by TVM and metal kit installed on MacOS operating system. Need either to update OS or change version of metal to 2.2 here tvm/metal_module.mm at main · apache/tvm · GitHub

Hi, I face similar “Invalid metallib file” exception when I try running tvm converted metal lib on iOS. I converted onnx model to tvm and created metal lib. Please suggest to resolve this exception.