Upload README.md with huggingface_hub
Browse files
README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
library_name: pytorch
|
| 4 |
+
tags:
|
| 5 |
+
- log-analysis
|
| 6 |
+
- anomaly-detection
|
| 7 |
+
- security
|
| 8 |
+
- transformer
|
| 9 |
+
- gpt
|
| 10 |
+
- language-model
|
| 11 |
+
- from-scratch
|
| 12 |
+
pipeline_tag: text-generation
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# LogSentry-LM
|
| 16 |
+
|
| 17 |
+
A small, from-scratch **GPT-style language model** trained on real server logs to
|
| 18 |
+
(1) autoregressively complete log lines and (2) **detect anomalous log lines** by
|
| 19 |
+
how "surprised" the model is by them (perplexity-based scoring).
|
| 20 |
+
|
| 21 |
+
It is a compact, educational decoder-only Transformer (~2M parameters) built with
|
| 22 |
+
plain PyTorch — no `transformers` modeling class — intended for learning and for
|
| 23 |
+
lightweight log anomaly detection.
|
| 24 |
+
|
| 25 |
+
## Model details
|
| 26 |
+
|
| 27 |
+
- **Architecture:** decoder-only Transformer (GPT-style), causal self-attention,
|
| 28 |
+
pre-norm residual blocks, learned token + positional embeddings.
|
| 29 |
+
- **Size:** `d_model=128`, `n_heads=4`, `n_layers=3`, `max_seq_len=128` (~2.03M params).
|
| 30 |
+
- **Tokenizer:** Byte-Pair Encoding (`tokenizers`), vocab ≈ 5.5k.
|
| 31 |
+
- **Training data:** combined **Loghub** samples — Linux + Apache + OpenSSH
|
| 32 |
+
(`*_2k.log`, ~6k lines total).
|
| 33 |
+
- **Objective:** next-token prediction (cross-entropy).
|
| 34 |
+
- **Framework:** PyTorch (trained on Apple Silicon MPS).
|
| 35 |
+
|
| 36 |
+
## Files in this repo
|
| 37 |
+
|
| 38 |
+
| File | Purpose |
|
| 39 |
+
| --- | --- |
|
| 40 |
+
| `logsentry_lm.pt` | Trained weights + anomaly threshold + config |
|
| 41 |
+
| `log_tokenizer.json` | BPE tokenizer |
|
| 42 |
+
| `architecture.py` | Transformer building blocks |
|
| 43 |
+
| `dataset.py` | Tokenizer loader + dataset |
|
| 44 |
+
| `train_and_generate.py` | Model class, training, generation, anomaly scoring |
|
| 45 |
+
| `detect_anomalies.py` | Standalone anomaly predictor |
|
| 46 |
+
|
| 47 |
+
## Usage
|
| 48 |
+
|
| 49 |
+
> ⚠️ This is a **custom architecture**, so `AutoModel.from_pretrained` does **not**
|
| 50 |
+
> work. Download the files and load manually:
|
| 51 |
+
|
| 52 |
+
```python
|
| 53 |
+
from huggingface_hub import hf_hub_download
|
| 54 |
+
import torch
|
| 55 |
+
from train_and_generate import LogSentryLM, score_line
|
| 56 |
+
from dataset import build_or_load_tokenizer
|
| 57 |
+
|
| 58 |
+
repo = "YOUR_USERNAME/LogSentry-LM"
|
| 59 |
+
ckpt_path = hf_hub_download(repo, "logsentry_lm.pt")
|
| 60 |
+
tok_path = hf_hub_download(repo, "log_tokenizer.json")
|
| 61 |
+
|
| 62 |
+
ckpt = torch.load(ckpt_path, map_location="cpu")
|
| 63 |
+
model = LogSentryLM(vocab_size=ckpt["vocab_size"], max_seq_len=ckpt["max_seq_len"])
|
| 64 |
+
model.load_state_dict(ckpt["model_state"]); model.eval()
|
| 65 |
+
|
| 66 |
+
tokenizer = build_or_load_tokenizer(None, tok_path) # loads the cached tokenizer
|
| 67 |
+
|
| 68 |
+
loss, ppl = score_line(model, tokenizer, "Failed password for root from 1.2.3.4 port 22 ssh2", "cpu")
|
| 69 |
+
print("anomaly" if loss > ckpt["threshold"] else "normal", f"(perplexity={ppl:.1f})")
|
| 70 |
+
```
|
| 71 |
+
|
| 72 |
+
## How anomaly detection works
|
| 73 |
+
|
| 74 |
+
The model learns what *normal* logs look like. A line it cannot predict scores a
|
| 75 |
+
high per-token loss (high perplexity) and is flagged as anomalous. The cutoff is
|
| 76 |
+
`threshold = mean + 2·std` of the training corpus line scores, stored in the
|
| 77 |
+
checkpoint. Reference behavior:
|
| 78 |
+
|
| 79 |
+
| Input | Perplexity | Verdict |
|
| 80 |
+
| --- | --- | --- |
|
| 81 |
+
| Normal Apache `[notice]` line | ~2 | normal |
|
| 82 |
+
| Real SSH login attempt | ~12–16 | borderline |
|
| 83 |
+
| Random gibberish / injected text | >100,000 | anomaly |
|
| 84 |
+
|
| 85 |
+
## Limitations
|
| 86 |
+
|
| 87 |
+
- Trained on a **small, mixed** corpus (three log sources). A single global
|
| 88 |
+
threshold is imperfect across sources; per-source models/thresholds work better.
|
| 89 |
+
- Not production hardened — intended for education and experimentation.
|
| 90 |
+
- Small context window (128 tokens) and small vocab; it models line-level, not
|
| 91 |
+
session-level, structure.
|
| 92 |
+
|
| 93 |
+
## Attribution & licensing
|
| 94 |
+
|
| 95 |
+
- Training data derived from the **[Loghub](https://github.com/logpai/loghub)**
|
| 96 |
+
collection (Linux, Apache, OpenSSH samples). Please cite Loghub and consult its
|
| 97 |
+
terms for any redistribution of the underlying logs.
|
| 98 |
+
- Model code released under the MIT license.
|
| 99 |
+
|
| 100 |
+
```bibtex
|
| 101 |
+
@inproceedings{zhu2023loghub,
|
| 102 |
+
title={Loghub: A Large Collection of System Log Datasets for AI-driven Log Analytics},
|
| 103 |
+
author={Zhu, Jieming and He, Shilin and He, Pinjia and Liu, Jinyang and Lyu, Michael R.},
|
| 104 |
+
booktitle={IEEE International Symposium on Software Reliability Engineering (ISSRE)},
|
| 105 |
+
year={2023}
|
| 106 |
+
}
|
| 107 |
+
```
|