Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Ggml org Llama cpp Inspect Org Model

From Leeroopedia
Field Value
Implementation Name Inspect Org Model
Type Wrapper Doc
Wraps SafeTensors file inspection via safetensors.safe_open()
Status Active

Overview

Description

The inspect-org-model.py script is a utility in the llama.cpp model conversion toolkit that enumerates all tensors in a HuggingFace model directory. It handles both single-file models (model.safetensors) and multi-file sharded models (via model.safetensors.index.json). For each tensor, it reports the tensor name, shape, and data type.

The script is located at examples/model-conversion/scripts/utils/inspect-org-model.py (68 lines). It accepts the model path via either the --model-path CLI argument or the MODEL_PATH environment variable.

Usage

python examples/model-conversion/scripts/utils/inspect-org-model.py --model-path /path/to/model

Or using the environment variable:

export MODEL_PATH=/path/to/model
python examples/model-conversion/scripts/utils/inspect-org-model.py

Code Reference

Source Location

File Lines Description
examples/model-conversion/scripts/utils/inspect-org-model.py 1-68 Full script

Signature

CLI interface:

parser = argparse.ArgumentParser(description='Process model with specified path')
parser.add_argument('--model-path', '-m', help='Path to the model')

Model path resolution:

model_path = os.environ.get('MODEL_PATH', args.model_path)

Multi-file model detection and processing (lines 21-46):

index_path = os.path.join(model_path, "model.safetensors.index.json")
single_file_path = os.path.join(model_path, "model.safetensors")

if os.path.exists(index_path):
    # Multi-file model
    with open(index_path, 'r') as f:
        index_data = json.load(f)
    weight_map = index_data.get("weight_map", {})
    file_tensors = defaultdict(list)
    for tensor_name, file_name in weight_map.items():
        file_tensors[file_name].append(tensor_name)
    for file_name, tensor_names in file_tensors.items():
        file_path = os.path.join(model_path, file_name)
        with safe_open(file_path, framework="pt") as f:
            for tensor_name in sorted(tensor_names):
                tensor = f.get_tensor(tensor_name)
                print(f"- {tensor_name} : shape = {tensor.shape}, dtype = {tensor.dtype}")
elif os.path.exists(single_file_path):
    # Single file model
    with safe_open(single_file_path, framework="pt") as f:
        keys = f.keys()
        for key in sorted(keys):
            tensor = f.get_tensor(key)
            print(f"- {key} : shape = {tensor.shape}, dtype = {tensor.dtype}")

Import

import argparse
import os
import json
from safetensors import safe_open
from collections import defaultdict

I/O Contract

Direction Type Description
Input str (path) Path to a model directory containing SafeTensors files, provided via --model-path or MODEL_PATH env var
Output stdout One line per tensor: - {name} : shape = {shape}, dtype = {dtype}
Output stdout Shard headers for multi-file models: --- From {filename} ---
Error stderr/exit(1) If neither model.safetensors.index.json nor model.safetensors is found

Expected directory contents:

Model Type Required Files
Single-file model.safetensors
Multi-file (sharded) model.safetensors.index.json plus referenced shard files (e.g., model-00001-of-00004.safetensors)

Usage Examples

Inspecting a single-file model:

python examples/model-conversion/scripts/utils/inspect-org-model.py \
    --model-path ./models/SmolLM2-1.7B-Instruct

Example output:

Single-file model detected
Tensors in model:
- lm_head.weight : shape = torch.Size([49152, 2048]), dtype = torch.float16
- model.embed_tokens.weight : shape = torch.Size([49152, 2048]), dtype = torch.float16
- model.layers.0.input_layernorm.weight : shape = torch.Size([2048]), dtype = torch.float16
- model.layers.0.mlp.down_proj.weight : shape = torch.Size([2048, 8192]), dtype = torch.float16
- model.layers.0.mlp.gate_proj.weight : shape = torch.Size([8192, 2048]), dtype = torch.float16
- model.layers.0.mlp.up_proj.weight : shape = torch.Size([8192, 2048]), dtype = torch.float16
- model.layers.0.self_attn.k_proj.weight : shape = torch.Size([512, 2048]), dtype = torch.float16
- model.layers.0.self_attn.q_proj.weight : shape = torch.Size([2048, 2048]), dtype = torch.float16
- model.layers.0.self_attn.v_proj.weight : shape = torch.Size([512, 2048]), dtype = torch.float16
...

Inspecting a sharded model:

python examples/model-conversion/scripts/utils/inspect-org-model.py \
    --model-path ./models/Llama-3.1-8B-Instruct

Example output:

Multi-file model detected
Tensors in model:

--- From model-00001-of-00004.safetensors ---
- model.embed_tokens.weight : shape = torch.Size([128256, 4096]), dtype = torch.bfloat16
- model.layers.0.input_layernorm.weight : shape = torch.Size([4096]), dtype = torch.bfloat16
...

Related Pages

Page Connections

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