forked from violetljj/blind-assist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_host_research.ps1
More file actions
130 lines (116 loc) · 3.82 KB
/
Copy pathrun_host_research.ps1
File metadata and controls
130 lines (116 loc) · 3.82 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
[CmdletBinding()]
param(
[ValidateSet("interactive", "balanced", "throughput")]
[string]$Profile = "balanced",
[Parameter(Mandatory = $true)]
[string]$Script,
[string]$Python = "E:\codex-tools\bin\blindassist-python.cmd",
[ValidateRange(0, 256)]
[int]$Workers = 0,
[ValidateRange(0.05, 64.0)]
[double]$EstimatedGiBPerWorker = 0.30,
[ValidateRange(0.5, 128.0)]
[double]$ReserveMemoryGiB = 4.0,
[Parameter(ValueFromRemainingArguments = $true)]
[string[]]$RunnerArguments
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
if (-not (Test-Path -LiteralPath $Script -PathType Leaf)) {
throw "Research script does not exist: $Script"
}
if (-not (Test-Path -LiteralPath $Python -PathType Leaf)) {
throw "Python launcher does not exist: $Python"
}
if ($RunnerArguments -contains "--workers") {
throw "Pass worker count through -Workers or -Profile, not --workers in RunnerArguments."
}
$logicalProcessors = [Environment]::ProcessorCount
$profileTarget = switch ($Profile) {
"interactive" { 8 }
"balanced" { 12 }
"throughput" { 16 }
}
$processorCap = [Math]::Max(1, $logicalProcessors - 2)
$requestedWorkers = if ($Workers -gt 0) { $Workers } else { $profileTarget }
$os = Get-CimInstance Win32_OperatingSystem
$availableGiB = [double]$os.FreePhysicalMemory / 1MB
$installedGiB = [double]$os.TotalVisibleMemorySize / 1MB
$memoryModules = @(
Get-CimInstance Win32_PhysicalMemory -ErrorAction SilentlyContinue
)
$memoryBudgetGiB = [Math]::Max(0.0, $availableGiB - $ReserveMemoryGiB)
$memoryCap = [Math]::Max(
1,
[int][Math]::Floor($memoryBudgetGiB / $EstimatedGiBPerWorker)
)
$resolvedWorkers = [Math]::Max(
1,
[Math]::Min($requestedWorkers, [Math]::Min($processorCap, $memoryCap))
)
$battery = Get-CimInstance Win32_Battery -ErrorAction SilentlyContinue
$onAcPower = $null -eq $battery -or $battery.BatteryStatus -in 2, 3, 6, 7, 8, 9, 11
$summary = [ordered]@{
scope = "HOST_RESEARCH_ONLY"
profile = $Profile
requested_workers = $requestedWorkers
resolved_workers = $resolvedWorkers
logical_processors = $logicalProcessors
installed_memory_gib = [Math]::Round($installedGiB, 2)
memory_modules = $memoryModules.Count
available_memory_gib = [Math]::Round($availableGiB, 2)
reserve_memory_gib = $ReserveMemoryGiB
estimated_gib_per_worker = $EstimatedGiBPerWorker
on_ac_power = $onAcPower
nested_numeric_threads = 1
script = (Resolve-Path -LiteralPath $Script).Path
}
Write-Host ($summary | ConvertTo-Json -Compress)
if (-not $onAcPower) {
Write-Warning "Host research is running on battery; sustained throughput may be power-limited."
}
if ($resolvedWorkers -lt $requestedWorkers) {
Write-Warning (
"Workers reduced from {0} to {1} by CPU/memory guard." -f
$requestedWorkers,
$resolvedWorkers
)
}
if (
$Profile -eq "throughput" -and
$installedGiB -lt 24.0
) {
Write-Warning (
"Throughput mode on this 16 GiB host is capacity-sensitive; " +
"EstimatedGiBPerWorker must come from a representative pilot."
)
}
$threadVariables = @(
"OMP_NUM_THREADS",
"OPENBLAS_NUM_THREADS",
"MKL_NUM_THREADS",
"NUMEXPR_NUM_THREADS",
"VECLIB_MAXIMUM_THREADS",
"BLIS_NUM_THREADS"
)
$previousValues = @{}
foreach ($name in $threadVariables) {
$previousValues[$name] = [Environment]::GetEnvironmentVariable($name, "Process")
[Environment]::SetEnvironmentVariable($name, "1", "Process")
}
try {
& $Python $Script @RunnerArguments --workers $resolvedWorkers
$runnerExitCode = $LASTEXITCODE
} finally {
foreach ($name in $threadVariables) {
[Environment]::SetEnvironmentVariable(
$name,
$previousValues[$name],
"Process"
)
}
}
if ($null -eq $runnerExitCode) {
$runnerExitCode = 0
}
exit $runnerExitCode