forked from ollama/ollama
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen_test.ps1
More file actions
222 lines (183 loc) · 9.27 KB
/
Copy pathcodegen_test.ps1
File metadata and controls
222 lines (183 loc) · 9.27 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# codegen_test.ps1
# Ask GIGABATEMAN to write a C# Notepad app, save output, extract code, compile & validate.
$ErrorActionPreference = "Stop"
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location -LiteralPath $ScriptDir
$Model = "gigabateman"
$OutputTxt = "codegen_output.txt" # raw LLM response
$CodeFile = "NotepadApp.cs" # extracted C# source
$CompileLog = "codegen_compile_log.txt" # compiler output
$ResultsFile = "codegen_results.txt" # final summary
# ---------------------------------------------------------------------------
Write-Host "=================================================================" -ForegroundColor Cyan
Write-Host " GIGABATEMAN Code-Gen Test | C# Notepad App " -ForegroundColor Cyan
Write-Host "=================================================================" -ForegroundColor Cyan
# ---- 1. Start Ollama ---------------------------------------------------------
Write-Host "`n[1/5] Starting Ollama..." -ForegroundColor Yellow
Stop-Process -Name "ollama" -Force -ErrorAction SilentlyContinue
Stop-Process -Name "llama-server" -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 2
$env:HSA_OVERRIDE_GFX_VERSION = "12.0.1"
$env:OLLAMA_LLM_LIBRARY = "rocm"
$env:OLLAMA_FLASH_ATTENTION = "1"
$env:GGML_HIP_GRAPHS = "0"
$env:GGML_HIP_DISABLE_GRAPHS = "1"
$env:GGML_CUDA_NO_VMM = "1"
$env:AMD_DIRECT_DISPATCH = "0"
$env:HSA_ENABLE_SDMA = "0"
$env:GPU_MAX_ALLOC_PERCENT = "85"
$env:GPU_SINGLE_ALLOC_PERCENT = "70"
$env:OLLAMA_RUNNERS_DIR = "build\lib\ollama"
$env:OLLAMA_DEBUG = "0"
$env:OLLAMA_KEEP_ALIVE = "5m"
$env:OLLAMA_HOST = "127.0.0.1:11434"
$env:OLLAMA_NUM_GPU = 33
$ollamaProc = Start-Process -FilePath ".\ollama.exe" -ArgumentList "serve" -NoNewWindow -PassThru
Start-Sleep -Seconds 5
# Wait for API
$ready = $false
for ($i = 0; $i -lt 15; $i++) {
$r = curl.exe -s -m 2 http://127.0.0.1:11434/api/tags
if ($LASTEXITCODE -eq 0) { $ready = $true; break }
Start-Sleep -Seconds 1
}
if (-not $ready) { Write-Host "[ERROR] Ollama not ready!" -ForegroundColor Red; exit 1 }
Write-Host " Ollama API is ready." -ForegroundColor Green
# ---- 2. Send code-gen prompt -------------------------------------------------
Write-Host "`n[2/5] Sending code-generation prompt to $Model..." -ForegroundColor Yellow
$CodePrompt = @"
You are an expert C# developer. Write a complete, fully working Windows Forms Notepad application in C#.
Requirements:
- A main form with a multi-line TextBox (rich text area) that fills the window
- A menu bar with: File (New, Open, Save, Save As, Exit), Edit (Cut, Copy, Paste, Select All), Help (About)
- New: clears the text, Open: opens a file dialog to load .txt files, Save/Save As: save to file
- Title bar shows the filename and an asterisk (*) if unsaved changes exist
- Word wrap toggle in the Format menu
- The app must compile with: csc /target:winexe NotepadApp.cs
- All code must be in a SINGLE file called NotepadApp.cs
- Do NOT use any external libraries beyond standard .NET Framework assemblies
- Output ONLY the raw C# code. Do not include any explanation, markdown fences, or comments outside the code.
"@
$payload = @{ model = $Model; prompt = $CodePrompt; stream = $false; options = @{ temperature = 0.2; num_predict = 4096 } } | ConvertTo-Json -Compress -Depth 5
$tempPayload = Join-Path $env:TEMP "codegen_payload.json"
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
[System.IO.File]::WriteAllText($tempPayload, $payload, $utf8NoBom)
Write-Host " Generating... (this may take 30-120 seconds)" -ForegroundColor DarkCyan
$startTime = Get-Date
$rawOutput = curl.exe -s -m 300 -X POST http://127.0.0.1:11434/api/generate -H "Content-Type: application/json" -d "@$tempPayload"
$elapsed = [math]::Round(((Get-Date) - $startTime).TotalSeconds, 1)
Remove-Item $tempPayload -ErrorAction SilentlyContinue
$result = $rawOutput | ConvertFrom-Json
$generatedText = $result.response
if (-not $generatedText) {
Write-Host "[ERROR] Model returned empty response!" -ForegroundColor Red
Stop-Process -Id $ollamaProc.Id -Force -ErrorAction SilentlyContinue
exit 1
}
$evalRate = if ($result.eval_duration -gt 0) { [math]::Round($result.eval_count / ($result.eval_duration / 1e9), 2) } else { 0 }
Write-Host " Done! Eval: $evalRate tokens/s | Time: ${elapsed}s | Tokens: $($result.eval_count)" -ForegroundColor Green
# Save raw LLM output
$generatedText | Out-File -FilePath $OutputTxt -Encoding utf8
Write-Host " Raw output saved to: $OutputTxt" -ForegroundColor DarkGray
# ---- 3. Extract C# code ------------------------------------------------------
Write-Host "`n[3/5] Extracting C# code from response..." -ForegroundColor Yellow
# Strip markdown code fences if the model added them despite instructions
$code = $generatedText
# Remove ```csharp or ```cs fences
$code = $code -replace '(?s)^```(?:csharp|cs|c#)?\s*', ''
$code = $code -replace '(?s)```\s*$', ''
# Trim leading/trailing whitespace
$code = $code.Trim()
# Sanity check: must contain 'using System' and 'class'
if ($code -notmatch 'using System' -or $code -notmatch '\bclass\b') {
Write-Host " [WARN] Response doesn't look like valid C#. Saving anyway." -ForegroundColor DarkYellow
}
$codeBytes = [System.Text.Encoding]::UTF8.GetBytes($code)
[System.IO.File]::WriteAllBytes((Join-Path (Get-Location) $CodeFile), $codeBytes)
Write-Host " C# code saved to: $CodeFile ($($code.Length) chars)" -ForegroundColor Green
# ---- 4. Compile with csc -----------------------------------------------------
Write-Host "`n[4/5] Compiling with csc..." -ForegroundColor Yellow
# Find csc.exe
$cscPaths = @(
"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe",
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe"
)
$csc = $null
foreach ($p in $cscPaths) { if (Test-Path $p) { $csc = $p; break } }
$compileSuccess = $false
$compileOutput = ""
if (-not $csc) {
Write-Host " [WARN] csc.exe not found, trying 'dotnet' build fallback..." -ForegroundColor DarkYellow
# Fallback: try dotnet script / dotnet-script or just report missing
$dotnetVer = & dotnet --version 2>&1
if ($LASTEXITCODE -eq 0) {
# Create a minimal .csproj and compile
$projDir = Join-Path (Get-Location) "NotepadAppBuild_q1prime"
New-Item -ItemType Directory -Force -Path $projDir | Out-Null
Copy-Item $CodeFile "$projDir\NotepadApp.cs" -Force
$csprojContent = @"
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<Nullable>disable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
</Project>
"@
$csprojContent | Out-File "$projDir\NotepadApp.csproj" -Encoding utf8 -NoNewline
Write-Host " Building with dotnet (net8.0-windows)..." -ForegroundColor DarkCyan
$compileOutput = & dotnet build "$projDir\NotepadApp.csproj" --nologo -o NotepadAppOutput_q1prime 2>&1 | Out-String
$compileSuccess = ($LASTEXITCODE -eq 0)
}
else {
$compileOutput = "Neither csc.exe nor dotnet SDK found on this system."
}
}
else {
Write-Host " Using: $csc" -ForegroundColor DarkGray
$compileOutput = & $csc /target:winexe /out:NotepadApp.exe $CodeFile 2>&1 | Out-String
$compileSuccess = ($LASTEXITCODE -eq 0)
}
$compileOutput | Out-File -FilePath $CompileLog -Encoding utf8
Write-Host " Compiler output saved to: $CompileLog" -ForegroundColor DarkGray
if ($compileSuccess) {
Write-Host " COMPILE SUCCESS!" -ForegroundColor Green
}
else {
Write-Host " COMPILE FAILED. See $CompileLog for details." -ForegroundColor Red
}
# ---- 5. Write results summary ------------------------------------------------
Write-Host "`n[5/5] Writing results summary..." -ForegroundColor Yellow
Stop-Process -Id $ollamaProc.Id -Force -ErrorAction SilentlyContinue
$summary = @"
=== GIGABATEMAN Code-Gen Test Results ===
Timestamp : $(Get-Date)
Model : $Model (Q2_K, 7B)
GPU Layers : 33 (full offload, RX 9070 XT gfx1201)
--- Generation Stats ---
Prompt Tokens : $($result.prompt_eval_count)
Output Tokens : $($result.eval_count)
Eval Rate : $evalRate tokens/s
Total Time : ${elapsed}s
--- Code Quality ---
Response Length : $($generatedText.Length) chars
Extracted Code : $($code.Length) chars
Contains using : $($code -match 'using System')
Contains class : $($code -match '\bclass\b')
Contains Form : $($code -match '\bForm\b')
--- Compilation ---
Compiler : $(if ($csc) { $csc } else { "dotnet build" })
Result : $(if ($compileSuccess) { "SUCCESS - NotepadApp.exe created" } else { "FAILED - see $CompileLog" })
--- Files ---
Raw LLM Output : $OutputTxt
Extracted Code : $CodeFile
Compiler Log : $CompileLog
Executable : $(if ($compileSuccess) { "NotepadApp.exe" } else { "N/A" })
"@
$summary | Out-File -FilePath $ResultsFile -Encoding utf8
Write-Host $summary -ForegroundColor Cyan
Write-Host "`n=================================================================" -ForegroundColor $(if ($compileSuccess) { "Green" } else { "Red" })
Write-Host " TEST $(if ($compileSuccess) { 'PASSED' } else { 'FAILED' }) - Results in $ResultsFile" -ForegroundColor $(if ($compileSuccess) { "Green" } else { "Red" })
Write-Host "=================================================================" -ForegroundColor $(if ($compileSuccess) { "Green" } else { "Red" })