Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ endif()
if(NOT KIWI_CPU_ARCH)
if (EMSCRIPTEN)
set(KIWI_CPU_ARCH "wasm")
# Apple cross builds (iOS, simulator) don't set CMAKE_SYSTEM_PROCESSOR to the target, so a single
# target architecture in CMAKE_OSX_ARCHITECTURES takes precedence. Multi-arch values like "arm64;x86_64"
# can't be handled by one configuration; build each architecture separately and merge them with lipo.
elseif (APPLE AND CMAKE_OSX_ARCHITECTURES MATCHES "^arm64e?$")
set(KIWI_CPU_ARCH "arm64")
elseif (APPLE AND CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64")
set(KIWI_CPU_ARCH "x86_64")
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)"
OR "${ANDROID_ABI}" STREQUAL "x86_64")
set(KIWI_CPU_ARCH "x86_64")
Expand Down Expand Up @@ -233,7 +240,8 @@ elseif (KIWI_CPU_ARCH MATCHES "arm64")
${CORE_SRCS}
src/archImpl/neon.cpp
)
set_source_files_properties(src/archImpl/neon.cpp PROPERTIES COMPILE_FLAGS "-march=armv8-a -DKIWI_ARCH_ARM64=1")
set_source_files_properties(src/archImpl/neon.cpp PROPERTIES COMPILE_FLAGS "-march=armv8-a")
add_compile_definitions(KIWI_ARCH_ARM64=1)
elseif (KIWI_CPU_ARCH MATCHES "wasm")
message("Compiling for wasm")
else()
Expand Down
48 changes: 38 additions & 10 deletions bindings/swift/scripts/build-xcframework.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,42 @@ build_platform() {

cmake --build . --config Release

# Create framework structure
local FRAMEWORK_DIR="$PLATFORM_BUILD_DIR/Kiwi.framework"
mkdir -p "$FRAMEWORK_DIR/Headers"

# Copy library
# Use find to locate the library because Xcode puts it in different places
# depending on the platform (e.g., Release-iphoneos, Release-iphonesimulator, Release)
local LIB_FILE=$(find . -name "libkiwi_static.a" | grep "Release" | head -n 1)
if [ -z "$LIB_FILE" ]; then
echo "Error: libkiwi_static.a not found in $PLATFORM_BUILD_DIR"
exit 1
fi
cp "$LIB_FILE" "$FRAMEWORK_DIR/Kiwi"
cp "$LIB_FILE" "$PLATFORM_BUILD_DIR/libkiwi.a"
echo "✓ Built $PLATFORM ($ARCHS)"
}

