Skip to content

Commit 2c62f9b

Browse files
committed
Update
[ghstack-poisoned]
2 parents 8af5beb + 0ffc394 commit 2c62f9b

File tree

38 files changed

+1195
-177
lines changed

38 files changed

+1195
-177
lines changed

.ci/docker/build.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ case "${IMAGE_NAME}" in
4343
ARM_SDK=yes
4444
CLANG_VERSION=12
4545
;;
46+
executorch-ubuntu-22.04-zephyr-sdk)
47+
ZEPHYR_SDK=yes
48+
GCC_VERSION=11
49+
;;
4650
executorch-ubuntu-22.04-qnn-sdk)
4751
QNN_SDK=yes
4852
CLANG_VERSION=12
@@ -87,6 +91,7 @@ docker build \
8791
--build-arg "LINTRUNNER=${LINTRUNNER:-}" \
8892
--build-arg "BUILD_DOCS=${BUILD_DOCS}" \
8993
--build-arg "ARM_SDK=${ARM_SDK:-}" \
94+
--build-arg "ZEPHYR_SDK=${ZEPHYR_SDK:-}" \
9095
--build-arg "QNN_SDK=${QNN_SDK:-}" \
9196
--build-arg "MEDIATEK_SDK=${MEDIATEK_SDK:-}" \
9297
--build-arg "ANDROID_NDK_VERSION=${ANDROID_NDK_VERSION:-}" \
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
#!/bin/bash
3+
# Copyright (c) Meta Platforms, Inc. and affiliates.
4+
# All rights reserved.
5+
#
6+
# This source code is licensed under the BSD-style license found in the
7+
# LICENSE file in the root directory of this source tree.
8+
9+
set -ex
10+
11+
# Double check if the NDK version is set
12+
[ -n "${ZEPHYR_SDK}" ]
13+
14+
install_prerequiresites() {
15+
rm /var/lib/dpkg/info/libc-bin.*
16+
apt-get clean
17+
apt-get -y update
18+
apt-get install -y libc-bin
19+
apt-get -y update
20+
apt-get clean
21+
apt-get install --no-install-recommends -y dos2unix
22+
apt-get install --no-install-recommends -y ca-certificates
23+
apt-get install -y --reinstall libc-bin
24+
apt-get install --no-install-recommends -y file
25+
apt-get install --no-install-recommends -y locales
26+
apt-get install --no-install-recommends -y git
27+
apt-get install --no-install-recommends -y build-essential
28+
apt-get install --no-install-recommends -y cmake
29+
apt-get install --no-install-recommends -y ninja-build gperf
30+
apt-get install --no-install-recommends -y device-tree-compiler
31+
apt-get install --no-install-recommends -y wget
32+
apt-get install --no-install-recommends -y curl
33+
apt-get install --no-install-recommends -y xz-utils
34+
apt-get install --no-install-recommends -y dos2unix
35+
apt-get install --no-install-recommends -y vim
36+
apt-get install --no-install-recommends -y nano
37+
apt-get install --no-install-recommends -y mc
38+
apt-get install --no-install-recommends -y openssh-server
39+
apt-get install -y gdb
40+
41+
# Zephyr SDK relies on python 3.12
42+
apt install software-properties-common -y
43+
add-apt-repository ppa:deadsnakes/ppa -y
44+
apt update
45+
apt install -y python3.12 python3.12-dev python3.12-venv python3-pip
46+
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1
47+
48+
# Upgrade cmake ot 3.24
49+
apt update
50+
apt install cmake
51+
apt install software-properties-common lsb-release
52+
apt update
53+
test -f /usr/share/doc/kitware-archive-keyring/copyright || \
54+
wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | tee /usr/share/keyrings/kitware-archive-keyring.gpg >/dev/null
55+
"deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/kitware.list > /dev/null
56+
apt update
57+
apt install cmake
58+
59+
# Install additional required software for Zephyr
60+
apt install --no-install-recommends -y ccache \
61+
dfu-util \
62+
python3-setuptools \
63+
python3-tk \
64+
python3-wheel \
65+
make \
66+
gcc \
67+
libsdl2-dev \
68+
libmagic1 \
69+
xterm \
70+
telnet \
71+
net-tools
72+
apt install --no-install-recommends -y gcc-multilib g++-multilib
73+
apt-get clean -y
74+
apt-get autoremove --purge -y
75+
rm -rf /var/lib/apt/lists/*
76+
wget https://apt.kitware.com/kitware-archive.sh && \
77+
chmod +x kitware-archive.sh && \
78+
./kitware-archive.sh && \
79+
rm -f kitware-archive.sh
80+
useradd -d /home/zephyruser -m -s /bin/bash zephyruser
81+
}
82+
83+
install_sdk() {
84+
wget https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.16.0/zephyr-sdk-0.16.0_linux-x86_64.tar.xz && \
85+
tar -xf zephyr-sdk-0.16.0_linux-x86_64.tar.xz && \
86+
rm -f zephyr-sdk-0.16.0_linux-x86_64.tar.xz && \
87+
cd zephyr-sdk-0.16.0/ && \
88+
./setup.sh -c -t arm-zephyr-eabi
89+
}
90+
91+
install_prerequiresites
92+
install_sdk

.ci/docker/ubuntu/Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ RUN rm install_android.sh
8484

8585
ARG ARM_SDK
8686

87+
ARG ZEPHYR_SDK
88+
COPY ./common/install_zephyr.sh install_zephyr.sh
89+
RUN if [ -n "${ZEPHYR_SDK}" ]; then bash ./install_zephyr.sh; fi
90+
RUN rm install_zephyr.sh
91+
8792
ARG QNN_SDK
8893

8994
ARG MEDIATEK_SDK

.github/workflows/build-presets.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
job-name: build
4545
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
4646
runner: linux.2xlarge
47-
docker-image: ci-image:executorch-ubuntu-22.04-arm-sdk
47+
docker-image: ci-image:executorch-ubuntu-22.04-zephyr-sdk
4848
submodules: recursive
4949
timeout: 90
5050
script: |

.github/workflows/docker-builds.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ jobs:
3737
executorch-ubuntu-22.04-clang12,
3838
executorch-ubuntu-22.04-linter,
3939
executorch-ubuntu-22.04-arm-sdk,
40+
executorch-ubuntu-22.04-zephyr-sdk,
4041
executorch-ubuntu-22.04-qnn-sdk,
4142
executorch-ubuntu-22.04-mediatek-sdk,
4243
executorch-ubuntu-22.04-clang12-android

backends/apple/coreml/compiler/coreml_preprocess.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
logger = logging.getLogger(__name__)
2929
logger.setLevel(logging.WARNING)
3030

31+
from executorch.backends.apple.coreml.compiler.torch_ops import * # noqa: F401, F403
32+
3133

3234
class COMPILE_SPEC_KEYS(Enum):
3335
COMPUTE_UNITS = "compute_units"
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# This file registers torch ops that are not yet in coremltools, or are in a more recent version of
8+
# coremltools than is used by ExecuTorch. Each op registered here should have a link to a PR in coremltools that adds
9+
# the op to the coremltools library.
10+
11+
import torch as _torch
12+
from coremltools import _logger as logger
13+
from coremltools.converters.mil.frontend import _utils
14+
from coremltools.converters.mil.frontend.torch.ops import (
15+
_get_inputs,
16+
NUM_TO_NUMPY_DTYPE,
17+
NUM_TO_TORCH_DTYPE,
18+
transpose,
19+
unbind,
20+
)
21+
22+
from coremltools.converters.mil.frontend.torch.torch_op_registry import (
23+
register_torch_op,
24+
)
25+
from coremltools.converters.mil.mil import types
26+
27+
28+
# https://github.com/apple/coremltools/pull/2556
29+
@register_torch_op(override=False)
30+
def transpose_copy(context, node):
31+
transpose(context, node)
32+
33+
34+
# https://github.com/apple/coremltools/pull/2557
35+
@register_torch_op(override=False)
36+
def unbind_copy(context, node):
37+
unbind(context, node)
38+
39+
40+
# https://github.com/apple/coremltools/pull/2558
41+
@register_torch_op(
42+
torch_alias=["torchao::dequantize_affine", "torchao.dequantize_affine"],
43+
override=False,
44+
)
45+
def dequantize_affine(context, node):
46+
inputs = _get_inputs(context, node, expected=[7, 8])
47+
int_data = inputs[0].val
48+
block_size = inputs[1].val
49+
scale = inputs[2].val
50+
zero_point = (
51+
inputs[3].val if inputs[3] is not None and inputs[3].val is not None else None
52+
)
53+
# I do not think we need to worry about input_dtype b/c it gets cast to int4/int8
54+
# For now, we just check that it is int8 or int32
55+
input_dtype = inputs[4].val # noqa: F841
56+
assert NUM_TO_TORCH_DTYPE[input_dtype] in [
57+
_torch.int8,
58+
_torch.int32,
59+
], "input_dtype should be int8 or int32"
60+
61+
quant_min = inputs[5].val
62+
quant_max = inputs[6].val
63+
64+
assert len(int_data.shape) == 2, "dequantize_affine only supports rank 2 inputs"
65+
66+
assert len(int_data.shape) == len(
67+
block_size
68+
), "block_size must have the same length as int_data.shape"
69+
assert block_size[0] == 1, "block_size[0] must be 1"
70+
group_size = block_size[1]
71+
k = int_data.shape[1]
72+
assert k % group_size == 0, "k must be divisible by group_size"
73+
scales_per_row = k // group_size
74+
scale = scale.reshape(-1, scales_per_row)
75+
if zero_point is not None:
76+
zero_point = zero_point.reshape(-1, scales_per_row)
77+
78+
# TODO: I don't know if CoreML can make use of this
79+
# We could add a cast op to the output, but I'm pretty CoreML will remove this during a later pass
80+
# For now, we just log a warning
81+
out_np_dtype = None
82+
if len(inputs) > 7:
83+
out_np_dtype = NUM_TO_NUMPY_DTYPE[inputs[7].val]
84+
logger.warning(
85+
f"Core ML ignores output_dtype {out_np_dtype} on torchao.dequantize_affine and instead uses the native precision."
86+
)
87+
88+
if quant_min == -8 and quant_max == 7:
89+
quantized_np_dtype = types.nptype_from_builtin(types.string_to_builtin("int4"))
90+
elif quant_min == -128 and quant_max == 127:
91+
quantized_np_dtype = types.nptype_from_builtin(types.string_to_builtin("int8"))
92+
else:
93+
raise ValueError(
94+
f"Unsupported quantization range: {quant_min} to {quant_max}. CoreML only supports 4-bit and 8-bit quantization."
95+
)
96+
97+
output = _utils._construct_constexpr_dequant_op(
98+
int_data.astype(quantized_np_dtype),
99+
zero_point,
100+
scale,
101+
axis=-1,
102+
name=node.name,
103+
)
104+
context.add(output, node.name)

0 commit comments

Comments
 (0)