Downloads · 30 days
0
reneeice/ood-qwen3-sn32-cc
ood-qwen3-sn32-cc is a machine learning model from reneeice. Use it for the machine learning task on the model card, and read the license before you ship it in a product. It is set up for peft. The card lists the license as apache-2.0.
DeepSVDD OOD detector for SN32 subnet. LoRA adapter for Qwen3-1.7B + projection head.
Downloads · 30 days
0
Access
Public
Updated Jun 29, 2026
Repo size
6.4 GB
Likes
0
Public
Click a slice to open those files.
.pt5.5 GB · 88%
From the Hugging Face model README
DeepSVDD OOD detector for SN32 subnet. LoRA adapter for Qwen3-1.7B + projection head.
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import AutoTokenizer, AutoModel
from peft import PeftModel, LoraConfig
device = "cuda:0"
# Load base model
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-1.7B-Base")
backbone = AutoModel.from_pretrained("Qwen/Qwen3-1.7B-Base", torch_dtype=torch.bfloat16)
# Load LoRA adapter
model = PeftModel.from_pretrained(backbone, "reneeice/ood-qwen3-sn32")
model = model.merge_and_unload() # optional: merge for faster inference
model.eval().to(device)
# Load OOD head
import requests
url = "https://huggingface.co/reneeice/ood-qwen3-sn32/resolve/main/ood_qwen3.pt"
ckpt = torch.hub.load_state_dict_from_url(url, map_location="cpu")
# Or download and load locally:
# ckpt = torch.load("ood_qwen3.pt", map_location="cpu")
hidden = backbone.config.hidden_size
proj = nn.Sequential(
nn.LayerNorm(hidden, dtype=torch.float32),
nn.Linear(hidden, 256, bias=False, dtype=torch.float32),
)
proj.load_state_dict(ckpt["proj"])
proj.eval().to(device)
center = ckpt["center"].to(device)
@torch.inference_mode()
def predict(texts, batch_size=8, max_length=512):
probs = []
for i in range(0, len(texts), batch_size):
batch = texts[i:i+batch_size]
enc = tokenizer(batch, padding=True, truncation=True,
max_length=max_length, return_tensors="pt")
ids = enc["input_ids"].to(device)
am = enc["attention_mask"].to(device)
out = model(input_ids=ids, attention_mask=am)
h = out.last_hidden_state
mask = am.unsqueeze(-1).to(h.dtype)
pooled = (h * mask).sum(1) / mask.sum(1).clamp(min=1)
z = proj(pooled.float())
z = F.normalize(z, dim=-1)
dist = ((z - center) ** 2).sum(-1)
tau = 0.933 # calibration factor
p = torch.clamp(dist / tau, 0.0, 1.0)
probs.extend(p.cpu().numpy().tolist())
return probs
# Example
texts = ["This is human text.", "This is AI generated text with typical patterns."]
scores = predict(texts)
for t, s in zip(texts, scores):
print(f"P(AI)={s:.4f}: {t}")