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
54 changes: 54 additions & 0 deletions kt-kernel/.githooks/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/sh
# commit-msg hook to enforce Conventional Commits (https://www.conventionalcommits.org/)
# This script checks the commit message subject (first line) for a conventional commit format.
# If the message does not conform, the hook exits non-zero to block the commit.

# Read the commit message (first line)
if [ -z "$1" ]; then
echo "commit-msg hook: no message file provided" >&2
exit 0
fi

MSG_FILE="$1"
read -r FIRST_LINE < "$MSG_FILE" || FIRST_LINE=""

# Trim leading/trailing whitespace
FIRST_LINE="$(echo "$FIRST_LINE" | sed -e 's/^[ \t]*//' -e 's/[ \t]*$//')"

# Allow empty message (let git handle it), or allow merges/reverts
case "$FIRST_LINE" in
Merge:*|merge:*|Revert:*|revert:*)
exit 0
;;
esac

# Conventional Commit regex (POSIX ERE)
# [type](scope)!?: subject
# types: feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert|wip
# scope: any chars except )

regex='^\[(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert|wip)\](\([^\)]+\))?(!)?: .+'

printf "%s" "$FIRST_LINE" | grep -E "$regex" >/dev/null 2>&1
if [ $? -eq 0 ]; then
exit 0
fi

cat <<'EOF' >&2
ERROR: Commit message does not follow Conventional Commits.

Expected format:
[type](scope)?: subject

Examples:
[feat]: add new feature
[fix(parser)]: handle edge case
Comment on lines +41 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The commit message format description and examples are inconsistent with the validation regex.

  1. The format description on line 41 is missing the optional breaking change indicator !.
  2. The example on line 45 for a commit with a scope, [fix(parser)]: ..., will be rejected by the regex, which expects the scope to be outside the square brackets, like [fix](parser): ....

To ensure the hook works as intended and the guidance is clear, both the description and the example should be aligned with the regex.

Suggested change
[type](scope)?: subject
Examples:
[feat]: add new feature
[fix(parser)]: handle edge case
[type](scope)?[!]?: subject
Examples:
[feat]: add new feature
[fix](parser): handle edge case

[docs]!: update API docs (breaking change)

Allowed types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert, wip

You can bypass this hook locally by running:
git commit --no-verify
EOF

exit 1
63 changes: 63 additions & 0 deletions kt-kernel/.githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/bash
# Pre-commit hook: run clang-format via CMake 'format' target and Black for Python before allowing commit.
# If formatting makes changes, stage them and abort so user can review.
set -euo pipefail

REPO_ROOT="$(git rev-parse --show-toplevel)"
BUILD_DIR="$REPO_ROOT/build"
FORMAT_TARGET="format"
CLANG_FORMAT_BIN="${CLANG_FORMAT_BIN:-clang-format}"
BLACK_BIN="${BLACK_BIN:-black}"

# Simple check clang-format present (optional)
# clang-format optional: if missing, skip C/C++ formatting
if ! command -v "$CLANG_FORMAT_BIN" >/dev/null 2>&1; then
echo "[pre-commit] clang-format not found (looked for $CLANG_FORMAT_BIN). Skipping C/C++ format." >&2
fi

# black optional: if missing, skip Python formatting
if ! command -v "$BLACK_BIN" >/dev/null 2>&1; then
echo "[pre-commit] black not found (looked for $BLACK_BIN). Skipping Python format." >&2
fi

# Configure build directory if missing (quiet)
if [ ! -d "$BUILD_DIR" ] || [ ! -f "$BUILD_DIR/Makefile" ] && [ ! -f "$BUILD_DIR/build.ninja" ]; then
echo "[pre-commit] configuring project (cmake) ..." >&2
cmake -S "$REPO_ROOT" -B "$BUILD_DIR" >/dev/null
fi

# Run format target (prefer ninja if present)
# Run clang-format target when available and tool present
if command -v "$CLANG_FORMAT_BIN" >/dev/null 2>&1; then
if [ -f "$BUILD_DIR/build.ninja" ]; then
(cd "$BUILD_DIR" && ninja -k0 "$FORMAT_TARGET" >/dev/null)
else
(cd "$BUILD_DIR" && make "$FORMAT_TARGET")
fi
fi

# Run black on staged python files (or entire repo if you prefer)
if command -v "$BLACK_BIN" >/dev/null 2>&1; then
# Get staged python files; if none, skip
PY_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.py$' || true)
if [ -n "$PY_FILES" ]; then
echo "[pre-commit] running black on staged python files..." >&2
$BLACK_BIN $PY_FILES
else
# Optionally format all python files; comment out if not desired
# $BLACK_BIN "$REPO_ROOT"
:
fi
Comment on lines +42 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current method of passing python files to black is not robust. It will fail for filenames containing spaces due to word splitting, and it could potentially exceed the shell's argument length limit if many files are staged. Using mapfile (a bash 4+ feature) to read files into an array is a more robust approach that correctly handles spaces in filenames.

