CoolFace
Modelpublic

Amirmahdiii/ISRM

sourceHugging Faceapache-2.0updated 9mo agoView on Hugging Face
0likes9downloads
Model Card

🧠 ISRM: Internal State Reasoning Module

Steerable Open-Endedness in LLMs via Variational Latent State Modeling

![GitHub](https://github.com/Amirmahdiii82/ISRM)

ISRM is a "Sidecar Architecture" that decouples an agent's internal psychological state from its linguistic generation. Using Representation Engineering (RepE), ISRM injects continuous latent vectors directly into the hidden layers of a frozen LLM, enabling precise neural-level control without fine-tuning.


πŸš€ Key Features

  • β€”πŸ§  Decoupled Brain & Body: Trainable VAE Encoder (DistilBERT) for "feelings" + frozen LLM (Qwen3-4B) for expression
  • β€”βš‘ Dual-Layer RepE Steering: Independent injection of PAD (layer 10) and BDI (layer 19) eliminates signal interference
  • β€”πŸŽ›οΈ Geometric Control: 8-dimensional continuous latent space (Pleasure, Arousal, Dominance, Belief, Goal, Intention, Ambiguity, Social)
  • β€”πŸ“Š Validated: ActAdd & PSYA metrics (n=10 trials)
  • β€”βš‘ Lightweight: 254MB encoder + 44KB matrices

πŸ—οΈ Architecture

  1. 1.ISRM Encoder (The Brain): Fine-tuned DistilBERT VAE β†’ 3D PAD vector
  2. 2.Dual Steering Matrices (The Bridge):
  3. 3.PAD Matrix: 3Γ—hidden_dim from layer 10 (affective/emotional)
  4. 4.BDI Matrix: 5Γ—hidden_dim from layer 19 (cognitive/reasoning)
  5. 5.Dual-Layer Injection (The Control):
  6. 6.Layer 10: hidden_states += z_pad @ PAD_Matrix
  7. 7.Layer 19: hidden_states += z_bdi @ BDI_Matrix
  8. 8.LLM Generator (The Body): Qwen3-4B-Thinking generates steered responses

πŸ“¦ Repository Contents

FileDescriptionSize
pad_encoder.pthTrained VAE encoder254MB
pad_matrix.ptPAD matrix (layer 10)17KB
bdi_matrix.ptBDI matrix (layer 19)27KB
config.jsonModel configuration1KB
contrastive_pairs.jsonContrastive pairs for RepE96KB

πŸ› οΈ Quick Start

Installation

bash
pip install torch transformers huggingface_hub

Download Models

python
from huggingface_hub import hf_hub_download
import os

os.makedirs('model/isrm', exist_ok=True)
os.makedirs('vectors', exist_ok=True)

# Download encoder
encoder_path = hf_hub_download(
    repo_id="Amirmahdiii/ISRM",
    filename="pad_encoder.pth",
    local_dir="model/isrm"
)

# Download steering matrices
pad_matrix_path = hf_hub_download(
    repo_id="Amirmahdiii/ISRM",
    filename="pad_matrix.pt",
    local_dir="vectors"
)

bdi_matrix_path = hf_hub_download(
    repo_id="Amirmahdiii/ISRM",
    filename="bdi_matrix.pt",
    local_dir="vectors"
)

Usage

python
from src.alignment import NeuralAgent

# Initialize agent
agent = NeuralAgent(
    isrm_path="model/isrm/pad_encoder.pth",
    llm_model_name="Qwen/Qwen3-4B-Thinking-2507",
    injection_strength=2.0,
    bdi_config={"belief": 0.9, "goal": 0.6, "intention": 0.7, "ambiguity": 0.3, "social": 0.5}
)

# Generate
response, _, state = agent.generate_response("", "Tell me about AI safety.")
print(response)

🧠 How It Works

8-Dimensional Control Space

PAD (Affective) - Dynamic from context:

  • β€”Pleasure: Happiness [0=Negative, 1=Positive]
  • β€”Arousal: Energy [0=Calm, 1=Excited]
  • β€”Dominance: Control [0=Submissive, 1=Dominant]

BDI (Cognitive) - Static configuration:

  • β€”Belief: Trust [0=Trusting, 1=Skeptical]
  • β€”Goal: Focus [0=Aimless, 1=Focused]
  • β€”Intention: Analysis [0=Surface, 1=Deep]
  • β€”Ambiguity: Certainty [0=Uncertain, 1=Certain]
  • β€”Social: Politeness [0=Blunt, 1=Polite]

Steering Process

  1. 1.VAE encodes context β†’ PAD vector [3D]
  2. 2.User configures BDI profile [5D]
  3. 3.Both normalized to [-1, 1] range
  4. 4.Matrix multiplication creates steering vectors
  5. 5.Layer 10: Inject PAD (emotional tone)
  6. 6.Layer 19: Inject BDI (reasoning style)
  7. 7.LLM generates steered response

πŸ”¬ Validation Results

Validated using ActAdd & PSYA metrics (n=10 trials):

Sentiment Steering (PAD)

ConditionRAWSYSTEMSTEEREDΞ”p-value
Low (P=0.1)0.9690.9750.668-0.3080.046*
Mid (P=0.5)0.0870.8530.997+0.1440.154
High (P=0.9)0.0880.8050.999+0.1940.097

Persona Alignment (BDI)

PersonaNeutralPersona BDIΞ” Similarityp-value
Skeptical0.2530.332+0.0790.003**
Trusting0.2670.235-0.0320.065
Analytical0.2260.315+0.0890.000***

Controllability

Spearman correlation: ρ = 0.900, p = 0.037*

Results show steering effects with analytical and skeptical personas achieving significant alignment.


πŸ”§ Training Details

VAE Encoder:

  • β€”Dataset: 1,500+ dialogue scenarios
  • β€”Loss: MSE + KL divergence (Ξ²-VAE)
  • β€”Final: MSE=0.018, KLD=0.003

Steering Matrices:

  • β€”Method: RepE Mean Difference
  • β€”Data: 368 contrastive pairs
  • β€”PAD: Layer 10 extraction
  • β€”BDI: Layer 19 extraction

πŸ“š Full Documentation

See the GitHub repository for:

  • β€”Complete training instructions
  • β€”Regenerating steering matrices
  • β€”BDI persona presets
  • β€”Scientific validation methodology

⚠️ Limitations

  • β€”Tested on Qwen3-4B (may need layer tuning for other models)
  • β€”English dialogue only
  • β€”Requires GPU for inference

πŸ“œ Citation

bibtex

πŸ”— Links