ir_model 教程

本教程直接以 Plot IR 构建报告文档,这样你就可以发布并非来自 JSONL 流的结果,例如调优扫描或用 stats 计算的摘要。

我想要用法
手动构建报告文档@ir_model.PlotDocument::new
按规模和系列各绘制一个值带 PlotPoint::new 的 @ir_model.Plot::new
选择图表的绘制方式@ir_model.PlotKind
按数值间隔摆放数值型 x 值在 @ir_model.Plot::new 中使用 x_axis=Linear
在图注中解释图表在 @ir_model.Plot::new 中使用 x_label= 和 note=
附上不匹配项和反例@ir_model.DifferentialReport::new
渲染文档@report.html, @report.plot_json, @report.plot_svg

快速上手

moon add Luna-Flow/mare_mark@0.4.2
import {
  "Luna-Flow/mare_mark/model",
  "Luna-Flow/mare_mark/ir_model",
  "Luna-Flow/mare_mark/report",
  "Luna-Flow/mare_mark/stats",
}
test "a document by hand" {
  let points = [
    @ir_model.PlotPoint::new("64", 0.8, "scalar"),
    @ir_model.PlotPoint::new("128", 3.1, "scalar"),
    @ir_model.PlotPoint::new("64", 0.5, "blocked"),
    @ir_model.PlotPoint::new("128", 1.4, "blocked"),
  ]
  let plot = @ir_model.Plot::new(@ir_model.PlotKind::Scaling, "GEMM time", "ms/op", "median", points)
  let document = @ir_model.PlotDocument::new("sweep-1", "native", [plot])
  inspect(@report.html(document).contains("GEMM time"), content="true")
}

日常任务

绘制比较结果

把 stats 的比较结果转化为区间图,每个数据集一个点:

test "interval plot from comparisons" {
  let baseline = [[100.0, 101.0, 99.0], [400.0, 404.0, 398.0]]
  let candidate = [[90.0, 92.0, 91.0], [410.0, 409.0, 412.0]]
  let points = []
  for dataset in 0..<2 {
    let result = @stats.compare_paired(
      "baseline", "candidate", baseline[dataset], candidate[dataset], 2.0, @model.confirmatory_interval(),
    )
    points.push(@ir_model.PlotPoint::new(dataset.to_string(), result.relative_delta_pct, "candidate"))
  }
  let plot = @ir_model.Plot::new(@ir_model.PlotKind::Interval, "Relative median delta", "%", "point estimate", points)
  inspect(plot.points[0].y, content="-9")
  inspect(plot.points[1].y, content="2.5")
}

把数值规模放在线性轴上

默认情况下,x 值是类别,按首次出现的顺序等距排列。大小、线程数和其他数字放在线性轴上更易读,在线性轴上 1024 与 4096 之间的距离是 1024 与 2048 之间距离的三倍:

test "a linear axis with a label and a note" {
  let points = [
    @ir_model.PlotPoint::new("4096", 40.6, "scalar"),
    @ir_model.PlotPoint::new("1024", 10.1, "scalar"),
    @ir_model.PlotPoint::new("2048", 20.3, "scalar"),
  ]
  let plot = @ir_model.Plot::new(
    @ir_model.PlotKind::Scaling,
    "Vector add",
    "µs/op",
    "median",
    points,
    x_axis=Linear,
    x_label="elements",
    note="Median of 8 confirmatory blocks per size.",
  )
  let svg = @report.plot_svg(plot)
  inspect(svg.contains(">elements</text>"), content="true")
  let page = @report.html(@ir_model.PlotDocument::new("sizes", "native", [plot]))
  inspect(page.contains("Median of 8 confirmatory blocks per size."), content="true")
}

渲染器按数值对值排序,因此点的顺序无关紧要。如果某个 x 值不是有限数,它会回退到类别。

附加正确性证据

test "differential evidence" {
  let corpus = @ir_model.CorpusSummary::new(200, 197, 2, 1, 0)
  let report = @ir_model.DifferentialReport::new([], [@ir_model.CapabilityCell::new("fast", "unsupported", 1)], [], corpus)
  let document = @ir_model.PlotDocument::new("corpus-run", "native", [], differential=report)
  inspect(@report.html(document).contains("failed 2"), content="true")
}

更进一步

用 @report.plot_svg 渲染,把一张图表嵌入你自己的页面,或者用 @report.plot_json 导出给其他工具;参见 report API。

常见陷阱

  • 分类轴上的数值型 x 值。 它们按首次出现的顺序等距排列;传入 x_axis=Linear 可以按数值间隔摆放它们。
  • 手写比较。 report.document_from_jsonl 按照 report API 中的成对规则根据记录计算 ComparisonReport;只有对不是来自记录的结果,才需要自己构建它。
  • 空的系列名称。 它们被绘制为 value。

后续步骤