ir_model API

目的

Luna-Flow/mare_mark/ir_model は Plot IR を定義します。report がイベントから構築し、JSON、SVG、HTML に描画する可搬ドキュメントです。分類、線形、対数の x 軸上の点、mismatch・capability・counterexample・corpus count の差分報告、基線に対する対応のある比較を含みます。ir_model の設計を参照してください。

ソース: src/ir_model/ir_model.mbt。

インポート

このパッケージを、使う側のパッケージの moon.pkg に追加します。

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

このページの例は、既定の別名 @ir_model でこのパッケージを呼び出します。

ドキュメント

PlotDocument

PlotDocument は 1 つのレポートです。

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() です。y 軸の scale を識別できないため V1 は非推奨です。

プロット

Plot

Plot は 1 つのグラフです。

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 は description に note を表示します。

AxisScale

AxisScale は plot の x 値の配置を指定します。

pub(all) enum AxisScale {
  Categorical
  Linear
  Log
}
値配置
Categorical初出順に等間隔で配置し、目盛りは x の文字列を表示する
Linear数値順に並べ、その値に比例した間隔で配置する。各 x は有限数値として解析できる必要がある
Log正の有限値の log10 で配置する

Linear plot の x を有限数値として解析できなければ Categorical に戻します。report.plot_svg はこの規則に従い、heatmap の x は常に分類軸にします。

AxisScale は Categorical、Linear、Log のいずれかです。x 軸の既定値は Categorical、y 軸は Linear です。対数軸は正の値を log10(value) で配置するため、正の有限値に限って選択してください。

PlotPoint

PlotPoint は名前付き系列の x 位置にある 1 個の値です。

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 は 1 つの失敗した検証です。

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 は 1 つの実装について、ある種類の結果の件数を数えます。

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候補・尺度・ケースごとに 1 行
practical_delta_pct決定のしきい値(%)
outlier_policy対応差に適用した filter の OutlierPolicy::text()
confidence_pct, resamplesbootstrap 区間の信頼水準と再標本化回数
min_blocks決定に必要な最小の有効 block 数
run_seed各行の seed の派生元
protocol_recorded, seed_recordedしきい値、policy、run seed が既定値ではなく記録に由来するか
note設定と取得元を説明する文

new はこの順に欄を受け取ります。empty() は行なし、数値 0、flag false、空文字列で、renderer は比較欄を表示しません。report.document_from_jsonl が report を作ります。規則は report APIを参照してください。

ComparisonRow

ComparisonRow は 1 ケースの 1 尺度で、候補を基線と比較した結果です。

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 の理由、それ以外は空です。有効 block がなければ estimate は None、bootstrap をしなければ interval は None です。blocks_used は比較に使った完全 block、blocks_incomplete は各実装の観測がちょうど 1 個ではない block、blocks_outliers は外れ値規則で除いた完全 block の数です。seed は行の bootstrap 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

使用 block の基線時間を 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 は 100md/med⁡(b)100m_d/\operatorname{med}(b)、speedup は med⁡(b)/(med⁡(b)+md)\operatorname{med}(b)/(\operatorname{med}(b)+m_d) です。決定は中央値同士の差ではなく relative_delta_pct を使います。

DeltaInterval

DeltaInterval は対応差の中央値の bootstrap 区間です。

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")
}