runner のチュートリアル

このチュートリアルでは、現実性を高めたベンチマークケースを構築します。単一比較、JSONL イベントストリーム、明示的なライフサイクルを持つ可変入力、状態付き操作系列、計測前に捕捉して最小化する誤実装、スケールごとの複数ランダムデータセット、独自プロトコルです。例はすべて完全な async test で、表示する出力はマシン速度に依存しない部分です。

やりたいこと用途
同じ入力で実装を比較する@runner.Case::single_step(...).with_immutable_input(...).compare(...)
まず参照実装と照合して検証する.against_equal(...)、または BenchSpec::advanced による完全なオラクル
生の記録を残すRunContext の中の @event.JsonlSink
可変な入力にライフサイクルを与えるprepare と reset を持つ @fixture.Fixture
状態を持つ操作列を計測するContext を使う Implementation::in_process
どれだけ丁寧に計測するかを選ぶ@runner.ProtocolPreset または @runner.validate_protocol
スケールごとに複数のデータセットを計測するExperimentDesign::HierarchicalDatasetsAndRepeats または MultipleDatasetsSingleMeasurement
検証対象を選ぶプロトコルの ValidationCoverage
クラッシュするかもしれないペイロードを隔離するImplementation::worker(native)

クイックスタート

moon add Luna-Flow/mare_mark@0.4.2
import {
  "Luna-Flow/mare_mark/model",
  "Luna-Flow/mare_mark/env_detect",
  "Luna-Flow/mare_mark/event",
  "Luna-Flow/mare_mark/runner",
  "moonbitlang/async",
  "Luna-Flow/mare_mark/fixture",
  "Luna-Flow/mare_mark/experiment",
  "Luna-Flow/mare_mark/generator",
}

実行には環境スナップショットが必要です。env_detect にマシンを記述させ、コードのビルド方法とベンチマークが一度に実行する操作数のように、分からないものを指定します。

async fn this_machine() -> @model.EnvironmentSnapshot {
  @env_detect.detect(compiler_flags="release", dtype_abi="i64", concurrency=1).snapshot
}

スナップショットには新しい provenance の実行 ID も入るため、このチュートリアルの実行ごとに独自の RunSummary.run_id があります。マシンを手書きで記述するには、代わりに @model のコンストラクターでスナップショットを構築します(model のチュートリアルを参照)。

次に、0, 1, …, n-1 を合計する 2 つの方法を比較します:

async test "quick start" {
  let looped = @runner.Implementation::stateless("loop", "1", (n : Int) => {
    let mut total = 0
    for i in 0..<n {
      total += i
    }
    @model.OperationResult::completed(total, ())
  })
  let closed = @runner.Implementation::stateless("formula", "1", (n : Int) => {
    @model.OperationResult::completed(n * (n - 1) / 2, ())
  })
  let plan = @runner.single_step("triangle", [100, 10000])
    .with_immutable_input(context => context.dataset_key.scale, n => n.to_string())
    .compare([looped, closed])
    .against_equal(n => n * (n - 1) / 2, (expected, actual) => expected == actual)
    .compile()
    .unwrap()
  let memory = @event.InMemorySink::new()
  let context = @runner.RunContext::new(
    this_machine(), memory.as_sink(), 42UL, @runner.ProtocolPreset::QuickCheck.validated(),
  )
  let summary = @runner.run(plan, context)
  inspect(summary.passed_count, content="4")
  inspect(memory.observations.length(), content="16")
  inspect(memory.calibrations.length(), content="4")
}

両方の実装が両方のスケールで検証に合格しました(4 つの検証)。その後、各スケールで実装ごとに 1 回のキャリブレーションと、それぞれ 2 つのバッチからなる 4 つのブロック(QuickCheck: 探索的 1 つ、確認的 3 つ)が実行されました。memory.observations[i].raw_elapsed_us に反復あたりの時間が入っています。

日常的な作業

実行を JSONL として保存する

JSONL ストリームは実行の監査記録です。イベントを JsonlSink に送り、同時に tee でメモリにも送ります:

