已合并
docs: Add CANN Dockerfiles for A2 and A3 #167
haozhang创建于 26 天前
docs: Add CANN Dockerfiles for A2 and A3 #167
已合并
haozhang创建于 26 天前
共 8 个文件变更+551-1
@@ -1 +1,40 @@
1-sift1M1+# Datasets and generated test data
2+/sift1M
3+**/*.bin
4+**/*.fvecs
5+**/*.ivecs
6+ 
7+# Build and packaging outputs
8+/build/
9+/dist/
10+**/dist/
11+**/*.whl
12+**/*.egg-info
13+/faiss/npu/ops/build/
14+/faiss/npu/ops/build_out/
15+ 
16+# Version control and Docker metadata
17+/.git/
Y
Yyihao123422 天前

严重程度: 建议

问题: .dockerignore 中仅用 /.git/ 排除根级 .git 目录,未覆盖子模块或嵌套仓库的 .git 路径。

原因: 如果仓库将来引入 git submodule,子模块目录下会存在独立的 .git 文件或目录,它们不会被 /.git/ 匹配,会被 COPY . /opt/faiss 复制进构建上下文,泄露内部仓库元数据并增大上下文体积。当前敏感文件已用 /*.key、/.env 等通配模式覆盖,但 .git 仅做了根级排除,防护口径不一致。

怎么改: 在 /.git/ 之外追加 **/.git 和 /.git/ 两条模式覆盖嵌套场景;同步在 test_dockerfiles.py 的 test_root_dockerignore_excludes_large_and_sensitive_inputs 中增加对应断言。

likedislike
haozhang
haozhang
21 天前 评论:
18+**/.git
19+/.gitignore
20+/.dockerignore
21+ 
22+# IDE and temporary files
23+/.idea/
24+/.vscode/
25+**/*.swp
26+**/*~
27+**/__pycache__/
28+**/*.pyc
29+**/.DS_Store
30+ 
31+# Sensitive local files
32+**/.env
33+**/.env.*
Y
Yyihao123421 天前

严重程度: 提示

问题: "Sensitive local files" 区段未排除 .envrc 文件(direnv 配置文件),存在敏感信息泄露风险

原因: 当前模式 /.env 匹配名为 .env 的文件,/.env.* 匹配 .env.local、.env.production 等(要求 .env 后紧跟一个点)。但 direnv 使用的 .envrc 文件(.env 后直接跟 rc,无点分隔)不会被这两个模式匹配。.envrc 文件中常包含 export SECRET=... 等敏感环境变量,如果开发者将其放在仓库中,COPY . /opt/faiss 会将其打入镜像,造成密钥泄露。该文件在 .dockerignore 的 'Sensitive local files' 设计意图范围内,属于遗漏。

怎么改: 在 .dockerignore 的 Sensitive local files 区段中,在 */.env. 之后添加一行 **/.envrc,或者将 */.env. 改为更宽泛的 */.env 以覆盖 .envrc 及其他 .env 前缀变体(注意后者可能过于宽泛,需评估是否影响正常文件)。

likedislike
haozhang
haozhang
19 天前 评论:
34+**/.envrc
35+**/*.key
36+**/*.pem
37+**/*.cert
38+**/credentials
39+**/secrets
40+**/credentials.json
@@ -0,0 +1,34 @@
1+# Copyright (c) Meta Platforms, Inc. and affiliates.
2+#
3+# This source code is licensed under the MIT license found in the
4+# LICENSE file in the root directory of this source tree.
5+ 
6+ARG CANN_REGISTRY=swr.cn-south-1.myhuaweicloud.com/ascendhub
7+ARG CANN_VERSION=9.1.0
8+ARG PYTHON_VERSION=3.12
9+ARG CANN_IMAGE_DIGEST=sha256:aae509d8ee803c59e907cf3e846bf140f84d7c12c39d63dc075e3798edd0a3ec
10+FROM ${CANN_REGISTRY}/cann:${CANN_VERSION}-910b-openeuler24.03-py${PYTHON_VERSION}@${CANN_IMAGE_DIGEST}
11+ARG CANN_VERSION
12+ARG PYTHON_VERSION
13+ARG FAISS_BUILD_JOBS
14+ 
15+LABEL org.opencontainers.image.source="https://gitcode.com/Ascend/faiss" \
16+ org.opencontainers.image.version="${CANN_VERSION}" \
17+ org.opencontainers.image.description="Faiss NPU build image for Ascend 910B on openEuler 24.03" \
18+ maintainer="Ascend/faiss maintainers"
19+ 
20+ENV ASCEND_CUSTOM_OPP_PATH=${ASCEND_HOME_PATH}/opp/vendors/custom_math \
21+ LD_LIBRARY_PATH=${ASCEND_HOME_PATH}/opp/vendors/custom_math/op_api/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
22+ 
23+ENV FAISS_PYTHON_VERSION=${PYTHON_VERSION}
24+ENV FAISS_NPU_COMPUTE_UNIT=ascend910b
25+ 
26+WORKDIR /opt/faiss
27+ 
28+COPY faiss/npu/docker/install_dependencies.sh /tmp/install_dependencies.sh
29+RUN bash /tmp/install_dependencies.sh && rm -f /tmp/install_dependencies.sh
30+ 
31+COPY . /opt/faiss
32+RUN bash faiss/npu/docker/build_and_install.sh "${FAISS_NPU_COMPUTE_UNIT}"
33+ 
34+CMD ["/bin/bash"]
L
Llan_xin25 天前

