Swiss army knife for scripted tensor surgery on model checkpoints.
pip install brainsurgeryInstall development tooling:
pip install -e ".[dev]"Enable git hooks:
pre-commit installRun hooks manually:
pre-commit run --all-filesRun mypy on demand (manual hook):
pre-commit run mypy --all-filesRun a YAML plan:
brainsurgery examples/gpt2.yamlRun multiple config inputs (deep-merged in order), then CLI overrides:
brainsurgery base.yaml override.yaml transforms[0].dump.format=compactEnable interactive mode after configured transforms:
brainsurgery -i examples/gpt2.yamlLaunch the browser-based Web UI:
brainsurgery webuiDetailed project documentation is in docs/:
- Documentation Index
- Jump point for all project documentation pages.
- Codebase Reference
- Architecture, core classes/protocols, execution processes, extension points, and module map.
- Interfaces Reference
- Batch CLI, interactive CLI, OLY, webcli, webui, and when to use each interface.
- OLY Specification
- One-line command grammar and mapping semantics.
- OLY YAML Grammar
- Exact YAML data model representable through OLY.
- OLY Conformance Matrix
- Acceptance/rejection cases and round-trip guarantees.
- SYNAPSE/1 Specification
- Declarative high-level model assembly language for PyTorch-oriented architectures.
brainsurgery [OPTIONS] [CONFIG_ITEMS]...
CONFIG_ITEMS can be:
- YAML files (
.yaml,.yml) loaded and deep-merged in order. key=valueoverrides applied last (supports dotted keys and list indices liketransforms[0].dump.format=tree).
Options:
--shard-size TEXT(default:5GB) default shard size for directory outputs.--num-workers INTEGER(default:8) max parallel I/O workers.--provider TEXT(default:inmemory) one ofinmemory,arena.--arena-root PATH(default:.brainsurgery) arena storage root.--arena-segment-size TEXT(default:1GB) segment size for arena provider.-i, --interactiverun configured transforms, then prompt for extra transforms.-s, --summarize / --no-summarize(default: summarize) emit executed transform summary.--summarize-path PATHwrite summary to file (otherwise prints YAML to stdout).--log-level TEXTone ofdebug,info,warning,error,critical.--install-completioninstall shell completion for current shell.--show-completionprint completion script for current shell.
inputs:
- model::/path/to/input.safetensors # alias::path
# with a single input, alias is optional and defaults to "model"
transforms:
- dump: { target: ".*", format: compact }
- copy: { from: ln_f.weight, to: ln_f_copy.weight }
output:
path: /path/to/output # file or directory
format: safetensors # optional: safetensors|torch
shard: 5GB # optional (safetensors only)Notes:
outputis optional. If omitted, no final checkpoint is written.- Transforms are always a list of single-key mappings.
Most transforms use tensor references:
alias::exprexpr(uses default model alias when available)alias::expr::[slice]
expr supports:
- Regex string matching (full-match).
- Structured path patterns (list form), matched against dot-separated tensor names.
Slices follow Python-like syntax in brackets, e.g. [:8], [:, :10], [1:3, :].
Structured expressions are list tokens (instead of regex strings), for example:
from: ["block", "$i", "weight"]
to: ["backup", "${i}", "weight"]Supported source tokens:
literalexact segment$xcapture one segment*xscapture zero or more segments~REGEXregex on one segment~x::REGEXbind one capture~x,y::REGEXbind multiple captures
Output tokens:
- literals (with
${x}interpolation) *xsvariadic splice
Equivalent command forms:
- YAML:
copy: { from: ln_f.weight, to: ln_f_copy.weight } - OLY:
copy: from: ln_f.weight, to: ln_f_copy.weight - YAML:
help: { assert: equal } - OLY:
help: assert: equal
Batch mode (default):
- Executes configured transforms in order.
- Any transform failure aborts execution (raises error).
Interactive mode (-i):
- First executes configured transforms in batch mode.
- Then opens prompt for additional transform blocks (YAML or OLY).
- On failure in an interactive block, logs error, stops only that submitted block, and returns to prompt.
exittransform cleanly stops the execution loop.- Interactive prompt uses a richer UI and supports tab completion for command names, payload keys, model aliases, and loaded tensor names.
Interactive prompt accepts:
- a single transform mapping (YAML or OLY), or
- a YAML list of transform mappings.
Special transforms:
helpprefixesexit
History is stored in ~/.brainsurgery_history.
All registered transforms (see Interfaces Reference and command-specific help output for operational details):
help: show command/assert help (help,help: copy,help: assert,help: { assert: equal }). OLY shorthand also works (for examplehelp: assert: equal).prefixes: list or manage model prefixes (alias::):mode=list(default),mode=addwithalias,mode=removewithalias,mode=renamewithfrom+to.exit: stop current execution loop.dump: print tensor summaries (format:json|tree|compact,verbosity:shape|stat|full).diff: compare two tensor sets by name, reporting missing-on-left, missing-on-right, and differing tensors (mode=refswithleft/right, ormode=aliaseswithleft_alias/right_alias; optionaleps).assert: evaluate assertion expressions; does not modify tensors.load: load full state_dict (path, optionalalias) or single tensor (path+to, optionalformat).save: save full state_dict (path, optionalalias,format,shard) or single tensor (target).copy: clone source tensor(s) to new destination(s) (destination must not exist).move: move tensor slot(s) without copy (no slicing; destination must not exist).delete: delete tensor(s) by target pattern.assign: copy values into existing destination tensor(s), shape/dtype/device must match.add:to = from_a + from_binto existing destination(s).subtract:to = from_a - from_binto existing destination(s).multiply:to = from_a * from_binto existing destination(s).matmul:to = from_a @ from_binto new destination(s).add_: in-placeto += from.subtract_: in-placeto -= from.scale: create new tensor(s):to = from * by.scale_: in-place scaling of target(s).cast: create new tensor(s) with new dtype.cast_: in-place dtype cast.clamp: create new tensor(s) clamped bymin/max.clamp_: in-place clamp.reshape: create new reshaped tensor(s) (shapeallows one-1).reshape_: in-place reshape/rebind.permute: create new tensor(s) with reordered dimensions (orderpermutation).fill: create new tensor(s) using source shape:mode=constant+valuemode=rand(+distribution,low/highormean/std, optionalseed)mode=tensor+values(broadcast allowed)
fill_: in-place fill with same modes asfill.zeroes: create new zero-filled tensor (target,shape).ones: create new one-filled tensor (target,shape).rand: create new random tensor (target,shape, optionaldistribution, params,seed).split: split one source tensor into multiple new tensors (tolist,sizes, optionaldim).concat: concatenate multiple source refs into one new tensor (fromlist, optionaldim).phlora_: in-place low-rank reconstruction (rank) on 2D tensors.phlora: split 2D target tensors into low-rank factors (target_a,target_b,rank); optionaldelete_original(defaulttrue),require_missing_dest(defaulttrue).
Used via:
- assert: { equal: { left: a.weight, right: b.weight, eps: 1e-6 } }OLY equivalent:
assert: equal: { left: a.weight, right: b.weight, eps: 1e-6 }
Supported operators:
exists: reference matches at least one tensor.count: exact number of matches (of,is).shape: tensor shape check (of,islist of ints).dimensions: rank comparison (of, any ofis|ge|gt|le|lt).dtype: dtype check (of,isdtype string).equal: pairwise equality between mapped tensors (left,right, optionaleps).iszero: all-zero check (of, optionaleps).reads: access-count comparison for instrumented backends (of, any ofis|ge|gt|le|lt;at_least/at_mostaccepted as compatibility aliases).writes: access-count comparison for instrumented backends (of, any ofis|ge|gt|le|lt;at_least/at_mostaccepted as compatibility aliases).all: all nested assertions succeed.any: at least one nested assertion succeeds.not: nested assertion fails.
- If
outputis omitted: no final checkpoint is saved. - If
outputis a directory-like path (or no suffix), safetensors output defaults to sharded writing using--shard-sizeunless shard is disabled. torchoutput requires file suffix.pt,.pth, or.bin.output.shardandsave.shardare safetensors-only.
When summarize is enabled (default), brainsurgery emits the exact transforms that actually ran:
- to stdout by default
- or to
--summarize-path
Useful for reproducibility when interactive edits or early exit changed execution.