Skip to content

Commit

Permalink
fix(api): run GC after changing pipeline (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Jan 20, 2023
1 parent 9a2e7ad commit 4a3bb97
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions api/onnx_web/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
OnnxStableDiffusionImg2ImgPipeline,
OnnxStableDiffusionInpaintPipeline,
)
from os import environ
from PIL import Image, ImageChops
from typing import Any

import gc
import numpy as np

from .image import (
Expand All @@ -19,6 +19,7 @@
UpscaleParams,
)
from .utils import (
is_debug,
safer_join,
BaseParams,
Border,
Expand Down Expand Up @@ -70,6 +71,9 @@ def load_pipeline(pipeline: DiffusionPipeline, model: str, provider: str, schedu
model, subfolder='scheduler')
last_pipeline_scheduler = scheduler

print('running garbage collection during pipeline change')
gc.collect()

return pipe


Expand Down Expand Up @@ -167,7 +171,7 @@ def run_inpaint_pipeline(
noise_source=noise_source,
mask_filter=mask_filter)

if environ.get('DEBUG') is not None:
if is_debug():
source_image.save(safer_join(ctx.output_path, 'last-source.png'))
mask_image.save(safer_join(ctx.output_path, 'last-mask.png'))
noise_image.save(safer_join(ctx.output_path, 'last-noise.png'))
Expand Down
5 changes: 5 additions & 0 deletions api/onnx_web/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
UpscaleParams,
)
from .utils import (
is_debug,
get_and_clamp_float,
get_and_clamp_int,
get_from_list,
Expand All @@ -57,6 +58,7 @@
Size,
)

import gc
import json
import numpy as np

Expand Down Expand Up @@ -259,6 +261,9 @@ def load_params(context: ServerContext):
CORS(app, origins=context.cors_origin)
executor = Executor(app)

if is_debug():
gc.set_debug(gc.DEBUG_STATS)


# TODO: these two use context

Expand Down
4 changes: 4 additions & 0 deletions api/onnx_web/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def tojson(self) -> Dict[str, int]:
}


def is_debug() -> bool:
return environ.get('DEBUG') is not None


def get_and_clamp_float(args: Any, key: str, default_value: float, max_value: float, min_value=0.0) -> float:
return min(max(float(args.get(key, default_value)), min_value), max_value)

Expand Down

0 comments on commit 4a3bb97

Please sign in to comment.