diff --git a/docs/guides/c_api.md b/docs/guides/c_api.md index 4c3d7cebf..ad00cc21a 100644 --- a/docs/guides/c_api.md +++ b/docs/guides/c_api.md @@ -83,8 +83,8 @@ respectively. Use `sd_dgl_ctx_release_staging()` to release idle GPU staging copies backed by RAM weights. Use `sd_dgl_ctx_unload_models()` to release cached model components -and reload them on demand. Both return `SD_STATUS_BUSY` while the context is -running a script or generation. +and reload them on demand. Both clear idle buffer caches on supported backends +and return `SD_STATUS_BUSY` while the context is running a script or generation. Use `sd_generate_video()` with options from `sd_vid_gen_opt_create()` to generate video. Its result exposes frames through the same image accessors, optional audio through diff --git a/examples/server/README.md b/examples/server/README.md index 12ca94cd8..b8c91c977 100644 --- a/examples/server/README.md +++ b/examples/server/README.md @@ -444,6 +444,9 @@ configured defaults. | `--weight-staging-ttl ` | `-1` | GPU staging copies of weights backed by RAM, including components placed on CPU by `--params-backend cpu` or auto-fit. The next request uploads the needed weights from RAM. | | `--weight-residency-ttl ` | `-1` | Cached model components, including RAM weights, GPU weights, staging copies, and component caches. The next request loads the needed components from their files. | +Both options also clear idle reusable buffers on supported backends after +releasing weights. + For either option, `-1` disables release, `0` releases as soon as the shared context is idle, and a positive integer sets the idle time in seconds. The timer starts when the last job finishes. Running and queued generation or DGL diff --git a/include/stable-diffusion.h b/include/stable-diffusion.h index 92a7fba70..8760e5b02 100644 --- a/include/stable-diffusion.h +++ b/include/stable-diffusion.h @@ -365,9 +365,11 @@ SD_API void sd_dgl_ctx_free(sd_dgl_ctx_t* ctx); SD_API bool sd_dgl_ctx_set(sd_dgl_ctx_t* ctx, const char* key, const char* value); // Releases idle GPU staging buffers that have RAM originals. +// Clears idle buffer caches on supported backends. // Returns SD_STATUS_BUSY during DGL execution or generation. SD_API sd_status_t sd_dgl_ctx_release_staging(sd_dgl_ctx_t* ctx); -// Unloads cached model components. The context keeps its settings and callbacks. +// Unloads cached model components and clears idle buffer caches on supported backends. +// The context keeps its settings and callbacks. // Subsequent requests reload components as needed. Returns SD_STATUS_BUSY during execution. SD_API sd_status_t sd_dgl_ctx_unload_models(sd_dgl_ctx_t* ctx); diff --git a/src/dgl/dgl.cpp b/src/dgl/dgl.cpp index 5439f20cd..1acd99475 100644 --- a/src/dgl/dgl.cpp +++ b/src/dgl/dgl.cpp @@ -36,6 +36,13 @@ namespace { return lock; } + static void clear_backend_buffer_caches(const Context& ctx) { + // Idle weight release can refill a cache that runner cleanup already cleared. + for (const auto& entry : *ctx.backends) { + sd_backend_clear_buffer_cache(entry.second.get()); + } + } + class ActiveRun { Context& context_; @@ -145,6 +152,7 @@ sd_status_t sd_dgl_ctx_release_staging(sd_dgl_ctx_t* ctx) { return api_call([&] { auto lock = lock_idle(ctx); ctx->models->release_idle_gpu_staging(); + clear_backend_buffer_caches(*ctx); }); } @@ -152,6 +160,7 @@ sd_status_t sd_dgl_ctx_unload_models(sd_dgl_ctx_t* ctx) { return api_call([&] { auto lock = lock_idle(ctx); ctx->unload_models(); + clear_backend_buffer_caches(*ctx); }); }