Skip to content

Commit 133864c

Browse files
authored
Merge pull request #12 from SharpAI/feat/add-fp8-fast
feat: Add MLXFast.fromFp8 bindings
2 parents 35191a6 + c1708ae commit 133864c

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

.github/workflows/upstream-sync.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ jobs:
3232

3333
- name: Create or Update Sync Branch
3434
run: |
35-
git checkout -B sync/upstream-latest
36-
git reset --hard upstream/main
35+
git checkout -B sync/upstream-latest origin/main
36+
git checkout upstream/main -- .
37+
git checkout origin/main -- .github/workflows
38+
git commit -m "chore: sync with upstream/main" || true
3739
git push -f origin sync/upstream-latest
3840
3941
- name: Create Pull Request to Main
4042
env:
41-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43+
GH_TOKEN: ${{ secrets.SWIFTLM_PR_TOKEN || secrets.GITHUB_TOKEN }}
4244
run: |
4345
gh pr create --base main --head sync/upstream-latest \
4446
--title "🔄 Auto-Sync: Apple Upstream Repository" \

Source/MLX/MLXFast.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,27 @@ public enum MLXFast {
420420
mlx_fast_set_prefetch_enabled(enabled)
421421
}
422422

423+
/// Convert an array of raw FP8 E4M3 bytes (stored as uint8 by safetensors loader)
424+
/// to the specified floating point dtype using proper FP8 E4M3 semantics.
425+
///
426+
/// MLX's safetensors loader maps `F8_E4M3` → `uint8` (raw bit patterns).
427+
/// Use this before applying block-wise scale_inv to dequantize FP8 weights correctly.
428+
///
429+
/// - Parameters:
430+
/// - x: uint8 array containing raw FP8 E4M3 bit patterns
431+
/// - dtype: target floating-point dtype (e.g. `.bfloat16`, `.float16`, `.float32`)
432+
/// - stream: stream or device to evaluate on
433+
/// - Returns: Array in the requested dtype with correctly converted FP8 values
434+
public static func fromFp8(
435+
_ x: MLXArray, dtype: DType = .bfloat16, stream: StreamOrDevice = .default
436+
) -> MLXArray {
437+
precondition(x.dtype == .uint8, "FP8 input must be uint8 bit patterns")
438+
precondition(dtype == .bfloat16 || dtype == .float16 || dtype == .float32, "Target dtype must be a floating point type")
439+
var result = mlx_array_new()
440+
mlx_from_fp8(&result, x.ctx, dtype.cmlxDtype, stream.ctx)
441+
return MLXArray(result)
442+
}
443+
423444
}
424445

425446
/// Optimized implementation of `NN.RoPE`.

0 commit comments

Comments
 (0)