| Обсуждение Пайплайн для 3B моделей на 1050Ti |
|
Автор темы:
Пролыч
Дата создания: 25.11.2026 18:02
|
|
Пайплайн для 3B моделей на 1050Ti
Я сделал пайплайн локальных 3B моделей для разработки небольших программ на Go.
Опробовано на 1050Ti 4g vram, FX 6300, ram 16g, SSD в вин10 и альпине линукс под WSL2. Да, пайплайн работает и на вин10 и в линуксе. Инструменты написаны на го самим пайплайном после моего ручного бутстрапа. Общий размер без llama-cli и моделей менее десяти мегабайт. Сам пайплайн на rexx (Regina). Инструменты на го 1.17.13 стдлиб без внешних зависимостей. Все исходники, промпты, темплеты и другие текстовые файлы должны быть в UTF-8. Оркестратор: Код:
/* pipeline.rex — TDD orchestrator: plan -> decomp -> tests (anti-vacuum) -> code (best-of-N + fix) -> review
Usage: regina pipeline.rex <goal.txt> <out/run-N>
*/
signal on novalue name undef
signal on syntax
parse arg goalFile outDir
if goalFile = '' | outDir = '' then do
say 'usage: regina pipeline.rex <goal.txt> <out/run-N>'
exit 1
end
addr = translate(address())
if pos('CMD', addr) > 0 | pos('WINDOWS', addr) > 0 then platform = 'WIN'
else platform = 'UNIX'
call readCfg 'pipeline.cfg'
value('GO111MODULE', 'on', , 'ENVIRONMENT')
value('GOPROXY', 'off', , 'ENVIRONMENT')
value('GOSUMDB', 'off', , 'ENVIRONMENT')
call ensureDir outDir
call ensureDir outDir || '/.work'
call writeFile outDir || '/go.mod', "module main" || '0A'x || '0A'x || "go 1.17" || '0A'x
goal = readFile(goalFile)
call writeFile outDir || '/goal.txt', goal
say '==> [1/5] Plan (contract)'
vars.GOAL = goal
plan = runStage('plan', 'text', outDir, 1)
call writeFile outDir || '/plan.md', plan
say '==> [2/5] Decomp'
vars.GOAL = goal
vars.PLAN = plan
decomp = runStage('decomp', 'text', outDir, 1)
call writeFile outDir || '/decomp.md', decomp
say '==> [3/5] Tests (from contract + anti-vacuum)'
tests = ''
testHash = ''
do testAttempt = 1 to cfg.max_attempts
vars.PLAN = plan
vars.ERRORS = ''
tests = runStage('tests', 'code', outDir, testAttempt)
call writeFile outDir || '/.work/main_test.go', tests
rc = runGates(outDir || '/.work/main_test.go', cfg.test_max_lines, 'test')
if rc \= 0 then do
say ' tests attempt 'testAttempt' failed gates'
iterate
end
testHash = runSha256(outDir || '/.work/main_test.go')
vars.PLAN = plan
stub = runStage('stub', 'code', outDir, testAttempt)
call writeFile outDir || '/.work/stub.go', stub
rc = runGates(outDir || '/.work/stub.go', cfg.max_lines, 'code')
if rc \= 0 then do
say ' stub failed gates, retry'
iterate
end
rc = runGo('build-stub', '')
if rc \= 0 then do
say ' stub does not compile, retry'
iterate
end
rc = runGo('test-stub-compile', '')
if rc \= 0 then do
say ' tests do not compile with stub, retry'
iterate
end
rc = runGo('test-stub-run', '')
if rc = 0 then do
say ' tests passed on stub — vacuum, retry'
iterate
end
call sysCopyFile outDir || '/.work/main_test.go', outDir || '/main_test.go'
leave
end
if testHash = '' then do
say 'pipeline: failed to generate valid tests'
exit 5
end
say '==> [4/5] Code (best-of-'cfg.best_of' + fix)'
code = ''
bestCode = ''
bestScore = -1
do i = 1 to cfg.best_of
vars.PLAN = plan
vars.DECOMP = decomp
vars.ERRORS = ''
cand = runStage('code', 'code', outDir, i)
call writeFile outDir || '/.work/cand_'i'.go', cand
rc = runGates(outDir || '/.work/cand_'i'.go', cfg.max_lines, 'code')
if rc \= 0 then do
say ' candidate 'i' failed gates'
iterate
end
rc = runGo('build-cand', i)
if rc \= 0 then do
say ' candidate 'i' does not build'
iterate
end
rc = runGo('test-cand', i)
if rc = 0 then do
say ' candidate 'i' OK'
code = cand
call sysCopyFile outDir || '/.work/cand_'i'.go', outDir || '/main.go'
leave
end
failedCount = runCountFailed(outDir || '/test-cand.log')
score = 1000 - failedCount
say ' candidate 'i': 'failedCount' tests failed, score='score
if score > bestScore then do
bestScore = score
bestCode = outDir || '/.work/cand_'i'.go'
end
end
if code = '' then do
if bestScore < 0 then do
say 'pipeline: no candidate passed gates'
exit 4
end
call sysCopyFile bestCode, outDir || '/main.go'
code = readFile(outDir || '/main.go')
fixAttempt = 0
gateRetries = 0
maxGateRetries = 3
do while fixAttempt < cfg.max_attempts
fixAttempt = fixAttempt + 1
rc = runGo('build-main', '')
call writeFile outDir || '/build.log', lastGoOut
if rc = 0 then do
rc = runGo('test-main', '')
call writeFile outDir || '/test.log', lastGoOut
if rc = 0 then leave
end
say ' fix 'fixAttempt'/'cfg.max_attempts
errors = tailTruncate(lastGoOut, cfg.errors_max_bytes)
vars.ERRORS = errors
vars.CODE = code
code = runStage('fix', 'code', outDir, fixAttempt)
call writeFile outDir || '/.work/fix_'fixAttempt'.go', code
rc = runGates(outDir || '/.work/fix_'fixAttempt'.go', cfg.max_lines, 'code')
if rc \= 0 then do
gateRetries = gateRetries + 1
say ' fix 'fixAttempt' failed gates (retry 'gateRetries'/'maxGateRetries')'
if gateRetries >= maxGateRetries then do
say 'pipeline: too many gates failures in fix loop'
exit 4
end
fixAttempt = fixAttempt - 1
iterate
end
gateRetries = 0
newHash = runSha256(outDir || '/main_test.go')
if newHash \= testHash then do
say 'pipeline: tests were modified during fix'
exit 5
end
call sysCopyFile outDir || '/.work/fix_'fixAttempt'.go', outDir || '/main.go'
code = readFile(outDir || '/main.go')
end
rc = runGo('build-main', '')
call writeFile outDir || '/build.log', lastGoOut
if rc \= 0 then exit 4
rc = runGo('test-main', '')
call writeFile outDir || '/test.log', lastGoOut
if rc \= 0 then exit 5
end
rc = runGo('vet', '')
call writeFile outDir || '/vet.log', lastGoOut
vetOut = lastGoOut
if cfg.race = 1 then do
rc = runGo('race', '')
call writeFile outDir || '/race.log', lastGoOut
vetOut = vetOut || '0A'x || '0A'x || 'RACE:' || '0A'x || lastGoOut
end
say '==> [5/5] Review'
vars.PLAN = plan
vars.CODE = readFile(outDir || '/main.go')
vars.TESTS = readFile(outDir || '/main_test.go')
vars.VET = vetOut
review = runStage('review', 'text', outDir, 1)
call writeFile outDir || '/review.md', review
say 'pipeline: ok'
exit 0
normPath: procedure expose platform
parse arg p
if platform = 'WIN' then return changestr('/', p, '\')
return p
runStage: procedure expose cfg. platform lastGoOut outDir vars.
parse arg stage, extract, outDir, attempt
tpl = readFile('prompts/' || stage || '.tpl')
prompt = renderPrompt(tpl)
promptFile = outDir || '/prompt_' || stage || '_' || attempt || '.txt'
call writeFile promptFile, prompt
key = 'cfg.' || translate('model_' || stage)
if symbol(key) = 'VAR' then model = value(key)
else model = cfg.model
key = 'cfg.' || translate('extra_' || stage)
if symbol(key) = 'VAR' then extra = value(key)
else extra = cfg.extra
key = 'cfg.' || translate('reasoning_budget_' || stage)
if symbol(key) = 'VAR' then reasoningBudget = value(key)
else reasoningBudget = cfg.reasoning_budget
key = 'cfg.' || translate('ctx_' || stage)
if symbol(key) = 'VAR' then ctx = value(key)
else ctx = cfg.ctx
key = 'cfg.' || translate('seed_offset_' || stage)
if symbol(key) = 'VAR' then offset = value(key)
else offset = 0
seed = cfg.seed + offset + attempt
outFile = outDir || '/' || outName(stage, attempt)
cmd = buildLlmreqCmd(model, normPath(promptFile), normPath(outFile), extract, ctx, reasoningBudget, extra, seed)
if platform = 'WIN' then cmd = 'cmd /c ' || cmd
address system cmd
if rc \= 0 then do
say 'pipeline: llmreq failed stage='stage' rc='rc
exit 3
end
return readFile(outFile)
outName: procedure
parse arg stage, attempt
select
when stage = 'plan' then return 'plan.md'
when stage = 'decomp' then return 'decomp.md'
when stage = 'tests' then return '.work/main_test.go'
when stage = 'stub' then return '.work/stub.go'
when stage = 'code' then return '.work/cand_'attempt'.go'
when stage = 'fix' then return '.work/fix_'attempt'.go'
when stage = 'review' then return 'review.md'
end
return stage || '.out'
buildLlmreqCmd: procedure expose cfg.
parse arg model, promptFile, outFile, extract, ctx, reasoningBudget, extra, seed
cmd = '"' || cfg.llmreq || '"' ,
|| ' --llama-bin "' || cfg.llama_bin || '"' ,
|| ' --model "' || model || '"' ,
|| ' --prompt-file "'|| promptFile || '"' ,
|| ' --extract ' || extract ,
|| ' --ctx ' || ctx ,
|| ' --ngl ' || cfg.ngl ,
|| ' --threads ' || cfg.threads ,
|| ' --max-tokens ' || cfg.max_tokens ,
|| ' --temp ' || cfg.temp ,
|| ' --seed ' || seed ,
|| ' --timeout ' || cfg.timeout || 's'
if reasoningBudget > 0 then cmd = cmd || ' --reasoning-budget ' || reasoningBudget
if extra \= '' then cmd = cmd || ' --extra "' || extra || '"'
cmd = cmd || ' > "' || outFile || '"'
return cmd
renderPrompt: procedure expose cfg. vars.
parse arg tpl
pos = 1
do while pos > 0
start = pos('{{', tpl, pos)
if start = 0 then leave
end = pos('}}', tpl, start)
if end = 0 then leave
key = substr(tpl, start + 2, end - start - 2)
varName = 'vars.' || key
if symbol(varName) = 'VAR' then val = value(varName)
else val = '{{' || key || '}}'
tpl = substr(tpl, 1, start - 1) || val || substr(tpl, end + 2)
pos = start + length(val)
end
tpl = changestr('{{MAX_LINES}}', tpl, cfg.max_lines)
tpl = changestr('{{TEST_MAX_LINES}}', tpl, cfg.test_max_lines)
return tpl
tailTruncate: procedure
parse arg s, maxBytes
if length(s) <= maxBytes then return s
return substr(s, length(s) - maxBytes + 1)
runGates: procedure expose cfg. platform
parse arg file, maxLines, kind
cmd = cfg.tools_dir || '/gates -w -kind ' || kind || ' -max-lines ' || maxLines || ' "' || normPath(file) || '"'
if platform = 'WIN' then cmd = 'cmd /c ' || cmd
address system cmd
return rc
runSha256: procedure expose cfg. platform outDir
parse arg file
logFile = outDir || '/.work/sha.txt'
cmd = cfg.tools_dir || '/sha256 "' || normPath(file) || '"'
if platform = 'WIN' then cmd = 'cmd /c ' || cmd
cmd = cmd || ' > "' || normPath(logFile) || '" 2>&1'
address system cmd
if rc \= 0 then return ''
return strip(readFile(logFile))
runCountFailed: procedure expose cfg. platform outDir
parse arg logFile
outFile = outDir || '/.work/count.txt'
cmd = cfg.tools_dir || '/count-failed "' || normPath(logFile) || '"'
if platform = 'WIN' then cmd = 'cmd /c ' || cmd
cmd = cmd || ' > "' || normPath(outFile) || '" 2>&1'
address system cmd
if rc \= 0 then return 0
return strip(readFile(outFile))
runGo: procedure expose cfg. platform lastGoOut outDir
parse arg kind, arg1
logFile = outDir || '/' || logName(kind)
cmd = goCmd(kind, arg1)
if platform = 'WIN' then cmd = 'cmd /c cd /d "' || normPath(outDir) || '" && ' || cmd || ' > "' || logName(kind) || '" 2>&1'
else cmd = 'cd "' || outDir || '" && ' || cmd || ' > "' || logName(kind) || '" 2>&1'
address system cmd
saveRc = rc
lastGoOut = changestr('0D0A'x, readFile(logFile), '0A'x)
return saveRc
goCmd: procedure expose platform
parse arg kind, n
w = '.work/'
select
when kind = 'build-main' then return 'go build -o main' || binExt() || ' main.go'
when kind = 'build-stub' then return 'go build -o ' || w || 'stub' || binExt() || ' ' || w || 'stub.go'
when kind = 'build-cand' then return 'go build -o ' || w || 'cand_'n || binExt() || ' ' || w || 'cand_'n'.go'
when kind = 'vet' then return 'go vet .'
when kind = 'test-main' then return 'go test -count=1 .'
when kind = 'test-stub-compile' then return 'go test -c -o ' || w || 'stub.test' || binExt() || ' ' || w || 'stub.go ' || w || 'main_test.go'
when kind = 'test-stub-run' then return 'go test -count=1 ' || w || 'stub.go ' || w || 'main_test.go'
when kind = 'test-cand' then return 'go test -count=1 ' || w || 'cand_'n'.go ' || w || 'main_test.go'
when kind = 'race' then return 'go test -race -count=1 .'
end
return ''
logName: procedure
parse arg kind
select
when kind = 'build-main' then return 'build.log'
when kind = 'build-stub' then return 'build-stub.log'
when kind = 'build-cand' then return 'build-cand.log'
when kind = 'vet' then return 'vet.log'
when kind = 'test-main' then return 'test.log'
when kind = 'test-stub-compile' then return 'test-stub-compile.log'
when kind = 'test-stub-run' then return 'test-stub-run.log'
when kind = 'test-cand' then return 'test-cand.log'
when kind = 'race' then return 'race.log'
end
return kind || '.log'
binExt: procedure expose platform
if platform = 'WIN' then return '.exe'
return ''
readFile: procedure
parse arg file
call stream file, 'c', 'open read binary'
s = ''
do while \ stream(file, 'c', 'query eof')
s = s || charin(file, , 4096)
end
call stream file, 'c', 'close'
return s
writeFile: procedure
parse arg file, content
call stream file, 'c', 'open write replace binary'
call charout file, content
call stream file, 'c', 'close'
return
sysCopyFile: procedure
parse arg src, dst
call writeFile dst, readFile(src)
return
readCfg: procedure expose cfg. platform
parse arg file
cfg.backend = 'cli'
cfg.llmreq = 'bin/llmreq'
cfg.llama_bin = 'llama-cli'
cfg.tools_dir = 'bin'
cfg.model = ''
cfg.ctx = 4096
cfg.ngl = 99
cfg.threads = 4
cfg.max_tokens = 1024
cfg.temp = 0.2
cfg.seed = 42
cfg.timeout = 300
cfg.extra = ''
cfg.reasoning_budget = 0
cfg.max_attempts = 3
cfg.errors_max_bytes = 4096
cfg.best_of = 3
cfg.max_lines = 120
cfg.test_max_lines = 200
cfg.race = 0
if \ sysFileExists(file) then return
call stream file, 'c', 'open read'
do while lines(file)
line = linein(file)
line = strip(line)
if line = '' then iterate
if substr(line, 1, 1) = '#' then iterate
eq = pos('=', line)
if eq = 0 then iterate
key = strip(translate(substr(line, 1, eq - 1)))
val = strip(substr(line, eq + 1))
interpret 'cfg.' || key || ' = "' || escape(val) || '"'
end
call stream file, 'c', 'close'
return
escape: procedure
parse arg s
s = changestr('"', s, '""')
return s
sysFileExists: procedure
parse arg f
return stream(f, 'c', 'query exists') \= ''
ensureDir: procedure expose platform
parse arg dir
if sysFileExists(dir) then return
if platform = 'WIN' then cmd = 'cmd /c mkdir "' || normPath(dir) || '" 2>nul'
else cmd = 'mkdir -p "' || dir || '" 2>/dev/null'
address system cmd
return
undef:
say 'pipeline: undefined variable' sigl
exit 1
syntax:
say 'pipeline: syntax error' rc 'at line' sigl
exit 1
|
|
| Ваши права в разделе |
Быстрая навигация
|