async test "write JSONL and keep events in memory" {
  let id = @runner.Implementation::stateless("identity", "1", (x : Int) => {
    @model.OperationResult::completed(x, ())
  })
  let plan = @runner.single_step("identity", [1])
    .with_immutable_input(context => context.dataset_key.scale, x => x.to_string())
    .compare([id])
    .against_equal(x => x, (expected, actual) => expected == actual)
    .compile()
    .unwrap()
  let jsonl = @event.JsonlSink::new()
  let memory = @event.InMemorySink::new()
  let sink = @event.tee(memory.as_sink(), jsonl.as_sink())
  let summary = @runner.run(
    plan,
    @runner.RunContext::new(this_machine(), sink, 7UL, @runner.ProtocolPreset::QuickCheck.validated()),
  )
  let location = summary.artifact_location.unwrap()
  inspect(location.has_prefix("jsonl://memory/identity|mmkp_2:a8272a0d9e84b872|7|"), content="true")
  let lines = jsonl.to_jsonl().split("\n").to_array()
  inspect(lines.length(), content="7")
  inspect(lines[0].contains("\"type\":\"validation\""), content="true")
  inspect(lines[6].contains("\"type\":\"summary\""), content="true")
}

1 回の検証、1 回のキャリブレーション、4 つの観測、サマリーで 7 行になります。ロケーションは実行 ID で終わります。ケース、プロトコル ID、シード、検出環境の provenance の実行 ID とタイムスタンプです。jsonl.to_jsonl() を自分の IO でファイルへ書くか、@event.streaming_jsonl で発生時に行をストリームします。

可変の入力に明示的なライフサイクルを与える

その場でのソートは入力を破壊します。フィクスチャは生成した配列を各バッチの前にコピーし(PerBatch)、そのコピーを計時区間の外に置きます(ExcludedFromMeasurement):

async test "an in-place sort on a fresh copy per batch" {
  let fixture : @fixture.Fixture[Int, Array[Int], Array[Int]] = @fixture.Fixture::new(
    "descending",
    "1",
    context => Array::makei(context.dataset_key.scale, i => context.dataset_key.scale - i),
    input => "len=" + input.length().to_string(),
    input => input.copy(),
    (input, _, _) => input,
    (_, _) => (),
    @model.SetupPolicy::new(
      @model.SetupFrequency::PerBatch,
      @model.SetupTiming::ExcludedFromMeasurement,
      @model.WorkspaceScope::BatchWorkspace,
    ),
  )
  let sort_in_place = @runner.Implementation::stateless("sort", "1", (xs : Array[Int]) => {
    xs.sort()
    @model.OperationResult::completed(xs[0], ())
  })
  let oracle = @experiment.ReferenceOracle::equal(
    "minimum",
    (input : Array[Int]) => input.fold(init=input[0], (low, x) => if x < low { x } else { low }),
    (expected, actual) => expected == actual,
  )
  let spec = @runner.BenchSpec::advanced(
    "sort",
    fixture,
    [sort_in_place],
    @runner.OutputSink::keep_last(),
    @experiment.OracleSpec::Reference(oracle),
    [64, 4096],
    n => n.to_string(),
    1,
    (input, _) => @model.CaseDescriptor::new("sort", [input.length().to_string()], "", ""),
    input => "len=" + input.length().to_string(),
    first => first.to_string(),
    _ => "",
    (input, implementation) => @model.ReplaySpec::new("sort-worker", [implementation, input.length().to_string()]),
  )
  let memory = @event.InMemorySink::new()
  let summary = @runner.run(
    spec.compile().unwrap(),
    @runner.RunContext::new(this_machine(), memory.as_sink(), 1UL, @runner.ProtocolPreset::QuickCheck.validated()),
  )
  inspect(summary.passed_count, content="2")
  inspect(memory.observations.all(o => o.valid), content="true")
}

clone_input がなければ、2 番目のバッチはすでにソート済みの配列をソートし、異なるワークロードを計測してしまいます。コピーを計測に含めるには IncludedInMeasurement を、個々の操作の前に毎回コピーするには PerIteration を使ってください。runner の設計に、各組み合わせで何が時計の内側に入るかを正確に示しています。

状態を持つ系列を検証する

累計のような状態を持つ操作は、系列として検証されます。実装と参照オラクルはそれぞれ自分のコンテキストを引き継ぎ、sequence_length が比較するステップ数を決めます:

