Skip to content

Commit

Permalink
llama.cpp: add scripts to read tensor values
Browse files Browse the repository at this point in the history
  • Loading branch information
danbev committed Dec 1, 2024
1 parent 904a343 commit 4008b3b
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 0 deletions.
70 changes: 70 additions & 0 deletions fundamentals/llama.cpp/scripts/read-pre-pos-emb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python3
import logging
import sys
from pathlib import Path
from gguf.gguf_reader import GGUFReader
import numpy as np

logger = logging.getLogger("reader")

def read_position_embeddings(gguf_file_path, tensor_name, row_idx=6, num_values=10):
"""
Reads and prints information about position embeddings from a GGUF file.
Parameters:
- gguf_file_path: Path to the GGUF file
- tensor_name: Name of the tensor to inspect
- row_idx: Which row to read (default 6)
- num_values: How many values to print (default 10)
"""
reader = GGUFReader(gguf_file_path)

# Find the specified tensor
target_tensor = None
for tensor in reader.tensors:
if tensor.name == tensor_name:
target_tensor = tensor
break

if target_tensor is None:
print(f"Tensor '{tensor_name}' not found in the model")
return

# Print tensor information
print(f"\nTensor Information:")
print(f"Name: {target_tensor.name}")
print(f"Shape: {target_tensor.shape}") # This should already be correct
print(f"Type: {target_tensor.tensor_type.name}")
print(f"Total elements: {target_tensor.n_elements}")

# Get the tensor data
data = target_tensor.data
if isinstance(data, np.ndarray):
# Print the actual shape we got
print(f"Actual data shape: {data.shape}")
print(f"Number of rows: {data.shape[0]}")
print(f"Elements per row: {data.shape[1]}")

# Get the specified row
row_data = data[row_idx]

print(f"\nFirst {num_values} values from row {row_idx}:")
for i in range(num_values):
print(f"[{i}] = {row_data[i]}")

if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: reader.py <path_to_gguf_file> [row_index] [num_values]")
sys.exit(1)

gguf_file_path = sys.argv[1]
row_idx = int(sys.argv[2]) if len(sys.argv) > 2 else 6
num_values = int(sys.argv[3]) if len(sys.argv) > 3 else 10

read_position_embeddings(
gguf_file_path,
#"v.enc.pre_tile_pos_embd.weight",
"v.pre_tile_position_embd.weight",
row_idx,
num_values
)
33 changes: 33 additions & 0 deletions fundamentals/llama.cpp/scripts/read-safetensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from safetensors import safe_open
import numpy as np

# Load the tensor
tensors = [
"vision_model.gated_positional_embedding.tile_embedding.weight",
"vision_model.gated_positional_embedding.tile_embedding.weight",
"vision_model.gated_positional_embedding.gate",
"vision_model.gated_positional_embedding.embedding",
"vision_model.post_tile_positional_embedding.embedding.weight",
"vision_model.post_tile_positional_embedding.gate",
"vision_model.pre_tile_positional_embedding.embedding.weight",
"vision_model.pre_tile_positional_embedding.gate",
"vision_model.class_embedding",
"vision_model.patch_embedding.weight",
]

with safe_open("/home/danbev/work/ai/llama-models/Llama-3.2-11B-Vision-Instruct/model-00001-of-00005.safetensors", framework="pt") as f:
#with safe_open("/home/danbev/Downloads/model-00001-of-00005.safetensors", framework="pt") as f:
# Get the tensor

for tensor_name in tensors:
tensor = f.get_tensor(tensor_name)

# Print tensor info
print(f"\nTensor Information:")
print(f"Name: {tensor_name}")
print(f"Shape: {tensor.shape}")
print(f"Type: {tensor.dtype}")
print(f"First 10 values:")
flattened = tensor.flatten()
for i, val in enumerate(flattened[:50]):
print(f"[{i}] = {val}")
53 changes: 53 additions & 0 deletions fundamentals/llama.cpp/scripts/read-tensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python3
import logging
import sys
from pathlib import Path
from gguf.gguf_reader import GGUFReader
import numpy as np

logger = logging.getLogger("reader")

def read_tensor_values(gguf_file_path, tensor_name):
"""
Reads and prints information about a specific tensor from a GGUF file.
Parameters:
- gguf_file_path: Path to the GGUF file
- tensor_name: Name of the tensor to inspect
"""
reader = GGUFReader(gguf_file_path)

# Find the specified tensor
target_tensor = None
for tensor in reader.tensors:
if tensor.name == tensor_name:
target_tensor = tensor
break

if target_tensor is None:
print(f"Tensor '{tensor_name}' not found in the model")
return

# Print tensor information
print(f"\nTensor Information:")
print(f"Name: {target_tensor.name}")
print(f"Shape: {' x '.join(map(str, target_tensor.shape))}")
print(f"Type: {target_tensor.tensor_type.name}")
print(f"Total elements: {target_tensor.n_elements}")

# Get the tensor data
data = target_tensor.data
if isinstance(data, np.ndarray):
print("\nFirst 10 values:")
values = data.flatten()[:100]
for i, value in enumerate(values):
print(f"[{i}] = {value}")

if __name__ == '__main__':
if len(sys.argv) < 3:
print("Usage: reader.py <path_to_gguf_file> <tensor_name>")
sys.exit(1)

gguf_file_path = sys.argv[1]
tensor_name = sys.argv[2]
read_tensor_values(gguf_file_path, tensor_name)

0 comments on commit 4008b3b

Please sign in to comment.