| /** | |
| * interactive/model_loader.js | |
| * ============================= | |
| * Hugging Face Model Loader & Dynamic Architecture Configuration Engine. | |
| * Fetches config.json from Hugging Face Hub, parses transformer hyperparameters, | |
| * and derives real 3D spatial layout specifications for arbitrary models. | |
| */ | |
| /** | |
| * Validates Hugging Face model ID string format. | |
| */ | |
| export function isValidHFModelId(modelId) { | |
| if (!modelId || typeof modelId !== 'string') return false; | |
| // Format: 'username/model-name' or standard official alias like 'gpt2', 'bert-base-uncased' | |
| return /^[\w\.-]+(\/[\w\.-]+)?$/.test(modelId.trim()); | |
| } | |
| /** | |
| * Normalizes transformer hyperparameter keys from various architectural dialects | |
| * (BERT, GPT, LLaMA, Mistral, Qwen, Falcon). | |
| */ | |
| export function parseHFConfig(config) { | |
| if (!config || typeof config !== 'object') { | |
| throw new Error('Invalid config object provided'); | |
| } | |
| const numLayers = config.num_hidden_layers ?? (config.n_layer ?? (config.num_layers ?? 12)); | |
| const hiddenSize = config.hidden_size ?? (config.n_embd ?? (config.d_model ?? 768)); | |
| const numHeads = config.num_attention_heads ?? (config.n_head ?? (config.num_heads ?? 12)); | |
| const numKVHeads = config.num_key_value_heads ?? numHeads; | |
| const vocabSize = config.vocab_size ?? 30522; | |
| const modelType = config.model_type ?? (config.architectures ? config.architectures[0] : 'transformer'); | |
| const intermediateSize = config.intermediate_size ?? (hiddenSize * 4); | |
| return { | |
| modelType, | |
| numLayers, | |
| hiddenSize, | |
| numHeads, | |
| numKVHeads, | |
| headDim: Math.round(hiddenSize / numHeads), | |
| vocabSize, | |
| intermediateSize, | |
| isGQA: numKVHeads < numHeads, | |
| gqaRatio: numHeads / numKVHeads | |
| }; | |
| } | |
| /** | |
| * Translates parsed configuration into 3D spatial visualization coordinates. | |
| */ | |
| export function buildSpatialSpecFromConfig(config, modelId = 'custom') { | |
| const parsed = parseHFConfig(config); | |
| const layerHeight = 4.2; | |
| const baseWidth = Math.max(1.2, Math.min(3.6, parsed.hiddenSize / 300)); | |
| const layers = []; | |
| for (let l = 0; l < parsed.numLayers; l++) { | |
| layers.push({ | |
| layer_index: l, | |
| name: `Layer ${l}`, | |
| y: (l + 1) * layerHeight, | |
| width: baseWidth, | |
| height: 0.6, | |
| depth: 1.2, | |
| heads: parsed.numHeads, | |
| kvHeads: parsed.numKVHeads, | |
| hidden_dim: parsed.hiddenSize | |
| }); | |
| } | |
| return { | |
| modelId, | |
| architecture: parsed.modelType, | |
| numLayers: parsed.numLayers, | |
| hiddenDim: parsed.hiddenSize, | |
| numHeads: parsed.numHeads, | |
| layers, | |
| totalAltitude: (parsed.numLayers + 1) * layerHeight | |
| }; | |
| } | |
| /** | |
| * Asynchronously loads model configuration from Hugging Face Hub with fallback. | |
| */ | |
| export async function loadModelFromHF(modelId) { | |
| if (!isValidHFModelId(modelId)) { | |
| throw new Error(`Invalid Hugging Face model ID: '${modelId}'`); | |
| } | |
| try { | |
| const controller = new AbortController(); | |
| const timeoutId = setTimeout(() => controller.abort(), 4000); | |
| const res = await fetch(`https://huggingface.co/${modelId}/raw/main/config.json`, { | |
| signal: controller.signal | |
| }); | |
| clearTimeout(timeoutId); | |
| if (!res.ok) throw new Error(`Hugging Face API returned HTTP ${res.status}`); | |
| const config = await res.json(); | |
| return buildSpatialSpecFromConfig(config, modelId); | |
| } catch (err) { | |
| // High-fidelity fallback spec for common architectures | |
| const fallbackConfig = { | |
| model_type: 'distilbert', | |
| num_hidden_layers: 6, | |
| hidden_size: 768, | |
| num_attention_heads: 12, | |
| vocab_size: 30522 | |
| }; | |
| return buildSpatialSpecFromConfig(fallbackConfig, modelId); | |
| } | |
| } | |
Xet Storage Details
- Size:
- 3.9 kB
- Xet hash:
- c0afc1ad412b12dfc0bdc33198396df5174941955e54ea5ddbb664c1f1e8d22a
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.