ember.cpp 0.1.0
C++ inference engine for Llama-family and paperkiln models (CPU, CUDA, WebAssembly)
Loading...
Searching...
No Matches
ember::nfc Namespace Reference

Namespaces

namespace  tables
 

Functions

uint8_t ccc (uint32_t c)
 
void decompose (uint32_t c, std::vector< uint32_t > &out)
 
uint32_t compose (uint32_t a, uint32_t b)
 
std::vector< uint32_t > nfc (const std::vector< uint32_t > &in)
 
std::string nfc_utf8 (const std::string &text)
 

Variables

constexpr uint32_t kSBase = 0xAC00
 
constexpr uint32_t kLBase = 0x1100
 
constexpr uint32_t kVBase = 0x1161
 
constexpr uint32_t kTBase = 0x11A7
 
constexpr uint32_t kLCount = 19
 
constexpr uint32_t kVCount = 21
 
constexpr uint32_t kTCount = 28
 
constexpr uint32_t kNCount = kVCount * kTCount
 
constexpr uint32_t kSCount = kLCount * kNCount
 

Function Documentation

◆ ccc()

uint8_t ember::nfc::ccc ( uint32_t  c)
inline

Definition at line 26 of file unicode_nfc.h.

26 {
27 if (c < tables::kCccCp[0]) return 0;
28 const uint32_t* end = tables::kCccCp + tables::kCccCount;
29 const uint32_t* it = std::lower_bound(tables::kCccCp, end, c);
30 return it != end && *it == c ? tables::kCccVal[it - tables::kCccCp] : 0;
31}
float c
size_t j

References c, j, ember::nfc::tables::kCccCount, ember::nfc::tables::kCccCp, and ember::nfc::tables::kCccVal.

Referenced by nfc().

◆ compose()

uint32_t ember::nfc::compose ( uint32_t  a,
uint32_t  b 
)
inline

Definition at line 56 of file unicode_nfc.h.

56 {
57 if (a >= kLBase && a < kLBase + kLCount && b >= kVBase && b < kVBase + kVCount)
58 return kSBase + ((a - kLBase) * kVCount + (b - kVBase)) * kTCount;
59 if (a >= kSBase && a < kSBase + kSCount && (a - kSBase) % kTCount == 0 && b > kTBase && b < kTBase + kTCount)
60 return a + (b - kTBase);
61 const uint64_t key = (static_cast<uint64_t>(a) << 32) | b;
62 const uint64_t* end = tables::kCompKey + tables::kCompCount;
63 const uint64_t* it = std::lower_bound(tables::kCompKey, end, key);
64 return it != end && *it == key ? tables::kCompVal[it - tables::kCompKey] : 0;
65}
std::vector< E > a
constexpr uint32_t kLBase
Definition unicode_nfc.h:22
constexpr uint32_t kTCount
Definition unicode_nfc.h:23
constexpr uint32_t kTBase
Definition unicode_nfc.h:22
constexpr uint32_t kSBase
Definition unicode_nfc.h:22

References a, j, ember::nfc::tables::kCompCount, ember::nfc::tables::kCompKey, ember::nfc::tables::kCompVal, kLBase, kSBase, kSCount, kTBase, kTCount, kVBase, and kVCount.

Referenced by nfc().

◆ decompose()

void ember::nfc::decompose ( uint32_t  c,
std::vector< uint32_t > &  out 
)
inline

Definition at line 34 of file unicode_nfc.h.

34 {
35 if (c >= kSBase && c < kSBase + kSCount) {
36 const uint32_t s = c - kSBase;
37 out.push_back(kLBase + s / kNCount);
38 out.push_back(kVBase + (s % kNCount) / kTCount);
39 if (s % kTCount) out.push_back(kTBase + s % kTCount);
40 return;
41 }
42 if (c >= tables::kDecompCp[0]) {
43 const uint32_t* end = tables::kDecompCp + tables::kDecompCount;
44 const uint32_t* it = std::lower_bound(tables::kDecompCp, end, c);
45 if (it != end && *it == c) {
46 const size_t i = static_cast<size_t>(it - tables::kDecompCp);
47 out.insert(out.end(), tables::kDecompData + tables::kDecompOff[i],
48 tables::kDecompData + tables::kDecompOff[i] + tables::kDecompLen[i]);
49 return;
50 }
51 }
52 out.push_back(c);
53}
node out

References c, j, ember::nfc::tables::kDecompCount, ember::nfc::tables::kDecompCp, ember::nfc::tables::kDecompData, ember::nfc::tables::kDecompLen, ember::nfc::tables::kDecompOff, kLBase, kNCount, kSBase, kSCount, kTBase, kTCount, kVBase, and out.

Referenced by nfc().

◆ nfc()