严重程度: 提示 问题: Dockerfile 未设置任何 LABEL 元数据(maintainer、version、description、source 等),不利于镜像追溯和管理。本 Dockerfile 及同目录其他 3 个 Dockerfile 均有此问题。 原因: OCI 规范推荐通过 LABEL 标注镜像来源、版本、描述等信息,便于在镜像仓库中检索、审计和追踪镜像来源;缺失元数据不利于企业镜像治理。 怎么改: 在 FROM 之后添加 LABEL:

LABEL org.opencontainers.image.source="https://gitcode.com/Ascend/faiss" \
      org.opencontainers.image.version="${CANN_VERSION}" \
      org.opencontainers.image.description="Faiss NPU build image for Ascend 910B on openEuler 24.03" \
      maintainer="Ascend/faiss maintainers"
likedislike
haozhang
haozhang
25 天前 评论:
@@ -0,0 +1,34 @@
1+# Copyright (c) Meta Platforms, Inc. and affiliates.
2+#
3+# This source code is licensed under the MIT license found in the
4+# LICENSE file in the root directory of this source tree.
5+ 
6+ARG CANN_REGISTRY=swr.cn-south-1.myhuaweicloud.com/ascendhub
7+ARG CANN_VERSION=9.1.0
8+ARG PYTHON_VERSION=3.12
9+ARG CANN_IMAGE_DIGEST=sha256:2e355805608859c10ad09813ad1745006b491d1ffe59d86025e1ce1fd6fcd98a
10+FROM ${CANN_REGISTRY}/cann:${CANN_VERSION}-910b-ubuntu22.04-py${PYTHON_VERSION}@${CANN_IMAGE_DIGEST}
11+ARG CANN_VERSION
12+ARG PYTHON_VERSION
13+ARG FAISS_BUILD_JOBS
14+ 
15+LABEL org.opencontainers.image.source="https://gitcode.com/Ascend/faiss" \
16+ org.opencontainers.image.version="${CANN_VERSION}" \
17+ org.opencontainers.image.description="Faiss NPU build image for Ascend 910B on Ubuntu 22.04" \
18+ maintainer="Ascend/faiss maintainers"
19+ 
20+ENV ASCEND_CUSTOM_OPP_PATH=${ASCEND_HOME_PATH}/opp/vendors/custom_math \
21+ LD_LIBRARY_PATH=${ASCEND_HOME_PATH}/opp/vendors/custom_math/op_api/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
22+ 
23+ENV FAISS_PYTHON_VERSION=${PYTHON_VERSION}
24+ENV FAISS_NPU_COMPUTE_UNIT=ascend910b
25+ 
26+WORKDIR /opt/faiss
27+ 
28+COPY faiss/npu/docker/install_dependencies.sh /tmp/install_dependencies.sh
29+RUN bash /tmp/install_dependencies.sh && rm -f /tmp/install_dependencies.sh
30+ 
31+COPY . /opt/faiss
YY
Yyihao123422 天前

严重程度: 建议

问题: 删除根目录 .dockerignore 后,COPY . /opt/faiss 会将整个仓库(含 .git、测试数据等)纳入构建上下文和镜像

