> For the complete documentation index, see [llms.txt](https://docs.clore.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.clore.ai/guides/guides_v2-es/modelos-de-lenguaje/exllamav2-fast.md).

# ExLlamaV2

Inferencia de LLM a máxima velocidad con ExLlamaV2 en GPUs de Clore.ai

Ejecuta LLMs a máxima velocidad con ExLlamaV2.

{% hint style="success" %}
Todos los ejemplos se pueden ejecutar en servidores GPU alquilados a través de [Marketplace de CLORE.AI](https://clore.ai/marketplace).
{% endhint %}

## Alquilar en CLORE.AI

1. Visita [Marketplace de CLORE.AI](https://clore.ai/marketplace)
2. Filtra por tipo de GPU, VRAM y precio
3. Elige **Bajo demanda** (tarifa fija) o **Spot** (precio ofertado)
4. Configura tu pedido:
   * Selecciona la imagen de Docker
   * Configura los puertos (TCP para SSH, HTTP para interfaces web)
   * Añade variables de entorno si es necesario
   * Introduce el comando de inicio
5. Selecciona el método de pago: **CLORE**, **BTC**, o **USDT/USDC**
6. Crea el pedido y espera al despliegue

### Accede a tu servidor

* Encuentra los detalles de conexión en **Mis pedidos**
* Interfaces web: Usa la URL del puerto HTTP
* SSH: `ssh -p <port> root@<proxy-address>`

## ¿Qué es ExLlamaV2?

ExLlamaV2 es el motor de inferencia más rápido para modelos de lenguaje grandes:

* 2-3x más rápido que otros motores
* Excelente cuantización (EXL2)
* Bajo uso de VRAM
* Admite decodificación especulativa

## Requisitos

| Tamaño del modelo | VRAM mínima | Recomendado |
| ----------------- | ----------- | ----------- |
| 7B                | 6GB         | RTX 3060    |
| 13B               | 10GB        | RTX 3090    |
| 34B               | 20 GB       | RTX 4090    |
| 70B               | 40 GB       | A100        |

## Despliegue rápido

**Imagen de Docker:**

```
pytorch/pytorch:2.11.0-cuda12.8-cudnn9-devel
```

**Puertos:**

```
22/tcp
8080/http
```

**Comando:**

```bash
pip install exllamav2 && \\
huggingface-cli download turboderp/Llama2-7B-exl2 --local-dir ./model && \\
python -m exllamav2.server --model_dir ./model --host 0.0.0.0 --port 8080
```

## Accediendo a tu servicio

Después del despliegue, encuentra tu `http_pub` URL en **Mis pedidos**:

1. Ve a **Mis pedidos** página
2. Haz clic en tu pedido
3. Encuentra la `http_pub` URL (p. ej., `abc123.clorecloud.net`)

Usa `https://YOUR_HTTP_PUB_URL` en lugar de `localhost` en los ejemplos a continuación.

## Instalación

```bash

# Instalar desde PyPI
pip install exllamav2

# O desde el código fuente (últimas funciones)
git clone https://github.com/turboderp/exllamav2
cd exllamav2
pip install .
```

## Descargar modelos

### Modelos cuantizados EXL2

```bash

# Llama 3.1 8B (4.0 bpw)
huggingface-cli download turboderp/Llama2-7B-exl2 \\
    --revision 4.0bpw \\
    --local-dir ./llama2-7b-exl2

# Llama 3.1 8B (4.0 bpw)
huggingface-cli download turboderp/Llama2-13B-exl2 \\
    --revision 4.0bpw \\
    --local-dir ./llama2-13b-exl2

# Mistral 7B (4.0 bpw)
huggingface-cli download turboderp/Mistral-7B-instruct-exl2 \\
    --revision 4.0bpw \\
    --local-dir ./mistral-7b-exl2

# Mixtral 8x7B
huggingface-cli download turboderp/Mixtral-8x7B-instruct-exl2 \\
    --revision 4.0bpw \\
    --local-dir ./mixtral-exl2
```

### Bits por peso (bpw)

| BPW | Calidad   | VRAM (7B) |
| --- | --------- | --------- |
| 2.0 | Baja      | \~3GB     |
| 3.0 | Bueno     | \~4 GB    |
| 4.0 | Excelente | \~5GB     |
| 5.0 | Excelente | \~6GB     |
| 6.0 | Casi FP16 | \~7GB     |

## API de Python

### Generación básica

```python
from exllamav2 import ExLlamaV2, ExLlamaV2Config, ExLlamaV2Cache, ExLlamaV2Tokenizer
from exllamav2.generator import ExLlamaV2StreamingGenerator, ExLlamaV2Sampler

# Cargar modelo
config = ExLlamaV2Config()
config.model_dir = "./llama2-7b-exl2"
config.prepare()

model = ExLlamaV2(config)
model.load()

tokenizer = ExLlamaV2Tokenizer(config)
cache = ExLlamaV2Cache(model, lazy=True)

# Crear generador
generator = ExLlamaV2StreamingGenerator(model, cache, tokenizer)

# Establecer ajustes de muestreo
settings = ExLlamaV2Sampler.Settings()
settings.temperature = 0.7
settings.top_k = 50
settings.top_p = 0.9

# Generar
prompt = "The future of artificial intelligence is"
output = generator.generate_simple(prompt, settings, num_tokens=200)
print(output)
```

### Generación en streaming

```python
from exllamav2.generator import ExLlamaV2StreamingGenerator

generator = ExLlamaV2StreamingGenerator(model, cache, tokenizer)

prompt = "Escribe una historia corta sobre un robot:"
input_ids = tokenizer.encode(prompt)

generator.set_stop_conditions([tokenizer.eos_token_id])
generator.begin_stream(input_ids, settings)

while True:
    chunk, eos, _ = generator.stream()
    if eos:
        break
    print(chunk, end="", flush=True)
```

### Formato de chat

```python
def format_chat(messages):
    text = ""
    for msg in messages:
        role = msg["role"]
        content = msg["content"]
        if role == "system":
            text += f"[INST] <<SYS>>\n{content}\n<</SYS>>\n\n"
        elif role == "user":
            text += f"{content} [/INST]"
        elif role == "assistant":
            text += f" {content}</s><s>[INST] "
    return text

messages = [
    {"role": "system", "content": "Eres un asistente útil."},
    {"role": "user", "content": "¿Qué es Python?"}
]

prompt = format_chat(messages)
output = generator.generate_simple(prompt, settings, num_tokens=300)
```

## Modo servidor

### Iniciar servidor

```bash
python -m exllamav2.server \\
    --model_dir ./llama2-7b-exl2 \\
    --host 0.0.0.0 \\
    --port 8080 \\
    --max_seq_len 4096 \\
    --cache_size 4096
```

### Uso de la API

```python
import requests

response = requests.post(
    "http://localhost:8080/v1/completions",
    json={
        "prompt": "Hola, ¿cómo estás?",
        "max_tokens": 100,
        "temperature": 0.7
    }
)

print(response.json()["choices"][0]["text"])

```

### Completaciones de chat

```python
import openai

client = openai.OpenAI(
    base_url="http://localhost:8080/v1",
    api_key="no necesario"
)

response = client.chat.completions.create(
    model="llama2-7b",
    messages=[{"role": "user", "content": "¡Hola!"}],
    temperature=0.7
)

print(response.choices[0].message.content)
```

## TabbyAPI (Servidor recomendado)

TabbyAPI proporciona un servidor ExLlamaV2 con muchas funciones:

```bash

# Clonar TabbyAPI
git clone https://github.com/theroyallab/tabbyAPI
cd tabbyAPI

# Instalar
pip install -r requirements.txt

# Configurar

# Edita config.yml con la ruta de tu modelo

# Ejecutar
python main.py
```

### Funciones de TabbyAPI

* API compatible con OpenAI
* Compatibilidad con múltiples modelos
* Intercambio en caliente de LoRA
* Transmisión
* Llamada a funciones
* API de administrador

## Decodificación especulativa

Usa un modelo más pequeño para acelerar la generación:

```python
from exllamav2 import ExLlamaV2, ExLlamaV2Config, ExLlamaV2Cache

# Cargar modelo principal (13B)
main_config = ExLlamaV2Config()
main_config.model_dir = "./llama2-13b-exl2"
main_config.prepare()
main_model = ExLlamaV2(main_config)
main_model.load()

# Cargar modelo borrador (7B)
draft_config = ExLlamaV2Config()
draft_config.model_dir = "./llama2-7b-exl2"
draft_config.prepare()
draft_model = ExLlamaV2(draft_config)
draft_model.load()

# Crear generador especulativo
from exllamav2.generator import ExLlamaV2DraftGenerator

generator = ExLlamaV2DraftGenerator(
    main_model, draft_model,
    cache_main, cache_draft,
    tokenizer
)

# Generar (más rápido con especulación)
output = generator.generate_simple(prompt, settings, num_tokens=500)
```

## Cuantiza tus propios modelos

### Convertir a EXL2

```python
from exllamav2 import ExLlamaV2, ExLlamaV2Config
from exllamav2.conversion import convert_model

# Origen: modelo de HuggingFace

# Destino: cuantizado a EXL2

convert_model(
    input_dir="./llama-3.1-8b-hf",
    output_dir="./llama-3.1-8b-exl2-4bpw",
    cal_dataset="wikitext",  # Conjunto de datos de calibración
    bits=4.0,  # Bits por peso
    head_bits=6,  # Mayor precisión para la atención
)
```

### Línea de comandos

```bash
python convert.py \\
    -i ./llama-3.1-8b-hf \\
    -o ./llama-3.1-8b-exl2 \\
    -cf ./llama-3.1-8b-exl2 \\
    -b 4.0 \\
    -hb 6
```

## Gestión de memoria

### Asignación de caché

```python

# Tamaño fijo de caché
cache = ExLlamaV2Cache(model, max_seq_len=4096)

# Caché dinámica
cache = ExLlamaV2Cache(model, lazy=True)
cache.current_seq_len = 0  # Crece según sea necesario
```

### Multi-GPU

```python
config = ExLlamaV2Config()
config.model_dir = "./large-model"

# Dividir entre GPUs
config.set_auto_split([0.5, 0.5])  # 50% cada GPU

model = ExLlamaV2(config)
model.load()
```

## Comparación de rendimiento

| Modelo       | Motor     | GPU      | Tokens/seg |
| ------------ | --------- | -------- | ---------- |
| Llama 3.1 8B | ExLlamaV2 | RTX 3090 | \~150      |
| Llama 3.1 8B | llama.cpp | RTX 3090 | \~100      |
| Llama 3.1 8B | vLLM      | RTX 3090 | \~120      |
| Llama 3.1 8B | ExLlamaV2 | RTX 3090 | \~90       |
| Mixtral 8x7B | ExLlamaV2 | A100     | \~70       |

## Ajustes avanzados

### Parámetros de muestreo

```python
settings = ExLlamaV2Sampler.Settings()
settings.temperature = 0.7
settings.top_k = 50
settings.top_p = 0.9
settings.token_repetition_penalty = 1.1
settings.token_frequency_penalty = 0.0
settings.token_presence_penalty = 0.0
settings.mirostat = False
settings.mirostat_tau = 5.0
settings.mirostat_eta = 0.1
```

### Generación por lotes

```python
prompts = [
    "El sentido de la vida es",
    "La inteligencia artificial",
    "El cambio climático es"
]

outputs = []
for prompt in prompts:
    output = generator.generate_simple(prompt, settings, num_tokens=100)
    outputs.append(output)
```

## Solución de problemas

### Memoria insuficiente en CUDA

```python

# Usar una caché más pequeña
cache = ExLlamaV2Cache(model, max_seq_len=2048)

# O un modelo con menor bpw (3.0 en lugar de 4.0)
```

### Carga lenta

```python

# Habilitar carga rápida
config.fasttensors = True
```

### Modelo no encontrado

```bash

# Verificar que existan los archivos del modelo
ls ./model/

# Debería contener: config.json, *.safetensors, tokenizer.json
```

## Integración con LangChain

```python
from langchain.llms.base import LLM
from typing import Optional, List

class ExLlamaV2LLM(LLM):
    model: ExLlamaV2
    tokenizer: ExLlamaV2Tokenizer
    generator: ExLlamaV2StreamingGenerator
    settings: ExLlamaV2Sampler.Settings

    @property
    def _llm_type(self) -> str:
        return "exllamav2"

    def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
        return self.generator.generate_simple(prompt, self.settings, num_tokens=500)

# Uso
llm = ExLlamaV2LLM(model=model, tokenizer=tokenizer, generator=generator, settings=settings)
result = llm("¿Qué es la computación cuántica?")
```

## Estimación de costos

Tarifas típicas del marketplace de CLORE.AI (a partir de 2024):

| GPU       | Tarifa por hora | Tarifa diaria | Sesión de 4 horas |
| --------- | --------------- | ------------- | ----------------- |
| RTX 3060  | \~$0.03         | \~$0.70       | \~$0.12           |
| RTX 3090  | \~$0.06         | \~$1.50       | \~$0.25           |
| RTX 4090  | \~$0.10         | \~$2.30       | \~$0.40           |
| A100 40GB | \~$0.17         | \~$4.00       | \~$0.70           |
| A100 80GB | \~$0.25         | \~$6.00       | \~$1.00           |

*Los precios varían según el proveedor y la demanda. Consulta* [*Marketplace de CLORE.AI*](https://clore.ai/marketplace) *las tarifas actuales.*

**Ahorra dinero:**

* Usa el **Spot** mercado para trabajo interrumpible — alrededor de un tercio de los servidores fija el precio spot por debajo del precio bajo demanda (mediana de \~13% de descuento), el resto lo iguala
* Paga con **CLORE** tokens
* Compara precios entre distintos proveedores

## Siguientes pasos

* Inferencia vLLM - servicio de alto rendimiento
* [Servidor llama.cpp](/guides/guides_v2-es/modelos-de-lenguaje/llamacpp-server.md) - Multiplataforma
* [Text Generation WebUI](/guides/guides_v2-es/modelos-de-lenguaje/text-generation-webui.md) - Interfaz web


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-es/modelos-de-lenguaje/exllamav2-fast.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
