You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Copy-NativeTensorFlowLibs.ps1 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <#
  2. .SYNOPSIS
  3. Copy the native TensorFlow library to enable the packing a nuget to make
  4. them available to TensorFlow.NET
  5. .DESCRIPTION
  6. The TensorFlow libraries are copied for Windows and Linux and it becomes
  7. possible to bundle a meta-package containing them.
  8. .PARAMETER CpuLibraries
  9. Switch indicating if the script should download the CPU or GPU version of the
  10. TensorFlow libraries.
  11. By default the GPU version of the libraries is downloaded.
  12. #>
  13. param(
  14. [switch] $CpuLibraries = $false
  15. )
  16. function Expand-TarGzFiles {
  17. <#
  18. .SYNOPSIS
  19. Expands the given list of files from the given archive into the given
  20. target directory.
  21. .PARAMETER Archive
  22. Path to the archive that should be considered.
  23. .PARAMETER Files
  24. Files that should be extracted from the archive.
  25. .PARAMETER TargetDirectory
  26. Directory into which the files should be expanded.
  27. #>
  28. param
  29. (
  30. [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string] $Archive,
  31. [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string []] $Files,
  32. [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string] $TargetDirectory
  33. )
  34. & 7z e $Archive -o"$TargetDirectory"
  35. $TarArchive = Join-Path $TargetDirectory "libtensorflow.tar"
  36. & 7z e $TarArchive $Files -o"$TargetDirectory"
  37. Remove-Item $TarArchive
  38. }
  39. function Expand-ZipFiles {
  40. <#
  41. .SYNOPSIS
  42. Expands the given list of files from the given archive into the given target directory.
  43. .PARAMETER Archive
  44. Path to the archive that should be considered.
  45. .PARAMETER Files
  46. Files that should be extracted from the archive.
  47. .PARAMETER TargetDirectory
  48. Directory into which the files should be expanded.
  49. #>
  50. param(
  51. [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string] $Archive,
  52. [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string []] $Files,
  53. [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string] $TargetDirectory
  54. )
  55. & 7z e $Archive $Files -o"$TargetDirectory"
  56. }
  57. function Split-ArchiveFromUrl {
  58. <#
  59. .SYNOPSIS
  60. Extracts the archive name out of the given Url.
  61. .PARAMETER ArchiveUrl
  62. Url of the archive that will be downloaded.
  63. #>
  64. param(
  65. [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string] $ArchiveUrl
  66. )
  67. $uriParts = $ArchiveUrl.split("/")
  68. $ArchivePath = $uriParts[$uriParts.Count - 1]
  69. return $ArchivePath
  70. }
  71. function Copy-Archive {
  72. <#
  73. .SYNOPSIS
  74. This function copies the given binary file to the given target location.
  75. .PARAMETER ArchiveUrl
  76. Url where the archive should be downloaded from.
  77. .PARAMETER TargetDirectory
  78. Target directory where the archive should be downloaded.
  79. #>
  80. param (
  81. [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  82. [string] $ArchiveUrl,
  83. [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  84. [string] $TargetDirectory
  85. )
  86. $ArchiveName = Split-ArchiveFromUrl $ArchiveUrl
  87. $TargetPath = [IO.Path]::Combine($PSScriptRoot, "..", "packages", $ArchiveName)
  88. if (Test-Path $TargetPath -PathType Leaf) {
  89. Write-Error "$TargetPath already exists, please remove to download againg."
  90. return $TargetPath
  91. }
  92. if (-not (Test-Path $TargetDirectory -PathType Container)) {
  93. Write-Host "Creating missing $TargetDirectory"
  94. New-Item -Path $TargetDirectory -ItemType Directory
  95. }
  96. Write-Host "Downloading $ArchiveUrl, this might take a while..."
  97. $wc = New-Object System.Net.WebClient
  98. $wc.DownloadFile($ArchiveUrl, $TargetPath)
  99. return $TargetPath
  100. }
  101. $LinuxGpuArchive = "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-linux-x86_64-1.14.0.tar.gz"
  102. $LinuxFiles = @(".\libtensorflow.tar", ".\lib\libtensorflow.so", ".\lib\libtensorflow.so.1", ".\lib\libtensorflow.so.1.14.0", `
  103. ".\lib\libtensorflow_framework.so", ".\lib\libtensorflow_framework.so.1", ".\lib\libtensorflow_framework.so.1.14.0")
  104. $WindowsGpuArchive = "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-windows-x86_64-1.14.0.zip"
  105. $WindowsFiles = @("lib\tensorflow.dll")
  106. $PackagesDirectory = [IO.Path]::Combine($PSScriptRoot, "..", "packages")
  107. if (-not $CpuLibraries) {
  108. $WindowsArchive = $WindowsGpuArchive
  109. $LinuxArchive = $LinuxGpuArchive
  110. }
  111. $Archive = Copy-Archive -ArchiveUrl $WindowsArchive -TargetDirectory $PackagesDirectory
  112. $TargetDirectory = [IO.Path]::Combine($PSScriptRoot, "..", "src", "runtime.win-x64.SciSharp.TensorFlow-Gpu.Redist")
  113. Expand-ZipFiles $Archive $WindowsFiles $TargetDirectory
  114. $Archive = Copy-Archive -ArchiveUrl $LinuxArchive -TargetDirectory $PackagesDirectory
  115. $TargetDirectory = [IO.Path]::Combine($PSScriptRoot, "..", "src", "runtime.linux-x64.SciSharp.Tensorflow-Gpu.Redist")
  116. Expand-TarGzFiles $Archive $LinuxFiles $TargetDirectory