Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backends/mlx/runtime/MLXSequenceCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ class MLXSequenceCache final : public cache::Cache,
int capacity() const override {
return capacity_;
}
int length() const {
int length() const override {
return length_;
}
// Includes persistent growth in a partially constructed forward.
Expand Down
3 changes: 3 additions & 0 deletions extension/llm/cache/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ class ET_EXPERIMENTAL SequenceControl : public CacheControl {

// Room for n more tokens within capacity.
virtual bool can_extend(int n = 1) const = 0;
// Where the sequence stands: one past its newest position, and so where it
// continues. Positions are dense from 0, so this is also how many it holds.
virtual int length() const = 0;
// Keep [0, position), dropping the rest. False = a target it has not
// reached, or one older than an evicting layer still retains.
virtual bool rewind(int position) = 0;
Expand Down
4 changes: 1 addition & 3 deletions extension/llm/cache/sequence_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,7 @@ class ET_EXPERIMENTAL SequenceCache : public Cache,
length_ = 0;
written_ = 0;
}
// Positions are dense from 0, so this is both what the sequence holds and
// where its next token goes.
int length() const {
int length() const override {
return length_;
}
// Whether rewind(position) would be accepted, without moving the length.
Expand Down
15 changes: 15 additions & 0 deletions extension/llm/cache/test/cache_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,21 @@ TEST_F(CacheTest, FaceRecoveryReturnsSameObject) {
EXPECT_EQ(plan->read[0].len, 1);
}

TEST_F(CacheTest, ControlFaceReportsTheLength) {
SequenceCache cache(CacheGeometry{{flat_layer()}}, config(8));
SequenceControl* control = static_cast<Cache&>(cache).as<SequenceControl>();
ASSERT_NE(control, nullptr);
EXPECT_EQ(control->length(), 0);
auto plan = cache.plan(0, 0, 3);
ASSERT_TRUE(plan.has_value());
cache.commit(*plan);
EXPECT_EQ(control->length(), 3);
ASSERT_TRUE(control->rewind(1));
EXPECT_EQ(control->length(), 1);
control->clear();
EXPECT_EQ(control->length(), 0);
}

TEST_F(CacheTest, NullFaceIdsNeverMatch) {
using executorch::extension::llm::cache::same_face;
EXPECT_FALSE(same_face(nullptr, nullptr));
Expand Down