From f08f50951f1a60c2cf1a37d6d6f48e8cce03773e Mon Sep 17 00:00:00 2001 From: space-nuko <24979496+space-nuko@users.noreply.github.com> Date: Fri, 2 Jun 2023 10:42:25 -0500 Subject: [PATCH] Show free VRAM --- src/lib/api.ts | 21 +++++++- src/lib/components/ComfyApp.ts | 18 +++++++ src/lib/components/ComfyQueue.svelte | 7 ++- src/lib/components/SystemStatsBar.svelte | 65 ++++++++++++++++++++++++ src/lib/stores/configDefs.ts | 14 +++++ src/lib/stores/systemState.ts | 39 ++++++++++++++ 6 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 src/lib/components/SystemStatsBar.svelte create mode 100644 src/lib/stores/systemState.ts diff --git a/src/lib/api.ts b/src/lib/api.ts index dc8765b..94e96dc 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -6,7 +6,7 @@ import type { SerializedLGraph, UUID } from "@litegraph-ts/core"; import type { SerializedLayoutState } from "./stores/layoutStates"; import type { ComfyNodeDef, ComfyNodeDefInput } from "./ComfyNodeDef"; import type { WorkflowInstID } from "./stores/workflowState"; -import type { ComfyAPIPromptErrorResponse } from "./apiErrors"; +import type { ComfyAPIPromptErrorResponse, ComfyExecutionError, ComfyInterruptedError } from "./apiErrors"; export type ComfyPromptRequest = { client_id?: string, @@ -60,6 +60,20 @@ export type ComfyAPIHistoryResponse = { error?: string } +export type ComfyDevice = { + name: string, + type: string, + index: number, + vram_total: number + vram_free: number + torch_vram_total: number + torch_vram_free: number +} + +export type ComfyAPISystemStatsResponse = { + devices: ComfyDevice[] +} + export type SerializedComfyBoxPromptData = { subgraphs: string[] } @@ -371,4 +385,9 @@ export default class ComfyAPI { async interrupt(): Promise { return fetch(this.getBackendUrl() + "/interrupt", { method: "POST" }); } + + async getSystemStats(): Promise { + return fetch(this.getBackendUrl() + "/system_stats") + .then(async (resp) => (await resp.json()) as ComfyAPISystemStatsResponse); + } } diff --git a/src/lib/components/ComfyApp.ts b/src/lib/components/ComfyApp.ts index 2c77775..2b10b4f 100644 --- a/src/lib/components/ComfyApp.ts +++ b/src/lib/components/ComfyApp.ts @@ -39,6 +39,7 @@ import DanbooruTags from "$lib/DanbooruTags"; import { deserializeTemplateFromSVG, type SerializedComfyBoxTemplate } from "$lib/ComfyBoxTemplate"; import templateState from "$lib/stores/templateState"; import { formatValidationError, type ComfyAPIPromptErrorResponse, formatExecutionError, type ComfyExecutionError } from "$lib/apiErrors"; +import systemState from "$lib/stores/systemState"; export const COMFYBOX_SERIAL_VERSION = 1; @@ -650,6 +651,23 @@ export default class ComfyApp { } }); + const config = get(configState); + + if (config.pollSystemStatsInterval > 0) { + const interval = Math.max(config.pollSystemStatsInterval, 250); + const refresh = async () => { + try { + const resp = await this.api.getSystemStats(); + systemState.updateState(resp) + } catch (error) { + // console.debug("Error retrieving stats", error) + systemState.updateState({ devices: [] }) + } + setTimeout(refresh, interval); + } + setTimeout(refresh, interval); + } + this.api.init(); } diff --git a/src/lib/components/ComfyQueue.svelte b/src/lib/components/ComfyQueue.svelte index 2b8c40d..31719e2 100644 --- a/src/lib/components/ComfyQueue.svelte +++ b/src/lib/components/ComfyQueue.svelte @@ -16,6 +16,7 @@ + +
+
+ VRAM: {text} +
+
+ + diff --git a/src/lib/stores/configDefs.ts b/src/lib/stores/configDefs.ts index e9ab20e..4cd07b2 100644 --- a/src/lib/stores/configDefs.ts +++ b/src/lib/stores/configDefs.ts @@ -155,6 +155,19 @@ const defCacheBuiltInResources: ConfigDefBoolean<"cacheBuiltInResources"> = { options: {} }; +const defPollSystemStatsInterval: ConfigDefNumber<"pollSystemStatsInterval"> = { + name: "pollSystemStatsInterval", + type: "number", + defaultValue: 1000, + category: "behavior", + description: "Interval in milliseconds to refresh system stats (total/free VRAM). Set to 0 to disable", + options: { + min: 0, + max: 60000, + step: 100 + } +}; + const defBuiltInTemplates: ConfigDefStringArray<"builtInTemplates"> = { name: "builtInTemplates", type: "string[]", @@ -198,6 +211,7 @@ export const CONFIG_DEFS = [ defPromptForWorkflowName, defConfirmWhenUnloadingUnsavedChanges, defCacheBuiltInResources, + defPollSystemStatsInterval, defBuiltInTemplates, // defLinkDisplayType ] as const; diff --git a/src/lib/stores/systemState.ts b/src/lib/stores/systemState.ts new file mode 100644 index 0000000..6411be4 --- /dev/null +++ b/src/lib/stores/systemState.ts @@ -0,0 +1,39 @@ +import { debounce, isMobileBrowser } from '$lib/utils'; +import { get, writable } from 'svelte/store'; +import type { Readable, Writable } from 'svelte/store'; +import type { WorkflowInstID, WorkflowReceiveOutputTargets } from './workflowState'; +import modalState, { type ModalData } from './modalState'; +import type { SlotType } from '@litegraph-ts/core'; +import type ComfyApp from '$lib/components/ComfyApp'; +import SendOutputModal, { type SendOutputModalResult } from "$lib/components/modal/SendOutputModal.svelte"; +import workflowState from './workflowState'; +import type { ComfyAPISystemStatsResponse, ComfyDevice } from '$lib/api'; + + + +export type SystemState = { + devices: ComfyDevice[] +} + +type SystemStateOps = { + updateState: (resp: ComfyAPISystemStatsResponse) => void +} + +export type WritableSystemStateStore = Writable & SystemStateOps; +const store: Writable = writable( + { + devices: [] + }) + +function updateState(resp: ComfyAPISystemStatsResponse) { + store.set({ + devices: resp.devices + }) +} + +const interfaceStateStore: WritableSystemStateStore = +{ + ...store, + updateState +} +export default interfaceStateStore;