models
Wrap NumPy, PyTorch, TensorFlow, and JAX models with Foolbox, manage bounds and preprocessing, load samples, measure accuracy, compose model wrappers, and plot image batches.
- License 许可
- MIT
- 最近更新
- 2026-09-03
- Stars
- ★ 351
- Forks
- 27
01 前言 Front Matter
- name
- models
- license
- MIT
- metadata
- { "sub-skill": "models", "disco-role": "operating", "root-skill": "foolbox" }
- description
- Wrap NumPy, PyTorch, TensorFlow, and JAX models with Foolbox, manage bounds and preprocessing, load samples, measure accuracy, compose model wrappers, and plot image batches.
- disable-model-invocation
- true
02 资源文件 4
- 参考文档references/api-reference.md8.0 KB
- 参考文档references/troubleshooting.md7.6 KB
- 参考文档references/workflows.md8.3 KB
- 脚本scripts/smoke_models.py3.6 KB
03 SKILL.md 正文
Foolbox models
Use this route when the task involves turning a callable or framework model into a
Foolbox model, choosing input bounds or channel layout, applying preprocessing,
changing bounds, checking clean accuracy, loading Foolbox's bundled image samples,
plotting image batches, thresholding inputs, or averaging randomized model calls.
Trigger phrases include NumPyModel, PyTorchModel, TensorFlowModel,
JAXModel, transform_bounds, accuracy, samples,
plot.images, thresholding, and expectation over transformation (EOT).
This sub-skill covers model wrappers and their utility helpers. It does not select or execute attacks, download a model zoo, or define adversarial criteria. Route those tasks to the corresponding root-skill route if available.
Command and output paths
The helper path in this document is relative to the generated Foolbox skill root,
not a native Foolbox checkout and not the caller's current directory. From any
cwd, set the directory containing the root SKILL.md and change into it first:
# Replace this non-executable placeholder with the absolute directory containing the root SKILL.md.
export SKILL_ROOT=/path/to/installed/skills/disco/foolbox
cd "$SKILL_ROOT"
Use the actual skill-root path when it differs from this installation. Keep
plots and other generated artifacts in an explicit absolute output directory or
"$SKILL_ROOT/outputs/"; create that directory before writing and never write
to the source/reference assets. The --plot-output value is a caller-chosen
filesystem path, so make it absolute (or place it under that output directory)
instead of relying on the process cwd.
Fast route
- Identify the framework, the model's native input tensor type, channel order, and the model's raw input range (before any normalization).
- Install Foolbox with
python -m pip install foolbox; install only the needed optional framework separately (torch,tensorflow, orjax). Pillow is needed bysamples()and Matplotlib byplot.images(); neither is needed to construct a basic model wrapper. - Construct the matching wrapper with a two-number
bounds=(lower, upper). For PyTorch and TensorFlow, pass preprocessing in the model's input convention. - Call the wrapper on a native tensor or EagerPy tensor and confirm logits have
shape
(batch, classes). Useaccuracy()andsamples()before any downstream work. - If callers need another input range, use
fmodel.transform_bounds(new_bounds). The default is non-mutating; do not confuse this conversion with clipping. - Add
ThresholdingWrapperonly when the model should receive a binary input, orExpectationOverTransformationWrapperaround a stochastic model when a multi-call mean is intended. Plot only four-dimensional image batches.
Framework choice and caveats
- NumPy:
NumPyModelaccepts any callable. It returns NumPy output when given NumPy input and has no inferable layout unlessdata_formatis supplied. Explicitly passchannels_firstorchannels_lasttosamples()and plotting. - PyTorch:
PyTorchModelrequires atorch.nn.Module, moves it to the selected device, defaults to CUDA when available otherwise CPU, and reportschannels_first. Put the module in.eval()mode when deterministic behavior is wanted; construction warns if it is still training. - TensorFlow:
TensorFlowModelrequires TensorFlow eager execution. Its device defaults to GPU when TensorFlow reports one, otherwise CPU, and its layout comes fromtf.keras.backend.image_data_format(). - JAX:
JAXModelaccepts a callable and defaults tochannels_last. JAX is optional;data_format=Noneis allowed at construction but reading the property then raisesAttributeError, so specify a supported layout for data helpers.
Framework packages are intentionally optional because their builds differ by platform and accelerator. Never make an unrelated NumPy-only workflow import an optional framework. See API details and troubleshooting for failure-specific handling.
Operating checklist
- Treat
boundsas the range of the inputs supplied to Foolbox, beforemean,std, orflip_axispreprocessing. - Use a negative
axisfor vector mean/std broadcasting:-3is the channel axis for NCHW and-1for NHWC. Ifaxisis present, each provided mean/std must be one-dimensional. - Keep model outputs as a batch of class scores with classes on the last axis;
accuracy()computesargmax(axis=-1)and returns a Pythonfloat. - For samples, use a supported bundled dataset name and remember that only the 20 bundled examples are available; batches above 20 repeat samples and warn.
- For plots, pass
data_formatwhenever shape-based inference could be ambiguous and pass the sameboundsused by the model.
Contract assumptions
Before writing code, record these values explicitly:
- the framework and the callable/model object that owns inference;
- the tensor layout (
NCHWorNHWC) and the class-score output shape; - the caller-facing bounds and the model-facing post-preprocessing scale;
- whether mean/std are scalar or channel vectors, and which negative axis broadcasts them;
- whether the model is deterministic, stochastic, or intentionally thresholded;
- whether sample assets and plotting dependencies are available in this runtime.
Prefer a small deterministic toy call before loading a pretrained model. Verify that the wrapper preserves the input backend, that logits have a final class axis, and that bounds conversion leaves equivalent logits approximately equal. Only then add framework device placement or a stochastic wrapper. Treat a clean accuracy value from the 20 bundled assets as a wiring check, not a model-quality claim.
Do not silently transpose arrays to repair a layout mismatch. Fix the model,
data_format, preprocessing axis, and sample request as one contract. Do not
put attacks, attack criteria, pretrained-weight downloads, or model-zoo loading
instructions in this route.
Linked runtime material
- Verified constructors, preprocessing, bounds, and return types
- Self-contained wrapping and utility workflows
- Installation and runtime troubleshooting
- NumPy model smoke helper
Minimal validation
Run the helper from any cwd by first changing to the generated skill root (or
replace the script path with the absolute $SKILL_ROOT path shown above):
cd "$SKILL_ROOT"
python sub-skills/models/scripts/smoke_models.py --help
python sub-skills/models/scripts/smoke_models.py
The second command uses only the NumPy path and bundled samples. Add --plot only
when Matplotlib is installed. For a saved plot, pass an explicit output such as
--plot-output "$SKILL_ROOT/outputs/model-smoke.png"; do not use an unqualified
filename. The helper selects a headless backend unless the caller has already
chosen one. It is a smoke check, not an attack benchmark and does not require an
optional deep-learning framework.
04 同仓库更多 Skills
passnet-triton-opt
Make a single PassNet Triton kernel fast AND numerically faithful: performance model (when a replacement can win at all), block/grid/warp tuning, autotune policy, launch overhead, and per-op numeric recipes to pass the dtype baseline tolerances. Use when a pass matches and is correct but speedup below expected, or when correctness fails by small numeric margins.
passnet-solve
END-TO-END playbook for solving one PassNet sample: analyze the computation graphs, decide the optimization strategy (what to fuse, what to replace, what to leave alone), drive the iteration loop, and maximize the sample score. This is the ENTRY skill — invoke it first for any PassNet optimization task; it tells you when to use passnet-pattern-fusion, passnet-triton-opt and passnet-feedback.
passnet-skill
PassNet GPU kernel optimization via compiler passes. Design and implement Triton-based optimization passes, create pass files under ./pass_dir/, self-evaluate with pass_evaluator, and iterate to maximize GPU speedup.
passnet-pattern-fusion
Author PassNet pass files: write patterns that actually MATCH the FX graph, pick fusion regions (which ops to absorb into one Triton kernel), and structure multi-pass submissions with the mandatory shared-dispatch architecture. Use when creating or fixing pattern()/replacement_args()/replacement_func() files, when a pass "failed to match", or when deciding how to fuse multiple kernels/ops.
passnet-orchestrate
Round/batch planning and decision support for PassNet graph optimization. Use for multi-sample triage, worker allocation, eval-budget policy, and post-eval keep/revert decisions. For solving one concrete sample end to end, use passnet-solve as the entry skill; consult this skill only when you need broader planning or a specific decision gate.
passnet-feedback
Fast, deterministic feedback for PassNet work: pre-flight pattern/match verification WITHOUT burning a GPU evaluation, per-node bottleneck analysis of a sample's graphs, and eval-log parsing into per-variant status + estimated score + failure classification. Use BEFORE every GPU evaluation (check_pattern), at the START of a sample (analyze_graph), and AFTER every evaluation (parse_eval_log).