Skip to content
Open
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
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ cc_library(
"ops/matmul_static_nuq.cc",
"ops/matmul_static_sfp.cc",
"ops/matmul_static_i8.cc",
"ops/matmul_static_q4_0.cc",
],
hdrs = [
"ops/matmul_static.h",
Expand Down
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ set(SOURCES
compression/compress.cc
compression/compress.h
compression/nuq-inl.h
compression/q4_0-inl.h
compression/sfp-inl.h
compression/int-inl.h
compression/types.h
Expand Down Expand Up @@ -165,6 +166,7 @@ set(SOURCES
ops/matmul_static_nuq.cc
ops/matmul_static_sfp.cc
ops/matmul_static_i8.cc
ops/matmul_static_q4_0.cc
ops/matmul-inl.h
ops/matmul.cc
ops/matmul.h
Expand Down Expand Up @@ -295,6 +297,7 @@ set(GEMMA_TEST_FILES
compression/compress_test.cc
compression/distortion_test.cc
compression/nuq_test.cc
compression/q4_0_test.cc
compression/sfp_test.cc
gemma/gemma_args_test.cc
gemma/tensor_info_test.cc
Expand Down
30 changes: 30 additions & 0 deletions compression/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ cc_library(
],
)

cc_library(
name = "q4_0",
textual_hdrs = ["q4_0-inl.h"],
deps = [
":types",
"@highway//:hwy",
],
)

cc_test(
name = "int_test",
size = "small",
Expand All @@ -110,6 +119,26 @@ cc_test(
],
)

cc_test(
name = "q4_0_test",
size = "small",
timeout = "long",
srcs = ["q4_0_test.cc"],
features = ["fully_static_link"],
linkstatic = True,
local_defines = ["HWY_IS_TEST"],
# for test_suite.
tags = ["hwy_ops_test"],
deps = [
":compress",
":q4_0",
"@googletest//:gtest_main", # buildcleaner: keep
"//:test_util",
"@highway//:hwy",
"@highway//:hwy_test_util",
],
)

cc_library(
name = "test_util",
textual_hdrs = [
Expand Down Expand Up @@ -176,6 +205,7 @@ cc_library(
":distortion",
":int",
":nuq",
":q4_0",
":sfp",
"//:basics",
"//:mat",
Expand Down
34 changes: 34 additions & 0 deletions compression/compress-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "compression/int-inl.h"
#include "compression/nuq-inl.h"
#include "compression/sfp-inl.h"
#include "compression/q4_0-inl.h"

HWY_BEFORE_NAMESPACE();
namespace gcpp {
Expand Down Expand Up @@ -638,6 +639,39 @@ struct CompressTraits<I8Stream> {
}
};

// Q4_0 block quantization.
template <>
struct CompressTraits<Q4_0Stream> {
using Packed = Q4_0Stream;

template <class DF, HWY_IF_F32_D(DF)>
static HWY_INLINE void Compress(DF df, const float* HWY_RESTRICT raw,
size_t num, CompressPerThread& tls,
const PackedSpan<Packed>& packed,
const size_t packed_ofs) {
Q4_0Codec::Enc(df, raw, num, packed, packed_ofs);
}

template <class D>
static HWY_INLINE void Load2(D d, const PackedSpan<const Packed>& packed,
const size_t packed_ofs, hn::Vec<D>& raw0,
hn::Vec<D>& raw1) {
Q4_0Codec::Dec2(d, packed, packed_ofs, raw0, raw1);
}

static float ToFloatSlow(const Packed x) {
HWY_DASSERT(!"Not supported");
return 0.0f;
}

template <class D, typename Raw>
static HWY_INLINE void DecompressAndZeroPad(
D d, const PackedSpan<const Packed>& packed, const size_t packed_ofs,
Raw* raw, const size_t num) {
Q4_0Codec::DecompressAndZeroPad(d, packed, packed_ofs, raw, num);
}
};

// Nonuniform quantization, 4.5 bits per element, two separate streams.
template <>
struct CompressTraits<NuqStream> {
Expand Down
3 changes: 3 additions & 0 deletions compression/python/compression_clif_aux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ class SbsWriterImpl : public ISbsWriter {
case Type::kI8:
InsertT<I8Stream>(name, weights, tensor_info);
break;
case Type::kQ4_0:
InsertT<Q4_0Stream>(name, weights, tensor_info);
break;
default:
HWY_ABORT("Unsupported destination (compressed) type %s",
TypeName(type));
Expand Down
Loading
Loading