@driftengine/capture 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (199) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +29 -0
  3. package/README.md +68 -0
  4. package/dist/browserFrames.d.ts +31 -0
  5. package/dist/browserFrames.js +105 -0
  6. package/dist/collide.d.ts +59 -0
  7. package/dist/collide.js +92 -0
  8. package/dist/decimate.d.ts +42 -0
  9. package/dist/decimate.js +439 -0
  10. package/dist/delight.d.ts +42 -0
  11. package/dist/delight.js +351 -0
  12. package/dist/delightScene.d.ts +37 -0
  13. package/dist/delightScene.js +183 -0
  14. package/dist/depth.d.ts +44 -0
  15. package/dist/depth.js +53 -0
  16. package/dist/features.d.ts +33 -0
  17. package/dist/features.js +260 -0
  18. package/dist/frames.d.ts +47 -0
  19. package/dist/frames.js +87 -0
  20. package/dist/fusion.d.ts +77 -0
  21. package/dist/fusion.js +152 -0
  22. package/dist/gaussians/adam.d.ts +57 -0
  23. package/dist/gaussians/adam.js +88 -0
  24. package/dist/gaussians/depth.d.ts +8 -0
  25. package/dist/gaussians/depth.js +73 -0
  26. package/dist/gaussians/gradients.d.ts +36 -0
  27. package/dist/gaussians/gradients.js +228 -0
  28. package/dist/gaussians/harmonics.d.ts +46 -0
  29. package/dist/gaussians/harmonics.js +99 -0
  30. package/dist/gaussians/optimise.d.ts +50 -0
  31. package/dist/gaussians/optimise.js +125 -0
  32. package/dist/gaussians/parameters.d.ts +35 -0
  33. package/dist/gaussians/parameters.js +165 -0
  34. package/dist/gaussians/project.d.ts +72 -0
  35. package/dist/gaussians/project.js +137 -0
  36. package/dist/gaussians/projectGradient.d.ts +15 -0
  37. package/dist/gaussians/projectGradient.js +213 -0
  38. package/dist/gaussians/rasterise.d.ts +13 -0
  39. package/dist/gaussians/rasterise.js +65 -0
  40. package/dist/gaussians/refine.d.ts +36 -0
  41. package/dist/gaussians/refine.js +136 -0
  42. package/dist/gaussians/similarity.d.ts +25 -0
  43. package/dist/gaussians/similarity.js +80 -0
  44. package/dist/index.d.ts +101 -0
  45. package/dist/index.js +96 -0
  46. package/dist/marching.d.ts +38 -0
  47. package/dist/marching.js +258 -0
  48. package/dist/masks.d.ts +59 -0
  49. package/dist/masks.js +165 -0
  50. package/dist/math/dense.d.ts +68 -0
  51. package/dist/math/dense.js +294 -0
  52. package/dist/math/schur.d.ts +23 -0
  53. package/dist/math/schur.js +140 -0
  54. package/dist/models/batchNorm.d.ts +17 -0
  55. package/dist/models/batchNorm.js +24 -0
  56. package/dist/models/clipTokenizer.d.ts +40 -0
  57. package/dist/models/clipTokenizer.js +122 -0
  58. package/dist/models/clipTower.d.ts +27 -0
  59. package/dist/models/clipTower.js +45 -0
  60. package/dist/models/depthAnything2.d.ts +38 -0
  61. package/dist/models/depthAnything2.js +163 -0
  62. package/dist/models/depthAnything3.d.ts +26 -0
  63. package/dist/models/depthAnything3.js +171 -0
  64. package/dist/models/dualDpt.d.ts +19 -0
  65. package/dist/models/dualDpt.js +138 -0
  66. package/dist/models/hiera.d.ts +49 -0
  67. package/dist/models/hiera.js +120 -0
  68. package/dist/models/hieraAttention.d.ts +16 -0
  69. package/dist/models/hieraAttention.js +68 -0
  70. package/dist/models/miniature.d.ts +36 -0
  71. package/dist/models/miniature.js +216 -0
  72. package/dist/models/mobileSam.d.ts +41 -0
  73. package/dist/models/mobileSam.js +88 -0
  74. package/dist/models/owlv2.d.ts +49 -0
  75. package/dist/models/owlv2.js +131 -0
  76. package/dist/models/owlv2Detect.d.ts +25 -0
  77. package/dist/models/owlv2Detect.js +80 -0
  78. package/dist/models/owlv2Miniature.d.ts +27 -0
  79. package/dist/models/owlv2Miniature.js +87 -0
  80. package/dist/models/rope.d.ts +23 -0
  81. package/dist/models/rope.js +84 -0
  82. package/dist/models/sam21.d.ts +45 -0
  83. package/dist/models/sam21.js +126 -0
  84. package/dist/models/sam21Miniature.d.ts +37 -0
  85. package/dist/models/sam21Miniature.js +194 -0
  86. package/dist/models/sam21Video.d.ts +30 -0
  87. package/dist/models/sam21Video.js +101 -0
  88. package/dist/models/sam2Choice.d.ts +19 -0
  89. package/dist/models/sam2Choice.js +55 -0
  90. package/dist/models/sam2Decoder.d.ts +36 -0
  91. package/dist/models/sam2Decoder.js +108 -0
  92. package/dist/models/sam2Memories.d.ts +54 -0
  93. package/dist/models/sam2Memories.js +93 -0
  94. package/dist/models/sam2Memory.d.ts +30 -0
  95. package/dist/models/sam2Memory.js +136 -0
  96. package/dist/models/sam2Positions.d.ts +24 -0
  97. package/dist/models/sam2Positions.js +94 -0
  98. package/dist/models/sam2Tracker.d.ts +69 -0
  99. package/dist/models/sam2Tracker.js +172 -0
  100. package/dist/models/samDecoder.d.ts +45 -0
  101. package/dist/models/samDecoder.js +96 -0
  102. package/dist/models/samMiniature.d.ts +43 -0
  103. package/dist/models/samMiniature.js +161 -0
  104. package/dist/models/samPrompt.d.ts +57 -0
  105. package/dist/models/samPrompt.js +160 -0
  106. package/dist/models/seeded.d.ts +16 -0
  107. package/dist/models/seeded.js +48 -0
  108. package/dist/models/tinyVit.d.ts +38 -0
  109. package/dist/models/tinyVit.js +82 -0
  110. package/dist/models/twoWay.d.ts +46 -0
  111. package/dist/models/twoWay.js +67 -0
  112. package/dist/models/vit.d.ts +45 -0
  113. package/dist/models/vit.js +157 -0
  114. package/dist/models/windowAttention.d.ts +25 -0
  115. package/dist/models/windowAttention.js +104 -0
  116. package/dist/poses.d.ts +31 -0
  117. package/dist/poses.js +750 -0
  118. package/dist/prepare.d.ts +30 -0
  119. package/dist/prepare.js +238 -0
  120. package/dist/propose.d.ts +36 -0
  121. package/dist/propose.js +63 -0
  122. package/dist/resample.d.ts +39 -0
  123. package/dist/resample.js +165 -0
  124. package/dist/run.d.ts +3 -0
  125. package/dist/run.js +1 -0
  126. package/dist/scene.d.ts +104 -0
  127. package/dist/scene.js +109 -0
  128. package/dist/segment.d.ts +40 -0
  129. package/dist/segment.js +184 -0
  130. package/dist/testScene.d.ts +55 -0
  131. package/dist/testScene.js +214 -0
  132. package/dist/twoView.d.ts +22 -0
  133. package/dist/twoView.js +435 -0
  134. package/package.json +63 -0
  135. package/src/browserFrames.ts +145 -0
  136. package/src/collide.ts +128 -0
  137. package/src/decimate.ts +510 -0
  138. package/src/delight.ts +427 -0
  139. package/src/delightScene.ts +249 -0
  140. package/src/depth.ts +100 -0
  141. package/src/features.ts +302 -0
  142. package/src/frames.ts +111 -0
  143. package/src/fusion.ts +229 -0
  144. package/src/gaussians/adam.ts +131 -0
  145. package/src/gaussians/depth.ts +87 -0
  146. package/src/gaussians/gradients.ts +285 -0
  147. package/src/gaussians/harmonics.ts +122 -0
  148. package/src/gaussians/optimise.ts +191 -0
  149. package/src/gaussians/parameters.ts +186 -0
  150. package/src/gaussians/project.ts +207 -0
  151. package/src/gaussians/projectGradient.ts +234 -0
  152. package/src/gaussians/rasterise.ts +81 -0
  153. package/src/gaussians/refine.ts +176 -0
  154. package/src/gaussians/similarity.ts +87 -0
  155. package/src/index.ts +225 -0
  156. package/src/marching.ts +358 -0
  157. package/src/masks.ts +201 -0
  158. package/src/math/dense.ts +317 -0
  159. package/src/math/schur.ts +167 -0
  160. package/src/models/batchNorm.ts +57 -0
  161. package/src/models/clipTokenizer.ts +129 -0
  162. package/src/models/clipTower.ts +87 -0
  163. package/src/models/depthAnything2.ts +276 -0
  164. package/src/models/depthAnything3.ts +229 -0
  165. package/src/models/dualDpt.ts +233 -0
  166. package/src/models/hiera.ts +231 -0
  167. package/src/models/hieraAttention.ts +121 -0
  168. package/src/models/miniature.ts +241 -0
  169. package/src/models/mobileSam.ts +135 -0
  170. package/src/models/owlv2.ts +190 -0
  171. package/src/models/owlv2Detect.ts +116 -0
  172. package/src/models/owlv2Miniature.ts +108 -0
  173. package/src/models/rope.ts +111 -0
  174. package/src/models/sam21.ts +182 -0
  175. package/src/models/sam21Miniature.ts +225 -0
  176. package/src/models/sam21Video.ts +154 -0
  177. package/src/models/sam2Choice.ts +62 -0
  178. package/src/models/sam2Decoder.ts +218 -0
  179. package/src/models/sam2Memories.ts +165 -0
  180. package/src/models/sam2Memory.ts +247 -0
  181. package/src/models/sam2Positions.ts +106 -0
  182. package/src/models/sam2Tracker.ts +300 -0
  183. package/src/models/samDecoder.ts +225 -0
  184. package/src/models/samMiniature.ts +190 -0
  185. package/src/models/samPrompt.ts +227 -0
  186. package/src/models/seeded.ts +50 -0
  187. package/src/models/tinyVit.ts +178 -0
  188. package/src/models/twoWay.ts +122 -0
  189. package/src/models/vit.ts +265 -0
  190. package/src/models/windowAttention.ts +155 -0
  191. package/src/poses.ts +894 -0
  192. package/src/prepare.ts +306 -0
  193. package/src/propose.ts +96 -0
  194. package/src/resample.ts +197 -0
  195. package/src/run.ts +7 -0
  196. package/src/scene.ts +141 -0
  197. package/src/segment.ts +220 -0
  198. package/src/testScene.ts +283 -0
  199. package/src/twoView.ts +536 -0
