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

Optimize utilize intermediate results #116

Merged
merged 6 commits into from
Sep 12, 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
4 changes: 2 additions & 2 deletions service/models/operations/calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
fetch_and_convert_static_interventions,
fetch_and_convert_dynamic_interventions,
)
from utils.rabbitmq import gen_rabbitmq_hook
from utils.rabbitmq import gen_calibrate_rabbitmq_hook
from utils.tds import fetch_dataset, fetch_model


Expand Down Expand Up @@ -77,7 +77,7 @@ def gen_pyciemss_args(self, job_id):

# TODO: Test RabbitMQ
try:
hook = gen_rabbitmq_hook(job_id)
hook = gen_calibrate_rabbitmq_hook(job_id)
except (socket.gaierror, AMQPConnectionError):
logging.warning(
"%s: Failed to connect to RabbitMQ. Unable to log progress", job_id
Expand Down
4 changes: 2 additions & 2 deletions service/models/operations/ensemble_calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from models.base import Dataset, OperationRequest, Timespan, ModelConfig
from models.converters import convert_to_solution_mapping
from utils.rabbitmq import gen_rabbitmq_hook
from utils.rabbitmq import gen_calibrate_rabbitmq_hook
from utils.tds import fetch_dataset, fetch_model


Expand Down Expand Up @@ -52,7 +52,7 @@ def gen_pyciemss_args(self, job_id):
dataset_path = fetch_dataset(self.dataset.dict(), job_id)

try:
hook = gen_rabbitmq_hook(job_id)
hook = gen_calibrate_rabbitmq_hook(job_id)
except (socket.gaierror, AMQPConnectionError):
logging.warning(
"%s: Failed to connect to RabbitMQ. Unable to log progress", job_id
Expand Down
20 changes: 20 additions & 0 deletions service/models/operations/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

from typing import ClassVar, List, Optional
from enum import Enum
from utils.rabbitmq import OptimizeHook
from pika.exceptions import AMQPConnectionError
import socket
import logging


import numpy as np
import torch
Expand Down Expand Up @@ -151,6 +156,20 @@ def gen_pyciemss_args(self, job_id):
if step_size is not None and solver_method == "euler":
solver_options["step_size"] = step_size

total_possible_iterations = (
extra_options.get("maxiter") + 1
) * extra_options.get("maxfeval")
try:
progress_hook = OptimizeHook(job_id, total_possible_iterations)
except (socket.gaierror, AMQPConnectionError):
logging.warning(
"%s: Failed to connect to RabbitMQ. Unable to log progress", job_id
)

# Log progress hook when unable to connect - for testing purposes.
def progress_hook(current_results):
logging.info(f"Optimize current results: {current_results.tolist()}")

return {
"model_path_or_json": amr_path,
"logging_step_size": self.logging_step_size,
Expand All @@ -173,6 +192,7 @@ def gen_pyciemss_args(self, job_id):
"n_samples_ouu": n_samples_ouu,
"solver_method": solver_method,
"solver_options": solver_options,
"progress_hook": progress_hook,
**extra_options,
}

Expand Down
38 changes: 36 additions & 2 deletions service/utils/rabbitmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def callback(ch, method, properties, body):
channel.start_consuming()


def gen_rabbitmq_hook(job_id):
def gen_calibrate_rabbitmq_hook(job_id):
connection = pika.BlockingConnection(
conn_config,
)
Expand All @@ -63,8 +63,42 @@ def hook(progress, loss):
exchange="",
routing_key="simulation-status",
body=json.dumps(
{"job_id": job_id, "progress": progress, "loss": str(loss)}
{
"job_id": job_id,
"type": "calibrate",
"progress": progress,
"loss": str(loss),
}
),
)

return hook


class OptimizeHook:
def __init__(self, job_id, total_possible_iterations):
connection = pika.BlockingConnection(
conn_config,
)
self.channel = connection.channel()
self.job_id = job_id
self.type = "optimize"
self.result = []
self.step = 0
self.total_possible_iterations = total_possible_iterations

def __call__(self, current_results):
self.step += 1
self.channel.basic_publish(
exchange="",
routing_key="simulation-status",
body=json.dumps(
{
"job_id": self.job_id,
"progress": self.step,
"type": self.type,
"current_results": current_results.tolist(),
"total_possible_iterations": self.total_possible_iterations,
}
),
)
Loading