From 0957b5daefe6d52430b269faab9414975ebad9d0 Mon Sep 17 00:00:00 2001 From: Zlobin Vladimir Date: Thu, 15 Aug 2024 14:44:07 +0400 Subject: [PATCH] Catch all exceptions, handle EOF (#775) Ticket 148739 --- .github/workflows/causal_lm_cpp.yml | 2 +- .../lcm_dreamshaper_v7/cpp/src/main.cpp | 8 +++++-- .../stable_diffusion_1_5/cpp/src/main.cpp | 8 +++++-- .../beam_search_causal_lm.cpp | 8 +++++-- .../cpp/benchmark_genai/benchmark_genai.cpp | 8 +++++-- samples/cpp/chat_sample/chat_sample.cpp | 21 +++++++++---------- .../continuous_batching_accuracy.cpp | 8 +++++-- .../continuous_batching_benchmark.cpp | 8 +++++-- .../cpp/greedy_causal_lm/greedy_causal_lm.cpp | 8 +++++-- .../multinomial_causal_lm.cpp | 8 +++++-- .../prompt_lookup_decoding_lm.cpp | 8 +++++-- .../speculative_decoding_lm.cpp | 8 +++++-- samples/python/chat_sample/chat_sample.py | 6 +++--- tests/python_tests/test_generate_api.py | 6 ------ 14 files changed, 74 insertions(+), 41 deletions(-) diff --git a/.github/workflows/causal_lm_cpp.yml b/.github/workflows/causal_lm_cpp.yml index 3b321ff408..f98ad5bfec 100644 --- a/.github/workflows/causal_lm_cpp.yml +++ b/.github/workflows/causal_lm_cpp.yml @@ -576,7 +576,7 @@ jobs: - name: Compare run: | source ./ov/setupvars.sh - printf 'What is 2 + 2?\nWhat is the previous answer?\nAdd 1 to it.\nSubtract 5 from it.\nWhy is the sun yellow?\nWhat was my first question?\nStop!\n' > ./input.txt + printf 'What is 2 + 2?\nWhat is the previous answer?\nAdd 1 to it.\nSubtract 5 from it.\nWhy is the sun yellow?\nWhat was my first question?\n' > ./input.txt timeout 30s ./build/samples/cpp/chat_sample/chat_sample ./TinyLlama-1.1B-Chat-v1.0/ < input.txt > ./pred.txt python -c " from transformers import LlamaTokenizer, AutoModelForCausalLM diff --git a/image_generation/lcm_dreamshaper_v7/cpp/src/main.cpp b/image_generation/lcm_dreamshaper_v7/cpp/src/main.cpp index e79082f547..bb1e1b1d22 100644 --- a/image_generation/lcm_dreamshaper_v7/cpp/src/main.cpp +++ b/image_generation/lcm_dreamshaper_v7/cpp/src/main.cpp @@ -404,9 +404,13 @@ int32_t main(int32_t argc, char* argv[]) try { return EXIT_SUCCESS; } catch (const std::exception& error) { - std::cerr << error.what() << '\n'; + try { + std::cerr << error.what() << '\n'; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } catch (...) { - std::cerr << "Non-exception object thrown\n"; + try { + std::cerr << "Non-exception object thrown\n"; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } diff --git a/image_generation/stable_diffusion_1_5/cpp/src/main.cpp b/image_generation/stable_diffusion_1_5/cpp/src/main.cpp index 8fbac91375..1550aa96f3 100644 --- a/image_generation/stable_diffusion_1_5/cpp/src/main.cpp +++ b/image_generation/stable_diffusion_1_5/cpp/src/main.cpp @@ -422,9 +422,13 @@ int32_t main(int32_t argc, char* argv[]) try { return EXIT_SUCCESS; } catch (const std::exception& error) { - std::cerr << error.what() << '\n'; + try { + std::cerr << error.what() << '\n'; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } catch (...) { - std::cerr << "Non-exception object thrown\n"; + try { + std::cerr << "Non-exception object thrown\n"; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } diff --git a/samples/cpp/beam_search_causal_lm/beam_search_causal_lm.cpp b/samples/cpp/beam_search_causal_lm/beam_search_causal_lm.cpp index 855ec58cb3..5f3187f33b 100644 --- a/samples/cpp/beam_search_causal_lm/beam_search_causal_lm.cpp +++ b/samples/cpp/beam_search_causal_lm/beam_search_causal_lm.cpp @@ -24,9 +24,13 @@ int main(int argc, char* argv[]) try { auto beams = pipe.generate(prompts, config); std::cout << beams << '\n'; } catch (const std::exception& error) { - std::cerr << error.what() << '\n'; + try { + std::cerr << error.what() << '\n'; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } catch (...) { - std::cerr << "Non-exception object thrown\n"; + try { + std::cerr << "Non-exception object thrown\n"; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } diff --git a/samples/cpp/benchmark_genai/benchmark_genai.cpp b/samples/cpp/benchmark_genai/benchmark_genai.cpp index 287d6b379a..8fadeac444 100644 --- a/samples/cpp/benchmark_genai/benchmark_genai.cpp +++ b/samples/cpp/benchmark_genai/benchmark_genai.cpp @@ -62,9 +62,13 @@ int main(int argc, char* argv[]) try { return 0; } catch (const std::exception& error) { - std::cerr << error.what() << '\n'; + try { + std::cerr << error.what() << '\n'; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } catch (...) { - std::cerr << "Non-exception object thrown\n"; + try { + std::cerr << "Non-exception object thrown\n"; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } diff --git a/samples/cpp/chat_sample/chat_sample.cpp b/samples/cpp/chat_sample/chat_sample.cpp index 1037cda329..827c08ae57 100644 --- a/samples/cpp/chat_sample/chat_sample.cpp +++ b/samples/cpp/chat_sample/chat_sample.cpp @@ -23,22 +23,21 @@ int main(int argc, char* argv[]) try { }; pipe.start_chat(); - for (;;) { - std::cout << "question:\n"; - - std::getline(std::cin, prompt); - if (prompt == "Stop!") - break; - + std::cout << "question:\n"; + while (std::getline(std::cin, prompt)) { pipe.generate(prompt, config, streamer); - - std::cout << "\n----------\n"; + std::cout << "\n----------\n" + "question:\n"; } pipe.finish_chat(); } catch (const std::exception& error) { - std::cerr << error.what() << '\n'; + try { + std::cerr << error.what() << '\n'; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } catch (...) { - std::cerr << "Non-exception object thrown\n"; + try { + std::cerr << "Non-exception object thrown\n"; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } diff --git a/samples/cpp/continuous_batching_accuracy/continuous_batching_accuracy.cpp b/samples/cpp/continuous_batching_accuracy/continuous_batching_accuracy.cpp index 77485e36db..32737f2736 100644 --- a/samples/cpp/continuous_batching_accuracy/continuous_batching_accuracy.cpp +++ b/samples/cpp/continuous_batching_accuracy/continuous_batching_accuracy.cpp @@ -111,9 +111,13 @@ int main(int argc, char* argv[]) try { std::cout << std::endl; } } catch (const std::exception& error) { - std::cerr << error.what() << '\n'; + try { + std::cerr << error.what() << '\n'; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } catch (...) { - std::cerr << "Non-exception object thrown\n"; + try { + std::cerr << "Non-exception object thrown\n"; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } diff --git a/samples/cpp/continuous_batching_benchmark/continuous_batching_benchmark.cpp b/samples/cpp/continuous_batching_benchmark/continuous_batching_benchmark.cpp index a687bd61d1..a51b83d759 100644 --- a/samples/cpp/continuous_batching_benchmark/continuous_batching_benchmark.cpp +++ b/samples/cpp/continuous_batching_benchmark/continuous_batching_benchmark.cpp @@ -521,9 +521,13 @@ int main(int argc, char* argv[]) try { std::cout << "Benchmark finished" << std::endl; } catch (const std::exception& error) { - std::cerr << error.what() << '\n'; + try { + std::cerr << error.what() << '\n'; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } catch (...) { - std::cerr << "Non-exception object thrown\n"; + try { + std::cerr << "Non-exception object thrown\n"; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } diff --git a/samples/cpp/greedy_causal_lm/greedy_causal_lm.cpp b/samples/cpp/greedy_causal_lm/greedy_causal_lm.cpp index 423aff9d7f..09e6af65e8 100644 --- a/samples/cpp/greedy_causal_lm/greedy_causal_lm.cpp +++ b/samples/cpp/greedy_causal_lm/greedy_causal_lm.cpp @@ -17,9 +17,13 @@ int main(int argc, char* argv[]) try { std::string result = pipe.generate(prompt, config); std::cout << result << std::endl; } catch (const std::exception& error) { - std::cerr << error.what() << '\n'; + try { + std::cerr << error.what() << '\n'; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } catch (...) { - std::cerr << "Non-exception object thrown\n"; + try { + std::cerr << "Non-exception object thrown\n"; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } diff --git a/samples/cpp/multinomial_causal_lm/multinomial_causal_lm.cpp b/samples/cpp/multinomial_causal_lm/multinomial_causal_lm.cpp index 7532b9dde1..1525cbc38a 100644 --- a/samples/cpp/multinomial_causal_lm/multinomial_causal_lm.cpp +++ b/samples/cpp/multinomial_causal_lm/multinomial_causal_lm.cpp @@ -28,9 +28,13 @@ int main(int argc, char* argv[]) try { // be printed each time a new token is generated. pipe.generate(prompt, config, streamer); } catch (const std::exception& error) { - std::cerr << error.what() << '\n'; + try { + std::cerr << error.what() << '\n'; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } catch (...) { - std::cerr << "Non-exception object thrown\n"; + try { + std::cerr << "Non-exception object thrown\n"; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } diff --git a/samples/cpp/prompt_lookup_decoding_lm/prompt_lookup_decoding_lm.cpp b/samples/cpp/prompt_lookup_decoding_lm/prompt_lookup_decoding_lm.cpp index 7b3de93df9..3419f3221a 100644 --- a/samples/cpp/prompt_lookup_decoding_lm/prompt_lookup_decoding_lm.cpp +++ b/samples/cpp/prompt_lookup_decoding_lm/prompt_lookup_decoding_lm.cpp @@ -333,9 +333,13 @@ int main(int argc, char* argv[]) try { // it is called for education purposes: model.reset_state(); } catch (const std::exception& error) { - std::cerr << error.what() << '\n'; + try { + std::cerr << error.what() << '\n'; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } catch (...) { - std::cerr << "Non-exception object thrown\n"; + try { + std::cerr << "Non-exception object thrown\n"; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } diff --git a/samples/cpp/speculative_decoding_lm/speculative_decoding_lm.cpp b/samples/cpp/speculative_decoding_lm/speculative_decoding_lm.cpp index 20ae1f9ecd..de2d2f8837 100644 --- a/samples/cpp/speculative_decoding_lm/speculative_decoding_lm.cpp +++ b/samples/cpp/speculative_decoding_lm/speculative_decoding_lm.cpp @@ -399,9 +399,13 @@ int main(int argc, char* argv[]) try { draft_model.reset_state(); main_model.reset_state(); } catch (const std::exception& error) { - std::cerr << error.what() << '\n'; + try { + std::cerr << error.what() << '\n'; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } catch (...) { - std::cerr << "Non-exception object thrown\n"; + try { + std::cerr << "Non-exception object thrown\n"; + } catch (const std::ios_base::failure&) {} return EXIT_FAILURE; } diff --git a/samples/python/chat_sample/chat_sample.py b/samples/python/chat_sample/chat_sample.py index 29e81026d6..eee66fb71d 100755 --- a/samples/python/chat_sample/chat_sample.py +++ b/samples/python/chat_sample/chat_sample.py @@ -26,11 +26,11 @@ def main(): pipe.start_chat() while True: - prompt = input('question:\n') - if 'Stop!' == prompt: + try: + prompt = input('question:\n') + except EOFError: break pipe.generate(prompt, config, streamer) - print('\n----------') pipe.finish_chat() diff --git a/tests/python_tests/test_generate_api.py b/tests/python_tests/test_generate_api.py index 8ede287229..9a02f506bb 100644 --- a/tests/python_tests/test_generate_api.py +++ b/tests/python_tests/test_generate_api.py @@ -162,12 +162,6 @@ def test_decoding(model_descr, generation_config, prompt): ] @pytest.mark.parametrize("inputs", input_tensors_list) @pytest.mark.parametrize("model_descr", get_models_list()) -@pytest.mark.xfail( - raises=TypeError, - reason="pybind was unable to find ov::Tensor from openvino yet", - strict=False, - condition=sys.platform in ["linux", "win32"] -) @pytest.mark.precommit @pytest.mark.nightly def test_ov_tensors(model_descr, inputs):