Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

diag: cluster up to 300k points, drop the smallest-cluster readout

Lincoln Stein9b458ae7f4eb
diff --git a/invokeai/app/api/routers/image_map.py b/invokeai/app/api/routers/image_map.py
index aac7ad2d4e..e72004b1e7 100644
--- a/invokeai/app/api/routers/image_map.py
+++ b/invokeai/app/api/routers/image_map.py
@@ -169,12 +169,16 @@ def _release_refresh_slot(user_id: str) -> None:
_refresh_claims.pop(user_id, None)
-# /points is polled, and between polls its inputs almost never change, so the
-# clustering it repeats is usually identical work. Caching the labels turns the
-# steady state into a dict lookup; entries are int64 label arrays, bounded at
-# ~400KB each by the 50k-point clustering cap (only a clustering that actually
-# ran is stored, so the all-noise array a skipped clustering returns — free to
-# recompute and unbounded in size — never lands here).
+# /points is refreshed on every gallery change, and between refreshes its
+# inputs almost never change, so the clustering it repeats is usually
+# identical work. Caching the labels turns the steady state into a dict
+# lookup; entries are int64 label arrays, bounded at 8 bytes per point by the
+# clustering cap (only a clustering that actually ran is stored, so the
+# all-noise array a skipped clustering returns — free to recompute and
+# unbounded in size — never lands here). At the current 300k cap that is
+# 2.4MB per entry and ~77MB across a full pool, up from ~400KB and ~13MB when
+# the cap was 50k; if the cap stays this high, the pool size below wants
+# revisiting.
#
# ONE entry per user, rather than a shared pool of N. A shared pool made the
# cache worse than none: with more concurrent map users than slots, strict LRU
diff --git a/invokeai/app/services/image_index/projection.py b/invokeai/app/services/image_index/projection.py
index d45f074f22..44e5f5e4ba 100644
--- a/invokeai/app/services/image_index/projection.py
+++ b/invokeai/app/services/image_index/projection.py
@@ -84,7 +84,14 @@ def compute_umap(embeddings: np.ndarray, seed: int = DEFAULT_UMAP_SEED) -> np.nd
# Above this many points, per-request DBSCAN is skipped (everything is served
# as unclustered) — sklearn DBSCAN materializes all radius neighborhoods, and
# the memory cost grows with the pair count.
-MAX_CLUSTERED_POINTS = 50_000
+#
+# Raised from 50k to cover a reported 170k-item gallery, which the old value
+# left entirely unclustered with nothing in the response saying so. The
+# neighbor-pair budget below, not this number, is what actually bounds
+# DBSCAN's memory; this bounds the per-request CPU and the size of the label
+# arrays the endpoint caches. Both grow with it — see the cluster cache in
+# the image_map router — so it is set from measurement, not headroom.
+MAX_CLUSTERED_POINTS = 300_000
# eps is clamped to this fraction of the projection's coordinate span. UMAP's
# output scale is data-dependent, so an absolute eps close to the span makes
diff --git a/invokeai/frontend/webv2/src/workbench/image-map/clusterStats.test.ts b/invokeai/frontend/webv2/src/workbench/image-map/clusterStats.test.ts
index b3cdde9986..811ec48101 100644
--- a/invokeai/frontend/webv2/src/workbench/image-map/clusterStats.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/image-map/clusterStats.test.ts
@@ -18,7 +18,6 @@ describe('summarizeClusters', () => {
expect(summarizeClusters(points(0, 0, 0, 1, 1, 2, -1, -1))).toEqual({
clusterCount: 3,
largestCluster: 3,
- smallestCluster: 1,
unclustered: 2,
});
});
@@ -28,7 +27,6 @@ describe('summarizeClusters', () => {
expect(summarizeClusters(points(-1, -1, -1))).toEqual({
clusterCount: 0,
largestCluster: 0,
- smallestCluster: 0,
unclustered: 3,
});
});
@@ -39,25 +37,19 @@ describe('summarizeClusters', () => {
expect(summarizeClusters(points(7, 7, 42))).toEqual({
clusterCount: 2,
largestCluster: 2,
- smallestCluster: 1,
unclustered: 0,
});
});
it('handles an empty map', () => {
- expect(summarizeClusters([])).toEqual({
- clusterCount: 0,
- largestCluster: 0,
- smallestCluster: 0,
- unclustered: 0,
- });
+ expect(summarizeClusters([])).toEqual({ clusterCount: 0, largestCluster: 0, unclustered: 0 });
});
});
describe('describeClusters', () => {
it('agrees with the noun it is counting', () => {
expect(describeClusters(summarizeClusters(points(0, 0, 1, -1)))).toBe(
- 'Cluster count: 2, Largest cluster: 2 media points, Smallest cluster: 1 media point, Unclustered: 1 media point'
+ 'Cluster count: 2, Largest cluster: 2 media points, Unclustered: 1 media point'
);
});
diff --git a/invokeai/frontend/webv2/src/workbench/image-map/clusterStats.ts b/invokeai/frontend/webv2/src/workbench/image-map/clusterStats.ts
index a22166d2d4..f8977e5ded 100644
--- a/invokeai/frontend/webv2/src/workbench/image-map/clusterStats.ts
+++ b/invokeai/frontend/webv2/src/workbench/image-map/clusterStats.ts
@@ -4,8 +4,6 @@ export interface ClusterStats {
clusterCount: number;
/** Size of the biggest cluster, or 0 when nothing clustered. */
largestCluster: number;
- /** Size of the smallest cluster, or 0 when nothing clustered. */
- smallestCluster: number;
/** Points DBSCAN labelled noise (cluster -1). */
unclustered: number;
}
@@ -22,7 +20,6 @@ export const describeClusters = (stats: ClusterStats): string =>
[
`Cluster count: ${stats.clusterCount.toLocaleString()}`,
`Largest cluster: ${count(stats.largestCluster, 'media point')}`,
- `Smallest cluster: ${count(stats.smallestCluster, 'media point')}`,
`Unclustered: ${count(stats.unclustered, 'media point')}`,
].join(', ');
@@ -52,17 +49,10 @@ export const summarizeClusters = (points: readonly ImageMapPoint[]): ClusterStat
}
let largestCluster = 0;
- let smallestCluster = Infinity;
for (const size of sizes.values()) {
largestCluster = Math.max(largestCluster, size);
- smallestCluster = Math.min(smallestCluster, size);
}
- return {
- clusterCount: sizes.size,
- largestCluster,
- smallestCluster: sizes.size > 0 ? smallestCluster : 0,
- unclustered,
- };
+ return { clusterCount: sizes.size, largestCluster, unclustered };
};
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapWidgetFooter.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapWidgetFooter.browser.test.tsx
index 550289f0c6..8d053339ca 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapWidgetFooter.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapWidgetFooter.browser.test.tsx
@@ -94,7 +94,7 @@ describe('ImageMapWidgetFooter', () => {
expect(host?.textContent).toContain('6 points');
expect(host?.textContent).toContain(
- 'Cluster count: 2, Largest cluster: 3 media points, Smallest cluster: 2 media points, Unclustered: 1 media point'
+ 'Cluster count: 2, Largest cluster: 3 media points, Unclustered: 1 media point'
);
});
@@ -127,9 +127,7 @@ describe('ImageMapWidgetFooter', () => {
const tooltip = document.querySelector('[role="tooltip"]');
- expect(tooltip?.textContent).toBe(
- 'Cluster count: 2, Largest cluster: 3 media points, Smallest cluster: 2 media points, Unclustered: 1 media point'
- );
+ expect(tooltip?.textContent).toBe('Cluster count: 2, Largest cluster: 3 media points, Unclustered: 1 media point');
});
it('says so when nothing clustered', async () => {
@@ -137,7 +135,7 @@ describe('ImageMapWidgetFooter', () => {
await renderFooter([-1, -1, -1]);
expect(host?.textContent).toContain(
- 'Cluster count: 0, Largest cluster: 0 media points, Smallest cluster: 0 media points, Unclustered: 3 media points'
+ 'Cluster count: 0, Largest cluster: 0 media points, Unclustered: 3 media points'
);
});
});