原因: 本 PR 删除了根目录的 .dockerignore(原内容为 sift1M),但未在仓库根目录或 faiss/npu/docker/ 目录添加替代的 .dockerignore 文件。所有 Dockerfile 中的 COPY . /opt/faiss 会将仓库中所有文件复制到镜像中,包括 .git 目录(通常数百MB)、sift1M 测试数据集(如果存在)、build 构建产物等。这不仅显著增大 Docker 构建上下文传输时间和最终镜像体积,还可能将敏感文件(如本地配置、密钥等)意外带入镜像。此前的 .dockerignore 虽然仅排除 sift1M,但至少提供了一个可扩展的排除机制。

怎么改: 在仓库根目录添加新的 .dockerignore 文件,至少排除以下内容:.git、build、.whl、sift1M、pycache、.pyc、.dockerignore 自身等。也可参考此前机器人摘要中提到的方案,统一排除 sift1M、/build/、/faiss/npu/ops/build/、/faiss/npu/ops/build_out/、/.git/ 等冗余目录。

likedislike
haozhang
haozhang
22 天前 评论:
Yyihao123422 天前

严重程度: 严重

问题: 根目录 .dockerignore 被删除且未添加替代文件,导致 COPY . /opt/faiss 将整个构建上下文(包括 .git 目录、sift1M 测试数据集、build/ 和 dist/ 等构建产物)全部复制到镜像中。

原因: 原 .dockerignore 包含 sift1M 一行,用于排除 sift1M 数据集。PR 描述提到"增加 Dockerfile 专用 .dockerignore",但实际未添加任何新的 .dockerignore 文件。Docker 构建上下文以仓库根目录(.)为根,.dockerignore 必须放在构建上下文根目录才能生效。删除后,COPY . /opt/faiss 会将所有文件送入镜像,显著增大构建上下文传输时间和最终镜像体积,尤其 sift1M 数据集和 .git 目录通常体积较大。

怎么改: 在仓库根目录创建或恢复 .dockerignore,至少排除不需要的文件:

# 数据集
sift1M
*.bin
*.fvecs
*.ivecs

# 构建产物
build/
dist/
*.whl
*.egg-info

# 版本控制
.git/
.gitignore

