@@ -584,7 +584,7 @@ static void gated_delta_net_f32_pp_thread(unsigned int nth, unsigned int ith, vo
584584 const uint32_t H = v -> ne [1 ];
585585 const uint32_t n_tokens = v -> ne [2 ];
586586 const uint32_t n_seqs = v -> ne [3 ];
587- const uint32_t K = state -> ne [ 1 ];
587+ const uint32_t K = octx -> op_params [ 0 ];
588588
589589 const uint32_t total_rows = H * n_seqs ;
590590 if (ith >= total_rows ) {
@@ -618,9 +618,8 @@ static void gated_delta_net_f32_pp_thread(unsigned int nth, unsigned int ith, vo
618618 struct fastdiv_values fd_rq3 = init_fastdiv_values (rq3 );
619619 struct fastdiv_values fd_rk3 = init_fastdiv_values (rk3 );
620620
621- const uint64_t state_seq_stride = state -> nb [2 ] / sizeof (float );
621+ const uint64_t state_seq_stride = state -> nb [3 ] / sizeof (float );
622622 const uint64_t state_size_per_snap = (uint64_t ) S_v * S_v * H * n_seqs ;
623- const int64_t shift = (int64_t ) n_tokens - (int64_t ) K ;
624623
625624 uint32_t ir_prefetch = ith ;
626625 int spad_idx = 0 ;
@@ -630,7 +629,8 @@ static void gated_delta_net_f32_pp_thread(unsigned int nth, unsigned int ith, vo
630629 const uint32_t piv1 = fastmodulo (ir_prefetch , H , & fd_H );
631630 const uint32_t piv3 = fastdiv (ir_prefetch , & fd_H );
632631 const float * ps_in = state_in_base + (uint64_t ) piv3 * state_seq_stride + (uint64_t ) piv1 * S_v * S_v ;
633- float * ps_out = state_out_base + (uint64_t ) (K - 1 ) * state_size_per_snap + ((uint64_t ) piv3 * H + piv1 ) * S_v * S_v ;
632+ // final state lands in snapshot slot 0 (most-recent-first ordering)
633+ float * ps_out = state_out_base + ((uint64_t ) piv3 * H + piv1 ) * S_v * S_v ;
634634
635635 // Push dummy write-back
636636 dma_queue_push (dma , dma_make_ptr (ps_out , s_work [spad_idx ]),
@@ -661,7 +661,8 @@ static void gated_delta_net_f32_pp_thread(unsigned int nth, unsigned int ith, vo
661661 const uint32_t iq3 = fastdiv (iv3 , & fd_rq3 );
662662 const uint32_t ik3 = fastdiv (iv3 , & fd_rk3 );
663663
664- float * s_out = state_out_base + (uint64_t ) (K - 1 ) * state_size_per_snap + ((uint64_t ) iv3 * H + iv1 ) * S_v * S_v ;
664+ // final state lands in snapshot slot 0 (most-recent-first ordering)
665+ float * s_out = state_out_base + ((uint64_t ) iv3 * H + iv1 ) * S_v * S_v ;
665666
666667 float * attn_data = dst_base + ((uint64_t ) iv3 * n_tokens * H + iv1 ) * S_v ;
667668
@@ -792,7 +793,8 @@ static void gated_delta_net_f32_pp_thread(unsigned int nth, unsigned int ith, vo
792793 }
793794
794795 if (K > 1 ) {
795- const int64_t target_slot = (int64_t ) t - shift ;
796+ // snapshot slot mapping: slot 0 = most recent state, slot s = s tokens back.
797+ const int64_t target_slot = (int64_t ) n_tokens - 1 - (int64_t ) t ;
796798 if (target_slot >= 0 && target_slot < (int64_t ) K ) {
797799 float * curr_state_o = state_out_base + (uint64_t ) target_slot * state_size_per_snap + ((uint64_t ) iv3 * H + iv1 ) * S_v * S_v ;
798800 if (curr_state_o != s_out ) {
@@ -844,7 +846,6 @@ static void gated_delta_net_f32_tg_thread(unsigned int nth, unsigned int ith, vo
844846 const uint32_t S_v = v -> ne [0 ];
845847 const uint32_t H = v -> ne [1 ];
846848 const uint32_t n_seqs = v -> ne [3 ];
847- const uint32_t K = state -> ne [1 ];
848849
849850 const uint32_t total_rows = H * n_seqs ;
850851 if (ith >= total_rows ) {
@@ -878,8 +879,7 @@ static void gated_delta_net_f32_tg_thread(unsigned int nth, unsigned int ith, vo
878879 struct fastdiv_values fd_rq3 = init_fastdiv_values (rq3 );
879880 struct fastdiv_values fd_rk3 = init_fastdiv_values (rk3 );
880881
881- const uint64_t state_seq_stride = state -> nb [2 ] / sizeof (float );
882- const uint64_t state_size_per_snap = (uint64_t ) S_v * S_v * H * n_seqs ;
882+ const uint64_t state_seq_stride = state -> nb [3 ] / sizeof (float );
883883
884884 uint32_t ir_prefetch = ith ;
885885 int spad_idx = 0 ;
@@ -889,7 +889,8 @@ static void gated_delta_net_f32_tg_thread(unsigned int nth, unsigned int ith, vo
889889 const uint32_t piv1 = fastmodulo (ir_prefetch , H , & fd_H );
890890 const uint32_t piv3 = fastdiv (ir_prefetch , & fd_H );
891891 const float * ps_in = state_in_base + (uint64_t ) piv3 * state_seq_stride + (uint64_t ) piv1 * S_v * S_v ;
892- float * ps_out = state_out_base + (uint64_t ) (K - 1 ) * state_size_per_snap + ((uint64_t ) piv3 * H + piv1 ) * S_v * S_v ;
892+ // final state lands in snapshot slot 0 (most-recent-first ordering)
893+ float * ps_out = state_out_base + ((uint64_t ) piv3 * H + piv1 ) * S_v * S_v ;
893894
894895 // Push dummy write-back
895896 dma_queue_push (dma , dma_make_ptr (ps_out , s_work [spad_idx ]),
@@ -920,7 +921,8 @@ static void gated_delta_net_f32_tg_thread(unsigned int nth, unsigned int ith, vo
920921 const uint32_t iq3 = fastdiv (iv3 , & fd_rq3 );
921922 const uint32_t ik3 = fastdiv (iv3 , & fd_rk3 );
922923
923- float * s_out = state_out_base + (uint64_t ) (K - 1 ) * state_size_per_snap + ((uint64_t ) iv3 * H + iv1 ) * S_v * S_v ;
924+ // final state lands in snapshot slot 0 (most-recent-first ordering)
925+ float * s_out = state_out_base + ((uint64_t ) iv3 * H + iv1 ) * S_v * S_v ;
924926
925927 float * attn_data = dst_base + ((uint64_t ) iv3 * H + iv1 ) * S_v ;
926928
@@ -1097,7 +1099,7 @@ int op_gated_delta_net(struct htp_ops_context * octx) {
10971099 const uint32_t H = v -> ne [1 ];
10981100 const uint32_t n_tokens = v -> ne [2 ];
10991101 const uint32_t n_seqs = v -> ne [3 ];
1100- const uint32_t K = state -> ne [ 1 ];
1102+ const uint32_t K = octx -> op_params [ 0 ];
11011103
11021104 if (S_v == 0 || S_v > HTP_GDN_MAX_SV || H == 0 || n_tokens == 0 || n_seqs == 0 ) {
11031105 return HTP_STATUS_NO_SUPPORT ;
@@ -1110,7 +1112,8 @@ int op_gated_delta_net(struct htp_ops_context * octx) {
11101112 (n_seqs % q -> ne [3 ]) != 0 || (n_seqs % k -> ne [3 ]) != 0 ) {
11111113 return HTP_STATUS_NO_SUPPORT ;
11121114 }
1113- if (state -> ne [0 ] * state -> ne [2 ] * state -> ne [3 ] != S_v * S_v * H * n_seqs ) {
1115+ // state holds s0 only: [S_v, S_v, H, n_seqs]
1116+ if (state -> ne [0 ] != S_v || state -> ne [1 ] != S_v || state -> ne [2 ] != H || state -> ne [3 ] != n_seqs ) {
11141117 return HTP_STATUS_NO_SUPPORT ;
11151118 }
11161119 if (dst -> ne [0 ] != S_v * H || dst -> ne [1 ] != n_tokens * n_seqs + S_v * n_seqs * K ) {
0 commit comments