Skip to content

Commit bff64be

Browse files
committed
Update base for Update on "[ET-VK] Enable Partial GPU lowering via Vulkan in stories model export"
## Context Simple change to add Vulkan Partitioner as a dependency for the llama exporter and runner, and provide a command line flag to invoke the vulkan partitioner during export. Included a small change to the Vulkan serializer which was needed for everything to work (i.e. enable serializing multiple graph outputs). Differential Revision: [D54805831](https://our.internmc.facebook.com/intern/diff/D54805831/) [ghstack-poisoned]
2 parents 708608e + e98a7e0 commit bff64be

File tree

266 files changed

+12963
-9401
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

266 files changed

+12963
-9401
lines changed

.ci/docker/ci_commit_pins/pytorch.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
f5b99976adcbb01fd71bd0a39ea15bdac6c9e48a
1+
6ca9ae4f8693639c395544327f7e362441a58c79

.ci/docker/common/install_conda.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ install_pip_dependencies() {
4444
}
4545

4646
fix_conda_ubuntu_libstdcxx() {
47+
cat /etc/issue
4748
# WARNING: This is a HACK from PyTorch core to be able to build PyTorch on 22.04.
48-
# The issue still exists with the latest conda 23.10.0-1 at the time of writing
49-
# (2023/11/16).
49+
# Specifically, ubuntu-20+ all comes lib libstdc++ newer than 3.30+, but anaconda
50+
# is stuck with 3.29. So, remove libstdc++6.so.3.29 as installed by
51+
# https://anaconda.org/anaconda/libstdcxx-ng/files?version=11.2.0
5052
#
5153
# PyTorch sev: https://github.com/pytorch/pytorch/issues/105248
5254
# Ref: https://github.com/pytorch/pytorch/blob/main/.ci/docker/common/install_conda.sh
53-
if grep -e "[12][82].04.[623]" /etc/issue >/dev/null; then
55+
if grep -e "2[02].04." /etc/issue >/dev/null; then
5456
rm "/opt/conda/envs/py_${PYTHON_VERSION}/lib/libstdc++.so.6"
5557
fi
5658
}

