Skip to content

Dispatch bf16 at run time when using the JIT#1584

Merged
awni merged 5 commits intomainfrom
jit_metal_runtime_dispatch
Nov 16, 2024
Merged

Dispatch bf16 at run time when using the JIT#1584
awni merged 5 commits intomainfrom
jit_metal_runtime_dispatch

Conversation

@awni
Copy link
Copy Markdown
Member

@awni awni commented Nov 12, 2024

This dispatches based on metal version at runtime when using the JIT.

The idea is to simplify distributing a single binary while supporting older OS yet getting newer features when available.

In general I think these are good guidelines to follow that give the most flexibility in terms of building applications with MLX:

  1. As much as possible minimize where we conditionally compile based on the metal version
  2. When using the JIT we can easily choose the right branch at run-time with negligible cost. As much as possible we should put things which dispatch in Metal in the JIT (when MLX_METAL_JIT=True) so that we at least have this option.
  3. When not using the JIT you have two options: either deploy a single binary for the minimal target without using newer features OR ship multiple binaries (like we do in PyPi).

CC @davidkoski

Comment thread mlx/backend/metal/kernels/utils.h Outdated
Comment on lines +4 to +16
#define jit_if #if
#define jit_else #else
#define jit_endif #endif

jit_if (__METAL_VERSION__ >= 310)

#include "mlx/backend/metal/kernels/metal_3_1/bf16.h"

jit_else

#include "mlx/backend/metal/kernels/metal_3_0/bf16.h"

jit_endif // clang-format on
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The purpose of this indirection is to keep the preprocessor dispatch directives inside the JIT source. Then they get evaluated by the metal run-time compiler.

Copy link
Copy Markdown
Member

@angeloskath angeloskath left a comment

Choose a reason for hiding this comment

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

This is great! I left one tiny comment, I am not even sure it is an improvement. It's just that it is the only place that we do not have absolute includes and it can be confusing.

Comment thread mlx/backend/metal/kernels/utils.h Outdated
@awni awni merged commit 610af35 into main Nov 16, 2024
@awni awni deleted the jit_metal_runtime_dispatch branch November 16, 2024 00:54
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.

2 participants