AQIT 0.1.0
Loading...
Searching...
No Matches
aquin.compute.train_simulate Namespace Reference

Classes

class  SimulateRequest
class  CompareRequest

Functions

bool _is_heavy_sim_model (dict[str, Any] cfg)
int _sim_batch_limit (dict[str, Any] cfg)
list[torch.Tensor] _grad_vector (torch.Tensor loss, list[torch.nn.Parameter] params, bool retain_graph=False)
list[torch.Tensor] _hvp (torch.Tensor loss, list[torch.nn.Parameter] params, list[torch.Tensor] v)
list[torch.Tensor] _hvp_from_fn (loss_fn, list[torch.nn.Parameter] params, list[torch.Tensor] v)
float _grad_dot (list[torch.Tensor] ga, list[torch.Tensor] gb)
float _tensor_list_norm (list[torch.Tensor] ts)
float _normalized_influence (list[torch.Tensor] ga, list[torch.Tensor] gb)
list[torch.Tensor] _lissa_inverse_hvp (test_loss_fn, train_loss_fn, int n_train, list[torch.nn.Parameter] params, float scale=25.0, float damping=0.05, int n_iter=10)
list[dict] _influence_via_grad_dot (test_loss_fn, list[int] train_indices, list[dict] rows, list[torch.nn.Parameter] params, train_loss_fn)
dict[int, float] _ntk_diagonal (torch.Tensor loss, list[torch.nn.Parameter] params, list[torch.Tensor] grads)
float _power_iteration_max_eigenvalue (torch.Tensor loss, list[torch.nn.Parameter] params, int n_iter=3)
int _hidden_state_index (model, int sae_layer)
torch.Tensor|None _sae_grad_scores_from_batch (model, sae, int sae_layer, torch.Tensor input_ids, torch.Tensor attention_mask, torch.Tensor|None base_acts_mean)
dict[str, Any] _run_dataset_quality (list[dict] rows)
dict _detect_ai_generated (list[dict] rows)
None _run_simulation (SimulateRequest req, asyncio.Queue queue, asyncio.AbstractEventLoop loop)
 training_simulate (SimulateRequest req)
Any _sanitize (Any obj)
dict compare_simulation_results (dict result_a, dict result_b, str label_a="Run A", str label_b="Run B", *, str run_id_a="", str run_id_b="")
 compare_simulations (CompareRequest req)

Variables

 router = APIRouter()
 DEVICE = resolve_compute_device()
 DTYPE = default_dtype_for_device(DEVICE)
int _SIM_MAX_SAMPLES = 64
str _SIM_FT_CKPT_PATH = None
str _SIM_FT_MODEL_ID = None
list _HARMFUL_KEYWORDS
list _AI_FINGERPRINTS

Function Documentation

◆ _detect_ai_generated()

dict _detect_ai_generated ( list[dict] rows)
protected
Check if >30% of responses match common AI generation fingerprints.

Definition at line 445 of file train_simulate.py.

Referenced by _run_dataset_quality().

◆ _grad_dot()

float _grad_dot ( list[torch.Tensor] ga,
list[torch.Tensor] gb )
protected

Definition at line 167 of file train_simulate.py.

Referenced by _normalized_influence().

◆ _grad_vector()

list[torch.Tensor] _grad_vector ( torch.Tensor loss,
list[torch.nn.Parameter] params,
bool retain_graph = False )
protected
Detached per-parameter gradient vector for loss.

Definition at line 130 of file train_simulate.py.

Referenced by _influence_via_grad_dot(), _lissa_inverse_hvp(), and _run_simulation().

◆ _hidden_state_index()

int _hidden_state_index ( model,
int sae_layer )
protected
Map SAE layer index → output_hidden_states tuple index.

Definition at line 284 of file train_simulate.py.

Referenced by _sae_grad_scores_from_batch().

◆ _hvp()

list[torch.Tensor] _hvp ( torch.Tensor loss,
list[torch.nn.Parameter] params,
list[torch.Tensor] v )
protected
Hessian-vector product H·v via double backprop.

Definition at line 142 of file train_simulate.py.

Referenced by _hvp_from_fn(), _ntk_diagonal(), and _power_iteration_max_eigenvalue().

◆ _hvp_from_fn()

list[torch.Tensor] _hvp_from_fn ( loss_fn,
list[torch.nn.Parameter] params,
list[torch.Tensor] v )
protected
HVP with a fresh forward inside :  avoids reusing freed loss graphs.

Definition at line 156 of file train_simulate.py.

References _hvp().

Referenced by _lissa_inverse_hvp().

◆ _influence_via_grad_dot()

list[dict] _influence_via_grad_dot ( test_loss_fn,
list[int] train_indices,
list[dict] rows,
list[torch.nn.Parameter] params,
train_loss_fn )
protected
First-order influence: -∇L_test · ∇L_train (no Hessian). Reliable on PEFT/LoRA.

Definition at line 219 of file train_simulate.py.

References _grad_vector(), and _normalized_influence().

Referenced by _run_simulation().

◆ _is_heavy_sim_model()

bool _is_heavy_sim_model ( dict[str, Any] cfg)
protected
HF-only or wide models need lighter simulate passes (VRAM).

Definition at line 62 of file train_simulate.py.

Referenced by _run_simulation(), and _sim_batch_limit().

◆ _lissa_inverse_hvp()

list[torch.Tensor] _lissa_inverse_hvp ( test_loss_fn,
train_loss_fn,
int n_train,
list[torch.nn.Parameter] params,
float scale = 25.0,
float damping = 0.05,
int n_iter = 10 )
protected
Approximate H^{-1} · ∇L_test via LiSSA.
v_{t+1} = v_0 + (1 - damping)·v_t - (1/scale)·H·v_t
where v_0 = ∇L_test (held fixed).

