Skip to content

Commit aef6672

Browse files
authored
[docs]: add contribuing guide and add hooks install (#1613)
* [feat]: update kt-kernel hooks and add contribution guide * [docs]: add contributing guide * [style]: format the python file and cpp file in kt-kernel
1 parent c32fefb commit aef6672

File tree

11 files changed

+288
-163
lines changed

11 files changed

+288
-163
lines changed

.github/CONTRIBUTING.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
## Before Commit!
2+
3+
Your commit message must follow Conventional Commits (https://www.conventionalcommits.org/) and your code should be formatted. The Git hooks will do most of the work automatically:
4+
5+
### Tool Requirements
6+
7+
You need a recent `clang-format` (>= 18). In a conda environment you can install:
8+
9+
```shell
10+
conda install -c conda-forge clang-format=18
11+
```
12+
13+
If you previously configured with an older version, remove the build directory and reconfigure:
14+
15+
```shell
16+
rm -rf kt-kernel/build
17+
```
18+
19+
Install `black` for Python formatting:
20+
21+
```shell
22+
conda install black
23+
```
24+
25+
### Install hook:
26+
```shell
27+
bash kt-kernel/scripts/install-git-hooks.sh
28+
#or just cmake the kt-kernel
29+
cmake -S kt-kernel -B kt-kernel/build
30+
```
31+
32+
There are manual commands if you need format.
33+
34+
```shell
35+
cmake -S kt-kernel -B kt-kernel/build
36+
cmake --build kt-kernel/build --target format
37+
```
38+
39+
## Developer Note
40+
41+
Formatting and commit message rules are enforced by Git hooks. After installing `clang-format` and `black`, just commit normally—the hooks will run formatting for you.
42+
43+
> [!NOTE]
44+
> If formatting modifies files, the commit is aborted after staging those changes. Review them and run `git commit` again. Repeat until no further formatting changes appear.
45+
46+
---
47+
48+
### Conventional Commit Regex (Reference)
49+
50+
The commit-msg hook enforces this pattern:
51+
52+
```text
53+
regex='^\[(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert|wip)\](\([^\)]+\))?(!)?: .+'
54+
```
55+
56+
Meaning (English):
57+
* `[type]` required — one of feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert|wip
58+
* Optional scope: `(scope)` — any chars except `)`
59+
* Optional breaking change marker: `!` right after type or scope
60+
* Separator: `: ` (colon + space)
61+
* Subject: free text (at least one character)
62+
63+
Examples:
64+
```text
65+
[feat]: add adaptive batching
66+
[fix(parser)]: handle empty token list
67+
[docs]!: update API section for breaking rename
68+
```
69+
70+
You can bypass locally (not recommended) with:
71+
```shell
72+
git commit --no-verify
73+
```
74+
## 提交前提醒
75+
76+
提交信息必须满足 Conventional Commits 规范 (https://www.conventionalcommits.org/),代码需要符合格式要求。Git 钩子已经集成了大部分工作:
77+
### 软件要求
78+
79+
需要较新的 `clang-format` (>= 18),在 conda 环境中安装:
80+
81+
```shell
82+
conda install -c conda-forge clang-format=18
83+
```
84+
85+
如果之前用老版本配置过,请删除构建目录重新配置:
86+
87+
```shell
88+
rm -rf kt-kernel/build
89+
```
90+
91+
安装 `black` 以进行 Python 文件格式化:
92+
93+
```shell
94+
conda install black
95+
```
96+
### 安装钩子
97+
```shell
98+
bash kt-kernel/scripts/install-git-hooks.sh
99+
#or just cmake the kt-kernel
100+
cmake -S kt-kernel -B kt-kernel/build
101+
```
102+
如果你需要手动格式化:
103+
```shell
104+
cmake -S kt-kernel -B kt-kernel/build
105+
cmake --build kt-kernel/build --target format
106+
```
107+
108+
## 开发者说明
109+
110+
本仓库通过 Git hooks 自动执行代码格式化与提交信息规范检查。只需安装好 `clang-format``black` 后正常执行提交即可,钩子会自动格式化。
111+
112+
> [!NOTE]
113+
> 如果格式化修改了文件,钩子会终止提交并已暂存这些改动。请查看修改后再次执行 `git commit`,重复直到没有新的格式化变更。
114+
115+
### 提交信息正则(参考)
116+
117+
钩子使用如下正则检查提交信息:
118+
```text
119+
regex='^\[(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert|wip)\](\([^\)]+\))?(!)?: .+'
120+
```
121+
含义:
122+
* `[type]` 必填:feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert|wip
123+
* 作用域可选:`(scope)`,不能包含右括号
124+
* 可选的破坏性标记:`!`
125+
* 分隔符:冒号+空格 `: `
126+
* 描述:至少一个字符
127+
128+
示例:
129+
```text
130+
[feat]: 增加自适应 batch 功能
131+
[fix(tokenizer)]: 修复空 token 列表处理
132+
[docs]!: 更新接口文档(存在破坏性修改)
133+
```
134+
135+
跳过钩子(不推荐,仅紧急时):
136+
```shell
137+
git commit --no-verify
138+
```
139+

kt-kernel/.githooks/pre-commit

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/bash
2-
# Pre-commit hook: run clang-format via CMake 'format' target and Black for Python before allowing commit.
3-
# If formatting makes changes, stage them and abort so user can review.
2+
# Pre-commit hook: run clang-format via kt-kernel's CMake 'format' target and Black for Python
3+
# before allowing commit. If formatting makes changes, stage them and abort so user can review.
44
set -euo pipefail
55

66
REPO_ROOT="$(git rev-parse --show-toplevel)"
7-
BUILD_DIR="$REPO_ROOT/build"
7+
# kt-kernel project directory within the monorepo
8+
KERNEL_DIR="$REPO_ROOT/kt-kernel"
9+
BUILD_DIR="$KERNEL_DIR/build"
810
FORMAT_TARGET="format"
911
CLANG_FORMAT_BIN="${CLANG_FORMAT_BIN:-clang-format}"
1012
BLACK_BIN="${BLACK_BIN:-black}"
@@ -20,10 +22,10 @@ if ! command -v "$BLACK_BIN" >/dev/null 2>&1; then
2022
echo "[pre-commit] black not found (looked for $BLACK_BIN). Skipping Python format." >&2
2123
fi
2224

23-
# Configure build directory if missing (quiet)
24-
if [ ! -d "$BUILD_DIR" ] || [ ! -f "$BUILD_DIR/Makefile" ] && [ ! -f "$BUILD_DIR/build.ninja" ]; then
25-
echo "[pre-commit] configuring project (cmake) ..." >&2
26-
cmake -S "$REPO_ROOT" -B "$BUILD_DIR" >/dev/null
25+
# Configure kt-kernel build directory if missing (quiet)
26+
if [ ! -d "$BUILD_DIR" ] || { [ ! -f "$BUILD_DIR/Makefile" ] && [ ! -f "$BUILD_DIR/build.ninja" ]; }; then
27+
echo "[pre-commit] configuring kt-kernel (cmake) ..." >&2
28+
cmake -S "$KERNEL_DIR" -B "$BUILD_DIR" >/dev/null
2729
fi
2830

2931
# Run format target (prefer ninja if present)
@@ -38,15 +40,18 @@ fi
3840

3941
# Run black on staged python files (or entire repo if you prefer)
4042
if command -v "$BLACK_BIN" >/dev/null 2>&1; then
41-
# Get staged python files; if none, skip
42-
PY_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.py$' || true)
43-
if [ -n "$PY_FILES" ]; then
44-
echo "[pre-commit] running black on staged python files..." >&2
45-
$BLACK_BIN $PY_FILES
46-
else
47-
# Optionally format all python files; comment out if not desired
48-
# $BLACK_BIN "$REPO_ROOT"
49-
:
43+
# Run black only on kt-kernel's python and scripts directories
44+
BLACK_PATHS=""
45+
if [ -d "$KERNEL_DIR/python" ]; then
46+
BLACK_PATHS="$BLACK_PATHS $KERNEL_DIR/python"
47+
fi
48+
if [ -d "$KERNEL_DIR/scripts" ]; then
49+
BLACK_PATHS="$BLACK_PATHS $KERNEL_DIR/scripts"
50+
fi
51+
if [ -n "$BLACK_PATHS" ]; then
52+
echo "[pre-commit] running black on:$BLACK_PATHS" >&2
53+
# shellcheck disable=SC2086
54+
$BLACK_BIN $BLACK_PATHS
5055
fi
5156
fi
5257

kt-kernel/CMakeLists.txt

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,40 @@ if(USE_CONDA_TOOLCHAIN)
7979
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH OFF)
8080
endif()
8181

82-
## Ensure git hooks are installed when configuring the project
83-
# If this is a git working copy and the installer exists, run it and fail the CMake configure
84-
# when installation fails. If no .git directory is present (e.g. source tarball), skip.
85-
if(EXISTS "${CMAKE_SOURCE_DIR}/.git" AND IS_DIRECTORY "${CMAKE_SOURCE_DIR}/.git")
86-
if(EXISTS "${CMAKE_SOURCE_DIR}/scripts/install-git-hooks.sh")
87-
message(STATUS "Detected .git; installing git hooks using scripts/install-git-hooks.sh")
88-
execute_process(
89-
COMMAND sh "${CMAKE_SOURCE_DIR}/scripts/install-git-hooks.sh"
90-
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
91-
RESULT_VARIABLE _INSTALL_GIT_HOOKS_RESULT
92-
OUTPUT_VARIABLE _INSTALL_GIT_HOOKS_OUT
93-
ERROR_VARIABLE _INSTALL_GIT_HOOKS_ERR
82+
## Ensure git hooks are installed when configuring the project (monorepo-aware)
83+
# If we are inside a git worktree (repo root is outside kt-kernel now), invoke the installer
84+
# which will link kt-kernel/.githooks into the top-level .git/hooks. Otherwise, skip.
85+
find_program(GIT_BIN git)
86+
if(GIT_BIN)
87+
execute_process(
88+
COMMAND "${GIT_BIN}" rev-parse --show-toplevel
89+
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
90+
OUTPUT_VARIABLE _GIT_TOP
91+
RESULT_VARIABLE _GIT_RV
92+
OUTPUT_STRIP_TRAILING_WHITESPACE
93+
ERROR_QUIET
94+
)
95+
if(_GIT_RV EQUAL 0 AND EXISTS "${_GIT_TOP}/.git" AND IS_DIRECTORY "${_GIT_TOP}/.git")
96+
if(EXISTS "${CMAKE_SOURCE_DIR}/scripts/install-git-hooks.sh")
97+
message(STATUS "Detected git worktree at ${_GIT_TOP}; installing hooks from kt-kernel/.githooks")
98+
execute_process(
99+
COMMAND sh "${CMAKE_SOURCE_DIR}/scripts/install-git-hooks.sh"
100+
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
101+
RESULT_VARIABLE _INSTALL_GIT_HOOKS_RESULT
102+
OUTPUT_VARIABLE _INSTALL_GIT_HOOKS_OUT
103+
ERROR_VARIABLE _INSTALL_GIT_HOOKS_ERR
94104
)
95-
if(NOT _INSTALL_GIT_HOOKS_RESULT EQUAL 0)
96-
message(FATAL_ERROR "Installing git hooks failed (exit ${_INSTALL_GIT_HOOKS_RESULT}).\nOutput:\n${_INSTALL_GIT_HOOKS_OUT}\nError:\n${_INSTALL_GIT_HOOKS_ERR}")
105+
if(NOT _INSTALL_GIT_HOOKS_RESULT EQUAL 0)
106+
message(FATAL_ERROR "Installing git hooks failed (exit ${_INSTALL_GIT_HOOKS_RESULT}).\nOutput:\n${_INSTALL_GIT_HOOKS_OUT}\nError:\n${_INSTALL_GIT_HOOKS_ERR}")
107+
endif()
108+
else()
109+
message(FATAL_ERROR "Required script 'scripts/install-git-hooks.sh' not found in kt-kernel; cannot install hooks.")
97110
endif()
98111
else()
99-
message(FATAL_ERROR "Repository appears to be a git repo but required script 'scripts/install-git-hooks.sh' was not found. Please ensure hooks installer is present.")
112+
message(STATUS "No git worktree detected; skipping git hooks installation")
100113
endif()
101114
else()
102-
message(STATUS "No .git directory found; skipping git hooks installation")
115+
message(STATUS "git not found; skipping git hooks installation")
103116
endif()
104117

105118
set(CMAKE_CXX_STANDARD 20)

kt-kernel/cpu_backend/shared_mem_buffer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
**/
1010
#include "shared_mem_buffer.h"
1111

12+
#include <errno.h>
1213
#include <numa.h>
1314

1415
#include <cstdio>
15-
#include <errno.h>
1616

1717
size_t MemoryRequest::total_size() {
1818
size_t total = 0;

kt-kernel/operators/moe-tp.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <cstdio>
88
#include <type_traits>
99

10+
#include "../cpu_backend/shared_mem_buffer.h"
1011
#include "common.hpp"
1112

1213
// Forward declaration for Llamafile backend type checking

kt-kernel/operators/moe_kernel/moe.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
#include <vector>
1414

1515
#include "../common.hpp"
16+
#include "../cpu_backend/shared_mem_buffer.h"
1617
#include "../moe-tp.hpp"
1718
#include "api/common.h"
1819
#include "api/mat_kernel.h"
1920
#include "llama.cpp/ggml.h"
20-
2121
template <class T, bool PLAIN = true>
2222
class MOE_KERNEL_TP
2323
#ifdef FORWARD_TIME_PROFILE

0 commit comments

Comments
 (0)