async test "a running total validated over five steps" {
  let fixture : @fixture.Fixture[Int, Int, Int] = @fixture.Fixture::immutable(
    "step",
    "1",
    context => context.dataset_key.scale,
    x => x.to_string(),
  )
  let running = @runner.Implementation::in_process("running", "1", () => 0, (step : Int, total : Int) => {
    @model.OperationResult::completed(total + step, total + step)
  })
  let reference = @experiment.ReferenceOracle::new(
    "running-total",
    () => 0,
    _ => 5,
    (step : Int, index, total : Int) => {
      ignore(index)
      @model.OperationResult::completed(total + step, total + step)
    },
    (_, _, expected, actual) => {
      match (expected, actual) {
        (Value(e), Value(a)) if e == a => @model.ValidationStatus::Valid
        _ => @model.ValidationStatus::Invalid("total differs")
      }
    },
    total => total.to_string(),
  )
  let spec = @runner.BenchSpec::advanced(
    "running-total",
    fixture,
    [running],
    @runner.OutputSink::new(() => 0, (sum, x) => sum + x, sum => sum),
    @experiment.OracleSpec::Reference(reference),
    [3],
    n => n.to_string(),
    5,
    (step, index) => @model.CaseDescriptor::new("add", [step.to_string(), index.to_string()], "total", ""),
    x => x.to_string(),
    x => x.to_string(),
    total => total.to_string(),
    (step, implementation) => @model.ReplaySpec::new("total-worker", [implementation, step.to_string()]),
  )
  let memory = @event.InMemorySink::new()
  let summary = @runner.run(
    spec.compile().unwrap(),
    @runner.RunContext::new(this_machine(), memory.as_sink(), 3UL, @runner.ProtocolPreset::QuickCheck.validated()),
  )
  inspect(summary.validation_count, content="5")
  inspect(summary.passed_count, content="5")
  guard memory.validations[4].evidence is Some(evidence) else { fail("no evidence") }
  inspect(evidence.actual, content="15")
  inspect(evidence.context, content="15")
}

系列のステップ 4 は 3⋅5=153 \cdot 5 = 15 を返し、証拠にはステップ後の値とコンテキストの両方が記録されます。

参照オラクルの sequence_length(input) は、正の値を返し、上記のケース長と等しくなければなりません。ランナーはその値を両方の系列に使用します。長さが一致しない場合は RunConfigError が発生し、実装またはオラクルが合格したステップの後で途中終了した場合は、長さ不一致を示す無効な結果が明示的に生成されます。これにより、共通の接頭部分を完全な検査と誤認しません。

誤った実装を捕捉して最小化する

off-by-one の誤りを含む実装は検証に失敗します。シュリンカーがあれば、ランナーは失敗アーティファクトを書き出す前に失敗する入力を縮小します:

async test "a failing implementation is minimized" {
  let off_by_one = @runner.Implementation::stateless("off-by-one", "0.1", (x : Int) => {
    @model.OperationResult::completed(x + 1, ())
  })
  let spec = @runner.BenchSpec::advanced(
    "identity",
    @fixture.Fixture::immutable("ints", "1", context => context.dataset_key.scale, (x : Int) => x.to_string()),
    [off_by_one],
    @runner.OutputSink::keep_last(),
    @experiment.OracleSpec::Reference(
      @experiment.ReferenceOracle::equal("identity", x => x, (expected, actual) => expected == actual),
    ),
    [40],
    n => n.to_string(),
    1,
    (x, _) => @model.CaseDescriptor::new("identity", [x.to_string()], "", ""),
    x => x.to_string(),
    x => x.to_string(),
    _ => "",
    (x, implementation) => @model.ReplaySpec::new("identity-worker", [implementation, x.to_string()]),
    shrinker=@experiment.Shrinker::new(x => if x > 0 { [x / 2] } else { [] }, x => x.to_string()),
  )
  let memory = @event.InMemorySink::new()
  let summary = @runner.run(
    spec.compile().unwrap(),
    @runner.RunContext::new(this_machine(), memory.as_sink(), 9UL, @runner.ProtocolPreset::QuickCheck.validated()),
  )
  inspect(summary.failed_count, content="1")
  let failure = memory.failures[0]
  inspect(failure.minimal_input, content="0")
  debug_inspect(failure.shrink_path, content="[\"20\", \"10\", \"5\", \"2\", \"1\", \"0\"]")
}

失敗イベントには、シード、元のフィンガープリントと最小のフィンガープリント、縮小パス、リプレイコマンド identity-worker off-by-one 40 が含まれます。実装はそれでも計時されますが、レポートはその系列を隠し、代わりに不一致を表示します。

スケールごとに複数のランダムデータセットを計測する

スケールごとに固定データセット 1 つなら 1 つの入力を非常に精密に計測できますが、結果はその入力に依存する可能性があります。ランダム入力ではスケールごとに複数のデータセットを計測します。HierarchicalDatasetsAndRepeats は repeats_per_dataset 個の連続ブロックごとに新しいデータセットを与えるため、実行は複数の入力を見ながら各入力を繰り返します。ジェネレーターはデータセットを使わなければなりません。すべてのデータセットで GenerationContext.seed は実行シードであり、dataset_key.dataset_id がそれらを区別します。

