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
29 changes: 20 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,23 @@ permissions:
jobs:
build:
runs-on: ubuntu-24.04
# Ubuntu 26.04's own GCC 16 (16-20260322 and later) ships <simd> (C++26 std::simd); the
# toolchain PPA's GCC 16 for 24.04 is an older snapshot without it.
container:
image: ubuntu:26.04
# A real init as PID 1 reaps orphans, as systemd does on a host: smoke_serve SIGKILLs
# serve and checks that its backend is gone, and without a reaper the exited backend
# stays a zombie that kill -0 still finds.
options: --init
steps:
- name: Tools for checkout and the build
run: |
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends ca-certificates git \
python3 python3-venv curl jq file procps make gcc-16 g++-16 cmake ninja-build libcurl4-openssl-dev libzstd-dev \
libssl-dev libdrm-dev libpcre2-dev
git config --global --add safe.directory "$GITHUB_WORKSPACE"
python3 -m venv /opt/ci-venv && echo /opt/ci-venv/bin >> "$GITHUB_PATH"
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Copyright notice on every file
run: python3 tools/copyright.py --check
Expand All @@ -38,20 +54,14 @@ jobs:
python3 tools/check_pins.py "$BASE"
- name: GGUF -> Q4NX repack, ternary -> Q4_0, MTP draft vocabulary
run: |
python3 -m pip install --user numpy gguf
python3 -m pip install numpy gguf
python3 tests/gguf_to_q4nx_test.py
python3 tests/ternary_to_q4_0_test.py
python3 tests/mtp_draft_vocab_test.py
python3 tests/check_pins_test.py
python3 tests/hrx_long_prompt_test.py
- name: Install build dependencies
# GCC 15, as on the Strix Halo development machine: the engine is C++26, which the
# runner's own GCC 13 cannot build
run: |
sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
sudo apt-get update && sudo apt-get install -y g++-15 ninja-build libcurl4-openssl-dev libzstd-dev libssl-dev libdrm-dev libpcre2-dev
- name: Configure
run: cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=gcc-15 -DCMAKE_CXX_COMPILER=g++-15
run: cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=gcc-16 -DCMAKE_CXX_COMPILER=g++-16
- name: Build
# Every target a ctest test invokes must be listed here: `ctest` runs whatever
# binaries exist, so a test whose target is missing reports "Not Run" and never
Expand All @@ -63,4 +73,5 @@ jobs:
laya_encoder_test http_guard_test moe_gguf_index_test laya_bounds_test \
npu_model_bounds_test npu_forward_loader_test tokenizer_linear_test hrx_placement_test hrx_cwsr_test -j
- name: Test
run: ctest --test-dir build --output-on-failure
# One hung test must not hold the job for hours: each test gets 10 minutes.
run: ctest --test-dir build --output-on-failure --timeout 600
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.
cmake_minimum_required(VERSION 3.24)
# The engine needs GCC 16: C++26 std::simd (<simd>) is in libstdc++ from GCC 16.
# Pick gcc-16 / g++-16 when no compiler was chosen and they are installed.
if(NOT CMAKE_CXX_COMPILER AND NOT DEFINED ENV{CXX})
find_program(ONEBIT_GXX16 g++-16)
find_program(ONEBIT_GCC16 gcc-16)
if(ONEBIT_GXX16 AND ONEBIT_GCC16)
set(CMAKE_C_COMPILER "${ONEBIT_GCC16}")
set(CMAKE_CXX_COMPILER "${ONEBIT_GXX16}")
endif()
endif()
project(onebit_engine VERSION 0.0.1 LANGUAGES C CXX)
if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_VERSION VERSION_LESS 16)
message(FATAL_ERROR "The engine needs GCC 16 or newer for C++26 std::simd; found "
"${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}. Install g++-16 or pass "
"-DCMAKE_CXX_COMPILER=g++-16 -DCMAKE_C_COMPILER=gcc-16.")
endif()

set(CMAKE_CXX_STANDARD 26)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down
42 changes: 25 additions & 17 deletions laya/scorer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@
#include <nlohmann/json.hpp>

