Open
【11月贡献大赛-Bug修复】fix(image-processor-config-auditor): 拦截负 image_std 归一化标准差(Fixes #4171) #7338
【11月贡献大赛-Bug修复】fix(image-processor-config-auditor): 拦截负 image_std 归一化标准差(Fixes #4171) #7338
Open
W8FXN182FAdd on 9 hours ago
Total: 3 files changed+101-2
@@ -0,0 +1,21 @@
1+# Ascend Image Processor Config Auditor
2+ 
3+审计 Ascend 视觉/多模态推理前保存的 Hugging Face `preprocessor_config.json` 图像预处理契约。工具离线、零依赖、只读,无需导入远端模型代码即可校验配置。
4+ 
5+## 用法
6+ 
7+```bash
8+python scripts/audit_image_processor_config.py /path/to/model
9+```
10+ 
11+命令输出确定性 JSON,退出码:0 READY、1 REVIEW、2 BLOCK、3 INPUT_ERROR。
12+ 
13+## 判定
14+ 
15+- `BLOCK`:启用的预处理阶段缺少必需参数、标准字段类型/形状非法、归一化标准差含零或负值、JSON 输入损坏或歧义。
16+- `REVIEW`:字段在其开关下未生效、padding 需求与模型相关、rescale 退化、处理器标识缺失、声明通道数不一致。
17+- `READY`:保存字段在已实现的通用契约下自洽。
18+ 
19+## 边界
20+ 
21+模型专用处理器可能扩展或替换通用流水线。本审计不保证远端代码安全、颜色语义、tokenizer/处理器组合、数值等价、输出质量、显存适配或 Ascend NPU 执行;运行时关键字参数可覆盖保存文件,需以生效配置为准。
@@ -130,8 +130,12 @@ def audit(config: dict[str, Any], model: dict[str, Any] | None) -> dict[str, Any
130 findings.append(finding("BLOCK", "INVALID_NORMALIZATION_VECTOR", f"{key} must be a finite number or non-empty list of finite numbers"))130 findings.append(finding("BLOCK", "INVALID_NORMALIZATION_VECTOR", f"{key} must be a finite number or non-empty list of finite numbers"))
131 else:131 else:
132 vectors[key] = vector132 vectors[key] = vector
133- if "image_std" in vectors and any(value == 0 for value in vectors["image_std"]):133+ if "image_std" in vectors:
134- findings.append(finding("BLOCK", "ZERO_NORMALIZATION_STD", "image_std cannot contain zero"))134+ std_values = vectors["image_std"]
135+ if any(value == 0 for value in std_values):
136+ findings.append(finding("BLOCK", "ZERO_NORMALIZATION_STD", "image_std cannot contain zero"))
137+ if any(value < 0 for value in std_values):
138+ findings.append(finding("BLOCK", "NEGATIVE_NORMALIZATION_STD", "image_std cannot contain negative values"))
135 if "image_mean" in vectors and "image_std" in vectors:139 if "image_mean" in vectors and "image_std" in vectors:
136 mean_length = len(vectors["image_mean"])140 mean_length = len(vectors["image_mean"])
137 std_length = len(vectors["image_std"])141 std_length = len(vectors["image_std"])
@@ -0,0 +1,74 @@
1+import importlib.util
2+import json
3+import subprocess
4+import sys
5+import tempfile
6+import unittest
7+from pathlib import Path
8+ 
9+ 
10+ROOT = Path(__file__).resolve().parents[1]
11+SCRIPT = ROOT / "scripts" / "audit_image_processor_config.py"
12+SPEC = importlib.util.spec_from_file_location("audit_image_processor_config_negstd", SCRIPT)
13+MODULE = importlib.util.module_from_spec(SPEC)
14+assert SPEC and SPEC.loader
15+SPEC.loader.exec_module(MODULE)
16+ 
17+ 
18+def codes(report):
19+ return {item["code"] for item in report["findings"]}
20+ 
21+ 
22+class NegativeImageStdRegressionTests(unittest.TestCase):
23+ def test_negative_std_blocks(self):
24+ report = MODULE.audit(
25+ {
26+ "image_processor_type": "CLIPImageProcessor",
27+ "do_normalize": True,
28+ "image_mean": [0.5, 0.5, 0.5],
29+ "image_std": [-0.5, 0.5, 0.5],
30+ },
31+ {"num_channels": 3},
32+ )
33+ self.assertEqual("BLOCK", report["decision"])
34+ self.assertIn("NEGATIVE_NORMALIZATION_STD", codes(report))
35+ 
36+ def test_positive_std_vector_stays_ready(self):
37+ report = MODULE.audit(
38+ {
39+ "image_processor_type": "CLIPImageProcessor",
40+ "do_normalize": True,
41+ "image_mean": [0.5, 0.5, 0.5],
42+ "image_std": [0.5, 0.5, 0.5],
43+ },
44+ {"num_channels": 3},
45+ )
46+ self.assertEqual("READY", report["decision"])
47+ self.assertNotIn("NEGATIVE_NORMALIZATION_STD", codes(report))
48+ 
49+ def test_zero_std_rule_preserved(self):
50+ report = MODULE.audit({"image_std": [1.0, 0.0, 1.0]}, {})
51+ self.assertIn("ZERO_NORMALIZATION_STD", codes(report))
52+ 
53+ def test_negative_std_cli_exit_code(self):
54+ with tempfile.TemporaryDirectory() as directory:
55+ root = Path(directory)
56+ config = {
57+ "image_processor_type": "CLIPImageProcessor",
58+ "do_normalize": True,
59+ "image_mean": [0.5, 0.5, 0.5],
60+ "image_std": [-0.5, 0.5, 0.5],
61+ }
62+ (root / "preprocessor_config.json").write_text(json.dumps(config), encoding="utf-8")
63+ completed = subprocess.run(
64+ [sys.executable, str(SCRIPT), str(root)],
65+ capture_output=True,
66+ text=True,
67+ check=False,
68+ )
69+ self.assertEqual(2, completed.returncode)
70+ self.assertIn("NEGATIVE_NORMALIZATION_STD", completed.stdout)
71+ 
72+ 
73+if __name__ == "__main__":
74+ unittest.main()