Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/lev/tensors.clj
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,17 @@
(recur (inc i)))))))

(defn first-mismatch
"[index byte-in-a byte-in-b] at the first byte two uint8 'tensors' differ
in, or :none when they agree."
[a b]
(loop [i 0]
(if (or (= i (:size a)) (= (ffi/read (:p a) :uint8 i) (ffi/read (:p b) :uint8 i)))
(if (= i (:size a)) :none [i (ffi/read (:p a) :uint8 i) (ffi/read (:p b) :uint8 i)])
[i (ffi/read (:p a) :uint8 i) (ffi/read (:p b) :uint8 i)])))
(if (= i (:size a))
:none
(let [x (ffi/read (:p a) :uint8 i)
y (ffi/read (:p b) :uint8 i)]
(if (= x y)
(recur (inc i))
[i x y])))))

(defn get
[p i]
Expand Down
7 changes: 7 additions & 0 deletions test/lev/tensors_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@
(is (t/eq-bytes? gold got)
(str "first mismatch " (t/first-mismatch gold got))))))))

(deftest first-mismatch-finds-the-first-differing-byte
(let [a (t/from-bytes [1 0 1 1] [4])]
(is (= :none (t/first-mismatch a (t/from-bytes [1 0 1 1] [4]))))
(is (= [0 1 0] (t/first-mismatch a (t/from-bytes [0 0 1 1] [4]))))
(is (= [2 1 0] (t/first-mismatch a (t/from-bytes [1 0 0 1] [4]))))
(is (= [3 1 0] (t/first-mismatch a (t/from-bytes [1 0 1 0] [4]))))))

;; --- embeddings: row gather + weight-only LayerNorm -------------------------

(deftest embeddings-match-torch
Expand Down
Loading