Spaces:
Sleeping
Sleeping
garbage collection
Browse files
server.py
CHANGED
@@ -11,6 +11,8 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
11 |
|
12 |
from concrete.ml.deployment import FHEModelServer
|
13 |
|
|
|
|
|
14 |
# Set up logging
|
15 |
logging.basicConfig(level=logging.INFO)
|
16 |
logger = logging.getLogger(__name__)
|
@@ -61,6 +63,12 @@ async def send_input(user_id: str = Form(), files: List[UploadFile] = File(...))
|
|
61 |
logger.error(f"Error in send_input: {str(e)}")
|
62 |
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
@app.post("/run_fhe")
|
65 |
def run_fhe(user_id: str = Form()):
|
66 |
"""Execute seizure detection on the encrypted input image using FHE."""
|
@@ -84,11 +92,34 @@ def run_fhe(user_id: str = Form()):
|
|
84 |
encrypted_image = encrypted_image_file.read()
|
85 |
evaluation_key = evaluation_key_file.read()
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
# Run the FHE execution
|
88 |
start = time.time()
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
fhe_execution_time = round(time.time() - start, 2)
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
# Retrieve the encrypted output path
|
93 |
encrypted_output_path = get_server_file_path("encrypted_output", user_id)
|
94 |
|
|
|
11 |
|
12 |
from concrete.ml.deployment import FHEModelServer
|
13 |
|
14 |
+
import gc
|
15 |
+
|
16 |
# Set up logging
|
17 |
logging.basicConfig(level=logging.INFO)
|
18 |
logger = logging.getLogger(__name__)
|
|
|
63 |
logger.error(f"Error in send_input: {str(e)}")
|
64 |
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
65 |
|
66 |
+
def get_memory_usage():
|
67 |
+
"""Get current memory usage in GB"""
|
68 |
+
with open('/proc/self/status') as f:
|
69 |
+
memusage = f.read().split('VmRSS:')[1].split('\n')[0][:-3]
|
70 |
+
return int(memusage.strip()) / 1024 / 1024 # Convert KB to GB
|
71 |
+
|
72 |
@app.post("/run_fhe")
|
73 |
def run_fhe(user_id: str = Form()):
|
74 |
"""Execute seizure detection on the encrypted input image using FHE."""
|
|
|
92 |
encrypted_image = encrypted_image_file.read()
|
93 |
evaluation_key = evaluation_key_file.read()
|
94 |
|
95 |
+
memory_before = get_memory_usage()
|
96 |
+
logger.info(f"Memory usage before FHE execution: {memory_before:.2f} GB")
|
97 |
+
|
98 |
+
# Force garbage collection before FHE execution
|
99 |
+
gc.collect()
|
100 |
+
|
101 |
# Run the FHE execution
|
102 |
start = time.time()
|
103 |
+
try:
|
104 |
+
encrypted_output = FHE_SERVER.run(encrypted_image, evaluation_key)
|
105 |
+
except MemoryError:
|
106 |
+
logger.error("FHE execution failed due to insufficient memory")
|
107 |
+
raise HTTPException(status_code=503, detail="Insufficient memory during FHE execution")
|
108 |
+
except Exception as e:
|
109 |
+
logger.error(f"FHE execution failed: {str(e)}")
|
110 |
+
raise HTTPException(status_code=500, detail="FHE execution failed")
|
111 |
+
finally:
|
112 |
+
# Force garbage collection after FHE execution
|
113 |
+
gc.collect()
|
114 |
+
|
115 |
fhe_execution_time = round(time.time() - start, 2)
|
116 |
|
117 |
+
# Check memory usage after FHE execution
|
118 |
+
memory_after = get_memory_usage()
|
119 |
+
logger.info(f"Memory usage after FHE execution: {memory_after:.2f} GB")
|
120 |
+
logger.info(f"Memory increase during FHE execution: {memory_after - memory_before:.2f} GB")
|
121 |
+
|
122 |
+
|
123 |
# Retrieve the encrypted output path
|
124 |
encrypted_output_path = get_server_file_path("encrypted_output", user_id)
|
125 |
|