Differentiable Logic for Inference-Time Latent Correction in PyTorch
Project description
TorchVeritas ⚖️
Differentiable Logic for Inference-Time Latent Correction
"Stop trying to prompt-engineer your model not to lie. Constrain it mathematically."
💡 What is TorchVeritas?
TorchVeritas is a lightweight yet powerful PyTorch library that enforces logical consistency inside a neural network's hidden layers during inference.
Unlike standard "guardrails" that filter output after it is generated (which is slow and wasteful), TorchVeritas uses Test-Time Optimization (TTO) to apply "Logical Hooks." These hooks actively steer latent vectors toward a truthful manifold before the model makes a final decision.
Why does this exist?
- For Autonomous Agents: Prevent an agent from executing a "delete" command if specific safety flags are not present.
- For Fintech/Compliance: Ensure outputs strictly adhere to non-negotiable rules (e.g., "If Credit_Score < 600, Interest_Rate MUST be 0%").
- For Reliability: Move from "Probabilistic Safety" (it might work) to "Deterministic Safety" (it must work).
📦 Installation
pip install torchveritas
⚡ Quick Start
Here is how to turn a standard "hallucinating" model into a logically consistent one in 4 lines of code.
import torch
import torch.nn as nn
from torchveritas import attach_veritas, Logic
# 1. Your existing model (Standard PyTorch)
model = nn.Sequential(
nn.Linear(10, 10),
nn.ReLU(),
nn.Linear(10, 2) # Output: [Neuron A, Neuron B]
)
# 2. Define a Logical Rule
# Rule: "Neuron A and Neuron B cannot BOTH be active at the same time."
# (e.g., A="Turn Left", B="Turn Right")
rule = Logic.exclusion(idx_a=0, idx_b=1)
# 3. Attach the Safety Layer
# This injects the optimization loop into the '2' layer (the final Linear layer)
attach_veritas(model, layer_name='2', constraint_fn=rule)
# 4. Run Inference
# The library automatically pauses execution, corrects any logic violations
# in the latent vector, and resumes.
input_data = torch.randn(1, 10)
output = model(input_data)
print("Safe Output:", output)
🔧 How It Works
When you attach TorchVeritas to a layer, it registers a Forward Hook.
- Intercept: As the data flows through the network, the hook captures the latent tensor.
- Check: It evaluates your
constraint_fn. If the "Violation Energy" is 0, it does nothing. - Optimize: If there is a violation, it runs a micro-optimization loop (using SGD) on the latent tensor itself to minimize the violation energy.
- Release: The corrected tensor is passed to the next layer.
This happens in milliseconds and ensures the downstream layers never see "illegal" thoughts.
🛠 Advanced Usage: Custom Logic
You are not limited to pre-built logic. You can define complex, domain-specific laws using standard PyTorch operations.
def strict_banking_law(latents):
"""
Custom Law: If 'Loan_Status' (index 0) is < 0.5 (Rejected),
then 'Interest_Rate' (index 1) must be exactly 0.0.
"""
loan_status = torch.sigmoid(latents[:, 0])
interest_rate = latents[:, 1]
# Violation = (1 - Loan_Status) * Interest_Rate
# This value is high only if Loan is Rejected AND Rate is high.
violation = (1.0 - loan_status) * interest_rate
return violation.mean()
# Attach with custom tuning parameters
attach_veritas(
model,
layer_name='output_layer',
constraint_fn=strict_banking_law,
steps=20, # More steps for harder constraints
lr=0.1, # Learning rate for the correction
verbose=True # Print when corrections happen
)
🤝 Contributing
We welcome contributions! Whether it's adding new Logic primitives (like Logic.xor or Logic.implies) or optimizing the steering engine, please submit a Pull Request.
📄 License
MIT License. Free for research and commercial use.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torchveritas-0.1.0.tar.gz.
File metadata
- Download URL: torchveritas-0.1.0.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ac403aab53ce44c21cc1c27f397507fc969f02d7ca02fe4d63eca950d1bd59d
|
|
| MD5 |
ba1b3ad5513b70b81cc06a0bcd52d51c
|
|
| BLAKE2b-256 |
e7d9f9f23916aebf5ea5b9b59bdd2806b62c33f1eb01228f354cff0a45f072eb
|
File details
Details for the file torchveritas-0.1.0-py3-none-any.whl.
File metadata
- Download URL: torchveritas-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
722189a6be5211e4219b0eea317be59362a15755d5ef4551640638d0346be4bb
|
|
| MD5 |
7b34ac382ba5c2b74ee2cbf21db01554
|
|
| BLAKE2b-256 |
d6c8ec6c643acd15f4288e70e7f728de86994cc12638140beb95afe45ba9be9e
|