Skip to content

Commit

Permalink
Catch all exceptions, handle EOF (openvinotoolkit#775)
Browse files Browse the repository at this point in the history
Ticket 148739
  • Loading branch information
Wovchena authored Aug 15, 2024
1 parent aba7c24 commit 0957b5d
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/causal_lm_cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions image_generation/lcm_dreamshaper_v7/cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 6 additions & 2 deletions image_generation/stable_diffusion_1_5/cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 6 additions & 2 deletions samples/cpp/beam_search_causal_lm/beam_search_causal_lm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 6 additions & 2 deletions samples/cpp/benchmark_genai/benchmark_genai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
21 changes: 10 additions & 11 deletions samples/cpp/chat_sample/chat_sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 6 additions & 2 deletions samples/cpp/greedy_causal_lm/greedy_causal_lm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 6 additions & 2 deletions samples/cpp/multinomial_causal_lm/multinomial_causal_lm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 3 additions & 3 deletions samples/python/chat_sample/chat_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 0 additions & 6 deletions tests/python_tests/test_generate_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 0957b5d

Please sign in to comment.