image.domain\self_evolve\optimize_types.mbt

100% · 2 / 2 points · 74 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/optimize_types
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 算法优化类型 — 优化建议 + 优化报告
  7. 7// Optimization types: suggestion + report
  8. 8
  9. 9///|
  10. 10/// 优化类型。
  11. 11/// 描述 Optimization kind.
  12. 12/// `#non_exhaustive` 表示未来可能添加新构造器
  13. 13#derive(Eq, @debug.Debug)
  14. 14#non_exhaustive
  15. 15pub(all) enum OptimizationKind {
  16. 16 RemoveRedundant // 消除冗余算子对(如 FlipH ∘ FlipH)
  17. 17 RemoveIdentity // 消除恒等算子(如 Brightness(0))
  18. 18 MergeAdjacent // 合并相邻同类算子(如 Brightness(a) ∘ Brightness(b) → Brightness(a+b))
  19. 19}
  20. 20
  21. 21///|
  22. 22pub extend OptimizationKind with @debug.Debug::{to_repr}
  23. 23
  24. 24///|
  25. 25pub extend OptimizationKind with Eq::{not_equal, equal}
  26. 26
  27. 27///|
  28. 28/// 优化建议。
  29. 29/// 描述 Optimization suggestion.
  30. 30#derive(Eq, @debug.Debug)
  31. 31pub(all) struct OptimizationSuggestion {
  32. 32 kind : OptimizationKind
  33. 33 op_indices : Array[Int32]
  34. 34 description : String
  35. 35 before_count : Int32
  36. 36 after_count : Int32
  37. 37}
  38. 38
  39. 39///|
  40. 40pub extend OptimizationSuggestion with @debug.Debug::{to_repr}
  41. 41
  42. 42///|
  43. 43pub extend OptimizationSuggestion with Eq::{not_equal, equal}
  44. 44
  45. 45///|
  46. 46/// 优化报告。
  47. 47/// 描述 Optimization report.
  48. 48#derive(Eq, @debug.Debug)
  49. 49pub(all) struct OptimizationReport {
  50. 50 original_length : Int32
  51. 51 optimized_length : Int32
  52. 52 suggestions : Array[OptimizationSuggestion]
  53. 53 optimized_pipeline : @imgir.IRPipeline
  54. 54}
  55. 55
  56. 56///|
  57. 57pub extend OptimizationReport with @debug.Debug::{to_repr}
  58. 58
  59. 59///|
  60. 60pub extend OptimizationReport with Eq::{not_equal, equal}
  61. 61
  62. 62///|
  63. 63/// 优化是否减少了算子数。
  64. 64/// 描述 Whether optimization reduced op count.
  65. 65pub fn OptimizationReport::is_reduced(self : OptimizationReport) -> Bool {
  66. 66● self.optimized_length < self.original_length
  67. 67}
  68. 68
  69. 69///|
  70. 70/// 减少的算子数。
  71. 71/// 描述 Number of ops removed.
  72. 72pub fn OptimizationReport::reduction(self : OptimizationReport) -> Int32 {
  73. 73● self.original_length - self.optimized_length
  74. 74}