|
| 1 | +$stopwatch = [System.Diagnostics.Stopwatch]::startNew() |
| 2 | + |
| 3 | +$llamaCppDirectory = "D:\Privat\GitHub\windows_llama.cpp\vendor\llama.cpp" |
| 4 | +$sourceDirectory = "R:\AI\LLM\source" |
| 5 | +$targetDirectory = "R:\AI\LLM\gguf" |
| 6 | +$cacheDirectory = "E:\cache" |
| 7 | + |
| 8 | +$exclude = @() |
| 9 | + |
| 10 | +$types = @( |
| 11 | + # "q2_K" |
| 12 | + # "q3_K" |
| 13 | + # "q3_K_L" |
| 14 | + # "q3_K_M" |
| 15 | + # "q3_K_S" |
| 16 | + # "q4_0" |
| 17 | + # "q4_1" |
| 18 | + # "q4_K" |
| 19 | + "q4_K_M" |
| 20 | + # "q4_K_S" |
| 21 | + # "q5_0" |
| 22 | + # "q5_1" |
| 23 | + # "q5_K" |
| 24 | + # "q5_K_M" |
| 25 | + # "q5_K_S" |
| 26 | + # "q6_K" |
| 27 | + # "q8_0" |
| 28 | +) |
| 29 | + |
| 30 | +$naturalSort = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) } |
| 31 | + |
| 32 | +$repositoryDirectories = Get-ChildItem -Directory $sourceDirectory -Exclude $exclude -Name | Sort-Object $naturalSort |
| 33 | + |
| 34 | +Write-Host "Quantizing $($repositoryDirectories.Length) large language models." -ForegroundColor "Yellow" |
| 35 | + |
| 36 | +conda activate llama.cpp |
| 37 | + |
| 38 | +ForEach ($repositoryName in $repositoryDirectories) { |
| 39 | + |
| 40 | + $sourceDirectoryPath = Join-Path -Path $sourceDirectory -ChildPath $repositoryName |
| 41 | + $targetDirectoryPath = Join-Path -Path $targetDirectory -ChildPath $repositoryName |
| 42 | + |
| 43 | + if (!(Test-Path -Path $targetDirectoryPath)) { |
| 44 | + New-Item -Path $targetDirectory -Name $repositoryName -ItemType "directory" |
| 45 | + } |
| 46 | + |
| 47 | + Write-Host "Working on ${repositoryName}..." -ForegroundColor "DarkYellow" |
| 48 | + |
| 49 | + # We are creating the intermediate unquantized model in a dedicated cache directory |
| 50 | + # so that it can be locatend on another drive to improve the quantization speed. |
| 51 | + $unquantizedModelPath = Join-Path -Path $cacheDirectory -ChildPath "${repositoryName}.model-unquantized.gguf" |
| 52 | + |
| 53 | + ForEach ($type in $types) { |
| 54 | + |
| 55 | + $quantizedModelPath = Join-Path -Path $targetDirectoryPath -ChildPath "model-quantized-${type}.gguf" |
| 56 | + |
| 57 | + if (!(Test-Path -Path $quantizedModelPath) -and !(Test-Path -Path $unquantizedModelPath)) { |
| 58 | + |
| 59 | + Write-Host "Converting ${sourceDirectoryPath} to ${unquantizedModelPath}..." -ForegroundColor "DarkYellow" |
| 60 | + |
| 61 | + $convertCommand = "${llamaCppDirectory}\convert.py --outfile $unquantizedModelPath $sourceDirectoryPath" |
| 62 | + |
| 63 | + Invoke-Expression "python $convertCommand" |
| 64 | + } |
| 65 | + |
| 66 | + if (!(Test-Path -Path $quantizedModelPath)) { |
| 67 | + |
| 68 | + $quantizeCommand = "${llamaCppDirectory}\build\bin\Release\quantize.exe" |
| 69 | + |
| 70 | + Write-Host "Quantizing ${unquantizedModelPath} to ${quantizedModelPath}..." -ForegroundColor "DarkYellow" |
| 71 | + |
| 72 | + Invoke-Expression "$quantizeCommand $unquantizedModelPath $quantizedModelPath $type" |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if ((Test-Path -Path $unquantizedModelPath)) { |
| 77 | + |
| 78 | + Write-Host "Removing intermediate unquantized model ${unquantizedModelPath}..." -ForegroundColor "DarkYellow" |
| 79 | + Remove-Item "${unquantizedModelPath}" -Recurse -Force |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +$stopwatch.Stop() |
| 84 | +$durationInSeconds = [Math]::Floor([Decimal]($stopwatch.Elapsed.TotalSeconds)) |
| 85 | + |
| 86 | +Write-Host "Successfully finished the quantization in ${durationInSeconds} seconds." -ForegroundColor "Yellow" |
0 commit comments