Skip to content

Commit 17ca767

Browse files
digantdesaifacebook-github-bot
authored andcommitted
Better 4bit packing (#2649)
Summary: Just use tensor methods, drop custom op and the previous python logic Also drop aot_utils for xnnpack Reviewed By: kimishpatel, GregoryComer Differential Revision: D55319010
1 parent 853fb5b commit 17ca767

File tree

11 files changed

+11
-241
lines changed

11 files changed

+11
-241
lines changed

CMakeLists.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ option(EXECUTORCH_BUILD_ARM_BAREMETAL
140140

141141
option(EXECUTORCH_BUILD_COREML "Build the Core ML backend" OFF)
142142

143-
option(EXECUTORCH_BUILD_EXTENSION_AOT_UTIL "Build the AOT util library" OFF)
144-
145143
option(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER "Build the Data Loader extension"
146144
OFF)
147145

@@ -372,10 +370,6 @@ if(EXECUTORCH_BUILD_EXECUTOR_RUNNER)
372370
target_compile_options(executor_runner PUBLIC ${_common_compile_options})
373371
endif()
374372

375-
if(EXECUTORCH_BUILD_EXTENSION_AOT_UTIL)
376-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/aot_util)
377-
endif()
378-
379373
# Add googletest if any test targets should be built
380374
if(EXECUTORCH_BUILD_GTESTS)
381375
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third-party/googletest)

backends/xnnpack/operators/TARGETS

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,5 @@ runtime.python_library(
1313
"//executorch/backends/xnnpack/utils:xnnpack_utils",
1414
"//executorch/exir:graph_module",
1515
"//executorch/exir/backend:backend_details",
16-
"//executorch/extension/aot_util:aot_util",
1716
],
1817
)

backends/xnnpack/operators/node_visitor.py

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
# LICENSE file in the root directory of this source tree.
66

77
import ctypes
8-
import sys
98

10-
from pathlib import Path
119
from typing import cast, Dict, List, Optional, Tuple
1210

