Skip to content

MinGW support - #1806

Merged
awni merged 6 commits into
ml-explore:mainfrom
stemann:feature/mingw
Feb 1, 2025
Merged

awni merged 6 commits into
ml-explore:mainfrom
stemann:feature/mingw

Conversation

@stemann

@stemann stemann commented Jan 28, 2025 •

Copy link
Copy Markdown
Contributor

Proposed changes

Tests running on MinGW - addressing #1720.

Also, fixes build on FreeBSD, cf. JuliaPackaging/Yggdrasil#10019.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

@stemann

stemann commented Jan 28, 2025

Copy link
Copy Markdown
Contributor Author

Currently disabling distributed/ring (#1784) for Windows, as (at least) the following headers are problematic

#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>

Might be possible to replace some with winsock2.h...

@stemann

stemann commented Jan 28, 2025

Copy link
Copy Markdown
Contributor Author

Using a MinGW toolchain installed via msys2:

# MinGW
pacman -S --needed \
    base-devel \
    mingw-w64-ucrt-x86_64-toolchain \
    mingw-w64-ucrt-x86_64-ccache \
    mingw-w64-ucrt-x86_64-cmake \
    mingw-w64-ucrt-x86_64-ninja

# MLX
pacman -S --needed \
    mingw-w64-ucrt-x86_64-openblas \
    mingw-w64-ucrt-x86_64-dlfcn

and CMakePresets.json:

{
  "$schema": "https://cmake.org/cmake/help/v3.29/_downloads/3e2d73bff478d88a7de0de736ba5e361/schema.json",
  "version": 8,
  "configurePresets": [
    {
      "name": "mingw",
      "displayName": "MinGW",
      "binaryDir": "${sourceDir}/out/build/${presetName}",
      "generator": "Ninja",
      "cacheVariables": {
        "CMAKE_C_COMPILER": "gcc.exe",
        "CMAKE_CXX_COMPILER": "g++.exe",
        "CMAKE_BUILD_TYPE": "Debug",
        "CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}",
        "CMAKE_PREFIX_PATH": "C:/msys64/ucrt64",
        "CMAKE_C_COMPILER_LAUNCHER": "ccache",
        "CMAKE_CXX_COMPILER_LAUNCHER": "ccache",
        "BLAS_INCLUDE_DIRS": "C:/msys64/ucrt64/include/openblas",
        "LAPACK_INCLUDE_DIRS": "C:/msys64/ucrt64/include/openblas",
        "MLX_BUILD_GGUF": "OFF"
      },
      "environment": {
        "PATH": "C:/msys64/ucrt64/bin;C:/msys64/usr/bin"
      }
    }
  ],
  "buildPresets": [{
    "name": "mingw",
    "configurePreset": "mingw"
  }]
}

Comment thread mlx/backend/common/jit_compiler.cpp Resolved
@stemann

stemann commented Jan 29, 2025

Copy link
Copy Markdown
Contributor Author

@zcbenz Any ideas for how to manage the distributed/ring implementation on Windows?

@zcbenz

zcbenz commented Jan 29, 2025

Copy link
Copy Markdown
Member

I haven't built the latest ring change yet, but I think you can change mlx/distributed/ring/CMakeLists.txt to make Windows build with no_ring.cpp so you don't have to patch the source code to disable ring calls.

Comment thread tests/random_tests.cpp
p = array({0.1, 0.2, 0.3});
// Ask for the wrong shape => throws
CHECK_THROWS_AS(random::bernoulli(p, {2}), std::invalid_argument);
CHECK_THROWS_AS(random::bernoulli(p, Shape({2})), std::invalid_argument);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's that about? Did it not compile without the cast?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it came up as ambiguous.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious.. it should not be ambigious.. maybe a compiler error.

The only option below that it is implicitly convertible to is Shape. Just curious, do your recall which candidates it was ambiguous between / have the error message handy?

array bernoulli(
    const array& p,
    const Shape& shape,
    const std::optional<array>& key = std::nullopt,
    StreamOrDevice s = {});
array bernoulli(
    const array& p,
    const std::optional<array>& key = std::nullopt,
    StreamOrDevice s = {});

template <typename T>
array bernoulli(
    T p,
    const std::optional<array>& key = std::nullopt,
    StreamOrDevice s = {}) {
  return bernoulli(array(p), key, s);
}

template <typename T>
array bernoulli(
    T p,
    const Shape& shape,
    const std::optional<array>& key = std::nullopt,
    StreamOrDevice s = {}) {
  return bernoulli(array(p), shape, key, s);
}

array bernoulli(
    const std::optional<array>& key = std::nullopt,
    StreamOrDevice s = {});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stemann stemann Feb 1, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here it is from the mentioned MinGW set-up:

[main] Configuring project: mlx 
[driver] Removing C:/Users/Administrator/source/repos/mlx/out/build/mingw/CMakeCache.txt
[driver] Removing C:\Users\Administrator\source\repos\mlx\out\build\mingw\CMakeFiles
[proc] Executing command: C:/msys64/ucrt64/bin/cmake.exe -DCMAKE_C_COMPILER=gcc.exe -DCMAKE_CXX_COMPILER=g++.exe -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=C:/Users/Administrator/source/repos/mlx/out/install/mingw -DCMAKE_PREFIX_PATH=C:/msys64/ucrt64 -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DBLAS_INCLUDE_DIRS=C:/msys64/ucrt64/include/openblas -DLAPACK_INCLUDE_DIRS=C:/msys64/ucrt64/include/openblas -DMLX_BUILD_GGUF=OFF -SC:/Users/Administrator/source/repos/mlx -BC:/Users/Administrator/source/repos/mlx/out/build/mingw -G Ninja
[cmake] -- The C compiler identification is GNU 14.2.0
[cmake] -- The CXX compiler identification is GNU 14.2.0
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Check for working C compiler: C:/msys64/ucrt64/bin/gcc.exe - skipped
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Check for working CXX compiler: C:/msys64/ucrt64/bin/g++.exe - skipped
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- Building MLX for AMD64 processor on Windows
...
[main] Building folder: C:/Users/Administrator/source/repos/mlx/out/build/mingw 
[build] Starting build
[proc] Executing command: C:/msys64/ucrt64/bin/cmake.exe --build C:/Users/Administrator/source/repos/mlx/out/build/mingw --parallel 6 --
[build] [1/69] Building CXX object CMakeFiles/mlx.dir/mlx/device.cpp.obj
[build] [2/69] Building CXX object CMakeFiles/mlx.dir/mlx/dtype.cpp.obj
[build] [3/69] Building CXX object CMakeFiles/mlx.dir/mlx/allocator.cpp.obj
...
[build] [61/69] Building CXX object CMakeFiles/mlx.dir/mlx/io/load.cpp.obj
[build] [62/69] Building CXX object tests/CMakeFiles/tests.dir/random_tests.cpp.obj
[build] FAILED: tests/CMakeFiles/tests.dir/random_tests.cpp.obj 
[build] ccache C:\msys64\ucrt64\bin\g++.exe -DMLX_VERSION=0.22.0 -IC:/Users/Administrator/source/repos/mlx -IC:/Users/Administrator/source/repos/mlx/out/build/mingw/_deps/doctest-src -g -std=gnu++17 -MD -MT tests/CMakeFiles/tests.dir/random_tests.cpp.obj -MF tests\CMakeFiles\tests.dir\random_tests.cpp.obj.d -o tests/CMakeFiles/tests.dir/random_tests.cpp.obj -c C:/Users/Administrator/source/repos/mlx/tests/random_tests.cpp
[build] In file included from C:/Users/Administrator/source/repos/mlx/tests/random_tests.cpp:5:
[build] C:/Users/Administrator/source/repos/mlx/tests/random_tests.cpp: In function 'void DOCTEST_ANON_FUNC_18()':
[build] C:/Users/Administrator/source/repos/mlx/tests/random_tests.cpp:559:36: error: call of overloaded 'bernoulli(mlx::core::array&, <brace-enclosed initializer list>)' is ambiguous
[build]   559 |   CHECK_THROWS_AS(random::bernoulli(p, {2}), std::invalid_argument);
[build]       |                   ~~~~~~~~~~~~~~~~~^~~~~~~~
[build] C:/Users/Administrator/source/repos/mlx/out/build/mingw/_deps/doctest-src/doctest/doctest.h:2156:35: note: in definition of macro 'DOCTEST_CAST_TO_VOID'
[build]  2156 | #define DOCTEST_CAST_TO_VOID(...) __VA_ARGS__;
[build]       |                                   ^~~~~~~~~~~
[build] C:/Users/Administrator/source/repos/mlx/out/build/mingw/_deps/doctest-src/doctest/doctest.h:2531:44: note: in expansion of macro 'DOCTEST_ASSERT_THROWS_AS'
[build]  2531 | #define DOCTEST_CHECK_THROWS_AS(expr, ...) DOCTEST_ASSERT_THROWS_AS(expr, DT_CHECK_THROWS_AS, "", __VA_ARGS__)
[build]       |                                            ^~~~~~~~~~~~~~~~~~~~~~~~
[build] C:/Users/Administrator/source/repos/mlx/out/build/mingw/_deps/doctest-src/doctest/doctest.h:2972:36: note: in expansion of macro 'DOCTEST_CHECK_THROWS_AS'
[build]  2972 | #define CHECK_THROWS_AS(expr, ...) DOCTEST_CHECK_THROWS_AS(expr, __VA_ARGS__)
[build]       |                                    ^~~~~~~~~~~~~~~~~~~~~~~
[build] C:/Users/Administrator/source/repos/mlx/tests/random_tests.cpp:559:3: note: in expansion of macro 'CHECK_THROWS_AS'
[build]   559 |   CHECK_THROWS_AS(random::bernoulli(p, {2}), std::invalid_argument);
[build]       |   ^~~~~~~~~~~~~~~
[build] In file included from C:/Users/Administrator/source/repos/mlx/mlx/mlx.h:18,
[build]                  from C:/Users/Administrator/source/repos/mlx/tests/random_tests.cpp:7:
[build] C:/Users/Administrator/source/repos/mlx/mlx/random.h:166:7: note: candidate: 'mlx::core::array mlx::core::random::bernoulli(T, const std::optional<mlx::core::array>&, mlx::core::StreamOrDevice) [with T = mlx::core::array; mlx::core::StreamOrDevice = std::variant<std::monostate, mlx::core::Stream, mlx::core::Device>]'
[build]   166 | array bernoulli(
[build]       |       ^~~~~~~~~
[build] C:/Users/Administrator/source/repos/mlx/mlx/random.h:174:7: note: candidate: 'mlx::core::array mlx::core::random::bernoulli(T, const mlx::core::Shape&, const std::optional<mlx::core::array>&, mlx::core::StreamOrDevice) [with T = mlx::core::array; mlx::core::Shape = std::vector<int>; mlx::core::StreamOrDevice = std::variant<std::monostate, mlx::core::Stream, mlx::core::Device>]'
[build]   174 | array bernoulli(
[build]       |       ^~~~~~~~~
[build] C:/Users/Administrator/source/repos/mlx/mlx/random.h:155:7: note: candidate: 'mlx::core::array mlx::core::random::bernoulli(const mlx::core::array&, const mlx::core::Shape&, const std::optional<mlx::core::array>&, mlx::core::StreamOrDevice)'
[build]   155 | array bernoulli(
[build]       |       ^~~~~~~~~
[build] C:/Users/Administrator/source/repos/mlx/mlx/random.h:160:7: note: candidate: 'mlx::core::array mlx::core::random::bernoulli(const mlx::core::array&, const std::optional<mlx::core::array>&, mlx::core::StreamOrDevice)'
[build]   160 | array bernoulli(
[build]       |       ^~~~~~~~~
[build] [63/69] Linking CXX static library libmlx.a
[build] ninja: build stopped: subcommand failed.
[proc] The command: C:/msys64/ucrt64/bin/cmake.exe --build C:/Users/Administrator/source/repos/mlx/out/build/mingw --parallel 6 -- exited with code: 1
[driver] Build completed: 00:00:27.117
[build] Build finished with exit code 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! It looks like the optional<array> is causing the ambiguity. That's good to know. This should not be legit 🤔 since the array needs to be explicitly constructed:

std::optional<array> = {5};

And presumably the optional needs std::in_place to be in-place constructed. But 🤷‍♂️ .

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem.

Yeah 😊

@stemann

stemann commented Jan 31, 2025

Copy link
Copy Markdown
Contributor Author

@awni I was wondering whether it would make sense to add some CI for Windows (and possibly other weird platforms) to reduce the need for patches (in JuliaPackaging) from version to version?

@awni

awni commented Jan 31, 2025

Copy link
Copy Markdown
Member

I was wondering whether it would make sense to add some CI for Windows (and possibly other weird platforms) to reduce the need for patches (in JuliaPackaging) from version to version?

I think it makes sense if the back-end / platform in question is used. I'd like to avoid a situation where we CI for a bunch of platforms that are rarely used. In this case it will mostly slow us down from adding new features to worry about platform compatibility for unused platforms.

So TLDR yes but only once the given platform has some non-trivial usage.

In this case, I don't think we've reached that point yet. If you feel otherwise let me know.

@stemann
stemann marked this pull request as ready for review February 1, 2025 11:37

@awni awni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Will merge when tests clear!

@awni
awni merged commit 2d8e667 into ml-explore:main Feb 1, 2025
@stemann
stemann deleted the feature/mingw branch February 1, 2025 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants