Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/guides/c_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions examples/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ configured defaults.
| `--weight-staging-ttl <seconds>` | `-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 <seconds>` | `-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
Expand Down
4 changes: 3 additions & 1 deletion include/stable-diffusion.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
9 changes: 9 additions & 0 deletions src/dgl/dgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_;

Expand Down Expand Up @@ -145,13 +152,15 @@ 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);
});
}

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);
});
}

Expand Down
Loading