Skip to content

win: symbol exports and minor fixes - #3024

Merged
zcbenz merged 4 commits into
ml-explore:mainfrom
dhiltgen:win_cpu_only
Jan 23, 2026
Merged

win: symbol exports and minor fixes#3024
zcbenz merged 4 commits into
ml-explore:mainfrom
dhiltgen:win_cpu_only

Conversation

@dhiltgen

Copy link
Copy Markdown
Contributor

Proposed changes

This PR extracts the non-CUDA portions of #2972 for Windows support.

Test results (known GGUF library incompatibility on Windows)

(.venv) PS C:\Users\danie\code\mlx> .\build\tests\Release\tests.exe 
[doctest] doctest version is "2.4.12"
[doctest] run with "--help" for options
===============================================================================
C:\Users\danie\code\mlx\tests\load_tests.cpp(43):
TEST CASE:  test gguf

C:\Users\danie\code\mlx\tests\load_tests.cpp(43): ERROR: test case THREW exception: [save_gguf] Compile with MLX_BUILD_GGUF=ON to enable GGUF support.

===============================================================================
C:\Users\danie\code\mlx\tests\load_tests.cpp(97):
TEST CASE:  test gguf metadata

C:\Users\danie\code\mlx\tests\load_tests.cpp(97): ERROR: test case THREW exception: [save_gguf] Compile with MLX_BUILD_GGUF=ON to enable GGUF support.

===============================================================================
[doctest] test cases:  221 |  219 passed | 2 failed | 0 skipped
[doctest] assertions: 3045 | 3045 passed | 0 failed |
[doctest] Status: FAILURE!

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)

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The documentation generator does not seem to recognize the macro:

/home/runner/work/mlx/mlx/docs/src/cpp/ops.rst:6: WARNING: Error when parsing function declaration.
If the function has no return type:
  Error in declarator or parameters-and-qualifiers
  Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 8]
    MLX_API array roll (const array &a, const Shape &shift, const std::vector< int > &axes, StreamOrDevice s={})

Comment thread CMakeLists.txt Outdated
Comment thread mlx/random.cpp Outdated
T f = T(1.0);
uint16_t* m = (uint16_t*)&f;
*m -= 1;
f.bits_ -= 1;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This does not work on mac:

  /Users/runner/actions-runner/_work/mlx/mlx/mlx/random.cpp:92:4: error: member reference base type '__fp16' is not a structure or union
     92 |   f.bits_ -= 1;
        |   ~^~~~~~
  /Users/runner/actions-runner/_work/mlx/mlx/mlx/random.cpp:128:22: note: in instantiation of function template specialization 'mlx::core::random::below_one<__fp16>' requested here
    128 |         return array(below_one<float16_t>(), float32);
        |                      ^

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.

I couldn't find a good pattern for this one, so I backed it out. As a result, there's one known test failure:

C:\Users\danie\code\mlx\tests\random_tests.cpp(244):
TEST CASE:  test random uniform

C:\Users\danie\code\mlx\tests\random_tests.cpp(368): ERROR: CHECK( abs(float(mean(out).item<bfloat16_t>()) - 0.5f) < 0.02 ) is NOT correct!
  values: CHECK( 0.244141 <  0.02 )

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I can reproduce this failure on Mac running CPP tests with CPU, and it is also failing on Windows. /cc @awni

Comment thread CMakeLists.txt Outdated
Comment thread CMakeLists.txt Outdated
Comment thread mlx/CMakeLists.txt Outdated
Comment thread mlx/CMakeLists.txt Outdated
Comment thread mlx/backend/cpu/reduce.cpp Outdated
@zcbenz

zcbenz commented Jan 20, 2026

Copy link
Copy Markdown
Collaborator

Properly exporting public APIs on Windows requires adding a MLX_API macro before every public function, which is a common practice. I'm good with that but also /cc @awni @angeloskath @davidkoski since it would change how we define APIs.

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me, thanks for putting this together!

We still need a few more reviews before merging though.

Comment thread benchmarks/cpp/CMakeLists.txt
Comment thread mlx/api.h
@@ -0,0 +1,31 @@
// Copyright © 2024 Apple Inc.

@awni awni Jan 22, 2026

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.

Can we change the name of this file to api.h or mlx_api.h if you prefer? mlx_export.h is conflated a bit with export.h which is for function exports.

Comment thread tests/linalg_tests.cpp Outdated
Comment on lines +352 to +354
// Note: When TF32 is enabled (default on CUDA/Metal for performance),
// matmul has ~1e-3 precision instead of ~1e-7. Use 1e-3 tolerance to
// accommodate this. The test verifies SVD correctness, not matmul precision.

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.