# Create a framework from one or more single-architecture builds.
# Each architecture is configured separately because the arch-specific sources and definitions
# (e.g. NEON kernels and KIWI_ARCH_ARM64) are chosen per configuration; they are merged with lipo here.
make_framework() {
local BUILD_SUBDIR=$1
shift

local PLATFORM_BUILD_DIR="$BUILD_DIR/$BUILD_SUBDIR"
mkdir -p "$PLATFORM_BUILD_DIR"
cd "$PLATFORM_BUILD_DIR"

# Create framework structure
local FRAMEWORK_DIR="$PLATFORM_BUILD_DIR/Kiwi.framework"
mkdir -p "$FRAMEWORK_DIR/Headers"

# Copy library
local LIBS=()
for SUB in "$@"; do
LIBS+=("$BUILD_DIR/$SUB/libkiwi.a")
done
if [ ${#LIBS[@]} -eq 1 ]; then
cp "${LIBS[0]}" "$FRAMEWORK_DIR/Kiwi"
else
lipo -create "${LIBS[@]}" -output "$FRAMEWORK_DIR/Kiwi"
fi

# Copy headers
cp "$PROJECT_ROOT/include/kiwi/capi.h" "$FRAMEWORK_DIR/Headers/"
Expand All @@ -80,17 +103,22 @@ framework module Kiwi {
}
EOF

echo "✓ Built $PLATFORM"
echo "✓ Created $BUILD_SUBDIR framework"
}

# Build for iOS Device (arm64)
build_platform "iOS" "iphoneos" "arm64" "12.0" "ios-arm64"
build_platform "iOS" "iphoneos" "arm64" "12.0" "ios-arm64-lib"
make_framework "ios-arm64" "ios-arm64-lib"

# Build for iOS Simulator (arm64 + x86_64)
build_platform "iOS" "iphonesimulator" "arm64;x86_64" "12.0" "ios-simulator"
build_platform "iOS" "iphonesimulator" "arm64" "12.0" "ios-simulator-arm64"
build_platform "iOS" "iphonesimulator" "x86_64" "12.0" "ios-simulator-x86_64"
make_framework "ios-simulator" "ios-simulator-arm64" "ios-simulator-x86_64"

# Build for macOS (arm64 + x86_64 universal)
build_platform "Darwin" "macosx" "arm64;x86_64" "10.14" "macos"
build_platform "Darwin" "macosx" "arm64" "10.14" "macos-arm64"
build_platform "Darwin" "macosx" "x86_64" "10.14" "macos-x86_64"
make_framework "macos" "macos-arm64" "macos-x86_64"

# Create XCFramework
echo "Creating XCFramework..."
Expand Down
2 changes: 1 addition & 1 deletion src/ArchUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace kiwi
{
return arch;
}
#elif CPUINFO_ARCH_ARM || CPUINFO_ARCH_ARM64 || KIWI_ARCH_NEON
#elif CPUINFO_ARCH_ARM || CPUINFO_ARCH_ARM64 || KIWI_ARCH_ARM64
if (ArchType::neon <= arch && arch <= ArchType::neon && arch <= best)
{
return arch;
Expand Down
8 changes: 5 additions & 3 deletions src/CoNgramModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2914,13 +2914,15 @@ namespace kiwi
case 7:
return make_unique<CoNgramModel<archType, KeyTy, VlKeyType, 7, quantized>>(std::move(mem));
default:
throw runtime_error{ "Unsupported `window_size` : " + to_string((size_t)header.windowSize) };
throw SerializationException{ "Unsupported `window_size` : " + to_string((size_t)header.windowSize) };
};
}

template<ArchType archType, bool useDistantTokens, bool quantized>
unique_ptr<CoNgramModelBase> createOptimizedModel(utils::MemoryObject&& mem)
{
if (mem.size() < sizeof(CoNgramModelHeader))
throw SerializationException{ "Invalid model file" };
auto& header = *reinterpret_cast<const CoNgramModelHeader*>(mem.get());
switch (header.keySize)
{
Expand All @@ -2933,7 +2935,7 @@ namespace kiwi
case 4:
return createOptimizedModelWithWindowSize<archType, uint32_t, uint32_t, useDistantTokens, quantized>(std::move(mem));
default:
throw runtime_error{ "Unsupported `key_size` : " + to_string((size_t)header.keySize) };
throw SerializationException{ "Unsupported `key_size` : " + to_string((size_t)header.keySize) };
}
}

Expand Down Expand Up @@ -2967,7 +2969,7 @@ namespace kiwi
cerr << "Quantization is not supported for ArchType::" << archToStr(archType) << ". Fall back to non-quantized model." << endl;
}
auto fn = tables[useDistantTokens ? 1 : 0][static_cast<ptrdiff_t>(archType)];
if (!fn) throw runtime_error{ string{"Unsupported architecture : "} + archToStr(archType) };
if (!fn) throw SerializationException{ string{"Unsupported architecture : "} + archToStr(archType) };
return (*fn)(std::move(mem));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/CoNgramModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace kiwi
}

template<size_t windowSize, ArchType _arch, class VocabTy, class VlVocabTy, bool quantized>
class CoNgramState;
struct CoNgramState;

template<ArchType arch, class KeyType, class VlKeyType, size_t windowSize, bool quantized = true>
class CoNgramModel : public CoNgramModelBase
Expand Down
Loading