technician1 commited on
Commit
434c5af
·
1 Parent(s): 54ffa12

Upload 2 files

Browse files
Files changed (1) hide show
  1. ChatIPC.cpp +28 -13
ChatIPC.cpp CHANGED
@@ -1281,15 +1281,27 @@ static void learn_files_parallel(KnowledgeBase &kb, const std::vector<std::strin
1281
  static constexpr std::uint64_t KB_MAGIC = 0x434850434B535641ULL;
1282
  static constexpr std::uint64_t KB_VERSION = 2ULL;
1283
 
1284
- static void write_u64(std::ostream &os, std::uint64_t v){
1285
- os.write(reinterpret_cast<const char*>(&v), sizeof(v));
1286
- if(!os) throw std::runtime_error("write_u64 failed");
 
 
 
 
 
1287
  }
1288
 
1289
- static std::uint64_t read_u64(std::istream &is){
1290
  std::uint64_t v = 0;
1291
- is.read(reinterpret_cast<char*>(&v), sizeof(v));
1292
- if(!is) throw std::runtime_error("read_u64 failed");
 
 
 
 
 
 
 
1293
  return v;
1294
  }
1295
 
@@ -1521,7 +1533,7 @@ static void print_kb_info(const std::string &fname) {
1521
  return;
1522
  }
1523
 
1524
- std::cout << "Reckoning knowledge-base "<< fname << " info.\n";
1525
 
1526
  try {
1527
  const std::uint64_t magic = read_u64(ifs);
@@ -1535,7 +1547,7 @@ static void print_kb_info(const std::string &fname) {
1535
  const std::uint64_t N = read_u64(ifs);
1536
  if (N > (1ULL << 26)) throw std::runtime_error("save file is corrupted: pool too large");
1537
 
1538
- // Fast-skip string pool to reach the n-gram graph data
1539
  for (std::uint64_t i = 0; i < N; ++i){
1540
  std::uint64_t len = read_u64(ifs);
1541
  if (len > (1ULL << 30)) throw std::runtime_error("save file is corrupted: string too large");
@@ -1550,17 +1562,20 @@ static void print_kb_info(const std::string &fname) {
1550
  const std::uint64_t ctx_size = read_u64(ifs);
1551
  if (ctx_size > (1ULL << 16)) throw std::runtime_error("save file is corrupted: n-gram context too large");
1552
 
1553
- // The maximum context size stored represents the n-gram size
1554
  if (ctx_size > max_ngram) max_ngram = ctx_size;
1555
 
1556
- // Skip the context keys
1557
- ifs.seekg(static_cast<std::streamoff>(ctx_size * sizeof(std::uint64_t)), std::ios_base::cur);
 
 
1558
 
1559
  const std::uint64_t M = read_u64(ifs);
1560
  if (M > (1ULL << 26)) throw std::runtime_error("save file is corrupted: graph degree too large");
1561
 
1562
- // Skip the next-nodes values
1563
- ifs.seekg(static_cast<std::streamoff>(M * sizeof(std::uint64_t)), std::ios_base::cur);
 
 
1564
  }
1565
 
1566
  std::cout << fname << " dictionary depth: " << file_def_depth << "\n";
 
1281
  static constexpr std::uint64_t KB_MAGIC = 0x434850434B535641ULL;
1282
  static constexpr std::uint64_t KB_VERSION = 2ULL;
1283
 
1284
+ static void write_u64(std::ostream &os, std::uint64_t v) {
1285
+ do {
1286
+ unsigned char byte = v & 0x7F;
1287
+ v >>= 7;
1288
+ if (v != 0) byte |= 0x80;
1289
+ os.put(static_cast<char>(byte));
1290
+ } while (v != 0);
1291
+ if (!os) throw std::runtime_error("write_u64 failed");
1292
  }
1293
 
1294
+ static std::uint64_t read_u64(std::istream &is) {
1295
  std::uint64_t v = 0;
1296
+ unsigned int shift = 0;
1297
+ unsigned char byte;
1298
+ do {
1299
+ if (!is.read(reinterpret_cast<char*>(&byte), 1)) {
1300
+ throw std::runtime_error("read_u64 failed");
1301
+ }
1302
+ v |= (static_cast<std::uint64_t>(byte & 0x7F) << shift);
1303
+ shift += 7;
1304
+ } while (byte & 0x80);
1305
  return v;
1306
  }
1307
 
 
1533
  return;
1534
  }
1535
 
1536
+ std::cout << "Reckoning knowledge-base " << fname << " info.\n";
1537
 
1538
  try {
1539
  const std::uint64_t magic = read_u64(ifs);
 
1547
  const std::uint64_t N = read_u64(ifs);
1548
  if (N > (1ULL << 26)) throw std::runtime_error("save file is corrupted: pool too large");
1549
 
1550
+ // Dynamically read and skip strings since lengths are now variable-length encoded
1551
  for (std::uint64_t i = 0; i < N; ++i){
1552
  std::uint64_t len = read_u64(ifs);
1553
  if (len > (1ULL << 30)) throw std::runtime_error("save file is corrupted: string too large");
 
1562
  const std::uint64_t ctx_size = read_u64(ifs);
1563
  if (ctx_size > (1ULL << 16)) throw std::runtime_error("save file is corrupted: n-gram context too large");
1564
 
 
1565
  if (ctx_size > max_ngram) max_ngram = ctx_size;
1566
 
1567
+ // Sequentially read (and skip) context keys using LEB128 stream parsing
1568
+ for(std::uint64_t c = 0; c < ctx_size; ++c) {
1569
+ read_u64(ifs);
1570
+ }
1571
 
1572
  const std::uint64_t M = read_u64(ifs);
1573
  if (M > (1ULL << 26)) throw std::runtime_error("save file is corrupted: graph degree too large");
1574
 
1575
+ // Sequentially read (and skip) next-node values
1576
+ for(std::uint64_t j = 0; j < M; ++j){
1577
+ read_u64(ifs);
1578
+ }
1579
  }
1580
 
1581
  std::cout << fname << " dictionary depth: " << file_def_depth << "\n";