Skip to content
Merged
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
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,12 @@ if (FREESCHED_BUILD_TESTS)
target_compile_options(test_moe_stream PRIVATE -Wall -Wextra -Werror -fno-exceptions -fno-rtti)
add_test(NAME test_moe_stream COMMAND test_moe_stream)

add_executable(test_slot_retirement tests/test_slot_retirement.cpp)
target_link_libraries(test_slot_retirement PRIVATE freesched)
target_compile_features(test_slot_retirement PRIVATE cxx_std_17)
target_compile_options(test_slot_retirement PRIVATE -Wall -Wextra -Werror -fno-exceptions -fno-rtti)
add_test(NAME test_slot_retirement COMMAND test_slot_retirement)

add_executable(test_route_replay tests/test_route_replay.cpp)
target_link_libraries(test_route_replay PRIVATE freesched)
target_compile_features(test_route_replay PRIVATE cxx_std_17)
Expand All @@ -458,7 +464,7 @@ if (FREESCHED_BUILD_TESTS)
add_test(NAME test_fsbench COMMAND test_fsbench)
set_tests_properties(test_fsbench PROPERTIES ENVIRONMENT "FSBENCH_BIN=$<TARGET_FILE:fsbench>")

set_tests_properties(test_c11_abi test_freesched test_adreno663_iq9075 test_pocketcoder test_moe_stream test_route_replay
set_tests_properties(test_c11_abi test_freesched test_adreno663_iq9075 test_pocketcoder test_moe_stream test_slot_retirement test_route_replay
test_ggml_zerocopy_hardening
PROPERTIES ENVIRONMENT "FREESCHED_MOCK=1")

Expand Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ TEST_IQ := $(BUILD_DIR)/test_adreno663_iq9075
TEST_VKPR := $(BUILD_DIR)/test_adreno663_probe
TEST_PC := $(BUILD_DIR)/test_pocketcoder
TEST_MOE := $(BUILD_DIR)/test_moe_stream
TEST_RETIRE:= $(BUILD_DIR)/test_slot_retirement
TEST_RR := $(BUILD_DIR)/test_route_replay
TEST_GGML := $(BUILD_DIR)/test_ggml_zerocopy_hardening
TEST_FSB := $(BUILD_DIR)/test_fsbench
Expand Down Expand Up @@ -119,7 +120,7 @@ ANDROID_OBJS := $(patsubst $(THIS_DIR)/src/%.cpp,$(ANDROID_BUILD)/obj/%.o,$(

.PHONY: all lib test twin-test smoke bench verify-headers audit verify android-arm64 clean

all: lib $(TEST_C11) $(TEST_CXX) $(TEST_IQ) $(TEST_VKPR) $(TEST_PC) $(TEST_MOE) $(TEST_RR) $(TEST_FSB) $(TEST_GGML) $(TEST_FH) $(TEST_FHFR) $(TEST_DSP) $(DSP_BENCH) $(TEST_LLM) $(LLM_RUNNER) $(TEST_HMX) $(HMX_BENCH) $(TEST_VLA) $(VLA_BENCH) $(TEST_V81) $(V81_VLA) $(TEST_V81T) $(V81T_VLA) $(FH_LIVE) $(BENCH_BIN) $(MOE_BENCH) $(SMOKE_BIN) $(VKPR_BIN) $(TEST_SPIRV) $(SPIRV_BENCH) $(PC_BIN) $(RR_BIN) $(FSB_BIN) $(GGML_EX)
all: lib $(TEST_C11) $(TEST_CXX) $(TEST_IQ) $(TEST_VKPR) $(TEST_PC) $(TEST_MOE) $(TEST_RETIRE) $(TEST_RR) $(TEST_FSB) $(TEST_GGML) $(TEST_FH) $(TEST_FHFR) $(TEST_DSP) $(DSP_BENCH) $(TEST_LLM) $(LLM_RUNNER) $(TEST_HMX) $(HMX_BENCH) $(TEST_VLA) $(VLA_BENCH) $(TEST_V81) $(V81_VLA) $(TEST_V81T) $(V81T_VLA) $(FH_LIVE) $(BENCH_BIN) $(MOE_BENCH) $(SMOKE_BIN) $(VKPR_BIN) $(TEST_SPIRV) $(SPIRV_BENCH) $(PC_BIN) $(RR_BIN) $(FSB_BIN) $(GGML_EX)

lib: $(LIB)

Expand Down Expand Up @@ -226,6 +227,10 @@ $(TEST_MOE): $(THIS_DIR)/tests/test_moe_stream.cpp $(LIB) $(HDRS)
@mkdir -p $(dir $@)
$(HOST_CXX) $(CXXFLAGS) $< $(LIB) -o $@ $(LDFLAGS)

$(TEST_RETIRE): $(THIS_DIR)/tests/test_slot_retirement.cpp $(LIB) $(HDRS)
@mkdir -p $(dir $@)
$(HOST_CXX) $(CXXFLAGS) $< $(LIB) -o $@ $(LDFLAGS)

$(TEST_RR): $(THIS_DIR)/tests/test_route_replay.cpp $(LIB) $(HDRS)
@mkdir -p $(dir $@)
$(HOST_CXX) $(CXXFLAGS) $< $(LIB) -o $@ $(LDFLAGS)
Expand Down Expand Up @@ -316,7 +321,7 @@ verify-headers: $(THIS_DIR)/include/freesched.h $(THIS_DIR)/include/fs_vk_adreno
$(HOST_CC) -x c -std=c11 -pedantic -fsyntax-only -Wall -Wextra -Werror -I$(THIS_DIR)/include $(THIS_DIR)/include/fs_vk_adreno_spirv.h
$(HOST_CXX) -x c++ -std=c++17 -pedantic -fsyntax-only -Wall -Wextra -Werror -I$(THIS_DIR)/include $(THIS_DIR)/include/fs_vk_adreno_spirv.h

test: $(TEST_C11) $(TEST_CXX) $(TEST_IQ) $(TEST_VKPR) $(TEST_PC) $(TEST_MOE) $(TEST_RR) $(TEST_FSB) $(TEST_GGML) $(TEST_FH) $(TEST_FHFR) $(TEST_DSP) $(DSP_BENCH) $(TEST_LLM) $(LLM_RUNNER) $(TEST_HMX) $(HMX_BENCH) $(TEST_VLA) $(VLA_BENCH) $(TEST_V81) $(V81_VLA) $(TEST_V81T) $(V81T_VLA) $(FH_LIVE) $(BENCH_BIN) $(MOE_BENCH) $(SMOKE_BIN) $(VKPR_BIN) $(TEST_SPIRV) $(SPIRV_BENCH) $(PC_BIN) $(RR_BIN) $(FSB_BIN) $(GGML_EX) twin-test
test: $(TEST_C11) $(TEST_CXX) $(TEST_IQ) $(TEST_VKPR) $(TEST_PC) $(TEST_MOE) $(TEST_RETIRE) $(TEST_RR) $(TEST_FSB) $(TEST_GGML) $(TEST_FH) $(TEST_FHFR) $(TEST_DSP) $(DSP_BENCH) $(TEST_LLM) $(LLM_RUNNER) $(TEST_HMX) $(HMX_BENCH) $(TEST_VLA) $(VLA_BENCH) $(TEST_V81) $(V81_VLA) $(TEST_V81T) $(V81T_VLA) $(FH_LIVE) $(BENCH_BIN) $(MOE_BENCH) $(SMOKE_BIN) $(VKPR_BIN) $(TEST_SPIRV) $(SPIRV_BENCH) $(PC_BIN) $(RR_BIN) $(FSB_BIN) $(GGML_EX) twin-test
FREESCHED_MOCK=1 $(TEST_C11)
FREESCHED_MOCK=1 $(TEST_CXX)
FREESCHED_MOCK=1 $(TEST_IQ)
Expand All @@ -341,6 +346,7 @@ test: $(TEST_C11) $(TEST_CXX) $(TEST_IQ) $(TEST_VKPR) $(TEST_PC) $(TEST_MOE) $(T
$(FH_LIVE) --mock enotty
FREESCHED_MOCK=1 $(TEST_PC)
FREESCHED_MOCK=1 $(TEST_MOE)
FREESCHED_MOCK=1 $(TEST_RETIRE)
FREESCHED_MOCK=1 $(TEST_RR)
FREESCHED_MOCK=1 $(TEST_GGML)
FSBENCH_BIN=$(FSB_BIN) $(TEST_FSB)
Expand Down
145 changes: 143 additions & 2 deletions include/fs_moe_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,62 @@ typedef struct fs_moe_stream_config {
uint32_t miss_tile_max; /* <= FS_MOE_MISS_TILE_MAX (0 -> 8) */
} fs_moe_stream_config_t;

/*
* Accelerator consumer lease token (generation-safe slot retirement, #18).
* `generation` identifies the slot reservation that published the bytes and
* `token_id` uniquely identifies this one consumer reference: retirement via
* fs_moe_streamer_release_lease() succeeds exactly once per token_id, so a
* stale copy can never retire another consumer's reference. The slot's
* backing storage is never recycled or mutated while any lease token for its
* current generation is live.
*/
typedef struct fs_slot_lease {
uint32_t slot_idx;
uint32_t expert_id;
uint64_t generation; /* Monotonic lease token catching double-release / use-after-release */
uint64_t generation; /* Slot reservation generation that published these bytes */
void* data_ptr; /* 2 MiB-aligned slot base pointer */
uint64_t byte_size; /* Valid expert_stride bytes */
int is_speculative;/* 1 if loaded into transient speculative slot pending commit */
uint64_t token_id; /* Unique accelerator consumer retirement token (0 = retired) */
} fs_slot_lease_t;

/*
* Generation-safe slot reservation (#18). A reservation alone is NOT a
* lifetime: it names one (slot_idx, generation) pair and stays valid only
* while that generation is the slot's current reservation. Retiring every
* reference of a generation (its I/O token plus every accelerator lease
* token) is what permits the slot's backing storage to be recycled.
*/
typedef struct fs_slot_reservation {
uint32_t slot_idx;
uint32_t expert_id;
uint64_t generation; /* Monotonic per-slot reservation generation (0 = invalid) */
int needs_io; /* 1 if a miss: caller must run & complete the I/O token */
int is_speculative;/* 1 if reserved from the transient speculative staging pool */
} fs_slot_reservation_t;

/*
* I/O completion/cancel retirement token (#18), distinct from accelerator
* lease tokens. Readers write ONLY into `staging_ptr` (never directly into
* slot storage); fs_moe_streamer_complete_io() publishes staging bytes into
* the slot solely if this token is still the slot's live I/O reference for
* its current generation and the reservation was not cancelled/evicted.
* Exactly one of fs_moe_streamer_complete_io() / fs_moe_streamer_retire_io()
* may retire a token; any later (stale or duplicate) call is rejected with
* FS_ERR_OWNERSHIP and publishes nothing. Only the token holder retires it:
* cancellation/eviction merely mark the reservation, so in-flight staging
* writes never race a recycled slot.
*/
typedef struct fs_io_token {
uint32_t slot_idx;
uint32_t expert_id;
uint64_t generation; /* Reservation generation this read was issued for */
uint64_t token_id; /* Unique I/O retirement token (0 = retired / no I/O) */
void* staging_ptr; /* Reader-owned staging buffer (byte_size bytes) */
uint64_t byte_size; /* Valid expert_stride bytes to read */
uint64_t file_offset; /* Absolute .fspack byte offset of the expert blob */
} fs_io_token_t;

typedef struct fs_moe_stream_stats {
uint64_t total_requests;
uint64_t cache_hits;
Expand All @@ -188,6 +235,13 @@ typedef struct fs_moe_stream_stats {
uint32_t active_leases;
uint32_t resident_slots;
fs_io_backend_mode_t active_io_mode;
/* Generation-safe retirement accounting (#18) */
uint64_t stale_io_discards; /* Completions discarded: generation mismatch / cancelled */
uint64_t io_retirements; /* I/O tokens retired exactly once */
uint64_t accel_retirements; /* Accelerator lease tokens retired exactly once */
uint64_t deferred_recycles; /* Cancel/evict marks deferred by live I/O or consumer refs */
uint64_t slot_recycles; /* Slots recycled after I/O + all consumers retired */
uint32_t pending_retire_slots;/* Slots marked for retirement, awaiting refs to drain */
} fs_moe_stream_stats_t;

typedef struct fs_moe_streamer fs_moe_streamer_t;
Expand All @@ -213,11 +267,98 @@ fs_status_t fs_moe_streamer_acquire_expert(fs_moe_streamer_t* streamer,
int speculative,
fs_slot_lease_t* out_lease);

/* Releases consumer lease after verifying generation + optional consumer fence retirement. */
/*
* Retires one accelerator consumer lease token (exact-single retirement:
* the token_id must still be live for the lease's generation) after the
* optional consumer fence signals. Recycling of a cancelled/evicted slot is
* deferred until this retirement drops the last consumer reference AND the
* slot's I/O token has also retired.
*/
fs_status_t fs_moe_streamer_release_lease(fs_moe_streamer_t* streamer,
fs_slot_lease_t* lease,
const fs_fence_t* consumer_fence);

/*
* Generation-safe asynchronous reservation protocol (#18), for callers whose
* pread workers and accelerator consumers complete out of order.
* fs_moe_streamer_acquire_expert() above is the synchronous composition of
* reserve -> read_reserved -> complete_io -> acquire_accel_lease.
*
* reserve_expert: on a resident hit, needs_io=0 and the I/O token is zeroed.
* On a miss, an unleased, fully-retired victim slot is reserved under a new
* monotonic generation with one live I/O token; slots with in-flight I/O,
* live consumer leases, or a pending retirement are never chosen (FS_ERR_BUSY
* if none is recyclable).
*/
fs_status_t fs_moe_streamer_reserve_expert(fs_moe_streamer_t* streamer,
uint32_t expert_id,
int speculative,
fs_slot_reservation_t* out_reservation,
fs_io_token_t* out_io_token);

/*
* Performs the bounded storage read for a live I/O token into its staging
* buffer (slot storage is untouched). Returns the I/O status to pass to
* fs_moe_streamer_complete_io(); does not itself retire the token. Readers
* may instead fill staging_ptr themselves (e.g. an external async engine).
* Returns FS_ERR_BUSY if another thread is already reading the same live
* token (the staging buffer is single-owner per token), FS_ERR_OWNERSHIP if
* the token is stale or its expert_id does not match the loading expert.
*/
fs_status_t fs_moe_streamer_read_reserved(fs_moe_streamer_t* streamer,
const fs_io_token_t* io_token);

/*
* Retires the I/O token exactly once. With io_status == FS_OK, staging bytes
* are published into the slot ONLY if the token is still the slot's live I/O
* reference, its generation and expert_id match the slot's current
* reservation, and the reservation was not cancelled/evicted; otherwise the
* bytes are discarded (stale_io_discards++) and FS_ERR_OWNERSHIP is returned.
* A non-OK io_status retires without publishing and is returned verbatim.
* Stale/duplicate tokens (token_id not live) return FS_ERR_OWNERSHIP and
* publish nothing. If read_reserved is still filling staging for this live
* token, FS_ERR_BUSY is returned and the token is NOT retired (retry after
* the read returns). On return the token is always zeroed if it was live.
*/
fs_status_t fs_moe_streamer_complete_io(fs_moe_streamer_t* streamer,
fs_io_token_t* io_token,
fs_status_t io_status);

/*
* Reader-side cancel/abandon: retires the I/O token exactly once without
* publishing (FS_OK if it was live, FS_ERR_OWNERSHIP if stale/duplicate,
* FS_ERR_BUSY if read_reserved is still filling staging for this token).
* Deferred slot recycling runs once consumer references also reach zero.
*/
fs_status_t fs_moe_streamer_retire_io(fs_moe_streamer_t* streamer,
fs_io_token_t* io_token);

/*
* Cancels a reservation (loading or resident): the slot generation is marked
* for retirement and hidden from new hits/leases, but its backing storage is
* recycled only after its I/O token and every accelerator lease retire.
* Zeroes the reservation on success; FS_ERR_OWNERSHIP if it is stale.
*/
fs_status_t fs_moe_streamer_cancel_reservation(fs_moe_streamer_t* streamer,
fs_slot_reservation_t* reservation);

/*
* Evicts a resident or loading expert with the same deferred-recycle
* semantics as cancel_reservation. FS_ERR_NOT_FOUND if not present.
*/
fs_status_t fs_moe_streamer_evict_expert(fs_moe_streamer_t* streamer,
uint32_t expert_id);

/*
* Takes one accelerator consumer lease token on a published reservation
* (I/O completed, generation current, not cancelled/evicted). Any number of
* distinct lease tokens may be live per generation; each retires exactly
* once via fs_moe_streamer_release_lease().
*/
fs_status_t fs_moe_streamer_acquire_accel_lease(fs_moe_streamer_t* streamer,
const fs_slot_reservation_t* reservation,
fs_slot_lease_t* out_lease);

/*
* Deferred speculative LFU admission (DSpark invariant):
* - begin_speculative: starts recording speculative expert hits/misses without mutating
Expand Down
Loading
Loading