@@ -0,0 +1,155 @@
1
+ /**
2
+ * Attention within the windows of a grid, as TinyViT attends: each window a batch of tokens, each
3
+ * head's scores given a learned bias by the two tokens' distance.
4
+ *
5
+ * **Windows are partitioned as the upstream partitions them**: a grid the window does not divide is
6
+ * padded with zeros after its last row and column *before* the attention's own norm, so a padded
7
+ * token is the norm's β and is attended to as a key like any other — and the padding is sliced away
8
+ * afterwards. A grid exactly one window across is attended whole.
9
+ *
10
+ * **The bias table is laid out once, at conversion**, `[heads, n, n]` from the learned vector of
11
+ * `side²` distances, numbered in the order the upstream first meets them. What it gives up: the
12
+ * table costs MobileSAM's file 2.4 MB at half precision, where a gather on the device would keep the
13
+ * 3,000 learned values instead.
14
+ *
15
+ * **Queries, keys and values come out of one projection per head**, `[q k v]` for each head in turn
16
+ * where the attention operator takes all queries, then all keys, then all values; the projection's
17
+ * rows are reordered at conversion, which is where a head could otherwise be read as the wrong one's
18
+ * keys. The norm takes PyTorch's default ε, 1e-5.
19
+ */
20
+ import type { GraphBuilder, Weights } from '@driftengine/texture';
21
+
22
+ const EPSILON = 1e-5;
23
+
24
+ /**
25
+ * `[rows · cols, dim]` from `x` of that shape: the attention under `p` — `norm.`, `qkv.`, `proj.` and
26
+ * `attention_biases`, the upstream's names — within windows `side` tokens square.
27
+ */
28
+ export function windowAttention(
29
+ weights: Weights,
30
+ graph: GraphBuilder,
31
+ p: string,
32
+ x: string,
33
+ dim: number,
34
+ heads: number,
35
+ side: number,
36
+ rows: number,
37
+ cols: number,
38
+ ): string {
39
+ const node = graph.node.bind(graph);
40
+ const norm = (value: string, q: string): string =>
41
+ node('layerNorm', [value, weights.read(`${q}weight`, [dim]), weights.read(`${q}bias`, [dim])], {
42
+ epsilon: EPSILON,
43
+ });
44
+ const linear = (value: string, q: string, out: number, inputs: number): string =>
45
+ node('linear', [
46
+ value,
47
+ weights.read(`${q}weight`, [out, inputs]),
48
+ weights.read(`${q}bias`, [out]),
49
+ ]);
50
+ const n = side * side;
51
+ const whole = rows === side && cols === side;
52
+ const padRows = (side - (rows % side)) % side;
53
+ const padCols = (side - (cols % side)) % side;
54
+ const across = (rows + padRows) / side;
55
+ const down = (cols + padCols) / side;
56
+ const count = whole ? 1 : across * down;
57
+ let tokens = x;
58
+ if (!whole) {
59
+ let grid = node('reshape', [x], { shape: [rows, cols, dim] });
60
+ if (padRows > 0 || padCols > 0) grid = node('pad', [grid], { after: [padRows, padCols, 0] });
61
+ const parts = node('reshape', [grid], { shape: [across, side, down, side, dim] });
62
+ tokens = node('reshape', [node('permute', [parts], { order: [0, 2, 1, 3, 4] })], {
63
+ shape: [count * n, dim],
64
+ });
65
+ }
66
+ const qkv = node('linear', [
67
+ norm(tokens, `${p}norm.`),
68
+ splitHeads(weights, `${p}qkv.weight`, [3 * dim, dim], heads),
69
+ splitHeads(weights, `${p}qkv.bias`, [3 * dim], heads),
70
+ ]);
71
+ const part = (i: number): string =>
72
+ node('reshape', [node('slice', [qkv], { axis: 1, start: i * dim, end: (i + 1) * dim })], {
73
+ shape: [count, n, dim],
74
+ });
75
+ const attended = node(
76
+ 'attention',
77
+ [part(0), part(1), part(2), biasTable(weights, p, heads, side)],
78
+ { heads },
79
+ );
80
+ const projected = linear(
81
+ node('reshape', [attended], { shape: [count * n, dim] }),
82
+ `${p}proj.`,
83
+ dim,
84
+ dim,
85
+ );
86
+ if (whole) return projected;
87
+ const back = node(
88
+ 'permute',
89
+ [node('reshape', [projected], { shape: [across, down, side, side, dim] })],
90
+ {
91
+ order: [0, 2, 1, 3, 4],
92
+ },
93
+ );
94
+ let grid = node('reshape', [back], { shape: [rows + padRows, cols + padCols, dim] });
95
+ if (padRows > 0) grid = node('slice', [grid], { axis: 0, start: 0, end: rows });
96
+ if (padCols > 0) grid = node('slice', [grid], { axis: 1, start: 0, end: cols });
97
+ return node('reshape', [grid], { shape: [rows * cols, dim] });
98
+ }
99
+
100
+ /**
101
+ * The projection `name` with its rows in the attention operator's order — every head's queries,
102
+ * then keys, then values — from the upstream's, where each head's three sit together.
103
+ */
104
+ function splitHeads(
105
+ weights: Weights,
106
+ name: string,
107
+ shape: readonly number[],
108
+ heads: number,
109
+ ): string {
110
+ const rows = shape[0] as number;
111
+ const width = rows / 3 / heads;
112
+ const columns = shape.length === 2 ? (shape[1] as number) : 1;
113
+ return weights.derive(name.replace(/\.(weight|bias)$/, '.split.$1'), shape, (values) => {
114
+ const from = values(name, shape);
115
+ const out = new Float32Array(from.length);
116
+ for (let head = 0; head < heads; head += 1) {
117
+ for (let part = 0; part < 3; part += 1) {
118
+ for (let j = 0; j < width; j += 1) {
119
+ const source = (head * 3 + part) * width + j;
120
+ const target = part * heads * width + head * width + j;
121
+ out.set(from.subarray(source * columns, (source + 1) * columns), target * columns);
122
+ }
123
+ }
124
+ }
125
+ return out;
126
+ });
127
+ }
128
+
129
+ /**
130
+ * `[heads, n, n]`: each pair of a window's tokens given its head's learned bias for their distance
131
+ * in rows and in columns, the distances numbered in the order the upstream first meets them.
132
+ */
133
+ function biasTable(weights: Weights, p: string, heads: number, side: number): string {
134
+ const n = side * side;
135
+ return weights.derive(`${p}attention_bias_table`, [heads, n, n], (values) => {
136
+ const learned = values(`${p}attention_biases`, [heads, n]);
137
+ const numbered = new Map<number, number>();
138
+ const index = new Int32Array(n * n);
139
+ for (let a = 0; a < n; a += 1) {
140
+ for (let b = 0; b < n; b += 1) {
141
+ const offset =
142
+ Math.abs(Math.floor(a / side) - Math.floor(b / side)) * side +
143
+ Math.abs((a % side) - (b % side));
144
+ if (!numbered.has(offset)) numbered.set(offset, numbered.size);
145
+ index[a * n + b] = numbered.get(offset) as number;
146
+ }
147
+ }
148
+ const out = new Float32Array(heads * n * n);
149
+ for (let head = 0; head < heads; head += 1) {
150
+ for (let i = 0; i < n * n; i += 1)
151
+ out[head * n * n + i] = learned[head * n + (index[i] as number)] as number;
152
+ }
153
+ return out;
154
+ });
155
+ }