cp500 commited on
Commit
a86f252
·
verified ·
1 Parent(s): d5f9738

Upload js/test/bio.test.ts with huggingface_hub

Browse files
Files changed (1) hide show
  1. js/test/bio.test.ts +51 -0
js/test/bio.test.ts ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { test } from 'node:test';
2
+ import { strict as assert } from 'node:assert';
3
+ import { decodeBio, BIO_O, BIO_B, BIO_I } from '../src/bio.js';
4
+
5
+ /** Build flat (T, 3) logits from a list of class indices. Each row
6
+ * gets a +5 bump for the chosen class so argmax is unambiguous. */
7
+ function logitsFromLabels(labels: number[]): Float32Array {
8
+ const out = new Float32Array(labels.length * 3);
9
+ for (let t = 0; t < labels.length; t++) {
10
+ out[t * 3 + labels[t]] = 5;
11
+ }
12
+ return out;
13
+ }
14
+
15
+ test('decodeBio: B I I O → one span', () => {
16
+ const logits = logitsFromLabels([BIO_B, BIO_I, BIO_I, BIO_O]);
17
+ assert.deepEqual(decodeBio(logits), [[0, 2]]);
18
+ });
19
+
20
+ test('decodeBio: orphan I is dropped', () => {
21
+ // I at the start without a preceding B should be silently skipped
22
+ // (matches Python valid_only=True).
23
+ const logits = logitsFromLabels([BIO_I, BIO_I, BIO_O, BIO_B, BIO_I]);
24
+ assert.deepEqual(decodeBio(logits), [[3, 4]]);
25
+ });
26
+
27
+ test('decodeBio: attention mask zeroes positions to O', () => {
28
+ const logits = logitsFromLabels([BIO_B, BIO_I, BIO_I, BIO_B]);
29
+ const attn = BigInt64Array.from([1n, 1n, 0n, 0n]);
30
+ // Positions 2,3 are masked → contribute O. So we get B I → span [0,1].
31
+ assert.deepEqual(decodeBio(logits, attn), [[0, 1]]);
32
+ });
33
+
34
+ test('decodeBio: threshold suppresses low-confidence spans', () => {
35
+ // Build a row where B has prob ~0.4 (below default 0.5).
36
+ const logits = new Float32Array(3 * 3);
37
+ // row 0: O=0.1, B=0.4 (weakest), I=0.5 (max but I-without-B is invalid)
38
+ logits[0] = -1; logits[1] = 0.0; logits[2] = 0.4;
39
+ // row 1: O=2, B=-1, I=-1 → O
40
+ logits[3] = 2; logits[4] = -1; logits[5] = -1;
41
+ // row 2: O=0, B=2, I=0 → B (high confidence)
42
+ logits[6] = 0; logits[7] = 2; logits[8] = 0;
43
+ const spans = decodeBio(logits, undefined, 0.7);
44
+ // The high-confidence B at position 2 should be the only B; its
45
+ // span runs to end-of-sequence (just position 2).
46
+ assert.deepEqual(spans, [[2, 2]]);
47
+ });
48
+
49
+ test('decodeBio: empty input', () => {
50
+ assert.deepEqual(decodeBio(new Float32Array(0)), []);
51
+ });