diff --git a/docs/zh_cn/01-how-to-build/windows.md b/docs/zh_cn/01-how-to-build/windows.md index 2c1c1bf3ad..6cce71998c 100644 --- a/docs/zh_cn/01-how-to-build/windows.md +++ b/docs/zh_cn/01-how-to-build/windows.md @@ -5,14 +5,8 @@ - [安装构建和编译工具链](#安装构建和编译工具链) - [安装依赖包](#安装依赖包) - [安装 MMDeploy Converter 依赖](#安装-mmdeploy-converter-依赖) - - [安装 MMDeploy SDK 依赖](#安装-mmdeploy-sdk-依赖) - - [安装推理引擎](#安装推理引擎) + - [安装推理引擎以及 MMDeploy SDK 依赖](#安装推理引擎以及-mmdeploy-sdk-依赖) - [编译 MMDeploy](#编译-mmdeploy) - - [编译选项说明](#编译选项说明) - - [编译安装 Model Converter](#编译安装-model-converter) - - [编译自定义算子](#编译自定义算子) - - [安装 Model Converter](#安装-model-converter) - - [编译 SDK 和 Demos](#编译-sdk-和-demos) - [注意事项](#注意事项) ______________________________________________________________________ @@ -68,213 +62,43 @@ pip install mmcv-full==1.4.0 -f https://download.openmmlab.com/mmcv/dist/$env:cu -#### 安装 MMDeploy SDK 依赖 +#### 安装推理引擎以及 MMDeploy SDK 依赖 -如果您只对模型转换感兴趣,那么可以跳过本章节。 +可以通过安装脚本简化相关依赖的安装,首先根据需求修改`.\tools\scripts\mmdeploy_init.ps1` 中的`Model converter && SDK config` 部分。下面以编译TensorRT自定义算子以及SDK为例展示脚本的用法,详细配置说明可参考 [cmake 选项说明](./cmake_option.md). - - - - - - - - - - - - - - - - - -
名称 安装方法
OpenCV - 1. 从这里下载 OpenCV 3+。 - 2. 您可以下载并安装 OpenCV 预编译包到指定的目录下。也可以选择源码编译安装的方式 - 3. 在安装目录中,找到 OpenCVConfig.cmake,并把它的路径添加到环境变量 PATH 中。像这样: -
$env:path = "\the\path\where\OpenCVConfig.cmake\locates;" + "$env:path"
-
pplcv pplcv 是 openPPL 开发的高性能图像处理库。 此依赖项为可选项,只有在 cuda 平台下,才需安装。
-

-git clone https://github.com/openppl-public/ppl.cv.git
-cd ppl.cv
-git checkout tags/v0.7.0 -b v0.7.0
-$env:PPLCV_DIR = "$pwd"
-mkdir pplcv-build
-cd pplcv-build
-cmake .. -G "Visual Studio 16 2019" -T v142 -A x64 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=install -DPPLCV_USE_CUDA=ON -DPPLCV_USE_MSVC_STATIC_RUNTIME=OFF
-cmake --build . --config Release -- /m
-cmake --install . --config Release
-cd ../..
-
-
- -#### 安装推理引擎 - -MMDeploy 的 Model Converter 和 SDK 共享推理引擎。您可以参考下文,选择自己感兴趣的推理引擎安装。 - -**目前,在 Windows 平台下,MMDeploy 支持 ONNXRuntime 和 TensorRT 两种推理引擎**。其他推理引擎尚未进行验证,或者验证未通过。后续将陆续予以支持 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
推理引擎 依赖包安装方法
ONNXRuntimeonnxruntime
(>=1.8.1)
- 1. 安装 onnxruntime 的 python 包 -
pip install onnxruntime==1.8.1
- 2. 从这里下载 onnxruntime 的预编译二进制包,解压并配置环境变量 -

-Invoke-WebRequest -Uri https://github.com/microsoft/onnxruntime/releases/download/v1.8.1/onnxruntime-win-x64-1.8.1.zip -OutFile onnxruntime-win-x64-1.8.1.zip
-Expand-Archive onnxruntime-win-x64-1.8.1.zip .
-$env:ONNXRUNTIME_DIR = "$pwd\onnxruntime-win-x64-1.8.1"
-$env:path = "$env:ONNXRUNTIME_DIR\lib;" + $env:path
-
-
TensorRT
TensorRT
- 1. 登录 NVIDIA 官网,从这里选取并下载 TensorRT tar 包。要保证它和您机器的 CPU 架构以及 CUDA 版本是匹配的。您可以参考这份 指南 安装 TensorRT。
- 2. 这里也有一份 TensorRT 8.2 GA Update 2 在 Windows x86_64 和 CUDA 11.x 下的安装示例,供您参考。首先,点击此处下载 CUDA 11.x TensorRT 8.2.3.0。然后,根据如下命令,安装并配置 TensorRT 以及相关依赖。 -

-cd \the\path\of\tensorrt\zip\file
-Expand-Archive TensorRT-8.2.3.0.Windows10.x86_64.cuda-11.4.cudnn8.2.zip .
-pip install $env:TENSORRT_DIR\python\tensorrt-8.2.3.0-cp37-none-win_amd64.whl
-$env:TENSORRT_DIR = "$pwd\TensorRT-8.2.3.0"
-$env:path = "$env:TENSORRT_DIR\lib;" + $env:path
-pip install pycuda
-
-
cudnn - 1. 从 cuDNN Archive 中选择和您环境中 CPU 架构、CUDA 版本以及 TensorRT 版本配套的 cuDNN。以前文 TensorRT 安装说明为例,它需要 cudnn8.2。因此,可以下载 CUDA 11.x cuDNN 8.2
- 2. 解压压缩包,并设置环境变量 -

-cd \the\path\of\cudnn\zip\file
-Expand-Archive cudnn-11.3-windows-x64-v8.2.1.32.zip .
-$env:CUDNN_DIR="$pwd\cuda"
-$env:path = "$env:CUDNN_DIR\bin;" + $env:path
-
-
PPL.NNppl.nn TODO
OpenVINOopenvino TODO
ncnn ncnn TODO
+- a) 修改配置参数如下: + ``` + $CMAKE_BUILD_TYPE = "Release" + $MMDEPLOY_TARGET_BACKENDS = "trt" + $tensorrtVersion = "8.2.3" + $cudnnVersion = "8.2.1" + $cudaVersion = "11.x" + $MMDEPLOY_BUILD_SDK = "ON" + $MMDEPLOY_TARGET_DEVICES = "cpu;cuda" + $opencvVersion = "4.5.5" + $MMDEPLOY_CODEBASES = "all" + ``` +- b) 在 MMDeploy 根目录下执行 + ```powershell + .\mmdeploy_init.ps1 -Action Download + ``` + 该操作会根据配置信息自动下载相关依赖到`3rdparty`文件夹,并将相关变量写入`env.txt`文件。 ### 编译 MMDeploy -```powershell -cd \the\root\path\of\MMDeploy -$env:MMDEPLOY_DIR="$pwd" -``` - -#### 编译 Model Converter +- a) 编译自定义算子以及SDK -如果您选择了 ONNXRuntime,TensorRT 和 ncnn 任一种推理后端,您需要编译对应的自定义算子库。 - -- **ONNXRuntime** 自定义算子 - -```powershell -mkdir build -ErrorAction SilentlyContinue -cd build -cmake .. -G "Visual Studio 16 2019" -A x64 -T v142 -DMMDEPLOY_TARGET_BACKENDS="ort" -DONNXRUNTIME_DIR="$env:ONNXRUNTIME_DIR" -cmake --build . --config Release -- /m -cmake --install . --config Release -``` - -- **TensorRT** 自定义算子 - -```powershell -mkdir build -ErrorAction SilentlyContinue -cd build -cmake .. -G "Visual Studio 16 2019" -A x64 -T v142 -DMMDEPLOY_TARGET_BACKENDS="trt" -DTENSORRT_DIR="$env:TENSORRT_DIR" -DCUDNN_DIR="$env:CUDNN_DIR" -cmake --build . --config Release -- /m -cmake --install . --config Release -``` - -- **ncnn** 自定义算子 - - TODO - -参考 [cmake 选项说明](cmake_option.md) - -#### 安装 Model Converter - -```powershell -cd $env:MMDEPLOY_DIR -pip install -e . -``` - -**注意** - -- 有些依赖项是可选的。运行 `pip install -e .` 将进行最小化依赖安装。 如果需安装其他可选依赖项,请执行`pip install -r requirements/optional.txt`, - 或者 `pip install -e .[optional]`。其中,`[optional]`可以替换为:`all`、`tests`、`build` 或 `optional`。 - -#### 编译 SDK 和 Demos - -下文展示2个构建SDK的样例,分别用 ONNXRuntime 和 TensorRT 作为推理引擎。您可以参考它们,并结合前文 SDK 的编译选项说明,激活其他的推理引擎。 - -- cpu + ONNXRuntime - - ```PowerShell - cd $env:MMDEPLOY_DIR - mkdir build -ErrorAction SilentlyContinue - cd build - cmake .. -G "Visual Studio 16 2019" -A x64 -T v142 ` - -DMMDEPLOY_BUILD_SDK=ON ` - -DMMDEPLOY_BUILD_EXAMPLES=ON ` - -DMMDEPLOY_BUILD_SDK_PYTHON_API=ON ` - -DMMDEPLOY_TARGET_DEVICES="cpu" ` - -DMMDEPLOY_TARGET_BACKENDS="ort" ` - -DONNXRUNTIME_DIR="$env:ONNXRUNTIME_DIR" - - cmake --build . --config Release -- /m - cmake --install . --config Release + ```powershell + .\tools\scripts\mmdeploy_init.ps1 -Action Build ``` -- cuda + TensorRT + 该操作会先试图读取`env.txt`文件中的变量,如果不存在该文件,则会自动执行`Download`操作,请确保按需求修改了`Model converter && SDK config`。 + 之后会编译自定义算子和SDK(如果打开了SDK编译选项)。模型转换需要的自定义算子库会安装在`\mmdeploy\lib`目录下,SDK相关文件会安装在`.\build\install`目录下 - ```PowerShell - cd $env:MMDEPLOY_DIR - mkdir build - cd build - cmake .. -G "Visual Studio 16 2019" -A x64 -T v142 ` - -DMMDEPLOY_BUILD_SDK=ON ` - -DMMDEPLOY_BUILD_EXAMPLES=ON ` - -DMMDEPLOY_BUILD_SDK_PYTHON_API=ON ` - -DMMDEPLOY_TARGET_DEVICES="cuda" ` - -DMMDEPLOY_TARGET_BACKENDS="trt" ` - -Dpplcv_DIR="$env:PPLCV_DIR/pplcv-build/install/lib/cmake/ppl" ` - -DTENSORRT_DIR="$env:TENSORRT_DIR" ` - -DCUDNN_DIR="$env:CUDNN_DIR" +- b) 安装MMDeploy的python转换工具 - cmake --build . --config Release -- /m - cmake --install . --config Release + ``` + pip install -e . ``` ### 注意事项 diff --git a/tools/scripts/mmdeploy_init.ps1 b/tools/scripts/mmdeploy_init.ps1 new file mode 100644 index 0000000000..5d0b52ce4b --- /dev/null +++ b/tools/scripts/mmdeploy_init.ps1 @@ -0,0 +1,641 @@ +<# +.SYNOPSIS +A helper script to help set env and download mmdeploy deps on windows. + + +.Description +This script have four usages: + 1) .\mmdeploy_init.ps1 -Action Download + 2) .\mmdeploy_init.ps1 -Action SetupEnv + 3) .\mmdeploy_init.ps1 -Action Build + 4) .\mmdeploy_init.ps1 -Action Install -Dir path\to\exe + +.EXAMPLE +PS> .\mmdeploy_init.ps1 +#> + +param( + [Parameter(Mandatory = $true)] + [string] $Action, + [string] $Dir = "3rdparty" +) + +# BEGIN Model converter && SDK config + +##### [common] ##### + +# build type +# possible values: +# - "Release": release build +# - "Debug": debug build +$CMAKE_BUILD_TYPE = "Release" + +# target inference engines to support +# possible values: +# - "trt": tensorrt +# - "ort": onnxruntime +# multiple backends should be separated by ; like "trt;ort" +$MMDEPLOY_TARGET_BACKENDS = "trt" + +# onnxruntime config, valid if "ort" in MMDEPLOY_TARGET_BACKENDS +# +# onnxruntime package type +# possible values: +# - "cpu": build with onnxruntime +# - "cuda": build with onnxruntime-gpu +$onnxruntimeDevice = "cpu" +# +# onnxruntime package version +# possible values: +# - ["1.8.1"] +$onnxruntimeVersion = "1.8.1" + +# tensorrt config, valid if "trt" in MMDEPLOY_TARGET_BACKENDS +# +# tensorrt version +# possible values: +# - ["8.2.3", "8.2.4", "8.2.5", "8.4.1", "8.4.2", "8.4.3", "8.5.1", "8.5.2"] +$tensorrtVersion = "8.2.3" +# +# cudnn version +# possible values: +# - ["8.2.1", "8.4.1", "8.6.0"] +$cudnnVersion = "8.2.1" + +# cuda config +# possible values: +# - ["10.2", "11.x"] +$cudaVersion = "11.x" + + +##### [SDK] ##### + +# whether build mmdeploy sdk +# possible values: +# - ["ON", "OFF"] +$MMDEPLOY_BUILD_SDK = "ON" + +# whether build sdk python api +# possible values: +# - ["ON", "OFF"] +$MMDEPLOY_BUILD_SDK_PYTHON_API = "OFF" + +# whether build csharp api +# possible values: +# - ["ON", "OFF"] +$MMDEPLOY_BUILD_SDK_CSHARP_API = "OFF" + +# whether build examples +# possible values: +# - ["ON", "OFF"] +$MMDEPLOY_BUILD_EXAMPLES = "OFF" + +# target devices to support +# possible values: +# - "cpu": cpu +# - "cuda": gpu +# - "acl": atlas +# multiple backends should be separated by ; like "cpu;cuda" +$MMDEPLOY_TARGET_DEVICES = "cpu;cuda" + + +# opencv config, will first consider $opencvCustomFolder, then $opencvVersion +# +# prebuilt opencv version. +# possible values: +# - ["4.5.5"] +$opencvVersion = "4.5.5" +# +# use custom opencv, the folder should contain OpenCVConfig.cmake +$opencvCustomFolder = "" + +# pplcv root dir, if specified, will use user defined pplcv +# thr root dir should contains lib and include folder. +$pplcvCustomFolder = "" + +# codebase config +# possible values: +# - ["mmcls", "mmdet", "mmseg", "mmpose", "mmocr", "mmedit", "mmrotate", "mmaction"] +# multiple backends should be separated by ; like "mmcls;mmdet" +$MMDEPLOY_CODEBASES = "all" + + +# END Model converter && SDK config + +$ErrorActionPreference = 'Stop' + +$EnvVars = @{} +$EnvPath = [System.Collections.ArrayList]@() + + +$envFile = "env.txt" + +Import-Module BitsTransfer + +function Show-HelpMessage { + param () + + Write-Host "usage: .\mmdeploy_init.ps1 command ..." +} + +class DownloadItem { + [string]$Url + [string]$Md5 + DownloadItem([string]$Url, [string]$Md5) { + $this.Url = $Url + $this.Md5 = $Md5 + } +} + +function Start-DownloadWithRetry { + param ( + [string] $Url, + [string] $Name, + [string] $Md5 = "", + [string] $SaveDir = "${env:Temp}", + [int] $Retries = 3 + ) + + Write-Host "Download $Name ..." + $fileName = [IO.Path]::GetFileName($Url) + $filePath = Join-Path -Path $SaveDir -ChildPath $fileName + $downloadStartTime = Get-Date + + while ($Retries -gt 0) { + if (Test-Path $filePath) { + $md5_val = (Get-FileHash $filePath -Algorithm MD5).Hash + if ($md5_val -eq $Md5) { + Write-Host "Using cached $filePath" + break + } + } + try { + $downloadAttemptStartTime = Get-Date + Write-Host "Downloading package from: $Url to path $filePath ." + Start-BitsTransfer $Url $filePath + # (New-Object System.Net.WebClient).DownloadFile($Url, $filePath) + # Invoke-WebRequest $Url -OutFile $filePath + $downloadCompleteTime = [math]::Round(($(Get-Date) - $downloadStartTime).TotalSeconds, 2) + Write-Host "Package $Name downloaded successfully in $downloadCompleteTime seconds" + break + } + catch { + $attemptTime = [math]::Round(($(Get-Date) - $downloadAttemptStartTime).TotalSeconds, 2) + Write-Warning "There is an error encountered after $attemptTime seconds during package downloading:`n $_" + $Retries-- + + if ($Retries -eq 0) { + Write-Error "File can't be downloaded. Please try later or check that file exists by url: $Url" + exit 1 + } + + Write-Warning "Waiting 5 seconds before retrying. Retries left: $Retries" + Start-Sleep -Seconds 5 + } + } + return $filePath +} + +function Get-Opencv { + param ( + $Version, + $CustomFolder = "" + ) + + if ($CustomFolder -ne "") { + Write-Host "Using custom opencv $CustomFolder" + $opencvBase = [IO.Path]::Combine($CustomFolder, "x64") + if ((Get-ChildItem $opencvBase -Filter "vc*").Length -gt 1) { + $vcx = (Get-ChildItem $opencvBase -Filter "vc*").Name[-1] + } + else { + $vcx = (Get-ChildItem $opencvBase -Filter "vc*").Name + } + # set variable + $EnvVars["OpenCV_DIR"] = [IO.Path]::Combine($opencvBase, $vcx, "lib") + [void]$EnvPath.Add([IO.Path]::Combine($opencvBase, $vcx, "bin")) + return; + } + + $opencvUrls = @{ + "4.5.5" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/opencv/opencv-win-amd64-4.5.5-vc16.zip", + "e1a744108105fb20e3c8371d58b38c1a" + ); + } + $item = $opencvUrls.$Version + $fileSrc = Start-DownloadWithRetry -Url $item.Url -Name "opencv" -Md5 $item.Md5 + $fileDst = [IO.Path]::Combine($pwd, $Dir, 'opencv') + # extract + Write-Host "Extract file $fileSrc to $fileDst" + Expand-Archive -Path $fileSrc -DestinationPath $fileDst -Force + Write-Host "Extract file $fileSrc done" + # set variable + $subFolder = (Get-ChildItem $fileDst).Name + $opencvBase = [IO.Path]::Combine($fileDst, $subFolder, "x64") + if ((Get-ChildItem $opencvBase -Filter "vc*").Length -gt 1) { + $vcx = (Get-ChildItem $opencvBase -Filter "vc*").Name[-1] + } + else { + $vcx = (Get-ChildItem $opencvBase -Filter "vc*").Name + } + $EnvVars["OpenCV_DIR"] = [IO.Path]::Combine($opencvBase, $vcx, "lib") + [void]$EnvPath.Add([IO.Path]::Combine($opencvBase, $vcx, "bin")) +} + +function Get-Onnxruntime { + param ( + [string]$Device, + [string]$Version + ) + + $sys_onnxruntime = @("C:\Windows\System32\onnxruntime.dll", "C:\Windows\SysWOW64\onnxruntime.dll") + foreach ($path in $sys_onnxruntime) { + if (Test-Path $path) { + Write-Warning "`nFound onnxruntime.dll in $path, it will conflict with downloaded version of onnxruntime, please rename it" + } + } + + $ortCpuUrls = @{ + "1.8.1" = [DownloadItem]::new( + "https://github.com/microsoft/onnxruntime/releases/download/v1.8.1/onnxruntime-win-x64-1.8.1.zip", + "dabe12f86a4a37caa4feec16ef16ade6" + ); + } + $ortCudaUrls = @{ + "1.8.1" = [DownloadItem]::new( + "https://github.com/microsoft/onnxruntime/releases/download/v1.8.1/onnxruntime-win-gpu-x64-1.8.1.zip", + "495442f6f124597635af3cfa96213122" + ) + } + $ortUrls = @{ + "cpu" = $ortCpuUrls; + "cuda" = $ortCudaUrls + } + $item = $ortUrls.$Device.$Version + $fileSrc = Start-DownloadWithRetry -Url $item.Url -Name "ONNX Runtime" -Md5 $item.Md5 + $fileDst = [IO.Path]::Combine($pwd, $Dir, 'onnxruntime') + # extract + Write-Host "Extract file $fileSrc to $fileDst" + Expand-Archive -Path $fileSrc -DestinationPath $fileDst -Force + Write-Host "Extract file $fileSrc done" + # set variable + $subFolder = (Get-ChildItem $fileDst).Name + $onnxruntimeDir = [IO.Path]::Combine($fileDst, $subFolder) + $EnvVars["ONNXRUNTIME_DIR"] = $onnxruntimeDir + [void]$EnvPath.Add([IO.Path]::Combine($onnxruntimeDir, "lib")) +} + + + +function Get-Tensorrt { + param( + [ValidateSet("10.2", "11.x")] + [string] $CudaVerion, + [ValidateSet("8.2.3", "8.2.4", "8.2.5", "8.4.1", "8.4.2", "8.4.3", "8.5.1", "8.5.2")] + [string] $Version + ) + # download + $tensorrtCu102Urls = @{ + "8.2.3" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/tensorrt/TensorRT-8.2.3.0.Windows10.x86_64.cuda-10.2.cudnn8.2.zip", + "e6b5a412101f8dc823eaf11bfb968376"); + "8.2.4" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/tensorrt/TensorRT-8.2.4.2.Windows10.x86_64.cuda-10.2.cudnn8.2.zip", + "05d9d817ae77da394fd80ad2df729329"); + "8.2.5" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/tensorrt/TensorRT-8.2.5.1.Windows10.x86_64.cuda-10.2.cudnn8.2.zip", + "7274036d6a2320e0c1e39d5ea0886611"); + "8.4.1" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/tensorrt/TensorRT-8.4.1.5.Windows10.x86_64.cuda-10.2.cudnn8.4.zip", + "7059ce9ebcf17ee8d0fff29d3f2d9c00"); + "8.4.2" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/tensorrt/TensorRT-8.4.2.4.Windows10.x86_64.cuda-10.2.cudnn8.4.zip", + "8ade017b1075fcdb1a395622f1d3ce7d"); + "8.4.3" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/tensorrt/TensorRT-8.4.3.1.Windows10.x86_64.cuda-10.2.cudnn8.4.zip", + "55044d4f927f28bfd9b45073f7d15a84"); + "8.5.1" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/tensorrt/TensorRT-8.5.1.7.Windows10.x86_64.cuda-10.2.cudnn8.6.zip", + "e00d666292625781e8e5715c9687264c"); + "8.5.2" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/tensorrt/TensorRT-8.5.2.2.Windows10.x86_64.cuda-10.2.cudnn8.6.zip", + "d52692c8f615ec2142c968214185451f"); + } + $tensorrtCu11xUrls = @{ + "8.2.3" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/tensorrt/TensorRT-8.2.3.0.Windows10.x86_64.cuda-11.4.cudnn8.2.zip", + "3364ad175781746a0e7359d2ed4b2287"); + "8.2.4" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/tensorrt/TensorRT-8.2.4.2.Windows10.x86_64.cuda-11.4.cudnn8.2.zip", + "ac7252b92cb76084f875554a5f8f759a"); + "8.2.5" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/tensorrt/TensorRT-8.2.5.1.Windows10.x86_64.cuda-11.4.cudnn8.2.zip", + "505e35eed6dc9ca4784199d0dc3b00e6"); + "8.4.1" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/tensorrt/TensorRT-8.4.1.5.Windows10.x86_64.cuda-11.6.cudnn8.4.zip", + "8b5afa6d9abea36d4cfe3df831faad14"); + "8.4.2" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/tensorrt/TensorRT-8.4.2.4.Windows10.x86_64.cuda-11.6.cudnn8.4.zip", + "0b2691dfdc5e40d97f3ca9249fc8f9b1"); + "8.4.3" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/tensorrt/TensorRT-8.4.3.1.Windows10.x86_64.cuda-11.6.cudnn8.4.zip", + "125b987f6571cf8b43528f0a26f46593"); + "8.5.1" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/tensorrt/TensorRT-8.5.1.7.Windows10.x86_64.cuda-11.8.cudnn8.6.zip", + "775667c49cd02f8fd14c305590978a97"); + "8.5.2" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/tensorrt/TensorRT-8.5.2.2.Windows10.x86_64.cuda-11.8.cudnn8.6.zip", + "b67ba529ad54338fee6e9d282c5f272c"); + } + $tensorrtUrls = @{ + "10.2" = $tensorrtCu102Urls; + "11.x" = $tensorrtCu11xUrls + } + $item = $tensorrtUrls.$CudaVerion.$Version + $fileSrc = Start-DownloadWithRetry -Url $item.Url -Name "TensorRT" -Md5 $item.Md5 + $fileDst = [IO.Path]::Combine($pwd, $Dir, 'tensorrt') + # extract + Write-Host "Extract file $fileSrc to $fileDst" + Expand-Archive -Path $fileSrc -DestinationPath $fileDst -Force + Write-Host "Extract file $fileSrc done" + # set variable + $subFolder = (Get-ChildItem $fileDst).Name + $tensorrtDir = [IO.Path]::Combine($fileDst, $subFolder) + $EnvVars["TENSORRT_DIR"] = $tensorrtDir + [void]$EnvPath.Add([IO.Path]::Combine($tensorrtDir, 'lib')) +} + +function Get-Cudnn { + param ( + [ValidateSet("10.2", "11.x")] + [string] $CudaVerion, + [ValidateSet("8.2.1", "8.4.1", "8.6.0")] + [string] $Version + ) + # download + $cudnnCu102Urls = @{ + "8.2.1" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/cudnn/cudnn-10.2-windows10-x64-v8.2.1.32.zip", + "14ce30ae33b5e2a7d4f1e124a251219a"); + "8.4.1" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/cudnn/cudnn-windows-x86_64-8.4.1.50_cuda10.2-archive.zip", + "5cccd75f8300b70e83800a4368ebce1f"); + "8.6.0" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/cudnn/cudnn-windows-x86_64-8.6.0.163_cuda10-archive.zip", + "fb84f9f990373e239ddb9515c30c754e") + } + $cudnnCu11xUrls = @{ + "8.2.1" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/cudnn/cudnn-11.3-windows-x64-v8.2.1.32.zip", + "1f6616595f397fe02a64f4d828e6eeb6"); + "8.4.1" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/cudnn/cudnn-windows-x86_64-8.4.1.50_cuda11.6-archive.zip", + "d770cf6922dc76d82bdc08af3cd19603"); + "8.6.0" = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/cudnn/cudnn-windows-x86_64-8.6.0.163_cuda11-archive.zip", + "55f0fc87255861ab10b0b796d271256c") + } + $cudnnUrls = @{ + "10.2" = $cudnnCu102Urls; + "11.x" = $cudnnCu11xUrls + } + $item = $cudnnUrls.$CudaVerion.$Version + $fileSrc = Start-DownloadWithRetry -Url $item.Url -Name "cuDNN" -Md5 $item.Md5 + $fileDst = [IO.Path]::Combine($pwd, $Dir, "cudnn") + # extract + Write-Host "Extract file $fileSrc to $fileDst" + Expand-Archive -Path $fileSrc -DestinationPath $fileDst -Force + Write-Host "Extract file $fileSrc done" + # set variable + $EnvVars["CUDNN_DIR"] = $fileDst + [void]$EnvPath.Add([IO.Path]::Combine($fileDst, "cuda", 'bin')) +} + +function Get-Pplcv { + param ( + [ValidateSet("10.2", "11.x")] + [string] $CudaVerion, + [string] $CustomFolder = "" + ) + if ($CustomFolder -ne "") { + Write-Host "Using custom pplcv $CustomFolder" + # set variable + $pplcvDir = [IO.Path]::Combine($CustomFolder, "lib", "cmake", "ppl") + $EnvVars["pplcv_DIR"] = $pplcvDir + return; + } + # download + $pplcvCu102url = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/pplcv/pplcv-0.6.2.windows10.x86_64.cuda-10.2.zip", + "800386f8ffbfeb5d0bf6e056bdd2a98a"); + $pplcvCu11xurl = [DownloadItem]::new( + "https://github.com/irexyc/mmdeploy-ci-resource/releases/download/pplcv/pplcv-0.6.2.windows10.x86_64.cuda-11.x.zip", + "26d028c4f19a86d6feb28614caa461ef"); + $pplcvUrls = @{ + "10.2" = $pplcvCu102url; + "11.x" = $pplcvCu11xurl + } + $item = [DownloadItem]($pplcvUrls.$CudaVerion) + $fileSrc = Start-DownloadWithRetry -Url $item.Url -Name "ppl.cv" -Md5 $item.Md5 + $fileDst = [IO.Path]::Combine($pwd, $Dir, 'pplcv') + # extract + Write-Host "Extract file $fileSrc to $fileDst" + Expand-Archive -Path $fileSrc -DestinationPath $fileDst -Force + Write-Host "Extract file $fileSrc done" + # set varialbe + $pplcvDir = [IO.Path]::Combine($fileDst, "lib", "cmake", "ppl") + $EnvVars["pplcv_DIR"] = $pplcvDir +} + +function Write-EnvFile { + param () + + "[environment variables]" | Out-File -FilePath $envFile -Append + foreach ($key in $EnvVars.Keys) { + "$key $($EnvVars.$key)" | Out-File -FilePath $envFile -Append + } + + "[environment path]" | Out-File -FilePath $envFile -Append + foreach ($val in $EnvPath) { + "$val" | Out-File -FilePath $envFile -Append + } +} + +function Get-Dependences { + param () + + if (Test-Path $envFile) { + remove-item $envFile + } + + # backend + if ($MMDEPLOY_TARGET_BACKENDS.Contains("trt")) { + Write-Host "Using tensorrt and cudnn for cuda $cudaVersion" + + Get-Tensorrt -CudaVerion $cudaVersion -Version $tensorrtVersion + Get-Cudnn -CudaVerion $cudaVersion -Version $cudnnVersion + } + if ($MMDEPLOY_TARGET_BACKENDS.Contains("ort")) { + Get-Onnxruntime -Device $onnxruntimeDevice -Version $onnxruntimeVersion + } + + # sdk + if ($MMDEPLOY_BUILD_SDK -ne "ON") { + Write-Host "Skip build mmdeploy sdk" + return + } + + # pplcv if use cuda device + if ($MMDEPLOY_TARGET_DEVICES.Contains("cuda")) { + Get-Pplcv -CudaVerion $cudaVersion -CustomFolder $pplcvCustomFolder + } + + # opencv + Get-Opencv -Version $opencvVersion -CustomFolder $opencvCustomFolder +} + +function Set-Environment { + # [environment variables] + # [environment path] + param () + if (-Not (Test-Path $envFile)) { + Write-Error "Can't find env.txt in $pwd, Please first download dependencies" + } + $env_content = Get-Content $envFile + + Write-Host "Setup up environment variables ..." + $i = 1 + for (; $i -lt $env_content.Length; $i++) { + if ($env_content[$i][0] -eq '[') { + $i++; + break; + } + $kv = $env_content[$i].Split(' ') + [System.Environment]::SetEnvironmentVariable($kv[0], $kv[1]); + $EnvVars[$kv[0]] = $kv[1] + } + Get-ChildItem Env: + + # print environment variable + Write-Host "`nSetup up environment path ..." + $path_val = [Environment]::GetEnvironmentVariable("PATH") + $path_list = $path_val.Split( [IO.Path]::PathSeparator) + for (; $i -lt $env_content.Length; $i++) { + if ($env_content[$i][0] -eq '[') { + $i++; + break; + } + $p = $env_content[$i] + $found = 0 + foreach ($v in $path_list) { + if ($v -eq $p) { + $found = 1 + break + } + } + if ($found -eq 0) { + $path_val = $p + [IO.Path]::PathSeparator + $path_val + } + } + [System.Environment]::SetEnvironmentVariable("PATH", $path_val); + # print path environment + Write-Host $env:PATH +} + +function Build-MMDeploy { + if (-Not (Test-Path $envFile)) { + Write-Warning "Can't find env.txt in $pwd, try to download dependencies" + Write-Warning "please make sure you have edited the configure setting." + Get-Dependences + Write-EnvFile + } + + Set-Environment + + $configureCommand = "cmake .. -A x64 -T v142" + if ($MMDEPLOY_TARGET_BACKENDS.Contains("trt") -or $MMDEPLOY_TARGET_DEVICES.Contains("cuda")) { + $configureCommand += ',cuda="' + $env:CUDA_PATH + '"' + } + $configureCommand += " -DMMDEPLOY_BUILD_SDK_CSHARP_API=${MMDEPLOY_BUILD_SDK_CSHARP_API}" + $configureCommand += " -DMMDEPLOY_BUILD_SDK_PYTHON_API=${MMDEPLOY_BUILD_SDK_PYTHON_API}" + $configureCommand += " -DMMDEPLOY_BUILD_SDK=${MMDEPLOY_BUILD_SDK}" + $configureCommand += " -DMMDEPLOY_TARGET_DEVICES='${MMDEPLOY_TARGET_DEVICES}'" + $configureCommand += " -DMMDEPLOY_TARGET_BACKENDS='${MMDEPLOY_TARGET_BACKENDS}'" + $configureCommand += " -DMMDEPLOY_CODEBASES='${MMDEPLOY_CODEBASES}'" + $configureCommand += " -DMMDEPLOY_BUILD_EXAMPLES=${MMDEPLOY_BUILD_EXAMPLES}" + foreach ($key in $EnvVars.Keys) { + $configureCommand += " -D${key}=$($EnvVars.$key)" + } + + $buildDir = [IO.Path]::Combine($pwd, "build") + if (Test-Path $buildDir -PathType Container) { + Remove-Item $buildDir -Recurse + } + + New-Item -ItemType "directory" -Path $buildDir + $oldWorkingDir = $pwd + Set-Location -Path $buildDir + + Write-Host $configureCommand + Invoke-Expression $configureCommand + + Invoke-Expression "cmake --build . --config $CMAKE_BUILD_TYPE -j" + Invoke-Expression "cmake --install . --config $CMAKE_BUILD_TYPE" + Set-Location -Path $oldWorkingDir +} + +function Install-Dependences { + # [environment path] + # [other path] + param () + + if (-Not (Test-Path $Dir -PathType Container)) { + Write-Error "Folder $Dir doesn't exits" + } + + if (-Not (Test-Path $envFile)) { + Write-Error "Can't find env.txt in $pwd, Please first download dependencies" + } + $env_content = Get-Content $envFile + Write-Host "Install Dependences to executable folder" + + $i = 1 + for (; $i -lt $env_content.Length; $i++) { + if ($env_content[$i].StartsWith("[")) { + $i++ + break + } + } + # environment path + for (; $i -lt $env_content.Length; $i++) { + if ($env_content[$i].StartsWith("[")) { + $i++; + break; + } + $visit = $env_content[$i] + Write-Host "checking folder: $visit" + $dlls = (Get-ChildItem $env_content[$i] -Filter "*.dll").FullName + foreach ($dll in $dlls) { + Write-Host "Copy $dll to $Dir" + Copy-Item -Path $dll -Destination $Dir + } + } +} + +if ($Action -eq "Build") { + Build-MMDeploy +} +elseif ($Action -eq "Download") { + Get-Dependences + Write-EnvFile +} +elseif ($Action -eq "Install") { + Install-Dependences +} +elseif ($Action -eq "SetupEnv") { + Set-Environment +} +else { + Show-HelpMessage +}