Core / Securing models and the ML supply chain
Securing models and the ML supply chain cheat sheet
Set up and baseline (lessons 0 and 1)
| Task | Command or code |
|---|---|
| Check the lab | python3 labcheck.py |
| Install the pinned versions | pip install -r requirements.txt |
| Load the fixed split of the digits | X_train, X_test, y_train, y_test = digits() |
| Train the course model (a logistic regression that settles: the same printed numbers on every processor) | model = train(X_train, y_train) |
| The same kind of model, not yet trained | logistic() or logistic(0.01) |
| One recall for each class | recalls(model, X_test, y_test) |
| Keep a measured value | record("digits.accuracy", 0.9533) |
| Show the results file | python3 common.py |
Poison the data and detect it (lesson 2)
| Task | Command or code |
|---|---|
| Flip a share of the labels of one class | flip_labels(y, 8, 3, 0.2) |
| Add copies with a wrong label | inject(X, y, 8, 3, 65) |
| Accuracy gate and class gate | gates(model, X_val, y_val, baseline) |
| Compare with a fixed baseline, not with last week | acc >= baseline - ACC_DROP |
| Run the whole table (five seeds for each row) | python3 poison.py |
Backdoors (lesson 3)
| Task | Command or code |
|---|---|
| Stamp a corner patch on images | stamp(X, 6, 6) |
| Plant a backdoor | plant(X, y, 20) |
| Success rate of a trigger | success_rate(model, X_test, y_test) |
| Scan for a trigger | scan(model, X_test) gives (row, col, class, share) |
| Flag a model | share > SCAN_LIMIT (0.60) |
| Remove the poisoned images, then retrain | remove(X, y, row, col), then train(Xc, yc) |
| Tune a downloaded network (a little tuning may leave a backdoor) | fine_tune(net, X, y, 40) |
Evasion and hardening (lesson 4)
| Task | Command or code |
|---|---|
| Gradient attack on a linear model | fgsm(model, X, y, 2) |
| Black-box search with a query budget | search(model.predict_proba, X, y, 300) |
| Train on noisy copies | noisy_copies(X, y, sigma=6) |
| Input check | input_ok(X, limit) |
| The limit of the check | np.percentile(roughness(X_train), 99) |
Privacy leaks and defences (lessons 5 and 6)
| Task | Command or code |
|---|---|
| Membership attack with a shadow model | attack_accuracy(lambda: RandomForestClassifier(random_state=0)) |
| Mean confidence on members and non-members | mean_confidence(make) |
| Rebuild a class from scores | invert(model, 3) |
| Show an 8 by 8 image | show(x) |
| Show labels only | LabelsOnly(model) |
| Train on noisy images | NoisyTraining(model, 3) |
| Count with epsilon privacy | true_count + rng.laplace(0, 1 / epsilon) |
| Clip what one user adds to a count | clipped_count(users, wanted, cap=1) |
Model theft (lesson 7)
| Task | Command or code |
|---|---|
| Copy a model from its labels | copy_from(victim, 225) |
| Fidelity of a copy | fidelity(copy, victim) |
| Profile the clients of a log | profile(make_log()) |
| Flag by rate and by near-copies | flagged(stats, per_minute=10, near_share=0.3) |
| Make and count a watermark | watermark(), hits(model, marks) |
| Copy of the file with half the trees | half_the_trees(model) |
Model files (lessons 8 and 9)
| Task | Command or code |
|---|---|
| Read a pickle without loading it | python3 -m pickletools file.pkl |
| Export a model to JSON or arrays | export_json(model, "models/target.json"), export_npz(...) |
Save and load weights as safetensors (make coef_ contiguous first) | save_file({"coef": np.ascontiguousarray(model.coef_), ...}, path), load_file(path) |
| Read the header of a safetensors file | struct.unpack("<Q", raw[:8]), then json.loads(raw[8:8 + size]) |
| Hash a file | sha256_of("models/target.json") |
Load only what is proven (.json, .npz or .safetensors) | load_model(path, expected_sha256) |
Refuse a small .npz that unpacks to a huge array | the loader adds the unpacked sizes: sum(i.file_size for i in zipfile.ZipFile(path).infolist()) |
| Load a pickle that you made yourself | convert_pickle("candidate/model.pkl", "candidate/model.json") |
Release gates and the scorecard (lessons 10 and 11)
| Task | Command or code |
|---|---|
| Build a folder that can be released | python3 ship.py ship |
| Run the seven gates | python3 gates.py ship |
| Train again from the recorded data and compare | reproduce_gate(model, X, y) |
| Make two damaged folders to test the gates | python3 makebad.py |
| Stop a deployment when a gate fails | python3 gates.py ship && ./deploy.sh |
| Grade a folder on five areas | python3 scorecard.py ship |
Grade a weaker model (the same ship.py, fewer images) | python3 weak.py weak, then python3 scorecard.py weak |
| Read the exit code | echo "exit code $?" |
The scenario test (lesson 12)
| Task | Command or code |
|---|---|
| Make the folder to grade | python3 colleague.py |
| Grade, convert, fix, grade again | python3 scorecard.py candidate, convert_pickle(...), remove(...), python3 scorecard.py candidate |
| Write the decision note | decision.md, first line HOLD or SHIP |
Safety habits
- Attack only the models that you train in your own lab, and follow every attack with a measured defence.
- Keep a trusted set that the data pipeline cannot touch. Compare every model with a fixed baseline, never with the last round.
- Look at the result for each class. An average hides a poisoned class.
- Test a model for a trigger, not only for accuracy on clean data. A little tuning on clean data may leave a backdoor in place.
- Report a robustness number with its attacker, its size of change and its number of queries. Repeat an attack with several seeds: one run is a measurement, not a result.
- Say "measured against this attack" and not "private". Only a result with a stated epsilon, from a method that is proved, is a privacy guarantee.
- Limit and watch queries. Show the least detail that the use needs.
- Do not load a pickle from anyone but yourself. Ship a model as numbers (JSON, npz or safetensors), with a hash that comes from a channel the sender cannot change.
- Let a script with an exit code decide a release, and keep its thresholds in the repository with an owner.