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

add try and except to um.py #87

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
108 changes: 104 additions & 4 deletions lib/umbridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,32 @@ namespace umbridge {
if (!enable_parallel) {
model_lock.lock();
}
std::vector<std::vector<double>> outputs = model.Evaluate(inputs, config_json);
std::vector<std::vector<double>> outputs;
try{
outputs = model.Evaluate(inputs, config_json);
}
catch(const std::exception& e){

std::cerr << "Exception caught: " << e.what() << std::endl;

json response_body;
response_body["error"]["type"] = "InvalidEvaluation";
response_body["error"]["message"] = std::string("Model was unable to provide a valid evaluation due to ") + e.what();
res.set_content(response_body.dump(), "application/json");
res.status = 500;
return;
}
catch (...) {

std::cerr << "Caught an unknown error during evaluation." << std::endl;

json response_body;
response_body["error"]["type"] = "InvalidEvaluation";
response_body["error"]["message"] = std::string("Model was unable to provide a valid evaluation due to an unknown error");
res.set_content(response_body.dump(), "application/json");
res.status = 500;
return;
}

if (model_lock.owns_lock()) {
model_lock.unlock(); // for safety, although should unlock after request finished
Expand Down Expand Up @@ -521,7 +546,32 @@ namespace umbridge {
if (!enable_parallel) {
model_lock.lock();
}
std::vector<double> gradient = model.Gradient(outWrt, inWrt, inputs, sens, config_json);
std::vector<double> gradient;
try{
gradient = model.Gradient(outWrt, inWrt, inputs, sens, config_json);
}
catch(const std::exception& e){

std::cerr << "Exception caught: " << e.what() << std::endl;

json response_body;
response_body["error"]["type"] = "InvalidGradient";
response_body["error"]["message"] = std::string("Model was unable to provide a valid gradient due to ") + e.what();
res.set_content(response_body.dump(), "application/json");
res.status = 500;
return;
}
catch (...) {

std::cerr << "Caught an unknown error during the evaluation of gradient." << std::endl;

json response_body;
response_body["error"]["type"] = "InvalidGradient";
response_body["error"]["message"] = std::string("Model was unable to provide a valid gradient due to an unknown error");
res.set_content(response_body.dump(), "application/json");
res.status = 500;
return;
}

if (model_lock.owns_lock()) {
model_lock.unlock(); // for safety, although should unlock after request finished
Expand Down Expand Up @@ -570,7 +620,32 @@ namespace umbridge {
if (!enable_parallel) {
model_lock.lock();
}
std::vector<double> jacobian_action = model.ApplyJacobian(outWrt, inWrt, inputs, vec, config_json);
std::vector<double> jacobian_action;
try{
jacobian_action = model.ApplyJacobian(outWrt, inWrt, inputs, vec, config_json);
}
catch(const std::exception& e){

std::cerr << "Exception caught: " << e.what() << std::endl;

json response_body;
response_body["error"]["type"] = "InvalidJacobian";
response_body["error"]["message"] = std::string("Model was unable to provide a valid jacobian due to ") + e.what();
res.set_content(response_body.dump(), "application/json");
res.status = 500;
return;
}
catch (...) {

std::cerr << "Caught an unknown error during the evaluation of jacobian." << std::endl;

json response_body;
response_body["error"]["type"] = "InvalidJacobian";
response_body["error"]["message"] = std::string("Model was unable to provide a valid jacobian due to an unknown error");
res.set_content(response_body.dump(), "application/json");
res.status = 500;
return;
}

if (model_lock.owns_lock()) {
model_lock.unlock(); // for safety, although should unlock after request finished
Expand Down Expand Up @@ -623,7 +698,32 @@ namespace umbridge {
if (!enable_parallel) {
model_lock.lock();
}
std::vector<double> hessian_action = model.ApplyHessian(outWrt, inWrt1, inWrt2, inputs, sens, vec, config_json);
std::vector<double> hessian_action;
try{
hessian_action = model.ApplyHessian(outWrt, inWrt1, inWrt2, inputs, sens, vec, config_json);
}
catch(const std::exception& e){

std::cerr << "Exception caught: " << e.what() << std::endl;

json response_body;
response_body["error"]["type"] = "InvalidHessian";
response_body["error"]["message"] = std::string("Model was unable to provide a valid hessian due to ") + e.what();
res.set_content(response_body.dump(), "application/json");
res.status = 500;
return;
}
catch (...) {

std::cerr << "Caught an unknown error during the evaluation of hessian." << std::endl;

json response_body;
response_body["error"]["type"] = "InvalidHessian";
response_body["error"]["message"] = std::string("Model was unable to provide a valid hessian due to an unknown error");
res.set_content(response_body.dump(), "application/json");
res.status = 500;
return;
}

if (model_lock.owns_lock()) {
model_lock.unlock(); // for safety, although should unlock after request finished
Expand Down
38 changes: 27 additions & 11 deletions umbridge/um.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,13 @@ async def evaluate(request):
if len(parameters[i]) != input_sizes[i]:
return error_response("InvalidInput", f"Input parameter {i} has invalid length! Expected {input_sizes[i]} but got {len(parameters[i])}.", 400)

output_future = model_executor.submit(model.__call__, parameters, config)
output = await asyncio.wrap_future(output_future)

try:
output_future = model_executor.submit(model.__call__, parameters, config)
output = await asyncio.wrap_future(output_future)
except Exception as e:
print(traceback.format_exc())
return error_response("InvalidEvaluation", str(traceback.format_exc()), 500)

# Check if output is a list of lists
if not isinstance(output, list):
return error_response("InvalidOutput", "Model output is not a list of lists!", 500)
Expand Down Expand Up @@ -262,9 +266,13 @@ async def gradient(request):
if len(sens) != output_sizes[out_wrt]:
return error_response("InvalidInput", f"Sensitivity vector sens has invalid length! Expected {output_sizes[out_wrt]} but got {len(sens)}.", 400)

output_future = model_executor.submit(model.gradient, out_wrt, in_wrt, parameters, sens, config)
output = await asyncio.wrap_future(output_future)

try:
output_future = model_executor.submit(model.gradient, out_wrt, in_wrt, parameters, sens, config)
output = await asyncio.wrap_future(output_future)
except Exception as e:
print(traceback.format_exc())
return error_response("InvalidGradient", str(traceback.format_exc()), 500)

# Check if output is a list
if not isinstance(output, list):
return error_response("InvalidOutput", "Model output is not a list!", 500)
Expand Down Expand Up @@ -313,8 +321,12 @@ async def applyjacobian(request):
if len(vec) != input_sizes[in_wrt]:
return error_response("InvalidInput", f"Vector vec has invalid length! Expected {input_sizes[in_wrt]} but got {len(vec)}.", 400)

output_future = model_executor.submit(model.apply_jacobian, out_wrt, in_wrt, parameters, vec, config)
output = await asyncio.wrap_future(output_future)
try:
output_future = model_executor.submit(model.apply_jacobian, out_wrt, in_wrt, parameters, vec, config)
output = await asyncio.wrap_future(output_future)
except Exception as e:
print(traceback.format_exc())
return error_response("InvalidJacobian", str(traceback.format_exc()), 500)

# Check if output is a list
if not isinstance(output, list):
Expand Down Expand Up @@ -366,9 +378,13 @@ async def applyhessian(request):
if in_wrt2 < 0 or in_wrt2 >= len(input_sizes):
return error_response("InvalidInput", "Invalid inWrt2 index! Expected between 0 and number of inputs minus one, but got " + str(in_wrt2), 400)

output_future = model_executor.submit(model.apply_hessian, out_wrt, in_wrt1, in_wrt2, parameters, sens, vec, config)
output = await asyncio.wrap_future(output_future)

try:
output_future = model_executor.submit(model.apply_hessian, out_wrt, in_wrt1, in_wrt2, parameters, sens, vec, config)
output = await asyncio.wrap_future(output_future)
except Exception as e:
print(traceback.format_exc())
return error_response("InvalidHessian", str(traceback.format_exc()), 500)

# Check if output is a list
if not isinstance(output, list):
return error_response("InvalidOutput", "Model output is not a list!", 500)
Expand Down
Loading