We usually run the tests with TF32 disabled. It's basically the main reason we have environment flags for that. Where did you notice this failing?

@awni

awni commented Jan 22, 2026

Copy link
Copy Markdown
Member

I think I'm ok with the MLX_API macro. One thing I observed is that it is infront of most functions in the top-level headers. Is that necessary even for functions which we don't really intend to be part of the API?

@zcbenz

zcbenz commented Jan 22, 2026

Copy link
Copy Markdown
Collaborator

Is that necessary even for functions which we don't really intend to be part of the API?

No, we only need to export functions that we expect users to explicitly use.

Comment thread mlx/dtype.h

template <typename T>
struct TypeToDtype {
struct MLX_API TypeToDtype {

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.

Suggested change
struct MLX_API TypeToDtype {
struct TypeToDtype {

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.

The template specializations are defined in dtype.cpp and called from Python bindings across the DLL boundary.

Comment thread mlx/event.h Outdated
#include <memory>
#include <stdexcept>

#include "mlx/mlx_export.h"

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.

Suggested change
#include "mlx/mlx_export.h"

Comment thread mlx/event.h Outdated
namespace mlx::core {

class Event {
class MLX_API Event {

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.

Suggested change
class MLX_API Event {
class Event {

Comment thread mlx/fence.h Outdated
* iOS 18+).
*/
class Fence {
class MLX_API Fence {

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.

Suggested change
class MLX_API Fence {
class Fence {

Comment thread mlx/fence.h Outdated
#include <vector>

#include "mlx/array.h"
#include "mlx/mlx_export.h"

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.

Suggested change
#include "mlx/mlx_export.h"

Comment thread mlx/primitives.h
};

class ArgReduce : public UnaryPrimitive {
class MLX_API ArgReduce : public UnaryPrimitive {

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.

Suggested change
class MLX_API ArgReduce : public UnaryPrimitive {
class ArgReduce : public UnaryPrimitive {

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.

Used directly by the C++ test suite (arg_reduce_tests.cpp)

Comment thread mlx/primitives.h Outdated

// Abstract base class
class Primitive {
class MLX_API Primitive {

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.

Suggested change
class MLX_API Primitive {
class Primitive {

Comment thread mlx/primitives.h Outdated
#include "mlx/array.h"
#include "mlx/device.h"
#include "mlx/io/load.h"
#include "mlx/mlx_export.h"

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.

Suggested change
#include "mlx/mlx_export.h"

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.

MLX_API used elsewhere in the file.

Comment thread mlx/scheduler.h
}

Scheduler& scheduler();
MLX_API Scheduler& scheduler();

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.

Did you add that one for a specific reason? Like would some test fail if we remove it?

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.

Used directly by the C++ test suite (scheduler_tests.cpp)

Comment thread mlx/utils.h Outdated
Comment thread mlx/utils.h
Comment on lines +94 to +95
MLX_API int
normalize_axis_index(int axis, int ndim, const std::string& msg_prefix = "");

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.

Suggested change
MLX_API int
normalize_axis_index(int axis, int ndim, const std::string& msg_prefix = "");
int
normalize_axis_index(int axis, int ndim, const std::string& msg_prefix = "");

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.

Used directly by the C++ test suite (utils_tests.cpp)

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.

Gotcha - ok for those ones I might do a follow up PR to make the tests a little less intrusive. But it's also not a big deal so let's 🚢

@awni

awni commented Jan 22, 2026

Copy link
Copy Markdown
Member

I went through and suggested we remove a few of the MLX_API. If they were needed for a test failure or something then feel free to disregard the suggestion. So basically two comments:

  • Let's change mlx_export.h to api.h
  • Remove several of the MLX_API for stuff that's not really supposed to be part of the public API even though it's in the main headers .. there are a few cases we are a little sloppy there.

@awni

awni commented Jan 22, 2026

Copy link
Copy Markdown
Member

Otherwise LGTM! Thanks for the change!

@zcbenz

zcbenz commented Jan 22, 2026

Copy link
Copy Markdown
Collaborator

@awni Would you need another review or are we good to merge?

@awni

awni commented Jan 23, 2026

Copy link
Copy Markdown
Member

Good to go on my end. Thanks for making the changes @dhiltgen. We can merge as soon as the tests clear.

@zcbenz
zcbenz merged commit 687508d into ml-explore:main Jan 23, 2026
29 of 32 checks passed
jzdziarski pushed a commit to jzdziarski/mlx that referenced this pull request Jul 26, 2026
Co-authored-by: Cheng <zcbenz@gmail.com>
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