ir_model API

用途

Luna-Flow/mare_mark/ir_model 定义 Plot IR,即 report 从事件构建并渲染为 JSON、SVG 或 HTML 的可移植文档:分类、线性或对数 x 轴上的点图,包含不匹配、能力、反例和语料计数的差分报告,以及实现相对于基线的成对比较。参见 ir_model 设计。

源码:src/ir_model/ir_model.mbt。

导入

把该包加入使用它的那个包的 moon.pkg:

import {
  "Luna-Flow/mare_mark/ir_model",
}

本页的示例通过它的默认别名 @ir_model 调用它。

文档

PlotDocument

PlotDocument 是一份报告。

pub struct PlotDocument {
  schema_version : @model.SchemaVersion
  run_id : String
  target : String
  plots : Array[Plot]
  differential : DifferentialReport
  comparisons : ComparisonReport
}
pub fn PlotDocument::new(String, String, Array[Plot], differential? : DifferentialReport, comparisons? : ComparisonReport) -> Self

new(run_id, target, plots) 将 schema_version 设为 SchemaVersion::V2(mmks_2);differential 默认为 DifferentialReport::empty(),comparisons 默认为 ComparisonReport::empty()。V1 无法标识 y 轴刻度,已弃用。

图表

Plot

Plot 是一张图表。

pub struct Plot {
  kind : PlotKind
  title : String
  unit : String
  interval_kind : String
  points : Array[PlotPoint]
  x_axis : AxisScale
  y_axis : AxisScale
  x_label : String
  note : String
}
pub fn Plot::new(PlotKind, String, String, String, Array[PlotPoint], x_axis? : AxisScale, y_axis? : AxisScale, x_label? : String, note? : String) -> Self

位置参数依次为 kind、title、unit、interval_kind 和 points。unit 标注 y 轴;interval_kind 说明这些值是什么("raw"、"median"、某个置信水平)。x_axis(默认 Categorical)决定 x 值的摆放方式,x_label(默认 "")是显示在图下方的 x 轴名称,note(默认 "")是解释该图的一句话;HTML 把它显示在图注中,SVG 把它放在描述中。

AxisScale

AxisScale 说明图中 x 值如何摆放。

pub(all) enum AxisScale {
  Categorical
  Linear
  Log
}
值摆放方式
Categorical按首次出现的顺序等距排列;刻度显示 x 文本
Linear按数值排序,间距与数值成比例;每个 x 都必须能解析为有限数
Log按正有限值的 log10 定位

当 Linear 图中某个 x 值不是有限数时,渲染器回退到 Categorical;report.plot_svg 就是这样做的,并且它总是按分类方式绘制热力图。

AxisScale 是 Categorical、Linear 或 Log;x 轴默认为 Categorical,y 轴默认为 Linear。对数轴按正值的 log10(value) 定位,因此调用方只应对正有限值选择该刻度。

PlotPoint

PlotPoint 是某个命名系列中,位于某个 x 位置上的一个值。

pub struct PlotPoint {
  x : String
  y : Double
  series : String
}
pub fn PlotPoint::new(String, Double, String) -> Self

即使在线性轴上,x 也是文本(例如 "1024")。空的 series 绘制为 "value"。

PlotKind

PlotKind 选择图表的绘制与标注方式。

pub(all) enum PlotKind {
  Scaling
  RawDistribution
  BlockOrder
  Interval
  Outlier
  ChangePoint
  Heatmap
  Pareto
}

Scaling、Interval 和 ChangePoint 绘制为点,并用线连接各类别的均值;Heatmap 绘制为单元格;其他种类绘制为点。

test "a plot document" {
  let plot = @ir_model.Plot::new(
    @ir_model.PlotKind::Interval,
    "Median delta",
    "µs",
    "95% bootstrap",
    [@ir_model.PlotPoint::new("1024", -9.5, "candidate")],
  )
  let document = @ir_model.PlotDocument::new("run-7", "native", [plot])
  inspect(document.schema_version.identifier(), content="mmks_2")
  inspect(document.differential.corpus.total, content="0")
  inspect(document.plots[0].x_axis is Categorical, content="true")
  inspect(document.comparisons.rows.length(), content="0")
}

