已合并
add .devcontainer and .vscode #1474
zhugexun_sjtu创建于 4月8日
add .devcontainer and .vscode #1474
已合并
zhugexun_sjtu创建于 4月8日
6 个文件变更+348-0
@@ -0,0 +1,146 @@
1+# Build:
2+# docker buildx build --network host -t ascendc:ubuntu24.04 .
3+#
4+# Run (standalone, without devcontainer):
5+# docker run -it --runtime=runc --privileged \
6+# --net=host --pid=host --ipc=host \
7+# --cap-add=SYS_PTRACE \
8+# -e ASCEND_VISIBLE_DEVICES=all \
9+# --device=/dev/davinci_manager \
10+# --device=/dev/devmm_svm \
11+# --device=/dev/hisi_hdc \
12+# -v /usr/local/Ascend/driver:/usr/local/Ascend/driver:ro \
13+# -v /usr/local/Ascend/add-ons:/usr/local/Ascend/add-ons:ro \
14+# -v /usr/local/dcmi:/usr/local/dcmi:ro \
15+# -v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi:ro \
16+# -v /etc/ascend_install.info:/etc/ascend_install.info:ro \
17+# -v /var/log/npu:/var/log/npu \
18+# -v /usr/slog:/usr/slog \
19+# ascendc:ubuntu24.04
20+#
21+# Mirror sources (override with --build-arg):
22+# APT_MIRROR: mirrors.huaweicloud.com (default) | mirrors.tuna.tsinghua.edu.cn | mirrors.ustc.edu.cn
23+# CONDA_MIRROR: mirrors.tuna.tsinghua.edu.cn/anaconda (default) | mirrors.ustc.edu.cn/anaconda
24+# PYPI_MIRROR: repo.huaweicloud.com/repository/pypi/simple (default) | pypi.tuna.tsinghua.edu.cn/simple
25+ 
26+# Stage 0: extract conda (multi-arch, matches host architecture automatically)
27+FROM continuumio/miniconda3 AS conda-base
28+ 
29+# Stage 1: main image
30+FROM ubuntu:24.04
31+ 
32+# ============================================================
33+# Build arguments — mirror sources
34+# ============================================================
35+ARG APT_MIRROR=mirrors.huaweicloud.com
36+ARG CONDA_MIRROR=https://mirrors.tuna.tsinghua.edu.cn/anaconda
37+ARG PYPI_MIRROR=https://repo.huaweicloud.com/repository/pypi/simple
38+ARG REPO_MIRROR=https://mirrors.tuna.tsinghua.edu.cn/git/git-repo
39+ 
40+# ============================================================
41+# Base environment
42+# ============================================================
43+ENV TZ=Asia/Shanghai \
44+ LANG=C.UTF-8 \
45+ SHELL=/bin/zsh \
46+ DEBIAN_FRONTEND=noninteractive \
47+ PATH=/opt/conda/bin:$PATH
48+ 
49+USER root
50+WORKDIR /tmp
51+ 
52+# ============================================================
53+# APT mirror & system packages (single layer to avoid stale cache)
54+# ============================================================
55+RUN arch="$(dpkg --print-architecture)" && \
56+ if [ "${arch}" = "arm64" ]; then \
57+ sed -i "s@//ports.ubuntu.com@//${APT_MIRROR}@g" /etc/apt/sources.list.d/ubuntu.sources; \
58+ else \
59+ sed -i "s@//archive.ubuntu.com@//${APT_MIRROR}@g; s@//security.ubuntu.com@//${APT_MIRROR}@g" \
60+ /etc/apt/sources.list.d/ubuntu.sources; \
61+ fi && \
62+ apt update && \
63+ apt install -y --no-install-recommends \
64+ tzdata lsb-release curl wget gnupg2 openssl ca-certificates openssh-client \
65+ build-essential cmake ninja-build ccache pkg-config autoconf automake libtool gperf \
66+ gdb gdbserver git git-lfs lcov vim htop tree zsh \
67+ && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone \
68+ && rm -rf /var/lib/apt/lists/*
69+ 
70+# ============================================================
71+# repo tool
72+# ============================================================
73+RUN curl -fsSL ${REPO_MIRROR} -o /usr/local/bin/repo && \
74+ chmod a+x /usr/local/bin/repo
75+ 
76+# ============================================================
77+# git global config
78+# ============================================================
79+RUN git config --global core.autocrlf input && \
80+ git config --global core.editor vim && \
81+ git config --global color.ui auto && \
82+ git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
83+ 
84+# ============================================================
85+# Trust gitcode.com SSH host key
86+# ============================================================
87+RUN mkdir -p /root/.ssh && \
88+ ssh-keyscan -H gitcode.com >> /root/.ssh/known_hosts && \
89+ chmod 600 /root/.ssh/known_hosts
90+ 
91+# ============================================================
92+# oh-my-zsh + plugins (via gitcode mirror)
93+# ============================================================
94+RUN git clone --depth=1 https://gitcode.com/ohmyzsh/ohmyzsh.git /root/.oh-my-zsh && \
95+ cp /root/.oh-my-zsh/templates/zshrc.zsh-template /root/.zshrc && \
96+ git clone --depth=1 https://gitcode.com/zsh-users/zsh-autosuggestions \
97+ /root/.oh-my-zsh/custom/plugins/zsh-autosuggestions && \
98+ git clone --depth=1 https://gitcode.com/zsh-users/zsh-syntax-highlighting \
99+ /root/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting && \
100+ sed -i 's/plugins=(git)/plugins=(git z zsh-autosuggestions zsh-syntax-highlighting)/' /root/.zshrc && \
101+ sed -i 's/ZSH_THEME="robbyrussell"/ZSH_THEME="agnoster"/' /root/.zshrc && \
102+ chsh -s "$(which zsh)" root
103+ 
104+# ============================================================
105+# conda — copied from stage 0, no architecture-specific download needed
106+# ============================================================
107+COPY --from=conda-base /opt/conda /opt/conda
108+ 
109+RUN conda config --remove channels defaults 2>/dev/null || true && \
110+ conda config --add channels ${CONDA_MIRROR}/pkgs/free && \
111+ conda config --add channels ${CONDA_MIRROR}/pkgs/main && \
112+ conda config --set show_channel_urls yes && \
113+ conda config --set channel_priority strict
114+ 
115+# ============================================================
116+# Python dependencies
117+# ============================================================
118+RUN printf "[global]\nindex-url = ${PYPI_MIRROR}\n" > /etc/pip.conf
119+ 
120+COPY environment-312.yml /tmp/
121+RUN conda env create -q -f /tmp/environment-312.yml && \
122+ conda clean -ya && \
123+ pip cache purge 2>/dev/null || true
124+ 
125+# ============================================================
126+# Shell customization
127+# ============================================================
128+RUN conda init zsh && \
129+ echo "conda activate py312" >> /root/.zshrc && \
130+ echo "export REPO_URL=\"${REPO_MIRROR}\"" >> /root/.zshrc
131+ 
132+# AscendC banner
133+RUN printf '\n\
134+echo "\\033[1;36m"\n\
135+echo " █████╗ ███████╗ ██████╗███████╗███╗ ██╗██████╗ ██████╗"\n\
136+echo "██╔══██╗██╔════╝██╔════╝██╔════╝████╗ ██║██╔══██╗██╔════╝"\n\
137+echo "███████║███████╗██║ █████╗ ██╔██╗ ██║██║ ██║██║ "\n\
138+echo "██╔══██║╚════██║██║ ██╔══╝ ██║╚██╗██║██║ ██║██║ "\n\
139+echo "██║ ██║███████║╚██████╗███████╗██║ ╚████║██████╔╝╚██████╗"\n\
140+echo "╚═╝ ╚═╝╚══════╝ ╚═════╝╚══════╝╚═╝ ╚═══╝╚═════╝ ╚═════╝"\n\
141+echo "\\033[0m"\n' >> /root/.zshrc
142+ 
143+RUN rm -rf /tmp/*
144+ 
145+WORKDIR /root
146+ENTRYPOINT ["/bin/zsh"]
@@ -0,0 +1,91 @@
1+# Dev Container
2+ 
3+Ubuntu 24.04 development environment for AscendC NPU kernel development.
4+ 
5+## Prerequisites
6+ 
7+| Requirement | Version |
8+|-------------|---------|
9+| Docker | ≥ 23.0.0 |
10+| docker buildx | any |
11+| Ascend driver | installed on host |
12+ 
13+> The Ascend **driver** is installed on the host and mounted read-only into the container (`/usr/local/Ascend/driver`, `/usr/local/Ascend/add-ons`).
14+>
15+> The **CANN toolkit and ops packages** should be installed inside the container, not on the host. Installing CANN on the host risks overwriting a shared environment when multiple developers use the same machine. After opening the container, install the matching CANN packages **manually**.
16+ 
17+Check your Docker version:
18+```bash
19+docker --version
20+```
21+ 
22+Install buildx if missing:
23+```bash
24+mkdir -p /usr/local/lib/docker/cli-plugins
25+ARCH=$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')
26+curl -fsSL "https://github.com/docker/buildx/releases/latest/download/buildx-linux-${ARCH}" \
27+ -o /usr/local/lib/docker/cli-plugins/docker-buildx
28+chmod +x /usr/local/lib/docker/cli-plugins/docker-buildx
29+```
30+ 
31+## Build the Image
32+ 
33+> **Build time:** approximately 5 minutes on a typical connection. The dominant cost is downloading the conda environment and PyTorch. Subsequent rebuilds hit the Docker layer cache and complete in seconds if only non-package layers changed.
34+ 
35+```bash
36+docker buildx build --network host -t ascendc:ubuntu24.04 .devcontainer/
37+```
38+ 
39+To override mirror sources:
40+```bash
41+docker buildx build --network host \
42+ --build-arg APT_MIRROR=mirrors.ustc.edu.cn \
43+ --build-arg CONDA_MIRROR=https://mirrors.ustc.edu.cn/anaconda \
44+ --build-arg PYPI_MIRROR=https://pypi.mirrors.ustc.edu.cn/simple \
45+ -t ascendc:ubuntu24.04 .devcontainer/
46+```
47+ 
48+Available mirrors:
49+ 
50+| Build arg | Default | Tsinghua | USTC | Huawei |
51+|-----------|---------|----------|------|--------|
52+| `APT_MIRROR` | `mirrors.huaweicloud.com` | `mirrors.tuna.tsinghua.edu.cn` | `mirrors.ustc.edu.cn` | `mirrors.huaweicloud.com` |
53+| `CONDA_MIRROR` | `mirrors.tuna.tsinghua.edu.cn/anaconda` | `mirrors.tuna.tsinghua.edu.cn/anaconda` | `mirrors.ustc.edu.cn/anaconda` | — |
54+| `PYPI_MIRROR` | `repo.huaweicloud.com/repository/pypi/simple` | `pypi.tuna.tsinghua.edu.cn/simple` | `pypi.mirrors.ustc.edu.cn/simple` | `repo.huaweicloud.com/repository/pypi/simple` |
55+ 
56+## Open in VS Code
57+ 
58+> If the image has not been built manually, VS Code will build it automatically on first open — expect the same ~5 minute build time.
59+ 
60+Install the [Dev Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) extension, then:
61+ 
62+```
63+Ctrl+Shift+P → Dev Containers: Reopen in Container
64+```
65+ 
66+## Python Environments
67+ 
68+One conda environment is pre-installed:
69+ 
70+| Environment | Python | PyTorch |
71+|-------------|--------|---------|
72+| `py312` | 3.12 | 2.7.1 |
73+ 
74+Default active environment is `py312`.
75+ 
76+## Data Directory
77+ 
78+The `/data` mount is not included as it varies per developer. Add your own in `devcontainer.json`:
79+ 
80+```jsonc
81+// mounts
82+"source=/your/data,target=/your/data,type=bind"
83+```
84+ 
85+## NPU Devices
86+ 
87+`ASCEND_VISIBLE_DEVICES=all` exposes all available NPU cards automatically. Override at runtime if needed:
88+ 
89+```bash
90+docker run -e ASCEND_VISIBLE_DEVICES=0,1 ...
91+```
@@ -0,0 +1,57 @@
1+{
2+ "name": "ascendc",
3+ "build": {
4+ "dockerfile": "./Dockerfile",
5+ "options": ["--network=host", "--tag=ascendc:ubuntu24.04"]
6+ },
7+ "remoteUser": "root",
8+ 
9+ // --privileged covers davinci compute devices; control devices must be listed explicitly
10+ // --runtime=runc avoids conflict with ascend-docker-runtime set as Docker's default runtime
11+ "runArgs": [
12+ "--runtime=runc",
13+ "--privileged",
14+ "--net=host",
15+ "--pid=host",
16+ "--ipc=host",
17+ "--cap-add=SYS_PTRACE",
18+ "--device=/dev/davinci_manager",
19+ "--device=/dev/devmm_svm",
20+ "--device=/dev/hisi_hdc"
21+ ],
22+ "containerEnv": {
23+ // Automatically adapts to the actual number of NPU cards
24+ "ASCEND_VISIBLE_DEVICES": "all"
25+ },
26+ 
27+ "mounts": [
28+ "source=ascendc-ccache,target=/root/.ccache,type=volume",
29+ "source=/usr/local/Ascend/driver,target=/usr/local/Ascend/driver,type=bind,readonly",
30+ "source=/usr/local/Ascend/add-ons,target=/usr/local/Ascend/add-ons,type=bind,readonly",
31+ "source=/usr/local/dcmi,target=/usr/local/dcmi,type=bind,readonly",
32+ "source=/usr/local/bin/npu-smi,target=/usr/local/bin/npu-smi,type=bind,readonly",
33+ "source=/etc/ascend_install.info,target=/etc/ascend_install.info,type=bind,readonly",
34+ "source=/var/log/npu,target=/var/log/npu,type=bind",
35+ "source=/usr/slog,target=/usr/slog,type=bind"
36+ // Add your own data directory, e.g.: "source=/your/data,target=/your/data,type=bind"
37+ ],
38+ 
39+ "customizations": {
40+ "vscode": {
41+ "extensions": [
42+ "eamodio.gitlens",
43+ "ms-python.python",
44+ "ms-vscode.cpptools",
45+ "ms-vscode.cmake-tools"
46+ ],
47+ "settings": {
48+ "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
49+ "C_Cpp.default.compileCommands": "${workspaceFolder}/build/compile_commands.json",
50+ "terminal.integrated.defaultProfile.linux": "zsh",
51+ "terminal.integrated.profiles.linux": {
52+ "zsh": { "path": "/bin/zsh" }
53+ }
54+ }
55+ }
56+ }
57+}
@@ -0,0 +1,26 @@
1+name: py312
2+dependencies:
3+ - python=3.12
4+ - pip
5+ - pip:
6+ - setuptools>=75,<80
7+ - attrs
8+ - decorator
9+ - psutil
10+ - scipy
11+ - absl-py
12+ - ml-dtypes
13+ - tornado
14+ - numpy>=2.2,<2.3
15+ - pybind11>=2.13.6,<3.0.0
16+ - PyYAML
17+ - ruamel.yaml
18+ - jinja2
19+ - coverage
20+ - pytest
21+ - cloudpickle
22+ - build
23+ - onnx==1.16.0
24+ - protobuf==3.20.3
25+ - torch==2.7.1
26+ - expecttest
@@ -0,0 +1,8 @@
1+{
2+ "recommendations": [
3+ "eamodio.gitlens",
4+ "ms-python.python",
5+ "ms-vscode.cpptools",
6+ "ms-vscode.cmake-tools"
7+ ]
8+}
@@ -0,0 +1,20 @@
1+{
2+ "files.eol": "\n",
3+ "files.insertFinalNewline": true,
4+ "files.trimFinalNewlines": true,
5+ "editor.wordWrap": "on",
6+ "editor.renderWhitespace": "all",
7+ "editor.trimWhitespaceOnDelete": true,
8+ "cmake.sourceDirectory": "${workspaceFolder}",
9+ "cmake.buildDirectory": "${workspaceFolder}/build",
10+ "files.associations": {
11+ "*.asc": "cpp",
12+ "*.aicpu": "cpp",
13+ ".clang-format": "yaml",
14+ ".clangd": "yaml",
15+ "*requirements*.txt": "pip-requirements"
16+ },
17+ "[python]": {
18+ "editor.tabSize": 4
19+ }
20+}