-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Improve GPU memory usage indication #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { | ||
| ChunkMemoryStatistics, | ||
| ChunkPriorityTier, | ||
| ChunkState, | ||
| getChunkStateStatisticIndex, | ||
| numChunkMemoryStatistics, | ||
| numChunkStatistics, | ||
| } from "#src/chunk_manager/base.js"; | ||
| import { | ||
| computeGpuMemoryBytes, | ||
| getGpuMemoryPressure, | ||
| getLayerGpuMemoryPressure, | ||
| } from "#src/ui/layer_bar_gpu_memory_pressure.js"; | ||
|
|
||
| function makeStatistics( | ||
| gpuMemoryBytesByTier: Partial<Record<ChunkPriorityTier, number>>, | ||
| ) { | ||
| const statistics = new Float64Array(numChunkStatistics); | ||
| for (const [tier, bytes] of Object.entries(gpuMemoryBytesByTier)) { | ||
| statistics[ | ||
| getChunkStateStatisticIndex(ChunkState.GPU_MEMORY, Number(tier)) * | ||
| numChunkMemoryStatistics + | ||
| ChunkMemoryStatistics.gpuMemoryBytes | ||
| ] = bytes; | ||
| } | ||
| return statistics; | ||
| } | ||
|
|
||
| describe("ui/layer_bar_gpu_memory_pressure", () => { | ||
| it("sums GPU memory across all GPU_MEMORY priority tiers and sources", () => { | ||
| const firstSource = makeStatistics({ | ||
| [ChunkPriorityTier.VISIBLE]: 10, | ||
| [ChunkPriorityTier.PREFETCH]: 20, | ||
| [ChunkPriorityTier.RECENT]: 30, | ||
| }); | ||
| const secondSource = makeStatistics({ | ||
| [ChunkPriorityTier.VISIBLE]: 40, | ||
| [ChunkPriorityTier.RECENT]: 50, | ||
| }); | ||
|
|
||
| expect(computeGpuMemoryBytes([firstSource, secondSource])).toBe(150); | ||
| }); | ||
|
|
||
| it("maps GPU usage ratio to warning and critical pressure", () => { | ||
| expect(getGpuMemoryPressure(89, 100)).toBe("normal"); | ||
| expect(getGpuMemoryPressure(90, 100)).toBe("warning"); | ||
| expect(getGpuMemoryPressure(97, 100)).toBe("warning"); | ||
| expect(getGpuMemoryPressure(98, 100)).toBe("critical"); | ||
| }); | ||
|
|
||
| it("treats invalid or unlimited GPU memory limits as normal pressure", () => { | ||
| expect(getGpuMemoryPressure(100, Number.POSITIVE_INFINITY)).toBe("normal"); | ||
| expect(getGpuMemoryPressure(100, Number.NaN)).toBe("normal"); | ||
| expect(getGpuMemoryPressure(100, 0)).toBe("normal"); | ||
| expect(getGpuMemoryPressure(Number.NaN, 100)).toBe("normal"); | ||
| }); | ||
|
|
||
| it("only applies global pressure to layers missing visible chunks", () => { | ||
| expect(getLayerGpuMemoryPressure("warning", 10, 9)).toBe("warning"); | ||
| expect(getLayerGpuMemoryPressure("critical", 10, 9)).toBe("critical"); | ||
| expect(getLayerGpuMemoryPressure("warning", 10, 10)).toBe("normal"); | ||
| expect(getLayerGpuMemoryPressure("warning", 0, 0)).toBe("normal"); | ||
| expect(getLayerGpuMemoryPressure("normal", 10, 9)).toBe("normal"); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Google Inc. | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { | ||
| ChunkMemoryStatistics, | ||
| ChunkPriorityTier, | ||
| ChunkState, | ||
| getChunkStateStatisticIndex, | ||
| numChunkMemoryStatistics, | ||
| } from "#src/chunk_manager/base.js"; | ||
|
|
||
| export type GpuMemoryPressure = "normal" | "warning" | "critical"; | ||
|
|
||
| export const GPU_MEMORY_PRESSURE_WARNING_RATIO = 0.9; | ||
| export const GPU_MEMORY_PRESSURE_CRITICAL_RATIO = 0.98; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Due to the size of chunks, I think it's quite likely 0.98 won't be reached in many cases. Thinking on that |
||
|
|
||
| export function computeGpuMemoryBytes( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think overall this might be a bit too inclusive. While I like that it's a non-biased stance, I think we might need to be a bit more heuristic based. |
||
| chunkStatistics: Iterable<Float64Array>, | ||
| ) { | ||
| let total = 0; | ||
| for (const statistics of chunkStatistics) { | ||
| for ( | ||
| let tier = ChunkPriorityTier.FIRST_TIER; | ||
| tier <= ChunkPriorityTier.LAST_TIER; | ||
| ++tier | ||
| ) { | ||
| total += | ||
| statistics[ | ||
| getChunkStateStatisticIndex(ChunkState.GPU_MEMORY, tier) * | ||
| numChunkMemoryStatistics + | ||
| ChunkMemoryStatistics.gpuMemoryBytes | ||
| ] || 0; | ||
| } | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| export function getGpuMemoryPressure( | ||
| gpuMemoryBytes: number, | ||
| gpuMemoryLimitBytes: number, | ||
| ): GpuMemoryPressure { | ||
| if ( | ||
| !Number.isFinite(gpuMemoryBytes) || | ||
| !Number.isFinite(gpuMemoryLimitBytes) || | ||
| gpuMemoryBytes < 0 || | ||
| gpuMemoryLimitBytes <= 0 | ||
| ) { | ||
| return "normal"; | ||
| } | ||
|
Comment on lines
+55
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure any of these can really happen, outside of the limit being 0, but then we'd just get infinity which is fine as a critical value |
||
| const ratio = gpuMemoryBytes / gpuMemoryLimitBytes; | ||
| if (ratio >= GPU_MEMORY_PRESSURE_CRITICAL_RATIO) { | ||
| return "critical"; | ||
| } | ||
| if (ratio >= GPU_MEMORY_PRESSURE_WARNING_RATIO) { | ||
| return "warning"; | ||
| } | ||
| return "normal"; | ||
| } | ||
|
|
||
| export function getLayerGpuMemoryPressure( | ||
| globalPressure: GpuMemoryPressure, | ||
| numVisibleChunksNeeded: number, | ||
| numVisibleChunksAvailable: number, | ||
| ): GpuMemoryPressure { | ||
| if ( | ||
| globalPressure === "normal" || | ||
| numVisibleChunksNeeded <= 0 || | ||
| numVisibleChunksAvailable >= numVisibleChunksNeeded | ||
| ) { | ||
| return "normal"; | ||
| } | ||
| return globalPressure; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would probably be better to use an enum