fn random_values(seed : UInt64, length : Int) -> Array[Int] {
  let mut state = seed
  Array::makei(length, _ => {
    state = state * 6364136223846793005UL + 1442695040888963407UL
    (state >> 33).to_int() % 1000
  })
}

async test "four datasets per scale, two blocks each" {
  let fixture : @fixture.Fixture[Int, Array[Int], Array[Int]] = @fixture.Fixture::immutable(
    "random-values",
    "1",
    context => {
      let seed = @generator.derive_seed(context.seed, context.case_id, context.dataset_key.dataset_id)
      random_values(seed, context.dataset_key.scale)
    },
    xs => xs.length().to_string() + ":" + xs.fold(init=0, (hash, x) => hash * 31 + x).to_string(),
  )
  let looped = @runner.Implementation::stateless("loop", "1", (xs : Array[Int]) => {
    let mut total = 0
    for x in xs {
      total += x
    }
    @model.OperationResult::completed(total, ())
  })
  let folded = @runner.Implementation::stateless("fold", "1", (xs : Array[Int]) => {
    @model.OperationResult::completed(xs.fold(init=0, (total, x) => total + x), ())
  })
  let spec = @runner.BenchSpec::advanced(
    "sum",
    fixture,
    [looped, folded],
    @runner.OutputSink::keep_last(),
    @experiment.OracleSpec::Reference(
      @experiment.ReferenceOracle::equal("sum", xs => xs.fold(init=0, (total, x) => total + x), (expected, actual) => expected == actual),
    ),
    [100, 1000],
    n => n.to_string(),
    1,
    (xs, _) => @model.CaseDescriptor::new("sum", [xs.length().to_string()], "", ""),
    xs => xs.length().to_string(),
    total => total.to_string(),
    _ => "",
    (xs, implementation) => @model.ReplaySpec::new("sum-worker", [implementation, xs.length().to_string()]),
  )
  let protocol = @runner.validate_protocol(
    @model.RunProtocol::new(
      @model.ExperimentDesign::HierarchicalDatasetsAndRepeats,
      1,
      None,
      @model.CalibrationProtocol::new(1000.0, 1, 1000, 100000.0, @model.BatchPolicy::PerImplementation),
      1.0,
      @model.OrderPolicy::BalancedBlocks(1UL),
      @model.OutlierPolicy::ReportOnly,
      @model.ValidationCoverage::ConfirmatoryOnly,
      2,
      6,
      repeats_per_dataset=2,
    ),
  ).unwrap()
  let memory = @event.InMemorySink::new()
  let summary = @runner.run(
    spec.compile(protocol~).unwrap(),
    @runner.RunContext::new(this_machine(), memory.as_sink(), 7UL, protocol),
  )
  inspect(summary.observation_count, content="32")
  inspect(summary.validation_count, content="12")
  let datasets = memory.observations
    .filter(o => o.implementation_id == "loop" && o.scale_text == "1000")
    .map(o => (o.block_id, o.dataset_id))
  debug_inspect(datasets, content="[(0, 4), (1, 4), (2, 5), (3, 5), (4, 6), (5, 6), (6, 7), (7, 7)]")
  inspect(memory.validations.all(v => v.measurement is None), content="true")
}

各スケールには 2+6=82 + 6 = 8 ブロックと 8/2=48 / 2 = 4 データセットがあるため、スケール 2 個、実装 2 個なら観測は 32 個です。スケールのブロック bb はインデックス ⌊b/2⌋\lfloor b/2 \rfloor のデータセットを計測します。ID はスケールをまたいで続きます(スケール 1000 にはデータセット 4 から 7 があります)。各スケールの最初のデータセットは 2 つの探索的ブロックだけで計測され、そこでウォームアップとキャリブレーションが行われます。ConfirmatoryOnly は確認的ブロックが計測する 3 データセットを検証します。データセット 3 個、スケール 2 個、実装 2 個、1 ステップなので検証は 12 個です。validate_protocol は 2 が両方のサンプル数を割り切ることを要求するため、データセットは両フェーズにまたがりません。

他のプロトコルの選択肢も同じように動きます。