# IDE 和临时文件
.idea/
.vscode/
*.swp
*~
__pycache__/
*.pyc
likedislike
haozhang
haozhang
22 天前 评论:
32+RUN bash faiss/npu/docker/build_and_install.sh "${FAISS_NPU_COMPUTE_UNIT}"
33+ 
34+CMD ["/bin/bash"]
@@ -0,0 +1,34 @@
1+# Copyright (c) Meta Platforms, Inc. and affiliates.
2+#
3+# This source code is licensed under the MIT license found in the
4+# LICENSE file in the root directory of this source tree.
5+ 
6+ARG CANN_REGISTRY=swr.cn-south-1.myhuaweicloud.com/ascendhub
7+ARG CANN_VERSION=9.1.0
8+ARG PYTHON_VERSION=3.12
9+ARG CANN_IMAGE_DIGEST=sha256:6f3d3d4fa926d2525cf184f4a9f9944a08155800a98c8a188b2e388674085f87
10+FROM ${CANN_REGISTRY}/cann:${CANN_VERSION}-a3-openeuler24.03-py${PYTHON_VERSION}@${CANN_IMAGE_DIGEST}
11+ARG CANN_VERSION
12+ARG PYTHON_VERSION
13+ARG FAISS_BUILD_JOBS
14+ 
15+LABEL org.opencontainers.image.source="https://gitcode.com/Ascend/faiss" \
16+ org.opencontainers.image.version="${CANN_VERSION}" \
17+ org.opencontainers.image.description="Faiss NPU build image for Ascend A3 on openEuler 24.03" \
18+ maintainer="Ascend/faiss maintainers"
19+ 
20+ENV ASCEND_CUSTOM_OPP_PATH=${ASCEND_HOME_PATH}/opp/vendors/custom_math \
21+ LD_LIBRARY_PATH=${ASCEND_HOME_PATH}/opp/vendors/custom_math/op_api/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
22+ 
23+ENV FAISS_PYTHON_VERSION=${PYTHON_VERSION}
24+ENV FAISS_NPU_COMPUTE_UNIT=ascend910_93
25+ 
26+WORKDIR /opt/faiss
27+ 
28+COPY faiss/npu/docker/install_dependencies.sh /tmp/install_dependencies.sh
29+RUN bash /tmp/install_dependencies.sh && rm -f /tmp/install_dependencies.sh
30+ 
31+COPY . /opt/faiss
32+RUN bash faiss/npu/docker/build_and_install.sh "${FAISS_NPU_COMPUTE_UNIT}"
33+ 
34+CMD ["/bin/bash"]
@@ -0,0 +1,34 @@
1+# Copyright (c) Meta Platforms, Inc. and affiliates.
2+#
3+# This source code is licensed under the MIT license found in the
4+# LICENSE file in the root directory of this source tree.
5+ 
6+ARG CANN_REGISTRY=swr.cn-south-1.myhuaweicloud.com/ascendhub
7+ARG CANN_VERSION=9.1.0
8+ARG PYTHON_VERSION=3.12
9+ARG CANN_IMAGE_DIGEST=sha256:a4356785f9ba992ba9d5170e355766c63a27722bc44af93ae4b940029011912d
10+FROM ${CANN_REGISTRY}/cann:${CANN_VERSION}-a3-ubuntu22.04-py${PYTHON_VERSION}@${CANN_IMAGE_DIGEST}
11+ARG CANN_VERSION
12+ARG PYTHON_VERSION
13+ARG FAISS_BUILD_JOBS
14+ 
15+LABEL org.opencontainers.image.source="https://gitcode.com/Ascend/faiss" \
16+ org.opencontainers.image.version="${CANN_VERSION}" \
17+ org.opencontainers.image.description="Faiss NPU build image for Ascend A3 on Ubuntu 22.04" \
18+ maintainer="Ascend/faiss maintainers"
19+ 
20+ENV ASCEND_CUSTOM_OPP_PATH=${ASCEND_HOME_PATH}/opp/vendors/custom_math \
21+ LD_LIBRARY_PATH=${ASCEND_HOME_PATH}/opp/vendors/custom_math/op_api/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
22+ 
23+ENV FAISS_PYTHON_VERSION=${PYTHON_VERSION}
24+ENV FAISS_NPU_COMPUTE_UNIT=ascend910_93
25+ 
26+WORKDIR /opt/faiss
27+ 
28+COPY faiss/npu/docker/install_dependencies.sh /tmp/install_dependencies.sh
29+RUN bash /tmp/install_dependencies.sh && rm -f /tmp/install_dependencies.sh
30+ 
31+COPY . /opt/faiss
32+RUN bash faiss/npu/docker/build_and_install.sh "${FAISS_NPU_COMPUTE_UNIT}"
33+ 
34+CMD ["/bin/bash"]
@@ -0,0 +1,116 @@
1+# Faiss NPU Docker 镜像
Y
Yyihao123416 天前

昇腾的镜像资料都是统一模板,严格按照https://gitcode.com/Ascend/IndexSDK/blob/master/docker/OVERVIEW.zh.md 模板来写。验证内容贴到issue下就行了

