Upload folder using huggingface_hub
Browse files- README.md +74 -82
- config.json +9 -24
- load_model.py +42 -0
- model.safetensors +1 -1
- pytorch_model.bin +3 -0
README.md
CHANGED
|
@@ -1,118 +1,110 @@
|
|
| 1 |
---
|
| 2 |
license: apache-2.0
|
| 3 |
-
library_name:
|
| 4 |
tags:
|
|
|
|
| 5 |
- moe
|
| 6 |
-
-
|
| 7 |
-
-
|
|
|
|
| 8 |
- compression
|
| 9 |
-
-
|
|
|
|
| 10 |
pipeline_tag: text-classification
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
-
# Hydra - M2M Protocol
|
| 14 |
-
|
| 15 |
-
A Mixture-of-Experts classifier for LLM API optimization.
|
| 16 |
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
-
|
| 24 |
-
-
|
| 25 |
-
- `BROTLI` - Byte compression (long/repetitive content)
|
| 26 |
-
- `ZLIB` - Fallback compression
|
| 27 |
|
| 28 |
-
|
| 29 |
-
Detects malicious inputs:
|
| 30 |
-
- `SAFE` - Normal request, allow
|
| 31 |
-
- `UNSAFE` - Prompt injection/jailbreak, block
|
| 32 |
-
|
| 33 |
-
## Model Architecture
|
| 34 |
|
| 35 |
| Property | Value |
|
| 36 |
|----------|-------|
|
| 37 |
-
|
|
| 38 |
-
|
|
| 39 |
| Hidden Size | 192 |
|
| 40 |
| Layers | 4 |
|
| 41 |
-
| Experts | 4
|
| 42 |
-
|
|
| 43 |
-
| Task Heads | Compression (4-class), Security (2-class) |
|
| 44 |
-
|
| 45 |
-
### Expert Architecture
|
| 46 |
-
|
| 47 |
-
Experts are **heterogeneous** (different depths and widths):
|
| 48 |
-
- Experts 0, 3: 2-layer MLP (192 → 384 → 192)
|
| 49 |
-
- Expert 1: 2-layer MLP, wider (192 → 768 → 192)
|
| 50 |
-
- Expert 2: 3-layer MLP (192 → 384 → 384 → 192)
|
| 51 |
-
|
| 52 |
-
## Usage with M2M Protocol (Rust)
|
| 53 |
-
|
| 54 |
-
```bash
|
| 55 |
-
# Install
|
| 56 |
-
cargo add m2m
|
| 57 |
-
|
| 58 |
-
# Download model
|
| 59 |
-
make model-download
|
| 60 |
-
# Or: huggingface-cli download infernet/hydra --local-dir ./models/hydra
|
| 61 |
-
```
|
| 62 |
|
| 63 |
-
|
| 64 |
-
use m2m::inference::HydraModel;
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
let security = model.predict_security(content)?;
|
| 75 |
-
if !security.safe {
|
| 76 |
-
println!("Threat: {:?}", security.threat_type);
|
| 77 |
-
}
|
| 78 |
-
```
|
| 79 |
-
|
| 80 |
-
## Usage with Python
|
| 81 |
|
| 82 |
```python
|
|
|
|
| 83 |
from safetensors.torch import load_file
|
| 84 |
-
from huggingface_hub import hf_hub_download
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
print(f"{name}: {list(tensor.shape)}")
|
| 93 |
```
|
| 94 |
|
| 95 |
-
##
|
| 96 |
|
| 97 |
-
|
| 98 |
-
-
|
| 99 |
-
- `layers.{0-3}.gate.weight`: [4, 192] - Expert router
|
| 100 |
-
- `layers.{0-3}.experts.{0-3}.net.*.weight` - Expert MLP layers
|
| 101 |
-
- `norm.weight/bias`: [192] - Final LayerNorm
|
| 102 |
-
- `compression_head.weight`: [4, 192] - Compression classifier
|
| 103 |
-
- `security_head.weight`: [2, 192] - Security classifier
|
| 104 |
|
| 105 |
-
##
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
##
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
## License
|
| 118 |
|
|
|
|
| 1 |
---
|
| 2 |
license: apache-2.0
|
| 3 |
+
library_name: transformers
|
| 4 |
tags:
|
| 5 |
+
- bitnet
|
| 6 |
- moe
|
| 7 |
+
- mixture-of-experts
|
| 8 |
+
- 1-bit
|
| 9 |
+
- quantized
|
| 10 |
- compression
|
| 11 |
+
- security
|
| 12 |
+
- m2m-protocol
|
| 13 |
pipeline_tag: text-classification
|
| 14 |
+
datasets:
|
| 15 |
+
- custom
|
| 16 |
+
language:
|
| 17 |
+
- en
|
| 18 |
---
|
| 19 |
|
| 20 |
+
# Hydra BitNet - M2M Protocol SLM
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
A 1.58-bit quantized Mixture-of-Experts model for LLM API optimization.
|
| 23 |
|
| 24 |
+
## Model Description
|
| 25 |
|
| 26 |
+
Hydra is an ultra-compact neural network designed for the M2M Protocol. It uses:
|
| 27 |
+
- **BitNet 1.58-bit quantization**: Weights are ternary {-1, 0, +1}
|
| 28 |
+
- **Mixture-of-Experts**: 4 specialized experts with top-2 routing
|
| 29 |
+
- **Task-specific heads**: Compression routing and security detection
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
## Model Details
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
| Property | Value |
|
| 34 |
|----------|-------|
|
| 35 |
+
| Parameters | ~9.7M |
|
| 36 |
+
| Model Size | ~3.7 MB (1.58-bit) |
|
| 37 |
| Hidden Size | 192 |
|
| 38 |
| Layers | 4 |
|
| 39 |
+
| Experts | 4 |
|
| 40 |
+
| Vocab Size | 32000 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
## Performance
|
|
|
|
| 43 |
|
| 44 |
+
### Compression Routing
|
| 45 |
+
- **Task**: Predict optimal compression algorithm (NONE, BPE, BROTLI, ZLIB)
|
| 46 |
+
- **Accuracy**: 99.4%
|
| 47 |
+
- **Latency**: <5ms on GPU
|
| 48 |
|
| 49 |
+
### Security Detection
|
| 50 |
+
- **Task**: Detect prompt injection and jailbreak attempts
|
| 51 |
+
- **Accuracy**: 96.2%
|
| 52 |
+
- **Latency**: <5ms on GPU
|
| 53 |
|
| 54 |
+
## Usage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
```python
|
| 57 |
+
import torch
|
| 58 |
from safetensors.torch import load_file
|
|
|
|
| 59 |
|
| 60 |
+
# Load model
|
| 61 |
+
weights = load_file("model.safetensors")
|
| 62 |
+
|
| 63 |
+
# Or use with the m2m-protocol package
|
| 64 |
+
from m2m_protocol import M2MClient
|
| 65 |
|
| 66 |
+
client = M2MClient(target_model="gpt-4")
|
| 67 |
+
result = client.process(your_message)
|
|
|
|
| 68 |
```
|
| 69 |
|
| 70 |
+
## Training
|
| 71 |
|
| 72 |
+
- **Compression Expert**: Trained with DPO on 100K message pairs
|
| 73 |
+
- **Security Expert**: Fine-tuned on 60K security samples (prompt injection, jailbreak, safe)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
## Architecture
|
| 76 |
|
| 77 |
+
```
|
| 78 |
+
HydraBitNet(
|
| 79 |
+
(embeddings): Embedding(256, 256)
|
| 80 |
+
(encoder): ModuleList(
|
| 81 |
+
(0-5): 6 x TaskSpecializedMoELayer(
|
| 82 |
+
(gate): Linear(256, 4)
|
| 83 |
+
(experts): ModuleList(
|
| 84 |
+
(0): CompressionExpert
|
| 85 |
+
(1): SecurityExpert
|
| 86 |
+
(2): SemanticExpert
|
| 87 |
+
(3): GeneralExpert
|
| 88 |
+
)
|
| 89 |
+
)
|
| 90 |
+
)
|
| 91 |
+
(classifier): ModuleDict(
|
| 92 |
+
(compression): BitLinear(256, 4)
|
| 93 |
+
(security): BitLinear(256, 2)
|
| 94 |
+
)
|
| 95 |
+
)
|
| 96 |
+
```
|
| 97 |
|
| 98 |
+
## Citation
|
| 99 |
|
| 100 |
+
```bibtex
|
| 101 |
+
@software{hydra_bitnet,
|
| 102 |
+
title = {Hydra BitNet: Ultra-Compact MoE for M2M Protocol},
|
| 103 |
+
author = {M2M Protocol Team},
|
| 104 |
+
year = {2026},
|
| 105 |
+
url = {https://github.com/infernet-org/m2m-protocol}
|
| 106 |
+
}
|
| 107 |
+
```
|
| 108 |
|
| 109 |
## License
|
| 110 |
|
config.json
CHANGED
|
@@ -1,31 +1,16 @@
|
|
| 1 |
{
|
| 2 |
-
"model_type": "hydra-
|
| 3 |
-
"architectures": [
|
| 4 |
-
"HydraMoEForSequenceClassification"
|
| 5 |
-
],
|
| 6 |
"vocab_size": 32000,
|
| 7 |
"hidden_size": 192,
|
| 8 |
"num_hidden_layers": 4,
|
| 9 |
"num_experts": 4,
|
| 10 |
"top_k_experts": 2,
|
| 11 |
-
"
|
| 12 |
-
"
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
"ZLIB"
|
| 20 |
-
]
|
| 21 |
-
},
|
| 22 |
-
"security": {
|
| 23 |
-
"num_labels": 2,
|
| 24 |
-
"labels": [
|
| 25 |
-
"SAFE",
|
| 26 |
-
"UNSAFE"
|
| 27 |
-
]
|
| 28 |
-
}
|
| 29 |
-
},
|
| 30 |
-
"_note": "Architecture derived from actual model.safetensors inspection"
|
| 31 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"model_type": "hydra-bitnet",
|
|
|
|
|
|
|
|
|
|
| 3 |
"vocab_size": 32000,
|
| 4 |
"hidden_size": 192,
|
| 5 |
"num_hidden_layers": 4,
|
| 6 |
"num_experts": 4,
|
| 7 |
"top_k_experts": 2,
|
| 8 |
+
"num_compression_classes": 4,
|
| 9 |
+
"num_security_classes": 2,
|
| 10 |
+
"max_position_embeddings": 512,
|
| 11 |
+
"quantization_bits": 1.58,
|
| 12 |
+
"architectures": [
|
| 13 |
+
"HydraBitNetForSequenceClassification"
|
| 14 |
+
],
|
| 15 |
+
"torch_dtype": "float32"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
}
|
load_model.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Load Hydra BitNet model."""
|
| 2 |
+
import torch
|
| 3 |
+
from safetensors.torch import load_file
|
| 4 |
+
|
| 5 |
+
def load_hydra(model_path: str, device: str = "cpu"):
|
| 6 |
+
"""Load Hydra model from HuggingFace format."""
|
| 7 |
+
import sys
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
# Add aisim to path if needed
|
| 11 |
+
aisim_path = Path(__file__).parent.parent / "aisim"
|
| 12 |
+
if aisim_path.exists():
|
| 13 |
+
sys.path.insert(0, str(aisim_path))
|
| 14 |
+
|
| 15 |
+
from bitnet_moe import M2MSentinel
|
| 16 |
+
import json
|
| 17 |
+
|
| 18 |
+
# Load config
|
| 19 |
+
with open(f"{model_path}/config.json") as f:
|
| 20 |
+
config = json.load(f)
|
| 21 |
+
|
| 22 |
+
# Create model
|
| 23 |
+
model = M2MSentinel(
|
| 24 |
+
vocab_size=config["vocab_size"],
|
| 25 |
+
dim=config["hidden_size"],
|
| 26 |
+
depth=config["num_hidden_layers"],
|
| 27 |
+
experts=config["num_experts"],
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Load weights
|
| 31 |
+
weights = load_file(f"{model_path}/model.safetensors")
|
| 32 |
+
model.load_state_dict(weights)
|
| 33 |
+
model = model.to(device)
|
| 34 |
+
model.eval()
|
| 35 |
+
|
| 36 |
+
return model, config
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
import sys
|
| 40 |
+
model_path = sys.argv[1] if len(sys.argv) > 1 else "."
|
| 41 |
+
model, config = load_hydra(model_path)
|
| 42 |
+
print(f"Loaded model: {config}")
|
model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 38902648
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ad48d0d8972f925560c81f3685692cd661e501699f41c84a33aa7885f19d3b13
|
| 3 |
size 38902648
|
pytorch_model.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:33f5841c410f631810b4a69cec4f62fa117641f7b780e08252bf17284505da8a
|
| 3 |
+
size 38918941
|