text stringlengths 0 93.6k |
|---|
param_group['lr'] = learning_rate |
val_loss_old = val_loss # Update old validation loss |
if epoch % test_every == 0 or epoch == max_epochs-1: |
# Test |
test_time, test_loss, test_err_edges, test_err_tour, test_err_tsp, test_pred_tour_len, test_gt_tour_len = test(net, config, epoch_bar, mode='test') |
epoch_bar.write('T: ' + metrics_to_str(epoch, test_time, learning_rate, test_loss, test_err_edges, test_err_tour, test_err_tsp, test_pred_tour_len, test_gt_tour_len)) |
writer.add_scalar('loss/test_loss', test_loss, epoch) |
writer.add_scalar('pred_tour_len/test_pred_tour_len', test_pred_tour_len, epoch) |
writer.add_scalar('optimality_gap/test_opt_gap', test_pred_tour_len/test_gt_tour_len - 1, epoch) |
# Save training checkpoint at the end of epoch |
torch.save({ |
'epoch': epoch, |
'model_state_dict': net.state_dict(), |
'optimizer_state_dict': optimizer.state_dict(), |
'train_loss': train_loss, |
'val_loss': val_loss, |
}, log_dir+"last_train_checkpoint.tar") |
# Save checkpoint after every 250 epochs |
if epoch != 0 and (epoch % 250 == 0 or epoch == max_epochs-1): |
torch.save({ |
'epoch': epoch, |
'model_state_dict': net.state_dict(), |
'optimizer_state_dict': optimizer.state_dict(), |
'train_loss': train_loss, |
'val_loss': val_loss, |
}, log_dir+f"checkpoint_epoch{epoch}.tar") |
return net |
if __name__ == "__main__": |
main(config) |
# <FILESEP> |
from enum import Enum |
from fastapi import Request, FastAPI, HTTPException |
from fastapi.middleware.cors import CORSMiddleware |
import os |
import aiohttp |
import json |
from modal import Image, Mount, Secret, Stub, asgi_app |
from utils import pretty_log |
image = Image.debian_slim(python_version="3.10").pip_install("pynacl", "requests") |
discord_secrets = [Secret.from_name("discord-secret-fsdl")] |
stub = Stub( |
"askfsdl-discord", |
image=image, |
secrets=discord_secrets, |
mounts=[Mount.from_local_python_packages("utils")], |
) |
class DiscordInteractionType(Enum): |
PING = 1 # hello from Discord |
APPLICATION_COMMAND = 2 # an actual command |
class DiscordResponseType(Enum): |
PONG = 1 # hello back |
DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE = 5 # we'll send a message later |
class DiscordApplicationCommandOptionType(Enum): |
STRING = 3 # with language models, strings are all you need |
@stub.function( |
# keep one instance warm to reduce latency, consuming ~0.2 GB while idle |
# this costs ~$3/month at current prices, so well within $10/month free tier credit |
keep_warm=1, |
) |
@asgi_app(label="askfsdl-discord-bot") |
def app() -> FastAPI: |
app = FastAPI() |
app.add_middleware( |
CORSMiddleware, |
allow_origins=["*"], |
allow_credentials=True, |
allow_methods=["*"], |
allow_headers=["*"], |
) |
@app.post("/") |
async def handle_request(request: Request): |
"Verify incoming requests and if they're a valid command spawn a response." |
# while loading the body, check that it's a valid request from Discord |
body = await verify(request) |
data = json.loads(body.decode()) |
if data.get("type") == DiscordInteractionType.PING.value: |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.