image.domain\self_evolve\grill.mbt

93.8% · 75 / 80 points · 289 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 烧烤测试 self_evolve/grill
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// grill-me 拷问引擎 — 4.7.4 领域规则可执行化
  7. 7// Grill engine: executable 4.7.4 domain rules
  8. 8//
  9. 9// 五条拷问规则:
  10. 10// 1. 数值鲁棒性:除零 / 奇异矩阵 / NaN / Inf / 溢出
  11. 11// 2. 退化几何:除零 / 共线点 / 退化三角形 / 零面积
  12. 12// 3. 场间断:边界不连续 / 梯度不存在 / 插值外推
  13. 13// 4. 时空拓扑:Allen 代数 / 时间区间 / 单调性
  14. 14// 5. 四目标兼容:纯 MoonBit 无 C FFI(静态保证)
  15. 15
  16. 16///|
  17. 17/// 拷问规则(对应 4.7.4 五条领域规则)。
  18. 18/// 描述 Grill rule (corresponds to 4.7.4 domain rules).
  19. 19/// `#non_exhaustive` 表示未来可能添加新构造器
  20. 20#derive(Eq, @debug.Debug)
  21. 21#non_exhaustive
  22. 22pub(all) enum GrillRule {
  23. 23 NumericRobustness // 数值鲁棒性
  24. 24 DegenerateGeometry // 退化几何
  25. 25 FieldDiscontinuityRule // 场间断
  26. 26 SpatiotemporalTopology // 时空拓扑
  27. 27 MultiTargetCompat // 四目标兼容
  28. 28}
  29. 29
  30. 30///|
  31. 31pub extend GrillRule with @debug.Debug::{to_repr}
  32. 32
  33. 33///|
  34. 34pub extend GrillRule with Eq::{not_equal, equal}
  35. 35
  36. 36///|
  37. 37/// 单条拷问结果。
  38. 38/// 描述 Single grill result.
  39. 39#derive(Eq, @debug.Debug)
  40. 40pub(all) struct GrillResult {
  41. 41 rule : GrillRule
  42. 42 target : String
  43. 43 passed : Bool
  44. 44 reason : String
  45. 45}
  46. 46
  47. 47///|
  48. 48pub extend GrillResult with @debug.Debug::{to_repr}
  49. 49
  50. 50///|
  51. 51pub extend GrillResult with Eq::{not_equal, equal}
  52. 52
  53. 53///|
  54. 54/// 拷问报告。
  55. 55/// 描述 Grill report.
  56. 56#derive(Eq, @debug.Debug)
  57. 57pub(all) struct GrillReport {
  58. 58 results : Array[GrillResult]
  59. 59 target_desc : String
  60. 60}
  61. 61
  62. 62///|
  63. 63pub extend GrillReport with @debug.Debug::{to_repr}
  64. 64
  65. 65///|
  66. 66pub extend GrillReport with Eq::{not_equal, equal}
  67. 67
  68. 68///|
  69. 69/// 全部 5 条拷问规则。
  70. 70/// 描述 All 5 grill rules.
  71. 71pub fn grill_rules() -> Array[GrillRule] {
  72. 72● [
  73. 73 NumericRobustness,
  74. 74 DegenerateGeometry,
  75. 75 FieldDiscontinuityRule,
  76. 76 SpatiotemporalTopology,
  77. 77 MultiTargetCompat,
  78. 78 ]
  79. 79}
  80. 80
  81. 81///|
  82. 82/// 规则描述。
  83. 83/// 描述 Rule description.
  84. 84pub fn grill_rule_desc(rule : GrillRule) -> String {
  85. 85● match rule {
  86. 86● NumericRobustness => "数值鲁棒性:除零/奇异矩阵/NaN/Inf/溢出"
  87. 87● DegenerateGeometry => "退化几何:除零/共线点/零面积"
  88. 88 FieldDiscontinuityRule =>
  89. 89● "场间断:边界不连续/梯度不存在/插值外推"
  90. 90 SpatiotemporalTopology =>
  91. 91● "时空拓扑:Allen代数/时间区间/单调性"
  92. 92● MultiTargetCompat => "四目标兼容:纯MoonBit无CFFI"
  93. 93 }
  94. 94}
  95. 95
  96. 96// 判断退化类型是否数值类
  97. 97
  98. 98///|
  99. 99fn is_numeric_kind(k : DegradationKind) -> Bool {
  100. 100● match k {
  101. 101● DivisionByZero => true
  102. 102○ SingularMatrix => true
  103. 103○ NaNValue => true
  104. 104○ InfValue => true
  105. 105○ Overflow => true
  106. 106● FieldDiscontinuity => false
  107. 107 }
  108. 108}
  109. 109
  110. 110// 统计数值类退化数
  111. 111
  112. 112///|
  113. 113fn count_numeric_degradations(degs : Array[DegradationCase]) -> Int32 {
  114. 114● let mut count = 0
  115. 115 for d in degs {
  116. 116● if is_numeric_kind(d.kind) {
  117. 117● count = count + 1
  118. 118 }
  119. 119 }
  120. 120 count
  121. 121}
  122. 122
  123. 123// 统计除零退化数(退化几何)
  124. 124
  125. 125///|
  126. 126fn count_divzero_degradations(degs : Array[DegradationCase]) -> Int32 {
  127. 127● let mut count = 0
  128. 128 for d in degs {
  129. 129● if d.kind == DivisionByZero {
  130. 130● count = count + 1
  131. 131 }
  132. 132 }
  133. 133 count
  134. 134}
  135. 135
  136. 136// 统计场间断违规数(FieldClamp lo>hi / FieldMask 极端阈值)
  137. 137
  138. 138///|
  139. 139fn count_field_discontinuity(ops : Array[@imgir.IROp]) -> Int32 {
  140. 140● let mut count = 0
  141. 141 for op in ops {
  142. 142● match op {
  143. 143● FieldClamp(lo, hi) => if lo > hi { count = count + 1 }
  144. 144 FieldMask(threshold) =>
  145. 145● if threshold > 1000000.0F || threshold < -1000000.0F {
  146. 146○ count = count + 1
  147. 147 }
  148. 148● _ => ()
  149. 149 }
  150. 150 }
  151. 151 count
  152. 152}
  153. 153
  154. 154// 统计时空拓扑违规数(STSnapshot/STInterpolate t 越界 [0,1])
  155. 155
  156. 156///|
  157. 157fn count_spatiotemporal_violation(ops : Array[@imgir.IROp]) -> Int32 {
  158. 158● let mut count = 0
  159. 159 for op in ops {
  160. 160● match op {
  161. 161● STSnapshot(t) => if t < 0.0F || t > 1.0F { count = count + 1 }
  162. 162● STInterpolate(t) => if t < 0.0F || t > 1.0F { count = count + 1 }
  163. 163● _ => ()
  164. 164 }
  165. 165 }
  166. 166 count
  167. 167}
  168. 168
  169. 169///|
  170. 170/// 对管道执行 5 条拷问规则检查。
  171. 171/// 运行 Run 5 grill rules on a pipeline.
  172. 172pub fn run_grill_check(pipeline : @imgir.IRPipeline) -> GrillReport {
  173. 173● let ops = pipeline.ops
  174. 174● let degs = scan_degradations(ops)
  175. 175● let desc = @imgir.ir_describe(pipeline)
  176. 176 let results : Array[GrillResult] = []
  177. 177 // 规则 1: 数值鲁棒性
  178. 178● let numeric_fail = count_numeric_degradations(degs)
  179. 179● results.push({
  180. 180 rule: NumericRobustness,
  181. 181 target: desc,
  182. 182 passed: numeric_fail == 0,
  183. 183 reason: if numeric_fail == 0 {
  184. 184● "无数值退化"
  185. 185 } else {
  186. 186● "发现 " +
  187. 187● numeric_fail.to_string() +
  188. 188 " 处数值退化(除零/奇异/NaN/Inf/溢出)"
  189. 189 },
  190. 190 })
  191. 191 // 规则 2: 退化几何
  192. 192● let geom_fail = count_divzero_degradations(degs)
  193. 193● results.push({
  194. 194 rule: DegenerateGeometry,
  195. 195 target: desc,
  196. 196 passed: geom_fail == 0,
  197. 197 reason: if geom_fail == 0 {
  198. 198● "无退化几何"
  199. 199 } else {
  200. 200● "发现 " +
  201. 201● geom_fail.to_string() +
  202. 202 " 处退化几何(除零/零面积)"
  203. 203 },
  204. 204 })
  205. 205 // 规则 3: 场间断
  206. 206● let field_fail = count_field_discontinuity(ops)
  207. 207● results.push({
  208. 208 rule: FieldDiscontinuityRule,
  209. 209 target: desc,
  210. 210 passed: field_fail == 0,
  211. 211 reason: if field_fail == 0 {
  212. 212● "无场间断"
  213. 213 } else {
  214. 214● "发现 " +
  215. 215● field_fail.to_string() +
  216. 216 " 处场间断(边界不连续/插值外推)"
  217. 217 },
  218. 218 })
  219. 219 // 规则 4: 时空拓扑
  220. 220● let st_fail = count_spatiotemporal_violation(ops)
  221. 221● results.push({
  222. 222 rule: SpatiotemporalTopology,
  223. 223 target: desc,
  224. 224 passed: st_fail == 0,
  225. 225 reason: if st_fail == 0 {
  226. 226● "时空拓扑一致"
  227. 227 } else {
  228. 228● "发现 " +
  229. 229● st_fail.to_string() +
  230. 230 " 处时空拓扑违规(t 越界 [0,1])"
  231. 231 },
  232. 232 })
  233. 233 // 规则 5: 四目标兼容(静态保证)
  234. 234● results.push({
  235. 235 rule: MultiTargetCompat,
  236. 236 target: desc,
  237. 237 passed: true,
  238. 238 reason: "纯 MoonBit 无 C FFI,四目标共用代码",
  239. 239 })
  240. 240 { results, target_desc: desc, }
  241. 241}
  242. 242
  243. 243///|
  244. 244/// 对全部 19 种算子执行拷问。
  245. 245/// 运行 Run grill on all 19 op variants.
  246. 246pub fn run_grill_all() -> GrillReport {
  247. 247● let results : Array[GrillResult] = []
  248. 248● for op in all_ops() {
  249. 249● let pipe = @imgir.IRPipeline::new().add(op)
  250. 250● let report = run_grill_check(pipe)
  251. 251 for r in report.results {
  252. 252● results.push(r)
  253. 253 }
  254. 254 }
  255. 255 { results, target_desc: "全部 19 种算子", }
  256. 256}
  257. 257
  258. 258///|
  259. 259/// 全部规则是否通过。
  260. 260/// 描述 Whether all rules passed.
  261. 261pub fn GrillReport::all_passed(self : GrillReport) -> Bool {
  262. 262● let mut ok = true
  263. 263 for r in self.results {
  264. 264● if !r.passed {
  265. 265● ok = false
  266. 266 }
  267. 267 }
  268. 268 ok
  269. 269}
  270. 270
  271. 271///|
  272. 272/// 失败规则数。
  273. 273/// 描述 Number of failed rules.
  274. 274pub fn GrillReport::failed_count(self : GrillReport) -> Int32 {
  275. 275● let mut count = 0
  276. 276 for r in self.results {
  277. 277● if !r.passed {
  278. 278● count = count + 1
  279. 279 }
  280. 280 }
  281. 281 count
  282. 282}
  283. 283
  284. 284///|
  285. 285/// 通过规则数。
  286. 286/// 描述 Number of passed rules.
  287. 287pub fn GrillReport::passed_count(self : GrillReport) -> Int32 {
  288. 288● self.results.length() - self.failed_count()
  289. 289}