likedislike
haozhang
haozhang
16 天前 评论:
2+ 
3+本目录提供在 Ascend NPU 环境中构建和使用 Faiss NPU 的 Docker 镜像方案。镜像面向 `linux/arm64`,支持 Ascend 910B(A2)和 A3,以及 Ubuntu 22.04 和 openEuler 24.03。
4+ 
5+## 1.快速参考
6+ 
7+- [问题反馈](https://gitcode.com/Ascend/faiss/issues)
8+- [Faiss 代码仓](https://gitcode.com/Ascend/faiss)
9+- [Faiss NPU 文档](../docs/zh/README.md)
10+- [Faiss NPU 快速入门](../docs/zh/01_quick_start.md)
11+ 
12+## 2.支持的 Tags 及 Dockerfile 链接
13+ 
14+### 2.1 Tag 规范
15+ 
16+本目录的本地构建 Tag 遵循以下格式:
17+ 
18+```bash
19+faiss-npu:<cann版本>-<芯片系列>-<操作系统>-py<python版本>
20+```
21+ 
22+| 字段 | 示例值 | 说明 |
23+| --- | --- | --- |
24+| `cann版本` | `9.1.0` | 容器内 CANN Toolkit 版本 |
25+| `芯片系列` | `910b`、`a3` | 目标芯片系列 |
26+| `操作系统` | `ubuntu22.04`、`openeuler24.03` | 基础操作系统 |
27+| `python版本` | `3.12` | Python 版本 |
28+ 
29+### 2.2 CANN 9.1.0 + Faiss 1.13.2 镜像
30+ 
31+ 
32+| Tag | Dockerfile | 镜像内容 |
33+| --- | --- | --- |
34+| `9.1.0-910b-ubuntu22.04-py3.12` | [Dockerfile.910b.ubuntu](Dockerfile.910b.ubuntu) | CANN Toolkit + Faiss NPU(Ascend 910B,Ubuntu 22.04) |
35+| `9.1.0-a3-ubuntu22.04-py3.12` | [Dockerfile.a3.ubuntu](Dockerfile.a3.ubuntu) | CANN Toolkit + Faiss NPU(Ascend A3,Ubuntu 22.04) |
36+| `9.1.0-910b-openeuler24.03-py3.12` | [Dockerfile.910b.openeuler](Dockerfile.910b.openeuler) | CANN Toolkit + Faiss NPU(Ascend 910B,openEuler 24.03) |
37+| `9.1.0-a3-openeuler24.03-py3.12` | [Dockerfile.a3.openeuler](Dockerfile.a3.openeuler) | CANN Toolkit + Faiss NPU(Ascend A3,openEuler 24.03) |
38+ 
39+## 3.快速开始
40+ 
41+### 3.1 前置要求
42+ 
43+#### 3.1.1 安装驱动
44+ 
45+- 主机必须安装与容器内 CANN 版本兼容的 Ascend NPU 驱动。请参阅 [CANN 兼容性矩阵](https://www.hiascend.com/document) 确认驱动与 CANN 的对应关系。
46+- 建议使用 Docker 24.0.x 或更高版本。
47+- 在 `linux/arm64` 主机上构建并运行对应芯片和操作系统的镜像。
48+ 
49+### 3.2 运行 Faiss NPU 容器
50+ 
51+#### 手动挂载设备
52+ 
53+- 通过 `--device` 将 NPU 设备文件挂载到容器中。`/dev/davinci0` 为按需挂载的 NPU 设备;`/dev/davinci_manager`、`/dev/devmm_svm` 和 `/dev/hisi_hdc` 为 NPU 管理设备。
54+- 以只读方式挂载宿主机的驱动文件、DCMI 和 `npu-smi`,使容器能够使用宿主机驱动环境。
55+- 将命令末尾的镜像 Tag 替换为第 2.2 节中与目标环境匹配的本地标签。
56+ 
57+```bash
58+docker run --rm -it --name faiss_npu_container --ipc=host \
59+ --device /dev/davinci0 \
60+ --device /dev/davinci_manager \
61+ --device /dev/devmm_svm \
62+ --device /dev/hisi_hdc \
63+ -v /usr/local/dcmi:/usr/local/dcmi:ro \
64+ -v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi:ro \
65+ -v /usr/local/Ascend/driver/lib64:/usr/local/Ascend/driver/lib64:ro \
66+ -v /usr/local/Ascend/driver/version.info:/usr/local/Ascend/driver/version.info:ro \
67+ -v /etc/ascend_install.info:/etc/ascend_install.info:ro \
68+ faiss-npu:9.1.0-910b-ubuntu22.04-py3.12 bash
69+```
70+ 
71+### 3.3 镜像内 Faiss NPU 环境
72+ 
73+ 
74+- 镜像构建时会根据目标芯片编译并安装 Faiss NPU 和自定义数学算子:910B 使用 `ascend910b`,A3 使用 `ascend910_93`。
75+- 自定义算子安装在 `${ASCEND_HOME_PATH}/opp` 下;镜像已设置 `ASCEND_CUSTOM_OPP_PATH` 和相关 `LD_LIBRARY_PATH`。
76+- `faiss-ascend` Python wheel 已安装至镜像的 Python 环境。有关 Faiss NPU 的使用方式,请参阅 [Faiss NPU 文档](../docs/zh/README.md)。
77+ 
78+### 3.4 如何本地构建
79+ 
80+在仓库根目录执行。以下示例构建 Ascend 910B、Ubuntu 22.04 对应的镜像:
81+ 
82+```bash
83+docker build --platform linux/arm64 \
84+ -f faiss/npu/docker/Dockerfile.910b.ubuntu \
85+ -t faiss-npu:9.1.0-910b-ubuntu22.04-py3.12 \
86+ .
87+```
L
Llan_xin23 天前

增加python test_ivfpq_accuracy.py 的验证

likedislike
haozhang
haozhang
23 天前 评论:
88+ 
89+将 Dockerfile 文件名和 Tag 替换为第 2.2 节中的其他组合,即可构建对应镜像。
90+ 
91+如需使用其他镜像仓或 CANN 版本,可通过 `CANN_REGISTRY`、`CANN_VERSION`、`PYTHON_VERSION` 和 `CANN_IMAGE_DIGEST` 传入构建参数。各 Dockerfile 都以 `tag@sha256` 形式固定基础镜像;变更版本时,应同时更新与该镜像匹配的 manifest digest。
92+ 
93+```bash
94+docker build --platform linux/arm64 \
95+ --build-arg CANN_REGISTRY=swr.cn-south-1.myhuaweicloud.com/ascendhub \
96+ --build-arg CANN_VERSION=9.1.0 \
97+ --build-arg PYTHON_VERSION=3.12 \
98+ --build-arg CANN_IMAGE_DIGEST=sha256:6f3d3d4fa926d2525cf184f4a9f9944a08155800a98c8a188b2e388674085f87 \
99+ -f faiss/npu/docker/Dockerfile.a3.openeuler \
100+ -t faiss-npu:9.1.0-a3-openeuler24.03-py3.12 \
101+ .
102+```
103+ 
104+默认构建并行度为 `min(nproc, 32)`。可使用正整数 `FAISS_BUILD_JOBS` 覆盖该值:
105+ 
106+```bash
107+docker build --platform linux/arm64 \
108+ --build-arg FAISS_BUILD_JOBS=16 \
109+ -f faiss/npu/docker/Dockerfile.910b.ubuntu \
110+ -t faiss-npu:9.1.0-910b-ubuntu22.04-py3.12 \
111+ .
112+```
113+ 
114+## 4.许可证
115+ 
116+Faiss 使用 [MIT 许可证](../../../LICENSE)。镜像中包含的 CANN 软件请参阅其 [许可证信息](https://github.com/Ascend/cann-container-image/blob/main/LICENSE);预装的 Python 软件包和系统库可能分别受其自身许可证约束。
@@ -0,0 +1,187 @@
1+#!/usr/bin/env bash
2+# Copyright (c) Meta Platforms, Inc. and affiliates.
3+#
4+# This source code is licensed under the MIT license found in the
5+# LICENSE file in the root directory of this source tree.
6+ 
7+set -euo pipefail
8+ 
9+if [[ $# -ne 1 ]]; then
10+ echo "Usage: $0 <ascend910b|ascend910_93>" >&2
11+ exit 2
12+fi
13+ 
14+readonly compute_unit="$1"
15+case "${compute_unit}" in
16+ ascend910b|ascend910_93) ;;
17+ *)
18+ echo "[ERROR] unsupported compute unit: ${compute_unit}" >&2
19+ exit 2
20+ ;;
21+esac
22+ 
23+readonly repo_root="${FAISS_ROOT:-/opt/faiss}"
24+readonly requested_python_version="${FAISS_PYTHON_VERSION:-}"
25+readonly cann_setenv="/usr/local/Ascend/ascend-toolkit/set_env.sh"
26+ 
27+if [[ ! -f "${repo_root}/CMakeLists.txt" ]]; then
28+ echo "[ERROR] Faiss source tree not found at ${repo_root}" >&2
29+ exit 1
30+fi
31+if [[ -z "${requested_python_version}" ]]; then
32+ echo "[ERROR] FAISS_PYTHON_VERSION is not set" >&2
33+ exit 1
34+fi
35+if [[ ! -r "${cann_setenv}" ]]; then
36+ echo "[ERROR] CANN environment script not found: ${cann_setenv}" >&2
37+ exit 1
38+fi
39+ 
40+active_python_version="$(python3 -c 'import sys; print(f"{sys.version_info[0]}.{sys.version_info[1]}")')"
41+if [[ "${active_python_version}" != "${requested_python_version}" ]]; then
42+ echo "[ERROR] python3 version ${active_python_version} does not match requested ${requested_python_version}" >&2
43+ exit 1
44+fi
45+ 
46+python_include_dir="$(python3 -c "import sysconfig; print(sysconfig.get_path('include'))")"
47+if [[ ! -f "${python_include_dir}/Python.h" ]]; then
48+ echo "[ERROR] Python.h not found in sysconfig include dir: ${python_include_dir}" >&2
49+ exit 1
50+fi
51+ 
52+# CANN set_env.sh may reference unset variables; temporarily disable nounset.
53+set +u
54+source "${cann_setenv}"
55+set -u
56+ 
57+if [[ -z "${ASCEND_HOME_PATH:-}" ]]; then
58+ echo "[ERROR] ASCEND_HOME_PATH is empty after sourcing ${cann_setenv}" >&2
59+ exit 1
60+fi
61+if [[ ! -d "${ASCEND_HOME_PATH}/opp" ]]; then
62+ echo "[ERROR] CANN operator directory not found: ${ASCEND_HOME_PATH}/opp" >&2
63+ exit 1
64+fi
65+ 
66+cd "${repo_root}/faiss/npu/ops"
67+bash ops_build.sh --compute-unit="${compute_unit}"
68+ 
69+readonly ops_output_dir="${repo_root}/faiss/npu/ops/build_out"
70+if [[ ! -d "${ops_output_dir}" ]]; then
71+ echo "[ERROR] custom ops output directory was not generated: ${ops_output_dir}" >&2
72+ exit 1
73+fi
74+ 
75+package_lists_dir="$(mktemp -d)"
76+readonly package_lists_dir
77+trap 'rm -rf -- "${package_lists_dir}"' EXIT
78+ 
79+readonly ops_packages_list="${package_lists_dir}/ops-packages"
80+if ! find "${ops_output_dir}" -maxdepth 1 -type f \
81+ -name 'cann-ops-math-custom_*.run' -print \
82+ | LC_ALL=C sort > "${ops_packages_list}"; then
83+ echo "[ERROR] failed to enumerate custom ops packages in ${ops_output_dir}" >&2
84+ exit 1
85+fi
86+mapfile -t ops_packages < "${ops_packages_list}"
87+if [[ ${#ops_packages[@]} -ne 1 ]]; then
88+ echo "[ERROR] expected exactly one custom ops package, found ${#ops_packages[@]}" >&2
89+ exit 1
90+fi
91+readonly ops_package="${ops_packages[0]}"
92+ 
93+rm -rf "${repo_root}/build"
94+mkdir -p "${repo_root}/build"
95+ 
96+cmake -S "${repo_root}" -B "${repo_root}/build" \
97+ -DCMAKE_BUILD_TYPE=Release \
98+ -DBUILD_SHARED_LIBS=OFF \
99+ -DFAISS_ENABLE_GPU=OFF \
100+ -DFAISS_ENABLE_NPU=ON \
101+ -DFAISS_ENABLE_PYTHON=ON \
102+ -DFAISS_ENABLE_EXTRAS=OFF \
103+ -DBUILD_TESTING=OFF
104+ 
105+readonly max_default_build_jobs=32
106+available_build_jobs="$(nproc)"
107+if [[ ! "${available_build_jobs}" =~ ^[1-9][0-9]*$ ]]; then
108+ echo \
109+ "[ERROR] nproc returned an invalid build job count: ${available_build_jobs}" \
110+ >&2
111+ exit 1
112+fi
113+readonly available_build_jobs
114+ 
115+if [[ -n "${FAISS_BUILD_JOBS:-}" ]]; then
116+ build_jobs="${FAISS_BUILD_JOBS}"
117+else
118+ build_jobs="${available_build_jobs}"
119+ if (( build_jobs > max_default_build_jobs )); then
120+ build_jobs="${max_default_build_jobs}"
121+ fi
122+fi
123+if [[ ! "${build_jobs}" =~ ^[1-9][0-9]*$ ]]; then
124+ echo "[ERROR] FAISS_BUILD_JOBS must be a positive integer: ${build_jobs}" >&2
125+ exit 1
126+fi
127+readonly build_jobs
128+echo "[INFO] building Faiss with ${build_jobs} parallel job(s)"
129+cmake --build "${repo_root}/build" --parallel "${build_jobs}"
130+ 
131+cd "${repo_root}/build/faiss/python"
132+python3 -m build --wheel --no-isolation
133+readonly wheels_list="${package_lists_dir}/wheels"
134+if ! find dist -maxdepth 1 -type f -name '*.whl' -print \
135+ | LC_ALL=C sort > "${wheels_list}"; then
136+ echo "[ERROR] failed to enumerate Faiss wheels" >&2
137+ exit 1
138+fi
139+mapfile -t wheels < "${wheels_list}"
140+if [[ ${#wheels[@]} -ne 1 ]]; then
141+ echo "[ERROR] expected exactly one Faiss wheel, found ${#wheels[@]}" >&2
142+ exit 1
143+fi
144+# Keep the pinned runtime dependencies (currently numpy and packaging) installed
145+# by install_dependencies.sh. The metadata check below makes newly declared
146+# wheel dependencies fail the image build instead of being silently omitted by
147+# --no-deps, without failing on unrelated packages from the CANN base image.
148+python3 -m pip install --force-reinstall --no-deps "${wheels[0]}"
149+python3 - <<'PY'
150+from importlib.metadata import PackageNotFoundError, distribution
151+ 
152+from packaging.requirements import Requirement
153+ 
154+ 
155+missing_or_incompatible = []
156+for requirement_text in distribution("faiss-ascend").requires or []:
157+ requirement = Requirement(requirement_text)
158+ if requirement.marker is not None and not requirement.marker.evaluate():
159+ continue
160+ try:
161+ installed_version = distribution(requirement.name).version
162+ except PackageNotFoundError:
163+ missing_or_incompatible.append(f"{requirement.name} is not installed")
164+ continue
165+ if requirement.specifier and not requirement.specifier.contains(
166+ installed_version,
167+ prereleases=True,
168+ ):
169+ missing_or_incompatible.append(
170+ f"{requirement.name} {installed_version} does not satisfy "
171+ f"{requirement.specifier}"
172+ )
173+ 
174+if missing_or_incompatible:
175+ details = "; ".join(missing_or_incompatible)
176+ raise SystemExit(f"[ERROR] faiss-ascend runtime dependency check failed: {details}")
177+ 
178+print("[INFO] faiss-ascend runtime dependency check passed")
179+PY
180+ 
181+bash "${ops_package}" --install-path="${ASCEND_HOME_PATH}/opp"
182+ 
183+# This script runs in one Docker RUN layer, so remove build-only artifacts here.
184+rm -rf \
185+ "${repo_root}/build" \
186+ "${repo_root}/faiss/npu/ops/build" \
187+ "${repo_root}/faiss/npu/ops/build_out"
@@ -0,0 +1,72 @@
1+#!/usr/bin/env bash
2+# Copyright (c) Meta Platforms, Inc. and affiliates.
3+#
4+# This source code is licensed under the MIT license found in the
5+# LICENSE file in the root directory of this source tree.
6+ 
7+set -euo pipefail
8+ 
9+if [[ ! -r /etc/os-release ]]; then
10+ echo "[ERROR] /etc/os-release is missing; unsupported base image" >&2
11+ exit 1
12+fi
13+ 
14+source /etc/os-release
15+os_id="${ID,,}"
16+ 
17+case "${os_id}" in
18+ ubuntu)
19+ export DEBIAN_FRONTEND=noninteractive
20+ apt-get update
21+ apt-get install -y --no-install-recommends \
22+ build-essential \
23+ ca-certificates \
24+ gawk \
25+ libopenblas-dev \
26+ libopenblas0 \
27+ python3-dev \
28+ python3-pip \
29+ swig
30+ rm -rf /var/lib/apt/lists/*
31+ ;;
32+ openeuler)
33+ if command -v dnf >/dev/null 2>&1; then
34+ package_manager="dnf"
35+ package_manager_install_options=(-y --setopt=install_weak_deps=False)
36+ elif command -v yum >/dev/null 2>&1; then
37+ package_manager="yum"
38+ # Legacy yum does not support or install weak dependencies.
39+ package_manager_install_options=(-y)
40+ else
41+ echo "[ERROR] neither dnf nor yum is available in the openEuler image" >&2
42+ exit 1
43+ fi
44+ "${package_manager}" install "${package_manager_install_options[@]}" \
45+ ca-certificates \
46+ gawk \
47+ gcc \
48+ gcc-c++ \
49+ make \
50+ openblas \
51+ openblas-devel \
52+ python3-devel \
53+ python3-pip \
54+ swig
55+ "${package_manager}" clean all
56+ rm -rf /var/cache/dnf /var/cache/yum
57+ ;;
58+ *)
59+ echo "[ERROR] unsupported operating system ID: ${ID}" >&2
60+ exit 1
61+ ;;
62+esac
63+ 
64+python3 -m pip install --upgrade --no-cache-dir "pip==24.2"
65+ 
66+python3 -m pip install --no-cache-dir \
67+ "build==1.2.1" \
68+ "cmake==3.26.4" \
69+ "numpy==1.26.4" \
70+ "packaging==24.1" \
71+ "setuptools==69.5.1" \
72+ "wheel==0.43.0"