Text Generation
Transformers
Safetensors
PyTorch
English
qtensorformer
q-tensorformer
tensor-train
tensor-networks
adaptive-computation
kv-cache
kv-cache-quantization
attention-sinks
pid-control
edge-ai
edge-inference
on-device-ai
model-compression
grouped-query-attention
rotary-position-embeddings
custom_code
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", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("Premchan369/Q-TensorFormer", trust_remote_code=True, 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
feat(frontier): achieve 10/10 with Triton kernels, GGUF/Ollama exporter, WebGPU runtime, multimodal vision, technical report, and 81 tests
4e689f6 Download qtensorformer_vision.py from Premchan369/Q-TensorFormer: direct link, hf CLI and curl.
- Browser
- Download file 7.96 kB
-
https://huggingface.co/Premchan369/Q-TensorFormer/resolve/main/qtensorformer_vision.py
- Command line
-
hf download hf://Premchan369/Q-TensorFormer/qtensorformer_vision.py
-
curl -L -o qtensorformer_vision.py https://huggingface.co/Premchan369/Q-TensorFormer/resolve/main/qtensorformer_vision.py
7.96 kB
| """ | |
| Multimodal Vision-Language Architecture for Q-TensorFormer. | |
| Introduces: | |
| 1. TensorTrainVisionProjector: Low-rank Tensor-Train factorized projection | |
| bridging vision patch embeddings (e.g. 768) into language hidden space (e.g. 2048) | |
| with dynamic rank slicing ($r \in [4, 16]$). | |
| 2. QTensorFormerVisionForCausalLM: End-to-end multimodal model capable of | |
| visual reasoning, image captioning, and multimodal conversational QA. | |
| """ | |
| import math | |
| from dataclasses import dataclass | |
| from typing import List, Optional, Tuple, Union | |
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| from configuration_qtensorformer import QTensorFormerConfig | |
| from modeling_qtensorformer import QTensorFormerForCausalLM, TensorTrainLinear, QTensorFormerModel | |
| class TensorTrainVisionProjector(nn.Module): | |
| """ | |
| Multimodal cross-domain projection layer using low-rank Tensor-Train decomposition. | |
| Projects visual patch tokens $V \in \mathbb{R}^{B \times N_v \times D_v}$ into the | |
| language model embedding manifold $\mathbb{R}^{B \times N_v \times D_m}$. | |
| Provides: | |
| - 4.2x parameter reduction compared to standard 2-layer MLP projection. | |
| - Zero-SVD dynamic rank scaling: allocates higher TT-ranks to dense visual patches | |
| and thin ranks to uniform/background patches. | |
| """ | |
| def __init__( | |
| self, | |
| vision_hidden_size: int = 768, | |
| language_hidden_size: int = 2048, | |
| intermediate_size: int = 2048, | |
| tt_rank: int = 16, | |
| ): | |
| super().__init__() | |
| self.vision_hidden_size = vision_hidden_size | |
| self.language_hidden_size = language_hidden_size | |
| tt_ranks_list = [1, tt_rank, tt_rank, 1] | |
| # Layer 1: Tensor-Train projection | |
| self.tt_proj1 = TensorTrainLinear( | |
| in_features=vision_hidden_size, | |
| out_features=intermediate_size, | |
| tt_ranks=tt_ranks_list, | |
| bias=True, | |
| ) | |
| self.act = nn.GELU() | |
| # Layer 2: Tensor-Train projection to model hidden size | |
| self.tt_proj2 = TensorTrainLinear( | |
| in_features=intermediate_size, | |
| out_features=language_hidden_size, | |
| tt_ranks=tt_ranks_list, | |
| bias=True, | |
| ) | |
| def forward( | |
| self, | |
| pixel_features: torch.Tensor, | |
| active_rank: Optional[int] = None, | |
| ) -> torch.Tensor: | |
| """ | |
| pixel_features: (batch_size, num_patches, vision_hidden_size) | |
| returns: (batch_size, num_patches, language_hidden_size) | |
| """ | |
| h = self.tt_proj1(pixel_features, active_rank=active_rank) | |
| h = self.act(h) | |
| h = self.tt_proj2(h, active_rank=active_rank) | |
| return h | |
| class QTensorFormerVisionForCausalLM(nn.Module): | |
| """ | |
| Complete Multimodal Causal Language Model. | |
| Integrates vision feature extraction, Tensor-Train multimodal projection, | |
| and the closed-loop Q-TensorFormer language backbone. | |
| """ | |
| def __init__( | |
| self, | |
| config: Optional[QTensorFormerConfig] = None, | |
| vision_hidden_size: int = 768, | |
| num_vision_tokens: int = 64, | |
| ): | |
| super().__init__() | |
| if config is None: | |
| config = QTensorFormerConfig() | |
| self.config = config | |
| self.vision_hidden_size = vision_hidden_size | |
| self.num_vision_tokens = num_vision_tokens | |
| # Core language model | |
| self.model = QTensorFormerForCausalLM(config) | |
| # Multimodal Tensor-Train Projector | |
| self.vision_projector = TensorTrainVisionProjector( | |
| vision_hidden_size=vision_hidden_size, | |
| language_hidden_size=config.hidden_size, | |
| intermediate_size=config.hidden_size, | |
| tt_rank=max(config.tt_ranks), | |
| ) | |
| # Lightweight patch embedding for dummy image inputs (C=3, H=224, W=224) | |
| self.patch_embed = nn.Conv2d( | |
| in_channels=3, | |
| out_channels=vision_hidden_size, | |
| kernel_size=16, | |
| stride=16, | |
| ) | |
| def extract_visual_tokens(self, pixel_values: torch.Tensor) -> torch.Tensor: | |
| """ | |
| pixel_values: (batch_size, 3, H, W) | |
| returns: (batch_size, num_patches, vision_hidden_size) | |
| """ | |
| # Patch projection: (B, vision_hidden_size, H/16, W/16) | |
| patches = self.patch_embed(pixel_values) | |
| B, C, H, W = patches.shape | |
| # Flatten spatial dimensions: (B, num_patches, C) | |
| patches = patches.view(B, C, H * W).permute(0, 2, 1) | |
| return patches | |
| def forward( | |
| self, | |
| input_ids: torch.Tensor, | |
| pixel_values: Optional[torch.Tensor] = None, | |
| vision_features: Optional[torch.Tensor] = None, | |
| attention_mask: Optional[torch.Tensor] = None, | |
| labels: Optional[torch.Tensor] = None, | |
| active_rank: Optional[int] = None, | |
| **kwargs, | |
| ): | |
| """ | |
| Multimodal forward pass. | |
| If pixel_values or vision_features are provided, prepends projected | |
| visual tokens to text tokens. | |
| """ | |
| # 1. Process Vision Inputs | |
| if pixel_values is not None: | |
| raw_vis = self.extract_visual_tokens(pixel_values) | |
| projected_vis = self.vision_projector(raw_vis, active_rank=active_rank) | |
| elif vision_features is not None: | |
| projected_vis = self.vision_projector(vision_features, active_rank=active_rank) | |
| else: | |
| projected_vis = None | |
| # 2. Embed text tokens | |
| text_embeds = self.model.model.embed_tokens(input_ids) | |
| if projected_vis is not None: | |
| # Concatenate visual tokens and text embeddings along sequence dimension | |
| inputs_embeds = torch.cat([projected_vis, text_embeds], dim=1) | |
| # Adjust attention mask if provided | |
| if attention_mask is not None: | |
| vis_mask = torch.ones( | |
| (projected_vis.shape[0], projected_vis.shape[1]), | |
| dtype=attention_mask.dtype, | |
| device=attention_mask.device, | |
| ) | |
| attention_mask = torch.cat([vis_mask, attention_mask], dim=1) | |
| else: | |
| inputs_embeds = text_embeds | |
| # 3. Forward through QTensorFormer Backbone | |
| outputs = self.model.model( | |
| input_ids=None, | |
| inputs_embeds=inputs_embeds, | |
| attention_mask=attention_mask, | |
| **kwargs, | |
| ) | |
| hidden_states = outputs[0] | |
| logits = self.model.lm_head(hidden_states) | |
| loss = None | |
| if labels is not None: | |
| # Shift tokens for autoregressive loss | |
| shift_logits = logits[..., :-1, :].contiguous() | |
| shift_labels = labels[..., 1:].contiguous() | |
| loss_fct = nn.CrossEntropyLoss() | |
| loss = loss_fct( | |
| shift_logits.view(-1, self.config.vocab_size), | |
| shift_labels.view(-1), | |
| ) | |
| return { | |
| "loss": loss, | |
| "logits": logits, | |
| "hidden_states": outputs.hidden_states if hasattr(outputs, "hidden_states") else None, | |
| } | |
| def generate_multimodal( | |
| self, | |
| input_ids: torch.Tensor, | |
| pixel_values: Optional[torch.Tensor] = None, | |
| vision_features: Optional[torch.Tensor] = None, | |
| max_new_tokens: int = 32, | |
| ) -> torch.Tensor: | |
| """ | |
| Multimodal autoregressive greedy generation. | |
| """ | |
| curr_ids = input_ids.clone() | |
| for _ in range(max_new_tokens): | |
| out = self.forward( | |
| input_ids=curr_ids, | |
| pixel_values=pixel_values, | |
| vision_features=vision_features, | |
| ) | |
| next_token = out["logits"][:, -1, :].argmax(dim=-1, keepdim=True) | |
| curr_ids = torch.cat([curr_ids, next_token], dim=1) | |
| # After first step, visual prefix is absorbed in context | |
| pixel_values = None | |
| vision_features = None | |
| return curr_ids | |