File size: 8,319 Bytes
06e4b6f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | //! `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::*;
#[pyfunction]
pub(crate) fn versao() -> &'static str {
concat!("khtst-rust-", env!("CARGO_PKG_VERSION"))
}
#[pyfunction]
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).
#[pyfunction]
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())
}
#[pyclass(name = "Bpe")]
pub(crate) struct BpePy {
bpe: tokens::Bpe,
}
#[pymethods]
impl BpePy {
#[staticmethod]
fn treinar(textos: Vec<String>, vocab_alvo: usize) -> PyResult<Self> {
Ok(BpePy { bpe: tokens::Bpe::treinar(&textos, vocab_alvo) })
}
#[staticmethod]
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(())
}
#[getter]
fn n_vocab(&self) -> usize {
self.bpe.n_vocab()
}
}
#[pyclass(name = "Config")]
pub(crate) struct ConfigPy {
pub(crate) cfg: config::ConfigKhtst,
caminho: Option<String>,
}
#[pymethods]
impl ConfigPy {
#[new]
#[allow(never_type_fallback)]
#[pyo3(signature = (caminho=None))]
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,
)
}
#[getter]
fn l_niveis(&self) -> usize {
self.cfg.l_niveis()
}
}
#[pyclass(name = "PonteDupla")]
pub(crate) struct PonteDuplaPy {
ponte: ponte::PonteDupla,
}
#[pymethods]
impl PonteDuplaPy {
#[new]
#[pyo3(signature = (capacidade=64, timeout_ms=2000))]
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())
})
}
}
#[cfg(feature = "ponte_async")]
#[pyclass(name = "PonteAsync")]
pub(crate) struct PonteAsyncPy {
interna: std::sync::Arc<ponte::assincrona::PonteAsync>,
}
#[cfg(feature = "ponte_async")]
#[pymethods]
impl PonteAsyncPy {
#[new]
#[pyo3(signature = (capacidade=64, timeout_ms=2000))]
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).
#[pyfunction]
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}")))
}
#[pyfunction]
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}")))
}
#[pyfunction]
pub(crate) fn checkpoint_valido(caminho: &str) -> bool {
crate::checkpoint::validar(caminho)
}
#[pyfunction]
pub(crate) fn checkpoint_manifesto(caminho: &str) -> Option<String> {
std::fs::read_to_string(crate::checkpoint::caminho_manifesto(caminho)).ok()
}
#[pyfunction]
pub(crate) fn sha256_arquivo(caminho: &str) -> Option<String> {
std::fs::read(caminho).ok().map(|d| crate::checkpoint::sha256_hex(&d))
}
|