anira-project / anira

an architecture for neural network inference in real-time audio applications
https://doi.org/10.1109/IS262782.2024.10704099
Apache License 2.0
115 stars 5 forks source link

Clang support for libtorch under Windows. #7

Open mchagneux opened 1 month ago

mchagneux commented 1 month ago

Hi !

I'm trying to work on Windows with a cross-platform toolchain so I'm using Ninja + Clang for my project. Since Clang for MSCV is ABI compatible with MSVC code, everything works fine with the anira binaries except for Clang errors related to the symbols \bigobjand \EHsc. Those flags are set in \lib\cmake\Caffe2\Caffe2Targets.cmake in the pre-built binaries (0.1.2), so it's possible to make it work by changing:

set_target_properties(torch_cpu PROPERTIES
  INTERFACE_COMPILE_DEFINITIONS "USE_DISTRIBUTED;USE_C10D_GLOO"
  INTERFACE_COMPILE_OPTIONS "\$<\$<COMPILE_LANGUAGE:CXX>:;\$<\$<OR:\$<CONFIG:Debug>,\$<CONFIG:RelWithDebInfo>>:/Z7>;/EHsc;/bigobj>"
  INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
  INTERFACE_LINK_LIBRARIES "protobuf::libprotobuf;c10;caffe2::mkl"
)

into

if (MSVC)
    set_target_properties(torch_cpu PROPERTIES
    INTERFACE_COMPILE_DEFINITIONS "USE_DISTRIBUTED;USE_C10D_GLOO"
    INTERFACE_COMPILE_OPTIONS "\$<\$<COMPILE_LANGUAGE:CXX>:;\$<\$<OR:\$<CONFIG:Debug>,\$<CONFIG:RelWithDebInfo>>:/Z7>;/EHsc;/bigobj>"
    INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
    INTERFACE_LINK_LIBRARIES "protobuf::libprotobuf;c10;caffe2::mkl"
    )
else()
    set_target_properties(torch_cpu PROPERTIES
    INTERFACE_COMPILE_DEFINITIONS "USE_DISTRIBUTED;USE_C10D_GLOO"
    INTERFACE_COMPILE_OPTIONS "\$<\$<COMPILE_LANGUAGE:CXX>:;\$<\$<OR:\$<CONFIG:Debug>,\$<CONFIG:RelWithDebInfo>>:/Z7>;-fexceptions>"
    INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
    INTERFACE_LINK_LIBRARIES "protobuf::libprotobuf;c10;caffe2::mkl"
    )
endif()

I don't know if this .cmake file is written as part of your code or if it comes with the libtorch library. In the latter case maybe there's a CMake command that could be used to intercept the MSVC flags and change them before compilation (this way there's no need to change the libtorch code), but I don't know it. If it's part of your code I suggest making the change so that this toolchain is readily enable on Windows.

Best,

Mathis.

faressc commented 1 month ago

These cmake files are part of the libtorch library. They look like they are automatically generated by the libtorch cmake install command. So it's something we cann't change since we download the libs from their servers on build.

faressc commented 1 month ago

We will think about your suggestion with the compile flags and report back :)