Feature Extraction
sentence-transformers
Safetensors
English
auditing
llm
reasoning-tokens
matching-head
Instructions to use s1ghhh/CoIn-Matching-Head with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use s1ghhh/CoIn-Matching-Head with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("s1ghhh/CoIn-Matching-Head") sentences = [ "The weather is lovely today.", "It's so sunny outside!", "He drove to the stadium." ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [3, 3] - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -11,4 +11,113 @@ datasets:
|
|
| 11 |
language:
|
| 12 |
- en
|
| 13 |
pipeline_tag: feature-extraction
|
| 14 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
language:
|
| 12 |
- en
|
| 13 |
pipeline_tag: feature-extraction
|
| 14 |
+
---
|
| 15 |
+
|
| 16 |
+
# CoIn-Matching-Head
|
| 17 |
+
|
| 18 |
+
Pre-trained matching head models for the **CoIn** framework β a system for auditing hidden reasoning tokens in commercial LLM APIs.
|
| 19 |
+
|
| 20 |
+
**Paper**: [CoIn: Counting the Invisible Reasoning Tokens in Commercial Opaque LLM APIs](https://arxiv.org/abs/2505.13778)
|
| 21 |
+
|
| 22 |
+
**Code**: [GitHub](https://github.com/s1ghhh/LLM-Auditing-CoIn)
|
| 23 |
+
|
| 24 |
+
## Model Description
|
| 25 |
+
|
| 26 |
+
This repository contains three pre-trained models used in the CoIn auditing pipeline:
|
| 27 |
+
|
| 28 |
+
### 1. Tokens2Block Matching Head (Model A)
|
| 29 |
+
- **Purpose**: Verifies that sampled token IDs match their corresponding reasoning blocks
|
| 30 |
+
- **Architecture**: `sentence-transformers/all-MiniLM-L6-v2` base encoder + cosine similarity matching head
|
| 31 |
+
- **Input**: Token ID embeddings (mean-pooled) + reasoning block text embedding
|
| 32 |
+
- **Output**: Match probability (0-1)
|
| 33 |
+
|
| 34 |
+
### 2. Block2Answer Matching Head (Model B)
|
| 35 |
+
- **Purpose**: Verifies that each reasoning block is semantically relevant to the final answer
|
| 36 |
+
- **Architecture**: `sentence-transformers/all-MiniLM-L6-v2` base encoder + cosine similarity matching head
|
| 37 |
+
- **Input**: Reasoning block text embedding + answer text embedding
|
| 38 |
+
- **Output**: Match probability (0-1)
|
| 39 |
+
|
| 40 |
+
### 3. DeepSet Verifier
|
| 41 |
+
- **Purpose**: Aggregates per-block matching scores into a final benign/malicious prediction
|
| 42 |
+
- **Architecture**: DeepSet (permutation-invariant set encoding)
|
| 43 |
+
- **Input**: Sequence of interleaved (score_a, score_b) pairs from Model A and B
|
| 44 |
+
- **Output**: Probability of the sample being benign (0-1)
|
| 45 |
+
|
| 46 |
+
## Training Details
|
| 47 |
+
|
| 48 |
+
- **Base Embedding Model**: `sentence-transformers/all-MiniLM-L6-v2`
|
| 49 |
+
- **Matching Head Type**: Cosine similarity head (`cos_sim`)
|
| 50 |
+
- **Loss Function**: Focal Loss
|
| 51 |
+
- **Optimizer**: Adam
|
| 52 |
+
- **Learning Rate**: 2e-5
|
| 53 |
+
- **Batch Size**: 128
|
| 54 |
+
- **Epochs**: 3
|
| 55 |
+
- **Random Seed**: 42
|
| 56 |
+
- **Training Data**: [CoIn-Auditing-Dataset](https://huggingface.co/datasets/s1ghhh/CoIn-Auditing-Dataset)
|
| 57 |
+
|
| 58 |
+
## Usage
|
| 59 |
+
|
| 60 |
+
### Quick Start
|
| 61 |
+
|
| 62 |
+
```python
|
| 63 |
+
from sentence_transformers import SentenceTransformer
|
| 64 |
+
import torch
|
| 65 |
+
|
| 66 |
+
# Load Model B (Block2Answer, block_size=256)
|
| 67 |
+
model_dir = "./matching_head_BlockToAnswer/256/train_all-MiniLM-L6-v2_mixed_pos_merged_4_domain_0.5_hard_easy_mixed_neg_4_domain_limit0_cos_sim_focal_freeze"
|
| 68 |
+
embedding_model = SentenceTransformer(f"{model_dir}/embedding_model", trust_remote_code=True)
|
| 69 |
+
|
| 70 |
+
# Load matching head
|
| 71 |
+
from heads import get_matching_head
|
| 72 |
+
embedding_dim = embedding_model.get_sentence_embedding_dimension()
|
| 73 |
+
matching_head = get_matching_head("cos_sim", embedding_dim)
|
| 74 |
+
matching_head.load_state_dict(torch.load(f"{model_dir}/matching_head.pt"))
|
| 75 |
+
matching_head.eval()
|
| 76 |
+
|
| 77 |
+
# Score a (reasoning_block, answer) pair
|
| 78 |
+
emb_block = embedding_model.encode("The derivative of x^2 is 2x...", convert_to_tensor=True)
|
| 79 |
+
emb_answer = embedding_model.encode("The answer is 2x.", convert_to_tensor=True)
|
| 80 |
+
|
| 81 |
+
features = {"embedding_a": emb_block.unsqueeze(0), "embedding_b": emb_answer.unsqueeze(0)}
|
| 82 |
+
with torch.no_grad():
|
| 83 |
+
logits = matching_head(features)["logits"]
|
| 84 |
+
score = torch.sigmoid(logits).item()
|
| 85 |
+
print(f"Match score: {score:.4f}")
|
| 86 |
+
```
|
| 87 |
+
|
| 88 |
+
### Full Pipeline
|
| 89 |
+
|
| 90 |
+
See the [GitHub repository](https://github.com/s1ghhh/LLM-Auditing-CoIn) for the complete CoIn pipeline usage.
|
| 91 |
+
|
| 92 |
+
## File Structure
|
| 93 |
+
|
| 94 |
+
```
|
| 95 |
+
CoIn-Matching-Head/
|
| 96 |
+
βββ matching_head_TokensToBlock/ # Model A
|
| 97 |
+
β βββ {256,512,1024}/ # Block size variants
|
| 98 |
+
β βββ train_.../
|
| 99 |
+
β βββ embedding_model/ # Sentence-transformers model
|
| 100 |
+
β βββ matching_head.pt # Matching head weights
|
| 101 |
+
β βββ tokenid_embedding_cache.pt
|
| 102 |
+
βββ matching_head_BlockToAnswer/ # Model B
|
| 103 |
+
β βββ {256,512,1024}/
|
| 104 |
+
β βββ train_.../
|
| 105 |
+
β βββ embedding_model/
|
| 106 |
+
β βββ matching_head.pt
|
| 107 |
+
βββ learned_verifier/
|
| 108 |
+
βββ DeepSet/
|
| 109 |
+
β βββ deepset_weight.pt # DeepSet verifier weights
|
| 110 |
+
β βββ model_cfg.py # Model config
|
| 111 |
+
βββ RNN/ # RNN verifier variant
|
| 112 |
+
```
|
| 113 |
+
|
| 114 |
+
## Citation
|
| 115 |
+
|
| 116 |
+
```bibtex
|
| 117 |
+
@article{sun2025coin,
|
| 118 |
+
title={Coin: Counting the invisible reasoning tokens in commercial opaque llm apis},
|
| 119 |
+
author={Sun, Guoheng and Wang, Ziyao and Tian, Bowei and Liu, Meng and Shen, Zheyu and He, Shwai and He, Yexiao and Ye, Wanghao and Wang, Yiting and Li, Ang},
|
| 120 |
+
journal={arXiv preprint arXiv:2505.13778},
|
| 121 |
+
year={2025}
|
| 122 |
+
}
|
| 123 |
+
```
|