Suggested change
PY_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.py$' || true)
if [ -n "$PY_FILES" ]; then
echo "[pre-commit] running black on staged python files..." >&2
$BLACK_BIN $PY_FILES
else
# Optionally format all python files; comment out if not desired
# $BLACK_BIN "$REPO_ROOT"
:
fi
mapfile -t PY_FILES < <(git diff --cached --name-only --diff-filter=ACM | grep -E '\.py$' || true)
if [ ${#PY_FILES[@]} -gt 0 ]; then
echo "[pre-commit] running black on staged python files..." >&2
"$BLACK_BIN" "${PY_FILES[@]}"
fi

fi

# Stage any formatting changes for tracked files
if ! git diff --quiet --exit-code; then
echo "[pre-commit] Formatting applied; updating index." >&2
# Add only modified tracked files (exclude untracked new files not staged yet unless user staged them)
git add -u
echo "[pre-commit] Re-run git commit to proceed after reviewing changes." >&2
exit 1
fi

echo "[pre-commit] format OK." >&2
exit 0
6 changes: 6 additions & 0 deletions kt-kernel/.gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "pybind11"]
path = third_party/pybind11
url = https://github.com/pybind/pybind11.git
[submodule "llama.cpp"]
path = third_party/llama.cpp
url = https://github.com/ggerganov/llama.cpp.git
18 changes: 18 additions & 0 deletions kt-kernel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,34 @@ High-performance kernel operations for KTransformers, featuring CPU-optimized Mo

## Installation

### Prerequisites

First, initialize git submodules:
```bash
git submodule update --init --recursive
```

### Standard Installation
```bash
pip install .
```

All dependencies (torch, safetensors, compressed-tensors, numpy) will be automatically installed from `pyproject.toml`.

### Editable Installation (Development)
```bash
pip install -e .
```

### Optional: Pre-install Dependencies

If you encounter network issues or prefer to install dependencies separately, you can optionally use:
```bash
pip install -r requirements.txt
```

**Note**: This step is **optional**. If your environment already has torch and other required packages, you can skip this and directly run `pip install .`

## Usage

```python
Expand Down
9 changes: 6 additions & 3 deletions kt-kernel/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ classifiers = [
]
requires-python = ">=3.8"
dependencies = [
# Install black by default so git hook can use it
# Core dependencies
"torch>=2.0.0",
"safetensors>=0.4.0",
"compressed-tensors>=0.7.0",
"numpy>=1.24.0",
# Development dependencies
"black>=25.9.0",
"torch",
"safetensors",
]

# No optional dev group needed for formatting; using custom git hooks instead of pre-commit
Expand Down
12 changes: 12 additions & 0 deletions kt-kernel/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Optional: Install these if not already available in your environment
# These dependencies will be automatically installed when running `pip install .`
# You can skip this file if you already have these packages installed

# Core dependencies (minimum versions)
torch>=2.0.0
safetensors>=0.4.0
compressed-tensors>=0.7.0
numpy>=1.24.0

# Development dependencies
black>=25.9.0
1 change: 1 addition & 0 deletions kt-kernel/third_party/llamafile/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The code in this folder is copied from [Mozilla-Ocho/llamafile](https://github.com/Mozilla-Ocho/llamafile). Special thanks to the Mozilla-Ocho team.
25 changes: 25 additions & 0 deletions kt-kernel/third_party/llamafile/bench.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Adapted from
// https://github.com/Mozilla-Ocho/llamafile/blob/0.8.8/llamafile/bench.h
// Copyrigth 2024 Mozilla Foundation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There is a typo in the word "Copyright". This typo appears in the copyright notices of multiple newly added files from third_party/llamafile/.

Suggested change
// Copyrigth 2024 Mozilla Foundation.
// Copyright 2024 Mozilla Foundation.

// Copyright(c) 2024 by KVCache.AI, All Rights Reserved.

// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#pragma once

#include <stdio.h>

#include "micros.h"

#define BENCH(x) \
do { \
x; \
__asm__ volatile("" ::: "memory"); \
long long start = micros(); \
for (int i = 0; i < ITERATIONS; ++i) { \
__asm__ volatile("" ::: "memory"); \
x; \
__asm__ volatile("" ::: "memory"); \
} \
printf("%9lld us %s\n", (micros() - start + ITERATIONS - 1) / ITERATIONS, #x); \
} while (0)
8 changes: 8 additions & 0 deletions kt-kernel/third_party/llamafile/flags.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Adapted from
// https://github.com/Mozilla-Ocho/llamafile/blob/0.8.8/llamafile/flags.cpp
// Copyrigth 2024 Mozilla Foundation.
// Copyright(c) 2024 by KVCache.AI, All Rights Reserved.

#include "flags.h"

bool FLAG_precise = false;
8 changes: 8 additions & 0 deletions kt-kernel/third_party/llamafile/flags.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Adapted from
// https://github.com/Mozilla-Ocho/llamafile/blob/0.8.8/llamafile/flags.cpp
// Copyrigth 2024 Mozilla Foundation.
// Copyright(c) 2024 by KVCache.AI, All Rights Reserved.

#pragma once

extern bool FLAG_precise;
Loading