#include <algorithm>
#include <thread>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <memory>
#include <simd>
#include <string>
#include <thread>
#include <vector>

namespace onebit::laya {
Expand Down Expand Up @@ -88,26 +89,35 @@ void parallel_ranges(int n, int t, F fn) {

// out[m, j0..j1) of in[m,n] @ W^T[k,n] + bias[k]. Every sum has one fixed order (sixteen lanes over
// the input, then the lanes in turn, then the tail), so the result does not depend on how the
// columns are split across threads, and the lanes vectorize.
// columns are split across threads. The lanes are a C++26 std::simd vector of 16 floats: each
// clone lowers it to its own registers (one zmm, two ymm, or four xmm) with the same lane order.
ONEBIT_LAYA_SIMD_CLONES
void linear_cols(const float* in, const float* W, const float* bias, int m, int n, int k, float* out, int j0, int j1) {
constexpr int V = 16;
const int nv = n / V * V;
using V = std::simd::vec<float, 16>;
constexpr int VL = 16;
const int nv = n / VL * VL;
auto load = [](const float* p) { return std::simd::unchecked_load<V>(p, VL); };
// Sum the lanes in order 0..15 onto s, as the scalar loop did: one store, then scalar adds
// (reading a[l] lane by lane makes AVX2 and SSE shuffle each lane out of the register).
auto add_lanes = [](float s, const V& a) {
alignas(64) float t[VL];
std::simd::unchecked_store(a, t, VL);
for (int l = 0; l < VL; ++l) s += t[l];
return s;
};
// Each weight row is read once per four input rows; the four rows' sums are independent.
for (int j = j0; j < j1; ++j) {
const float* wr = W + (size_t)j * n;
const float b = bias ? bias[j] : 0.0f;
int i = 0;
for (; i + 4 <= m; i += 4) {
const float *x0 = in + (size_t)i * n, *x1 = x0 + n, *x2 = x1 + n, *x3 = x2 + n;
float a0[V] = {}, a1[V] = {}, a2[V] = {}, a3[V] = {};
for (int c = 0; c < nv; c += V)
for (int l = 0; l < V; ++l) {
const float w = wr[c + l];
a0[l] += x0[c + l] * w; a1[l] += x1[c + l] * w; a2[l] += x2[c + l] * w; a3[l] += x3[c + l] * w;
}
float s0 = b, s1 = b, s2 = b, s3 = b;
for (int l = 0; l < V; ++l) { s0 += a0[l]; s1 += a1[l]; s2 += a2[l]; s3 += a3[l]; }
V a0 = 0.0f, a1 = 0.0f, a2 = 0.0f, a3 = 0.0f;
for (int c = 0; c < nv; c += VL) {
const V w = load(wr + c);
a0 += load(x0 + c) * w; a1 += load(x1 + c) * w; a2 += load(x2 + c) * w; a3 += load(x3 + c) * w;
}
float s0 = add_lanes(b, a0), s1 = add_lanes(b, a1), s2 = add_lanes(b, a2), s3 = add_lanes(b, a3);
for (int c = nv; c < n; ++c) {
const float w = wr[c];
s0 += x0[c] * w; s1 += x1[c] * w; s2 += x2[c] * w; s3 += x3[c] * w;
Expand All @@ -117,11 +127,9 @@ void linear_cols(const float* in, const float* W, const float* bias, int m, int
}
for (; i < m; ++i) {
const float* x = in + (size_t)i * n;
float a[V] = {};
for (int c = 0; c < nv; c += V)
for (int l = 0; l < V; ++l) a[l] += x[c + l] * wr[c + l];
float acc = b;
for (int l = 0; l < V; ++l) acc += a[l];
V a = 0.0f;
for (int c = 0; c < nv; c += VL) a += load(x + c) * load(wr + c);
float acc = add_lanes(b, a);
for (int c = nv; c < n; ++c) acc += x[c] * wr[c];
out[(size_t)i * k + j] = acc;
}
Expand Down
Loading