image.domain\self_evolve\pipeline_optimize.mbt

86.5% · 45 / 52 points · 203 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/pipeline_optimize
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 管道优化建议 — 基于等价性与退化分析推荐管道优化
  7. 7// Pipeline optimization suggestion: recommend optimizations based on equivalence and degradation
  8. 8//
  9. 9// Phase 8 v299: 将演化从"推荐"推向"管道优化" — 主动建议如何优化管道结构。
  10. 10
  11. 11///|
  12. 12/// 优化建议类型。
  13. 13/// 描述 Optimization suggestion kind.
  14. 14/// `#non_exhaustive` 表示未来可能添加新构造器
  15. 15#derive(Eq, @debug.Debug)
  16. 16#non_exhaustive
  17. 17pub(all) enum SuggestionKind {
  18. 18 RemoveRedundant
  19. 19 ReorderOps
  20. 20 MergeAdjacent
  21. 21 SplitPipeline
  22. 22 NoOptimization
  23. 23}
  24. 24
  25. 25///|
  26. 26pub extend SuggestionKind with @debug.Debug::{to_repr}
  27. 27
  28. 28///|
  29. 29pub extend SuggestionKind with Eq::{not_equal, equal}
  30. 30
  31. 31///|
  32. 32/// 单条管道优化建议。
  33. 33/// 描述 Single pipeline optimization suggestion.
  34. 34#derive(Eq, @debug.Debug)
  35. 35pub(all) struct PipelineOptSuggestion {
  36. 36 pipeline_desc : String
  37. 37 suggestion_kind : SuggestionKind
  38. 38 estimated_benefit : Int32
  39. 39 description : String
  40. 40}
  41. 41
  42. 42///|
  43. 43pub extend PipelineOptSuggestion with @debug.Debug::{to_repr}
  44. 44
  45. 45///|
  46. 46pub extend PipelineOptSuggestion with Eq::{not_equal, equal}
  47. 47
  48. 48///|
  49. 49/// 管道优化报告。
  50. 50/// 管线 Pipeline optimization report.
  51. 51#derive(Eq, @debug.Debug)
  52. 52pub(all) struct PipelineOptReport {
  53. 53 suggestions : Array[PipelineOptSuggestion]
  54. 54 total : Int32
  55. 55 best_benefit : Int32
  56. 56 summary : String
  57. 57}
  58. 58
  59. 59///|
  60. 60pub extend PipelineOptReport with @debug.Debug::{to_repr}
  61. 61
  62. 62///|
  63. 63pub extend PipelineOptReport with Eq::{not_equal, equal}
  64. 64
  65. 65///|
  66. 66/// 找出管道中的冗余算子标签(连续相同标签)。
  67. 67/// 查找 Find redundant op tags in pipeline (consecutive same tags).
  68. 68pub fn find_redundant_ops(pipeline : @imgir.IRPipeline) -> Array[String] {
  69. 69● let result : Array[String] = []
  70. 70 let ops = pipeline.ops
  71. 71● let n = ops.length()
  72. 72 for i in 1..<n {
  73. 73● let prev_tag = ops[i - 1].tag()
  74. 74● let curr_tag = ops[i].tag()
  75. 75 if prev_tag == curr_tag {
  76. 76● result.push(curr_tag)
  77. 77 }
  78. 78 }
  79. 79 result
  80. 80}
  81. 81
  82. 82///|
  83. 83/// 估算管道优化收益(0-100)。
  84. 84/// 描述 Estimate pipeline optimization benefit (0-100).
  85. 85pub fn estimate_benefit(pipeline : @imgir.IRPipeline) -> Int32 {
  86. 86● let ops = pipeline.ops
  87. 87● let n = ops.length()
  88. 88● let redundant = find_redundant_ops(pipeline)
  89. 89● let redundant_count = redundant.length()
  90. 90● let degs = analyze_propagation(pipeline)
  91. 91 let deg_count = degs.total_propagations
  92. 92 let benefit = redundant_count * 20 + deg_count * 5
  93. 93 if benefit > 100 {
  94. 94○ 100
  95. 95● } else if benefit == 0 && n > 5 {
  96. 96○ 10
  97. 97 } else {
  98. 98● benefit
  99. 99 }
  100. 100}
  101. 101
  102. 102///|
  103. 103/// 为管道生成优化建议。
  104. 104/// 生成 Generate optimization suggestions for a pipeline.
  105. 105pub fn suggest_optimization(pipeline : @imgir.IRPipeline) -> PipelineOptReport {
  106. 106● let suggestions : Array[PipelineOptSuggestion] = []
  107. 107● let desc = @imgir.ir_describe(pipeline)
  108. 108 let ops = pipeline.ops
  109. 109● let n = ops.length()
  110. 110● let redundant = find_redundant_ops(pipeline)
  111. 111 let mut best_benefit = 0
  112. 112● if redundant.length() > 0 {
  113. 113● let benefit = redundant.length() * 20
  114. 114 if benefit > best_benefit {
  115. 115● best_benefit = benefit
  116. 116 }
  117. 117● suggestions.push({
  118. 118 pipeline_desc: desc,
  119. 119 suggestion_kind: RemoveRedundant,
  120. 120 estimated_benefit: benefit,
  121. 121 description: "移除 " +
  122. 122● redundant.length().to_string() +
  123. 123 " 个冗余算子 (" +
  124. 124 redundant[0] +
  125. 125 " 等)",
  126. 126 })
  127. 127 }
  128. 128● let prop_report = analyze_propagation(pipeline)
  129. 129 if prop_report.total_propagations > 0 {
  130. 130● let benefit = prop_report.total_propagations * 5
  131. 131 if benefit > best_benefit {
  132. 132● best_benefit = benefit
  133. 133 }
  134. 134● suggestions.push({
  135. 135 pipeline_desc: desc,
  136. 136 suggestion_kind: ReorderOps,
  137. 137 estimated_benefit: benefit,
  138. 138 description: "重排算子以消除 " +
  139. 139● prop_report.total_propagations.to_string() +
  140. 140 " 处退化传播",
  141. 141 })
  142. 142 }
  143. 143 if n >= 2 {
  144. 144● let mut mergeable = 0
  145. 145 for i in 1..<n {
  146. 146● if ops[i - 1].tag() == ops[i].tag() {
  147. 147● mergeable = mergeable + 1
  148. 148 }
  149. 149 }
  150. 150 if mergeable > 0 {
  151. 151● let benefit = mergeable * 15
  152. 152 if benefit > best_benefit {
  153. 153○ best_benefit = benefit
  154. 154 }
  155. 155● suggestions.push({
  156. 156 pipeline_desc: desc,
  157. 157 suggestion_kind: MergeAdjacent,
  158. 158 estimated_benefit: benefit,
  159. 159 description: "合并 " +
  160. 160● mergeable.to_string() +
  161. 161 " 对相邻同类算子",
  162. 162 })
  163. 163 }
  164. 164 }
  165. 165 if n > 6 {
  166. 166○ let benefit = 10
  167. 167 if benefit > best_benefit {
  168. 168○ best_benefit = benefit
  169. 169 }
  170. 170○ suggestions.push({
  171. 171 pipeline_desc: desc,
  172. 172 suggestion_kind: SplitPipeline,
  173. 173 estimated_benefit: benefit,
  174. 174 description: "管道过长 (" +
  175. 175○ n.to_string() +
  176. 176 " 步),建议拆分为子管道",
  177. 177 })
  178. 178 }
  179. 179● if suggestions.length() == 0 {
  180. 180● suggestions.push({
  181. 181 pipeline_desc: desc,
  182. 182 suggestion_kind: NoOptimization,
  183. 183 estimated_benefit: 0,
  184. 184 description: "管道结构良好,无需优化",
  185. 185 })
  186. 186 }
  187. 187● let total = suggestions.length()
  188. 188 let summary = "管道优化: " +
  189. 189● total.to_string() +
  190. 190 " 条建议, 最高收益 " +
  191. 191● best_benefit.to_string() +
  192. 192 "(管道 " +
  193. 193 desc +
  194. 194 ")"
  195. 195 { suggestions, total, best_benefit, summary, }
  196. 196}
  197. 197
  198. 198///|
  199. 199/// 优化报告是否有优化建议。
  200. 200/// 描述 Whether optimization report has suggestions.
  201. 201pub fn PipelineOptReport::has_suggestions(self : PipelineOptReport) -> Bool {
  202. 202● self.best_benefit > 0
  203. 203}