File size: 4,629 Bytes
9019b27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
98
99
100
101
102
103
104
105
106
107
108
109
110
# Setup lightweight BitNet for Kinetic (local repo: F:\Kinetic\BitNet)
# Prefer smallest instruct model: Falcon-E-1B-Instruct (1B, 1.58-bit)
#
# Usage (from repo root, VS2022 Developer PowerShell recommended on Windows):
#   powershell -ExecutionPolicy Bypass -File scripts\setup_bitnet_light.ps1
#   powershell -File scripts\setup_bitnet_light.ps1 -Model Falcon1B
#   powershell -File scripts\setup_bitnet_light.ps1 -Model BitNet2B -SkipBuild

param(
    # BitNetLarge (~0.7B) is lightest with pretuned x86 kernels in-repo
    [ValidateSet("BitNetLarge", "Falcon1B", "BitNet2B")]
    [string]$Model = "BitNetLarge",
    [switch]$SkipBuild,
    [switch]$SkipDownload,
    [string]$BitNetRoot = "F:\Kinetic\BitNet"
)

$ErrorActionPreference = "Stop"

if (-not (Test-Path $BitNetRoot)) {
    Write-Error "BitNet not found at $BitNetRoot"
}

Set-Location $BitNetRoot
Write-Host "==> BitNet root: $BitNetRoot" -ForegroundColor Cyan

# Map light models → HF repos used by setup_env.py
$hfMap = @{
    "Falcon1B"    = "tiiuae/Falcon-E-1B-Instruct"
    "BitNetLarge" = "1bitLLM/bitnet_b1_58-large"   # ~0.7B
    "BitNet2B"    = "microsoft/BitNet-b1.58-2B-4T"
}
$hf = $hfMap[$Model]
Write-Host "==> Lightweight model choice: $Model ($hf)" -ForegroundColor Cyan

# Submodules (llama.cpp)
Write-Host "==> git submodule update --init --recursive" -ForegroundColor Cyan
git submodule update --init --recursive
if ($LASTEXITCODE -ne 0) { Write-Error "submodule update failed" }

# Python deps (minimal for convert + download)
Write-Host "==> pip install huggingface_hub gguf deps" -ForegroundColor Cyan
python -m pip install -q huggingface_hub
if (Test-Path "3rdparty\llama.cpp\gguf-py") {
    python -m pip install -q "3rdparty\llama.cpp\gguf-py"
}

# Prefer MinGW clang build (no VS ClangCL) — see scripts/build_bitnet_mingw.ps1
if (-not $SkipBuild) {
    Write-Host "==> MinGW/clang build (build_bitnet_mingw.ps1)" -ForegroundColor Cyan
    & "$PSScriptRoot\build_bitnet_mingw.ps1" -BitNetRoot $BitNetRoot
    if ($LASTEXITCODE -ne 0) { Write-Error "build failed" }
}

if (-not $SkipDownload) {
    Write-Host "==> Download + convert light model: $hf" -ForegroundColor Cyan
    $name = switch ($Model) {
        "Falcon1B" { "Falcon-E-1B-Instruct" }
        "BitNetLarge" { "bitnet_b1_58-large" }
        "BitNet2B" { "BitNet-b1.58-2B-4T" }
    }
    $dest = Join-Path "models" $name
    New-Item -ItemType Directory -Force -Path $dest | Out-Null
    python -c "from huggingface_hub import snapshot_download; snapshot_download('$hf', local_dir=r'$dest'); print('ok')"
    $i2s = Join-Path $dest "ggml-model-i2_s.gguf"
    if (-not (Test-Path $i2s)) {
        python -m pip install -q safetensors
        python utils/convert-hf-to-gguf-bitnet.py $dest --outtype f32
        $f32 = Join-Path $dest "ggml-model-f32.gguf"
        $quant = Join-Path $BitNetRoot "build\bin\llama-quantize.exe"
        if (-not (Test-Path $quant)) { $quant = Join-Path $BitNetRoot "build\bin\Release\llama-quantize.exe" }
        & $quant $f32 $i2s I2_S 1
    }
}

# Resolve GGUF for Kinetic config
$ggufs = Get-ChildItem -Path models -Recurse -Filter "*.gguf" -ErrorAction SilentlyContinue |
    Sort-Object Length
if ($ggufs) {
    $best = $ggufs | Select-Object -First 1
    # Prefer i2_s if present
    $i2 = $ggufs | Where-Object { $_.Name -like "*i2_s*" } | Select-Object -First 1
    if ($i2) { $best = $i2 }
    Write-Host "==> GGUF: $($best.FullName) ($([math]::Round($best.Length/1MB,1)) MB)" -ForegroundColor Green
    $env:KINETIC_BITNET_REPO = $BitNetRoot
    $env:KINETIC_BITNET_MODEL = $best.FullName
    Write-Host "Set for this session:" -ForegroundColor Green
    Write-Host "  `$env:KINETIC_BITNET_REPO = '$BitNetRoot'"
    Write-Host "  `$env:KINETIC_BITNET_MODEL = '$($best.FullName)'"
} else {
    Write-Host "==> No GGUF found yet under models/" -ForegroundColor Yellow
}

# Patch Kinetic bitnet.yaml model_path if GGUF exists
$cfg = "F:\Kinetic\configs\bitnet.yaml"
if ((Test-Path $cfg) -and $ggufs) {
    Write-Host "==> Update configs/bitnet.yaml model_path" -ForegroundColor Cyan
    $posix = ($best.FullName -replace "\\", "/")
    $text = Get-Content $cfg -Raw
    $text = $text -replace "model_path:.*", "model_path: $posix"
    Set-Content -Path $cfg -Value $text -NoNewline
}

Write-Host ""
Write-Host "Next:" -ForegroundColor Cyan
Write-Host "  cd F:\Kinetic"
Write-Host "  python -m kinetic.cli bitnet-info --config configs/bitnet.yaml"
Write-Host "  python -m kinetic.cli reason -i `"Pick up the bottle`" --config configs/bitnet.yaml --json"
Write-Host "  python -m kinetic.demos.pick_bottle --config configs/bitnet.yaml"