]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
model-conversion : remove hardcoded /bin/bash shebangs [no ci] (#15765)
authorDaniel Bevenius <redacted>
Wed, 3 Sep 2025 10:50:47 +0000 (12:50 +0200)
committerGitHub <redacted>
Wed, 3 Sep 2025 10:50:47 +0000 (12:50 +0200)
* model-conversion : remove hardcoded /bin/bash shebangs [no ci]

This commit updates the bash scripts to use env instead of using
hardcoded /bin/bash in the shebang line.

The motivation for this is that some systems may have bash installed
in a different location, and using /usr/bin/env bash ensures that
the script will use the first bash interpreter found in the user's
PATH, making the scripts more portable across different environments.

* model-conversion : rename script to .py [no ci]

This commit renames run-casual-gen-embeddings-org.sh to
run-casual-gen-embeddings-org.py to reflect its Python nature.

18 files changed:
examples/model-conversion/Makefile
examples/model-conversion/scripts/causal/compare-embeddings-logits.sh
examples/model-conversion/scripts/causal/convert-model.sh
examples/model-conversion/scripts/causal/run-casual-gen-embeddings-org.py [new file with mode: 0755]
examples/model-conversion/scripts/causal/run-casual-gen-embeddings-org.sh [deleted file]
examples/model-conversion/scripts/causal/run-converted-model-embeddings-logits.sh
examples/model-conversion/scripts/causal/run-converted-model.sh
examples/model-conversion/scripts/embedding/compare-embeddings-logits.sh
examples/model-conversion/scripts/embedding/convert-model.sh
examples/model-conversion/scripts/embedding/run-converted-model.sh
examples/model-conversion/scripts/utils/create-collection-add-model.sh
examples/model-conversion/scripts/utils/curl-embedding-server.sh
examples/model-conversion/scripts/utils/inspect-converted-model.sh
examples/model-conversion/scripts/utils/perplexity-gen.sh
examples/model-conversion/scripts/utils/perplexity-run-simple.sh
examples/model-conversion/scripts/utils/perplexity-run.sh
examples/model-conversion/scripts/utils/quantize.sh
examples/model-conversion/scripts/utils/run-embedding-server.sh

index 03b928afda3e76c84fdde0b558954499b2867d34..ac7a4147297c51707434271640550c5382ed4df8 100644 (file)
@@ -63,7 +63,7 @@ causal-verify-logits: causal-run-original-model causal-run-converted-model
        @MODEL_PATH="$(MODEL_PATH)" ./scripts/utils/check-nmse.py -m ${MODEL_PATH}
 
 causal-run-original-embeddings:
-       @./scripts/causal/run-casual-gen-embeddings-org.sh
+       @./scripts/causal/run-casual-gen-embeddings-org.py
 
 causal-run-converted-embeddings:
        @./scripts/causal/run-converted-model-embeddings-logits.sh
index 287158f63871732b5017f135008b92cf32a4fcd8..c53c89d48acc6049b4478749ebe5f68628b98f09 100755 (executable)
@@ -1,4 +1,4 @@
-#/bin/bash
+#!/usr/bin/env bash
 
 set -e
 
index 9d95025950d42ceff887922a90a98a6b01ad5857..32ffe132e7853d8d3d259f8098d87e5f5bcdddb3 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/usr/bin/env bash
 
 set -e
 
diff --git a/examples/model-conversion/scripts/causal/run-casual-gen-embeddings-org.py b/examples/model-conversion/scripts/causal/run-casual-gen-embeddings-org.py
new file mode 100755 (executable)
index 0000000..2fb54ab
--- /dev/null
@@ -0,0 +1,113 @@
+#!/usr/bin/env python3
+
+import argparse
+import os
+import importlib
+import sys
+import torch
+import numpy as np
+
+from transformers import AutoTokenizer, AutoConfig, AutoModel, AutoModelForCausalLM
+from pathlib import Path
+
+unreleased_model_name = os.getenv('UNRELEASED_MODEL_NAME')
+
+parser = argparse.ArgumentParser(description='Process model with specified path')
+parser.add_argument('--model-path', '-m', help='Path to the model')
+args = parser.parse_args()
+
+model_path = os.environ.get('MODEL_PATH', args.model_path)
+if model_path is None:
+    parser.error("Model path must be specified either via --model-path argument or MODEL_PATH environment variable")
+
+config = AutoConfig.from_pretrained(model_path)
+
+print("Model type:       ", config.model_type)
+print("Vocab size:       ", config.vocab_size)
+print("Hidden size:      ", config.hidden_size)
+print("Number of layers: ", config.num_hidden_layers)
+print("BOS token id:     ", config.bos_token_id)
+print("EOS token id:     ", config.eos_token_id)
+
+print("Loading model and tokenizer using AutoTokenizer:", model_path)
+tokenizer = AutoTokenizer.from_pretrained(model_path)
+
+if unreleased_model_name:
+    model_name_lower = unreleased_model_name.lower()
+    unreleased_module_path = f"transformers.models.{model_name_lower}.modular_{model_name_lower}"
+    class_name = f"{unreleased_model_name}ForCausalLM"
+    print(f"Importing unreleased model module: {unreleased_module_path}")
+
+    try:
+        model_class = getattr(importlib.import_module(unreleased_module_path), class_name)
+        model = model_class.from_pretrained(model_path)
+    except (ImportError, AttributeError) as e:
+        print(f"Failed to import or load model: {e}")
+else:
+    model = AutoModelForCausalLM.from_pretrained(model_path)
+print(f"Model class: {type(model)}")
+#print(f"Model file: {type(model).__module__}")
+
+model_name = os.path.basename(model_path)
+print(f"Model name: {model_name}")
+
+prompt = "Hello world today"
+input_ids = tokenizer(prompt, return_tensors="pt").input_ids
+print(f"Input tokens: {input_ids}")
+print(f"Input text: {repr(prompt)}")
+print(f"Tokenized: {tokenizer.convert_ids_to_tokens(input_ids[0])}")
+
+with torch.no_grad():
+    outputs = model(input_ids, output_hidden_states=True)
+
+    # Extract hidden states from the last layer
+    # outputs.hidden_states is a tuple of (num_layers + 1) tensors
+    # Index -1 gets the last layer, shape: [batch_size, seq_len, hidden_size]
+    last_hidden_states = outputs.hidden_states[-1]
+
+    # Get embeddings for all tokens
+    token_embeddings = last_hidden_states[0].cpu().numpy()  # Remove batch dimension
+
+    print(f"Hidden states shape: {last_hidden_states.shape}")
+    print(f"Token embeddings shape: {token_embeddings.shape}")
+    print(f"Hidden dimension: {token_embeddings.shape[-1]}")
+    print(f"Number of tokens: {token_embeddings.shape[0]}")
+
+    # Save raw token embeddings
+    data_dir = Path("data")
+    data_dir.mkdir(exist_ok=True)
+    bin_filename = data_dir / f"pytorch-{model_name}-embeddings.bin"
+    txt_filename = data_dir / f"pytorch-{model_name}-embeddings.txt"
+
+    # Save all token embeddings as binary
+    print(token_embeddings)
+    token_embeddings.astype(np.float32).tofile(bin_filename)
+
+    # Save as text for inspection
+    with open(txt_filename, "w") as f:
+        for i, embedding in enumerate(token_embeddings):
+            for j, val in enumerate(embedding):
+                f.write(f"{i} {j} {val:.6f}\n")
+
+    # Print embeddings per token in the requested format
+    print("\nToken embeddings:")
+    tokens = tokenizer.convert_ids_to_tokens(input_ids[0])
+    for i, embedding in enumerate(token_embeddings):
+        # Format: show first few values, ..., then last few values
+        if len(embedding) > 10:
+            # Show first 3 and last 3 values with ... in between
+            first_vals = " ".join(f"{val:8.6f}" for val in embedding[:3])
+            last_vals = " ".join(f"{val:8.6f}" for val in embedding[-3:])
+            print(f"embedding {i}: {first_vals}  ... {last_vals}")
+        else:
+            # If embedding is short, show all values
+            vals = " ".join(f"{val:8.6f}" for val in embedding)
+            print(f"embedding {i}: {vals}")
+
+    # Also show token info for reference
+    print(f"\nToken reference:")
+    for i, token in enumerate(tokens):
+        print(f"  Token {i}: {repr(token)}")
+
+    print(f"Saved bin logits to: {bin_filename}")
+    print(f"Saved txt logist to: {txt_filename}")
diff --git a/examples/model-conversion/scripts/causal/run-casual-gen-embeddings-org.sh b/examples/model-conversion/scripts/causal/run-casual-gen-embeddings-org.sh
deleted file mode 100755 (executable)
index 2fb54ab..0000000
+++ /dev/null
@@ -1,113 +0,0 @@
-#!/usr/bin/env python3
-
-import argparse
-import os
-import importlib
-import sys
-import torch
-import numpy as np
-
-from transformers import AutoTokenizer, AutoConfig, AutoModel, AutoModelForCausalLM
-from pathlib import Path
-
-unreleased_model_name = os.getenv('UNRELEASED_MODEL_NAME')
-
-parser = argparse.ArgumentParser(description='Process model with specified path')
-parser.add_argument('--model-path', '-m', help='Path to the model')
-args = parser.parse_args()
-
-model_path = os.environ.get('MODEL_PATH', args.model_path)
-if model_path is None:
-    parser.error("Model path must be specified either via --model-path argument or MODEL_PATH environment variable")
-
-config = AutoConfig.from_pretrained(model_path)
-
-print("Model type:       ", config.model_type)
-print("Vocab size:       ", config.vocab_size)
-print("Hidden size:      ", config.hidden_size)
-print("Number of layers: ", config.num_hidden_layers)
-print("BOS token id:     ", config.bos_token_id)
-print("EOS token id:     ", config.eos_token_id)
-
-print("Loading model and tokenizer using AutoTokenizer:", model_path)
-tokenizer = AutoTokenizer.from_pretrained(model_path)
-
-if unreleased_model_name:
-    model_name_lower = unreleased_model_name.lower()
-    unreleased_module_path = f"transformers.models.{model_name_lower}.modular_{model_name_lower}"
-    class_name = f"{unreleased_model_name}ForCausalLM"
-    print(f"Importing unreleased model module: {unreleased_module_path}")
-
-    try:
-        model_class = getattr(importlib.import_module(unreleased_module_path), class_name)
-        model = model_class.from_pretrained(model_path)
-    except (ImportError, AttributeError) as e:
-        print(f"Failed to import or load model: {e}")
-else:
-    model = AutoModelForCausalLM.from_pretrained(model_path)
-print(f"Model class: {type(model)}")
-#print(f"Model file: {type(model).__module__}")
-
-model_name = os.path.basename(model_path)
-print(f"Model name: {model_name}")
-
-prompt = "Hello world today"
-input_ids = tokenizer(prompt, return_tensors="pt").input_ids
-print(f"Input tokens: {input_ids}")
-print(f"Input text: {repr(prompt)}")
-print(f"Tokenized: {tokenizer.convert_ids_to_tokens(input_ids[0])}")
-
-with torch.no_grad():
-    outputs = model(input_ids, output_hidden_states=True)
-
-    # Extract hidden states from the last layer
-    # outputs.hidden_states is a tuple of (num_layers + 1) tensors
-    # Index -1 gets the last layer, shape: [batch_size, seq_len, hidden_size]
-    last_hidden_states = outputs.hidden_states[-1]
-
-    # Get embeddings for all tokens
-    token_embeddings = last_hidden_states[0].cpu().numpy()  # Remove batch dimension
-
-    print(f"Hidden states shape: {last_hidden_states.shape}")
-    print(f"Token embeddings shape: {token_embeddings.shape}")
-    print(f"Hidden dimension: {token_embeddings.shape[-1]}")
-    print(f"Number of tokens: {token_embeddings.shape[0]}")
-
-    # Save raw token embeddings
-    data_dir = Path("data")
-    data_dir.mkdir(exist_ok=True)
-    bin_filename = data_dir / f"pytorch-{model_name}-embeddings.bin"
-    txt_filename = data_dir / f"pytorch-{model_name}-embeddings.txt"
-
-    # Save all token embeddings as binary
-    print(token_embeddings)
-    token_embeddings.astype(np.float32).tofile(bin_filename)
-
-    # Save as text for inspection
-    with open(txt_filename, "w") as f:
-        for i, embedding in enumerate(token_embeddings):
-            for j, val in enumerate(embedding):
-                f.write(f"{i} {j} {val:.6f}\n")
-
-    # Print embeddings per token in the requested format
-    print("\nToken embeddings:")
-    tokens = tokenizer.convert_ids_to_tokens(input_ids[0])
-    for i, embedding in enumerate(token_embeddings):
-        # Format: show first few values, ..., then last few values
-        if len(embedding) > 10:
-            # Show first 3 and last 3 values with ... in between
-            first_vals = " ".join(f"{val:8.6f}" for val in embedding[:3])
-            last_vals = " ".join(f"{val:8.6f}" for val in embedding[-3:])
-            print(f"embedding {i}: {first_vals}  ... {last_vals}")
-        else:
-            # If embedding is short, show all values
-            vals = " ".join(f"{val:8.6f}" for val in embedding)
-            print(f"embedding {i}: {vals}")
-
-    # Also show token info for reference
-    print(f"\nToken reference:")
-    for i, token in enumerate(tokens):
-        print(f"  Token {i}: {repr(token)}")
-
-    print(f"Saved bin logits to: {bin_filename}")
-    print(f"Saved txt logist to: {txt_filename}")
index e2762729e76a37a4f8e891347359f4f60cb840e1..f5f567d4ffa1282d315725ca497078a33ede2387 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/usr/bin/env bash
 
 set -e
 
index 0609e35357bd40267cc2dfde20be1b5df3b1ddfd..0929e42413e6723eb827204dc2fd770e2a3c4075 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/usr/bin/env bash
 
 set -e
 
index 5896090411a79d56337c734de9fca80a8c3aa047..24b28106275dfc80729cf33f711e17220327c2ab 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/usr/bin/env bash
 
 set -e
 
index 4809da6cb6fac1e96e873fe63096106847cfff6a..485001b5feeccefff460fc961415cd702acbd19e 100644 (file)
@@ -1,4 +1,6 @@
 
+#!/usr/bin/env bash
+
 COLLECTION_SLUG=$(python ./create_collection.py --return-slug)
 echo "Created collection: $COLLECTION_SLUG"
 
index b6898665faa9a49d19a9ce3ef9aed2fe5d013419..7ed69e1ea50f553954fd23e5b89491f5ffa7a068 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/usr/bin/env bash
 curl --request POST \
     --url http://localhost:8080/embedding \
     --header "Content-Type: application/json" \
index e5b9324542e7321eb14c803621a19a10e7390dfa..32d84826fa089fb2c2ca436d5a72b46b6d713f15 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/usr/bin/env bash
 
 # First try command line argument, then environment variable, then file
 CONVERTED_MODEL="${1:-"$CONVERTED_MODEL"}"
index 3db0b3fd27ac37cc69f65158d1fad880c5edd1c0..4885acbae24d171a8ac1b5fbc65dd3f7158dac0f 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/usr/bin/env bash
 
 set -e
 
index 69b3438f598b3c61a270f9c7e94cc0af0777acc9..a2545436a5c52e956cb61f3e973f633aaa947d7f 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/usr/bin/env bash
 
 set -e
 
index 3bce7c8472153ba53cf6a176dbafaa01d89111ba..68b38e662859b27013ec69617f0c8d620a8856e1 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/usr/bin/env bash
 
 set -e
 
index 90460aa6b0010986eda1b1630f99e796f3d6294a..c25c5c21f3c3e3c9711b23e84cff01c4481f71b1 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/usr/bin/env bash
 
 set -e
 
index 828fc4706977214cd2a05db2177922bf228e5921..d30b765964b0cc6cc2d5edf6dc00d9f44dd3a9b5 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/usr/bin/env bash
 
 set -e
 #