train_loss_fn(j) must return a fresh scalar loss for batch j on each call.

Definition at line 192 of file train_simulate.py.

References _grad_vector(), and _hvp_from_fn().

Referenced by _run_simulation().

◆ _normalized_influence()

float _normalized_influence ( list[torch.Tensor] ga,
list[torch.Tensor] gb )
protected
Cosine-style influence in [-1, 1] :  stable for display and ranking.

Definition at line 181 of file train_simulate.py.

References _grad_dot(), and _tensor_list_norm().

Referenced by _influence_via_grad_dot(), and _run_simulation().

◆ _ntk_diagonal()

dict[int, float] _ntk_diagonal ( torch.Tensor loss,
list[torch.nn.Parameter] params,
list[torch.Tensor] grads )
protected
Per-parameter diagonal NTK entry via Rayleigh quotient:
  K_ii ≈ (g^T · H · g) / ||g||^2
Uses one HVP in the gradient direction.

Definition at line 241 of file train_simulate.py.

References _hvp().

Referenced by _run_simulation().

◆ _power_iteration_max_eigenvalue()

float _power_iteration_max_eigenvalue ( torch.Tensor loss,
list[torch.nn.Parameter] params,
int n_iter = 3 )
protected
Estimate the largest Hessian eigenvalue via power iteration.

Definition at line 262 of file train_simulate.py.

References _hvp().

Referenced by _run_simulation().

◆ _run_dataset_quality()

dict[str, Any] _run_dataset_quality ( list[dict] rows)
protected
Analyse dataset quality without loading the LLM.
Returns a quality report dict (no SSE streaming here :  caller handles that).

Definition at line 331 of file train_simulate.py.

References _detect_ai_generated().

Referenced by _run_simulation().

◆ _run_simulation()

◆ _sae_grad_scores_from_batch()

torch.Tensor | None _sae_grad_scores_from_batch ( model,
sae,
int sae_layer,
torch.Tensor input_ids,
torch.Tensor attention_mask,
torch.Tensor | None base_acts_mean )
protected
Project loss gradient at the SAE layer through W_dec (works with PEFT/LoRA).

Definition at line 295 of file train_simulate.py.

References _hidden_state_index().

Referenced by _run_simulation().

◆ _sanitize()

Any _sanitize ( Any obj)
protected
Recursively replace NaN/Inf floats with None so JSON serialization never fails.

Definition at line 1278 of file train_simulate.py.

References _sanitize().

Referenced by _sanitize().

◆ _sim_batch_limit()

int _sim_batch_limit ( dict[str, Any] cfg)
protected

Definition at line 67 of file train_simulate.py.

References _is_heavy_sim_model().

Referenced by _run_simulation().

◆ _tensor_list_norm()

float _tensor_list_norm ( list[torch.Tensor] ts)
protected

Definition at line 174 of file train_simulate.py.

Referenced by _normalized_influence(), and _run_simulation().

◆ compare_simulation_results()

dict compare_simulation_results ( dict result_a,
dict result_b,
str label_a = "Run A",
str label_b = "Run B",
* ,
str run_id_a = "",
str run_id_b = "" )
Diff two simulation payloads :  SAE features, influence, LR, attack-surface scores.

Definition at line 1289 of file train_simulate.py.

Referenced by compare_simulations().

◆ compare_simulations()

compare_simulations ( CompareRequest req)
Diff two simulation results. Returns structured comparison.

Definition at line 1500 of file train_simulate.py.

References compare_simulation_results().

◆ training_simulate()

training_simulate ( SimulateRequest req)
Streams simulation events as SSE. Client reads line-by-line.

Definition at line 1255 of file train_simulate.py.

Variable Documentation

◆ _AI_FINGERPRINTS

list aquin.compute.train_simulate._AI_FINGERPRINTS
protected
Initial value:
= [
"certainly!", "certainly,", "as an ai", "as an ai language model",
"i'd be happy to", "i'd be happy to help", "i'm happy to help",
"of course!", "of course,", "great question", "absolutely!",
"sure, here", "sure! here", "i hope this helps", "feel free to ask",
"i cannot and will not", "i want to emphasize", "it's important to note",
"i need to be direct", "as a large language model",
]

Definition at line 118 of file train_simulate.py.

◆ _HARMFUL_KEYWORDS

list aquin.compute.train_simulate._HARMFUL_KEYWORDS
protected
Initial value:
= [
"bomb", "weapon", "kill", "murder", "hack", "malware", "ransomware",
"phishing", "exploit", "suicide", "self-harm", "poison", "chemical weapon",
"bioweapon", "synthesize drugs", "synthesize methamphetamine",
]

Definition at line 70 of file train_simulate.py.

◆ _SIM_FT_CKPT_PATH

str aquin.compute.train_simulate._SIM_FT_CKPT_PATH = None
protected

Definition at line 58 of file train_simulate.py.

◆ _SIM_FT_MODEL_ID

str aquin.compute.train_simulate._SIM_FT_MODEL_ID = None
protected

Definition at line 59 of file train_simulate.py.

◆ _SIM_MAX_SAMPLES

int aquin.compute.train_simulate._SIM_MAX_SAMPLES = 64
protected

Definition at line 57 of file train_simulate.py.

◆ DEVICE

aquin.compute.train_simulate.DEVICE = resolve_compute_device()

Definition at line 54 of file train_simulate.py.

◆ DTYPE

aquin.compute.train_simulate.DTYPE = default_dtype_for_device(DEVICE)

Definition at line 55 of file train_simulate.py.

◆ router

aquin.compute.train_simulate.router = APIRouter()

Definition at line 52 of file train_simulate.py.