cp500 commited on
Commit
060b8ea
·
verified ·
1 Parent(s): b0225a3

Upload js/src/pairs.ts with huggingface_hub

Browse files
Files changed (1) hide show
  1. js/src/pairs.ts +145 -0
js/src/pairs.ts ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * Pair-index builder + per-mention argmax + cluster grouping.
3
+ *
4
+ * Pure JS, no ORT, no DOM. Mirrors the Python helpers in
5
+ * ``infon/scripts/coref_onnx_experiment.py`` (``build_pairs`` /
6
+ * ``split_pairs_by_mention``) so the JS pipeline produces
7
+ * bit-identical pair tensors.
8
+ */
9
+
10
+ /**
11
+ * Enumerate ``(i, j)`` candidate pairs for ``M`` mentions.
12
+ *
13
+ * For mention ``m`` (1-indexed because index 0 is DUMMY) we emit
14
+ * ``(m, 0), (m, 1), …, (m, m-1)`` — DUMMY first, then every earlier
15
+ * mention. This is the same triangular shape the Python
16
+ * ``build_pairs`` returns; the scorer ONNX expects this exact layout
17
+ * because the in-graph ``index_select`` over the prepended DUMMY
18
+ * relies on j=0 meaning "no antecedent."
19
+ *
20
+ * @param nMentions number of mentions in the doc
21
+ * @returns ``[pairI, pairJ]`` BigInt64 typed arrays of equal length.
22
+ * Lengths: ``M*(M+1)/2``.
23
+ */
24
+ export function buildPairs(nMentions: number): [BigInt64Array, BigInt64Array] {
25
+ const pi: bigint[] = [];
26
+ const pj: bigint[] = [];
27
+ for (let m = 1; m <= nMentions; m++) {
28
+ pi.push(BigInt(m));
29
+ pj.push(0n);
30
+ for (let j = 1; j < m; j++) {
31
+ pi.push(BigInt(m));
32
+ pj.push(BigInt(j));
33
+ }
34
+ }
35
+ return [BigInt64Array.from(pi), BigInt64Array.from(pj)];
36
+ }
37
+
38
+ /**
39
+ * Group flat pair scores back into per-mention argmax decisions.
40
+ * Mirrors ``split_pairs_by_mention`` in the Python harness.
41
+ *
42
+ * @returns ``decisions[i]`` = the mention index (1-based) chosen as
43
+ * mention i's antecedent, or ``0`` for DUMMY (no antecedent).
44
+ * Translate to 0-based mention indices with ``decisions[i] - 1``.
45
+ */
46
+ export function pickAntecedents(
47
+ nMentions: number,
48
+ pairI: BigInt64Array,
49
+ pairJ: BigInt64Array,
50
+ scores: Float32Array,
51
+ ): { antecedent: number; score: number }[] {
52
+ const out: { antecedent: number; score: number }[] = [];
53
+ for (let m = 1; m <= nMentions; m++) {
54
+ let bestIdx = -1;
55
+ let bestScore = -Infinity;
56
+ for (let k = 0; k < pairI.length; k++) {
57
+ if (Number(pairI[k]) !== m) continue;
58
+ const s = scores[k];
59
+ if (s > bestScore) {
60
+ bestScore = s;
61
+ bestIdx = k;
62
+ }
63
+ }
64
+ out.push({
65
+ antecedent: bestIdx >= 0 ? Number(pairJ[bestIdx]) : 0,
66
+ score: bestScore,
67
+ });
68
+ }
69
+ return out;
70
+ }
71
+
72
+ /**
73
+ * Group antecedent decisions into clusters using union-find.
74
+ *
75
+ * Each mention either points to DUMMY (starts its own cluster) or to
76
+ * an earlier mention (joins that mention's cluster). Cluster IDs are
77
+ * dense 0-based; singletons are not assigned a cluster (returned as
78
+ * ``-1``) so callers can render them differently.
79
+ *
80
+ * @param decisions ``decisions[i].antecedent`` is the *1-based*
81
+ * mention index this mention links to, or ``0`` for
82
+ * DUMMY. (Same convention as the model output.)
83
+ * @returns
84
+ * - ``cluster[i]`` — cluster id for mention i, or -1 if singleton
85
+ * - ``clusters`` — list of multi-mention clusters, each a list of
86
+ * mention indices in document order
87
+ */
88
+ export function groupClusters(
89
+ decisions: { antecedent: number }[],
90
+ ): { cluster: number[]; clusters: number[][] } {
91
+ const n = decisions.length;
92
+ // Union-find. parent[i] points to a smaller-or-equal mention index.
93
+ const parent = Array.from({ length: n }, (_, i) => i);
94
+ const find = (x: number): number => {
95
+ while (parent[x] !== x) {
96
+ parent[x] = parent[parent[x]]; // path compression
97
+ x = parent[x];
98
+ }
99
+ return x;
100
+ };
101
+ const union = (a: number, b: number) => {
102
+ const ra = find(a);
103
+ const rb = find(b);
104
+ if (ra !== rb) {
105
+ // Always attach the higher-index root under the lower-index root
106
+ // so cluster representatives are first-mention.
107
+ if (ra < rb) parent[rb] = ra;
108
+ else parent[ra] = rb;
109
+ }
110
+ };
111
+
112
+ for (let i = 0; i < n; i++) {
113
+ const ant = decisions[i].antecedent;
114
+ if (ant > 0) {
115
+ // ant is 1-based; the mention it points to is ant - 1.
116
+ union(i, ant - 1);
117
+ }
118
+ }
119
+
120
+ // Bucket by root.
121
+ const roots: number[][] = [];
122
+ const rootIdx = new Map<number, number>();
123
+ for (let i = 0; i < n; i++) {
124
+ const r = find(i);
125
+ let idx = rootIdx.get(r);
126
+ if (idx === undefined) {
127
+ idx = roots.length;
128
+ roots.push([]);
129
+ rootIdx.set(r, idx);
130
+ }
131
+ roots[idx].push(i);
132
+ }
133
+
134
+ // Collapse: only multi-mention clusters get a stable id; singletons
135
+ // get -1.
136
+ const cluster = new Array<number>(n).fill(-1);
137
+ const clusters: number[][] = [];
138
+ for (const group of roots) {
139
+ if (group.length < 2) continue;
140
+ const cid = clusters.length;
141
+ clusters.push(group);
142
+ for (const m of group) cluster[m] = cid;
143
+ }
144
+ return { cluster, clusters };
145
+ }