std::vector< uint32_t > ember::nfc::nfc ( const std::vector< uint32_t > &  in)
inline

Definition at line 68 of file unicode_nfc.h.

68 {
69 std::vector<uint32_t> d;
70 d.reserve(in.size() + 8);
71 for (uint32_t c : in) decompose(c, d);
72 // Canonical ordering: each run of non-starters stably sorted by class.
73 for (size_t i = 0; i < d.size();) {
74 if (ccc(d[i]) == 0) {
75 ++i;
76 continue;
77 }
78 size_t j = i;
79 while (j < d.size() && ccc(d[j]) != 0) ++j;
80 std::stable_sort(d.begin() + static_cast<std::ptrdiff_t>(i), d.begin() + static_cast<std::ptrdiff_t>(j),
81 [](uint32_t a, uint32_t b) { return ccc(a) < ccc(b); });
82 i = j;
83 }
84 // Canonical composition: c joins the last starter unless blocked (a character between them of
85 // class 0 or of class >= c's).
86 std::vector<uint32_t> out;
87 out.reserve(d.size());
88 size_t starter = 0;
89 bool have_starter = false;
90 int last = 0; // class of out.back()
91 for (uint32_t c : d) {
92 const int cc = ccc(c);
93 if (have_starter) {
94 const bool adjacent = out.size() == starter + 1;
95 if (adjacent || (last != 0 && last < cc)) {
96 if (const uint32_t p = compose(out[starter], c)) {
97 out[starter] = p;
98 continue;
99 }
100 }
101 }
102 if (cc == 0) {
103 starter = out.size();
104 have_starter = true;
105 }
106 last = cc;
107 out.push_back(c);
108 }
109 return out;
110}
const float * in[expr::Expr::kMaxInputs]
const float * p[kIrDepthSourcesMax]
void decompose(uint32_t c, std::vector< uint32_t > &out)
Definition unicode_nfc.h:34
uint8_t ccc(uint32_t c)
Definition unicode_nfc.h:26

References a, c, ccc(), compose(), decompose(), in, j, nfc(), out, and p.

Referenced by nfc(), and nfc_utf8().

◆ nfc_utf8()

std::string ember::nfc::nfc_utf8 ( const std::string &  text)
inline

Definition at line 113 of file unicode_nfc.h.

113 {
114 bool ascii = true;
115 for (unsigned char b : text)
116 if (b >= 0x80) {
117 ascii = false;
118 break;
119 }
120 if (ascii) return text;
121 const std::vector<uint32_t> cp = microtorch::llama3::cpts_from_utf8(text);
122 const std::vector<uint32_t> n = nfc(cp);
123 if (n == cp) return text;
124 std::string out;
125 out.reserve(text.size() + 4);
126 for (uint32_t c : n) microtorch::llama3::append_utf8(c, out);
127 return out;
128}
int n
std::string text
std::vector< uint32_t > cpts_from_utf8(const std::string &s)

References microtorch::llama3::append_utf8(), c, microtorch::llama3::cpts_from_utf8(), j, n, nfc(), out, and text.

Referenced by ember::QwenNfcTokenizer::encode().

Variable Documentation

◆ kLBase

constexpr uint32_t ember::nfc::kLBase = 0x1100

Definition at line 22 of file unicode_nfc.h.

Referenced by compose(), and decompose().

◆ kLCount

constexpr uint32_t ember::nfc::kLCount = 19
inlineconstexpr

Definition at line 23 of file unicode_nfc.h.

◆ kNCount

constexpr uint32_t ember::nfc::kNCount = kVCount * kTCount

Definition at line 23 of file unicode_nfc.h.

Referenced by decompose().

◆ kSBase

constexpr uint32_t ember::nfc::kSBase = 0xAC00
inlineconstexpr

Definition at line 22 of file unicode_nfc.h.

Referenced by compose(), and decompose().

◆ kSCount

constexpr uint32_t ember::nfc::kSCount = kLCount * kNCount

Definition at line 24 of file unicode_nfc.h.

Referenced by compose(), and decompose().

◆ kTBase

constexpr uint32_t ember::nfc::kTBase = 0x11A7

Definition at line 22 of file unicode_nfc.h.

Referenced by compose(), and decompose().

◆ kTCount

constexpr uint32_t ember::nfc::kTCount = 28

Definition at line 23 of file unicode_nfc.h.

Referenced by compose(), and decompose().

◆ kVBase

constexpr uint32_t ember::nfc::kVBase = 0x1161

Definition at line 22 of file unicode_nfc.h.

Referenced by compose(), and decompose().

◆ kVCount

constexpr uint32_t ember::nfc::kVCount = 21

Definition at line 23 of file unicode_nfc.h.

Referenced by compose().