image.domain\self_evolve\param_tune.mbt

83.3% · 35 / 42 points · 164 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/param_tune
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 自适应参数调优 — 根据历史自动调优参数
  7. 7// Adaptive parameter tuning: auto-tune parameters based on history
  8. 8//
  9. 9// Phase 10 v308: 将演化从"策略组合"推向"参数调优" — 根据历史数据自动调整参数。
  10. 10
  11. 11///|
  12. 12/// 参数类型。
  13. 13/// 描述 Parameter kind.
  14. 14/// `#non_exhaustive` 表示未来可能添加新构造器
  15. 15#derive(Eq, @debug.Debug)
  16. 16#non_exhaustive
  17. 17pub(all) enum ParamKind {
  18. 18 Threshold
  19. 19 Weight
  20. 20 StepSize
  21. 21 MaxIter
  22. 22}
  23. 23
  24. 24///|
  25. 25pub extend ParamKind with @debug.Debug::{to_repr}
  26. 26
  27. 27///|
  28. 28pub extend ParamKind with Eq::{not_equal, equal}
  29. 29
  30. 30///|
  31. 31/// 单个调优参数。
  32. 32/// 描述 Single tuned parameter.
  33. 33#derive(Eq, @debug.Debug)
  34. 34pub(all) struct TunedParam {
  35. 35 param_kind : ParamKind
  36. 36 old_value : Int32
  37. 37 new_value : Int32
  38. 38 adjustment : Int32
  39. 39 rationale : String
  40. 40}
  41. 41
  42. 42///|
  43. 43pub extend TunedParam with @debug.Debug::{to_repr}
  44. 44
  45. 45///|
  46. 46pub extend TunedParam with Eq::{not_equal, equal}
  47. 47
  48. 48///|
  49. 49/// 参数调优报告。
  50. 50/// 描述 Parameter tuning report.
  51. 51#derive(Eq, @debug.Debug)
  52. 52pub(all) struct TuneReport {
  53. 53 params : Array[TunedParam]
  54. 54 total : Int32
  55. 55 adjusted_count : Int32
  56. 56 summary : String
  57. 57}
  58. 58
  59. 59///|
  60. 60pub extend TuneReport with @debug.Debug::{to_repr}
  61. 61
  62. 62///|
  63. 63pub extend TuneReport with Eq::{not_equal, equal}
  64. 64
  65. 65///|
  66. 66/// 为单个算子调优参数。
  67. 67/// 调优 Tune parameters for a single op.
  68. 68pub fn tune_params(op : @imgir.IROp) -> Array[TunedParam] {
  69. 69● let tag = op.tag()
  70. 70 let params : Array[TunedParam] = []
  71. 71● let degs = detect_degradation(op)
  72. 72● let cov = coverage_percentage(op)
  73. 73● let score = score_op(op)
  74. 74 let old_threshold = 50
  75. 75 let new_threshold = if cov < 50 {
  76. 76● 30
  77. 77● } else if cov > 80 {
  78. 78● 60
  79. 79 } else {
  80. 80● old_threshold
  81. 81 }
  82. 82 if new_threshold != old_threshold {
  83. 83● params.push({
  84. 84 param_kind: Threshold,
  85. 85 old_value: old_threshold,
  86. 86 new_value: new_threshold,
  87. 87 adjustment: new_threshold - old_threshold,
  88. 88● rationale: tag + " 覆盖 " + cov.to_string() + "%, 调整阈值",
  89. 89 })
  90. 90 }
  91. 91 let old_weight = 10
  92. 92● let new_weight = if degs.length() > 0 {
  93. 93● old_weight + degs.length() * 2
  94. 94 } else {
  95. 95● old_weight
  96. 96 }
  97. 97 if new_weight != old_weight {
  98. 98● params.push({
  99. 99 param_kind: Weight,
  100. 100 old_value: old_weight,
  101. 101 new_value: new_weight,
  102. 102 adjustment: new_weight - old_weight,
  103. 103 rationale: tag +
  104. 104 " 退化 " +
  105. 105● degs.length().to_string() +
  106. 106 " 个, 调整权重",
  107. 107 })
  108. 108 }
  109. 109 let old_step = 1
  110. 110◐ let new_step = if score.score > 70 { 2 } else { old_step }
  111. 111 if new_step != old_step {
  112. 112○ params.push({
  113. 113 param_kind: StepSize,
  114. 114 old_value: old_step,
  115. 115 new_value: new_step,
  116. 116 adjustment: new_step - old_step,
  117. 117○ rationale: tag + " 评分 " + score.score.to_string() + ", 加大步长",
  118. 118 })
  119. 119 }
  120. 120 let old_max_iter = 100
  121. 121◐ let new_max_iter = if degs.length() > 1 { 200 } else { old_max_iter }
  122. 122 if new_max_iter != old_max_iter {
  123. 123○ params.push({
  124. 124 param_kind: MaxIter,
  125. 125 old_value: old_max_iter,
  126. 126 new_value: new_max_iter,
  127. 127 adjustment: new_max_iter - old_max_iter,
  128. 128 rationale: tag + " 多退化, 增加最大迭代",
  129. 129 })
  130. 130 }
  131. 131 params
  132. 132}
  133. 133
  134. 134///|
  135. 135/// 为所有算子调优参数。
  136. 136/// 调优 Tune parameters for all ops.
  137. 137pub fn tune_all_params() -> TuneReport {
  138. 138● let params : Array[TunedParam] = []
  139. 139● for op in all_ops() {
  140. 140● for p in tune_params(op) {
  141. 141● params.push(p)
  142. 142 }
  143. 143 }
  144. 144● let total = params.length()
  145. 145 let mut adjusted = 0
  146. 146 for p in params {
  147. 147● if p.adjustment != 0 {
  148. 148● adjusted = adjusted + 1
  149. 149 }
  150. 150 }
  151. 151 let summary = "参数调优: " +
  152. 152● total.to_string() +
  153. 153 " 参数, " +
  154. 154● adjusted.to_string() +
  155. 155 " 已调整"
  156. 156 { params, total, adjusted_count: adjusted, summary, }
  157. 157}
  158. 158
  159. 159///|
  160. 160/// 调优报告是否有参数调整。
  161. 161/// 描述 Whether tune report has adjustments.
  162. 162pub fn TuneReport::has_adjustments(self : TuneReport) -> Bool {
  163. 163● self.adjusted_count > 0
  164. 164}