test "a plot on a linear axis" {
  let points = [
    @ir_model.PlotPoint::new("1024", 10.1, "scalar"),
    @ir_model.PlotPoint::new("4096", 40.6, "scalar"),
  ]
  let plot = @ir_model.Plot::new(
    @ir_model.PlotKind::Scaling,
    "vector-add",
    "µs/op",
    "median",
    points,
    x_axis=Linear,
    x_label="scale",
    note="Median of the confirmatory observations.",
  )
  inspect(plot.x_label, content="scale")
}

差分报告

DifferentialReport

DifferentialReport 汇集一次运行的正确性证据。

pub struct DifferentialReport {
  mismatches : Array[MismatchRow]
  capabilities : Array[CapabilityCell]
  counterexamples : Array[Counterexample]
  corpus : CorpusSummary
}
pub fn DifferentialReport::new(Array[MismatchRow], Array[CapabilityCell], Array[Counterexample], CorpusSummary) -> Self
pub fn DifferentialReport::empty() -> Self

empty() 没有任何行,所有语料计数为 0。

MismatchRow

MismatchRow 是一次失败的验证。

pub struct MismatchRow {
  implementation : String
  operation : String
  operands : Array[String]
  expected : String
  actual : String
  flags_diff : String
  trap : String
  fingerprint : String
  replay_command : String
}
pub fn MismatchRow::new(String, String, Array[String], String, String, String, String, String, String) -> Self

report 将 flags_diff 填为期望标志、" → " 和实际标志,各组标志以 ", " 连接。

CapabilityCell

CapabilityCell 统计某个实现某一类结果的数量。

pub struct CapabilityCell {
  implementation : String
  outcome_kind : String
  count : Int
}
pub fn CapabilityCell::new(String, String, Int) -> Self

outcome_kind 是一个 ExecutionOutcome::kind 字符串。

Counterexample

Counterexample 是一个最小化后的失败输入。

pub struct Counterexample {
  implementation : String
  minimal_input : String
  fingerprint : String
  shrink_path : Array[String]
  replay_command : String
}
pub fn Counterexample::new(String, String, String, Array[String], String) -> Self

CorpusSummary

CorpusSummary 统计验证结果。

pub struct CorpusSummary {
  total : Int
  passed : Int
  failed : Int
  unsupported : Int
  expected_difference : Int
}
pub fn CorpusSummary::new(Int, Int, Int, Int, Int) -> Self
test "a differential report" {
  let row = @ir_model.MismatchRow::new("fast", "add", ["0.1", "0.2"], "0.3", "0.30000000000000004", " → inexact", "", "sha256:…", "worker fast 3")
  let report = @ir_model.DifferentialReport::new([row], [], [], @ir_model.CorpusSummary::new(10, 9, 1, 0, 0))
  let document = @ir_model.PlotDocument::new("run-8", "js", [], differential=report)
  inspect(document.differential.mismatches[0].operation, content="add")
}

比较

ComparisonReport

ComparisonReport 保存文档中的成对比较以及做这些比较所用的设置。

pub struct ComparisonReport {
  rows : Array[ComparisonRow]
  practical_delta_pct : Double
  outlier_policy : String
  confidence_pct : Double
  resamples : Int
  min_blocks : Int
  run_seed : UInt64
  protocol_recorded : Bool
  seed_recorded : Bool
  note : String
}
pub fn ComparisonReport::new(Array[ComparisonRow], Double, String, Double, Int, Int, UInt64, Bool, Bool, String) -> Self
pub fn ComparisonReport::empty() -> Self
字段含义
rows每个候选、规模和用例一行
practical_delta_pct决策阈值,以百分比表示
outlier_policy应用于成对差值的过滤器的 OutlierPolicy::text()
confidence_pct, resamples自助法区间的置信水平和重抽样次数
min_blocks对一行做出决策所需的最少可用区组数
run_seed派生各行种子所用的种子
protocol_recorded, seed_recorded阈值和策略以及运行种子是否来自记录而非默认值
note一句说明这些设置及其来源的人类可读语句

