MCPcopy Create free account
hub / github.com/0xShug0/audio.cpp / unicode_regex_split_custom_afmoe

Function unicode_regex_split_custom_afmoe

external/llama_tokenizer/unicode.cpp:818–889  ·  view source on GitHub ↗

AFMOE digit handling: splits digits with leading 1-2 based on total length modulo 3

Source from the content-addressed store, hash-verified

816
817// AFMOE digit handling: splits digits with leading 1-2 based on total length modulo 3
818static std::vector<size_t> unicode_regex_split_custom_afmoe(const std::string & text, const std::vector<size_t> & offsets) {
819 std::vector<size_t> bpe_offsets;
820 bpe_offsets.reserve(offsets.size());
821
822 const auto cpts = unicode_cpts_from_utf8(text);
823
824 size_t start = 0;
825 for (auto offset : offsets) {
826 const size_t offset_ini = start;
827 const size_t offset_end = start + offset;
828 assert(offset_end <= cpts.size());
829 start = offset_end;
830
831 auto _get_flags = [&] (const size_t pos) -> unicode_cpt_flags {
832 return (offset_ini <= pos && pos < offset_end) ? unicode_cpt_flags_from_cpt(cpts[pos]) : unicode_cpt_flags{};
833 };
834
835 size_t _prev_end = offset_ini;
836 auto _add_token = [&] (const size_t end) -> size_t {
837 assert(_prev_end <= end && end <= offset_end);
838 size_t len = end - _prev_end;
839 if (len > 0) {
840 bpe_offsets.push_back(len);
841 }
842 _prev_end = end;
843 return len;
844 };
845
846 for (size_t pos = offset_ini; pos < offset_end; ) {
847 const auto flags = _get_flags(pos);
848
849 // Handle digit sequences with special splitting logic
850 if (flags.is_number) {
851 size_t digit_start = pos;
852 size_t digit_count = 0;
853
854 // Count consecutive digits
855 while (_get_flags(pos).is_number && pos < offset_end) {
856 digit_count++;
857 pos++;
858 }
859
860 // Split based on total length modulo 3
861 size_t remainder = digit_count % 3;
862 size_t current = digit_start;
863
864 // Emit leading 1-2 digits if needed
865 if (remainder > 0) {
866 _add_token(current + remainder);
867 current += remainder;
868 }
869
870 // Emit groups of 3
871 while (current < digit_start + digit_count) {
872 _add_token(current + 3);
873 current += 3;
874 }
875 continue;

Callers 1

Calls 3

unicode_cpts_from_utf8Function · 0.85
sizeMethod · 0.45

Tested by

no test coverage detected