From 4008b3bf610cccecba79d19c5eb637009e2e3893 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Sun, 1 Dec 2024 07:33:06 +0100 Subject: [PATCH] llama.cpp: add scripts to read tensor values --- .../llama.cpp/scripts/read-pre-pos-emb.py | 70 +++++++++++++++++++ .../llama.cpp/scripts/read-safetensor.py | 33 +++++++++ fundamentals/llama.cpp/scripts/read-tensor.py | 53 ++++++++++++++ 3 files changed, 156 insertions(+) create mode 100755 fundamentals/llama.cpp/scripts/read-pre-pos-emb.py create mode 100644 fundamentals/llama.cpp/scripts/read-safetensor.py create mode 100755 fundamentals/llama.cpp/scripts/read-tensor.py diff --git a/fundamentals/llama.cpp/scripts/read-pre-pos-emb.py b/fundamentals/llama.cpp/scripts/read-pre-pos-emb.py new file mode 100755 index 0000000..56e210f --- /dev/null +++ b/fundamentals/llama.cpp/scripts/read-pre-pos-emb.py @@ -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 [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 + ) diff --git a/fundamentals/llama.cpp/scripts/read-safetensor.py b/fundamentals/llama.cpp/scripts/read-safetensor.py new file mode 100644 index 0000000..9ba77b7 --- /dev/null +++ b/fundamentals/llama.cpp/scripts/read-safetensor.py @@ -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}") diff --git a/fundamentals/llama.cpp/scripts/read-tensor.py b/fundamentals/llama.cpp/scripts/read-tensor.py new file mode 100755 index 0000000..22a9623 --- /dev/null +++ b/fundamentals/llama.cpp/scripts/read-tensor.py @@ -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 ") + sys.exit(1) + + gguf_file_path = sys.argv[1] + tensor_name = sys.argv[2] + read_tensor_values(gguf_file_path, tensor_name)