Text Generation
Transformers
Safetensors
ncp_smol
next-concept-prediction
conceptlm
causal-lm
smollm2
tessera
custom_code
Instructions to use yava-code/Tessera-135M-Gate with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use yava-code/Tessera-135M-Gate with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="yava-code/Tessera-135M-Gate", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("yava-code/Tessera-135M-Gate", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use yava-code/Tessera-135M-Gate with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "yava-code/Tessera-135M-Gate" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "yava-code/Tessera-135M-Gate", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/yava-code/Tessera-135M-Gate
- SGLang
How to use yava-code/Tessera-135M-Gate 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 "yava-code/Tessera-135M-Gate" \ --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": "yava-code/Tessera-135M-Gate", "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 "yava-code/Tessera-135M-Gate" \ --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": "yava-code/Tessera-135M-Gate", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use yava-code/Tessera-135M-Gate with Docker Model Runner:
docker model run hf.co/yava-code/Tessera-135M-Gate
Download quantizer.py from yava-code/Tessera-135M-Gate: direct link, hf CLI and curl.
- Browser
- Download file 3.79 kB
-
https://huggingface.co/yava-code/Tessera-135M-Gate/resolve/main/quantizer.py
- Command line
-
hf download hf://yava-code/Tessera-135M-Gate/quantizer.py
-
curl -L -o quantizer.py https://huggingface.co/yava-code/Tessera-135M-Gate/resolve/main/quantizer.py
3.79 kB
| from __future__ import annotations | |
| from dataclasses import dataclass | |
| import torch | |
| import torch.nn.functional as F | |
| from torch import Tensor, nn | |
| class QuantizerOutput: | |
| codes: Tensor | |
| indices: Tensor | |
| loss: Tensor | |
| class CodebookTransform(nn.Module): | |
| def __init__(self, segments: int, dim: int) -> None: | |
| super().__init__() | |
| self.layers = nn.ModuleList( | |
| [ | |
| nn.Sequential( | |
| nn.Linear(dim, 2 * dim), | |
| nn.ReLU(), | |
| nn.Linear(2 * dim, dim), | |
| ) | |
| for _ in range(segments) | |
| ] | |
| ) | |
| def forward(self, codebook: Tensor) -> Tensor: | |
| return torch.stack( | |
| [layer(codes) for layer, codes in zip(self.layers, codebook, strict=True)] | |
| ) | |
| class ProductVectorQuantizer(nn.Module): | |
| def __init__(self, hidden_size: int, segments: int, codebook_size: int) -> None: | |
| super().__init__() | |
| if hidden_size % segments: | |
| raise ValueError("hidden_size must be divisible by segments") | |
| self.hidden_size = hidden_size | |
| self.segments = segments | |
| self.codebook_size = codebook_size | |
| self.segment_dim = hidden_size // segments | |
| self.register_buffer( | |
| "codebook", | |
| torch.empty(segments, codebook_size, self.segment_dim), | |
| ) | |
| self.transform = CodebookTransform(segments, self.segment_dim) | |
| nn.init.normal_(self.codebook, std=0.02) | |
| def transformed_codes(self) -> Tensor: | |
| return self.transform(self.codebook) | |
| def forward(self, concepts: Tensor) -> QuantizerOutput: | |
| if concepts.shape[-1] != self.hidden_size: | |
| raise ValueError(f"expected hidden size {self.hidden_size}, got {concepts.shape[-1]}") | |
| shape = concepts.shape[:-1] | |
| x = concepts.reshape(*shape, self.segments, self.segment_dim) | |
| target = x.detach() | |
| codes = self.transformed_codes() | |
| distances = ( | |
| target.square().sum(dim=-1, keepdim=True) | |
| + codes.square() | |
| .sum(dim=-1) | |
| .view(*([1] * len(shape)), self.segments, self.codebook_size) | |
| - 2 * torch.einsum("...sd,snd->...sn", target, codes) | |
| ) | |
| indices = distances.argmin(dim=-1) | |
| gather_index = indices.unsqueeze(-1).expand(*indices.shape, self.segment_dim) | |
| expanded = codes.view(*([1] * len(shape)), *codes.shape).expand(*shape, *codes.shape) | |
| quantized = expanded.gather(-2, gather_index.unsqueeze(-2)).squeeze(-2) | |
| loss = F.mse_loss(quantized, target) | |
| return QuantizerOutput(quantized.reshape(*shape, self.hidden_size), indices, loss) | |
| def expected(self, logits: Tensor) -> Tensor: | |
| if logits.shape[-2:] != (self.segments, self.codebook_size): | |
| raise ValueError("logits do not match the product codebook") | |
| probs = logits.float().softmax(dim=-1).to(logits.dtype) | |
| predicted = torch.einsum("...sn,snd->...sd", probs, self.transformed_codes()) | |
| return predicted.flatten(-2) | |
| def usage(self, indices: Tensor) -> dict[str, float]: | |
| flat = indices.reshape(-1, self.segments) | |
| perplexities = [] | |
| active = [] | |
| for segment in range(self.segments): | |
| counts = torch.bincount(flat[:, segment], minlength=self.codebook_size).float() | |
| probs = counts / counts.sum().clamp_min(1) | |
| entropy = -(probs * probs.clamp_min(1e-12).log()).sum() | |
| perplexities.append(entropy.exp()) | |
| active.append((counts > 0).float().mean()) | |
| return { | |
| "codebook_perplexity": torch.stack(perplexities).mean().item(), | |
| "codebook_usage": torch.stack(active).mean().item(), | |
| } | |