Download agent-scratch/main-src/binding-errors.lisp from asemqaffaf/AsemLLM-runs: direct link, hf CLI and curl.
- Browser
- Download file 15.5 kB
-
https://huggingface.co/asemqaffaf/AsemLLM-runs/resolve/main/agent-scratch/main-src/binding-errors.lisp
- Command line
-
hf download hf://asemqaffaf/AsemLLM-runs/agent-scratch/main-src/binding-errors.lisp
-
curl -L -o binding-errors.lisp https://huggingface.co/asemqaffaf/AsemLLM-runs/resolve/main/agent-scratch/main-src/binding-errors.lisp
15.5 kB
| ;;;; Post-hoc analysis of saved predictions only. No model/evaluator/training imports. | |
| (defpackage :asem-binding-errors | |
| (:use :cl) | |
| (:import-from :asem-verify-saved #:g #:obj #:read-json) | |
| (:export #:analyze-bundle #:summarize #:association #:world-detail #:pair-detail)) | |
| (in-package :asem-binding-errors) | |
| (defun arr (xs) (coerce xs 'vector)) | |
| (defun xs (v) (coerce v 'list)) | |
| (defun jb (v) (if v :true :false)) | |
| (defun yes (o key) (eq (g o key) :true)) | |
| (defun tally (rows key &optional (value :true)) (count value rows :key (lambda (r) (g r key)) :test #'equalp)) | |
| (defun rate (k n) (if (plusp n) (/ k n 1d0) :null)) | |
| (defun selected (rows key value) (remove-if-not (lambda (r) (equal (g r key) value)) rows)) | |
| (defun data-string (value) (with-standard-io-syntax (write-to-string value :readably t))) | |
| (defun json-equivalent-p (a b) | |
| "Object member order is irrelevant; numeric round trips allow 1e-12 tolerance." | |
| (cond ((and (consp a) (eq (car a) :obj) (consp b) (eq (car b) :obj)) | |
| (and (= (length a) (length b)) | |
| (every (lambda (kv) (let ((other (assoc (car kv) (cdr b) :test #'equal))) | |
| (and other (json-equivalent-p (cdr kv) (cdr other))))) (cdr a)))) | |
| ((and (numberp a) (numberp b)) (<= (abs (- a b)) 1d-12)) | |
| ((or (stringp a) (stringp b)) (equal a b)) | |
| ((and (vectorp a) (vectorp b)) (and (= (length a) (length b)) (every #'json-equivalent-p a b))) | |
| (t (equal a b)))) | |
| (defun prefix-overlap (generated gold) | |
| (let ((n 0)) | |
| (loop for a across generated for b across gold while (= a b) do (incf n)) | |
| (values n (cond ((equalp generated gold) "correct") ((zerop (length generated)) "empty_prefix") | |
| ((zerop n) "first_token") ((= n (length generated)) "short_prefix") (t "later_token"))))) | |
| (defun world-detail (label pair family world other row) | |
| (let* ((key (g world "key")) (pred (g row "summed_option_prediction")) | |
| (mean (g row "mean_token_option_prediction")) (options (g world "options")) | |
| (generated (g row "generated_token_ids")) (tails (g world "option_token_ids")) | |
| (prefix-index (position generated tails :test #'equalp)) | |
| (prefix (equalp generated (aref tails key))) (rank (= pred key)) | |
| (overlap (multiple-value-list (prefix-overlap generated (aref tails key)))) | |
| (query (asem-verify-saved::safe-data (g world "query"))) | |
| (direction (string-downcase (symbol-name (first query)))) | |
| (opposite-index nil) (rank-role "not_applicable") (prefix-role "not_applicable")) | |
| (when (equal (g pair "category") "relation") | |
| ;; Derive direction from the graph/query, rather than naming every wrong answer a reversal. | |
| (let* ((facts (asem-verify-saved::safe-data (g world "facts"))) | |
| (opposite (if (eq (first query) :rel-subj) "rel-obj" "rel-subj")) | |
| (answer (asem-verify-saved::resolve-answer "relation" (getf facts :pairs) (list opposite (second query))))) | |
| (setf opposite-index (position answer options :test #'equal)) | |
| (unless (and opposite-index (= opposite-index (g other "key"))) (error "Opposite-role/other-world key mismatch")) | |
| (flet ((classify (choice) | |
| (cond ((null choice) "unlisted_prefix") ((= choice key) "correct") | |
| ((= choice opposite-index) "opposite_direction") | |
| ((equal (aref options choice) (second query)) "query_anchor") (t "other_entity")))) | |
| (setf rank-role (classify pred) prefix-role (classify prefix-index))))) | |
| (obj "label" label "id" (g world "id") "pair_id" (g pair "id") "family" (g pair "family") | |
| "category" (g pair "category") "predicate" (g family "predicate") "direction" direction | |
| "key" key "ranking_prediction" pred "mean_ranking_prediction" mean "prefix_option_index" (or prefix-index :null) | |
| "gold_text" (aref options key) "ranked_option_text" (aref options pred) | |
| "generated_prefix_text" (or (g row "generated_text") :null) | |
| "common_prefix_tokens" (first overlap) "prefix_error_stage" (second overlap) | |
| "gold_token_length" (length (aref tails key)) "generated_prefix_length" (length generated) | |
| "ranking_correct" (jb rank) "mean_ranking_correct" (jb (= mean key)) "prefix_correct" (jb prefix) | |
| "ranking_correct_prefix_wrong" (jb (and rank (not prefix))) | |
| "prefix_correct_ranking_wrong" (jb (and prefix (not rank))) | |
| "wrong_prefix_is_listed_option" (jb (and (not prefix) prefix-index)) | |
| "wrong_prefix_is_unlisted" (jb (and (not prefix) (null prefix-index))) | |
| "ranking_only_listed_error" (jb (and rank (not prefix) prefix-index)) | |
| "ranking_only_unlisted_error" (jb (and rank (not prefix) (null prefix-index))) | |
| "opposite_direction_key" (or opposite-index :null) "ranking_role" rank-role "prefix_role" prefix-role))) | |
| (defun state (a b) (cond ((and a b) "both_correct") (a "world0_only") (b "world1_only") (t "neither_correct"))) | |
| (defun pair-detail (label pair family a b) | |
| (let* ((r0 (yes a "ranking_correct")) (r1 (yes b "ranking_correct")) | |
| (p0 (yes a "prefix_correct")) (p1 (yes b "prefix_correct")) | |
| (changed (/= (g a "ranking_prediction") (g b "ranking_prediction"))) | |
| (mixed (not (eq r0 r1))) | |
| (transition (cond ((and r0 r1) "correct_switch") | |
| (mixed (if changed "one_correct_changed_to_wrong" "one_correct_unchanged_choice")) | |
| (changed "neither_correct_changed_choice") (t "neither_correct_unchanged_choice")))) | |
| (when (and r0 r1 (not changed)) (error "Fixed option cannot answer both changed keys")) | |
| (obj "label" label "id" (g pair "id") "category" (g pair "category") "family" (g pair "family") | |
| "predicate" (g family "predicate") "direction" (g a "direction") | |
| "ranking_state" (state r0 r1) "prefix_state" (state p0 p1) | |
| "rank_world0_correct" (jb r0) "rank_world1_correct" (jb r1) | |
| "prefix_world0_correct" (jb p0) "prefix_world1_correct" (jb p1) | |
| "ranking_changed" (jb changed) "transition" transition | |
| "both_opposite_direction" (jb (and (equal (g a "ranking_role") "opposite_direction") (equal (g b "ranking_role") "opposite_direction")))))) | |
| (defun association (pairs metric) | |
| (let* ((n (length pairs)) (field (format nil "~a_state" metric)) | |
| (both (tally pairs field "both_correct")) (a-only (tally pairs field "world0_only")) | |
| (b-only (tally pairs field "world1_only")) (neither (tally pairs field "neither_correct")) | |
| (a (+ both a-only)) (b (+ both b-only)) | |
| (pa (rate a n)) (pb (rate b n)) (joint (rate both n)) | |
| (product (if (plusp n) (* pa pb) :null)) | |
| (den (if (plusp n) (sqrt (* pa (- 1d0 pa) pb (- 1d0 pb))) 0d0))) | |
| (assert (= n (+ both a-only b-only neither))) | |
| (obj "pairs" n "both_correct" both "world0_only" a-only "world1_only" b-only "neither_correct" neither | |
| "world0_correct" a "world1_correct" b "world0_accuracy" pa "world1_accuracy" pb "joint_accuracy" joint | |
| "correct_world0_failed_world1" a-only "correct_world1_failed_world0" b-only | |
| "world1_correct_given_world0_correct" (rate both a) "world0_correct_given_world1_correct" (rate both b) | |
| "product_of_marginal_accuracies" product | |
| "joint_minus_product" (if (plusp n) (- joint product) :null) | |
| "phi" (if (plusp den) (/ (- joint product) den) :null)))) | |
| (defun partition (rows field values) | |
| (cons :obj (loop for v in values collect (cons v (tally rows field v))))) | |
| (defun summarize (worlds pairs) | |
| (let* ((n (length worlds)) (rank (tally worlds "ranking_correct")) (prefix (tally worlds "prefix_correct")) | |
| (rank-only (tally worlds "ranking_correct_prefix_wrong")) | |
| (prefix-only (tally worlds "prefix_correct_ranking_wrong")) | |
| (both (- rank rank-only)) (neither (- n both rank-only prefix-only)) | |
| (rel (selected worlds "category" "relation"))) | |
| (assert (= n (* 2 (length pairs)))) | |
| (assert (= prefix (+ both prefix-only))) | |
| (assert (= (- n prefix) (+ (tally worlds "wrong_prefix_is_listed_option") (tally worlds "wrong_prefix_is_unlisted")))) | |
| (obj "worlds" n "pairs" (length pairs) "ranking_correct" rank "prefix_correct" prefix | |
| "ranking_generation_contingency" (obj "both_correct" both "ranking_only" rank-only "prefix_only" prefix-only "neither_correct" neither) | |
| "prefix_correct_given_ranking_correct" (rate both rank) | |
| "ranking_correct_prefix_wrong_listed" (tally worlds "ranking_only_listed_error") | |
| "ranking_correct_prefix_wrong_unlisted" (tally worlds "ranking_only_unlisted_error") | |
| "wrong_prefix_listed" (tally worlds "wrong_prefix_is_listed_option") "wrong_prefix_unlisted" (tally worlds "wrong_prefix_is_unlisted") | |
| "ranking_only_prefix_error_stages" (partition (remove-if-not (lambda (w) (yes w "ranking_correct_prefix_wrong")) worlds) | |
| "prefix_error_stage" '("first_token" "later_token" "short_prefix" "empty_prefix")) | |
| "ranking_pair_outcomes" (association pairs "ranking") "prefix_pair_outcomes" (association pairs "prefix") | |
| "transitions" (partition pairs "transition" '("correct_switch" "one_correct_changed_to_wrong" "one_correct_unchanged_choice" "neither_correct_changed_choice" "neither_correct_unchanged_choice")) | |
| "relationship_ranking_roles" (partition rel "ranking_role" '("correct" "opposite_direction" "query_anchor" "other_entity")) | |
| "relationship_prefix_roles" (partition rel "prefix_role" '("correct" "opposite_direction" "query_anchor" "other_entity" "unlisted_prefix")) | |
| "both_opposite_direction_pairs" (tally pairs "both_opposite_direction")))) | |
| (defun group-summaries (worlds pairs field values) | |
| (cons :obj (loop for value in values collect | |
| (cons value (summarize (selected worlds field value) (selected pairs field value)))))) | |
| (defun analyze-bundle (bundle) | |
| (let* ((root (string-right-trim "/" bundle)) | |
| (verified (asem-verify-saved:verify-binding root)) | |
| (saved (read-json (format nil "~a/verification.json" root))) | |
| (pairs (xs (read-json (format nil "~a/pairs.json" root)))) | |
| (families (xs (read-json (format nil "~a/families.json" root)))) | |
| (all-worlds nil) (all-pairs nil) (models nil) (family-patterns nil)) | |
| ;; Frozen outputs and the previously saved verification must agree before post-hoc slicing. | |
| (unless (json-equivalent-p verified saved) (error "Frozen verification no longer reproduces")) | |
| (dolist (label '("r7" "control" "treatment")) | |
| (let* ((result (read-json (format nil "~a/~a.json" root label))) | |
| (rows (xs (g (g (g result "conditions") "real") "items"))) | |
| (index (make-hash-table :test 'equal)) (worlds nil) (ps nil)) | |
| (dolist (r rows) (when (gethash (g r "id") index) (error "Duplicate row ID")) (setf (gethash (g r "id") index) r)) | |
| (dolist (p pairs) | |
| (let* ((f (or (find (g p "family") families :key (lambda (x) (g x "id")) :test #'equal) (error "Missing family"))) | |
| (ws (xs (g p "worlds"))) | |
| (a (world-detail label p f (first ws) (second ws) (gethash (g (first ws) "id") index))) | |
| (b (world-detail label p f (second ws) (first ws) (gethash (g (second ws) "id") index)))) | |
| (push a worlds) (push b worlds) (push (pair-detail label p f a b) ps))) | |
| (setf worlds (nreverse worlds) ps (nreverse ps)) | |
| (let* ((summary (summarize worlds ps)) | |
| (categories (group-summaries worlds ps "category" '("relation" "extract" "reverse" "compare" "temporal"))) | |
| (reference (g (g saved "summaries") label))) | |
| ;; Independent decompositions must add back to the frozen verifier's original totals. | |
| (dolist (category '("relation" "extract" "reverse" "compare" "temporal")) | |
| (let ((c (g categories category)) (old (g (g reference "by_category") category))) | |
| (assert (= (g (g c "ranking_pair_outcomes") "both_correct") (g old "joint_sum_correct"))) | |
| (assert (= (g (g c "prefix_pair_outcomes") "both_correct") (g old "joint_prefix_correct"))) | |
| (when (equal category "relation") (assert (= (g (g c "relationship_ranking_roles") "opposite_direction") (g old "wrong_role_choices")))))) | |
| (push (cons label (obj "overall" summary "by_category" categories | |
| "relationship_by_direction" (group-summaries (selected worlds "category" "relation") (selected ps "category" "relation") "direction" '("rel-subj" "rel-obj")) | |
| "relationship_by_predicate" (group-summaries (selected worlds "category" "relation") (selected ps "category" "relation") "predicate" '("teach" "hire" "call" "thank" "visit" "help")) | |
| "by_family" (group-summaries worlds ps "family" (mapcar (lambda (f) (g f "id")) families)))) models)) | |
| (setf all-worlds (nconc all-worlds worlds) all-pairs (nconc all-pairs ps)))) | |
| (dolist (f families) | |
| (let* ((id (g f "id")) | |
| (ss (mapcar (lambda (entry) (cons (car entry) (g (g (cdr entry) "by_family") id))) models)) | |
| (zero-rank (every (lambda (s) (zerop (g (g (cdr s) "ranking_pair_outcomes") "both_correct"))) ss)) | |
| (zero-prefix (every (lambda (s) (zerop (g (g (cdr s) "prefix_pair_outcomes") "both_correct"))) ss))) | |
| (push (obj "family" id "category" (g f "category") "predicate" (g f "predicate") | |
| "passage_frames" (g f "passage_frames") "question_frames" (g f "question_frames") | |
| "zero_joint_ranking_all_models" (jb zero-rank) "zero_joint_prefix_all_models" (jb zero-prefix)) family-patterns))) | |
| (obj "schema" "asem-binding-errors-1" "analysis" "post-hoc descriptive; no new inference or training" | |
| "prediction_count" (length all-worlds) "pair_checkpoint_count" (length all-pairs) | |
| "reference" "r7" "promotion" "none" "locked_test_accessed" :false "training_updates" 0 | |
| "floor_interpretation" (obj "observed_control_joint_prefix_accuracy" .055d0 "lowest_empirical_treatment_minus_control" -.055d0 | |
| "registered_deficit" -.10d0 "informative_noninferiority_gate" :false | |
| "note" "The frozen historical classifier is preserved. Excluding a 10-point loss at a 5.5% observed Control score provides no meaningful reassurance.") | |
| "association_note" "Marginal-product and phi values are descriptive associations, not chance baselines or causal tests. Conditioning on a correct world selects examples; world labels are counterbalanced." | |
| "generation_note" "Ranking-correct/prefix-wrong is a behavioral dissociation; an unlisted prefix alone does not identify copying, spelling, decoding, or comprehension as its cause." | |
| "models" (cons :obj (nreverse models)) "family_patterns" (arr (nreverse family-patterns)) | |
| "worlds" (arr all-worlds) "pair_outcomes" (arr all-pairs) | |
| "input_sha256" (cons :obj (loop for name in '("manifest.json" "families.json" "pairs.json" "r7.json" "control.json" "treatment.json" "verification.json") | |
| collect (cons name (asem-sha256:sha256-file-hex (format nil "~a/~a" root name))))) | |
| "analysis_source_sha256" (cons :obj (loop for path in '("lisp/src/binding-errors.lisp" "lisp/bin/binding-errors.lisp") collect (cons path (asem-sha256:sha256-file-hex path))))))) | |