image.domain\self_evolve\equivalence.mbt

97.4% · 38 / 39 points · 147 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/equivalence
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 算子等价性验证 — 验证两个算子序列是否等价
  7. 7// Op equivalence verification: verify whether two op sequences are equivalent
  8. 8//
  9. 9// Phase 7 v294: 将演化从"独立"推向"等价验证" — 证明两个管道产生相同结果。
  10. 10
  11. 11///|
  12. 12/// 单次等价性检查结果。
  13. 13/// 描述 Single equivalence check result.
  14. 14#derive(Eq, @debug.Debug)
  15. 15pub(all) struct EquivalenceCheck {
  16. 16 pipeline_a_desc : String
  17. 17 pipeline_b_desc : String
  18. 18 is_equivalent : Bool
  19. 19 reason : String
  20. 20}
  21. 21
  22. 22///|
  23. 23pub extend EquivalenceCheck with @debug.Debug::{to_repr}
  24. 24
  25. 25///|
  26. 26pub extend EquivalenceCheck with Eq::{not_equal, equal}
  27. 27
  28. 28///|
  29. 29/// 等价性验证报告。
  30. 30/// 描述 Equivalence verification report.
  31. 31#derive(Eq, @debug.Debug)
  32. 32pub(all) struct EquivalenceReport {
  33. 33 checks : Array[EquivalenceCheck]
  34. 34 total_equivalent : Int32
  35. 35 total_not_equivalent : Int32
  36. 36 summary : String
  37. 37}
  38. 38
  39. 39///|
  40. 40pub extend EquivalenceReport with @debug.Debug::{to_repr}
  41. 41
  42. 42///|
  43. 43pub extend EquivalenceReport with Eq::{not_equal, equal}
  44. 44
  45. 45///|
  46. 46/// 检查两个管道是否等价(相同算子序列)。
  47. 47/// 检查 Check whether two pipelines are equivalent (same op sequence).
  48. 48pub fn check_equivalence(
  49. 49 pipe_a : @imgir.IRPipeline,
  50. 50 pipe_b : @imgir.IRPipeline,
  51. 51) -> EquivalenceCheck {
  52. 52● let desc_a = @imgir.ir_describe(pipe_a)
  53. 53● let desc_b = @imgir.ir_describe(pipe_b)
  54. 54 let ops_a = pipe_a.ops
  55. 55 let ops_b = pipe_b.ops
  56. 56● let len_a = ops_a.length()
  57. 57● let len_b = ops_b.length()
  58. 58 let mut is_equiv = true
  59. 59 let mut reason = ""
  60. 60 if len_a != len_b {
  61. 61● is_equiv = false
  62. 62● reason = "长度不同: " + len_a.to_string() + " vs " + len_b.to_string()
  63. 63 } else {
  64. 64● for i in 0..<len_a {
  65. 65● let tag_a = ops_a[i].tag()
  66. 66● let tag_b = ops_b[i].tag()
  67. 67 if tag_a != tag_b {
  68. 68● is_equiv = false
  69. 69● reason = "位置 " + i.to_string() + ": " + tag_a + " vs " + tag_b
  70. 70 break
  71. 71 }
  72. 72 }
  73. 73 if is_equiv {
  74. 74● reason = "算子序列完全相同"
  75. 75 }
  76. 76 }
  77. 77 {
  78. 78 pipeline_a_desc: desc_a,
  79. 79 pipeline_b_desc: desc_b,
  80. 80 is_equivalent: is_equiv,
  81. 81 reason,
  82. 82 }
  83. 83}
  84. 84
  85. 85///|
  86. 86/// 简化等价判断。
  87. 87/// 描述 Simplified equivalence check.
  88. 88pub fn are_equivalent(
  89. 89 pipe_a : @imgir.IRPipeline,
  90. 90 pipe_b : @imgir.IRPipeline,
  91. 91) -> Bool {
  92. 92● check_equivalence(pipe_a, pipe_b).is_equivalent
  93. 93}
  94. 94
  95. 95///|
  96. 96/// 检查所有算子的自等价性(每个算子与自己等价)。
  97. 97/// 检查 Check self-equivalence for all ops.
  98. 98pub fn check_equivalence_all() -> EquivalenceReport {
  99. 99● let checks : Array[EquivalenceCheck] = []
  100. 100● let ops = all_ops()
  101. 101 for op in ops {
  102. 102● let pipe_a = @imgir.IRPipeline::new().add(op)
  103. 103● let pipe_b = @imgir.IRPipeline::new().add(op)
  104. 104● checks.push(check_equivalence(pipe_a, pipe_b))
  105. 105 }
  106. 106 let mut equiv_count = 0
  107. 107 let mut non_equiv_count = 0
  108. 108 for c in checks {
  109. 109● if c.is_equivalent {
  110. 110● equiv_count = equiv_count + 1
  111. 111 } else {
  112. 112○ non_equiv_count = non_equiv_count + 1
  113. 113 }
  114. 114 }
  115. 115 let summary = "等价性验证: " +
  116. 116● equiv_count.to_string() +
  117. 117 " 等价, " +
  118. 118● non_equiv_count.to_string() +
  119. 119 " 不等价"
  120. 120 {
  121. 121 checks,
  122. 122 total_equivalent: equiv_count,
  123. 123 total_not_equivalent: non_equiv_count,
  124. 124 summary,
  125. 125 }
  126. 126}
  127. 127
  128. 128///|
  129. 129/// 获取与指定算子等价的算子标签(相同 tag 的算子)。
  130. 130/// 获取 Get equivalent op tags (ops with same tag).
  131. 131pub fn equivalence_class(op : @imgir.IROp) -> Array[String] {
  132. 132● let target_tag = op.tag()
  133. 133 let result : Array[String] = []
  134. 134● for other in all_ops() {
  135. 135● if other.tag() == target_tag {
  136. 136● result.push(other.tag())
  137. 137 }
  138. 138 }
  139. 139 result
  140. 140}
  141. 141
  142. 142///|
  143. 143/// 等价性报告是否全部等价。
  144. 144/// 描述 Whether all checks are equivalent.
  145. 145pub fn EquivalenceReport::all_equivalent(self : EquivalenceReport) -> Bool {
  146. 146● self.total_not_equivalent == 0
  147. 147}