执行摘要
- 一句话:跳过因 terratorch 缺失而失败的测试
- 推荐动作:值得快速审阅,以确认跳过逻辑正确,确保 test_initialization.py 和 test_registry.py 中的内联检查与现有模式一致,且模块级 skipif 按预期工作。
功能与动机
参见 PR 描述:在将 terratorch 从测试依赖中移除后,两项模型注册表参数化测试(test_can_initialize_small_subset[PrithviGeoSpatialMAE] 和 test_registry_imports[PrithviGeoSpatialMAE/Terratorch])在每次针对当前主分支重跑这些作业的 PR 上都会失败。这些测试通过模型注册表的间接参数化方式覆盖,因此不受 #41377 中添加的每个文件的 pytest.importorskip 的保护。
实现拆解
- tests/models/test_registry.py:在
test_registry_imports 函数中,在加载模型类之前,为 PrithviGeoSpatialMAE 和 Terratorch 添加了内联检查,如果 importlib.util.find_spec("terratorch") 返回 None,则跳过测试。
- tests/models/test_initialization.py:在
test_can_initialize_small_subset 函数中,在与 MoonshotKimiaForCausalLM 类似的现有跳过之后,添加了相同的检查。
- tests/models/multimodal/pooling/test_prithvi_mae.py:将模块级的
pytest.importorskip("terratorch") 替换为 pytestmark = pytest.mark.skipif(importlib.util.find_spec("terratorch") is None, ...)。这避免了在收集期间引发跳过异常,防止出现“0 collected, 1 skipped”的退出码。
- tests/models/test_terratorch.py:同样的模块级替换。
- tests/plugins_tests/test_terratorch_io_processor_plugins.py:同样的模块级替换。
关键文件:
tests/models/test_registry.py(模块 注册表;类别 test;类型 test-coverage;符号 test_registry_imports): 对参数化测试 test_registry_imports 中的 PrithviGeoSpatialMAE 和 Terratorch 应用内联跳过,以避免在 terratorch 缺失时断言失败。
tests/models/test_initialization.py(模块 初始化;类别 test;类型 test-coverage;符号 test_can_initialize_small_subset): 在 test_can_initialize_small_subset 中添加相同的条件跳过,遵循已有模式(如 MoonshotKimiaForCausalLM)。
tests/models/multimodal/pooling/test_prithvi_mae.py(模块 Prithvi MAE;类别 test;类型 test-coverage): 将模块级的 importorskip 替换为 pytestmark.skipif,避免了“no tests collected”退出码问题。
tests/models/test_terratorch.py(模块 Terratorch;类别 test;类型 test-coverage): 与 test_prithvi_mae.py 相同,将模块级的 importorskip 替换为 skipif。
tests/plugins_tests/test_terratorch_io_processor_plugins.py(模块 Terratorch 插件;类别 test;类型 test-coverage): 相同的模块级替换。
关键符号:test_registry_imports, test_can_initialize_small_subset
关键源码片段
tests/models/test_registry.py
对参数化测试 test_registry_imports 中的 PrithviGeoSpatialMAE 和 Terratorch 应用内联跳过,以避免在 terratorch 缺失时断言失败。
# tests/models/test_registry.py (source fragment)
def test_registry_imports(model_arch):
# Skip if transformers version is incompatible
model_info = HF_EXAMPLE_MODELS.get_hf_info(model_arch)
model_info.check_transformers_version(
on_fail="skip",
check_max_version=False,
check_version_reason="vllm",
)
# 新增 : 如果 terratorch 未安装 , 则跳过 Prithvi 和 Terratorch 的测试
if model_arch in ("PrithviGeoSpatialMAE", "Terratorch"):
import importlib.util
if importlib.util.find_spec("terratorch") is None:
pytest.skip(
"terratorch is not installed; "
"temporarily skipped while PyPI has `lightning` quarantined "
"(see #41376)"
)
# Ensure all model classes can be imported successfully
model_cls = ModelRegistry._try_load_model_cls(model_arch)
assert model_cls is not None
tests/models/test_initialization.py
在 test_can_initialize_small_subset 中添加相同的条件跳过,遵循已有模式(如 MoonshotKimiaForCausalLM)。
# tests/models/test_initialization.py (source fragment)
if model_arch == "MoonshotKimiaForCausalLM":
pytest.skip(
"Kimi-Audio requires SpeechToTextConfig "
"which is not configured in test environment"
)
# 新增 : 如果 remove terratorch 未安装 , 则跳过 Prithvi/Terratorch
if model_arch in ("PrithviGeoSpatialMAE", "Terratorch"):
import importlib.util
if importlib.util.find_spec("terratorch") is None:
pytest.skip(
"terratorch is not installed; "
"temporarily skipped while PyPI has `lightning` quarantined "
"(see #41376)"
)
if model_arch in ["DeepseekV32ForCausalLM", "GlmMoeDsaForCausalLM"]:
from vllm.platforms import current_platform
tests/models/multimodal/pooling/test_prithvi_mae.py
将模块级的 importorskip 替换为 pytestmark.skipif,避免了“no tests collected”退出码问题。
# tests/models/multimodal/pooling/test_prithvi_mae.py (head version)
import importlib.util
import pytest
import torch
from ....conftest import VllmRunner
# 替换原来的 pytest.importorskip("terratorch", ...)
# 使用 pytestmark.skipif 避免收集时跳过导致 exit code 5
pytestmark = pytest.mark.skipif(
importlib.util.find_spec("terratorch") is None,
reason="terratorch unavailable while PyPI has `lightning` quarantined; see #41376",
)
评论区精华
无显著讨论。提交历史中的第二次提交说明:模块级的 pytest.importorskip("terratorch") 导致 plugin-tests-2-gpus 作业以状态码 5(“未收集到测试”)退出,因为 importorskip 在收集时抛出了异常——pytest 报告“0 collected, 1 skipped”,这不是可接受的通过状态。因此将其转换为 pytestmark.skipif。
- importorskip 导致 exit code 5 (testing): 使用模块级的 skipif 标记替换 importorskip,以避免收集时立刻跳过。
风险与影响
- 风险:风险很低。这些变更仅影响测试跳过逻辑,不涉及任何生产代码。当 terratorch 可用时,测试按原样运行。使用
importlib.util.find_spec 不存在运行时性能影响。
- 影响:影响仅限于 CI 测试:修复了因外部依赖临时不可用而导致的回归,确保了主分支 CI 的稳定性。对其他团队或用户无影响。待 terratorch 恢复后,这些跳过将自动失效。
- 风险标记:仅测试变更, 低风险
关联脉络
- PR #41377 [CI/Build] Skip terratorch + torchgeo while PyPI has lightning quarantined: 本 PR 是 #41377 的后续:从测试依赖中移除 terratorch 后,模型注册表参数化测试因缺少 per-file importorskip 覆盖而失败。
参与讨论