Skip to content

Commit 1138b85

Browse files
authored
model-conversion : use save_output_data for causual embeddings [no ci] (ggml-org#26890)
This commit updates the python script that runs the original model to generate embeddings for the causal model, to use save_output_data which stores the token ids and the prompt in addition to logits. The motivation for this is that the embedding logits verification will fail as it expects these files (-prompt.txt and -tokens.bin) to exist. With the changes in this commit the causal-verify-embeddings target works again.
1 parent 9afff1b commit 1138b85

1 file changed

Lines changed: 6 additions & 17 deletions

File tree

‎examples/model-conversion/scripts/causal/run-casual-gen-embeddings-org.py‎

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import argparse
44
import os
5+
import sys
56
import importlib
67
import torch
78
import numpy as np
89

910
from transformers import AutoTokenizer, AutoConfig, AutoModelForCausalLM
10-
from pathlib import Path
11+
12+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
13+
from utils.common import save_output_data
1114

1215
unreleased_model_name = os.getenv('UNRELEASED_MODEL_NAME')
1316

@@ -54,6 +57,7 @@
5457

5558
prompt = "Hello world today"
5659
input_ids = tokenizer(prompt, return_tensors="pt").input_ids # ty: ignore[call-non-callable]
60+
token_ids = input_ids[0].cpu().tolist()
5761
print(f"Input tokens: {input_ids}")
5862
print(f"Input text: {repr(prompt)}")
5963
print(f"Tokenized: {tokenizer.convert_ids_to_tokens(input_ids[0])}") # ty: ignore[unresolved-attribute]
@@ -74,21 +78,8 @@
7478
print(f"Hidden dimension: {token_embeddings.shape[-1]}")
7579
print(f"Number of tokens: {token_embeddings.shape[0]}")
7680

77-
# Save raw token embeddings
78-
data_dir = Path("data")
79-
data_dir.mkdir(exist_ok=True)
80-
bin_filename = data_dir / f"pytorch-{model_name}-embeddings.bin"
81-
txt_filename = data_dir / f"pytorch-{model_name}-embeddings.txt"
82-
83-
# Save all token embeddings as binary
8481
print(token_embeddings)
85-
token_embeddings.astype(np.float32).tofile(bin_filename)
86-
87-
# Save as text for inspection
88-
with open(txt_filename, "w") as f:
89-
for i, embedding in enumerate(token_embeddings):
90-
for j, val in enumerate(embedding):
91-
f.write(f"{i} {j} {val:.6f}\n")
82+
save_output_data(token_embeddings, token_ids, prompt, model_name, type_suffix="-embeddings")
9283

9384
# Print embeddings per token in the requested format
9485
print("\nToken embeddings:")
@@ -110,5 +101,3 @@
110101
for i, token in enumerate(tokens):
111102
print(f" Token {i}: {repr(token)}")
112103

113-
print(f"Saved bin logits to: {bin_filename}")
114-
print(f"Saved txt logist to: {txt_filename}")

0 commit comments

Comments
 (0)