@@ -586,6 +586,7 @@ static void gated_delta_net_f32_pp_thread(unsigned int nth, unsigned int ith, vo
586586 const uint32_t H = v -> ne [1 ];
587587 const uint32_t n_tokens = v -> ne [2 ];
588588 const uint32_t n_seqs = v -> ne [3 ];
589+ const uint32_t K = state -> ne [1 ];
589590
590591 const uint32_t total_rows = H * n_seqs ;
591592 if (ith >= total_rows ) {
@@ -606,6 +607,10 @@ static void gated_delta_net_f32_pp_thread(unsigned int nth, unsigned int ith, vo
606607 float local_k [HTP_GDN_MAX_SV ] __attribute__((aligned (128 )));
607608 float local_sums [4 ] __attribute__((aligned (128 )));
608609
610+ const uint64_t state_seq_stride = state -> nb [2 ] / sizeof (float );
611+ const uint64_t state_size_per_snap = (uint64_t ) S_v * S_v * H * n_seqs ;
612+ const int64_t shift = (int64_t ) n_tokens - (int64_t ) K ;
613+
609614 for (uint32_t ir = ith ; ir < total_rows ; ir += nth ) {
610615 const uint32_t iv1 = ir % H ;
611616 const uint32_t iv3 = ir / H ;
@@ -615,8 +620,8 @@ static void gated_delta_net_f32_pp_thread(unsigned int nth, unsigned int ith, vo
615620 const uint32_t iq3 = iv3 / rq3 ;
616621 const uint32_t ik3 = iv3 / rk3 ;
617622
618- float * s_out = state_out_base + ((uint64_t ) iv3 * H + iv1 ) * S_v * S_v ;
619- const float * s_in = state_in_base + (( uint64_t ) iv3 * H + iv1 ) * S_v * S_v ;
623+ float * s_out = state_out_base + (uint64_t ) ( K - 1 ) * state_size_per_snap + ( (uint64_t ) iv3 * H + iv1 ) * S_v * S_v ;
624+ const float * s_in = state_in_base + (uint64_t ) iv3 * state_seq_stride + ( uint64_t ) iv1 * S_v * S_v ;
620625
621626 memcpy (s_out , s_in , gctx -> state_bytes );
622627 float * s_work = s_out ;
@@ -689,6 +694,16 @@ static void gated_delta_net_f32_pp_thread(unsigned int nth, unsigned int ith, vo
689694 }
690695 }
691696
697+ if (K > 1 ) {
698+ const int64_t target_slot = (int64_t ) t - shift ;
699+ if (target_slot >= 0 && target_slot < (int64_t ) K ) {
700+ float * curr_state_o = state_out_base + (uint64_t ) target_slot * state_size_per_snap + ((uint64_t ) iv3 * H + iv1 ) * S_v * S_v ;
701+ if (curr_state_o != s_work ) {
702+ memcpy (curr_state_o , s_work , gctx -> state_bytes );
703+ }
704+ }
705+ }
706+
692707 attn_data += (uint64_t ) S_v * H ;
693708 }
694709 }
@@ -709,6 +724,7 @@ static void gated_delta_net_f32_tg_thread(unsigned int nth, unsigned int ith, vo
709724 const uint32_t S_v = v -> ne [0 ];
710725 const uint32_t H = v -> ne [1 ];
711726 const uint32_t n_seqs = v -> ne [3 ];
727+ const uint32_t K = state -> ne [1 ];
712728
713729 const uint32_t total_rows = H * n_seqs ;
714730 if (ith >= total_rows ) {
@@ -736,6 +752,9 @@ static void gated_delta_net_f32_tg_thread(unsigned int nth, unsigned int ith, vo
736752 spad = gctx -> vtcm_state_base + gctx -> vtcm_state_per_thread * ith ;
737753 }
738754
755+ const uint64_t state_seq_stride = state -> nb [2 ] / sizeof (float );
756+ const uint64_t state_size_per_snap = (uint64_t ) S_v * S_v * H * n_seqs ;
757+
739758 for (uint32_t ir = ith ; ir < total_rows ; ir += nth ) {
740759 const uint32_t iv1 = ir % H ;
741760 const uint32_t iv3 = ir / H ;
@@ -745,8 +764,8 @@ static void gated_delta_net_f32_tg_thread(unsigned int nth, unsigned int ith, vo
745764 const uint32_t iq3 = iv3 / rq3 ;
746765 const uint32_t ik3 = iv3 / rk3 ;
747766
748- float * s_out = state_out_base + ((uint64_t ) iv3 * H + iv1 ) * S_v * S_v ;
749- const float * s_in = state_in_base + (( uint64_t ) iv3 * H + iv1 ) * S_v * S_v ;
767+ float * s_out = state_out_base + (uint64_t ) ( K - 1 ) * state_size_per_snap + ( (uint64_t ) iv3 * H + iv1 ) * S_v * S_v ;
768+ const float * s_in = state_in_base + (uint64_t ) iv3 * state_seq_stride + ( uint64_t ) iv1 * S_v * S_v ;
750769 float * s_work ;
751770
752771 if (spad ) {
@@ -901,6 +920,7 @@ int op_gated_delta_net(struct htp_ops_context * octx) {
901920 const uint32_t H = v -> ne [1 ];
902921 const uint32_t n_tokens = v -> ne [2 ];
903922 const uint32_t n_seqs = v -> ne [3 ];
923+ const uint32_t K = state -> ne [1 ];
904924
905925 if (S_v == 0 || S_v > HTP_GDN_MAX_SV || H == 0 || n_tokens == 0 || n_seqs == 0 ) {
906926 return HTP_STATUS_NO_SUPPORT ;
@@ -913,10 +933,10 @@ int op_gated_delta_net(struct htp_ops_context * octx) {
913933 (n_seqs % q -> ne [3 ]) != 0 || (n_seqs % k -> ne [3 ]) != 0 ) {
914934 return HTP_STATUS_NO_SUPPORT ;
915935 }
916- if (state -> ne [0 ] * state -> ne [1 ] * state -> ne [ 2 ] * state -> ne [3 ] != S_v * S_v * H * n_seqs ) {
936+ if (state -> ne [0 ] * state -> ne [2 ] * state -> ne [3 ] != S_v * S_v * H * n_seqs ) {
917937 return HTP_STATUS_NO_SUPPORT ;
918938 }
919- if (dst -> ne [0 ] != S_v * H || dst -> ne [1 ] != n_tokens * n_seqs + S_v * n_seqs ) {
939+ if (dst -> ne [0 ] != S_v * H || dst -> ne [1 ] != n_tokens * n_seqs + S_v * n_seqs * K ) {
920940 return HTP_STATUS_NO_SUPPORT ;
921941 }
922942
0 commit comments