Skip to content

Commit 2cccd2b

Browse files
author
Awni Hannun
committed
fix
1 parent 724f9f2 commit 2cccd2b

8 files changed

Lines changed: 119 additions & 125 deletions

File tree

.github/actions/build-linux/action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ inputs:
99
runs:
1010
using: "composite"
1111
steps:
12+
- name: Setup tmate session
13+
uses: mxschmitt/action-tmate@v3
14+
1215
- name: Install Python package
1316
id: python_build
1417
shell: sh

mlx/backend/cuda/CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/unary)
6969
# fp4 is not available on < 12.8
7070
if(CMAKE_CUDA_COMPILER_VERSION VERSION_LESS 12.8.0)
7171
target_include_directories(mlx PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/quantized/)
72-
target_sources(
73-
mlx PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/quantized/no_cublas_qqmm.cpp)
74-
else()
7572
target_sources(mlx
76-
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/quantized/cublas_qqmm.cpp)
73+
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/quantized/no_qqmm_impl.cpp)
74+
else()
75+
target_sources(
76+
mlx PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/quantized/qqmm_impl.cpp
77+
${CMAKE_CURRENT_SOURCE_DIR}/quantized/cublas_qqmm.cpp)
7778
endif()
7879

7980
if(CMAKE_CUDA_COMPILER_VERSION VERSION_GREATER_EQUAL 12.9.0)

