### Define Model and Hardware Configuration Presets
Source: https://context7.com/erans/selfhostllm/llms.txt
This HTML structure defines model options with associated memory requirements and quantization levels. It includes support for Mixture-of-Experts (MoE) active memory specifications and standard dropdown presets for quantization and context length.
```html
```
--------------------------------
### Manage URL Configuration Sharing
Source: https://context7.com/erans/selfhostllm/llms.txt
Functions to serialize hardware and model settings into URL query parameters and restore them upon page load. This enables users to share specific calculator configurations via direct links without requiring a backend database.
```javascript
function updateURL() {
const params = new URLSearchParams();
const gpuType = document.getElementById('gpu-type').value;
if (gpuType) params.set('gpu', gpuType);
params.set('gpu_count', document.getElementById('gpu-count').value);
params.set('sys_overhead', document.getElementById('system-overhead').value);
const modelInputType = document.querySelector('input[name="model-input-type"]:checked').value;
params.set('model_type', modelInputType);
if (modelInputType === 'preset') {
params.set('model', document.getElementById('model-preset').value);
} else if (modelInputType === 'parameters') {
params.set('model_params', document.getElementById('model-parameters').value);
} else if (modelInputType === 'memory') {
params.set('model_memory', document.getElementById('model-memory-input').value);
}
params.set('quant', document.getElementById('quantization').value);
params.set('context', document.getElementById('context-preset').value);
params.set('kv_cache', document.getElementById('kv-cache-overhead').value);
const newURL = window.location.pathname + '?' + params.toString();
window.history.replaceState({}, '', newURL);
}
function loadFromURL() {
const params = new URLSearchParams(window.location.search);
if (params.has('gpu')) document.getElementById('gpu-type').value = params.get('gpu');
if (params.has('gpu_count')) document.getElementById('gpu-count').value = params.get('gpu_count');
if (params.has('quant')) document.getElementById('quantization').value = params.get('quant');
calculate();
}
```
--------------------------------
### Calculate LLM Memory Requirements
Source: https://context7.com/erans/selfhostllm/llms.txt
This snippet demonstrates the core mathematical formula used to determine the maximum number of concurrent requests an LLM deployment can handle based on GPU memory and KV cache overhead.
```javascript
const totalVRAM = gpuCount * vramPerGpu;
const adjustedModelMemory = modelMemory * quantization;
const kvCachePerRequest = (contextLength * adjustedModelMemory * kvCacheOverhead) / 1000;
const availableMemory = totalVRAM - systemOverhead - adjustedModelMemory;
const maxConcurrentRequests = availableMemory / kvCachePerRequest;
```
--------------------------------
### Implement Searchable Select Component
Source: https://context7.com/erans/selfhostllm/llms.txt
A custom UI component that wraps standard inputs with filtering logic and keyboard navigation support. It improves user experience by allowing quick selection of hardware or model presets from large lists.
```javascript
function setupSearchableSelect(config) {
const select = document.getElementById(config.id);
const input = document.createElement('input');
input.type = 'text';
input.className = 'integrated-select-input';
input.placeholder = config.placeholder || 'Select or type to filter...';
input.setAttribute('role', 'combobox');
input.addEventListener('input', () => {
renderOptions(input.value);
openMenu();
});
input.addEventListener('keydown', (event) => {
if (event.key === 'ArrowDown') {
highlightedIndex = Math.min(filteredOptions.length - 1, highlightedIndex + 1);
}
if (event.key === 'ArrowUp') {
highlightedIndex = Math.max(0, highlightedIndex - 1);
}
if (event.key === 'Enter') {
pickHighlighted();
}
if (event.key === 'Escape') {
closeMenu();
}
});
}
```
--------------------------------
### Estimate GPU Performance for LLM Inference
Source: https://context7.com/erans/selfhostllm/llms.txt
Calculates the estimated tokens per second for a given GPU configuration. It accounts for memory bandwidth, model size, quantization levels, context length, and multi-GPU scaling factors.
```javascript
function calculatePerformance(modelMemory, quantization, contextLength, gpuModel, gpuCount) {
const bandwidth = getGPUBandwidth(gpuModel) * gpuCount;
if (!bandwidth) return null;
const bandwidthMap = {
'rtx4090': 1008,
'rtx4080': 736,
'rtx3090': 936,
'a100': 1600,
'a100-80': 2000,
'h100': 3000,
'rx7900xtx': 960
};
let efficiency = modelParams <= 7 ? 0.85 : modelParams <= 30 ? 0.7 : modelParams <= 70 ? 0.5 : 0.3;
let quantBoost = quantization <= 0.25 ? 2.5 : quantization <= 0.3 ? 2.2 : quantization <= 0.5 ? 1.8 : quantization <= 0.75 ? 1.3 : 1.0;
let contextImpact = contextLength >= 131072 ? 0.3 : contextLength >= 32768 ? 0.6 : contextLength >= 8192 ? 0.85 : 1.0;
let multiGpuScaling = gpuCount > 1 ? 0.85 + (0.15 / gpuCount) : 1.0;
const tokensPerSecond = (bandwidth / modelMemory) * efficiency * quantBoost * contextImpact * multiGpuScaling * 0.6;
return { tokensPerSecond, bandwidth, efficiency, quantBoost, contextImpact };
}
```
--------------------------------
### Configure Cloudflare Worker for Static Assets
Source: https://context7.com/erans/selfhostllm/llms.txt
A serverless script for Cloudflare Workers that serves static assets from KV storage. It includes logic for URL redirection, aggressive caching headers, and CORS support for cross-origin requests.
```javascript
import { getAssetFromKV } from '@cloudflare/kv-asset-handler';
addEventListener('fetch', (event) => {
event.respondWith(handleRequest(event));
});
async function handleRequest(event) {
const url = new URL(event.request.url);
const path = url.pathname;
try {
if (path === '/mac') {
return Response.redirect(`${url.origin}/mac/`, 301);
}
const response = await getAssetFromKV(event, {
cacheControl: {
browserTTL: 31536000,
edgeTTL: 86400,
},
});
const headers = new Headers(response.headers);
headers.set('Access-Control-Allow-Origin', '*');
return new Response(response.body, {
status: response.status,
headers: headers,
});
} catch (e) {
return new Response('Not Found', { status: 404 });
}
}
```
--------------------------------
### Check Apple Silicon Mac Compatibility
Source: https://context7.com/erans/selfhostllm/llms.txt
Evaluates whether a specific LLM can run on an Apple Silicon Mac by comparing total memory requirements (model + KV cache) against available system RAM. It also provides a helper to retrieve memory bandwidth for various M-series chips.
```javascript
function checkCompatibility() {
const macRam = parseFloat(document.getElementById('mac-ram').value) || 16;
const systemOverhead = parseFloat(document.getElementById('system-overhead').value) || 4;
const quantization = parseFloat(document.getElementById('quantization').value) || 1.0;
const contextLength = parseInt(document.getElementById('context-length').value) || 8192;
const selectedOption = modelSelect.options[modelSelect.selectedIndex];
const activeMemory = selectedOption.getAttribute('data-active-memory');
const baseMemory = activeMemory ? parseFloat(activeMemory) : parseFloat(selectedOption.getAttribute('data-memory'));
const modelMemory = baseMemory * quantization;
const kvCachePerToken = modelMemory * 0.00002;
const kvCacheMemory = contextLength * kvCachePerToken;
const totalMemoryNeeded = modelMemory + kvCacheMemory;
const availableRam = macRam - systemOverhead;
const memoryMargin = availableRam - totalMemoryNeeded;
return memoryMargin;
}
function getMacBandwidth(macModel) {
const bandwidthMap = {
'm5': 153, 'm4-max': 546, 'm4-pro': 273, 'm4': 120,
'm3-ultra': 819, 'm3-max': 400, 'm2-ultra': 800, 'm1-max': 400
};
return bandwidthMap[macModel] || 153;
}
```
--------------------------------
### Implement GPU Memory Calculation Function
Source: https://context7.com/erans/selfhostllm/llms.txt
This function retrieves user inputs from the DOM, calculates the required memory for a specific model configuration, and determines the concurrent request capacity. It handles various input types including model presets, parameter counts, and direct memory values.
```javascript
function calculate() {
const gpuCount = parseInt(document.getElementById('gpu-count').value) || 1;
const vramPerGpu = parseFloat(document.getElementById('vram-per-gpu').value) || 0;
const systemOverhead = parseFloat(document.getElementById('system-overhead').value) || 2;
let modelMemory;
const modelInputType = document.querySelector('input[name="model-input-type"]:checked').value;
if (modelInputType === 'preset') {
const modelSelect = document.getElementById('model-preset');
const selectedOption = modelSelect.options[modelSelect.selectedIndex];
const activeMemory = selectedOption.getAttribute('data-active-memory');
modelMemory = activeMemory ? parseFloat(activeMemory) : parseFloat(selectedOption.getAttribute('data-memory'));
} else if (modelInputType === 'parameters') {
const paramCount = parseFloat(document.getElementById('model-parameters').value) || 7;
modelMemory = paramCount * 2;
} else {
modelMemory = parseFloat(document.getElementById('model-memory-input').value) || 14;
}
const quantization = parseFloat(document.getElementById('quantization').value);
const kvCacheOverhead = parseFloat(document.getElementById('kv-cache-overhead').value) / 100;
const totalVRAM = gpuCount * vramPerGpu;
const adjustedModelMemory = modelMemory * quantization;
const kvCachePerRequest = (contextLength * adjustedModelMemory * kvCacheOverhead) / 1000;
const availableMemory = totalVRAM - systemOverhead - adjustedModelMemory;
const maxConcurrentRequests = availableMemory / kvCachePerRequest;
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.