Skip to content

Commit

Permalink
Minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
minseongg committed Nov 19, 2024
1 parent 9a35402 commit 5d554d4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 110 deletions.
4 changes: 2 additions & 2 deletions hazardflow-designs/src/gemmini/execute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ fn spad_write_req(resp: (MeshRespExtended, (Dataflow, U<3>, U<16>)), bank_i: U<2
.mesh_resp
.data
.map(|e| {
let e_clipped = clip_with_saturation(U::from(e)); // Lower 8 bits
let e_clipped = clip_with_saturation(U::from(e[0])); // Lower 8 bits
if activation == 1.into_u() {
// Check MSB for signedness.
if e_clipped[8 - 1] {
Expand Down Expand Up @@ -940,7 +940,7 @@ fn acc_write_req(resp: (MeshRespExtended, (Dataflow, U<3>, U<16>)), bank_i: U<1>
let write_signals = compute_write_signal(resp);
let resp = resp.0;

let wdata = resp.mesh_resp.data.map(|v| U::from(v.sext::<32>()));
let wdata = resp.mesh_resp.data.map(|v| U::from(v[0].sext::<32>()));
let wmask = write_signals.w_mask.map(|v| v.repeat::<4>()).concat();

if write_signals.start_array_outputting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const FIFO_LENGTH: usize = MAX_SIMULTANEOUS_MATMULS + 1;
pub type A = Array<Array<S<INPUT_BITS>, TILE_ROWS>, MESH_ROWS>;
/// Type of data of `b`.
pub type B = Array<Array<S<INPUT_BITS>, TILE_COLS>, MESH_COLS>;
/// Type of data of `c`.
pub type C = Array<Array<S<OUTPUT_BITS>, TILE_COLS>, MESH_COLS>;
/// Type of data of `d`.
pub type D = Array<Array<S<INPUT_BITS>, TILE_COLS>, MESH_COLS>;

Expand Down Expand Up @@ -88,8 +90,8 @@ pub struct MeshResp {
pub tag: MeshTag,
/// Indicates that the row represents the last row.
pub last: bool,
/// Data.
pub data: Array<S<OUTPUT_BITS>, MESH_COLS>,
/// Output data.
pub data: C,
}

/// Matmul operation configuration.
Expand Down
45 changes: 20 additions & 25 deletions hazardflow-designs/src/gemmini/execute/systolic_array/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,34 +146,29 @@ pub fn preprocess_type((data, req): (Valid<(A, B, D)>, Valid<(ReqExtended, bool)
}

/// Interface type conversion.
pub fn postprocess_type(
(out_row, out_col): (MeshRowData, MeshColData),
) -> Valid<(Array<S<OUTPUT_BITS>, MESH_COLS>, PeColControl)> {
pub fn postprocess_type((out_row, out_col): (MeshRowData, MeshColData)) -> Valid<(C, PeColControl)> {
// # Safety
//
// All the input and output interfaces are `Valid` type.
unsafe {
(out_row, out_col).fsm::<Valid<(Array<S<OUTPUT_BITS>, MESH_COLS>, PeColControl)>, ()>(
(),
|(_, col_data), _, ()| {
let out_valid = col_data[0][0].0.is_some();
let dataflow_os = col_data[0][0].1.is_some_and(|v| matches!(v.control.dataflow, Dataflow::OS));

let out_b = col_data
.map(|tile_r| tile_r.map(|(data, _)| data.map_or(0.into_u(), |v| U::from(v.b))).concat())
.map(S::from);
let out_c = col_data
.map(|tile_r| tile_r.map(|(data, _)| data.map_or(0.into_u(), |v| U::from(v.d))).concat())
.map(S::from);

let matmul_result = if dataflow_os { out_c } else { out_b };

let ep = if out_valid { Some((matmul_result, col_data[0][0].1.unwrap())) } else { None };
let ir0 = ().repeat::<1>().repeat::<MESH_COLS>();
let ir1 = ((), ()).repeat::<1>().repeat::<MESH_COLS>();

(ep, (ir0, ir1), ())
},
)
(out_row, out_col).fsm::<Valid<(C, PeColControl)>, ()>((), |(_, col_data), _, ()| {
let out_valid = col_data[0][0].0.is_some();
let dataflow_os = col_data[0][0].1.is_some_and(|v| matches!(v.control.dataflow, Dataflow::OS));

let out_b = col_data
.map(|tile_r| tile_r.map(|(data, _)| data.map_or(0.into_u(), |v| U::from(v.b))).concat())
.map(|v| S::from(v).repeat::<TILE_COLS>());
let out_c = col_data
.map(|tile_r| tile_r.map(|(data, _)| data.map_or(0.into_u(), |v| U::from(v.d))).concat())
.map(|v| S::from(v).repeat::<TILE_COLS>());

let matmul_result = if dataflow_os { out_c } else { out_b };

let ep = if out_valid { Some((matmul_result, col_data[0][0].1.unwrap())) } else { None };
let ir0 = ().repeat::<1>().repeat::<MESH_COLS>();
let ir1 = ((), ()).repeat::<1>().repeat::<MESH_COLS>();

(ep, (ir0, ir1), ())
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,38 +313,18 @@ async def ws_no_transpose(dut):
await tb.in_ctrl_req.send(req_with_rob_id(WS, False, False, REG2))

for i in range(16):
await tb.in_a_data_req.send(
InpDataTransaction(
payload_Some_0_0=0,
)
)
await tb.in_b_data_req.send(
InpDataTransaction(
payload_Some_0_0=0,
)
)
await tb.in_a_data_req.send(InpDataTransaction(payload_Some_0_0=0))
await tb.in_b_data_req.send(InpDataTransaction(payload_Some_0_0=0))
# When running WS dataflow, weight should be sent in reverse order
# due to the way the weight is preloaded in the PEs.
await tb.in_d_data_req.send(
InpDataTransaction(
payload_Some_0_0=concatenate_data(weight[15 - i], 8),
)
)
await tb.in_d_data_req.send(InpDataTransaction(payload_Some_0_0=concatenate_data(weight[15 - i], 8)))

# 2. Send activation and bias data
await tb.in_ctrl_req.send(req_with_none_rob_id(WS, False, False, REG1))

for i in range(16):
await tb.in_a_data_req.send(
InpDataTransaction(
payload_Some_0_0=concatenate_data(activation[i], 8),
)
)
await tb.in_b_data_req.send(
InpDataTransaction(
payload_Some_0_0=concatenate_data(bias[i], 8),
)
)
await tb.in_a_data_req.send(InpDataTransaction(payload_Some_0_0=concatenate_data(activation[i], 8)))
await tb.in_b_data_req.send(InpDataTransaction(payload_Some_0_0=concatenate_data(bias[i], 8)))
await tb.in_d_data_req.send(InpDataTransaction(payload_Some_0_0=0))

output_data = []
Expand Down Expand Up @@ -386,30 +366,18 @@ async def ws_transpose_a(dut):
# 1. Preload weight data
await tb.in_ctrl_req.send(req_with_rob_id(WS, True, False, REG2))
for i in range(16):
await tb.in_a_data_req.send(
InpDataTransaction(
payload_Some_0_0=concatenate_data(activation[i], 8),
)
)
await tb.in_a_data_req.send(InpDataTransaction(payload_Some_0_0=concatenate_data(activation[i], 8)))
await tb.in_b_data_req.send(InpDataTransaction(payload_Some_0_0=0))
# When running WS dataflow, weight should be sent in reverse order
# due to the way the weight is preloaded in the PEs.
await tb.in_d_data_req.send(
InpDataTransaction(
payload_Some_0_0=concatenate_data(weight[15 - i], 8),
)
)
await tb.in_d_data_req.send(InpDataTransaction(payload_Some_0_0=concatenate_data(weight[15 - i], 8)))

# 2. Send activation and bias data
await tb.in_ctrl_req.send(req_with_none_rob_id(WS, True, False, REG1))

for i in range(16):
await tb.in_a_data_req.send(InpDataTransaction(payload_Some_0_0=0))
await tb.in_b_data_req.send(
InpDataTransaction(
payload_Some_0_0=concatenate_data(bias[i], 8),
)
)
await tb.in_b_data_req.send(InpDataTransaction(payload_Some_0_0=concatenate_data(bias[i], 8)))
await tb.in_d_data_req.send(InpDataTransaction(payload_Some_0_0=0))

output_data = []
Expand Down Expand Up @@ -537,11 +505,7 @@ async def os_no_transpose(dut):
await tb.in_ctrl_req.send(req_with_none_rob_id(OS, False, False, REG1))
for i in range(16):
await tb.in_a_data_req.send(InpDataTransaction(payload_Some_0_0=0))
await tb.in_b_data_req.send(
InpDataTransaction(
payload_Some_0_0=concatenate_data(weight[i], 8),
)
)
await tb.in_b_data_req.send(InpDataTransaction(payload_Some_0_0=concatenate_data(weight[i], 8)))
await tb.in_d_data_req.send(InpDataTransaction(payload_Some_0_0=0))

await tb.in_ctrl_req.send(os_flush_request(REG1, rnd_shift))
Expand Down Expand Up @@ -592,24 +556,12 @@ async def os_transpose_a(dut):
for i in range(16):
await tb.in_a_data_req.send(InpDataTransaction(payload_Some_0_0=0))
await tb.in_b_data_req.send(InpDataTransaction(payload_Some_0_0=0))
await tb.in_d_data_req.send(
InpDataTransaction(
payload_Some_0_0=concatenate_data(bias[15 - i], 8),
)
)
await tb.in_d_data_req.send(InpDataTransaction(payload_Some_0_0=concatenate_data(bias[15 - i], 8)))

await tb.in_ctrl_req.send(req_with_none_rob_id(OS, True, False, REG1))
for i in range(16):
await tb.in_a_data_req.send(
InpDataTransaction(
payload_Some_0_0=concatenate_data(activation[i], 8),
)
)
await tb.in_b_data_req.send(
InpDataTransaction(
payload_Some_0_0=concatenate_data(weight[i], 8),
)
)
await tb.in_a_data_req.send(InpDataTransaction(payload_Some_0_0=concatenate_data(activation[i], 8)))
await tb.in_b_data_req.send(InpDataTransaction(payload_Some_0_0=concatenate_data(weight[i], 8)))
await tb.in_d_data_req.send(InpDataTransaction(payload_Some_0_0=0))

await tb.in_ctrl_req.send(os_flush_request(REG1, rnd_shift))
Expand Down Expand Up @@ -646,9 +598,7 @@ async def os_transpose_both(dut):
await ClockCycles(dut.clk, 10)

# Input data
activation, weight, bias, expected_output, rnd_shift = generate_os_test_data(
True, True
)
activation, weight, bias, expected_output, rnd_shift = generate_os_test_data(True, True)

tb.log.info(f"[Ouptut-Stationary] Transpose A and B, Shift: {rnd_shift}")
tb.log.info(f"Activation:\n{activation}")
Expand All @@ -661,33 +611,19 @@ async def os_transpose_both(dut):
for i in range(16):
await tb.in_a_data_req.send(InpDataTransaction(payload_Some_0_0=0))
await tb.in_b_data_req.send(InpDataTransaction(payload_Some_0_0=0))
await tb.in_d_data_req.send(
InpDataTransaction(payload_Some_0_0=concatenate_data(bias[15 - i], 8))
)
await tb.in_d_data_req.send(InpDataTransaction(payload_Some_0_0=concatenate_data(bias[15 - i], 8)))

# 1. Send weight data
await tb.in_ctrl_req.send(req_with_rob_id(OS, True, True, REG2))
for i in range(16):
await tb.in_a_data_req.send(InpDataTransaction(payload_Some_0_0=0))
await tb.in_b_data_req.send(
InpDataTransaction(
payload_Some_0_0=concatenate_data(weight[i], 8),
)
)
await tb.in_d_data_req.send(
InpDataTransaction(
payload_Some_0_0=concatenate_data(bias[15 - i], 8),
)
)
await tb.in_b_data_req.send(InpDataTransaction(payload_Some_0_0=concatenate_data(weight[i], 8)))
await tb.in_d_data_req.send(InpDataTransaction(payload_Some_0_0=concatenate_data(bias[15 - i], 8)))

# 2. Send activation data
await tb.in_ctrl_req.send(req_with_none_rob_id(OS, True, True, REG1))
for i in range(16):
await tb.in_a_data_req.send(
InpDataTransaction(
payload_Some_0_0=concatenate_data(activation[i], 8),
)
)
await tb.in_a_data_req.send(InpDataTransaction(payload_Some_0_0=concatenate_data(activation[i], 8)))
await tb.in_b_data_req.send(InpDataTransaction(payload_Some_0_0=0))
await tb.in_d_data_req.send(InpDataTransaction(payload_Some_0_0=0))

Expand Down

0 comments on commit 5d554d4

Please sign in to comment.