Text Generation
Transformers
English
qtensorformer
tensor-networks
model-compression
adaptive-computation
kv-cache-compression
hardware-aware
energy-aware
quantum-machine-learning
green-ai
Instructions to use Premchan369/Q-TensorFormer with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Premchan369/Q-TensorFormer with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="Premchan369/Q-TensorFormer")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("Premchan369/Q-TensorFormer", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Premchan369/Q-TensorFormer with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Premchan369/Q-TensorFormer" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Premchan369/Q-TensorFormer", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/Premchan369/Q-TensorFormer
- SGLang
How to use Premchan369/Q-TensorFormer 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 "Premchan369/Q-TensorFormer" \ --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": "Premchan369/Q-TensorFormer", "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 "Premchan369/Q-TensorFormer" \ --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": "Premchan369/Q-TensorFormer", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use Premchan369/Q-TensorFormer with Docker Model Runner:
docker model run hf.co/Premchan369/Q-TensorFormer
Premchandyadav369
Transform Q-TensorFormer into an Information-Value Adaptive Resource Allocation Architecture
eaeea8f Download tests/test_kv_cache.py from Premchan369/Q-TensorFormer: direct link, hf CLI and curl.
- Browser
- Download file 2.78 kB
-
https://huggingface.co/Premchan369/Q-TensorFormer/resolve/main/tests/test_kv_cache.py
- Command line
-
hf download hf://Premchan369/Q-TensorFormer/tests/test_kv_cache.py
-
curl -L -o test_kv_cache.py https://huggingface.co/Premchan369/Q-TensorFormer/resolve/main/tests/test_kv_cache.py
2.78 kB
| """ | |
| Tests for Adaptive KV Cache Module. | |
| """ | |
| import sys | |
| import os | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| import torch | |
| import pytest | |
| from src.kv_cache import AdaptiveKVCache, KVPrecision, QuantizedKVTensor | |
| def test_quantized_kv_tensor(): | |
| tensor = torch.randn(2, 4, 8, 32) | |
| # FP16 | |
| q_fp16 = QuantizedKVTensor(tensor, KVPrecision.FP16) | |
| rec_fp16 = q_fp16.dequantize() | |
| assert torch.allclose(tensor, rec_fp16, atol=1e-2) | |
| # INT8 | |
| q_int8 = QuantizedKVTensor(tensor, KVPrecision.INT8) | |
| rec_int8 = q_int8.dequantize() | |
| cos_sim_int8 = torch.cosine_similarity(tensor.flatten(), rec_int8.flatten(), dim=0) | |
| assert cos_sim_int8 > 0.99, f"INT8 cosine similarity {cos_sim_int8} too low" | |
| # INT4 | |
| q_int4 = QuantizedKVTensor(tensor, KVPrecision.INT4) | |
| rec_int4 = q_int4.dequantize() | |
| cos_sim_int4 = torch.cosine_similarity(tensor.flatten(), rec_int4.flatten(), dim=0) | |
| assert cos_sim_int4 > 0.90, f"INT4 cosine similarity {cos_sim_int4} too low" | |
| print("✓ test_quantized_kv_tensor passed") | |
| def test_adaptive_kv_cache_append_and_evict(): | |
| cache = AdaptiveKVCache(max_capacity=16, default_precision=KVPrecision.FP16, window_size=4) | |
| B, H, D = 1, 2, 16 | |
| # Append 10 tokens | |
| k1 = torch.randn(B, H, 10, D) | |
| v1 = torch.randn(B, H, 10, D) | |
| out_k1, out_v1 = cache.update(k1, v1) | |
| assert cache.seq_len == 10 | |
| assert out_k1.shape[-2] == 10 | |
| # Append 10 more tokens (exceeds max_capacity 16 -> should trigger eviction) | |
| k2 = torch.randn(B, H, 10, D) | |
| v2 = torch.randn(B, H, 10, D) | |
| out_k2, out_v2 = cache.update(k2, v2) | |
| assert cache.seq_len == 16, f"Expected cache seq_len 16, got {cache.seq_len}" | |
| assert cache.evicted_tokens_count == 4 | |
| assert cache.current_mb > 0.0 | |
| print("✓ test_adaptive_kv_cache_append_and_evict passed") | |
| def test_adaptive_kv_cache_precision_switch(): | |
| cache = AdaptiveKVCache(max_capacity=32, default_precision=KVPrecision.FP16) | |
| k = torch.randn(1, 2, 8, 16) | |
| v = torch.randn(1, 2, 8, 16) | |
| cache.update(k, v) | |
| bytes_fp16 = cache.current_bytes | |
| # Switch to INT8 | |
| cache.set_precision(KVPrecision.INT8) | |
| bytes_int8 = cache.current_bytes | |
| assert bytes_int8 < bytes_fp16, f"INT8 ({bytes_int8}) should be smaller than FP16 ({bytes_fp16})" | |
| # Switch to INT4 | |
| cache.set_precision(KVPrecision.INT4) | |
| bytes_int4 = cache.current_bytes | |
| assert bytes_int4 < bytes_int8, f"INT4 ({bytes_int4}) should be smaller than INT8 ({bytes_int8})" | |
| print("✓ test_adaptive_kv_cache_precision_switch passed") | |
| if __name__ == "__main__": | |
| test_quantized_kv_tensor() | |
| test_adaptive_kv_cache_append_and_evict() | |
| test_adaptive_kv_cache_precision_switch() | |
| print("All KV Cache tests passed!") | |