1311
import torch
@@ -449,18 +447,6 @@ def define_tensor(
449447
if quant_params is not None:
450448
vals_to_ids[quant_params.q_input] = id_out
451449

452-
@staticmethod
453-
def find_aot_util_path() -> str:
454-
# Look for .so installed by wheel (OSS). TODO(gjcomer) Improve this.
455-
rel_path = "executorch/extension/aot_util/libaot_util.so"
456-
for sys_path in sys.path:
457-
so_path = Path(sys_path) / rel_path
458-
if so_path.exists():
459-
return str(so_path.absolute().as_posix())
460-
461-
# Fall back to buck.
462-
return "//executorch/extension/aot_util:aot_util"
463-
464450
@staticmethod
465451
def convert_to_qc4w(inp: torch.Tensor) -> torch.Tensor:
466452
"""
@@ -478,37 +464,24 @@ def convert_to_qc4w(inp: torch.Tensor) -> torch.Tensor:
478464
# Assuming we have a 2d tensor
479465
if inp.ndim != 2:
480466
inp = inp.squeeze()
481-
assert (
482-
inp.ndim == 2
483-
), f"convert_to_qc4w: expecting input tensor to be 2d, got {inp.ndim}"
484-
oc, ic = inp.shape
467+
assert (
468+
inp.ndim == 2
469+
), f"convert_to_qc4w: expecting input tensor to be 2d, got {inp.ndim}"
485470

486471
# pad ic
487-
if ic % 2 != 0:
472+
if inp.shape[-1] % 2 != 0:
488473
inp = F.pad(input=inp, pad=(0, 1, 0, 0), mode="constant", value=0)
489474

475+
# Shape after padding
476+
oc, ic = inp.shape
477+
assert ic % 2 == 0, "convert_to_qc4w: expecting ic to be even"
478+
490479
# Adjust inp tensor for zp
491480
inp = inp.to(dtype=torch.uint8) + 8
492481

493-
# prepare result tensor
494-
ric = int((ic + 1) / 2)
495-
result = torch.zeros([oc, ric], dtype=torch.uint8)
496-
497-
try:
498-
aot_path = NodeVisitor.find_aot_util_path()
499-
torch.ops.load_library(aot_path)
500-
result = torch.ops.xnnpack.convert_to_qc4w(inp)
501-
except:
502-
# Fallback to python implementation
503-
# TODO Warn the user? They might be developing in-tree and didn't install,
504-
# in which case, this will be very slow for large models.
505-
for o in range(oc):
506-
for i in range(ric):
507-
j = 2 * i
508-
result[o][i] = inp[o][j]
509-
result[o][i] += inp[o][j + 1] << 4
510-
511-
return result
482+
# Prepare the Result tensor
483+
inp = inp.contiguous().view(-1)
484+
return (inp[1::2] << 4 | inp[::2]).view(oc, int(ic / 2))
512485

513486
def get_serialized_buffer_index(
514487
self,

backends/xnnpack/test/TARGETS

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,3 @@ runtime.python_test(
7575
"//executorch/backends/xnnpack:xnnpack_preprocess",
7676
],
7777
)
78-
79-
runtime.python_test(
80-
name = "test_custom_convert_qc4w_op",
81-
srcs = ["ops/test_custom_convert_to_qc4w.py"],
82-
deps = [
83-
"//caffe2:torch",
84-
"//executorch/extension/aot_util:aot_util",
85-
],
86-
external_deps = [
87-
"libtorch",
88-
],
89-
)

backends/xnnpack/test/ops/test_custom_convert_to_qc4w.py

Lines changed: 0 additions & 54 deletions
This file was deleted.

build/Utils.cmake

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ function(executorch_print_configuration_summary)
4646
" EXECUTORCH_BUILD_COREML : ${EXECUTORCH_BUILD_COREML}")
4747
message(STATUS " EXECUTORCH_BUILD_EXECUTOR_RUNNER : "
4848
"${EXECUTORCH_BUILD_EXECUTOR_RUNNER}")
49-
message(STATUS " EXECUTORCH_BUILD_EXTENSION_AOT_UTIL : "
50-
"${EXECUTORCH_BUILD_EXTENSION_AOT_UTIL}")
5149
message(STATUS " EXECUTORCH_BUILD_EXTENSION_DATA_LOADER : "
5250
"${EXECUTORCH_BUILD_EXTENSION_DATA_LOADER}")
5351
message(STATUS " EXECUTORCH_BUILD_EXTENSION_MODULE : "

build/cmake_deps.toml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,6 @@ filters = [
8989

9090
# ---------------------------------- core end ----------------------------------
9191
# ---------------------------------- extension start ----------------------------------
92-
93-
[targets.extension_aot_util]
94-
buck_targets = [
95-
"//extension/aot_util:aot_util",
96-
]
97-
filters = [
98-
".cpp$",
99-
]
100-
deps = [
101-
"executorch",
102-
]
10392
[targets.extension_data_loader]
10493
buck_targets = [
10594
"//extension/data_loader:buffer_data_loader",

extension/aot_util/CMakeLists.txt

Lines changed: 0 additions & 68 deletions
This file was deleted.

extension/aot_util/TARGETS

Lines changed: 0 additions & 12 deletions
This file was deleted.

extension/aot_util/convert_to_qc4w.cpp

Lines changed: 0 additions & 32 deletions
This file was deleted.

setup.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,6 @@ def run(self):
231231
}
232232
ext_modules = []
233233

234-
if os.environ.get("EXECUTORCH_BUILD_AOT_UTIL", "ON") == "ON":
235-
ext_modules.append(
236-
CMakeExtension("executorch.extension.aot_util.aot_util", "extension/aot_util")
237-
)
238-
239234
if os.environ.get("EXECUTORCH_BUILD_PYBIND", "OFF") == "ON":
240235
ext_modules.append(CMakeExtension("executorch.extension.pybindings.portable_lib"))
241236

0 commit comments

Comments
 (0)