// SPDX-License-Identifier: Apache-2.0
//
// Archived: 2026-07-12
// Reason: Replaced by C kernel FFI (c-kernels/codec.c) for the inner
// nearest-codeword loop. The Rust orchestration (encode_kv_tensor,
// decode_kv_pages, decode_kv_slice) remains in kv/codec.rs — only the
// inner numerical loop was moved to C.
//
// This file preserves the original Rust `encode_vector_block` from
// kv/codec.rs (line ~243) for reference. The function below is NOT
// compiled or used — it exists purely as a historical record.
#![allow(dead_code)]
/// Original Rust encode_vector_block from kv/codec.rs.
///
/// Encodes a single vector block by delegating to `FibQuantizer::encode`,
/// which internally performs:
/// 1. L2 normalize the vector.
/// 2. Apply the deterministic rotation (f64 → f32).
/// 3. For each block of `block_dim` floats, find the nearest codeword
/// via `gpu_backend::nearest_codeword_f32` (squared L2 argmin).
/// 4. Pack indices with `pack_indices`.
///
/// The C replacement (`fq_encode_vector_block` in c-kernels/codec.c)
/// handles step 3 only. Steps 1, 2, and 4 remain in Rust.
#[allow(clippy::too_many_arguments)]
fn encode_vector_block(
quantizer: &crate::FibQuantizer,
profile: &crate::profile::FibQuantProfileV1,
block_id: u32,
batch: u32,
layer: u32,
head: u32,
token: u32,
vector: &[f32],
) -> Result<crate::kv::block::KvEncodedBlockV1, crate::FibQuantError> {
use crate::kv::block::{KvBlockEncodingV1, KvEncodedBlockV1};
use crate::kv::profile::{KvAxisPolicyV1, KvFallbackModeV1};
match profile.axis_policy {
KvAxisPolicyV1::Raw => Ok(KvEncodedBlockV1::raw(
block_id, batch, layer, head, token,
vector.to_vec(),
0, // caller provides encoded_block_bytes
"raw_axis_policy",
)),
KvAxisPolicyV1::PerToken => match quantizer.encode(vector) {
Ok(code) => Ok(KvEncodedBlockV1::fib_quant(
block_id, batch, layer, head, token,
code,
0,
"fib_quant_per_token",
)),
Err(err) if profile.fallback_policy.mode == KvFallbackModeV1::KeepRaw => {
Ok(KvEncodedBlockV1::raw(
block_id, batch, layer, head, token,
vector.to_vec(),
0,
format!("encode_fallback:{err}"),
))
}
Err(err) => Err(err),
},
KvAxisPolicyV1::PerChannel | KvAxisPolicyV1::RoleAwareKiviStyle => {
if profile.fallback_policy.mode == KvFallbackModeV1::KeepRaw {
Ok(KvEncodedBlockV1::raw(
block_id, batch, layer, head, token,
vector.to_vec(),
0,
"unsupported_axis_raw_fallback",
))
} else {
Err(crate::FibQuantError::DependencyUnsupported(
"CPU reference codec supports per-token FibQuant compression only".into(),
))
}
}
}
}