Skip to content

Commit

Permalink
Use chrono instead of time_t.
Browse files Browse the repository at this point in the history
  • Loading branch information
popovaan committed Jul 23, 2024
1 parent d514de0 commit 48c06f4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
21 changes: 11 additions & 10 deletions src/cpp/src/block_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <memory>
#include <list>
#include <map>
#include <time.h>
#include <chrono>

#include "sequence_group.hpp"

Expand All @@ -16,15 +16,15 @@ class KVCacheBlock {
int m_index;
size_t m_hash;
size_t m_num_hashed_tokens;
time_t m_timestamp;
std::chrono::time_point<std::chrono::system_clock> m_timestamp;
public:
using Ptr = std::shared_ptr<KVCacheBlock>;
using CPtr = std::shared_ptr<const KVCacheBlock>;

explicit KVCacheBlock(int index)
: m_ref_count(0),
m_index(index),
m_timestamp(time(NULL)) { }
m_timestamp(std::chrono::system_clock::now()) { }

int get_index() const {
return m_index;
Expand Down Expand Up @@ -64,11 +64,11 @@ class KVCacheBlock {
m_num_hashed_tokens = num_hashed_tokens;
}

void set_timestamp(const time_t& timestamp) {
void set_timestamp(const std::chrono::time_point<std::chrono::system_clock>& timestamp) {
m_timestamp = timestamp;
}

time_t get_timestamp() {
std::chrono::time_point<std::chrono::system_clock> get_timestamp() {
return m_timestamp;
}
};
Expand All @@ -82,7 +82,7 @@ class Evicor {
}

static bool block_is_less(const std::pair<size_t, KVCacheBlock::Ptr>& lhs, const std::pair<size_t, KVCacheBlock::Ptr>& rhs) {
return difftime(lhs.second->get_timestamp(), rhs.second->get_timestamp()) > 0.0 ? false : true;
return lhs.second->get_timestamp() < rhs.second->get_timestamp();
}

KVCacheBlock::Ptr get_block(size_t hash) {
Expand All @@ -91,7 +91,7 @@ class Evicor {
return nullptr;
}
KVCacheBlock::Ptr block = blocks[hash];
block->set_timestamp(time(NULL));
block->set_timestamp(std::chrono::system_clock::now());
block->increment();
blocks.erase(hash);
return block;
Expand All @@ -103,7 +103,7 @@ class Evicor {
}
auto hash_block = std::min_element(std::begin(blocks), std::end(blocks), block_is_less);
auto block = hash_block->second;
block->set_timestamp(time(NULL));
block->set_timestamp(std::chrono::system_clock::now());
block->increment();
blocks.erase(hash_block->first);
return block;
Expand Down Expand Up @@ -214,6 +214,7 @@ class BlockAllocator {
}
if (cached_blocks.find(hash) != cached_blocks.end()) {
// use cashed block from cached_blocks
// TODO: add tokens validation in case of hash collision
block = cached_blocks[hash];
cached_blocks[hash]->increment();
return block;
Expand Down Expand Up @@ -540,7 +541,7 @@ class BlockManager {
auto hash = sequence->get_hash(content_len, prompt_ids);
auto block = m_allocator.get_cashed_block(hash, cached_blocks);
if (block != nullptr) {
block->set_timestamp(time(NULL));
block->set_timestamp(std::chrono::system_clock::now());
m_block_table[seq_id].push_back(block);
group->update_processed_tokens_num(content_len);
}
Expand All @@ -555,7 +556,7 @@ class BlockManager {
auto hash = sequence->get_hash(prev_iteration_content_len + i, prompt_ids);
auto block = m_allocator.get_cashed_block(hash, cached_blocks);
if (block != nullptr) {
block->set_timestamp(time(NULL));
block->set_timestamp(std::chrono::system_clock::now());
m_block_table[seq_id].push_back(block);
group->update_processed_tokens_num(prev_iteration_content_len + i);
break;
Expand Down
8 changes: 1 addition & 7 deletions tests/cpp/evictor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@
#include "openvino/runtime/core.hpp"
#include "scheduler.hpp"
#include <chrono>
#include <thread>

TEST(TestEvictor, general_test) {
ov::genai::Evicor evictor;
auto block0 = std::make_shared<ov::genai::KVCacheBlock>(0);
block0->set_hash(77, 1);
std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::seconds(1));
auto block1 = std::make_shared<ov::genai::KVCacheBlock>(1);
block1->set_hash(56, 2);
std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::seconds(1));
auto block2 = std::make_shared<ov::genai::KVCacheBlock>(2);
block2->set_hash(23, 3);
std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::seconds(1));
evictor.add(block0->get_hash(), block0);
evictor.add(block1->get_hash(), block1);
evictor.add(block2->get_hash(), block2);
Expand All @@ -38,13 +34,11 @@ TEST(TestEvictor, general_test) {

auto block3 = std::make_shared<ov::genai::KVCacheBlock>(7);
block3->set_hash(12, 4);
std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::seconds(1));
auto block4 = std::make_shared<ov::genai::KVCacheBlock>(10);
block4->set_hash(99, 5);
std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::seconds(1));
evictor.add(block3->get_hash(), block3);
evictor.add(block4->get_hash(), block4);
block2->set_timestamp(time(NULL));
block2->set_timestamp(std::chrono::system_clock::now());

EXPECT_EQ(evictor.get_lru_block()->get_index(), 7);
EXPECT_EQ(evictor.get_lru_block()->get_index(), 10);
Expand Down

0 comments on commit 48c06f4

Please sign in to comment.