v0.16.0: fechamento dos 5 débitos r-51 §10 + elástico ativo (provas r-52, resultados r-53) — semeadura determinística do init_stiefel e do codebook (T-S1: errata maior v015-dev-7 ERRADICADA — verificação bit-a-bit de 2 corridas IGUAL em dados reais), elástico ATIVO com variações de teste simultâneas e variâncias contextuais (T-A1..A5: Thompson por contexto, Welford kernel AVX-512 16 lanes T-W0, orçamento de exploração monótono, 7,2 simultâneas/réplica medidas), TC-quad warm-start ESPECTRAL Marchenko–Pastur (T-W1: ρ=0,05 ABAIXO do piso v0.15 — 87% vs 0; ρ=0,1 93% vs 47%; latentes reais 8,01 vs 6,46 nats; errata v016-dev-7: jackknife por grupos inválido para cauda pesada), R=10 réplicas × 6 braços com permutação pareada (p=0,52 — honesto), GumbelSt 800 passos com CE held-out no shard de validação (gap +2,1% vs +7,7% em treino), diagnóstico por coluna como série contínua (D5), 32 parâmetros não-estáticos — 28/28 gates, cargo 263/263 (235 v0.15 preservados), 11 erratas v016-dev documentadas (part 2)
b8768de verified Download src/python/nucleo.rs from PowerMachine/khtst-rust: direct link, hf CLI and curl.
- Browser
- Download file 8.32 kB
-
https://huggingface.co/PowerMachine/khtst-rust/resolve/58d56d96bbbc7b0dee6270bfc8355c3ca04f20e6/src/python/nucleo.rs
- Command line
-
hf download hf://PowerMachine/khtst-rust@58d56d96bbbc7b0dee6270bfc8355c3ca04f20e6/src/python/nucleo.rs
-
curl -L -o nucleo.rs https://huggingface.co/PowerMachine/khtst-rust/resolve/58d56d96bbbc7b0dee6270bfc8355c3ca04f20e6/src/python/nucleo.rs
8.32 kB
| //! `python::nucleo` — extraído do monólito na v0.10.0 | |
| //! (divisão SRP, crítica Qwen3.8 lote 3 nº 1; r-35 §2). | |
| use super::*; | |
| pub(crate) fn versao() -> &'static str { | |
| concat!("khtst-rust-", env!("CARGO_PKG_VERSION")) | |
| } | |
| pub(crate) fn info_build() -> String { | |
| format!( | |
| "rustc={}; perfil={}; features={}; alvo={}", | |
| build_info::RUSTC_VERSION, | |
| build_info::PROFILE, | |
| build_info::FEATURES_STR, | |
| build_info::TARGET, | |
| ) | |
| } | |
| /// Lint de config com sugestões por Levenshtein (diagnóstico de digitação). | |
| pub(crate) fn lint_config(py: Python, caminho: &str) -> PyResult<PyObject> { | |
| let txt = std::fs::read_to_string(caminho) | |
| .map_err(|e| pyo3::exceptions::PyIOError::new_err(format!("{caminho}: {e}")))?; | |
| let v: serde_json::Value = serde_json::from_str(&txt) | |
| .map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("JSON inválido: {e}")))?; | |
| let achatado = diagnostico::achatar(&v); | |
| let avisos = diagnostico::lint_chaves(&achatado); | |
| let saida: Vec<serde_json::Value> = avisos | |
| .iter() | |
| .map(|a| serde_json::json!({ | |
| "chave": a.chave, "sugestao": a.sugestao, "causa": a.causa, "correcao": a.correcao, | |
| })) | |
| .collect(); | |
| Ok(pythonize::pythonize(py, &serde_json::json!(saida))?.into()) | |
| } | |
| pub(crate) struct BpePy { | |
| bpe: tokens::Bpe, | |
| } | |
| impl BpePy { | |
| fn treinar(textos: Vec<String>, vocab_alvo: usize) -> PyResult<Self> { | |
| Ok(BpePy { bpe: tokens::Bpe::treinar(&textos, vocab_alvo) }) | |
| } | |
| fn carregar(caminho: &str) -> PyResult<Self> { | |
| let txt = std::fs::read_to_string(caminho) | |
| .map_err(|e| pyo3::exceptions::PyIOError::new_err(format!("{e}")))?; | |
| let v: serde_json::Value = serde_json::from_str(&txt) | |
| .map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("JSON inválido: {e}")))?; | |
| let bpe = tokens::Bpe::de_json(&v) | |
| .ok_or_else(|| pyo3::exceptions::PyValueError::new_err("formato BPE inválido"))?; | |
| Ok(BpePy { bpe }) | |
| } | |
| fn codificar(&mut self, texto: &str) -> Vec<u32> { | |
| self.bpe.codificar(texto) | |
| } | |
| fn decodificar(&self, ids: Vec<u32>) -> String { | |
| self.bpe.decodificar(&ids) | |
| } | |
| fn salvar(&self, caminho: &str) -> PyResult<()> { | |
| let txt = serde_json::to_string(&self.bpe.para_json()).unwrap(); | |
| crate::checkpoint::salvar_atomico(caminho, &txt) | |
| .map_err(|e| pyo3::exceptions::PyIOError::new_err(format!("{e}")))?; | |
| Ok(()) | |
| } | |
| fn n_vocab(&self) -> usize { | |
| self.bpe.n_vocab() | |
| } | |
| } | |
| pub(crate) struct ConfigPy { | |
| pub(crate) cfg: config::ConfigKhtst, | |
| caminho: Option<String>, | |
| } | |
| impl ConfigPy { | |
| fn new(caminho: Option<String>) -> PyResult<Self> { | |
| let cfg = match &caminho { | |
| Some(c) => { | |
| let txt = std::fs::read_to_string(c) | |
| .map_err(|e| pyo3::exceptions::PyIOError::new_err(format!("{c}: {e}")))?; | |
| let v: serde_json::Value = serde_json::from_str(&txt) | |
| .map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("JSON inválido: {e}")))?; | |
| serde_json::from_value(v) | |
| .map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("config: {e}")))? | |
| } | |
| None => config::ConfigKhtst::default(), | |
| }; | |
| cfg.validar().map_err(pyo3::exceptions::PyValueError::new_err)?; | |
| Ok(ConfigPy { cfg, caminho }) | |
| } | |
| fn validar(&self) -> PyResult<()> { | |
| self.cfg.validar().map_err(pyo3::exceptions::PyValueError::new_err) | |
| } | |
| fn checar_orcamento_cache(&self) -> PyResult<usize> { | |
| self.cfg.checar_orcamento_cache().map_err(pyo3::exceptions::PyValueError::new_err) | |
| } | |
| fn resumo(&self) -> String { | |
| format!( | |
| "modelo(vocab={}, d={}, camadas={}, ctx={}) ncm(L={}, niveis={:?}, K={}, d_lat={}, modo={}, ativo={})", | |
| self.cfg.modelo.vocab, self.cfg.modelo.d_modelo, self.cfg.modelo.n_camadas, | |
| self.cfg.modelo.comprimento_ctx, self.cfg.l_niveis(), self.cfg.ncm.niveis, | |
| self.cfg.ncm.k_por_nivel, self.cfg.ncm.d_lat, self.cfg.ncm.modo_quantizacao, self.cfg.ncm.ativo, | |
| ) | |
| } | |
| fn l_niveis(&self) -> usize { | |
| self.cfg.l_niveis() | |
| } | |
| } | |
| pub(crate) struct PonteDuplaPy { | |
| ponte: ponte::PonteDupla, | |
| } | |
| impl PonteDuplaPy { | |
| fn new(capacidade: usize, timeout_ms: u64) -> Self { | |
| PonteDuplaPy { ponte: ponte::PonteDupla::nova(capacidade, timeout_ms) } | |
| } | |
| fn rodada(&self, carga_a: Vec<f32>, carga_b: Vec<f32>, nivel: usize) -> PyResult<(Vec<f32>, Vec<f32>)> { | |
| let (ma, mb) = self.ponte.rodada(carga_a, carga_b, nivel) | |
| .map_err(pyo3::exceptions::PyRuntimeError::new_err)?; | |
| Ok((ma.carga, mb.carga)) | |
| } | |
| fn demo_professor_aluno(&self, rodadas: usize, dim: usize) -> PyResult<PyObject> { | |
| let inicio: Vec<f32> = (0..dim).map(|i| (i as f32) * 0.01).collect(); | |
| let stats = ponte::demo_professor_aluno(&self.ponte, rodadas, inicio, &|a, _| { | |
| a.iter().map(|x| x * 1.05).collect() | |
| }) | |
| .map_err(pyo3::exceptions::PyRuntimeError::new_err)?; | |
| Python::with_gil(|py| { | |
| Ok(pythonize::pythonize(py, &serde_json::to_value(&stats).unwrap())?.into()) | |
| }) | |
| } | |
| fn estatisticas(&self) -> PyResult<PyObject> { | |
| let s = self.ponte.estatisticas(); | |
| Python::with_gil(|py| { | |
| Ok(pythonize::pythonize(py, &serde_json::to_value(&s).unwrap())?.into()) | |
| }) | |
| } | |
| } | |
| pub(crate) struct PonteAsyncPy { | |
| interna: std::sync::Arc<ponte::assincrona::PonteAsync>, | |
| } | |
| impl PonteAsyncPy { | |
| fn new(capacidade: usize, timeout_ms: u64) -> Self { | |
| PonteAsyncPy { interna: std::sync::Arc::new(ponte::assincrona::PonteAsync::nova(capacidade, timeout_ms)) } | |
| } | |
| /// Corrotina: `await ponte.trocar(a, b, nivel)` — duplex simultânea. | |
| fn trocar<'p>(&self, py: Python<'p>, carga_a: Vec<f32>, carga_b: Vec<f32>, nivel: usize) -> PyResult<Bound<'p, PyAny>> { | |
| let p = self.interna.clone(); | |
| pyo3_async_runtimes::tokio::future_into_py(py, async move { | |
| let (ma, mb) = p.rodada_async(carga_a, carga_b, nivel) | |
| .await | |
| .map_err(pyo3::exceptions::PyRuntimeError::new_err)?; | |
| Ok::<_, PyErr>((ma.carga, mb.carga)) | |
| }) | |
| } | |
| } | |
| /// Utilitários de prova expostos ao Python (V-série da suíte). | |
| pub(crate) fn verificar_mascara(cfg: &ConfigPy) -> PyResult<usize> { | |
| let dev = candle_core::Device::Cpu; | |
| let m = mascaras::mascara_block_causal(&cfg.cfg.ncm.niveis, gerador::N_PREFIXO, &dev) | |
| .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("{e}")))?; | |
| mascaras::contar_vazamentos(&cfg.cfg.ncm.niveis, gerador::N_PREFIXO, &m) | |
| .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("{e}"))) | |
| } | |
| pub(crate) fn verificar_stiefel(k: usize, d: usize) -> PyResult<f64> { | |
| let dev = candle_core::Device::Cpu; | |
| let c = stiefel::init_stiefel(k, d, &dev) | |
| .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("{e}")))?; | |
| stiefel::erro_ortogonalidade(&c.transpose(0, 1).unwrap()).map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("{e}"))) | |
| } | |
| pub(crate) fn checkpoint_valido(caminho: &str) -> bool { | |
| crate::checkpoint::validar(caminho) | |
| } | |
| pub(crate) fn checkpoint_manifesto(caminho: &str) -> Option<String> { | |
| std::fs::read_to_string(crate::checkpoint::caminho_manifesto(caminho)).ok() | |
| } | |
| pub(crate) fn sha256_arquivo(caminho: &str) -> Option<String> { | |
| std::fs::read(caminho).ok().map(|d| crate::checkpoint::sha256_hex(&d)) | |
| } | |