Skip to content

Commit e6f0efa

Browse files
authored
Merge pull request #29 from t81dev/phase-19-hardening-13781412227940955739
Harden Phase 19: Cost-Aware Fabric & Economic Introspection
2 parents e1995ad + 965fb1c commit e6f0efa

19 files changed

Lines changed: 276 additions & 54 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ venv/
4747
# --- Ternary Data & Logs ---
4848
*.tfrm
4949
*.tfd
50+
*.csv
5051
weights.txt
5152
trace.vcd
5253
waveform.vcd

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ OBJ_DIR = obj_dir
3939
VERILATOR_FLAGS = --cc $(HW_DIR)/ternary_fabric_top.v -I$(HW_DIR) --Mdir $(OBJ_DIR) -Wno-fatal
4040

4141
# Targets
42-
ALL_C_BINS = $(BIN_DIR)/mediator_mock $(BIN_DIR)/pt5_example $(BIN_DIR)/reference_tfmbs $(BIN_DIR)/test_device $(BIN_DIR)/mock_llama $(BIN_DIR)/test_dynamic_detection $(BIN_DIR)/test_phase10 $(BIN_DIR)/test_multi_tile $(BIN_DIR)/test_eviction
42+
ALL_C_BINS = $(BIN_DIR)/mediator_mock $(BIN_DIR)/pt5_example $(BIN_DIR)/reference_tfmbs $(BIN_DIR)/test_device $(BIN_DIR)/mock_llama $(BIN_DIR)/test_dynamic_detection $(BIN_DIR)/test_phase10 $(BIN_DIR)/test_multi_tile $(BIN_DIR)/test_eviction $(BIN_DIR)/sparse_stress $(BIN_DIR)/test_convergence
4343
ALL_LIBS = $(BIN_DIR)/libtfmbs_device$(SHLIB_EXT) $(BIN_DIR)/libtfmbs_intercept$(SHLIB_EXT)
4444
ALL_HW_SIM = $(BIN_DIR)/fabric_tb.vvp
4545

