Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -272,6 +272,7 @@ async def _engine_call(engine, coro, timeout_sec: float):
|
|
| 272 |
_RAM_CLEANUP_THRESHOLD_MB = float(os.environ.get("RAM_CLEANUP_THRESHOLD_MB", "300"))
|
| 273 |
_RAM_CLEANUP_INTERVAL_SEC = int(os.environ.get("RAM_CLEANUP_INTERVAL_SEC", "60"))
|
| 274 |
_CLEAR_HASH_AFTER_MOVE = os.environ.get("CLEAR_HASH_AFTER_MOVE", "1").strip().lower() not in {"0", "false", "no", "off"}
|
|
|
|
| 275 |
|
| 276 |
async def memory_cleanup_task():
|
| 277 |
"""
|
|
@@ -311,7 +312,10 @@ async def memory_cleanup_task():
|
|
| 311 |
async def lifespan(app: FastAPI):
|
| 312 |
cleanup_task = asyncio.create_task(memory_cleanup_task())
|
| 313 |
print(f"[STARTUP] Memory cleanup task started (every {_RAM_CLEANUP_INTERVAL_SEC}s, threshold {_RAM_CLEANUP_THRESHOLD_MB}MB)")
|
| 314 |
-
print(
|
|
|
|
|
|
|
|
|
|
| 315 |
yield
|
| 316 |
cleanup_task.cancel()
|
| 317 |
try:
|
|
@@ -489,48 +493,51 @@ async def get_move(request: MoveRequest):
|
|
| 489 |
tsec,
|
| 490 |
)
|
| 491 |
|
| 492 |
-
|
| 493 |
-
|
| 494 |
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
|
|
|
|
|
|
| 503 |
break
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
pv = " ".join(pv_parts)
|
| 507 |
-
del pv_board
|
| 508 |
-
|
| 509 |
-
score_pawns = score_cp / 100.0 if abs(score_cp) < 9900 else (100.0 if score_cp > 0 else -100.0)
|
| 510 |
-
board_fen_only = board.fen().split(" ")[0]
|
| 511 |
-
opening_name = openings_db.get(board_fen_only)
|
| 512 |
-
best_move = result.move.uci()
|
| 513 |
|
| 514 |
-
|
| 515 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 516 |
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
pv=pv,
|
| 524 |
-
mate_in=mate_in,
|
| 525 |
-
opening=opening_name
|
| 526 |
-
)
|
| 527 |
-
# On constrained RAM environments (e.g. HF Spaces), keeping hash between
|
| 528 |
-
# move calls can still cause memory pressure over long games.
|
| 529 |
-
if _CLEAR_HASH_AFTER_MOVE:
|
| 530 |
-
async with _ENGINE_IO_LOCK:
|
| 531 |
await _clear_engine_hash(engine)
|
| 532 |
-
|
| 533 |
-
|
|
|
|
|
|
|
|
|
|
| 534 |
except HTTPException:
|
| 535 |
raise
|
| 536 |
except Exception as e:
|
|
|
|
| 272 |
_RAM_CLEANUP_THRESHOLD_MB = float(os.environ.get("RAM_CLEANUP_THRESHOLD_MB", "300"))
|
| 273 |
_RAM_CLEANUP_INTERVAL_SEC = int(os.environ.get("RAM_CLEANUP_INTERVAL_SEC", "60"))
|
| 274 |
_CLEAR_HASH_AFTER_MOVE = os.environ.get("CLEAR_HASH_AFTER_MOVE", "1").strip().lower() not in {"0", "false", "no", "off"}
|
| 275 |
+
_RESTART_ENGINE_AFTER_MOVE = os.environ.get("RESTART_ENGINE_AFTER_MOVE", "1").strip().lower() not in {"0", "false", "no", "off"}
|
| 276 |
|
| 277 |
async def memory_cleanup_task():
|
| 278 |
"""
|
|
|
|
| 312 |
async def lifespan(app: FastAPI):
|
| 313 |
cleanup_task = asyncio.create_task(memory_cleanup_task())
|
| 314 |
print(f"[STARTUP] Memory cleanup task started (every {_RAM_CLEANUP_INTERVAL_SEC}s, threshold {_RAM_CLEANUP_THRESHOLD_MB}MB)")
|
| 315 |
+
print(
|
| 316 |
+
f"[STARTUP] Engine config: hash_mb={_engine_hash_mb()} "
|
| 317 |
+
f"clear_after_move={_CLEAR_HASH_AFTER_MOVE} restart_after_move={_RESTART_ENGINE_AFTER_MOVE}"
|
| 318 |
+
)
|
| 319 |
yield
|
| 320 |
cleanup_task.cancel()
|
| 321 |
try:
|
|
|
|
| 493 |
tsec,
|
| 494 |
)
|
| 495 |
|
| 496 |
+
score_cp, mate_in = get_normalized_score(info)
|
| 497 |
+
depth, nodes, nps = normalize_search_stats(info)
|
| 498 |
|
| 499 |
+
pv_board = board.copy()
|
| 500 |
+
pv_parts = []
|
| 501 |
+
for m in info.get("pv", [])[:5]:
|
| 502 |
+
if m in pv_board.legal_moves:
|
| 503 |
+
try:
|
| 504 |
+
pv_parts.append(pv_board.san(m))
|
| 505 |
+
pv_board.push(m)
|
| 506 |
+
except Exception:
|
| 507 |
+
break
|
| 508 |
+
else:
|
| 509 |
break
|
| 510 |
+
pv = " ".join(pv_parts)
|
| 511 |
+
del pv_board
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 512 |
|
| 513 |
+
score_pawns = score_cp / 100.0 if abs(score_cp) < 9900 else (100.0 if score_cp > 0 else -100.0)
|
| 514 |
+
board_fen_only = board.fen().split(" ")[0]
|
| 515 |
+
opening_name = openings_db.get(board_fen_only)
|
| 516 |
+
best_move = result.move.uci()
|
| 517 |
+
|
| 518 |
+
response = MoveResponse(
|
| 519 |
+
bestmove=best_move,
|
| 520 |
+
score=score_pawns,
|
| 521 |
+
depth=depth,
|
| 522 |
+
nodes=nodes,
|
| 523 |
+
nps=nps,
|
| 524 |
+
pv=pv,
|
| 525 |
+
mate_in=mate_in,
|
| 526 |
+
opening=opening_name
|
| 527 |
+
)
|
| 528 |
|
| 529 |
+
# IMPORTANT: do reset/clear while holding the engine IO lock so no
|
| 530 |
+
# other /move call can reuse a half-cleared engine.
|
| 531 |
+
if _RESTART_ENGINE_AFTER_MOVE:
|
| 532 |
+
await _detach_and_quit_engine(engine)
|
| 533 |
+
force_memory_release()
|
| 534 |
+
elif _CLEAR_HASH_AFTER_MOVE:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
await _clear_engine_hash(engine)
|
| 536 |
+
force_memory_release()
|
| 537 |
+
|
| 538 |
+
del result
|
| 539 |
+
del info
|
| 540 |
+
return response
|
| 541 |
except HTTPException:
|
| 542 |
raise
|
| 543 |
except Exception as e:
|