Skip to content

Commit 42224ee

Browse files
authored
Merge branch 'main' into agent/conv-transpose3d-metal
2 parents 63a6925 + 7408e68 commit 42224ee

8 files changed

Lines changed: 73 additions & 31 deletions

File tree

.github/actions/setup/action.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,7 @@ runs:
229229
else
230230
cmakeArgs+=("-DMLX_BUILD_METAL=ON")
231231
if ${{ inputs.toolkit == 'jit' }} ; then
232-
cmakeArgs+=(
233-
"-DBUILD_SHARED_LIBS=ON"
234-
"-DCMAKE_BUILD_TYPE=MinSizeRel"
235-
"-DMLX_BUILD_CPU=OFF"
236-
"-DMLX_BUILD_SAFETENSORS=OFF"
237-
"-DMLX_BUILD_GGUF=OFF"
238-
"-DMLX_METAL_JIT=ON"
239-
)
232+
cmakeArgs+=("-DMLX_METAL_JIT=ON")
240233
fi
241234
fi
242235
fi

.github/workflows/build_and_test.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ jobs:
105105
with:
106106
toolkit: 'cpu'
107107
- uses: actions/upload-artifact@v7
108-
if: matrix.toolkit == 'metal'
108+
if: matrix.toolkit != 'cpu'
109109
with:
110110
name: mlx-${{ matrix.toolkit }}-macos${{ matrix.macos-target }}
111111
path: |
@@ -115,24 +115,28 @@ jobs:
115115
if-no-files-found: error
116116

117117
mac_test:
118-
name: Test macOS
118+
name: Test macOS (${{ matrix.toolkit }})
119119
if: github.repository == 'ml-explore/mlx'
120120
runs-on: [self-hosted, macos]
121121
needs: mac_build
122+
strategy:
123+
fail-fast: false
124+
matrix:
125+
toolkit: ['metal', 'jit']
122126
steps:
123127
- uses: actions/checkout@v7
124128
- uses: ./.github/actions/setup
125129
with:
126-
toolkit: 'metal'
130+
toolkit: ${{ matrix.toolkit }}
127131
use-ccache: false
128132
- uses: actions/download-artifact@v8
129133
with:
130134
path: artifact
131-
pattern: mlx-metal-*
135+
pattern: mlx-${{ matrix.toolkit }}-*
132136
- run: ls -lhR artifact
133137
- uses: ./.github/actions/test-macos
134138
with:
135-
toolkit: 'metal'
139+
toolkit: ${{ matrix.toolkit }}
136140

137141
build_documentation:
138142
name: Build Documentation

mlx/backend/cuda/cuda_utils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ class CudaHandle {
5050
}
5151
}
5252

53+
Handle release() {
54+
Handle handle = handle_;
55+
handle_ = nullptr;
56+
return handle;
57+
}
58+
5359
operator Handle() const {
5460
return handle_;
5561
}

