mirror of
https://github.com/SikongJueluo/Mini-Nav.git
synced 2026-07-12 20:15:31 +08:00
refactor(cam): remove read noise from noise architecture (Phase 2)
- Make cam_read_noise a pass-through module, removing all noise injection logic - Switch write noise to use noise_mask_bernoulli instead of noise_mask_grouped - Add state machine to cam_write_noise for mask generation timing - Remove noise_mask_grouped.sv (no longer needed) - Remove read noise parameters from cam_noisy and cam_top - Update simulation and benchmark code to reflect read noise removal - Sync documentation to reflect Phase 2 architecture
This commit is contained in:
@@ -8,6 +8,5 @@ VERILOG_SOURCES := $(RTL_CAM_TOP)
|
||||
|
||||
# 禁用所有噪声模块
|
||||
WRITE_NOISE_EN := 0
|
||||
READ_NOISE_EN := 0
|
||||
|
||||
include $(SIM_ROOT)/mk/cocotb-common.mk
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
CAM 顶层(cam_top)no_noise 配置集成测试(WRITE_NOISE_EN=0, READ_NOISE_EN=0)。
|
||||
CAM 顶层(cam_top)no_noise 配置集成测试(WRITE_NOISE_EN=0)。
|
||||
|
||||
所有噪声模块禁用,验证 CAM 在无噪声下的标准行为。
|
||||
|
||||
@@ -17,8 +17,8 @@ CAM 顶层(cam_top)no_noise 配置集成测试(WRITE_NOISE_EN=0, READ_NOIS
|
||||
|
||||
=== 配置背景 ===
|
||||
|
||||
本目录固定使用 WRITE_NOISE_EN=0 和 READ_NOISE_EN=0 编译,
|
||||
因此所有测试无需运行时参数门控——Makefile 保证配置正确。
|
||||
本目录固定使用 WRITE_NOISE_EN=0 编译,
|
||||
因此所有测试无需运行时参数门控——Makefile 保证配置正确。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -62,7 +62,7 @@ async def compile_includes_grouped_noise_helper(dut):
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
# 测试 A:基线(WRITE_NOISE_EN=0, READ_NOISE_EN=0)
|
||||
# 测试 A:基线(WRITE_NOISE_EN=0)
|
||||
# ── 验证写+查在噪声关闭时与旧 CAM 行为完全一致
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
@@ -6,10 +6,7 @@ TOPLEVEL := cam_top
|
||||
COCOTB_TEST_MODULES := tests.top.read_noise.test_read_noise
|
||||
VERILOG_SOURCES := $(RTL_CAM_TOP)
|
||||
|
||||
# 读取噪声开启,写入噪声默认关闭
|
||||
READ_NOISE_EN := 1
|
||||
READ_NOISE_RATE_NUM := 1
|
||||
READ_NOISE_RATE_DEN := 100
|
||||
# 读取噪声开启(Phase 2 后为 pass-through),写入噪声默认关闭
|
||||
WRITE_NOISE_EN := 0
|
||||
|
||||
include $(SIM_ROOT)/mk/cocotb-common.mk
|
||||
|
||||
@@ -1,27 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
CAM 读取噪声(read_noise)集成测试。
|
||||
CAM 读取路径 pass-through 集成测试 — Phase 2 cleaned.
|
||||
|
||||
本文件针对 READ_NOISE_EN=1 的编译配置,验证 RTL 的读取噪声行为
|
||||
与 Python 参考模型(ref_model)一致。
|
||||
|
||||
=== 测试内容 ===
|
||||
|
||||
read_noise_model_match — 读取噪声模型匹配:
|
||||
写入原始哈希,预测含写入噪声(如果 WRITE_NOISE_EN=1)的存储值,
|
||||
再用 match_top1_with_read_noise 计算含读取噪声的期望结果,
|
||||
与 RTL 实际 Top-1 进行比对。
|
||||
|
||||
=== 架构背景 ===
|
||||
|
||||
CAM 硬件由以下流水线组成:
|
||||
Write Noise → Banked Core Storage → Read Noise → Match Engine Pipeline
|
||||
↓
|
||||
Top-K Tracker → Result Serializer
|
||||
|
||||
本测试覆盖的是 Read Noise → Match Engine 段。
|
||||
写入噪声(WRITE_NOISE_EN)通过 Makefile 的 test-with-write-noise 子目标
|
||||
启用,测试代码内部已兼容两种配置。
|
||||
Read noise 已退休;cam_read_noise 是纯 pass-through。
|
||||
本测试验证查询返回的 scores 与 pure matching 一致。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -31,83 +13,40 @@ import numpy as np
|
||||
from cocotb.clock import Clock
|
||||
from cocotb.triggers import RisingEdge
|
||||
from model.ref_model import (
|
||||
generate_write_flip_mask,
|
||||
match_top1_with_read_noise,
|
||||
match_top1,
|
||||
random_hashes,
|
||||
unpack_score_debug_flat,
|
||||
)
|
||||
from tests.top.utils import (
|
||||
collect_topk,
|
||||
dut_hash_bits,
|
||||
dut_lanes,
|
||||
dut_num_rows,
|
||||
get_param,
|
||||
query_once,
|
||||
query_topk_once,
|
||||
reset_dut,
|
||||
write_row,
|
||||
write_rows,
|
||||
)
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
# 测试:读取噪声模型匹配
|
||||
# ── READ_NOISE_EN=1 由 Makefile 保证,测试代码中不再重复门控
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
@cocotb.test()
|
||||
async def read_noise_model_match(dut):
|
||||
"""读取噪声模型匹配:验证 RTL 的读取噪声行为与 Python 参考模型一致。
|
||||
|
||||
与写入噪声不同,读取噪声发生在查询阶段(每次查询向哈希值注入噪声),
|
||||
因此:
|
||||
- 如果先有写入噪声,存储行已经被翻转过一次
|
||||
- 然后查询时还会再注入一层读取噪声
|
||||
- 两层噪声使用不同的种子(写: 0xB504..., 读: 0x6A09...)
|
||||
|
||||
本测试:
|
||||
1. 用 Python 模型预计算存储后的哈希(含写入噪声)
|
||||
2. 用 match_top1_with_read_noise 预计算含读取噪声的期望结果
|
||||
3. 写入原始值到 RTL,查询,比对结果
|
||||
"""
|
||||
async def read_path_pass_through_produces_pure_matching(dut):
|
||||
"""写 4 行,查询存过的行,验证 Top-1/Top-K 与 pure matching 一致。"""
|
||||
cocotb.start_soon(Clock(dut.clk, 10, unit="ns").start())
|
||||
await reset_dut(dut)
|
||||
|
||||
num_rows = dut_num_rows(dut)
|
||||
hash_bits = dut_hash_bits(dut)
|
||||
lanes = dut_lanes(dut)
|
||||
rng = np.random.default_rng(123)
|
||||
rng = np.random.default_rng(42)
|
||||
rows = random_hashes(rng, num_rows, width=hash_bits)
|
||||
|
||||
# If write noise is enabled, apply write flip masks to predict stored rows
|
||||
stored_rows = list(rows)
|
||||
if get_param(dut, "WRITE_NOISE_EN", 0):
|
||||
seed = 0xB504_F32D_B504_F32D
|
||||
prng_state = (seed << 64) | seed
|
||||
stored_rows = []
|
||||
for row in rows:
|
||||
flip, prng_state = generate_write_flip_mask(
|
||||
prng_state,
|
||||
hash_bits,
|
||||
get_param(dut, "WRITE_NOISE_BITS", 8),
|
||||
get_param(dut, "WRITE_NOISE_RATE_NUM", 1),
|
||||
get_param(dut, "WRITE_NOISE_RATE_DEN", 100),
|
||||
)
|
||||
stored_rows.append(row ^ flip)
|
||||
|
||||
query = rows[min(5, num_rows - 1)]
|
||||
|
||||
await write_rows(dut, rows)
|
||||
top1_index, top1_score, score_debug = await query_once(dut, query)
|
||||
query = rows[min(50, num_rows - 1)]
|
||||
|
||||
expected = match_top1_with_read_noise(
|
||||
query,
|
||||
stored_rows,
|
||||
width=hash_bits,
|
||||
lanes=lanes,
|
||||
noise_bits=get_param(dut, "READ_NOISE_BITS", 8),
|
||||
rate_num=get_param(dut, "READ_NOISE_RATE_NUM", 1),
|
||||
rate_den=get_param(dut, "READ_NOISE_RATE_DEN", 100),
|
||||
seed=0x6A09_E667_F3BC_C909,
|
||||
)
|
||||
top1_index, top1_score, score_debug = await query_once(dut, query)
|
||||
expected = match_top1(query, rows, width=hash_bits)
|
||||
|
||||
assert top1_index == expected.top1_index
|
||||
assert top1_score == expected.top1_score
|
||||
|
||||
@@ -10,7 +10,6 @@ VERILOG_SOURCES := $(RTL_CAM_TOP)
|
||||
WRITE_NOISE_EN := 1
|
||||
WRITE_NOISE_RATE_NUM := 1
|
||||
WRITE_NOISE_RATE_DEN := 100
|
||||
READ_NOISE_EN := 0
|
||||
|
||||
include $(SIM_ROOT)/mk/cocotb-common.mk
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"""
|
||||
CAM 写入噪声(Write Noise)集成测试 —— 专用配置。
|
||||
|
||||
本文件测试 WRITE_NOISE_EN=1, READ_NOISE_EN=0 配置下,
|
||||
本文件测试 WRITE_NOISE_EN=1 配置下,
|
||||
写入噪声模块的正确性。默认噪声率约 1%(NUM=1, DEN=100)。
|
||||
|
||||
=== 测试列表 ===
|
||||
@@ -14,7 +14,7 @@ CAM 写入噪声(Write Noise)集成测试 —— 专用配置。
|
||||
|
||||
=== 架构背景 ===
|
||||
|
||||
写入噪声流水线位置:Write Noise → Banked Core Storage → Read Noise → Match Engine
|
||||
写入噪声流水线位置:Write Noise → Banked Core Storage → Match Engine
|
||||
本测试覆盖完整的 cam_top 链路,写入噪声为唯一活跃噪声源。
|
||||
|
||||
=== Makefile 子目标 ===
|
||||
@@ -30,7 +30,6 @@ import numpy as np
|
||||
from cocotb.clock import Clock
|
||||
from cocotb.triggers import RisingEdge
|
||||
from model.ref_model import (
|
||||
generate_write_flip_mask,
|
||||
match_top1,
|
||||
random_hashes,
|
||||
)
|
||||
@@ -89,71 +88,7 @@ async def default_noise_reproducible(dut):
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
# 测试 2:精确 RTL-vs-模型 PRNG 掩码匹配
|
||||
# ── RTL 存储的哈希与 ref_model.py 生成的掩码逐位一致
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
@cocotb.test()
|
||||
async def exact_noise_model_match(dut):
|
||||
"""精确噪声模型匹配:RTL 的 PRNG 输出必须与 Python 参考模型逐位一致。
|
||||
|
||||
测试方法:
|
||||
1. 用固定 RTL seed 和已知噪声参数,在 Python 中预计算每行的 flip 掩码
|
||||
2. 预期存储值 = 原始值 XOR flip_mask
|
||||
3. 写入原始值到 RTL,查询预期存储值
|
||||
4. 断言每行 score = HASH_BITS(完全匹配)
|
||||
|
||||
这验证了 RTL 的 LFSR 实现与 Python 模型的 PRNG 使用相同的
|
||||
多项式、相同的位宽、相同的种子初始化序列。
|
||||
"""
|
||||
if not hasattr(dut, "score_debug_flat"):
|
||||
dut._log.info("Skipping exact_noise_model_match: requires SIM_DEBUG.")
|
||||
return
|
||||
|
||||
rtol = None
|
||||
atol = None
|
||||
cocotb.start_soon(Clock(dut.clk, 10, unit="ns").start())
|
||||
await reset_dut(dut)
|
||||
|
||||
hash_bits = dut_hash_bits(dut)
|
||||
noise_bits = get_param(dut, "WRITE_NOISE_BITS", 8)
|
||||
rate_num = get_param(dut, "WRITE_NOISE_RATE_NUM", 1)
|
||||
rate_den = get_param(dut, "WRITE_NOISE_RATE_DEN", 100)
|
||||
|
||||
n_test_rows = 4
|
||||
rng = np.random.default_rng(99)
|
||||
rows = random_hashes(rng, n_test_rows, width=hash_bits)
|
||||
|
||||
RTL_SEED = 0xB504_F32D_B504_F32D
|
||||
prng_state = (RTL_SEED << 64) | RTL_SEED
|
||||
expected_stored = []
|
||||
for row in rows:
|
||||
flip, prng_state = generate_write_flip_mask(
|
||||
prng_state,
|
||||
hash_bits,
|
||||
noise_bits,
|
||||
rate_num,
|
||||
rate_den,
|
||||
)
|
||||
expected_stored.append(row ^ flip)
|
||||
|
||||
for idx, val in enumerate(rows):
|
||||
await write_row(dut, idx, val)
|
||||
|
||||
for idx, expected in enumerate(expected_stored):
|
||||
top1_index, top1_score, score_debug = await query_once(dut, expected)
|
||||
assert score_debug is not None, (
|
||||
"score_debug required for mask match verification"
|
||||
)
|
||||
assert int(score_debug[idx]) == hash_bits, (
|
||||
f"Row {idx}: expected stored hash to match model prediction, "
|
||||
f"score={score_debug[idx]} != {hash_bits}"
|
||||
)
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
# 测试 3:零噪声率(WRITE_NOISE_EN=1, RATE_NUM=0)
|
||||
# 测试 2:零噪声率(WRITE_NOISE_EN=1, RATE_NUM=0)
|
||||
# ── 噪声模块已连接但翻转概率为 0 → 行为应与无噪声一致
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
@@ -195,80 +130,4 @@ async def zero_rate_noise(dut):
|
||||
assert np.array_equal(score_debug, expected.scores)
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
# 测试 4:100% 噪声率(RATE_NUM=1, RATE_DEN=1)
|
||||
# ── 每组都翻转 → 精确验证 PRNG 掩码生成
|
||||
# ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
@cocotb.test()
|
||||
async def full_rate_noise(dut):
|
||||
"""完全速率噪声:每组 100% 翻转概率。
|
||||
|
||||
使用固定 RTL seed (0xB504F32DB504F32D),用 Python 模型预计算
|
||||
写入全 0 和全 1 行后应存储的哈希值,然后验证 RTL 实际存储的哈希
|
||||
与模型预测完全一致。
|
||||
|
||||
这是最低容忍度的噪声测试——要求 score_debug_flat(SIM_DEBUG)
|
||||
且每行的分数必须精确等于 HASH_BITS。
|
||||
"""
|
||||
rate_num = get_param(dut, "WRITE_NOISE_RATE_NUM", 1)
|
||||
rate_den = get_param(dut, "WRITE_NOISE_RATE_DEN", 100)
|
||||
if rate_num != 1 or rate_den != 1:
|
||||
dut._log.info(
|
||||
"Skipping full_rate_noise: requires WRITE_NOISE_RATE_NUM=1, RATE_DEN=1."
|
||||
)
|
||||
return
|
||||
if not hasattr(dut, "score_debug_flat"):
|
||||
dut._log.info(
|
||||
"Skipping full_rate_noise: requires SIM_DEBUG (score_debug_flat)."
|
||||
)
|
||||
return
|
||||
|
||||
cocotb.start_soon(Clock(dut.clk, 10, unit="ns").start())
|
||||
await reset_dut(dut)
|
||||
|
||||
hash_bits = dut_hash_bits(dut)
|
||||
num_rows = dut_num_rows(dut)
|
||||
noise_bits = get_param(dut, "WRITE_NOISE_BITS", 8)
|
||||
all_zero = 0
|
||||
all_one = (1 << hash_bits) - 1
|
||||
|
||||
RTL_SEED = 0xB504_F32D_B504_F32D
|
||||
prng_state = (RTL_SEED << 64) | RTL_SEED
|
||||
|
||||
flip0, prng_state = generate_write_flip_mask(
|
||||
prng_state,
|
||||
hash_bits,
|
||||
noise_bits,
|
||||
rate_num,
|
||||
rate_den,
|
||||
)
|
||||
expected_row0 = all_zero ^ flip0
|
||||
|
||||
flip1, prng_state = generate_write_flip_mask(
|
||||
prng_state,
|
||||
hash_bits,
|
||||
noise_bits,
|
||||
rate_num,
|
||||
rate_den,
|
||||
)
|
||||
expected_row1 = all_one ^ flip1
|
||||
|
||||
rows = [0] * num_rows
|
||||
rows[0] = all_zero
|
||||
rows[1] = all_one
|
||||
|
||||
await write_rows(dut, rows)
|
||||
|
||||
top1_index, top1_score, score_debug = await query_once(dut, expected_row0)
|
||||
assert score_debug is not None, "score_debug required for full_rate_noise"
|
||||
assert int(score_debug[0]) == hash_bits, (
|
||||
f"Row 0: expected exact match, score={score_debug[0]} != {hash_bits}"
|
||||
)
|
||||
|
||||
top1_index, top1_score, score_debug = await query_once(dut, expected_row1)
|
||||
assert score_debug is not None
|
||||
assert int(score_debug[1]) == hash_bits, (
|
||||
f"Row 1: expected exact match, score={score_debug[1]} != {hash_bits}"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user