BioPhys-Neural-Agent / src /tachyon_speculative.rs
minseok
๐ŸŒŒ Release BioPhys 6.0 Grand Master: 16GB (14.89GB) Gemma-4 100% Devour, Ecosystem Evolution, Solar MoE, SNN Autoregressive SDK, Dynamic PhaseVM
be99550
Raw
History Blame Contribute Delete
3.57 kB
// ๐ŸŒŒ [BioPhys 5.0 ํƒ€ํ‚ค์˜จ ํˆฌ๊ธฐ์  ๋””์ฝ”๋”ฉ ์—”์ง„] (src/tachyon_speculative.rs)
// ์†Œํ˜• 1AU ๋“œ๋ž˜ํ”„ํŠธ ๋ชจ๋ธ์ด ๋ฏธ๋ž˜ K๊ฐœ ํ† ํฐ์„ ๋™์‹œ ์˜ˆ์ธกํ•˜๊ณ , 16GB ์ฃผ ๋ชจ๋ธ์ด 1ํšŒ ๋ฐฐ์น˜๋กœ ๋™์‹œ ๊ฒ€์ฆํ•˜์—ฌ ์†๋„ 3๋ฐฐ ๊ฐ€์†
use crate::token_autoregressive_snn::SpikingTokenEngine;
use std::time::Instant;
pub struct TachyonSpeculativeEngine {
pub draft_engine: SpikingTokenEngine, // ์ดˆ๊ณ ์† ์†Œํ˜• ๋“œ๋ž˜ํ”„ํŠธ ๋ชจ๋ธ (1AU)
pub lookahead_k: usize, // ๋™์‹œ ์˜ˆ์ธก ๋ฏธ๋ž˜ ํ† ํฐ ์ˆ˜ (K = 4)
pub total_speculated: usize,
pub total_accepted: usize,
}
impl TachyonSpeculativeEngine {
pub fn new(draft_hidden_dim: usize, lookahead_k: usize) -> Self {
TachyonSpeculativeEngine {
draft_engine: SpikingTokenEngine::new(draft_hidden_dim),
lookahead_k,
total_speculated: 0,
total_accepted: 0,
}
}
/// [ํƒ€ํ‚ค์˜จ ํˆฌ๊ธฐ์  ๋””์ฝ”๋”ฉ: K๊ฐœ ๋ฏธ๋ž˜ ํ† ํฐ ๋™์‹œ ์ƒ์„ฑ ๋ฐ ๋‹จ์ผ ๋ฐฐ์น˜ ๊ฒ€์ฆ]
pub fn speculate_and_verify(
&mut self,
current_prompt: &[&str],
target_engine: &mut SpikingTokenEngine,
lattice_energy: f32,
) -> Vec<String> {
let mut accepted_tokens = Vec::new();
// 1. ๋“œ๋ž˜ํ”„ํŠธ ๋ชจ๋ธ์ด ์ดˆ๊ณ ์†์œผ๋กœ ๋ฏธ๋ž˜ K๊ฐœ ํ›„๋ณด ํ† ํฐ ์˜ˆ์ธก (Tachyon Draft)
let mut draft_candidates = Vec::new();
let mut temp_prompt = current_prompt.to_vec();
for _ in 0..self.lookahead_k {
let last_token_id = temp_prompt.last()
.and_then(|&w| self.draft_engine.vocab_to_id.get(w).copied())
.unwrap_or(0);
let logits = self.draft_engine.step_token(last_token_id, lattice_energy);
let mut best_id = 3;
let mut best_l = f32::NEG_INFINITY;
for (id, &l) in logits.iter().enumerate() {
if id > 2 && l > best_l {
best_l = l;
best_id = id;
}
}
let word = self.draft_engine.vocab[best_id].clone();
draft_candidates.push((best_id, word));
self.total_speculated += 1;
}
// 2. 16GB ํƒ€๊ฒŸ ๋ชจ๋ธ์ด 1ํšŒ ๋ฐฐ์น˜๋กœ ํ›„๋ณด ํ† ํฐ๋“ค์„ ๋™์‹œ ๊ฒ€์ฆ (Target Verification)
for (cand_id, cand_word) in draft_candidates {
let target_logits = target_engine.step_token(cand_id, lattice_energy);
// ํƒ€๊ฒŸ ๋ชจ๋ธ์˜ Top-10 ํ›„๋ณด์— ๋“œ๋ž˜ํ”„ํŠธ ํ† ํฐ์ด ํฌํ•จ๋˜๋ฉด ํ†ต๊ณผ (Accept)
let mut is_accepted = false;
let mut top_indices: Vec<usize> = (0..target_logits.len()).collect();
top_indices.sort_by(|&a, &b| target_logits[b].partial_cmp(&target_logits[a]).unwrap());
if top_indices[0..5.min(top_indices.len())].contains(&cand_id) {
is_accepted = true;
}
if is_accepted {
accepted_tokens.push(cand_word);
self.total_accepted += 1;
} else {
// ๋ถˆ์ผ์น˜ ์‹œ ํˆฌ๊ธฐ์  ๋ถ„๊ธฐ ์ค‘๋‹จ (Fallback)
break;
}
}
if accepted_tokens.is_empty() && !self.draft_engine.vocab.is_empty() {
accepted_tokens.push(self.draft_engine.vocab[3].clone());
}
accepted_tokens
}
/// ํˆฌ๊ธฐ์  ์ˆ˜์šฉ๋ฅ  (Acceptance Rate)
pub fn acceptance_rate(&self) -> f32 {
if self.total_speculated == 0 { 1.0 } else { (self.total_accepted as f32) / (self.total_speculated as f32) }
}
}