| 293 | #endif |
| 294 | |
| 295 | void ZenFileSystem::initialize(const ZenFileSystemOptions& options) { |
| 296 | options_ = options; |
| 297 | per_kv_token_mem_size_ = bytesOfType(options.k_dtype) * options.per_k_token_ele / lanesOfType(options.k_dtype); |
| 298 | |
| 299 | // 1. Check if this working dirs exists |
| 300 | if (options_.record) { |
| 301 | if (!std::filesystem::exists(options.working_dir)) { std::filesystem::create_directory(options.working_dir); } |
| 302 | } |
| 303 | |
| 304 | // 2. Create index.json instance |
| 305 | auto ms = |
| 306 | std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()).time_since_epoch().count(); |
| 307 | index_["uuid"] = XXH64(&ms, sizeof(ms), 0); |
| 308 | index_["working_dir"] = options.working_dir; |
| 309 | index_["blob_bits_size"] = options.blob_bits_size; |
| 310 | index_["lane_bits_size"] = options.lane_bits; |
| 311 | index_["page_bits_size"] = options.page_bits; |
| 312 | index_["per_k_token_ele"] = options.per_k_token_ele; |
| 313 | index_["per_v_token_ele"] = options.per_v_token_ele; |
| 314 | index_["k_dtype"] = static_cast<int>(options.k_dtype); |
| 315 | index_["v_dtype"] = static_cast<int>(options.v_dtype); |
| 316 | index_["mmap_type"] = static_cast<int>(options.mmap_type); |
| 317 | |
| 318 | // 3. Validate options |
| 319 | if (options.per_k_token_ele != options.per_v_token_ele) { |
| 320 | MLLM_ERROR_EXIT(ExitCode::kCoreError, "per_k_token_ele != per_v_token_ele"); |
| 321 | } |
| 322 | if (options.k_dtype != options.v_dtype) { MLLM_ERROR_EXIT(ExitCode::kCoreError, "k_dtype != v_dtype"); } |
| 323 | MLLM_RT_ASSERT(options.page_bits + options.lane_bits < 32); |
| 324 | |
| 325 | // X. Create the first block Tensor for KV |
| 326 | { |
| 327 | size_t total_bits = (1 << (options_.page_bits + options_.lane_bits)); |
| 328 | size_t uint64_count = (total_bits + 64 - 1) / 64; |
| 329 | |
| 330 | // The first blob is in memory |
| 331 | blob_.insert({blob_.size(), ZenFSBlob{.data = Tensor::zeros({(1 << (options_.page_bits + options_.lane_bits)), |
| 332 | (int32_t)options.per_k_token_ele}, |
| 333 | options.k_dtype), |
| 334 | .type = ZenFSBlobType::kInMemory, |
| 335 | .mmap_file = std::nullopt, |
| 336 | .free_lanes_bits_map = std::vector<uint64_t>(uint64_count, ~0ULL)}}); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | void ZenFileSystem::finalize() { |
| 341 | // 1. Write to index.json |
no test coverage detected