mirror of
https://github.com/opencv/opencv.git
synced 2026-07-31 08:13:04 +04:00
Merge pull request #28837 from varun-jaiswal17:gemma3-tokenizer
Add Gemma3 tokenizer support for dnn #28837 - Adds Gemma3 tokenizer support - Implements character-level BPE - Adds 6 tests covering English, phrase, mixed case, numbers, special tokens, and encode/decode - add gemma3_inference.py Merge with: - **Companion PR** : https://github.com/opencv/opencv_extra/pull/1346 - forward pass bug in gemma3_inference.py : https://github.com/opencv/opencv/pull/28836 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
@@ -527,7 +527,33 @@ public:
|
||||
case 't' : { buf[i++] = '\t'; break; }
|
||||
case 'b' : { buf[i++] = '\b'; break; }
|
||||
case 'f' : { buf[i++] = '\f'; break; }
|
||||
case 'u' : { CV_PARSE_ERROR_CPP( "'\\uXXXX' currently not supported" ); break; }
|
||||
case 'u' : {
|
||||
if (i + 4 >= CV_FS_MAX_LEN)
|
||||
CV_PARSE_ERROR_CPP("string is too long");
|
||||
ptr++;
|
||||
uint32_t codepoint = 0;
|
||||
for (int k = 0; k < 4; k++, ptr++) {
|
||||
char hex = *ptr;
|
||||
uint32_t digit = 0;
|
||||
if (hex >= '0' && hex <= '9') digit = (uint32_t)(hex - '0');
|
||||
else if (hex >= 'a' && hex <= 'f') digit = (uint32_t)(hex - 'a') + 10u;
|
||||
else if (hex >= 'A' && hex <= 'F') digit = (uint32_t)(hex - 'A') + 10u;
|
||||
else CV_PARSE_ERROR_CPP("invalid \\uXXXX escape sequence");
|
||||
codepoint = (codepoint << 4) | digit;
|
||||
}
|
||||
if (codepoint < 0x80) {
|
||||
buf[i++] = (char)codepoint;
|
||||
} else if (codepoint < 0x800) {
|
||||
buf[i++] = (char)(0xC0 | (codepoint >> 6));
|
||||
buf[i++] = (char)(0x80 | (codepoint & 0x3F));
|
||||
} else {
|
||||
buf[i++] = (char)(0xE0 | (codepoint >> 12));
|
||||
buf[i++] = (char)(0x80 | ((codepoint >> 6) & 0x3F));
|
||||
buf[i++] = (char)(0x80 | (codepoint & 0x3F));
|
||||
}
|
||||
beg = ptr;
|
||||
continue;
|
||||
}
|
||||
default : { CV_PARSE_ERROR_CPP( "Invalid escape character" ); }
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1685,6 +1685,53 @@ TEST(Core_InputOutput, FileStorage_json_bool)
|
||||
fs.release();
|
||||
}
|
||||
|
||||
TEST(Core_InputOutput, FileStorage_json_unicode_escape)
|
||||
{
|
||||
// Test \uXXXX Unicode escape sequences in JSON strings
|
||||
std::string test = R"({
|
||||
"ascii": "\u0041\u0042\u0043",
|
||||
"copyright": "\u00A9",
|
||||
"chinese": "\u4E2D",
|
||||
"emoji_base": "\u263A",
|
||||
"mixed": "Hello \u4E16\u754C"
|
||||
})";
|
||||
FileStorage fs(test, FileStorage::READ | FileStorage::MEMORY);
|
||||
|
||||
// Test ASCII characters (\u0041=A, \u0042=B, \u0043=C)
|
||||
ASSERT_TRUE(fs["ascii"].isString());
|
||||
ASSERT_EQ((std::string)fs["ascii"], "ABC");
|
||||
|
||||
// Test 2-byte UTF-8 character (\u00A9=©)
|
||||
ASSERT_TRUE(fs["copyright"].isString());
|
||||
std::string copyright_str = (std::string)fs["copyright"];
|
||||
ASSERT_EQ(copyright_str.size(), 2u); // © is 2 bytes in UTF-8
|
||||
ASSERT_EQ((unsigned char)copyright_str[0], 0xC2);
|
||||
ASSERT_EQ((unsigned char)copyright_str[1], 0xA9);
|
||||
|
||||
// Test 3-byte UTF-8 character (\u4E2D=中)
|
||||
ASSERT_TRUE(fs["chinese"].isString());
|
||||
std::string chinese_str = (std::string)fs["chinese"];
|
||||
ASSERT_EQ(chinese_str.size(), 3u); // 中 is 3 bytes in UTF-8
|
||||
ASSERT_EQ((unsigned char)chinese_str[0], 0xE4);
|
||||
ASSERT_EQ((unsigned char)chinese_str[1], 0xB8);
|
||||
ASSERT_EQ((unsigned char)chinese_str[2], 0xAD);
|
||||
|
||||
// Test another 3-byte character (\u263A=☺)
|
||||
ASSERT_TRUE(fs["emoji_base"].isString());
|
||||
std::string emoji_str = (std::string)fs["emoji_base"];
|
||||
ASSERT_EQ(emoji_str.size(), 3u);
|
||||
ASSERT_EQ((unsigned char)emoji_str[0], 0xE2);
|
||||
ASSERT_EQ((unsigned char)emoji_str[1], 0x98);
|
||||
ASSERT_EQ((unsigned char)emoji_str[2], 0xBA);
|
||||
|
||||
// Test mixed ASCII and Unicode
|
||||
ASSERT_TRUE(fs["mixed"].isString());
|
||||
std::string mixed = (std::string)fs["mixed"];
|
||||
ASSERT_EQ(mixed.substr(0, 6), "Hello "); // First 6 chars are "Hello "
|
||||
|
||||
fs.release();
|
||||
}
|
||||
|
||||
TEST(Core_InputOutput, FileStorage_free_file_after_exception)
|
||||
{
|
||||
const std::string fileName = cv::tempfile("FileStorage_free_file_after_exception_test.yml");
|
||||
|
||||
Reference in New Issue
Block a user