import pytest
from modelexpress import p2p_pb2
from modelexpress.metadata.payload import accelerators_compatible
WEIGHTS = p2p_pb2.MX_SOURCE_TYPE_WEIGHTS
LORA = p2p_pb2.MX_SOURCE_TYPE_LORA
ARTIFACT_TYPES = [
p2p_pb2.MX_SOURCE_TYPE_CUDA_GRAPH,
p2p_pb2.MX_SOURCE_TYPE_TORCH_COMPILE_CACHE,
p2p_pb2.MX_SOURCE_TYPE_TRITON_CACHE,
p2p_pb2.MX_SOURCE_TYPE_DEEP_GEMM_CACHE,
p2p_pb2.MX_SOURCE_TYPE_TILELANG_CACHE,
p2p_pb2.MX_SOURCE_TYPE_CUTE_DSL_CACHE,
p2p_pb2.MX_SOURCE_TYPE_FLASHINFER_CACHE,
]
@pytest.mark.parametrize("mx_source_type", [None, WEIGHTS, LORA, *ARTIFACT_TYPES])
def test_empty_source_is_compatible(mx_source_type):
assert accelerators_compatible(
"cuda", "", mx_source_type=mx_source_type, quantization="", dtype="bfloat16"
)
@pytest.mark.parametrize("mx_source_type", [None, WEIGHTS, LORA, *ARTIFACT_TYPES])
def test_empty_target_is_compatible(mx_source_type):
assert accelerators_compatible(
"", "xpu", mx_source_type=mx_source_type, quantization="", dtype="bfloat16"
)
@pytest.mark.parametrize("mx_source_type", [None, WEIGHTS, LORA, *ARTIFACT_TYPES])
def test_exact_match_is_compatible(mx_source_type):
assert accelerators_compatible("cuda", "cuda", mx_source_type=mx_source_type)
def test_same_accelerator_lora_is_compatible():
assert accelerators_compatible("cuda", "cuda", mx_source_type=LORA)
@pytest.mark.parametrize("quantization", ["", "none", "None", " NONE "])
def test_weights_cuda_to_xpu_unquantized_allowed(quantization):
assert accelerators_compatible(
"cuda",
"xpu",
mx_source_type=WEIGHTS,
quantization=quantization,
dtype="bfloat16",
)
@pytest.mark.parametrize("quantization", ["", "none"])
def test_weights_xpu_to_cuda_unquantized_allowed(quantization):
assert accelerators_compatible(
"xpu",
"cuda",
mx_source_type=WEIGHTS,
quantization=quantization,
dtype="float16",
)
@pytest.mark.parametrize(
"quantization", ["fp8", "FP8", "awq", "gptq", "nvfp4", "modelopt_fp4"]
)
def test_weights_cross_family_quantized_rejected(quantization):
assert not accelerators_compatible(
"cuda",
"xpu",
mx_source_type=WEIGHTS,
quantization=quantization,
dtype="bfloat16",
)
assert not accelerators_compatible(
"xpu",
"cuda",
mx_source_type=WEIGHTS,
quantization=quantization,
dtype="bfloat16",
)
@pytest.mark.parametrize(
"dtype", ["float8_e4m3fn", "float8_e5m2", "fp8", "nvfp4", "mxfp8", "mxfp4"]
)
def test_weights_cross_family_quantized_dtype_rejected(dtype):
assert not accelerators_compatible(
"cuda", "xpu", mx_source_type=WEIGHTS, quantization="", dtype=dtype
)
@pytest.mark.parametrize(
"dtype", ["int8", "int4", "qint8", "uint4", "auto", "e4m3", "some_future_fp2"]
)
def test_weights_cross_family_non_allowlisted_dtype_rejected(dtype):
assert not accelerators_compatible(
"cuda", "xpu", mx_source_type=WEIGHTS, quantization="", dtype=dtype
)
assert not accelerators_compatible(
"xpu", "cuda", mx_source_type=WEIGHTS, quantization="", dtype=dtype
)
@pytest.mark.parametrize(
"dtype",
["float32", "torch.float32", "float16", "torch.bfloat16", "bf16", "half", "F32"],
)
def test_weights_cross_family_plain_dtype_aliases_allowed(dtype):
assert accelerators_compatible(
"cuda", "xpu", mx_source_type=WEIGHTS, quantization="", dtype=dtype
)
@pytest.mark.parametrize("known,unknown", [("cuda", ""), ("", "xpu")])
def test_weights_unknown_accelerator_quantized_fails_closed_when_not_deferred(
known, unknown
):
assert not accelerators_compatible(
known,
unknown,
mx_source_type=WEIGHTS,
quantization="fp8",
dtype="bfloat16",
defer_unknown=False,
)
assert not accelerators_compatible(
known,
unknown,
mx_source_type=WEIGHTS,
quantization="",
dtype="float8_e4m3fn",
defer_unknown=False,
)
@pytest.mark.parametrize("known,unknown", [("cuda", ""), ("", "xpu")])
def test_weights_unknown_accelerator_quantized_deferred_by_default(known, unknown):
assert accelerators_compatible(
known, unknown, mx_source_type=WEIGHTS, quantization="fp8", dtype="bfloat16"
)
def test_weights_same_family_quantized_survives_empty_check():
assert accelerators_compatible(
"cuda", "cuda", mx_source_type=WEIGHTS, quantization="fp8", dtype="bfloat16"
)
def test_weights_cross_family_empty_dtype_fails_closed():
assert not accelerators_compatible(
"cuda", "xpu", mx_source_type=WEIGHTS, quantization="", dtype=""
)
assert not accelerators_compatible(
"xpu", "cuda", mx_source_type=WEIGHTS, quantization="none", dtype=" "
)
def test_weights_cross_family_omitted_quantization_fails_closed():
assert not accelerators_compatible("cuda", "xpu", mx_source_type=WEIGHTS)
assert not accelerators_compatible(
"cuda", "xpu", mx_source_type=WEIGHTS, quantization=""
)
assert not accelerators_compatible(
"cuda", "xpu", mx_source_type=WEIGHTS, dtype="bfloat16"
)
@pytest.mark.parametrize("quantization", ["fp8", "awq", "nvfp4"])
def test_same_family_quantized_allowed(quantization):
assert accelerators_compatible(
"cuda",
"cuda",
mx_source_type=WEIGHTS,
quantization=quantization,
dtype="bfloat16",
)
@pytest.mark.parametrize(
"target,source",
[
("cuda", "rocm"),
("xpu", "rocm"),
("cuda", "neuron"),
("rocm", "xpu"),
],
)
def test_weights_unproven_pairs_rejected(target, source):
assert not accelerators_compatible(
target, source, mx_source_type=WEIGHTS, quantization="", dtype="bfloat16"
)
def test_unspecified_source_type_rejects_cross_family():
assert not accelerators_compatible("cuda", "xpu")
def test_lora_cross_accelerator_not_enabled_until_live_path_exists():
assert not accelerators_compatible("cuda", "xpu", mx_source_type=LORA)
@pytest.mark.parametrize("mx_source_type", ARTIFACT_TYPES)
def test_artifacts_never_cross_family(mx_source_type):
assert not accelerators_compatible("cuda", "xpu", mx_source_type=mx_source_type)
assert not accelerators_compatible("xpu", "cuda", mx_source_type=mx_source_type)