mlx/backend/cuda/quantized/cuda_fp4.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,14 @@ struct __nv_fp4_e2m1 {
8585
struct __nv_fp4x4_e2m1 {
8686
__device__ operator float4() {
8787
float4 out;
88-
out.x = float(*(__nv_fp4_e2m1*)(__high & 0xf));
89-
out.y = float(*(__nv_fp4_e2m1*)((__high >> 4) & 0xf));
90-
out.z = float(*(__nv_fp4_e2m1*)(__low & 0xf));
91-
out.w = float(*(__nv_fp4_e2m1*)((__low >> 4) & 0xf));
88+
auto bits = __high & 0xf;
89+
out.x = float(*(__nv_fp4_e2m1*)(&bits));
90+
bits = (__high >> 4) & 0xf;
91+
out.y = float(*(__nv_fp4_e2m1*)(&bits));
92+
bits = (__low) & 0xf;
93+
out.z = float(*(__nv_fp4_e2m1*)(&bits));
94+
bits = (__low >> 4) & 0xf;
95+
out.w = float(*(__nv_fp4_e2m1*)(&bits));
9296
return out;
9397
}
9498
uint8_t __high{0};

mlx/backend/cuda/quantized/no_cublas_qqmm.cpp

Lines changed: 0 additions & 75 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright © 2026 Apple Inc.
2+
3+
#include "mlx/backend/cuda/quantized/qqmm_impl.h"
4+
5+
namespace mlx::core {
6+
void qqmm_impl(
7+
cu::CommandEncoder&,
8+
int,
9+
int,
10+
int,
11+
bool,
12+
int64_t,
13+
bool,
14+
int64_t,
15+
array&,
16+
const array&,
17+
const array&,
18+
const array&,
19+
const array&,
20+
Dtype,
21+
QuantizationMode,
22+
float) {
23+
throw std::runtime_error(
24+
"[QQMatmul::eval_gpu] QQMM is only supported with CUDA 12.8 or higher.");
25+
}
26+
} // namespace mlx::core

mlx/backend/cuda/quantized/qqmm.cpp

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright © 2025 Apple Inc.
22

33
#include "mlx/backend/cuda/device.h"
4-
#include "mlx/backend/cuda/quantized/cublas_qqmm.h"
54
#include "mlx/backend/cuda/quantized/qmv.h"
5+
#include "mlx/backend/cuda/quantized/qqmm_impl.h"
66
#include "mlx/backend/cuda/quantized/qqmm_utils.h"
77
#include "mlx/backend/cuda/quantized/quantized.h"
88
#include "mlx/backend/cuda/quantized/quantized_utils.h"
@@ -38,47 +38,6 @@ array pad_and_swizzle_scales(
3838
return scale_tiled;
3939
}
4040

41-
void qqmm_impl(
42-
cu::CommandEncoder& encoder,
43-
int M,
44-
int N,
45-
int K,
46-
bool a_transposed,
47-
int64_t lda,
48-
bool b_transposed,
49-
int64_t ldb,
50-
array& out,
51-
const array& a,
52-
const array& b,
53-
const array& a_scale,
54-
const array& b_scale,
55-
Dtype out_dtype,
56-
QuantizationMode mode,
57-
float alpha = 1.0f) {
58-
// Invoke CublasQQMM
59-
std::string qmode = quantization_mode_to_string(mode);
60-
61-
// Currently only supports non-batched QQMM operations
62-
// that covers all use cases for training, we will just collapse (batch,
63-
// seq_len) into (tokens)
64-
CublasQQMM qqmm(
65-
encoder.device(),
66-
a_transposed,
67-
M,
68-
K,
69-
lda,
70-
b_transposed,
71-
K,
72-
N,
73-
ldb,
74-
1, // batch_count
75-
0, // a_batch_stride
76-
0, // b_batch_stride
77-
out_dtype,
78-
qmode);
79-
80-
qqmm.run(encoder, out, a, b, a_scale, b_scale, alpha);
81-
}
8241
} // namespace
8342

8443
void QQMatmul::eval_gpu(const std::vector<array>& inputs, array& out) {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright © 2026 Apple Inc.
2+
3+
#include "mlx/backend/cuda/quantized/qqmm_impl.h"
4+
#include "mlx/backend/cuda/quantized/cublas_qqmm.h"
5+
6+
namespace mlx::core {
7+
8+
void qqmm_impl(
9+
cu::CommandEncoder& encoder,
10+
int M,
11+
int N,
12+
int K,
13+
bool a_transposed,
14+
int64_t lda,
15+
bool b_transposed,
16+
int64_t ldb,
17+
array& out,
18+
const array& a,
19+
const array& b,
20+
const array& a_scale,
21+
const array& b_scale,
22+
Dtype out_dtype,
23+
QuantizationMode mode,
24+
float alpha) {
25+
// Invoke CublasQQMM
26+
std::string qmode = quantization_mode_to_string(mode);
27+
28+
// Currently only supports non-batched QQMM operations
29+
// that covers all use cases for training, we will just collapse (batch,
30+
// seq_len) into (tokens)
31+
CublasQQMM qqmm(
32+
encoder.device(),
33+
a_transposed,
34+
M,
35+
K,
36+
lda,
37+
b_transposed,
38+
K,
39+
N,
40+
ldb,
41+
1, // batch_count
42+
0, // a_batch_stride
43+
0, // b_batch_stride
44+
out_dtype,
45+
qmode);
46+
47+
qqmm.run(encoder, out, a, b, a_scale, b_scale, alpha);
48+
}
49+
50+
} // namespace mlx::core
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright © 2026 Apple Inc.
2+
#pragma once
3+
4+
#include "mlx/backend/cuda/device.h"
5+
#include "mlx/primitives.h"
6+
7+
namespace mlx::core {
8+
void qqmm_impl(
9+
cu::CommandEncoder& encoder,
10+
int M,
11+
int N,
12+
int K,
13+
bool a_transposed,
14+
int64_t lda,
15+
bool b_transposed,
16+
int64_t ldb,
17+
array& out,
18+
const array& a,
19+
const array& b,
20+
const array& a_scale,
21+
const array& b_scale,
22+
Dtype out_dtype,
23+
QuantizationMode mode,
24+
float alpha = 1.0f);
25+
26+
} // namespace mlx::core

0 commit comments

Comments
 (0)