Skip to content

Conversation

@dhiltgen
Copy link

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)

Copy link
Collaborator

@zcbenz zcbenz left a comment

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={})

mlx/random.cpp Outdated
T f = T(1.0);
uint16_t* m = (uint16_t*)&f;
*m -= 1;
f.bits_ -= 1;
Copy link
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
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
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

@zcbenz
Copy link
Collaborator

zcbenz commented Jan 20, 2026

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.

Copy link
Collaborator

@zcbenz zcbenz left a comment

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.

@@ -0,0 +1,31 @@
// Copyright © 2024 Apple Inc.
Copy link
Member

@awni awni Jan 22, 2026

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 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
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
Copy link
Member

awni commented Jan 22, 2026

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
Copy link
Collaborator

zcbenz commented Jan 22, 2026

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.


template <typename T>
struct TypeToDtype {
struct MLX_API TypeToDtype {
Copy link
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
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.

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

#include "mlx/mlx_export.h"
Copy link
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"

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

class Event {
class MLX_API Event {
Copy link
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 {

mlx/fence.h Outdated
* iOS 18+).
*/
class Fence {
class MLX_API Fence {
Copy link
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 {

mlx/fence.h Outdated
#include <vector>

#include "mlx/array.h"
#include "mlx/mlx_export.h"
Copy link
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"

};

class ArgReduce : public UnaryPrimitive {
class MLX_API ArgReduce : public UnaryPrimitive {
Copy link
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
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)

mlx/primitives.h Outdated

// Abstract base class
class Primitive {
class MLX_API Primitive {
Copy link
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 {

mlx/primitives.h Outdated
#include "mlx/array.h"
#include "mlx/device.h"
#include "mlx/io/load.h"
#include "mlx/mlx_export.h"
Copy link
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
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.

}

Scheduler& scheduler();
MLX_API Scheduler& scheduler();
Copy link
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
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)

mlx/utils.h Outdated
namespace env {

int get_var(const char* name, int default_value);
MLX_API int get_var(const char* name, int default_value);
Copy link
Member

Choose a reason for hiding this comment

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

Probably shouldn't expose that one.

Comment on lines +94 to +95
MLX_API int
normalize_axis_index(int axis, int ndim, const std::string& msg_prefix = "");
Copy link
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
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
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
Copy link
Member

awni commented Jan 22, 2026

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
Copy link
Member

awni commented Jan 22, 2026

Otherwise LGTM! Thanks for the change!

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