new 按此顺序接收这些字段。empty() 没有行,数值为零,标志为 false,字符串为空;渲染器不会为它显示比较部分。report.document_from_jsonl 负责填充报告;规则见 report API。

ComparisonRow

ComparisonRow 是某个用例在某个规模下,一个候选与基线的比较。

pub struct ComparisonRow {
  case_id : String
  scale : String
  baseline : String
  candidate : String
  decision : ComparisonDecision
  reason : String
  estimate : PairedEstimate?
  interval : DeltaInterval?
  blocks_used : Int
  blocks_incomplete : Int
  blocks_outliers : Int
  seed : UInt64
}
pub fn ComparisonRow::new(String, String, String, String, ComparisonDecision, String, PairedEstimate?, DeltaInterval?, Int, Int, Int, UInt64) -> Self

reason 解释 Unknown 或 Invalid 决策,其他情况下为空。没有可用区组时 estimate 为 None;自助法没有运行时 interval 为 None。blocks_used 统计参与比较的完整区组数,blocks_incomplete 统计并非每个实现恰好有一个观测的区组数,blocks_outliers 统计被离群值策略移除的完整区组数。seed 是该行的自助法种子。new 按此顺序接收这些字段。

PairedEstimate

PairedEstimate 保存一次比较的点估计,单位为 µs/op。

pub struct PairedEstimate {
  baseline_median_us : Double
  candidate_median_us : Double
  median_delta_us : Double
  relative_delta_pct : Double
  speedup : Double
}
pub fn PairedEstimate::new(Double, Double, Double, Double, Double) -> Self

在所用区组上,记基线时间为 bib_i,候选时间为 cic_i,成对差值为 di=ci−bid_i = c_i - b_i:baseline_median_us 为 med⁡(b)\operatorname{med}(b),candidate_median_us 为 med⁡(c)\operatorname{med}(c),median_delta_us 为 md=med⁡(d)m_d = \operatorname{med}(d),relative_delta_pct 为 100 md/med⁡(b)100\,m_d / \operatorname{med}(b),speedup 为 med⁡(b)/(med⁡(b)+md)\operatorname{med}(b) / (\operatorname{med}(b) + m_d)。决策使用 relative_delta_pct,而不是两个中位数之差。

DeltaInterval

DeltaInterval 是成对差值中位数的自助法区间。

pub struct DeltaInterval {
  low_us : Double
  high_us : Double
  low_pct : Double
  high_pct : Double
}
pub fn DeltaInterval::new(Double, Double, Double, Double) -> Self

low_us 和 high_us 的单位为 µs/op;low_pct 和 high_pct 是同一对边界相对于基线中位数的百分比,因此可以与 relative_delta_pct 和阈值对照阅读。

ComparisonDecision

ComparisonDecision 是一次比较的结果。

pub(all) enum ComparisonDecision {
  Faster
  Slower
  Equivalent
  Unknown
  Invalid
}
pub fn ComparisonDecision::text(Self) -> String

这些值与 @stats.Decision 一一对应。text 给出 report.plot_json 写出的小写名称("faster"、"slower"、"equivalent"、"unknown"、"invalid")。

test "a comparison row" {
  let estimate = @ir_model.PairedEstimate::new(10.0, 7.0, -3.0, -30.0, 10.0 / 7.0)
  let interval = @ir_model.DeltaInterval::new(-3.1, -2.9, -31.0, -29.0)
  let row = @ir_model.ComparisonRow::new(
    "vector-add", "1024", "scalar", "simd", Faster, "", Some(estimate), Some(interval), 8, 0, 0, 7UL,
  )
  let report = @ir_model.ComparisonReport::new([row], 2.0, "tukey_fence", 95.0, 10000, 3, 42UL, true, true, "hand-made")
  let document = @ir_model.PlotDocument::new("run-9", "native", [], comparisons=report)
  inspect(document.comparisons.rows[0].decision.text(), content="faster")
  inspect(@ir_model.ComparisonReport::empty().rows.length(), content="0")
}