File size: 3,918 Bytes
17b0632
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
param(
  [string]$AdapterRepo = "DevGwardo/minicpm5-1b-toolcall",
  [string]$OutDir = "gguf",
  [string]$MergedDir = "merged",
  [switch]$SkipMerge
)

$ErrorActionPreference = 'Stop'

# Project root = parent of this script's directory (pipeline\).
$ProjectRoot = if ($PSScriptRoot) { Split-Path $PSScriptRoot -Parent } else { (Get-Location).Path }

function Resolve-FromRoot([string]$p) {
  if ([System.IO.Path]::IsPathRooted($p)) { return $p }
  return (Join-Path $ProjectRoot $p)
}

$Python       = Resolve-FromRoot '.venv\Scripts\python.exe'
$MergeScript  = Resolve-FromRoot 'pipeline\merge_local.py'
$ConvertScript = Resolve-FromRoot 'llama.cpp-src\convert_hf_to_gguf.py'
$Quantize     = Resolve-FromRoot 'llama-bin\llama-quantize.exe'
$MergedFull   = Resolve-FromRoot $MergedDir
$OutFull      = Resolve-FromRoot $OutDir

# NOTE: $AdapterRepo is informational. pipeline/merge_local.py currently
# hardcodes BASE="openbmb/MiniCPM5-1B" and ADAPTER="DevGwardo/minicpm5-1b-toolcall",
# so a non-default -AdapterRepo value is accepted but NOT forwarded to the merge.
if ($AdapterRepo -ne 'DevGwardo/minicpm5-1b-toolcall') {
  Write-Host ("WARNING: -AdapterRepo '{0}' is ignored by pipeline/merge_local.py (hardcoded adapter)." -f $AdapterRepo) -ForegroundColor Yellow
}
if ($MergedDir -ne 'merged') {
  Write-Host 'WARNING: pipeline/merge_local.py always writes to .\merged; a custom -MergedDir only affects convert/quantize inputs.' -ForegroundColor Yellow
}

# 1) Sanity-check paths; create $OutDir.
$required = @($Python, $MergeScript, $ConvertScript, $Quantize)
foreach ($p in $required) {
  if (-not (Test-Path $p)) { throw "Missing required file: $p" }
}
New-Item -ItemType Directory -Force -Path $OutFull | Out-Null
Write-Host "Project root : $ProjectRoot"
Write-Host "Merged dir   : $MergedFull"
Write-Host "Output dir   : $OutFull"

Push-Location $ProjectRoot
try {
  # 2) Merge (idempotent: skip when merged\config.json already exists).
  $mergedConfig = Join-Path $MergedFull 'config.json'
  if (Test-Path $mergedConfig) {
    Write-Host 'Merged model found, skipping merge.' -ForegroundColor Green
  }
  elseif ($SkipMerge) {
    Write-Host 'ERROR: merged model not found.' -ForegroundColor Red
    Write-Host ("  Expected: {0}" -f $mergedConfig)
    Write-Host '  Run the merge first (one of):'
    Write-Host '    powershell -NoProfile -File pipeline\make_gguf.ps1'
    Write-Host '    .venv\Scripts\python.exe pipeline\merge_local.py'
    exit 1
  }
  else {
    Write-Host 'Merging LoRA into base (pipeline\merge_local.py) ...'
    & $Python $MergeScript
    if ($LASTEXITCODE -ne 0) { throw "merge_local.py failed with exit code $LASTEXITCODE." }
    if (-not (Test-Path $mergedConfig)) { throw "Merge finished but config.json still missing: $mergedConfig" }
  }

  # 3) Convert merged HF dir -> f16 GGUF.
  $f16 = Join-Path $OutFull 'minicpm5-1b-toolcall-f16.gguf'
  $q8  = Join-Path $OutFull 'minicpm5-1b-toolcall-Q8_0.gguf'
  $q4  = Join-Path $OutFull 'minicpm5-1b-toolcall-Q4_K_M.gguf'

  Write-Host 'Converting to f16 GGUF ...'
  & $Python $ConvertScript $MergedFull --outfile $f16 --outtype f16
  if ($LASTEXITCODE -ne 0) { throw "convert_hf_to_gguf.py failed with exit code $LASTEXITCODE." }

  # 4) Quantize.
  Write-Host 'Quantizing Q8_0 ...'
  & $Quantize $f16 $q8 Q8_0
  if ($LASTEXITCODE -ne 0) { throw "llama-quantize Q8_0 failed with exit code $LASTEXITCODE." }

  Write-Host 'Quantizing Q4_K_M ...'
  & $Quantize $f16 $q4 Q4_K_M
  if ($LASTEXITCODE -ne 0) { throw "llama-quantize Q4_K_M failed with exit code $LASTEXITCODE." }

  # 5) Report outputs + sizes.
  Write-Host ''
  Write-Host 'GGUF outputs:'
  Get-ChildItem $OutFull -Filter '*.gguf' |
    Select-Object Name,
      @{ Name = 'SizeMB'; Expression = { '{0:N1}' -f ($_.Length / 1MB) } },
      @{ Name = 'Bytes';  Expression = { $_.Length } } |
    Format-Table -AutoSize | Out-String | Write-Host
}
finally {
  Pop-Location
}