Skip to content

Commit d9321bf

Browse files
committed
build scripts
1 parent 9997471 commit d9321bf

File tree

6 files changed

+734
-188
lines changed

6 files changed

+734
-188
lines changed

Diff for: darwin/fix_rpath.sh

+36-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
#!/bin/bash
22

33
# Set the directory where your dylib files are located.
4-
LIB_DIR="../bin/MAC_ARM64"
4+
if [ $# -lt 1 ]; then
5+
echo "Usage: $0 <platform> (MAC_ARM64, OS64, SIMULATOR64, or SIMULATORARM64)"
6+
exit 1
7+
else
8+
PLATFORM="$1"
9+
fi
10+
11+
LIB_DIR="../bin/${PLATFORM}"
512

613
fix_rpaths() {
714
local lib_file="$1"
@@ -13,8 +20,29 @@ fix_rpaths() {
1320

1421
echo "Fixing rpaths for: $lib_file"
1522

16-
dependencies=$(otool -L "$lib_file" | grep @rpath | awk '{print $1}')
23+
# Get the absolute path for better rpath handling
24+
ABSOLUTE_LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/${LIB_DIR}" && pwd)"
25+
26+
# First clear existing rpaths to avoid duplicates
27+
existing_rpaths=$(otool -l "$lib_file" | grep -A2 LC_RPATH | grep path | awk '{print $2}')
28+
for rpath in $existing_rpaths; do
29+
echo "Removing existing rpath: $rpath"
30+
install_name_tool -delete_rpath "$rpath" "$lib_file" || true
31+
done
32+
33+
# Add standard framework paths
34+
install_name_tool -add_rpath "@executable_path/Frameworks" "$lib_file"
35+
install_name_tool -add_rpath "@loader_path/Frameworks" "$lib_file"
36+
37+
# Now add the absolute path
38+
install_name_tool -add_rpath "$ABSOLUTE_LIB_DIR" "$lib_file"
39+
if [ $? -ne 0 ]; then
40+
echo "Error: install_name_tool -add_rpath failed for $lib_file"
41+
exit 1
42+
fi
1743

44+
# Fix dependencies
45+
dependencies=$(otool -L "$lib_file" | grep @rpath | awk '{print $1}')
1846
for dep in $dependencies; do
1947
dep_name=$(echo "$dep" | awk -F/ '{print $NF}' | awk '{print $1}')
2048
install_name_tool -change "$dep" "@rpath/$dep_name" "$lib_file"
@@ -24,32 +52,27 @@ fix_rpaths() {
2452
fi
2553
done
2654

27-
install_name_tool -add_rpath "$PWD/$LIB_DIR" "$lib_file"
28-
if [ $? -ne 0 ]; then
29-
echo "Error: install_name_tool -add_rpath failed for $lib_file"
30-
exit 1
31-
fi
32-
3355
echo "Verifying changes for $lib_file:"
3456
otool -L "$lib_file"
57+
echo "RPaths:"
58+
otool -l "$lib_file" | grep -A2 LC_RPATH
3559
echo "-------------------------"
3660
}
3761

3862
if [ ! -d "$LIB_DIR" ]; then
39-
echo "Error: Library directory '$LIB_DIR' not found. Run this script from the project root."
63+
echo "Error: Library directory '$LIB_DIR' not found. Run this script from the project root."
4064
exit 1
4165
fi
4266

67+
# Main libraries
4368
fix_rpaths "$LIB_DIR/libllama.dylib"
44-
4569
fix_rpaths "$LIB_DIR/libggml.dylib"
4670

71+
# Component libraries
4772
for lib in "$LIB_DIR/libggml-"*.dylib; do
4873
fix_rpaths "$lib"
4974
done
5075

51-
52-
5376
echo "Re-signing libraries..."
5477
codesign -s - -f "$LIB_DIR"/*.dylib
5578
if [ $? -ne 0 ]; then
@@ -59,6 +82,6 @@ fi
5982

6083
chmod +x "$LIB_DIR"/*.dylib
6184

62-
echo "Rpath fixing and re-signing complete."
85+
echo "Rpath fixing and re-signing complete for ${PLATFORM}."
6386

6487
exit 0

Diff for: darwin/run_build.sh

+9-10
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@ build_for_platform() {
1919
local output_dir="${output_base_dir}/bin/${platform}"
2020
local shared_libs="ON"
2121
local lib_extension="dylib"
22+
local deployment_target=13.1
2223

2324
local postfix=""
24-
if [[ "$platform" == "SIMULATORARM64" ]]; then
25+
if [[ "$platform" == "SIMULATORARM64" || "$platform" == "SIMULATOR64" ]]; then
2526
postfix="-iphonesimulator"
2627
fi
2728
if [[ "$platform" == "OS64" ]]; then
2829
postfix="-iphoneos"
30+
deployment_target=13.1 # Set higher deployment target for iOS platforms
2931
fi
3032

31-
echo "Building for platform: ${platform}"
33+
echo "Building for platform: ${platform} with deployment target iOS ${deployment_target}"
3234

3335
rm -rf "${build_dir}"
3436
mkdir -p "${build_dir}"
@@ -43,7 +45,7 @@ build_for_platform() {
4345
-G Xcode \
4446
-DCMAKE_TOOLCHAIN_FILE="${script_dir}/ios-arm64.toolchain.cmake" \
4547
-DPLATFORM="${platform}" \
46-
-DDEPLOYMENT_TARGET=12 \
48+
-DDEPLOYMENT_TARGET=${deployment_target} \
4749
-DENABLE_BITCODE=0 \
4850
-DENABLE_ARC=0 \
4951
-DENABLE_VISIBILITY=1 \
@@ -78,20 +80,17 @@ build_for_platform() {
7880
}
7981

8082
main() {
83+
# Check if a specific platform was provided as a third argument
84+
local platform=${3:-"MAC_ARM64"}
85+
8186
cp "${script_dir}/ios-arm64.toolchain.cmake" "${llama_cpp_path}/"
8287

8388
pushd "${llama_cpp_path}" > /dev/null
8489

85-
build_for_platform "MAC_ARM64"
86-
# build_for_platform "OS64"
87-
# build_for_platform "SIMULATORARM64"
90+
build_for_platform "${platform}"
8891

8992
# return to original directory
9093
popd > /dev/null
91-
92-
rm -rf "${output_base_dir}/include"
93-
mkdir -p "${output_base_dir}/include"
94-
cp -r "${llama_cpp_path}/build_MAC_ARM64/install/include/"* "${output_base_dir}/include/"
9594
}
9695

9796
main "$@"

Diff for: diagnose_libraries.sh

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/bin/bash
2+
3+
# Script to diagnose differences between working and non-working libraries
4+
# Usage: ./diagnose_libraries.sh
5+
6+
# Exit on error
7+
set -e
8+
9+
# Colors for output
10+
RED='\033[0;31m'
11+
GREEN='\033[0;32m'
12+
BLUE='\033[0;34m'
13+
YELLOW='\033[1;33m'
14+
NC='\033[0m' # No Color
15+
16+
# Define directories
17+
WORKING_DIR="bin/MAC_ARM64.old"
18+
NEW_DIR="bin/MAC_ARM64"
19+
20+
if [ ! -d "$WORKING_DIR" ] || [ ! -d "$NEW_DIR" ]; then
21+
echo -e "${RED}Error: Both $WORKING_DIR and $NEW_DIR must exist${NC}"
22+
exit 1
23+
fi
24+
25+
echo -e "${GREEN}Comparing library files between working ($WORKING_DIR) and new ($NEW_DIR) directories${NC}"
26+
echo
27+
28+
# Function to examine a specific library
29+
examine_library() {
30+
local lib_name=$1
31+
local working_lib="$WORKING_DIR/$lib_name"
32+
local new_lib="$NEW_DIR/$lib_name"
33+
34+
if [ ! -f "$working_lib" ] || [ ! -f "$new_lib" ]; then
35+
echo -e "${RED}Error: $lib_name not found in both directories${NC}"
36+
return
37+
fi
38+
39+
echo -e "${BLUE}=== Examining $lib_name ===${NC}"
40+
41+
# Compare file sizes
42+
local working_size=$(stat -f "%z" "$working_lib")
43+
local new_size=$(stat -f "%z" "$new_lib")
44+
45+
echo -e "${YELLOW}File sizes:${NC}"
46+
echo "Working: $working_size bytes"
47+
echo "New: $new_size bytes"
48+
echo
49+
50+
# Compare otool -L output (dependencies)
51+
echo -e "${YELLOW}Dependencies (otool -L):${NC}"
52+
echo -e "${YELLOW}Working:${NC}"
53+
otool -L "$working_lib" | sed 's/^/ /'
54+
echo -e "${YELLOW}New:${NC}"
55+
otool -L "$new_lib" | sed 's/^/ /'
56+
echo
57+
58+
# Compare rpaths
59+
echo -e "${YELLOW}RPaths (otool -l | grep LC_RPATH -A2):${NC}"
60+
echo -e "${YELLOW}Working:${NC}"
61+
otool -l "$working_lib" | grep -A2 LC_RPATH | sed 's/^/ /'
62+
echo -e "${YELLOW}New:${NC}"
63+
otool -l "$new_lib" | grep -A2 LC_RPATH | sed 's/^/ /'
64+
echo
65+
66+
# Check codesign
67+
echo -e "${YELLOW}Code Signature:${NC}"
68+
echo -e "${YELLOW}Working:${NC}"
69+
codesign -vv "$working_lib" 2>&1 | sed 's/^/ /'
70+
echo -e "${YELLOW}New:${NC}"
71+
codesign -vv "$new_lib" 2>&1 | sed 's/^/ /'
72+
echo
73+
74+
# Check install names
75+
echo -e "${YELLOW}Install Name (otool -D):${NC}"
76+
echo -e "${YELLOW}Working:${NC}"
77+
otool -D "$working_lib" | sed 's/^/ /'
78+
echo -e "${YELLOW}New:${NC}"
79+
otool -D "$new_lib" | sed 's/^/ /'
80+
echo
81+
82+
# Check compatibility version
83+
echo -e "${YELLOW}Compatibility Version:${NC}"
84+
echo -e "${YELLOW}Working:${NC}"
85+
otool -l "$working_lib" | grep -A4 LC_ID_DYLIB | grep compat | sed 's/^/ /'
86+
echo -e "${YELLOW}New:${NC}"
87+
otool -l "$new_lib" | grep -A4 LC_ID_DYLIB | grep compat | sed 's/^/ /'
88+
echo
89+
90+
# Basic information about the binary
91+
echo -e "${YELLOW}File Info:${NC}"
92+
echo -e "${YELLOW}Working:${NC}"
93+
file "$working_lib" | sed 's/^/ /'
94+
echo -e "${YELLOW}New:${NC}"
95+
file "$new_lib" | sed 's/^/ /'
96+
echo
97+
98+
echo -e "${BLUE}=== End of $lib_name examination ===${NC}"
99+
echo "-------------------------------------------------------------"
100+
echo
101+
}
102+
103+
# Examine main libraries
104+
examine_library "libllama.dylib"
105+
examine_library "libggml.dylib"
106+
examine_library "libggml-metal.dylib"
107+
108+
echo -e "${GREEN}Diagnosis complete.${NC}"
109+
echo
110+
echo "If you suspect a specific issue with the fix_rpath.sh script, try running:"
111+
echo "otool -l bin/MAC_ARM64/libllama.dylib | grep -A2 LC_RPATH"
112+
echo
113+
echo "Also check if your dart example needs additional environment variables:"
114+
echo "DYLD_PRINT_LIBRARIES=1 DYLD_PRINT_RPATHS=1 dart example/simple.dart"
115+
echo
116+
echo "This will show library loading details and might reveal the issue."

Diff for: example/simple.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ void main() {
1212
int nPredict = 32;
1313

1414
// Load library
15-
final lib = llama_cpp(DynamicLibrary.open("bin/MAC_ARM64/libllama.dylib"));
15+
final lib = llama_cpp(DynamicLibrary.open(
16+
"/Users/adel/Workspace/llama_cpp_dart/bin/MAC_ARM64/libllama.dylib"));
1617
lib.llama_backend_init();
1718

1819
// Initialize model

Diff for: lib/src/llama.dart

+3-10
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ class Llama {
488488
throw ArgumentError('Prompt cannot be empty');
489489
}
490490

491-
llama_batch? promptBatch = null;
491+
llama_batch? promptBatch;
492492

493493
try {
494494
// Tokenize the input text
@@ -498,9 +498,6 @@ class Llama {
498498
// Check if token count exceeds batch size
499499
int batchSize = _contextParams?.nBatch ?? 512;
500500
if (nTokens > batchSize) {
501-
print(
502-
"Warning: Input length (${nTokens} tokens) exceeds batch size (${batchSize})");
503-
print("Trimming input to fit batch size...");
504501
tokens = tokens.sublist(0, batchSize - 1);
505502
nTokens = tokens.length;
506503
}
@@ -524,12 +521,8 @@ class Llama {
524521

525522
// Process the batch
526523
bool isEncoderOnly = false;
527-
try {
528-
isEncoderOnly = lib.llama_model_has_encoder(model) &&
529-
!lib.llama_model_has_decoder(model);
530-
} catch (e) {
531-
print("Warning: Could not determine model type: $e");
532-
}
524+
isEncoderOnly = lib.llama_model_has_encoder(model) &&
525+
!lib.llama_model_has_decoder(model);
533526

534527
if (isEncoderOnly) {
535528
if (lib.llama_encode(context, promptBatch) != 0) {

0 commit comments

Comments
 (0)