From b50118bde89f48eef18b9fc518a687657dc71077 Mon Sep 17 00:00:00 2001 From: monabraeunig <138789374+monabraeunig@users.noreply.github.com> Date: Wed, 13 Nov 2024 14:34:51 +0100 Subject: [PATCH 1/5] add try and except to um.py --- umbridge/um.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/umbridge/um.py b/umbridge/um.py index ee52588..2f193e6 100755 --- a/umbridge/um.py +++ b/umbridge/um.py @@ -206,8 +206,11 @@ 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(f"An error occured during the evaluation: {e}") # Check if output is a list of lists if not isinstance(output, list): From c66334dd971b5715913740378b4206b0ea703ad9 Mon Sep 17 00:00:00 2001 From: monabraeunig <138789374+monabraeunig@users.noreply.github.com> Date: Wed, 13 Nov 2024 15:32:54 +0100 Subject: [PATCH 2/5] Update um.py --- umbridge/um.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/umbridge/um.py b/umbridge/um.py index 2f193e6..b50ddb5 100755 --- a/umbridge/um.py +++ b/umbridge/um.py @@ -210,7 +210,7 @@ async def evaluate(request): output_future = model_executor.submit(model.__call__, parameters, config) output = await asyncio.wrap_future(output_future) except Exception as e: - print(f"An error occured during the evaluation: {e}") + return error_response("EvaluationError", str(e), 500) # Check if output is a list of lists if not isinstance(output, list): From 1ff768f385024924b28f3c256595c0d723ec4f9e Mon Sep 17 00:00:00 2001 From: monabraeunig <138789374+monabraeunig@users.noreply.github.com> Date: Wed, 13 Nov 2024 15:41:38 +0100 Subject: [PATCH 3/5] Update um.py --- umbridge/um.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/umbridge/um.py b/umbridge/um.py index b50ddb5..834070c 100755 --- a/umbridge/um.py +++ b/umbridge/um.py @@ -265,8 +265,11 @@ 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: + return error_response("GradientComputationError", str(e), 500) # Check if output is a list if not isinstance(output, list): @@ -316,8 +319,11 @@ 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: + return error_response("JacobianComputationError", str(e), 500) # Check if output is a list if not isinstance(output, list): @@ -369,8 +375,11 @@ 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: + return error_response("HessianComputationError", str(e), 500) # Check if output is a list if not isinstance(output, list): From a5ce29713beacc902129262e761d42df8c97e9c8 Mon Sep 17 00:00:00 2001 From: monabraeunig <138789374+monabraeunig@users.noreply.github.com> Date: Thu, 26 Dec 2024 19:43:15 +0100 Subject: [PATCH 4/5] Update um.py --- umbridge/um.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/umbridge/um.py b/umbridge/um.py index 834070c..91d9a9a 100755 --- a/umbridge/um.py +++ b/umbridge/um.py @@ -210,8 +210,9 @@ async def evaluate(request): output_future = model_executor.submit(model.__call__, parameters, config) output = await asyncio.wrap_future(output_future) except Exception as e: - return error_response("EvaluationError", str(e), 500) - + 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) @@ -269,8 +270,9 @@ async def gradient(request): 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: - return error_response("GradientComputationError", str(e), 500) - + 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) @@ -323,7 +325,8 @@ async def applyjacobian(request): 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: - return error_response("JacobianComputationError", str(e), 500) + print(traceback.format_exc()) + return error_response("InvalidJacobian", str(traceback.format_exc()), 500) # Check if output is a list if not isinstance(output, list): @@ -379,8 +382,9 @@ async def applyhessian(request): 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: - return error_response("HessianComputationError", str(e), 500) - + 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) From e4f7f568a7b0217102facdf36a3f056aaa548199 Mon Sep 17 00:00:00 2001 From: monabraeunig <138789374+monabraeunig@users.noreply.github.com> Date: Sat, 11 Jan 2025 18:48:07 +0100 Subject: [PATCH 5/5] Update umbridge.h Evaluate, Gradient, Jacobian , Hessian error --- lib/umbridge.h | 108 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 4 deletions(-) diff --git a/lib/umbridge.h b/lib/umbridge.h index a779112..ccf2356 100644 --- a/lib/umbridge.h +++ b/lib/umbridge.h @@ -466,7 +466,32 @@ namespace umbridge { if (!enable_parallel) { model_lock.lock(); } - std::vector> outputs = model.Evaluate(inputs, config_json); + std::vector> 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 @@ -521,7 +546,32 @@ namespace umbridge { if (!enable_parallel) { model_lock.lock(); } - std::vector gradient = model.Gradient(outWrt, inWrt, inputs, sens, config_json); + std::vector 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 @@ -570,7 +620,32 @@ namespace umbridge { if (!enable_parallel) { model_lock.lock(); } - std::vector jacobian_action = model.ApplyJacobian(outWrt, inWrt, inputs, vec, config_json); + std::vector 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 @@ -623,7 +698,32 @@ namespace umbridge { if (!enable_parallel) { model_lock.lock(); } - std::vector hessian_action = model.ApplyHessian(outWrt, inWrt1, inWrt2, inputs, sens, vec, config_json); + std::vector 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