Skip to content
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
9 changes: 9 additions & 0 deletions src/models/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,15 @@ std::vector<int32_t> Tokenizer::EncodeBatch(std::span<const std::string> strings
}

std::shared_ptr<Tensor> Tokenizer::EncodeBatch(std::span<const char*> strings) const {
if (strings.empty()) {
throw std::runtime_error("EncodeBatch: input strings must not be empty");
}
for (size_t i = 0; i < strings.size(); i++) {
if (strings[i] == nullptr) {
throw std::runtime_error("EncodeBatch: input string at index " + std::to_string(i) + " must not be null");
}
}

Comment thread
kunal-vaishnavi marked this conversation as resolved.
std::vector<std::vector<int32_t>> sequences;
std::vector<std::span<const int32_t>> span_sequences;
for (size_t i = 0; i < strings.size(); i++) {
Expand Down
2 changes: 2 additions & 0 deletions src/ort_genai_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,8 @@ OgaResult* OGA_API_CALL OgaTokenizerEncode(const OgaTokenizer* tokenizer, const

OgaResult* OGA_API_CALL OgaTokenizerEncodeBatch(const OgaTokenizer* tokenizer, const char** strings, size_t count, OgaTensor** out) {
OGA_TRY
if (count > 0 && strings == nullptr)
Comment thread
kunal-vaishnavi marked this conversation as resolved.
throw std::runtime_error("EncodeBatch: strings pointer must not be null when count > 0");
auto tensor = tokenizer->EncodeBatch(std::span<const char*>(strings, count));
*out = ReturnShared<OgaTensor>(tensor);
return nullptr;
Expand Down
15 changes: 15 additions & 0 deletions test/c_api_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ TEST(CAPITests, TokenizerCAPI) {
#endif
}

TEST(CAPITests, EncodeBatchEmptyInputThrows) {
#if TEST_PHI2
auto model = OgaModel::Create(PHI2_PATH);
auto tokenizer = OgaTokenizer::Create(*model);

// EncodeBatch with zero strings should throw, not crash with SIGFPE
ASSERT_THROW(tokenizer->EncodeBatch(nullptr, 0), std::runtime_error);
Comment thread
Copilot marked this conversation as resolved.

// Invalid pointers with count > 0 should also be rejected deterministically.
ASSERT_THROW(tokenizer->EncodeBatch(nullptr, 1), std::runtime_error);
const char* bad_strings[] = {nullptr};
ASSERT_THROW(tokenizer->EncodeBatch(bad_strings, 1), std::runtime_error);
#endif
}

TEST(CAPITests, TokenizerUpdateOptions) {
#if TEST_PHI2
auto config = OgaConfig::Create(PHI2_PATH);
Expand Down
Loading