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

Refactoring try-excepts and remove prints. #57

Merged
merged 6 commits into from
Dec 17, 2024
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
184 changes: 99 additions & 85 deletions src/discord-cluster-manager/cogs/leaderboard_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from consts import GitHubGPU, ModalGPU
from discord import Interaction, SelectOption, app_commands, ui
from discord.ext import commands
from utils import extract_score, get_user_from_id
from utils import extract_score, get_user_from_id, setup_logging

logger = setup_logging()


class LeaderboardSubmitCog(app_commands.Group):
Expand All @@ -21,8 +23,6 @@ def __init__(
@app_commands.describe(
leaderboard_name="Name of the competition / kernel to optimize",
script="The Python / CUDA script file to run",
dtype="dtype (e.g. FP32, BF16, FP4) that the input and output expects.",
shape="Data input shape as a tuple",
)
# TODO: Modularize this so all the write functionality is in here. Haven't figured
# a good way to do this yet.
Expand All @@ -31,8 +31,6 @@ async def submit(
interaction: discord.Interaction,
leaderboard_name: str,
script: discord.Attachment,
dtype: app_commands.Choice[str] = None,
shape: app_commands.Choice[str] = None,
):
pass

Expand All @@ -51,8 +49,6 @@ async def submit_modal(
leaderboard_name: str,
script: discord.Attachment,
gpu_type: app_commands.Choice[str],
dtype: app_commands.Choice[str] = "fp32",
shape: app_commands.Choice[str] = None,
):
try:
# Read the template file
Expand Down Expand Up @@ -109,8 +105,6 @@ async def submit_github(
leaderboard_name: str,
script: discord.Attachment,
gpu_type: app_commands.Choice[str],
dtype: app_commands.Choice[str] = "fp32",
shape: app_commands.Choice[str] = None,
):
# Read the template file
submission_content = await script.read()
Expand Down Expand Up @@ -147,7 +141,6 @@ async def submit_github(
return

github_command = github_cog.run_github
print(github_command)
try:
github_thread = await github_command.callback(
github_cog,
Expand Down Expand Up @@ -274,109 +267,130 @@ async def leaderboard_create(
deadline: str,
reference_code: discord.Attachment,
):
# Try parsing with time first
try:
# Try parsing with time first
date_value = datetime.strptime(deadline, "%Y-%m-%d %H:%M")
except ValueError:
try:
date_value = datetime.strptime(deadline, "%Y-%m-%d %H:%M")
except ValueError:
# If that fails, try parsing just the date (will set time to 00:00)
date_value = datetime.strptime(deadline, "%Y-%m-%d")

# Read the template file
template_content = await reference_code.read()

# Ask the user to select GPUs
view = GPUSelectionView([gpu.name for gpu in GitHubGPU])

if interaction.response.is_done():
await interaction.followup.send(
"Please select GPUs for this leaderboard.",
view=view,
ephemeral=True,
)
else:
except ValueError as ve:
logger.error(f"Value Error: {str(ve)}", exc_info=True)
await interaction.response.send_message(
"Please select GPUs for this leaderboard.",
view=view,
"Invalid date format. Please use YYYY-MM-DD or YYYY-MM-DD HH:MM",
ephemeral=True,
)
return

await view.wait()
# Ask the user to select GPUs
view = GPUSelectionView([gpu.name for gpu in GitHubGPU])

if interaction.response.is_done():
await interaction.followup.send(
"Please select GPUs for this leaderboard.",
view=view,
ephemeral=True,
)
else:
await interaction.response.send_message(
"Please select GPUs for this leaderboard.",
view=view,
ephemeral=True,
)

await view.wait()

try:
# Read the template file
template_content = await reference_code.read()

with self.bot.leaderboard_db as db:
print(
leaderboard_name,
type(date_value),
type(template_content.decode("utf-8")),
)
db.create_leaderboard({
err = db.create_leaderboard({
"name": leaderboard_name,
"deadline": date_value,
"reference_code": template_content.decode("utf-8"),
"gpu_types": view.selected_gpus,
})

if interaction.response.is_done():
await interaction.followup.send(
f"Leaderboard '{leaderboard_name}' created.\n"
+ f"Reference code: {reference_code}.\n"
+ f"Submission deadline: {date_value}",
ephemeral=True,
)
else:
await interaction.response.send_message(
f"Leaderboard '{leaderboard_name}' created.\n"
+ f"Reference code: {reference_code}.\n"
+ f"Submission deadline: {date_value}",
ephemeral=True,
)
except ValueError:
await interaction.response.send_message(
"Invalid date format. Please use YYYY-MM-DD or YYYY-MM-DD HH:MM",
if err:
if "duplicate key" in err:
await interaction.followup.send(
f'Error: Tried to create a leaderboard "{leaderboard_name}" that already exists.',
ephemeral=True,
)
else:
# Handle any other errors
logger.error(f"Error in leaderboard creation: {err}")
await interaction.followup.send(
"Error in leaderboard creation.",
ephemeral=True,
)
return

await interaction.followup.send(
f"Leaderboard '{leaderboard_name}'.\n"
+ f"Reference code: {reference_code}. Submission deadline: {date_value}",
ephemeral=True,
)

except Exception as e:
logger.error(f"Error in leaderboard creation: {e}")
# Handle any other errors
await interaction.followup.send(
"Error in leaderboard creation.",
ephemeral=True,
)


@discord.app_commands.describe(leaderboard_name="Name of the leaderboard")
async def get_leaderboard_submissions(
self,
interaction: discord.Interaction,
leaderboard_name: str,
dtype: app_commands.Choice[str] = "fp32",
):
with self.bot.leaderboard_db as db:
# TODO: query that gets leaderboard id given leaderboard name
leaderboard_id = db.get_leaderboard(leaderboard_name)["id"]
if not leaderboard_id:
try:
with self.bot.leaderboard_db as db:
# TODO: query that gets leaderboard id given leaderboard name
leaderboard_id = db.get_leaderboard(leaderboard_name)["id"]
if not leaderboard_id:
await interaction.response.send_message(
f'Leaderboard "{leaderboard_name}" not found.', ephemeral=True
)
return

submissions = db.get_leaderboard_submissions(leaderboard_name)

if not submissions:
await interaction.response.send_message(
"Leaderboard not found.", ephemeral=True
f'No submissions found for "{leaderboard_name}".', ephemeral=True
)
return

# submissions = db.get_leaderboard_submissions(leaderboard_id) # Add dtype
submissions = db.get_leaderboard_submissions(leaderboard_name) # Add dtype

if not submissions:
await interaction.response.send_message(
"No submissions found.", ephemeral=True
# Create embed
embed = discord.Embed(
title=f'Leaderboard Submissions for "{leaderboard_name}"',
color=discord.Color.blue(),
)
return

# Create embed
embed = discord.Embed(
title=f'Leaderboard Submissions for "{leaderboard_name}"',
color=discord.Color.blue(),
)

for submission in submissions:
user_id = await get_user_from_id(
submission["user_id"], interaction, self.bot
)
print("members", interaction.guild.members)
print(user_id)
for submission in submissions:
user_id = await get_user_from_id(
submission["user_id"], interaction, self.bot
)

embed.add_field(
name=f"{user_id}: {submission['submission_name']}",
value=f"Submission speed: {submission['submission_score']}",
inline=False,
)
embed.add_field(
name=f"{user_id}: {submission['submission_name']}",
value=f"Submission speed: {submission['submission_score']}",
inline=False,
)

await interaction.response.send_message(embed=embed)
await interaction.response.send_message(embed=embed)
except Exception as e:
logger.error(str(e))
if "'NoneType' object is not subscriptable" in str(e):
await interaction.response.send_message(
f"The leaderboard '{leaderboard_name}' doesn't exist.",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be weird because there was already a db.get_leaderboard(...) call above.

ephemeral=True,
)
else:
await interaction.response.send_message(
"An unknown error occurred.", ephemeral=True
)
Loading
Loading