Skip to content

Commit 9cc3049

Browse files
committed
feat: add unified jailbreak classifier with LazyLock optimization and benchmarks
- Implement unified jailbreak model factory with auto-detection from config.json - Supports ModernBERT, DeBERTa V3, and Qwen3Guard models - Automatic architecture detection and model loading - Performance optimizations: - Use LazyLock for static default labels (zero-cost after init) - Use parking_lot::Mutex instead of std::sync::Mutex for Qwen3Guard - Lock-free classification for ModernBERT and DeBERTa (Arc-wrapped) - Early lock release in Qwen3Guard to minimize hold time - Add comprehensive Go benchmark suite: - Test all jailbreak models (ModernBERT, DeBERTa, Unified, Qwen3Guard) - Measure accuracy, confidence, and latency percentiles (p50/p95/p99) - Test both CPU and GPU performance - Benchmark results show DeBERTa V3 achieves 95% accuracy - Update FFI bindings: - Add init_unified_jailbreak_classifier and classify_unified_jailbreak_text - Update ClassificationResult to include label field - Rename 'class' to 'predicted_class' for consistency - Add HuggingFace model ID support in unified factory - Auto-fetch config.json from HuggingFace Hub - Support both local paths and HF model IDs - Add unit tests for unified jailbreak classifier - Tests in semantic-router_test.go, config_test.go, extproc_test.go - Fix test compilation errors with proper struct field usage - Update Go interfaces to use unified classifier by default - Deprecate useModernBERT flag in favor of auto-detection Signed-off-by: Yue Zhu <[email protected]>
1 parent c3ce62e commit 9cc3049

File tree

26 files changed

+3073
-56
lines changed

26 files changed

+3073
-56
lines changed

bench/.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Benchmark results
2+
results/*.txt
3+
results/*.prof
4+
results/*.out
5+
6+
# Test binaries
7+
*.test
8+
jailbreak_bench.test
9+
10+
# Go coverage files
11+
*.coverprofile
12+
coverage.html
13+
14+
# Temporary files
15+
*.tmp
16+
*.log
17+

bench/Makefile

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
.PHONY: all bench bench-quick bench-full bench-concurrent bench-memory bench-compare clean help
2+
3+
# Default target
4+
all: help
5+
6+
# Run quick benchmarks (shorter time)
7+
bench-quick:
8+
@echo "Running quick jailbreak benchmarks..."
9+
go test -bench=. -benchmem -benchtime=3s
10+
11+
# Run full benchmarks (longer time for more accuracy)
12+
bench-full:
13+
@echo "Running full jailbreak benchmarks..."
14+
@mkdir -p results
15+
go test -bench=. -benchmem -benchtime=10s | tee results/bench_$$(date +%Y%m%d_%H%M%S).txt
16+
17+
# Run only concurrent benchmarks
18+
bench-concurrent:
19+
@echo "Running concurrency benchmarks..."
20+
go test -bench=Concurrent -benchmem -benchtime=30s
21+
22+
# Run with memory profiling
23+
bench-memory:
24+
@echo "Running benchmarks with memory profiling..."
25+
@mkdir -p results
26+
go test -bench=. -benchmem -memprofile=results/mem.prof -cpuprofile=results/cpu.prof
27+
@echo "View memory profile: go tool pprof results/mem.prof"
28+
@echo "View CPU profile: go tool pprof results/cpu.prof"
29+
30+
# Compare with previous results (requires benchstat)
31+
bench-compare:
32+
@if ! command -v benchstat >/dev/null 2>&1; then \
33+
echo "Installing benchstat..."; \
34+
go install golang.org/x/perf/cmd/benchstat@latest; \
35+
fi
36+
@if [ -z "$$(ls -t results/bench_*.txt 2>/dev/null | head -2 | tail -1)" ]; then \
37+
echo "No previous results found. Run 'make bench-full' first."; \
38+
exit 1; \
39+
fi
40+
@echo "Comparing with previous results..."
41+
@OLD=$$(ls -t results/bench_*.txt | head -2 | tail -1); \
42+
NEW=$$(ls -t results/bench_*.txt | head -1); \
43+
echo "Old: $$OLD"; \
44+
echo "New: $$NEW"; \
45+
benchstat $$OLD $$NEW
46+
47+
# Run benchmarks for specific model
48+
bench-modernbert:
49+
@echo "Running ModernBERT benchmarks..."
50+
go test -bench=BenchmarkModernBert -benchmem
51+
52+
bench-deberta:
53+
@echo "Running DeBERTa benchmarks..."
54+
go test -bench=BenchmarkDeberta -benchmem
55+
56+
bench-unified:
57+
@echo "Running Unified classifier benchmarks..."
58+
go test -bench=BenchmarkUnified -benchmem
59+
60+
# Run only initialization benchmarks
61+
bench-init:
62+
@echo "Running initialization benchmarks..."
63+
go test -bench=BenchmarkInit -benchmem
64+
65+
# Run benchmarks with race detector (slower but checks for race conditions)
66+
bench-race:
67+
@echo "Running benchmarks with race detector..."
68+
go test -race -bench=Concurrent -benchtime=5s
69+
70+
# Clean results
71+
clean:
72+
@echo "Cleaning benchmark results..."
73+
rm -rf results/*.txt results/*.prof
74+
rm -f /tmp/jailbreak_bench.test
75+
76+
# Help target
77+
help:
78+
@echo "Jailbreak Classifier Benchmarks"
79+
@echo ""
80+
@echo "Available targets:"
81+
@echo " make bench-quick - Run quick benchmarks (3s each)"
82+
@echo " make bench-full - Run full benchmarks (10s each, save results)"
83+
@echo " make bench-concurrent - Run concurrency benchmarks only"
84+
@echo " make bench-memory - Run with CPU and memory profiling"
85+
@echo " make bench-compare - Compare with previous results"
86+
@echo ""
87+
@echo "Model-specific benchmarks:"
88+
@echo " make bench-modernbert - Benchmark ModernBERT only"
89+
@echo " make bench-deberta - Benchmark DeBERTa only"
90+
@echo " make bench-unified - Benchmark Unified classifier only"
91+
@echo ""
92+
@echo "Other targets:"
93+
@echo " make bench-init - Benchmark initialization only"
94+
@echo " make bench-race - Run with race detector"
95+
@echo " make clean - Clean benchmark results"
96+
@echo " make help - Show this help message"
97+

0 commit comments

Comments
 (0)