Skip to content

Commit 957eccd

Browse files
committed
[Executorch] Add quantized kv cache to oss ci
Pull Request resolved: #6997 Fixes to make sure quantized kv cache works in oss ghstack-source-id: 255034795 @exported-using-ghexport Differential Revision: [D66269487](https://our.internmc.facebook.com/intern/diff/D66269487/)
1 parent 681bc31 commit 957eccd

File tree

7 files changed

+39
-5
lines changed

7 files changed

+39
-5
lines changed

.ci/scripts/test_llama.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ else
100100
COREML=OFF
101101
fi
102102

103+
if [[ "${MODE}" =~ .*quantize_kv.* ]]; then
104+
QUANTIZE_KV_CACHE=ON
105+
else
106+
QUANTIZE_KV_CACHE=OFF
107+
fi
108+
103109
echo "COREML option ${COREML}"
104110

105111
if [[ "${MODE}" =~ .*qnn.* ]]; then
@@ -235,6 +241,9 @@ fi
235241
if [[ "${QNN}" == "ON" ]]; then
236242
EXPORT_ARGS="${EXPORT_ARGS} -kv -v --qnn --disable_dynamic_shape"
237243
fi
244+
if [[ "${QUANTIZE_KV_CACHE}" == "ON" ]]; then
245+
EXPORT_ARGS="${EXPORT_ARGS} --quantize_kv_cache"
246+
fi
238247
# Add dynamically linked library location
239248
$PYTHON_EXECUTABLE -m examples.models.llama.export_llama ${EXPORT_ARGS}
240249

.github/workflows/pull.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ jobs:
8686
strategy:
8787
matrix:
8888
dtype: [fp32]
89-
mode: [portable, xnnpack+custom, xnnpack+custom+qe]
89+
mode: [portable, xnnpack+custom, xnnpack+custom+qe,xnnpack+custom+quantize_kv,xnnpack+quantize_kv]
9090
include:
9191
- dtype: bf16
9292
mode: portable

.github/workflows/trunk.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ jobs:
225225
strategy:
226226
matrix:
227227
dtype: [fp32]
228-
mode: [portable, xnnpack+kv+custom, mps, coreml]
228+
mode: [portable, xnnpack+kv+custom, mps, coreml, xnnpack+custom+quantize_kv]
229229
include:
230230
- dtype: bf16
231231
mode: portable

examples/models/llama/source_transformation/quantized_kv_cache.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,30 @@
1010
import torch
1111
import torch.nn as nn
1212
from executorch.examples.models.llama.llama_transformer import KVCache
13+
14+
from executorch.extension.llm.custom_ops import custom_ops # noqa: F401
1315
from torch.ao.quantization.fx._decomposed import quantized_decomposed_lib # noqa: F401
1416

1517

18+
try:
19+
op = torch.ops.quantized_decomposed.quantize_per_token.out
20+
assert op is not None
21+
except:
22+
import executorch
23+
import glob
24+
25+
executorch_package_path = executorch.__path__[0]
26+
libs = list(
27+
glob.glob(
28+
f"{executorch_package_path}/**/libquantized_ops_aot_lib.*", recursive=True
29+
)
30+
)
31+
assert len(libs) == 1, f"Expected 1 library but got {len(libs)}"
32+
logging.info(f"Loading custom ops library: {libs[0]}")
33+
torch.ops.load_library(libs[0])
34+
op = torch.ops.quantized_decomposed.quantize_per_token.out
35+
assert op is not None
36+
1637
"""
1738
Heavily "inspired" by AO's implementation of the same in torchao/_models/llama/model.py
1839
"""

examples/models/llama/source_transformation/sdpa.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def forward(
5656

5757
k_cache = self.kv_cache.k_cache
5858
v_cache = self.kv_cache.v_cache
59-
if isinstance(self.kv_cache, QuantizedKVCache):
59+
if hasattr(self.kv_cache, "quantized_cache_dtype"):
6060
# updated quantize cache, scale and zero points
6161
# returns dequantized kv cache
6262
# Not most optimal. Optimizations to follow next

extension/llm/custom_ops/custom_ops.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717

1818
from torch.library import impl
1919

20-
# TODO rename this file to custom_ops_meta_registration.py
2120
try:
2221
op = torch.ops.llama.sdpa_with_kv_cache.default
2322
assert op is not None
2423
op2 = torch.ops.llama.fast_hadamard_transform.default
2524
assert op2 is not None
2625
except:
27-
libs = list(Path(__file__).parent.resolve().glob("libcustom_ops_aot_lib.*"))
26+
path = Path(__file__).parent.resolve()
27+
logging.info(f"Looking for libcustom_ops_aot_lib.so in {path}")
28+
libs = list(path.glob("libcustom_ops_aot_lib.*"))
2829
assert len(libs) == 1, f"Expected 1 library but got {len(libs)}"
2930
logging.info(f"Loading custom ops library: {libs[0]}")
3031
torch.ops.load_library(libs[0])

kernels/quantized/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,17 @@ if(NOT CMAKE_GENERATOR STREQUAL "Xcode"
6060
set(_quantized_aot_ops
6161
"quantized_decomposed::add.out"
6262
"quantized_decomposed::choose_qparams.Tensor_out"
63+
"quantized_decomposed::choose_qparams_per_token_asymmetric.out"
6364
"quantized_decomposed::dequantize_per_channel.out"
6465
"quantized_decomposed::dequantize_per_tensor.out"
6566
"quantized_decomposed::dequantize_per_tensor.Tensor_out"
67+
"quantized_decomposed::dequantize_per_token.out"
6668
"quantized_decomposed::mixed_linear.out"
6769
"quantized_decomposed::mixed_mm.out"
6870
"quantized_decomposed::quantize_per_channel.out"
6971
"quantized_decomposed::quantize_per_tensor.out"
7072
"quantized_decomposed::quantize_per_tensor.Tensor_out"
73+
"quantized_decomposed::quantize_per_token.out"
7174
)
7275
gen_selected_ops(
7376
LIB_NAME "quantized_ops_aot_lib" ROOT_OPS ${_quantized_aot_ops}

0 commit comments

Comments
 (0)