# Downloads Lc0 self-play data and converts it to montyformat for the policy trainer. # powershell -ExecutionPolicy Bypass -File C:\Users\rektd\repos\Rigby\train\policy\fetch.ps1 # ... fetch.ps1 -From 20261025 -To 20261115 -Run test91 -Parallel 32 # Default: the last three weeks of test91 (about 40 GB of tars, 190M positions). # # The server limits each connection to about 2.3 MB/s but not their number # (32 connections: ~72 MB/s), so tars are downloaded -Parallel at a time, in # batches of whole days (about two tars per connection) into data\lc0\staging. # Each day of a batch is then converted into data\lc0\monty\-.binpack # and its tars deleted, so the disk only holds one batch of tars. Days already # converted are skipped, so an interrupted run can simply be started again. At # the end every day file is shuffled into data\lc0\policy.binpack, which is # what train.ps1 reads. param( [string] $From = (Get-Date).AddDays(-21).ToString('yyyyMMdd'), [string] $To = (Get-Date -Format 'yyyyMMdd'), [string] $Run = 'test91', [int] $Parallel = 32, [switch] $NoInterleave ) # Native tools report through exit codes; 'Stop' would turn cargo's progress # output on stderr into errors under Windows PowerShell. $ErrorActionPreference = 'Continue' Set-Location $PSScriptRoot . .\env.ps1 cargo build --release --bins if ($LASTEXITCODE -ne 0) { throw 'build failed' } $base = "https://storage.lczero.org/files/training_data/$Run/" $data = (Resolve-Path '..\data').Path $monty = Join-Path $data 'lc0\monty' $staging = Join-Path $data 'lc0\staging' New-Item -ItemType Directory -Force $monty, $staging | Out-Null # The directory listing: name, date and size of every hourly tar. Empty tars # (10240 bytes) are hours without games. Write-Host "Listing $base ..." $html = (Invoke-WebRequest -UseBasicParsing $base).Content $tars = [regex]::Matches($html, '>(training-[^<]+-(\d{8})-\d{4}\.tar)\s+\S+\s+\S+\s+(\d+)') | ForEach-Object { [pscustomobject]@{ Name = $_.Groups[1].Value; Day = $_.Groups[2].Value; Size = [long]$_.Groups[3].Value } } | Where-Object { $_.Day -ge $From -and $_.Day -le $To -and $_.Size -gt 10240 } $days = $tars | Group-Object Day | Sort-Object Name $totalGb = ($tars | Measure-Object Size -Sum).Sum / 1e9 Write-Host ("{0} tars over {1} days, {2:N1} GB to download" -f $tars.Count, $days.Count, $totalGb) $pending = @($days | Where-Object { -not (Test-Path (Join-Path $monty "$Run-$($_.Name).binpack")) }) $done = $days.Count - $pending.Count if ($done) { Write-Host "$done days already converted" } while ($pending.Count) { # Whole days until the batch has about two tars per connection. $batch = @() $count = 0 while ($pending.Count -and ($batch.Count -eq 0 -or $count -lt 2 * $Parallel)) { $batch += $pending[0] $count += $pending[0].Count $pending = @($pending | Select-Object -Skip 1) } if (Test-Path $staging) { Remove-Item -Recurse -Force $staging } New-Item -ItemType Directory -Force $staging | Out-Null $config = Join-Path $staging 'curl.txt' # Forward slashes: curl reads backslashes in a config file as escapes. $lines = foreach ($day in $batch) { $dir = Join-Path $staging $day.Name New-Item -ItemType Directory -Force $dir | Out-Null foreach ($tar in $day.Group) { "url = `"$base$($tar.Name)`"`noutput = `"$((Join-Path $dir $tar.Name) -replace '\\', '/')`"" } } $lines | Set-Content -Encoding ascii $config $gb = ($batch.Group | Measure-Object Size -Sum).Sum / 1e9 $start = Get-Date Write-Host ("{0}..{1}: downloading {2} tars ({3:N2} GB)" -f $batch[0].Name, $batch[-1].Name, $count, $gb) curl.exe -sSf --retry 5 -Z --parallel-max $Parallel -K $config if ($LASTEXITCODE -ne 0) { throw "download failed for $($batch[0].Name)..$($batch[-1].Name)" } $secs = ((Get-Date) - $start).TotalSeconds Write-Host (" {0:N0} MB/s" -f ($gb * 1000 / [math]::Max($secs, 1))) foreach ($day in $batch) { $out = Join-Path $monty "$Run-$($day.Name).binpack" & .\target\release\lc02monty.exe "$out.part" (Join-Path $staging $day.Name) --delete | Select-Object -Last 9 if ($LASTEXITCODE -ne 0) { throw "conversion failed for $($day.Name)" } Move-Item -Force "$out.part" $out } } if (Test-Path $staging) { Remove-Item -Recurse -Force $staging } if (-not $NoInterleave) { $policy = Join-Path $data 'lc0\policy.binpack' Write-Host "Shuffling the day files into $policy ..." & .\target\release\interleave.exe "$policy.part" $monty if ($LASTEXITCODE -ne 0) { throw 'interleave failed' } Move-Item -Force "$policy.part" $policy Write-Host ("Done: {0:N1} GB in {1}" -f ((Get-Item $policy).Length / 1e9), $policy) }