Text Generation
PyTorch
Transformers
English
language-model
graph-neural-network
sparse-attention
adaptive-depth
temporal-decay
mesh-attention
efficient-transformer
novel-architecture
causal-lm
research
preprint
mesh-transformer
dynamic-graph
early-exit
per-token-routing
Eval Results (legacy)
Instructions to use vigneshwar234/TemporalMesh-Transformer with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use vigneshwar234/TemporalMesh-Transformer with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="vigneshwar234/TemporalMesh-Transformer")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("vigneshwar234/TemporalMesh-Transformer", dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use vigneshwar234/TemporalMesh-Transformer with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "vigneshwar234/TemporalMesh-Transformer" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "vigneshwar234/TemporalMesh-Transformer", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/vigneshwar234/TemporalMesh-Transformer
- SGLang
How to use vigneshwar234/TemporalMesh-Transformer with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "vigneshwar234/TemporalMesh-Transformer" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "vigneshwar234/TemporalMesh-Transformer", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "vigneshwar234/TemporalMesh-Transformer" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "vigneshwar234/TemporalMesh-Transformer", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use vigneshwar234/TemporalMesh-Transformer with Docker Model Runner:
docker model run hf.co/vigneshwar234/TemporalMesh-Transformer
Add source: tmt/model/exit_gate.py
Browse files- tmt/model/exit_gate.py +61 -0
tmt/model/exit_gate.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
exit_gate.py — ExitGate: per-token adaptive depth routing.
|
| 3 |
+
|
| 4 |
+
Novel vs standard: every token in a standard transformer passes through all N
|
| 5 |
+
layers unconditionally. ExitGate computes a confidence scalar after each layer
|
| 6 |
+
norm. If confidence > exit_threshold the token's representation is frozen and
|
| 7 |
+
it skips all remaining layers, halving average compute on easy tokens.
|
| 8 |
+
|
| 9 |
+
The auxiliary training loss encourages the gate to be decisive (push toward 0
|
| 10 |
+
or 1) without forcing early exits — the model learns when it is confident.
|
| 11 |
+
"""
|
| 12 |
+
from __future__ import annotations
|
| 13 |
+
|
| 14 |
+
from typing import Tuple
|
| 15 |
+
|
| 16 |
+
import torch
|
| 17 |
+
import torch.nn as nn
|
| 18 |
+
from torch import Tensor
|
| 19 |
+
|
| 20 |
+
from .config import TMTConfig
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class ExitGate(nn.Module):
|
| 24 |
+
"""Single linear → sigmoid confidence gate per token."""
|
| 25 |
+
|
| 26 |
+
def __init__(self, cfg: TMTConfig) -> None:
|
| 27 |
+
super().__init__()
|
| 28 |
+
self.threshold = cfg.exit_threshold
|
| 29 |
+
# Single scalar projection: d_model → 1
|
| 30 |
+
self.gate_proj = nn.Linear(cfg.d_model, 1)
|
| 31 |
+
nn.init.zeros_(self.gate_proj.weight)
|
| 32 |
+
nn.init.constant_(self.gate_proj.bias, -2.0) # start pessimistic
|
| 33 |
+
|
| 34 |
+
def forward(self, x: Tensor, exit_mask: Tensor) -> Tuple[Tensor, Tensor, Tensor]:
|
| 35 |
+
"""
|
| 36 |
+
Args:
|
| 37 |
+
x: (B, S, D) current token representations
|
| 38 |
+
exit_mask: (B, S) bool — True where token has already exited
|
| 39 |
+
|
| 40 |
+
Returns:
|
| 41 |
+
x: (B, S, D) unchanged (gating is applied in TMTLayer)
|
| 42 |
+
new_mask: (B, S) bool — updated exit mask
|
| 43 |
+
confidence: (B, S) float confidence scores for auxiliary loss
|
| 44 |
+
"""
|
| 45 |
+
confidence = torch.sigmoid(self.gate_proj(x)).squeeze(-1) # (B, S)
|
| 46 |
+
|
| 47 |
+
# New exits: not already exited AND confidence above threshold
|
| 48 |
+
newly_exited = (~exit_mask) & (confidence > self.threshold)
|
| 49 |
+
new_mask = exit_mask | newly_exited
|
| 50 |
+
return x, new_mask, confidence
|
| 51 |
+
|
| 52 |
+
def auxiliary_loss(self, confidence: Tensor) -> Tensor:
|
| 53 |
+
"""
|
| 54 |
+
Encourage decisive gates — push confidence toward 0 or 1.
|
| 55 |
+
Loss = -E[|conf - 0.5|] so the model is penalised for being uncertain.
|
| 56 |
+
"""
|
| 57 |
+
return -(confidence - 0.5).abs().mean()
|
| 58 |
+
|
| 59 |
+
def __repr__(self) -> str:
|
| 60 |
+
p = sum(p.numel() for p in self.parameters())
|
| 61 |
+
return f"ExitGate(threshold={self.threshold}, params={p:,})"
|