Skip to content
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

Clear torch cache memory every 20s #90

Merged
merged 1 commit into from
May 2, 2023
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
19 changes: 12 additions & 7 deletions generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,7 @@ def generate(callback=None, **kwargs):
raise
return
else:
clear_torch_cache()
raise

decoded_output = None
Expand All @@ -844,13 +845,17 @@ def generate(callback=None, **kwargs):
if save_dir and decoded_output:
save_generate_output(output=decoded_output, base_model=base_model, save_dir=save_dir)
else:
outputs = model.generate(**gen_kwargs)
outputs = [decoder(s) for s in outputs.sequences]
yield prompter.get_response(outputs, prompt=inputs_decoded,
sanitize_bot_response=sanitize_bot_response)
if save_dir and outputs and len(outputs) >= 1:
decoded_output = prompt + outputs[0]
save_generate_output(output=decoded_output, base_model=base_model, save_dir=save_dir)
try:
outputs = model.generate(**gen_kwargs)
outputs = [decoder(s) for s in outputs.sequences]
yield prompter.get_response(outputs, prompt=inputs_decoded,
sanitize_bot_response=sanitize_bot_response)
if save_dir and outputs and len(outputs) >= 1:
decoded_output = prompt + outputs[0]
save_generate_output(output=decoded_output, base_model=base_model, save_dir=save_dir)
except BaseException:
clear_torch_cache()
raise


def get_generate_params(model_lower, chat,
Expand Down
9 changes: 8 additions & 1 deletion gradio_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from finetune import prompt_type_to_model_name, prompt_types_strings, generate_prompt, inv_prompt_type_to_model_lower
from generate import get_model, languages_covered, evaluate, eval_func_param_names, score_qa

import gradio as gr
from apscheduler.schedulers.background import BackgroundScheduler


def go_gradio(**kwargs):
allow_api = kwargs['allow_api']
Expand Down Expand Up @@ -58,7 +61,6 @@ def go_gradio(**kwargs):
"""
else:
css_code = """footer {visibility: hidden}"""
import gradio as gr

if kwargs['gradio_avoid_processing_markdown']:
from gradio_client import utils as client_utils
Expand Down Expand Up @@ -841,6 +843,11 @@ def get_system_info():

demo.queue(concurrency_count=kwargs['concurrency_count'], api_open=kwargs['api_open'])
favicon_path = "h2o-logo.svg"

scheduler = BackgroundScheduler()
scheduler.add_job(func=clear_torch_cache, trigger="interval", seconds=20)
scheduler.start()

demo.launch(share=kwargs['share'], server_name="0.0.0.0", show_error=True,
favicon_path=favicon_path, prevent_thread_lock=True) # , enable_queue=True)
print("Started GUI", flush=True)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ bitsandbytes==0.38.1
git+https://github.com/huggingface/peft.git@098962fa6515f2e4fe83a757f5995d3ffbb1c373
transformers==4.28.1
tokenizers==0.13.3
APScheduler==3.10.1

# optional for generate
pynvml==11.5.0
Expand Down