-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into refactor/redesign-schema
- Loading branch information
Showing
23 changed files
with
1,340 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -188,3 +188,6 @@ testing/ | |
|
||
# prisma | ||
database/prisma/ | ||
|
||
# scores data | ||
scores/*.pt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import json | ||
from pathlib import Path | ||
|
||
import torch | ||
from bittensor.btlogging import logging as logger | ||
|
||
from database.client import connect_db, disconnect_db | ||
from database.prisma.models import Score_Model | ||
|
||
|
||
class ScoreStorage: | ||
"""Handles persistence of validator scores""" | ||
|
||
SCORES_DIR = Path("scores") | ||
SCORES_FILE = SCORES_DIR / "miner_scores.pt" | ||
|
||
@classmethod | ||
async def migrate_from_db(cls) -> bool: | ||
"""One-time migration of scores from database to .pt file | ||
Returns: | ||
bool: True if migration successful or file already exists, False if migration failed | ||
""" | ||
try: | ||
if cls.SCORES_FILE.exists(): | ||
logger.info("Scores file already exists, skipping migration") | ||
return True | ||
|
||
# Connect to database first | ||
await connect_db() | ||
|
||
try: | ||
# Get scores from database | ||
score_record = await Score_Model.prisma().find_first( | ||
order={"created_at": "desc"} | ||
) | ||
if not score_record: | ||
logger.warning("No scores found in database to migrate") | ||
return True # Not an error, just no scores yet | ||
|
||
scores = torch.tensor(json.loads(score_record.score)) | ||
|
||
# Create scores directory if it doesn't exist | ||
cls.SCORES_DIR.mkdir(exist_ok=True) | ||
|
||
# Save scores to .pt file | ||
torch.save(scores, cls.SCORES_FILE) | ||
logger.success(f"Successfully migrated scores to {cls.SCORES_FILE}") | ||
|
||
# Verify the migration | ||
loaded_scores = torch.load(cls.SCORES_FILE) | ||
if torch.equal(scores, loaded_scores): | ||
logger.success("Migration verification successful - scores match") | ||
return True | ||
else: | ||
logger.error("Migration verification failed - scores do not match") | ||
return False | ||
|
||
finally: | ||
await disconnect_db() | ||
|
||
except Exception as e: | ||
logger.error(f"Failed to migrate scores: {e}") | ||
return False | ||
|
||
@classmethod | ||
async def save(cls, scores: torch.Tensor) -> None: | ||
"""Save validator scores to .pt file""" | ||
try: | ||
cls.SCORES_DIR.mkdir(exist_ok=True) | ||
torch.save(scores, cls.SCORES_FILE) | ||
logger.success("Successfully saved validator scores to file") | ||
except Exception as e: | ||
logger.error(f"Failed to save validator scores: {e}") | ||
raise | ||
|
||
@classmethod | ||
async def load(cls) -> torch.Tensor | None: | ||
"""Load validator scores from .pt file""" | ||
try: | ||
if not cls.SCORES_FILE.exists(): | ||
logger.warning("No validator scores file found") | ||
return None | ||
|
||
scores = torch.load(cls.SCORES_FILE) | ||
logger.success("Successfully loaded validator scores from file") | ||
return scores | ||
except Exception as e: | ||
logger.error(f"Failed to load validator scores: {e}") | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
FROM python:3.11-slim-bookworm | ||
|
||
WORKDIR /app | ||
|
||
ENV PATH="/root/.cargo/bin/:$PATH" | ||
ENV UV_SYSTEM_PYTHON=true | ||
ENV NVM_DIR=/root/.nvm | ||
ENV NODE_VERSION=v20.11.1 | ||
ENV NODE_PATH=$NVM_DIR/versions/node/$NODE_VERSION/lib/node_modules | ||
ENV PATH=$NVM_DIR/versions/node/$NODE_VERSION/bin:$PATH | ||
|
||
RUN apt-get update \ | ||
&& apt-get install -y --no-install-recommends \ | ||
build-essential curl git ca-certificates \ | ||
&& apt-get clean \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
|
||
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv | ||
COPY . . | ||
|
||
ARG TARGETPLATFORM | ||
|
||
RUN echo "Building for TARGETPLATFORM: $TARGETPLATFORM" | ||
|
||
RUN git config --global --add safe.directory /app | ||
|
||
# jank because pytorch has different versions for cpu for darwin VS linux, see pyproject.toml for specifics | ||
# RUN if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \ | ||
# uv pip install --no-cache -e .[dataset] --find-links https://download.pytorch.org/whl/torch_stable.html; \ | ||
# else \ | ||
# uv pip install --no-cache -e .[dataset]; \ | ||
# fi | ||
RUN uv pip install --no-cache -e ".[dataset]" --find-links https://download.pytorch.org/whl/torch_stable.html; | ||
|
||
ENTRYPOINT ["./entrypoints.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.