mirror of
https://github.com/wassname/llm-moral-foundations2.git
synced 2026-08-15 12:35:02 +08:00
16 lines
427 B
Python
16 lines
427 B
Python
import re
|
|
import gc
|
|
import torch
|
|
|
|
def sanitize_filename(s, whitelist=r"a-zA-Z0-9_"):
|
|
# keep space and hyphen in the form of _
|
|
s = s.strip().replace(" ", "_").replace("'", "").replace("-", "_").replace("/", "_")
|
|
# Keep only characters in the whitelist
|
|
return re.sub(f"[^{whitelist}]", "", s)
|
|
|
|
def clear_mem():
|
|
"""Clear GPU memory"""
|
|
torch.cuda.synchronize()
|
|
gc.collect()
|
|
torch.cuda.empty_cache()
|