Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Source/MLX/Ops.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,28 @@ public func expm1(_ array: MLXArray, stream: StreamOrDevice = .default) -> MLXAr
return MLXArray(result)
}

/// Convert an E4M3 FP8 array to the given floating-point ``DType``.
///
/// The input array is interpreted as raw FP8 bytes.
///
/// - Parameters:
/// - array: input array containing E4M3 FP8 values
/// - dtype: data type of the output array
/// - stream: stream or device to evaluate on
///
/// ### See Also
/// - ``toFP8(_:stream:)``
/// - <doc:conversion>
public func fromFP8(
_ array: MLXArray,
dtype: DType = .bfloat16,
stream: StreamOrDevice = .default
) -> MLXArray {
var result = mlx_array_new()
mlx_from_fp8(&result, array.ctx, dtype.cmlxDtype, stream.ctx)
return MLXArray(result)
}

@available(*, deprecated, renamed: "gatherMM(_:_:lhsIndices:rhsIndices:sortedIndices:stream:)")
public func gatherMatmul(
_ a: MLXArray, _ b: MLXArray, lhsIndices: MLXArray? = nil, rhsIndices: MLXArray? = nil,
Expand Down Expand Up @@ -3076,6 +3098,21 @@ public func tiled(_ array: MLXArray, repetitions: Int, stream: StreamOrDevice =
return MLXArray(result)
}

/// Convert a floating-point array to E4M3 FP8 bytes.
///
/// - Parameters:
/// - array: input array
/// - stream: stream or device to evaluate on
///
/// ### See Also
/// - ``fromFP8(_:dtype:stream:)``
/// - <doc:conversion>
public func toFP8(_ array: MLXArray, stream: StreamOrDevice = .default) -> MLXArray {
var result = mlx_array_new()
mlx_to_fp8(&result, array.ctx, stream.ctx)
return MLXArray(result)
}

/// Returns the `k` largest elements from the input along a given axis.
///
/// The elements will not necessarily be in sorted order.
Expand Down
54 changes: 54 additions & 0 deletions Tests/MLXTests/FP8Tests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright © 2026 Apple Inc.

import Foundation
import MLX
import XCTest

class FP8Tests: XCTestCase {

override class func setUp() {
setDefaultDevice()
}

func testFromFP8DecodesE4M3Bytes() {
let allBytes = (0 ... 255).map { UInt8($0) }
let bytes = MLXArray(allBytes)
let decoded = fromFP8(bytes, dtype: .float32)

XCTAssertEqual(decoded.dtype, .float32)
XCTAssertEqual(decoded.shape, [256])

let expected = MLXArray(allBytes.map { e4m3Value($0) })
assertEqual(decoded, expected)
}

func testToFP8EncodesE4M3Bytes() {
let values = MLXArray(
[
0.0, -0.0, 1.0, -2.0, 0.125, 0.015625, 448.0, -448.0,
] as [Float])

let encoded = toFP8(values)

XCTAssertEqual(encoded.dtype, .uint8)
XCTAssertEqual(encoded.shape, values.shape)
XCTAssertEqual(
encoded.asArray(UInt8.self),
[0x00, 0x80, 0x38, 0xC0, 0x20, 0x08, 0x7E, 0xFE])
}
}

private func e4m3Value(_ value: UInt8) -> Float {
let sign: Float = (value & 0x80) == 0 ? 1 : -1
let exponent = Int((value >> 3) & 0x0F)
let mantissa = Int(value & 0x07)

if exponent == 0 {
if mantissa == 0 {
return sign < 0 ? -0.0 : 0.0
}
return sign * Float(mantissa) / 512
}

return sign * Float(8 + mantissa) * powf(2, Float(exponent - 10))
}
Loading