use std::sync::OnceLock;
use crate::device::MlxDevice;
use crate::error::{MlxError, Result};
use crate::kernel_registry::KernelRegistry;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum DenseMmBackend {
Auto,
TensorRequired,
FallbackRequired,
}
pub(super) fn tensor_disabled_from_env() -> bool {
static FORCE_FALLBACK: OnceLock<bool> = OnceLock::new();
*FORCE_FALLBACK
.get_or_init(|| std::env::var("MLX_NATIVE_DISABLE_METAL_TENSOR").as_deref() == Ok("1"))
}
pub(super) fn is_unavailable_tensor_api(device: &MlxDevice, error: &MlxError) -> bool {
tensor_api_unavailable(
device
.metal_device()
.supports_family(metal::MTLGPUFamily::Metal4),
error,
)
}
fn tensor_api_unavailable(device_supports_metal4: bool, error: &MlxError) -> bool {
matches!(
error,
MlxError::ShaderCompilationError { message, .. }
if !device_supports_metal4
|| (message.contains("fatal error")
&& message.contains("'metal_tensor' file not found"))
)
}
pub(super) fn tensor_pipeline_available(
backend: DenseMmBackend,
pipeline_name: &str,
registry: &mut KernelRegistry,
device: &MlxDevice,
) -> Result<bool> {
let supported = match backend {
DenseMmBackend::FallbackRequired => false,
DenseMmBackend::TensorRequired => true,
DenseMmBackend::Auto if tensor_disabled_from_env() => false,
DenseMmBackend::Auto => {
registry
.probe_optional_pipeline(
pipeline_name,
device.metal_device(),
device.registry_id(),
|error| is_unavailable_tensor_api(device, error),
)?
.available
}
};
if backend == DenseMmBackend::Auto && std::env::var("MLX_LOG_TENSOR_PROBE").is_ok() {
eprintln!(
"[mlx-native] {pipeline_name}: {}",
if supported {
"using tensor API"
} else {
"using tiled simdgroup fallback"
}
);
}
Ok(supported)
}
#[cfg(test)]
mod tests {
use super::*;
const HOSTED_MACOS_26_DIAGNOSTIC: &str = "error: unsupported deferred-static-alloca-size \
function body in _ZN5metal18cooperative_tensorIfNS_7extentsIiJLm18446744073709551615E\
Lm18446744073709551615EEEEN3mpp10tensor_ops17__mutmul2d_detail16__operand_layoutIXtl\
NS4_19matmul2d_descriptorELi32ELi64ELi32ELb0ELb1ELb0ELNS7_4modeE1EEELNS5_36__matmul2d_\
cooperative_operand_indexE2ENS_20execution_simdgroupsILm4EEEDhDhfiJEEEEE.MTL_SIZEAS";
fn compile_error(message: &str) -> MlxError {
MlxError::ShaderCompilationError {
name: "tensor".into(),
message: message.into(),
}
}
#[test]
fn missing_tensor_header_is_unavailable_on_every_device() {
let missing = compile_error("fatal error: 'metal_tensor' file not found");
assert!(tensor_api_unavailable(true, &missing));
assert!(tensor_api_unavailable(false, &missing));
}
#[test]
fn a_device_with_metal4_fails_closed_on_every_other_diagnostic() {
for message in [
"fatal error: use of undeclared identifier 'broken'",
"note: 'metal_tensor' file not found in cached diagnostics",
HOSTED_MACOS_26_DIAGNOSTIC,
] {
assert!(
!tensor_api_unavailable(true, &compile_error(message)),
"{message}"
);
}
}
#[test]
fn a_device_without_metal4_has_no_tensor_api() {
assert!(tensor_api_unavailable(
false,
&compile_error(HOSTED_MACOS_26_DIAGNOSTIC)
));
}
#[test]
fn only_a_pipeline_build_failure_is_classified() {
for device_supports_metal4 in [true, false] {
assert!(!tensor_api_unavailable(
device_supports_metal4,
&MlxError::KernelNotFound("tensor".into())
));
assert!(!tensor_api_unavailable(
device_supports_metal4,
&MlxError::InvalidArgument("tensor".into())
));
}
}
}