.github/pytorch-probot.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
ciflow_push_tags:
33
- ciflow/nightly
44
- ciflow/trunk
5+
- ciflow/binaries
6+
- ciflow/binaries/all
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# From https://github.com/pytorch/test-infra/wiki/Using-Nova-Reusable-Build-Workflows
2+
name: Build Linux Wheels
3+
4+
on:
5+
pull_request:
6+
paths:
7+
- build/packaging/**
8+
- .github/workflows/build-wheels-linux.yml
9+
push:
10+
branches:
11+
- nightly
12+
- release/*
13+
tags:
14+
# NOTE: Binary build pipelines should only get triggered on release candidate builds
15+
# Release candidate tags look like: v1.11.0-rc1
16+
- v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+
17+
- ciflow/binaries/*
18+
workflow_dispatch:
19+
20+
jobs:
21+
generate-matrix:
22+
uses: pytorch/test-infra/.github/workflows/generate_binary_build_matrix.yml@main
23+
with:
24+
package-type: wheel
25+
os: linux
26+
test-infra-repository: pytorch/test-infra
27+
test-infra-ref: main
28+
with-cuda: disabled
29+
with-rocm: disabled
30+
31+
build:
32+
needs: generate-matrix
33+
permissions:
34+
id-token: write
35+
contents: read
36+
strategy:
37+
fail-fast: false
38+
matrix:
39+
include:
40+
- repository: pytorch/executorch
41+
pre-script: build/packaging/pre_build_script.sh
42+
post-script: build/packaging/post_build_script.sh
43+
smoke-test-script: build/packaging/smoke_test.py
44+
package-name: executorch
45+
name: ${{ matrix.repository }}
46+
uses: pytorch/test-infra/.github/workflows/build_wheels_linux.yml@main
47+
with:
48+
repository: ${{ matrix.repository }}
49+
ref: ""
50+
test-infra-repository: pytorch/test-infra
51+
test-infra-ref: main
52+
build-matrix: ${{ needs.generate-matrix.outputs.matrix }}
53+
pre-script: ${{ matrix.pre-script }}
54+
post-script: ${{ matrix.post-script }}
55+
package-name: ${{ matrix.package-name }}
56+
smoke-test-script: ${{ matrix.smoke-test-script }}
57+
trigger-event: ${{ github.event_name }}

.github/workflows/build-wheels-m1.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# From https://github.com/pytorch/test-infra/wiki/Using-Nova-Reusable-Build-Workflows
2+
name: Build M1 Wheels
3+
4+
on:
5+
pull_request:
6+
paths:
7+
- build/packaging/**
8+
- .github/workflows/build-wheels-m1.yml
9+
push:
10+
branches:
11+
- nightly
12+
- release/*
13+
tags:
14+
# NOTE: Binary build pipelines should only get triggered on release candidate builds
15+
# Release candidate tags look like: v1.11.0-rc1
16+
- v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+
17+
- ciflow/binaries/*
18+
workflow_dispatch:
19+
20+
jobs:
21+
generate-matrix:
22+
uses: pytorch/test-infra/.github/workflows/generate_binary_build_matrix.yml@main
23+
with:
24+
package-type: wheel
25+
os: macos-arm64
26+
test-infra-repository: pytorch/test-infra
27+
test-infra-ref: main
28+
with-cuda: disabled
29+
with-rocm: disabled
30+
31+
build:
32+
needs: generate-matrix
33+
permissions:
34+
id-token: write
35+
contents: read
36+
strategy:
37+
fail-fast: false
38+
matrix:
39+
include:
40+
- repository: pytorch/executorch
41+
pre-script: build/packaging/pre_build_script.sh
42+
post-script: build/packaging/post_build_script.sh
43+
smoke-test-script: build/packaging/smoke_test.py
44+
package-name: executorch
45+
name: ${{ matrix.repository }}
46+
uses: pytorch/test-infra/.github/workflows/build_wheels_macos.yml@main
47+
with:
48+
repository: ${{ matrix.repository }}
49+
ref: ""
50+
test-infra-repository: pytorch/test-infra
51+
test-infra-ref: main
52+
build-matrix: ${{ needs.generate-matrix.outputs.matrix }}
53+
pre-script: ${{ matrix.pre-script }}
54+
post-script: ${{ matrix.post-script }}
55+
package-name: ${{ matrix.package-name }}
56+
runner-type: macos-m1-stable
57+
smoke-test-script: ${{ matrix.smoke-test-script }}
58+
trigger-event: ${{ github.event_name }}

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@
5555
[submodule "backends/vulkan/third-party/Vulkan-Headers"]
5656
path = backends/vulkan/third-party/Vulkan-Headers
5757
url = https://github.com/KhronosGroup/Vulkan-Headers
58+
[submodule "third-party/lm-evaluation-harness"]
59+
path = third-party/lm-evaluation-harness
60+
url = https://github.com/EleutherAI/lm-evaluation-harness
61+
branch = v0.4.1
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright © 2024 Apple Inc. All rights reserved.
2+
#
3+
# Please refer to the license found in the LICENSE file in the root directory of the source tree.
4+
5+
from coremltools.optimize.torch.quantization._coreml_quantizer import ( # noqa: FLAKE8 F401
6+
CoreMLQuantizer,
7+
)

backends/apple/coreml/scripts/generate_test_models.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ cd "$EXECUTORCH_ROOT_PATH"
2323
MODELS=("add" "mul" "mv3")
2424
for MODEL in "${MODELS[@]}"
2525
do
26-
# TODO: Don't use the script in examples directory.
27-
python3 -m examples.apple.coreml.scripts.export_and_delegate --model_name "$MODEL" --save_processed_bytes
26+
# TODO: Don't use the script in examples directory.
27+
python3 -m examples.apple.coreml.scripts.export --model_name "$MODEL" --save_processed_bytes
2828
mv -f "$MODEL""_coreml_all.pte" "$COREML_DIR_PATH/runtime/test/models"
2929
mv -f "$MODEL""_coreml_all.bin" "$COREML_DIR_PATH/runtime/test/models"
3030
done

backends/apple/coreml/setup.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ This is a tutorial for setting up the Core ML backend.
1111
```
1212
cd executorch
1313
14-
./backends/apple/coreml/scripts/install_requirements.sh
14+
./backends/apple/coreml/scripts/install_requirements.sh
1515
16-
```
16+
```
1717

18-
3. Run the example script to validate that the **Core ML** backend is set up correctly.
18+
3. Run the example script to validate that the **Core ML** backend is set up correctly.
1919

2020
```
2121
cd executorch
2222
2323
# Saves add_coreml_all.pte in the current directory if successful.
2424
25-
python3 -m examples.apple.coreml.scripts.export_and_delegate --model_name add
25+
python3 -m examples.apple.coreml.scripts.export --model_name add
2626
2727
```
2828

@@ -66,6 +66,6 @@ coreml_backend.xcframework
6666
- Accelerate.framework
6767
- CoreML.framework
6868
- libsqlite3.tbd
69-
```
69+
```
7070

71-
6. The target could now run a **Core ML** delegated **Program**.
71+
6. The target could now run a **Core ML** delegated **Program**.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Copyright © 2024 Apple Inc. All rights reserved.
2+
#
3+
# Please refer to the license found in the LICENSE file in the root directory of the source tree.
4+
5+
from typing import Tuple
6+
7+
import numpy as np
8+
import pytest
9+
10+
import torch
11+
12+
from coremltools.optimize.torch.quantization.quantization_config import (
13+
LinearQuantizerConfig,
14+
QuantizationScheme,
15+
)
16+
17+
from executorch.backends.apple.coreml.quantizer.coreml_quantizer import CoreMLQuantizer
18+
from torch._export import capture_pre_autograd_graph
19+
from torch.ao.quantization.quantize_pt2e import (
20+
convert_pt2e,
21+
prepare_pt2e,
22+
prepare_qat_pt2e,
23+
)
24+
25+
26+
class TestCoreMLQuantizer:
27+
@staticmethod
28+
def quantize_and_compare(
29+
model,
30+
example_inputs: Tuple[torch.Tensor],
31+
quantization_type: str,
32+
) -> None:
33+
assert quantization_type in {"PTQ", "QAT"}
34+
35+
pre_autograd_aten_dialect = capture_pre_autograd_graph(model, example_inputs)
36+
37+
quantization_config = LinearQuantizerConfig.from_dict(
38+
{
39+
"global_config": {
40+
"quantization_scheme": QuantizationScheme.symmetric,
41+
"milestones": [0, 0, 10, 10],
42+
"activation_dtype": torch.quint8,
43+
"weight_dtype": torch.qint8,
44+
"weight_per_channel": True,
45+
}
46+
}
47+
)
48+
quantizer = CoreMLQuantizer(quantization_config)
49+
50+
if quantization_type == "PTQ":
51+
prepared_graph = prepare_pt2e(pre_autograd_aten_dialect, quantizer)
52+
elif quantization_type == "QAT":
53+
prepared_graph = prepare_qat_pt2e(pre_autograd_aten_dialect, quantizer)
54+
55+
prepared_graph(*example_inputs)
56+
converted_graph = convert_pt2e(prepared_graph)
57+
58+
model_output = model(*example_inputs).detach().numpy()
59+
quantized_output = converted_graph(*example_inputs).detach().numpy()
60+
np.testing.assert_allclose(quantized_output, model_output, rtol=5e-2, atol=5e-2)
61+
62+
@pytest.mark.parametrize("quantization_type", ("PTQ", "QAT"))
63+
def test_conv_relu(self, quantization_type):
64+
SHAPE = (1, 3, 256, 256)
65+
66+
class Model(torch.nn.Module):
67+
def __init__(self) -> None:
68+
super().__init__()
69+
self.conv = torch.nn.Conv2d(
70+
in_channels=3, out_channels=16, kernel_size=3, padding=1
71+
)
72+
self.relu = torch.nn.ReLU()
73+
74+
def forward(self, x: torch.Tensor) -> torch.Tensor:
75+
a = self.conv(x)
76+
return self.relu(a)
77+
78+
model = Model()
79+
80+
example_inputs = (torch.randn(SHAPE),)
81+
self.quantize_and_compare(
82+
model,
83+
example_inputs,
84+
quantization_type,
85+
)
86+
87+
@pytest.mark.parametrize("quantization_type", ("PTQ", "QAT"))
88+
def test_linear(self, quantization_type):
89+
SHAPE = (1, 5)
90+
91+
class Model(torch.nn.Module):
92+
def __init__(self) -> None:
93+
super().__init__()
94+
self.linear = torch.nn.Linear(5, 10)
95+
96+
def forward(self, x: torch.Tensor) -> torch.Tensor:
97+
return self.linear(x)
98+
99+
model = Model()
100+
101+
example_inputs = (torch.randn(SHAPE),)
102+
self.quantize_and_compare(
103+
model,
104+
example_inputs,
105+
quantization_type,
106+
)
107+
108+
109+
if __name__ == "__main__":
110+
test_runner = TestCoreMLQuantizer()
111+
test_runner.test_conv_relu("PTQ")
112+
test_runner.test_linear("QAT")

0 commit comments

Comments
 (0)