@@ -85,6 +85,12 @@ $(BIN_DIR)/test_multi_tile: tests/test_multi_tile.c $(BIN_DIR)/libtfmbs_device$(
8585
$(BIN_DIR)/test_eviction: tests/test_eviction.c $(BIN_DIR)/libtfmbs_device$(SHLIB_EXT)
8686
$(CC) $(CFLAGS) -o $@ $< -L$(BIN_DIR) -ltfmbs_device
8787

88+
$(BIN_DIR)/sparse_stress: benchmarks/synthetic/sparse_stress.c $(BIN_DIR)/libtfmbs_device$(SHLIB_EXT)
89+
$(CC) $(CFLAGS) -o $@ $< -L$(BIN_DIR) -ltfmbs_device
90+
91+
$(BIN_DIR)/test_convergence: tests/test_convergence.c $(BIN_DIR)/libtfmbs_device$(SHLIB_EXT)
92+
$(CC) $(CFLAGS) -o $@ $< -L$(BIN_DIR) -ltfmbs_device
93+
8894
run_mock_llama: $(BIN_DIR)/mock_llama $(ALL_LIBS)
8995
export FABRIC_SHORT_CIRCUIT=1; \
9096
$(LIBPATH_ENV)=$(BIN_DIR) $(PRELOAD_ENV)=$(BIN_DIR)/libtfmbs_intercept$(SHLIB_EXT) ./$(BIN_DIR)/mock_llama

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,26 +159,26 @@ LD_PRELOAD=./libtfmbs_intercept.so ./llama-cli -m model.gguf
159159
### Telemetry Insights
160160

161161
```text
162-
[TFMBS-Telemetry] GEMV Completed
163-
- Zero-Skips: 172,401 (65.8% reduction in total operations)
164-
- Pool Usage: 84.2 MB / 128 MB (LRU Managed)
165-
- Async Queue: 0 in-flight (Non-blocking execution)
162+
[TFMBS-Telemetry] Batch of 8 GEMV(s) Completed
163+
- Zero-Skips: 497,990 (95.0% reduction)
164+
- Cycles: 7,427 (Cost: 568023.0, Sem-Eff: 0.05, Econ-Eff: 0.05)
165+
- Residency: Hits: 54, Misses: 0
166+
- Pool Usage: 0.5 MB / 128 MB (0.4%)
167+
- Evictions: 0
166168
```
167169

168-
Telemetry exposes sparsity, residency, and queue pressure in real time.
170+
Telemetry exposes sparsity, residency, economic cost, and semantic efficiency in real time.
169171

170172
---
171173

172174
## 🏗️ Project State & Architecture
173175

174-
The project is currently in **Phase 15**, representing full-stack operation:
176+
The project is currently in **Phase 19**, representing a **Cost-Aware Economic Fabric**:
175177

176-
* PyTorch frontends
177-
* GGUF ingestion
178-
* Multi-tile emulation
179-
* IOCTL device layer
180-
* Interposition illusion
181-
* RTL kernel parity
178+
* **Economic Introspection:** Decisions are driven by projected cost and residency rebates.
179+
* **Hysteresis Scheduling:** Stabilized tile selection to prevent state thrashing.
180+
* **Sparse-Stress Hardening:** Verified performance at 99%+ sparsity.
181+
* **Temporal Pipelining:** Overlapping memory hydration with compute.
182182

183183
### Architecture Layers
184184

USER_MANUAL.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ If you want to avoid the overhead of the "First Scan" heuristic, you can explici
7171
When running with the interposer or Python API, the Fabric provides real-time telemetry:
7272

7373
* **Zero-Skips:** The number of operations eliminated because an operand was zero. Typically 50-75% for LLM workloads.
74+
* **Semantic Efficiency:** The ratio of useful operations (`active_ops`) to total operations.
75+
* **Economic Efficiency:** The ratio of useful operations to total `fabric_cost` (weighted sum of ops, memory, and residency misses).
7476
* **Pool Usage:** Current consumption of the 128MB emulation pool.
75-
* **Eviction Events:** Increments when the LRU policy frees space for new weight frames.
77+
* **Eviction Events:** Increments when the policy (Recency + Frequency) frees space for new weight frames.
7678
* **Async Queue:** Shows in-flight GEMVs being processed by the background worker thread.
7779

7880
---

benchmarks/density_metrics.csv

Lines changed: 0 additions & 3 deletions
This file was deleted.

benchmarks/gemm_metrics.csv

Lines changed: 0 additions & 3 deletions
This file was deleted.

benchmarks/lstm_metrics.csv

Lines changed: 0 additions & 3 deletions
This file was deleted.

benchmarks/saturation_metrics.csv

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <time.h>
5+
#include "tfmbs_device.h"
6+
#include "tfmbs_api.h"
7+
8+
void run_stress(float sparsity, int use_batching) {
9+
int rows = 512;
10+
int cols = 1024;
11+
int size = rows * cols;
12+
int num_steps = 10;
13+
14+
void* w = fabric_alloc(size);
15+
void* i = fabric_alloc(cols);
16+
void* o = fabric_alloc(rows * sizeof(int32_t));
17+
18+
int8_t* host_w = malloc(size);
19+
int8_t* host_i = malloc(cols);
20+
memset(host_i, 1, cols);
21+
22+
// Initialize with high sparsity
23+
for (int j = 0; j < size; j++) {
24+
host_w[j] = ((float)rand() / RAND_MAX < (1.0 - sparsity)) ? 1 : 0;
25+
}
26+
fabric_memcpy_to(w, host_w, size, 1);
27+
fabric_memcpy_to(i, host_i, cols, 1);
28+
29+
printf("\n[Sparse-Stress] Sparsity: %.1f%%, Batching: %s\n", sparsity * 100.0, use_batching ? "ON" : "OFF");
30+
31+
fabric_handle_t handles[num_steps];
32+
33+
for (int s = 0; s < num_steps; s++) {
34+
handles[s] = fabric_exec_gemv_async(w, i, o, rows, cols);
35+
if (!use_batching) {
36+
fabric_wait(handles[s]);
37+
fabric_dump_economic_csv("sparse_stress_economic.csv");
38+
fabric_dump_metrics_csv("sparse_stress_metrics.csv");
39+
}
40+
}
41+
42+
if (use_batching) {
43+
for (int s = 0; s < num_steps; s++) {
44+
fabric_wait(handles[s]);
45+
fabric_dump_economic_csv("sparse_stress_economic.csv");
46+
fabric_dump_metrics_csv("sparse_stress_metrics.csv");
47+
}
48+
}
49+
50+
fabric_free(w); fabric_free(i); fabric_free(o);
51+
free(host_w); free(host_i);
52+
}
53+
54+
int main() {
55+
srand(42);
56+
// Sparsity levels: 95%, 98%, 99%
57+
float levels[] = {0.95, 0.98, 0.99};
58+
59+
for (int l = 0; l < 3; l++) {
60+
run_stress(levels[l], 0); // Batching OFF
61+
run_stress(levels[l], 1); // Batching ON
62+
}
63+
64+
return 0;
65+
}

benchmarks/temporal_metrics.csv

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)