Files
6381918256 hanzo-hmm: HMM compute-job pricing (compute_price) — the canonical pricing (#1)
Price a heterogeneous, SLA-bound compute job via the Hamiltonian Market Maker,
NOT a constant-product (x*y=k) AMM. Compute is perishable/heterogeneous/SLA-
bound; an AMM prices a fungible storable pair and cannot express scarcity
convexity, deadline pressure, or per-resource isolation.

compute_price::price(job, market) -> HanzoPrice (wei, 18 decimals):
- maps market imbalance (demand vs supply) into the Hamiltonian phase space
  (position = tanh(imbalance) shadow-price displacement; momentum = elasticity *
  displacement) under an ANHARMONIC potential (quartic term => super-linear
  scarcity bite that a quadratic / x*y=k cannot express);
- evolves the existing symplectic leapfrog integrator with friction to the
  energy-modulated equilibrium and reads HamiltonianDynamics::calculate_price
  (price modulated by total energy E = T + V; scarcity raises E raises price);
- modulates by SLA tightness (tighter deadline => higher price, perishability)
  and quality/privacy tier, then clamps to [1e13, 1e16] wei (AI_TOKEN_ECONOMICS
  min/max). Deterministic (noise-free path, no rng) => reproducible + testable.

Pure ADAPTER over crate::hamiltonian — re-implements no mechanics (DRY). The
broker calls this to set the on-chain escrow amount; ComputeSettlement then
settles that amount against a canonical PoAI proof (LP-302).

Tests (8 named properties + tier + bad-input): monotone in scarcity, monotone
in demand, SLA perishability, bounded, heterogeneity (resources price
independently), determinism, tier ordering, explicit input rejection. Found +
fixed a real bug: raw-imbalance seeding diverged the stiff quartic to NaN at
extreme scarcity; normalized tanh seeding keeps the integrator in its stable
basin while a strictly-increasing scarcity factor preserves monotonicity.
cargo test -p hanzo-hmm: 25/25 (8 new + 17 pre-existing, no regression).

hamiltonian.rs: add set_phase_space(PhaseSpace) (dimension-checked) so (q,p) can
be seeded deterministically without the stochastic perturb path. No behavior
change to existing API; all prior tests still pass.

Refs LP-302 (settlement), zip-0418 / AI_TOKEN_ECONOMICS (HMM).

Co-authored-by: zeekay <z@zeekay.io>
2026-06-25 14:42:18 -07:00
..

hanzo-hmm — Hidden Markov Model + Hamiltonian MarketMaker

Pure Hidden Markov Model primitives plus a Hamiltonian MarketMaker built on top of them, used in the Hanzo network to price heterogeneous compute.

Naming. This crate is not an LLM engine. It is the pricing / routing layer. The actual model-serving engine lives in ~/work/hanzo/engine (a mistral.rs fork exposed as hanzo-engine).

Two layers, one crate

Layer Module Purpose
HMM core hmm_core Viterbi, forward-backward, Baum-Welch on HiddenMarkovModel<S, O>
MarketMaker lib::MarketMaker Hamiltonian price dynamics + BitDelta adapters + active-inference routing

The HMM layer is standalone and reusable for any sequence modelling task (state detection, sequence prediction, anomaly detection). The MarketMaker layer composes HMM regime detection with Hamiltonian mechanics and BitDelta-quantized per-tenant adapters to set prices and routing decisions across compute classes.

HMM core usage

use hanzo_hmm::HiddenMarkovModel;

let states       = vec!["Fair", "Loaded"];
let observations = vec![1, 2, 3, 4, 5, 6];
let initial      = vec![0.5, 0.5];
let transitions  = vec![
    vec![0.7, 0.3],
    vec![0.4, 0.6],
];
let emissions = vec![
    vec![1.0 / 6.0; 6],
    vec![0.1, 0.1, 0.1, 0.1, 0.1, 0.5],
];

let hmm = HiddenMarkovModel::new(
    states, observations, initial, transitions, emissions,
)?;

let observed = vec![6, 6, 6, 1, 2];
let path     = hmm.viterbi(&observed)?;          // most likely state sequence
let p        = hmm.forward(&observed)?;          // P(observations | model)
let _seq     = hmm.generate(100);                // sample observations

MarketMaker usage

use hanzo_hmm::{MarketMaker, Config, RoutingRequest, UserPreferences, PerformanceRequirements};

let mm = MarketMaker::new(Config::default()).await?;

let decision = mm.route_request("tenant-42", &RoutingRequest {
    input: "...".into(),
    context: vec![],
    preferences: UserPreferences {
        max_latency_ms: Some(1_500),
        max_cost_per_token: Some(0.001),
        preferred_models: vec![],
        quality_threshold: 0.8,
    },
    requirements: PerformanceRequirements {
        min_tokens_per_second: Some(40.0),
        max_memory_gb: Some(48.0),
        requires_function_calling: false,
        requires_vision: false,
    },
    observations: vec![0.1, 0.2, 0.05, 0.4],
}).await?;

Algorithms

  • Viterbi — most likely state sequence given observations
  • ForwardP(observations | model)
  • Backward — backward probabilities per state
  • Baum-Welch — parameter learning from observation sequences
let path  = hmm.viterbi(&observations)?;
let prob  = hmm.forward(&observations)?;
let beta  = hmm.backward(&observations)?;
hmm.baum_welch(&training_sequences, max_iterations, tolerance)?;

License

MIT