Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ollama Ollama Llama Arch

From Leeroopedia
Knowledge Sources
Domains Model Architecture, GGUF
Last Updated 2025-02-15 00:00 GMT

Overview

Defines the mapping tables between model architecture enums, their string names, GGUF key-value identifiers, and tensor name patterns for all supported LLM architectures.

Description

Contains large static std::map tables: LLM_ARCH_NAMES maps llm_arch enum values to string names (e.g., LLM_ARCH_LLAMA to "llama"), LLM_KV_NAMES maps key-value identifiers to GGUF metadata key strings, and per-architecture tensor name maps that associate llm_tensor enum values with their expected tensor name patterns in GGUF files. Each architecture (LLaMA, Falcon, GPT-2, Qwen, Gemma, DeepSeek, Mamba, etc.) has its own tensor mapping entry. Also implements the LLM_KV and LLM_TN helper classes for constructing architecture-specific key/tensor name strings.

Usage

This file is the core registry that enables the model loader to work with dozens of different model architectures. It is used whenever loading, saving, or interpreting GGUF model files.

Code Reference

Source Location

  • Repository: Ollama
  • File: llama/llama.cpp/src/llama-arch.cpp
  • Lines: 1-2499

Signature

static const std::map<llm_arch, const char *> LLM_ARCH_NAMES = {
    { LLM_ARCH_LLAMA,    "llama"    },
    { LLM_ARCH_FALCON,   "falcon"   },
    { LLM_ARCH_GPT2,     "gpt2"     },
    { LLM_ARCH_QWEN2,    "qwen2"    },
    { LLM_ARCH_GEMMA,    "gemma"    },
    { LLM_ARCH_DEEPSEEK2,"deepseek2"},
    { LLM_ARCH_MAMBA,    "mamba"    },
    // ... 100+ architectures
};

static const std::map<llm_kv, const char *> LLM_KV_NAMES = {
    { LLM_KV_GENERAL_ARCHITECTURE, "general.architecture" },
    { LLM_KV_CONTEXT_LENGTH,       "%s.context_length"    },
    { LLM_KV_EMBEDDING_LENGTH,     "%s.embedding_length"  },
    // ... 200+ keys
};

Import

#include "llama-arch.h"

I/O Contract

Inputs

Name Type Required Description
arch llm_arch Yes Architecture enum to look up
kv llm_kv Yes Key-value enum to look up

Outputs

Name Type Description
name const char * Architecture or key name string
tensor_names map Tensor name patterns for a specific architecture

Usage Examples

#include "llama-arch.h"

// Look up architecture name
const char * arch_name = LLM_ARCH_NAMES.at(LLM_ARCH_LLAMA); // "llama"

// Construct architecture-prefixed key
LLM_KV kv(LLM_ARCH_LLAMA);
std::string key = kv(LLM_KV_CONTEXT_LENGTH); // "llama.context_length"

// Construct tensor name with layer index
LLM_TN tn(LLM_ARCH_LLAMA);
std::string tname = tn(LLM_TENSOR_ATTN_Q, "weight", 0); // "blk.0.attn_q.weight"

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment