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