GitHub, Git, Conda, Docker, Apptainer 环境搭建

GitHub 分布式版本控制系统

  • 生成 SSH 密钥对 用于远程访问 GitHub 账户

    ssh-keygen -o -a 100 -t ed25519
    
  • 添加 SSH 公钥到 GitHub 账户

  • 配置用户名和邮箱,用于提交代码时进行身份验证

    git config --global user.name "用户名"
    git config --global user.email "邮箱"
    

Git 常用命令

  • 克隆远程仓库到本地
    • git clone git@github.com:jinyongch/MNIST.git
  • 初始化本地仓库
    • git init
  • 将修改的文件添加到暂存区
    • git add .
  • 将暂存区内容提交到本地仓库
    • git commit -m "message"
  • 上传本地仓库代码到远程仓库
    • git push -u origin master
  • 下载远程仓库代码到本地仓库
    • git pull
  • 查看本地仓库状态
    • git status
  • 查看文件修改内容
    • git diff
  • 查看远程仓库信息
    • git remote -v
  • 修改远程仓库地址
    • git remote set-url origin git@github.com:jinyongch/MNIST.git
  • 删除本地仓库
    • rm -fr .git
  • 分支操作
    • 查看分支 git branch -a
    • 创建分支 git branch new_branch_name
    • 切换分支 git checkout other_branch_name
    • 删除分支 git branch -D other_branch_name
    • 合并分支 git merge other_branch_name

Conda 环境

  • 准备工作,安装 Miniconda

    wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh && bash Miniconda3-latest-Linux-x86_64.sh -b -p $HOME/installs/miniconda3 && rm -fr Miniconda3-latest-Linux-x86_64.sh && echo ". $HOME/installs/miniconda3/etc/profile.d/conda.sh" >> $HOME/.bashrc && source $HOME/.bashrc
    
  • 换源

    • conda 换源

      cat <<EOF | tee ~/.condarc
      show_channel_urls: false
      channels:
        - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
        - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
        - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2/
        - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
        - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
        - defaults
      auto_activate_base: false
      EOF
      
    • pip 换源

      conda activate
      pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
      
  • 创建 Conda 环境

    1. 创建环境 conda create -n mnist python=3.8 -y
    2. 激活环境 conda activate mnist
    3. 安装 Pytorch conda install pytorch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 pytorch-cuda=11.7 -c pytorch -c nvidia -y
    4. 安装其他包 pip install packages
  • Conda常用指令

    1. 查看环境 conda env list
    2. 停用环境 conda deactivate
    3. 删除环境 conda remove -n mnist --all

Docker 环境

  • 准备工作,设置环境变量 HOSTNAMEUID

    cat <<'EOF' | tee -a $([[ "$SHELL" == "/bin/bash" ]] && echo ".bashrc" || echo ".zshrc")
    export HOSTNAME=$(hostname)
    export UID=$(id -u)
    EOF
    
  • 创建 Docker 环境。

    1. 下载配置文件 docker.zip,解压上传到项目的根目录下。
    2. 修改 docker/docker-compose.yml 文件中的容器名和 Pytorch 版本。
    3. 执行 cd docker && docker-compose down && docker-compose up -d 启动 docker 容器。
    4. 本地执行 ssh d_beaver 远程登录 docker 容器。
    5. 通过 VSCode 远程登录 docker 容器进行 debug。
  • 与 Conda 环境相比,Docker 环境不需要安装 Pytorch,但只能在一台节点上运行。

Apptainer 环境

  • Docker 容器转 Apptainer 容器,可以实现多节点上运行。

    1. Docker 容器转 Docker 镜像

      docker commit mnist mnist:v1
      
    2. Docker 镜像转 Apptainer 镜像

      apptainer build /scratch/mnist.sif docker-daemon:mnist:v1
      
    3. 启动 Apptainer 容器

      apptainer exec --nv --bind /scratch:/scratch --bind /shares:/shares /scratch/mnist.sif bash -c "cd $HOME/Experiments/MNIST && python main.py"
      
    4. 移动 Apptainer 镜像到 /shares/containers 目录以实现多节点运行

      mv /scratch/mnist.sif /shares/containers
      
  • 移动 Docker 镜像

    • 导出 Docker 镜像为 tar 归档文件

      docker save -o /scratch/mnist_v1.tar mnist:v1
      
    • 导入 tar 归档文件为 Docker 镜像

      docker load -i /scratch/mnist_v1.tar