mlx/backend/cuda/device.cpp

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,42 @@ bool CommandEncoder::needs_commit() {
462462

463463
void CommandEncoder::commit() {
464464
nvtx3::scoped_range r("CommandEncoder::commit");
465+
try {
466+
commit_impl();
467+
} catch (...) {
468+
// Clear pending CUDA error first.
469+
cudaGetLastError();
470+
// Clear states.
471+
clear_graph_state();
472+
node_count_ = 0;
473+
bytes_in_graph_ = 0;
474+
// Clear graph.
475+
try {
476+
graph_.reset();
477+
} catch (...) {
478+
// Destroying could fail.
479+
graph_.release();
480+
}
481+
try {
482+
graph_ = CudaGraph(device_);
483+
} catch (...) {
484+
// Keep the original error.
485+
}
486+
// Re-throw the error.
487+
throw;
488+
}
489+
}
490+
491+
void CommandEncoder::synchronize() {
492+
CHECK_CUDA_ERROR(cudaStreamSynchronize(stream_));
493+
auto p = std::make_shared<std::promise<void>>();
494+
std::future<void> f = p->get_future();
495+
add_completed_handler([p = std::move(p)]() { p->set_value(); });
496+
commit();
497+
f.wait();
498+
}
499+
500+
void CommandEncoder::commit_impl() {
465501
if (!temporaries_.empty()) {
466502
add_completed_handler([temporaries = std::move(temporaries_)]() {});
467503
}
@@ -520,13 +556,8 @@ void CommandEncoder::commit() {
520556
}
521557

522558
// Reset state
523-
from_nodes_.clear();
524-
to_nodes_.clear();
525-
graph_deps_key_.clear();
526-
graph_nodes_key_.clear();
527-
node_map_.clear();
559+
clear_graph_state();
528560
graph_ = CudaGraph(device_);
529-
is_graph_updatable_ = true;
530561
}
531562

532563
// Put completion handlers in a batch.
@@ -535,13 +566,16 @@ void CommandEncoder::commit() {
535566
bytes_in_graph_ = 0;
536567
}
537568

538-
void CommandEncoder::synchronize() {
539-
CHECK_CUDA_ERROR(cudaStreamSynchronize(stream_));
540-
auto p = std::make_shared<std::promise<void>>();
541-
std::future<void> f = p->get_future();
542-
add_completed_handler([p = std::move(p)]() { p->set_value(); });
543-
commit();
544-
f.wait();
569+
void CommandEncoder::clear_graph_state() {
570+
from_nodes_.clear();
571+
to_nodes_.clear();
572+
graph_deps_key_.clear();
573+
graph_nodes_key_.clear();
574+
node_map_.clear();
575+
active_deps_.clear();
576+
active_outputs_.clear();
577+
concurrent_nodes_.clear();
578+
is_graph_updatable_ = true;
545579
}
546580

547581
Device& device(int cuda_device) {

mlx/backend/cuda/device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ class CommandEncoder {
138138
std::string id;
139139
};
140140

141+
void commit_impl();
142+
void clear_graph_state();
141143
void insert_graph_dependencies(GraphNode node);
142144
void insert_graph_dependencies(std::vector<GraphNode> nodes);
143145

mlx/backend/metal/kernels/fp_quantized.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,8 @@ template <
12321232
int group_size,
12331233
int bits,
12341234
bool batched,
1235-
bool has_global_scale = false>
1235+
bool has_global_scale = false,
1236+
int results_per_simdgroup = 4>
12361237
[[kernel]] void fp_qmv(
12371238
const device uint32_t* w,
12381239
const device uint8_t* scales,

mlx/backend/metal/kernels/quantized.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,8 @@ template <
16031603
int group_size,
16041604
int bits,
16051605
bool batched,
1606-
bool has_global_scale = false>
1606+
bool has_global_scale = false,
1607+
int results_per_simdgroup = 4>
16071608
[[kernel]] void affine_qmv_fast(
16081609
const device uint32_t* w [[buffer(0)]],
16091610
const device T* scales [[buffer(1)]],
@@ -1660,7 +1661,8 @@ template <
16601661
int group_size,
16611662
const int bits,
16621663
bool batched,
1663-
bool has_global_scale = false>
1664+
bool has_global_scale = false,
1665+
int results_per_simdgroup = 4>
16641666
[[kernel]] void affine_qmv(
16651667
const device uint32_t* w [[buffer(0)]],
16661668
const device T* scales [[buffer(1)]],

mlx/backend/metal/quantized.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ void gather_qmm_nax(
972972
kernel = get_qmm_nax_kernel_wrapped(
973973
d,
974974
kname,
975-
"gather_qmm_t_nax_",
975+
"gather_qmm_t_nax",
976976
mode,
977977
type_string,
978978
group_size,
@@ -987,7 +987,7 @@ void gather_qmm_nax(
987987
kernel = get_qmm_nax_kernel_wrapped(
988988
d,
989989
kname,
990-
"gather_qmm_n_nax_",
990+
"gather_qmm_n_nax",
991991
mode,
992992
type_string,
993993
group_size,

0 commit comments

Comments
 (0)