open-standards/eoc/eoc-rs/crates/eoc-quantize/src/scheme.rs
Transaction Science 8180ee8c71 ingest wave D: 3 crates · op-fraud-graph + smart-byte-ilp + eoc-quantize
OpenPay (1 crate, 2487 LOC, 33 tests)
  op-fraud-graph    Graph-based fraud detection. EntityKind (card/email/
                    device/IP/address/phone/BIN+last4/account) with
                    canonicalization + hashed EntityKey + billing↔shipping
                    mismatch detection. Adjacency-list FraudGraph with
                    idempotent upsert + edge-merge. Double-hashing Bloom
                    filter (capacity+FPR-sized). Union-find connected
                    components. PageRank with dangling-sink handling.
                    Louvain community detection. Shared-instrument hub
                    rings. Sliding ring-buffer velocity counters. Synthetic
                    identity heuristics (freshness + heavy-use + mail-drop).
                    Structuring / smurfing / mule-chain DFS. EntityRisk +
                    TransactionRisk + RiskBand combiner.

Smart Byte (1 crate, ~1844 LOC, 44 tests)
  smart-byte-ilp    Interledger Protocol v4. OER codec (length-determinant,
                    var-octet-string, BE u32/u64). Prepare/Fulfill/Reject
                    packet roundtrip with all 19 IL-RFC-27 reject codes.
                    SHA-256 Condition + Fulfillment with constant-time
                    verify. ILP address validator (7 schemes, 1-128 char
                    segments, 1023-byte cap, segment-aligned prefix match).
                    STREAM (15 frame types). BTP. SPSP payment-pointer
                    resolver. Open Payments API. Longest-prefix routing
                    Connector with fixed-point quote conversion + balance +
                    rate-limit checks.

EOC (1 crate, 1921 LOC, 36 tests)
  eoc-quantize      Quantization primitives. int8 (symmetric/asymmetric
                    with zero-inclusion fix), int4 (GPTQ + AWQ group quant),
                    int2 (BitNet), fp8 (E4M3/E5M2), nf4 (QLoRA). GGUF v3
                    header parse. safetensors read sketch. Calibration
                    datasets + per-channel scales. KV-cache quant. Energy
                    tradeoff curves (joules-per-token across schemes).

Co-Authored-By: Claude Opus 4.7 <[email protected]>
2026-05-24 16:10:10 -04:00

72 lines
2.1 KiB
Rust

//! Quantization scheme trait + the catalogue of numeric kinds.
use serde::{Deserialize, Serialize};
/// The numeric kinds known to EOC. Bits-per-weight is the energy axis;
/// these values are what we route on.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Numeric {
/// IEEE 754 binary32.
Fp32,
/// IEEE 754 binary16.
Fp16,
/// bfloat16.
Bf16,
/// OFP8 E4M3 (Nvidia Hopper training/inference).
Fp8E4m3,
/// OFP8 E5M2 (Nvidia Hopper training).
Fp8E5m2,
/// 8-bit integer, symmetric or asymmetric.
Int8,
/// 4-bit grouped integer (GPTQ / AWQ family).
Int4,
/// NormalFloat4 (QLoRA).
Nf4,
/// 2-bit ternary / sign-magnitude (BitNet family).
Int2,
}
impl Numeric {
/// Bits per weight stored on disk (excluding metadata / scales).
pub fn bits_per_weight(self) -> u32 {
match self {
Self::Fp32 => 32,
Self::Fp16 | Self::Bf16 => 16,
Self::Fp8E4m3 | Self::Fp8E5m2 | Self::Int8 => 8,
Self::Int4 | Self::Nf4 => 4,
Self::Int2 => 2,
}
}
/// Short label.
pub fn label(self) -> &'static str {
match self {
Self::Fp32 => "fp32",
Self::Fp16 => "fp16",
Self::Bf16 => "bf16",
Self::Fp8E4m3 => "fp8-e4m3",
Self::Fp8E5m2 => "fp8-e5m2",
Self::Int8 => "int8",
Self::Int4 => "int4",
Self::Nf4 => "nf4",
Self::Int2 => "int2",
}
}
}
/// Abstract quantization scheme: round-trip between dense `f32` weights
/// and a packed encoded form, with associated metadata (scales, zero
/// points, etc.).
pub trait QuantizationScheme {
/// Encoded representation produced by `quantize`.
type Encoded;
/// The numeric kind this scheme produces.
fn numeric(&self) -> Numeric;
/// Encode a slice of fp32 weights into the packed form.
fn quantize(&self, weights: &[f32]) -> Self::Encoded;
/// Decode the packed form back to fp32.
fn dequantize(&self, encoded: &Self::Encoded) -> Vec<f32>;
}