対象設定
ブロックごとに 1 データセットを計測するMultipleDatasetsSingleMeasurement(repeats_per_dataset は 1)
探索的データセットも含め、すべてのデータセットを検証する。ValidationCoverage::EveryDataset
計測するすべてのバッチの直前にも検証する。ValidationCoverage::EveryMeasurement(検証にバッチの MeasurementKey が付きます)。

カバレッジにかかわらず、検証が計時区間内で実行されることはありません。カバレッジを増やすと実時間のコストは増えますが、正確さは変わりません。

独自のプロトコルを書く

プリセットはよくあるケースをカバーします。CI のゲートでは、より多くの確認的ブロックが欲しいかもしれません。すべてのローテーション周期が完全になるよう、実装の数の倍数にします:

test "a custom protocol" {
  let protocol = @model.RunProtocol::new(
    @model.ExperimentDesign::FixedDatasetRepeatedMeasurements,
    5,
    Some(20000.0),
    @model.CalibrationProtocol::new(2000.0, 4, 100000, 200000.0, @model.BatchPolicy::PerImplementation),
    2.0,
    @model.OrderPolicy::BalancedBlocks(2026UL),
    @model.OutlierPolicy::ReportOnly,
    @model.ValidationCoverage::EveryDataset,
    2,
    30,
  )
  let validated = @runner.validate_protocol(protocol).unwrap()
  let preset = @runner.ProtocolPreset::Custom(validated)
  inspect(preset.validated().protocol.confirmatory_samples, content="30")
  inspect(@model.protocol_identity(protocol), content="mmkp_2:099f165751bb177e")
}

実装 2 個では、2+30=322 + 30 = 32 ブロックが 16 個の完全なサイクルになります。ID はすべてのフィールドのダイジェストなので、順序シードを含め、どれかを変えると別の ID になります。

さらに先へ

安全でないコードを隔離する。 Implementation::worker は各操作をタイムアウト付きの子プロセスで実行します。クラッシュは Aborted、ハングは Timeout という結果になり、どちらもインフラストラクチャの失敗として数えられます。ワーカーには native ターゲットが必要です。WorkerSpec を参照してください。

非同期デバイス。 キューに入った処理が終わるまでブロックする関数を、synchronize= として Implementation::in_process に渡してください。ランナーは時計を開始する直前と停止する直前にそれを呼び出します。

バッチあたりの作業量を等しくする。 BatchPolicy::SharedBatchSize は、すべての実装にキャリブレーションされた最小のバッチサイズを与えます。

複数の実装同士の比較。 単一の参照がない場合は、OracleSpec::Relational または ReferenceAndRelational を使って実装をペアごとに比較してください。experiment のチュートリアルを参照してください。

再現可能な入力。 フィクスチャ内で @generator.derive_seed(context.seed, context.case_id, context.dataset_key.dataset_id) によってデータセットのシードを導出してください。generator のチュートリアルを参照してください。

よくある落とし穴

  • ペイロード内にあるペイロードでない処理。 実装の関数内での割り当て、解析、出力は計時されます。フィクスチャに移してください。
  • 変更を加えるコードで clone_input を忘れる。 後のバッチが異なる入力を計測することになります。
  • 無効な観測から計時値を読む。 比較の前に valid でフィルタリングし、検証に失敗したデータセットを除外してください。
  • フェーズを混在させる。 探索的ブロックは方向付けのためのものです。判断は確認的ブロックで行ってください。
  • provenance の実行 ID を再利用すること。 RunSummary.run_id はケース、プロトコル ID、シード、provenance の実行 ID とタイムスタンプから構築されます。実行ごとに同じ手書きスナップショットを使うと、ケースのすべての実行が同じ ID になります。@env_detect.detect は毎回新しい provenance の実行 ID を作ります。
  • すべてのデータセットで同じ入力。 MultipleDatasetsSingleMeasurement と HierarchicalDatasetsAndRepeats では、フィクスチャは同じ実行シードでデータセットごとに 1 回呼び出されます。dataset_key.dataset_id から入力を導出しないと、すべてのデータセットが同一になります。
  • 複数データセットでの PerRun セットアップ。 実行全体の準備値は 1 つのデータセットからしか作れないため、compile と run は拒否します。PerDataset を使ってください。
  • 非同期コンテキストの外で run を呼ぶ。 これは async fn です。
  • ノイズのあるデータでゼロの閾値を使う。 validate_protocol は 0.0 を受け入れ、そのとき stats.compare_paired はゼロでない差をすべて Faster か Slower とします。気にする最小の変化を選んでください。

次のステップ