MoonBit tree speculative decoding algorithms and reproducible offline experiments
pub struct AdaptiveBenchmark {
model_id : String
prompt_tokens : Int
output_tokens : Int
baseline_target_requests : Int
adaptive_target_requests : Int
adaptive_draft_requests : Int
adaptive_rounds : Int
adaptive_candidate_nodes : Int
adaptive_accepted_nodes : Int
planned_depths : Array[Int]
}pub struct AdaptivePolicy {
depth : Int
min_depth : Int
max_depth : Int
target_acceptance : Double
}fn AdaptivePolicy::new(min_depth : Int, max_depth : Int, target_acceptance : Double) -> AdaptivePolicypub struct AdaptiveTreePolicy {
min_width : Int
max_width : Int
min_depth : Int
max_depth : Int
max_nodes : Int
entropy_threshold : Double
target_acceptance : Double
depth : Int
}fn AdaptiveTreePolicy::build(self : AdaptiveTreePolicy, prefix : Array[Int], depth_logits : Array[Array[Double]]) -> Result[DraftTree, AdaptiveTreeError]fn AdaptiveTreePolicy::build_from_batch_model(self : AdaptiveTreePolicy, prefix : Array[Int], model : (Array[Array[Int]]) -> Result[Array[Array[Double]], String]) -> Result[ModelDraftTree, ModelTreeError]fn AdaptiveTreePolicy::build_from_batch_model_with_depth_limit(self : AdaptiveTreePolicy, prefix : Array[Int], depth_limit : Int, model : (Array[Array[Int]]) -> Result[Array[Array[Double]], String]) -> Result[ModelDraftTree, ModelTreeError]fn AdaptiveTreePolicy::build_from_model(self : AdaptiveTreePolicy, prefix : Array[Int], model : (Array[Int]) -> Result[Array[Double], String]) -> Result[ModelDraftTree, ModelTreeError]fn AdaptiveTreePolicy::new(min_width : Int, max_width : Int, min_depth : Int, max_depth : Int, max_nodes : Int, entropy_threshold : Double, target_acceptance : Double) -> Result[AdaptiveTreePolicy, AdaptiveTreeError]fn AdaptiveTreePolicy::width_for_logits(self : AdaptiveTreePolicy, logits : Array[Double]) -> Result[Int, AdaptiveTreeError]pub enum BenchmarkError {
InvalidModelId
InvalidOutputBudget
BaselineFailure(TreeExperimentError)
AdaptiveFailure(TreeExperimentError)
OutputLengthMismatch
} derive(Eq, Debug)pub struct DecisionTrace {
round : Int
node_id : Int?
token : Int
kind : DecisionKind
target_probability : Double
draft_probability : Double
threshold : Double
}pub struct DecodeBudget {
max_target_batches : Int
max_output_tokens : Int
max_draft_tokens : Int
}fn DecodeBudget::allow_emission(self : DecodeBudget, metrics : DecodeMetrics, emitted_count : Int) -> Result[Unit, BudgetError]fn DecodeBudget::allow_round(self : DecodeBudget, metrics : DecodeMetrics, proposal_depth : Int) -> Result[Unit, BudgetError]fn DecodeBudget::new(max_target_batches : Int, max_output_tokens : Int, max_draft_tokens : Int) -> Result[DecodeBudget, BudgetError]pub struct DecodeMetrics {
target_batches : Int
draft_tokens : Int
emitted_tokens : Int
accepted_tokens : Int
rejected_rounds : Int
}fn DecodeMetrics::record(self : DecodeMetrics, result : VerifyResult, proposal : DraftProposal) -> Unitpub struct DecodeSession {
generated : Array[Int]
metrics : DecodeMetrics
budget : DecodeBudget
cache : KvSlots
stop_reason : StopReason
pending : (Int, Int)?
}fn DecodeSession::apply_round(self : DecodeSession, transaction : Int, result : ReplayRoundResult) -> Result[SessionSnapshot, SessionError]fn DecodeSession::cancel_round(self : DecodeSession, transaction : Int) -> Result[Unit, SessionError]fn DecodeSession::new(prefix : Array[Int], budget : DecodeBudget, cache_capacity : Int) -> DecodeSessionfn DecodeSession::prepare_round(self : DecodeSession, depth : Int) -> Result[(Int, Array[Int]), SessionError]fn DecodeSession::release_cache(self : DecodeSession, transaction : Int) -> Result[Unit, SessionError]pub struct DecodingRun {
generated : Array[Int]
target_queries : Int
draft_queries : Int
logical_target_batches : Int
candidate_nodes : Int
accepted_nodes : Int
}pub struct DeterministicRng {
state : Int
}fn DeterministicRng::uniforms(self : DeterministicRng, count : Int) -> Result[Array[Double], RngError]fn DiagnosticReport::add(self : DiagnosticReport, name : String, passed : Bool, detail : String) -> Unitpub struct ExperimentPair {
case_name : String
baseline : ExperimentRecord
speculative : ExperimentRecord
}fn ExperimentPair::new(case_name : String, baseline : ExperimentRecord, speculative : ExperimentRecord) -> Result[ExperimentPair, ExperimentError]pub struct ExperimentRecord {
name : String
metrics : DecodeMetrics
generated_tokens : Int
prompt : Array[Int]
target_model_id : String
}fn ExperimentRecord::from_result(name : String, target_model_id : String, prompt : Array[Int], result : SimulationResult) -> Result[ExperimentRecord, ExperimentError]pub struct ExperimentSummary {
cases : Int
reduction : SampleSummary
acceptance : SampleSummary
speculative_tokens_per_call : SampleSummary
}pub struct OnlineMoments {
count : Int
mean : Double
squared_deviation_sum : Double
minimum : Double
maximum : Double
}fn ReplayModel::next_logits(self : ReplayModel, context : Array[Int]) -> Result[Array[Double], ReplayError]pub struct SampleSummary {
count : Int
minimum : Double
maximum : Double
mean : Double
population_variance : Double
median : Double
p90 : Double
}pub struct SamplingConfig {
temperature : Double
top_k : Int
top_p : Double
min_p : Double
}fn SamplingConfig::new(temperature? : Double, top_k? : Int, top_p? : Double, min_p? : Double) -> Result[SamplingConfig, SamplingError]pub struct SessionSnapshot {
generated : Array[Int]
metrics : DecodeMetrics
stop_reason : StopReason
}fn TokenMask::apply(self : TokenMask, distribution : Array[Double]) -> Result[Array[Double], ConstraintError]fn TokenMask::except(vocabulary_size : Int, token_ids : Array[Int]) -> Result[TokenMask, ConstraintError]fn TokenMask::only(vocabulary_size : Int, token_ids : Array[Int]) -> Result[TokenMask, ConstraintError]pub struct TreeExperiment {
prompt : Array[Int]
config : TreeExperimentConfig
baseline : DecodingRun
speculative : DecodingRun
}pub struct TreeExperimentConfig {
width : Int
depth : Int
node_budget : Int
output_tokens : Int
seed : Int
}fn TreeExperimentConfig::new(width : Int, depth : Int, node_budget : Int, output_tokens : Int, seed : Int) -> Result[TreeExperimentConfig, TreeExperimentError]pub enum TreeExperimentError {
InvalidConfiguration
ModelFailure(ModelTreeError)
TargetFailure(String)
InvalidProbabilities
VerificationFailure(TreeEvaluateError)
InvalidRandomSeed
} derive(Eq, Debug)pub struct TreePolicy {
width : Int
max_nodes : Int
}fn TreePolicy::build(self : TreePolicy, prefix : Array[Int], depth_logits : Array[Array[Double]]) -> Result[DraftTree, PolicyError]fn TreePolicy::build_from_batch_model(self : TreePolicy, prefix : Array[Int], depth : Int, model : (Array[Array[Int]]) -> Result[Array[Array[Double]], String]) -> Result[ModelDraftTree, ModelTreeError]fn TreePolicy::build_from_model(self : TreePolicy, prefix : Array[Int], depth : Int, model : (Array[Int]) -> Result[Array[Double], String]) -> Result[ModelDraftTree, ModelTreeError]pub struct WorkloadConfig {
rounds : Int
proposal_depth : Int
vocabulary_size : Int
agreement : Double
}fn WorkloadConfig::new(rounds : Int, proposal_depth : Int, vocabulary_size : Int, agreement : Double) -> Result[WorkloadConfig, WorkloadError]fn append_replay_round(generated : Array[Int], metrics : DecodeMetrics, result : ReplayRoundResult) -> Unitfn benchmark_adaptive_batch_decoding(model_id : String, prompt : Array[Int], draft : (Array[Array[Int]]) -> Result[Array[Array[Double]], String], target : (Array[Array[Int]]) -> Result[Array[Array[Double]], String], policy : AdaptiveTreePolicy, output_tokens : Int, seed : Int) -> Result[AdaptiveBenchmark, BenchmarkError]fn compare_tree_decoding(prefix : Array[Int], draft : (Array[Int]) -> Result[Array[Double], String], target : (Array[Int]) -> Result[Array[Double], String], config : TreeExperimentConfig) -> Result[TreeExperiment, TreeExperimentError]fn decode_adaptive_tree_from_batch_models(prefix : Array[Int], draft : (Array[Array[Int]]) -> Result[Array[Array[Double]], String], target : (Array[Array[Int]]) -> Result[Array[Array[Double]], String], policy : AdaptiveTreePolicy, output_tokens : Int, seed : Int) -> Result[AdaptiveDecodingRun, TreeExperimentError]fn decode_autoregressive(prefix : Array[Int], target_logits : Array[Array[Double]], uniforms : Array[Double]) -> Result[SimulationResult, BaselineError]fn decode_baseline_from_model(prefix : Array[Int], target : (Array[Int]) -> Result[Array[Double], String], config : TreeExperimentConfig) -> Result[DecodingRun, TreeExperimentError]fn decode_tree_from_batch_models(prefix : Array[Int], draft : (Array[Array[Int]]) -> Result[Array[Array[Double]], String], target : (Array[Array[Int]]) -> Result[Array[Array[Double]], String], config : TreeExperimentConfig) -> Result[DecodingRun, TreeExperimentError]fn decode_tree_from_model(prefix : Array[Int], draft : (Array[Int]) -> Result[Array[Double], String], target : (Array[Int]) -> Result[Array[Double], String], config : TreeExperimentConfig) -> Result[DecodingRun, TreeExperimentError]fn distribution_for_sampling(logits : Array[Double], config : SamplingConfig) -> Result[Array[Double], SamplingError]fn evaluate_tree(tree : DraftTree, target_distributions : Array[Array[Double]], uniforms : Array[Double], fallback_uniforms : Array[Double]) -> Result[TreeEvaluation, TreeEvaluateError]fn filter_min_p(distribution : Array[Double], threshold : Double) -> Result[Array[Double], SamplingError]fn filter_top_p(distribution : Array[Double], threshold : Double) -> Result[Array[Double], SamplingError]fn generate_workload(config : WorkloadConfig, seed : Int) -> Result[Array[SimulationRound], WorkloadError]fn make_proposal(prefix : Array[Int], tokens : Array[Int], distributions : Array[Array[Double]]) -> Result[DraftProposal, DraftError]fn proposal_from_replay(draft : ReplayModel, prefix : Array[Int], depth : Int, draft_uniforms : Array[Double]) -> Result[DraftProposal, ReplayDecodeError]fn residual_distribution(target : Array[Double], draft : Array[Double]) -> Result[Array[Double], ProbabilityError]fn run_replay_round(draft : ReplayModel, target : ReplayModel, prefix : Array[Int], depth : Int, accept_uniforms : Array[Double], fallback_uniforms : Array[Double], draft_uniforms : Array[Double]) -> Result[ReplayRoundResult, ReplayDecodeError]fn sample_categorical(distribution : Array[Double], unit_interval : Double) -> Result[Int, ProbabilityError]fn sample_constrained_logits(logits : Array[Double], config : SamplingConfig, mask : TokenMask, unit_interval : Double) -> Result[Int, ConstraintError]fn sample_logits(logits : Array[Double], config : SamplingConfig, unit_interval : Double) -> Result[Int, SamplingError]fn score_tree_from_batch_model(tree : DraftTree, model : (Array[Array[Int]]) -> Result[Array[Array[Double]], String]) -> Result[ScoredTree, ModelTreeError]fn score_tree_from_model(tree : DraftTree, model : (Array[Int]) -> Result[Array[Double], String]) -> Result[ScoredTree, ModelTreeError]fn score_tree_layers_from_batch_model(tree : DraftTree, model : (Array[Array[Int]]) -> Result[Array[Array[Double]], String]) -> Result[ScoredTree, ModelTreeError]fn simulate(prefix : Array[Int], schedule : Array[SimulationRound]) -> Result[SimulationResult, SimulationError]fn softmax(logits : Array[Double], temperature? : Double) -> Result[Array[Double], ProbabilityError]fn summarize_experiment(pairs : Array[ExperimentPair]) -> Result[ExperimentSummary, ExperimentError]fn top_tokens(logits : Array[Double], width : Int) -> Result[Array[(Int, Array[Double])], PolicyError]fn trace_single_path(round : Int, proposal : DraftProposal, target_distributions : Array[Array[Double]], accept_uniforms : Array[Double]) -> Result[DecodeTrace, VerifyError]fn verify_from_replay(target : ReplayModel, proposal : DraftProposal, accept_uniforms : Array[Double], fallback_uniforms : Array[Double]) -> Result[VerifyResult, ReplayDecodeError]fn verify_proposal(proposal : DraftProposal, target_distributions : Array[Array[Double]], accept_uniforms : Array[Double], fallback_uniforms : Array[Double]) -> Result[VerifyResult, VerifyError]Install
Download zipMoonBit tree speculative decoding algorithms and reproducible offline experiments