diff --git a/custom_ops/gpu_ops/append_attn/decoder_write_cache_with_rope_impl.cuh b/custom_ops/gpu_ops/append_attn/decoder_write_cache_with_rope_impl.cuh index 2b3110f9d94..52c8ad5aff9 100644 --- a/custom_ops/gpu_ops/append_attn/decoder_write_cache_with_rope_impl.cuh +++ b/custom_ops/gpu_ops/append_attn/decoder_write_cache_with_rope_impl.cuh @@ -334,8 +334,9 @@ __global__ void append_decode_cache_T_rope_kernel( if (hi < num_heads + kv_num_heads) { // q k rope const uint32_t emb_idx = write_seq_id * half_head_size + h_bias / 2; - Load(&cos_emb[emb_idx], &cos_emb_vec); - Load(&sin_emb[emb_idx], &sin_emb_vec); + uint32_t new_emb_idx = rope_3d ? emb_idx + ori_bi * max_seq_len * head_size : emb_idx; + Load(&cos_emb[new_emb_idx], &cos_emb_vec); + Load(&sin_emb[new_emb_idx], &sin_emb_vec); } #pragma unroll for (int i = 0; i < HalfVecSize; i++) { @@ -401,7 +402,8 @@ __global__ void append_decode_cache_T_neox_rope_kernel( const int head_size, const int block_size, const uint32_t elem_cnt, - const int kv_num_heads) { + const int kv_num_heads, + const bool rope_3d) { using LoadT = AlignedVector; using LoadBiasT = AlignedVector; using LoadKVT = AlignedVector; @@ -448,8 +450,9 @@ __global__ void append_decode_cache_T_neox_rope_kernel( if (hi < num_heads + kv_num_heads) { // q k rope const uint32_t emb_idx = write_seq_id * head_size + h_bias; - Load(&cos_emb[emb_idx], &cos_emb_vec); - Load(&sin_emb[emb_idx], &sin_emb_vec); + uint32_t new_emb_idx = rope_3d ? emb_idx + ori_bi * max_seq_len * head_size * 2 : emb_idx; + Load(&cos_emb[new_emb_idx], &cos_emb_vec); + Load(&sin_emb[new_emb_idx], &sin_emb_vec); } #pragma unroll for (int i = 0; i < VecSize; i++) { @@ -517,7 +520,8 @@ __global__ void append_decode_cache_T_neox_rope_kernel( const int head_size, const int block_size, const uint32_t elem_cnt, - const int kv_num_heads) { + const int kv_num_heads, + const bool rope_3d) { using LoadT = AlignedVector; using LoadBiasT = AlignedVector; using LoadOutScaleT = AlignedVector; @@ -574,8 +578,9 @@ __global__ void append_decode_cache_T_neox_rope_kernel( if (hi < num_heads + kv_num_heads) { // q k rope const uint32_t emb_idx = write_seq_id * head_size + h_bias; - Load(&cos_emb[emb_idx], &cos_emb_vec); - Load(&sin_emb[emb_idx], &sin_emb_vec); + uint32_t new_emb_idx = rope_3d ? emb_idx + ori_bi * max_seq_len * head_size * 2 : emb_idx; + Load(&cos_emb[new_emb_idx], &cos_emb_vec); + Load(&sin_emb[new_emb_idx], &sin_emb_vec); } #pragma unroll for (int i = 0; i < VecSize; i++) { @@ -647,7 +652,8 @@ __global__ void append_decode_cache_int8_rope_kernel( const int block_size, const float max_bound, const float min_bound, - const int kv_num_heads) { + const int kv_num_heads, + const bool rope_3d) { static_assert(HeadDim == 128, "just support HeadDim be 128 now!"); static_assert(VecSize == 4, "just support VecSize be 4 now, 32 * 4!"); constexpr int NUM_WARPS = 4; @@ -690,8 +696,9 @@ __global__ void append_decode_cache_int8_rope_kernel( // q rope const uint32_t emb_idx = write_seq_id * half_head_size + head_bias / 2; - Load(&cos_emb[emb_idx], &cos_emb_vec); - Load(&sin_emb[emb_idx], &sin_emb_vec); + uint32_t new_emb_idx = rope_3d ? emb_idx + bid * max_seq_len * HeadDim : emb_idx; + Load(&cos_emb[new_emb_idx], &cos_emb_vec); + Load(&sin_emb[new_emb_idx], &sin_emb_vec); #pragma unroll for (int i = 0; i < HalfVecSize; i++) { // dequant + add_bias + rope @@ -768,10 +775,11 @@ __global__ void append_decode_cache_int8_rope_kernel( const T *cache_v_scale_cur = cache_v_scale + v_head_idx * HeadDim + head_bias; if (head_idx < num_heads + kv_num_heads) { const uint32_t emb_idx = write_seq_id * half_head_size + head_bias / 2; - Load(&cos_emb[emb_idx], &cos_emb_vec1); - Load(&cos_emb[emb_idx + 4], &cos_emb_vec2); - Load(&sin_emb[emb_idx], &sin_emb_vec1); - Load(&sin_emb[emb_idx + 4], &sin_emb_vec2); + uint32_t new_emb_idx = rope_3d ? emb_idx + bid * max_seq_len * HeadDim : emb_idx; + Load(&cos_emb[new_emb_idx], &cos_emb_vec1); + Load(&cos_emb[new_emb_idx + 4], &cos_emb_vec2); + Load(&sin_emb[new_emb_idx], &sin_emb_vec1); + Load(&sin_emb[new_emb_idx + 4], &sin_emb_vec2); if constexpr (!is_scale_channel_wise) { scale = __ldg(&cache_k_scale[kv_head_idx]); } @@ -898,7 +906,8 @@ __global__ void append_decode_cache_int8_rope_kernel( const int block_size, const float max_bound, const float min_bound, - const int kv_num_heads) { + const int kv_num_heads, + const bool rope_3d) { static_assert(HeadDim == 128, "just support HeadDim be 128 now!"); static_assert(VecSize == 4, "just support VecSize be 4 now, 32 * 4!"); constexpr int NUM_WARPS = 4; @@ -948,9 +957,10 @@ __global__ void append_decode_cache_int8_rope_kernel( // q rope const uint32_t emb_idx = write_seq_id * half_head_size + head_bias / 2; - Load(&cos_emb[emb_idx], &cos_emb_vec); + uint32_t new_emb_idx = rope_3d ? emb_idx + bid * max_seq_len * HeadDim : emb_idx; + Load(&cos_emb[new_emb_idx], &cos_emb_vec); - Load(&sin_emb[emb_idx], &sin_emb_vec); + Load(&sin_emb[new_emb_idx], &sin_emb_vec); #pragma unroll for (int i = 0; i < HalfVecSize; i++) { @@ -1043,10 +1053,11 @@ __global__ void append_decode_cache_int8_rope_kernel( const T *cache_v_scale_cur = cache_v_scales + v_head_idx * HeadDim + head_bias; if (head_idx < num_heads + kv_num_heads) { const uint32_t emb_idx = write_seq_id * half_head_size + head_bias / 2; - Load(&cos_emb[emb_idx], &cos_emb_vec1); - Load(&cos_emb[emb_idx + 4], &cos_emb_vec2); - Load(&sin_emb[emb_idx], &sin_emb_vec1); - Load(&sin_emb[emb_idx + 4], &sin_emb_vec2); + uint32_t new_emb_idx = rope_3d ? emb_idx + bid * max_seq_len * HeadDim : emb_idx; + Load(&cos_emb[new_emb_idx], &cos_emb_vec1); + Load(&cos_emb[new_emb_idx + 4], &cos_emb_vec2); + Load(&sin_emb[new_emb_idx], &sin_emb_vec1); + Load(&sin_emb[new_emb_idx + 4], &sin_emb_vec2); if constexpr (!is_scale_channel_wise) { scale = __ldg(&cache_k_scales[kv_head_idx]); } @@ -1196,7 +1207,8 @@ __global__ void append_decode_cache_int8_neox_rope_kernel( const int block_size, const float max_bound, const float min_bound, - const int kv_num_heads) { + const int kv_num_heads, + const bool rope_3d) { static_assert(HeadDim == 128, "just support HeadDim be 128 now!"); static_assert(VecSize == 4, "just support VecSize be 4 now, 32 * 4!"); constexpr int NUM_WARPS = 4; @@ -1244,8 +1256,9 @@ __global__ void append_decode_cache_int8_neox_rope_kernel( // q rope const uint32_t emb_idx = write_seq_id * HeadDim + head_bias; - Load(&cos_emb[emb_idx], &cos_emb_vec); - Load(&sin_emb[emb_idx], &sin_emb_vec); + uint32_t new_emb_idx = rope_3d ? emb_idx + bid * max_seq_len * HeadDim * 2 : emb_idx; + Load(&cos_emb[new_emb_idx], &cos_emb_vec); + Load(&sin_emb[new_emb_idx], &sin_emb_vec); #pragma unroll for (int i = 0; i < VecSize; i++) { @@ -1326,10 +1339,11 @@ __global__ void append_decode_cache_int8_neox_rope_kernel( T scale; const uint32_t emb_idx = write_seq_id * HeadDim + head_bias; - Load(&cos_emb[emb_idx], &cos_emb_vec1); - Load(&cos_emb[emb_idx + 8], &cos_emb_vec2); - Load(&sin_emb[emb_idx], &sin_emb_vec1); - Load(&sin_emb[emb_idx + 8], &sin_emb_vec2); + uint32_t new_emb_idx = rope_3d ? emb_idx + bid * max_seq_len * HeadDim * 2 : emb_idx; + Load(&cos_emb[new_emb_idx], &cos_emb_vec1); + Load(&cos_emb[new_emb_idx + 8], &cos_emb_vec2); + Load(&sin_emb[new_emb_idx], &sin_emb_vec1); + Load(&sin_emb[new_emb_idx + 8], &sin_emb_vec2); scale = __ldg(&cache_k_scales[kv_head_idx]); #pragma unroll for (int i = 0; i < HALF_K_VEC_SIZE; i++) { @@ -1499,7 +1513,8 @@ __global__ void append_decode_cache_int8_neox_rope_kernel( const int block_size, const float max_bound, const float min_bound, - const int kv_num_heads) { + const int kv_num_heads, + const bool rope_3d) { static_assert(HeadDim == 128, "just support HeadDim be 128 now!"); static_assert(VecSize == 4, "just support VecSize be 4 now, 32 * 4!"); constexpr int NUM_WARPS = 4; @@ -1559,8 +1574,10 @@ __global__ void append_decode_cache_int8_neox_rope_kernel( // q rope const uint32_t emb_idx = write_seq_id * HeadDim + head_bias; - Load(&cos_emb[emb_idx], &cos_emb_vec); - Load(&sin_emb[emb_idx], &sin_emb_vec); + uint32_t new_emb_idx = rope_3d ? emb_idx + bid * max_seq_len * HeadDim * 2 : emb_idx; + Load(&cos_emb[new_emb_idx], &cos_emb_vec); + Load(&sin_emb[new_emb_idx], &sin_emb_vec); + #pragma unroll for (int i = 0; i < VecSize; i++) { @@ -1668,10 +1685,11 @@ __global__ void append_decode_cache_int8_neox_rope_kernel( T scale; const uint32_t emb_idx = write_seq_id * HeadDim + head_bias; - Load(&cos_emb[emb_idx], &cos_emb_vec1); - Load(&cos_emb[emb_idx + 8], &cos_emb_vec2); - Load(&sin_emb[emb_idx], &sin_emb_vec1); - Load(&sin_emb[emb_idx + 8], &sin_emb_vec2); + uint32_t new_emb_idx = rope_3d ? emb_idx + bid * max_seq_len * HeadDim * 2 : emb_idx; + Load(&cos_emb[new_emb_idx], &cos_emb_vec1); + Load(&cos_emb[new_emb_idx + 8], &cos_emb_vec2); + Load(&sin_emb[new_emb_idx], &sin_emb_vec1); + Load(&sin_emb[new_emb_idx + 8], &sin_emb_vec2); scale = __ldg(&cache_k_scales[kv_head_idx]); #pragma unroll for (int i = 0; i < HALF_K_VEC_SIZE; i++) { @@ -1890,7 +1908,8 @@ __global__ void append_decode_cache_int4_rope_kernel( const int block_size, const float max_bound, const float min_bound, - const int kv_num_heads) { + const int kv_num_heads, + const bool rope_3d) { static_assert(HeadDim == 128, "just support HeadDim be 128 now!"); static_assert(VecSize == 4, "just support VecSize be 4 now, 32 * 4!"); constexpr int NUM_WARPS = 4; @@ -1934,8 +1953,9 @@ __global__ void append_decode_cache_int4_rope_kernel( // q rope const uint32_t emb_idx = write_seq_id * half_head_size + head_bias / 2; - Load(&cos_emb[emb_idx], &cos_emb_vec); - Load(&sin_emb[emb_idx], &sin_emb_vec); + uint32_t new_emb_idx = rope_3d ? emb_idx + bid * max_seq_len * HeadDim : emb_idx; + Load(&cos_emb[new_emb_idx], &cos_emb_vec); + Load(&sin_emb[new_emb_idx], &sin_emb_vec); #pragma unroll for (int i = 0; i < HalfVecSize; i++) { // dequant + add_bias + rope @@ -2009,10 +2029,11 @@ __global__ void append_decode_cache_int4_rope_kernel( Load(&qkv_now[bias_idx + 8], &src_vec2); if (head_idx < num_heads + kv_num_heads) { const uint32_t emb_idx = write_seq_id * half_head_size + head_bias / 2; - Load(&cos_emb[emb_idx], &cos_emb_vec1); - Load(&cos_emb[emb_idx + 4], &cos_emb_vec2); - Load(&sin_emb[emb_idx], &sin_emb_vec1); - Load(&sin_emb[emb_idx + 4], &sin_emb_vec2); + uint32_t new_emb_idx = rope_3d ? emb_idx + bid * max_seq_len * HeadDim : emb_idx; + Load(&cos_emb[new_emb_idx], &cos_emb_vec1); + Load(&cos_emb[new_emb_idx + 4], &cos_emb_vec2); + Load(&sin_emb[new_emb_idx], &sin_emb_vec1); + Load(&sin_emb[new_emb_idx + 4], &sin_emb_vec2); Load(&cache_k_scale[cache_idx], &scale_vec1); Load(&cache_k_scale[cache_idx + 8], &scale_vec2); Load(&cache_k_zero_points[cache_idx], &zp_vec1); @@ -2189,7 +2210,8 @@ __global__ void append_decode_cache_int4_rope_kernel( const int block_size, const float max_bound, const float min_bound, - const int kv_num_heads) { + const int kv_num_heads, + const bool rope_3d) { static_assert(HeadDim == 128, "just support HeadDim be 128 now!"); static_assert(VecSize == 4, "just support VecSize be 4 now, 32 * 4!"); constexpr int NUM_WARPS = 4; @@ -2238,8 +2260,9 @@ __global__ void append_decode_cache_int4_rope_kernel( Load(&qkv_out_scales[bias_idx], &out_scale_vec); // q rope const uint32_t emb_idx = write_seq_id * half_head_size + head_bias / 2; - Load(&cos_emb[emb_idx], &cos_emb_vec); - Load(&sin_emb[emb_idx], &sin_emb_vec); + uint32_t new_emb_idx = rope_3d ? emb_idx + bid * max_seq_len * HeadDim : emb_idx; + Load(&cos_emb[new_emb_idx], &cos_emb_vec); + Load(&sin_emb[new_emb_idx], &sin_emb_vec); #pragma unroll for (int i = 0; i < HalfVecSize; i++) { // dequant + add_bias + rope @@ -2326,10 +2349,11 @@ __global__ void append_decode_cache_int4_rope_kernel( &out_scale_vec2); if (head_idx < num_heads + kv_num_heads) { const uint32_t emb_idx = write_seq_id * half_head_size + head_bias / 2; - Load(&cos_emb[emb_idx], &cos_emb_vec1); - Load(&cos_emb[emb_idx + 4], &cos_emb_vec2); - Load(&sin_emb[emb_idx], &sin_emb_vec1); - Load(&sin_emb[emb_idx + 4], &sin_emb_vec2); + uint32_t new_emb_idx = rope_3d ? emb_idx + bid * max_seq_len * HeadDim : emb_idx; + Load(&cos_emb[new_emb_idx], &cos_emb_vec1); + Load(&cos_emb[new_emb_idx + 4], &cos_emb_vec2); + Load(&sin_emb[new_emb_idx], &sin_emb_vec1); + Load(&sin_emb[new_emb_idx + 4], &sin_emb_vec2); Load(&cache_k_scale[cache_idx], &scale_vec1); Load(&cache_k_scale[cache_idx + 8], &scale_vec2); Load(&cache_k_zero_points[cache_idx], &zp_vec1); @@ -2513,7 +2537,8 @@ __global__ void append_decode_cache_int4_neox_rope_kernel( const int block_size, const float max_bound, const float min_bound, - const int kv_num_heads) { + const int kv_num_heads, + const bool rope_3d) { static_assert(HeadDim == 128, "just support HeadDim be 128 now!"); static_assert(VecSize == 4, "just support VecSize be 4 now, 32 * 4!"); constexpr int NUM_WARPS = 4; @@ -2560,8 +2585,9 @@ __global__ void append_decode_cache_int4_neox_rope_kernel( // q rope const uint32_t emb_idx = write_seq_id * HeadDim + head_bias; - Load(&cos_emb[emb_idx], &cos_emb_vec); - Load(&sin_emb[emb_idx], &sin_emb_vec); + uint32_t new_emb_idx = rope_3d ? emb_idx + bid * max_seq_len * HeadDim * 2 : emb_idx; + Load(&cos_emb[new_emb_idx], &cos_emb_vec); + Load(&sin_emb[new_emb_idx], &sin_emb_vec); #pragma unroll for (int i = 0; i < VecSize; i++) { // dequant + add_bias + rope @@ -2642,10 +2668,11 @@ __global__ void append_decode_cache_int4_neox_rope_kernel( Load(&qkv_now[right_bias_idx], &right_src_vec1); Load(&qkv_now[right_bias_idx + 8], &right_src_vec2); const uint32_t emb_idx = write_seq_id * HeadDim + head_bias; - Load(&cos_emb[emb_idx], &cos_emb_vec1); - Load(&cos_emb[emb_idx + 8], &cos_emb_vec2); - Load(&sin_emb[emb_idx], &sin_emb_vec1); - Load(&sin_emb[emb_idx + 8], &sin_emb_vec2); + uint32_t new_emb_idx = rope_3d ? emb_idx + bid * max_seq_len * HeadDim * 2 : emb_idx; + Load(&cos_emb[new_emb_idx], &cos_emb_vec1); + Load(&cos_emb[new_emb_idx + 8], &cos_emb_vec2); + Load(&sin_emb[new_emb_idx], &sin_emb_vec1); + Load(&sin_emb[new_emb_idx + 8], &sin_emb_vec2); Load(&cache_k_scale[left_cache_idx], &left_scale_vec1); Load(&cache_k_scale[left_cache_idx + 8], @@ -2887,7 +2914,8 @@ __global__ void append_decode_cache_int4_neox_rope_kernel( const int block_size, const float max_bound, const float min_bound, - const int kv_num_heads) { + const int kv_num_heads, + const bool rope_3d) { static_assert(HeadDim == 128, "just support HeadDim be 128 now!"); static_assert(VecSize == 4, "just support VecSize be 4 now, 32 * 4!"); constexpr int NUM_WARPS = 4; @@ -2945,8 +2973,9 @@ __global__ void append_decode_cache_int4_neox_rope_kernel( &right_out_scale_vec); // q rope const uint32_t emb_idx = write_seq_id * HeadDim + head_bias; - Load(&cos_emb[emb_idx], &cos_emb_vec); - Load(&sin_emb[emb_idx], &sin_emb_vec); + uint32_t new_emb_idx = rope_3d ? emb_idx + bid * max_seq_len * HeadDim * 2 : emb_idx; + Load(&cos_emb[new_emb_idx], &cos_emb_vec); + Load(&sin_emb[new_emb_idx], &sin_emb_vec); #pragma unroll for (int i = 0; i < VecSize; i++) { // dequant + add_bias + rope @@ -3055,10 +3084,11 @@ __global__ void append_decode_cache_int4_neox_rope_kernel( &right_out_scale_vec2); const uint32_t emb_idx = write_seq_id * HeadDim + head_bias; - Load(&cos_emb[emb_idx], &cos_emb_vec1); - Load(&cos_emb[emb_idx + 8], &cos_emb_vec2); - Load(&sin_emb[emb_idx], &sin_emb_vec1); - Load(&sin_emb[emb_idx + 8], &sin_emb_vec2); + uint32_t new_emb_idx = rope_3d ? emb_idx + bid * max_seq_len * HeadDim * 2 : emb_idx; + Load(&cos_emb[new_emb_idx], &cos_emb_vec1); + Load(&cos_emb[new_emb_idx + 8], &cos_emb_vec2); + Load(&sin_emb[new_emb_idx], &sin_emb_vec1); + Load(&sin_emb[new_emb_idx + 8], &sin_emb_vec2); Load(&cache_k_scale[left_cache_idx], &left_scale_vec1); Load(&cache_k_scale[left_cache_idx + 8], diff --git a/custom_ops/gpu_ops/append_attn/decoder_write_cache_with_rope_kernel.cu b/custom_ops/gpu_ops/append_attn/decoder_write_cache_with_rope_kernel.cu index 8561460d1e1..ffee65ee076 100644 --- a/custom_ops/gpu_ops/append_attn/decoder_write_cache_with_rope_kernel.cu +++ b/custom_ops/gpu_ops/append_attn/decoder_write_cache_with_rope_kernel.cu @@ -134,7 +134,8 @@ void append_decode_cache_rope(const QKV_TYPE* qkv, dim_head, block_size, elem_nums, - kv_num_heads); + kv_num_heads, + rope_3d); } else { append_decode_cache_T_neox_rope_kernel <<>>(reinterpret_cast(qkv), @@ -154,7 +155,8 @@ void append_decode_cache_rope(const QKV_TYPE* qkv, dim_head, block_size, elem_nums, - kv_num_heads); + kv_num_heads, + rope_3d); } } else { if (qkv_out_scales) { @@ -261,7 +263,8 @@ void append_decode_cache_int8_rope(const QKV_TYPE* qkv, block_size, 127.0f, -127.0f, - kv_num_heads); + kv_num_heads, + rope_3d); } else { append_decode_cache_int8_neox_rope_kernel <<>>( @@ -284,7 +287,8 @@ void append_decode_cache_int8_rope(const QKV_TYPE* qkv, block_size, 127.0f, -127.0f, - kv_num_heads); + kv_num_heads, + rope_3d); } } else { if (qkv_out_scales) { @@ -311,7 +315,8 @@ void append_decode_cache_int8_rope(const QKV_TYPE* qkv, block_size, 127.0f, -127.0f, - kv_num_heads); + kv_num_heads, + rope_3d); } else { append_decode_cache_int8_rope_kernel <<>>( @@ -334,7 +339,8 @@ void append_decode_cache_int8_rope(const QKV_TYPE* qkv, block_size, 127.0f, -127.0f, - kv_num_heads); + kv_num_heads, + rope_3d); } } } @@ -398,7 +404,8 @@ void append_decode_cache_int4_rope(const QKV_TYPE* qkv, block_size, 7.0f, -8.0f, - kv_num_heads); + kv_num_heads, + rope_3d); } else { append_decode_cache_int4_neox_rope_kernel <<>>( @@ -423,7 +430,8 @@ void append_decode_cache_int4_rope(const QKV_TYPE* qkv, block_size, 7.0f, -8.0f, - kv_num_heads); + kv_num_heads, + rope_3d); } } else { if (qkv_out_scales) { @@ -452,7 +460,8 @@ void append_decode_cache_int4_rope(const QKV_TYPE* qkv, block_size, 7.0f, -8.0f, - kv_num_heads); + kv_num_heads, + rope_3d); } else { append_decode_cache_int4_rope_kernel <<>>( @@ -477,7 +486,8 @@ void append_decode_cache_int4_rope(const QKV_TYPE* qkv, block_size, 7.0f, -8.0f, - kv_num_heads); + kv_num_heads, + rope_3d); } } } diff --git a/custom_ops/gpu_ops/moe/moe_ffn.cu b/custom_ops/gpu_ops/moe/moe_ffn.cu index f9aadb49405..3f56786ce9e 100644 --- a/custom_ops/gpu_ops/moe/moe_ffn.cu +++ b/custom_ops/gpu_ops/moe/moe_ffn.cu @@ -304,7 +304,6 @@ paddle::Tensor MoeExpertFFNFunc( const paddle::optional& expert_idx_per_token, const std::string& quant_method, const bool used_in_ep_low_latency) { - cudaCheckError(); const auto t_type = quant_method == "w4a8" ? up_gate_proj_scale.get().dtype() : permute_input.dtype(); auto ffn_out = paddle::empty_like(permute_input, t_type); diff --git a/fastdeploy/model_executor/layers/backends/dcu/fused_moe_triton_backends.py b/fastdeploy/model_executor/layers/backends/dcu/fused_moe_triton_backends.py index 0a6c31b067c..cd6a5116146 100644 --- a/fastdeploy/model_executor/layers/backends/dcu/fused_moe_triton_backends.py +++ b/fastdeploy/model_executor/layers/backends/dcu/fused_moe_triton_backends.py @@ -38,7 +38,7 @@ def __init__(self, quant_method=None): "down_proj_weight_scale", ] - def process_prequanted_weights(self, layer: nn.Layer, state_dict) -> None: + def process_prequanted_weights(self, layer: nn.Layer, state_dict, is_rearrange: bool = False) -> None: """process_prequanted_weights""" pass @@ -46,7 +46,7 @@ def create_weights(self, layer: nn.Layer, state_dict): """ Triton MoE create weight process. """ - up_gate_proj_weights, down_proj_weights = layer.extract_moe_ffn_weights(state_dict) + up_gate_proj_weights, down_proj_weights, _, _ = layer.extract_moe_ffn_weights(state_dict) assert len(up_gate_proj_weights) == layer.num_local_experts assert len(down_proj_weights) == layer.num_local_experts assert self.quant_method.name() == "wint8" diff --git a/fastdeploy/model_executor/layers/backends/gcu/moe/fused_moe_method_gcu_backend.py b/fastdeploy/model_executor/layers/backends/gcu/moe/fused_moe_method_gcu_backend.py index cf7462e26b4..c899cafc792 100644 --- a/fastdeploy/model_executor/layers/backends/gcu/moe/fused_moe_method_gcu_backend.py +++ b/fastdeploy/model_executor/layers/backends/gcu/moe/fused_moe_method_gcu_backend.py @@ -49,7 +49,7 @@ def __init__(self, quant_config): self.group_size = -1 def process_loaded_weights(self, layer: nn.Layer, state_dict): - up_gate_proj_weights, down_proj_weights = layer.extract_moe_ffn_weights(state_dict) + up_gate_proj_weights, down_proj_weights, _, _ = layer.extract_moe_ffn_weights(state_dict) stacked_up_gate_proj_weights = paddle.stack(up_gate_proj_weights, axis=0) stacked_down_proj_weights = paddle.stack(down_proj_weights, axis=0) layer.up_gate_proj_weight.set_value(paddle.transpose(stacked_up_gate_proj_weights, [0, 2, 1])) @@ -254,7 +254,7 @@ def __init__(self, quant_config): self.quant_multi_process_group_size = int(os.getenv("FD_MOE_QUANT_MULTI_PROCESS_GROUP_SIZE", 8)) logger.info(f"GCUWeightOnlyMoEMethod quant_multi_process_group_size: {self.quant_multi_process_group_size}") - def process_prequanted_weights(self, layer: nn.Layer, state_dict): + def process_prequanted_weights(self, layer: nn.Layer, state_dict, is_rearrange: bool = False): """ Paddle gcu process prequanted weights. """ @@ -299,7 +299,7 @@ def create_weights(self, layer: nn.Layer, state_dict): """ Paddle cutlass create weight process. """ - up_gate_proj_weights, down_proj_weights = layer.extract_moe_ffn_weights(state_dict) + up_gate_proj_weights, down_proj_weights, _, _ = layer.extract_moe_ffn_weights(state_dict) self.check(layer, up_gate_proj_weights, down_proj_weights) def quant_worker(p_group_idx, shared_dict, weights, moe_quant_type, group_size): diff --git a/fastdeploy/model_executor/layers/backends/gcu/quantization/weight_only.py b/fastdeploy/model_executor/layers/backends/gcu/quantization/weight_only.py index 9aebf64ce00..bfebcd2479a 100644 --- a/fastdeploy/model_executor/layers/backends/gcu/quantization/weight_only.py +++ b/fastdeploy/model_executor/layers/backends/gcu/quantization/weight_only.py @@ -59,7 +59,7 @@ def create_weights(self, layer, **extra_weight_attrs): is_bias=False, ) - def process_prequanted_weights(self, layer, state_dict) -> None: + def process_prequanted_weights(self, layer, state_dict, is_rearrange: bool = False) -> None: """ Process pre-quantized weights before applying them to the model Args: diff --git a/fastdeploy/model_executor/layers/backends/metax/moe/fused_moe_triton_metax_backend.py b/fastdeploy/model_executor/layers/backends/metax/moe/fused_moe_triton_metax_backend.py index 50ceecf18fa..7b783412b12 100644 --- a/fastdeploy/model_executor/layers/backends/metax/moe/fused_moe_triton_metax_backend.py +++ b/fastdeploy/model_executor/layers/backends/metax/moe/fused_moe_triton_metax_backend.py @@ -41,7 +41,7 @@ def __init__(self, quant_config=None): "down_proj_weight_scale", ] - def process_prequanted_weights(self, layer: nn.Layer, state_dict) -> None: + def process_prequanted_weights(self, layer: nn.Layer, state_dict, is_rearrange: bool = False) -> None: """process_prequanted_weights""" pass @@ -49,7 +49,7 @@ def create_weights(self, layer: nn.Layer, state_dict): """ Triton MoE create weight process. """ - up_gate_proj_weights, down_proj_weights = layer.extract_moe_ffn_weights(state_dict) + up_gate_proj_weights, down_proj_weights, _, _ = layer.extract_moe_ffn_weights(state_dict) assert len(up_gate_proj_weights) == layer.num_local_experts assert len(down_proj_weights) == layer.num_local_experts diff --git a/fastdeploy/model_executor/layers/moe/fused_moe_cutlass_backend.py b/fastdeploy/model_executor/layers/moe/fused_moe_cutlass_backend.py index 2be90f8f999..f7be9d1b458 100644 --- a/fastdeploy/model_executor/layers/moe/fused_moe_cutlass_backend.py +++ b/fastdeploy/model_executor/layers/moe/fused_moe_cutlass_backend.py @@ -71,7 +71,9 @@ class CutlassMoEMethod(UnquantizedFusedMoEMethod): """ def process_loaded_weights(self, layer: nn.Layer, state_dict): - up_gate_proj_weights, down_proj_weights = layer.extract_moe_ffn_weights(state_dict) + up_gate_proj_weights, down_proj_weights, logical_expert_ids, ep_rank_to_expert_id_list = ( + layer.extract_moe_ffn_weights(state_dict) + ) stacked_up_gate_proj_weights = paddle.stack(up_gate_proj_weights, axis=0) stacked_down_proj_weights = paddle.stack(down_proj_weights, axis=0) @@ -325,7 +327,7 @@ def __init__(self, quant_config): self.moe_quant_type = "w4a8" self.pack_num = 2 - def process_prequanted_weights(self, layer: nn.Layer, state_dict): + def process_prequanted_weights(self, layer: nn.Layer, state_dict, is_rearrange: bool = False): """ Paddle cutlass process prequanted weights. """ @@ -341,6 +343,7 @@ def process_prequanted_weights(self, layer: nn.Layer, state_dict): state_dict, up_gate_proj_expert_weight_key, down_proj_expert_weight_key, + is_rearrange, ) ) @@ -350,22 +353,62 @@ def process_prequanted_weights(self, layer: nn.Layer, state_dict): up_gate_proj_in_scale = [] down_proj_in_scale = [] + if isinstance(state_dict, list): + state_dict = dict(state_dict) + if layer.ep_size > 1: for expert_idx in ep_rank_to_expert_id_list: - scale_tensor = get_tensor(state_dict[up_gate_proj_expert_in_scale_key.format(expert_idx)]) + scale_tensor = get_tensor( + ( + state_dict[up_gate_proj_expert_in_scale_key.format(expert_idx)] + if up_gate_proj_expert_in_scale_key.format(expert_idx) in state_dict + else up_gate_proj_expert_in_scale_key.format(expert_idx) + ), + layer.fd_config.model_config.model, + ) up_gate_proj_in_scale_all_experts.append(scale_tensor) for expert_idx in logical_expert_ids: up_gate_proj_weight_scale.append( - get_tensor(state_dict.pop(up_gate_proj_expert_weight_scale_key.format(expert_idx))) + get_tensor( + ( + state_dict.pop(up_gate_proj_expert_weight_scale_key.format(expert_idx)) + if up_gate_proj_expert_weight_scale_key.format(expert_idx) in state_dict + else up_gate_proj_expert_weight_scale_key.format(expert_idx) + ), + layer.fd_config.model_config.model, + ) ) down_proj_weight_scale.append( - get_tensor(state_dict.pop(down_proj_expert_weight_scale_key.format(expert_idx))) + get_tensor( + ( + state_dict.pop(down_proj_expert_weight_scale_key.format(expert_idx)) + if down_proj_expert_weight_scale_key.format(expert_idx) in state_dict + else down_proj_expert_weight_scale_key.format(expert_idx) + ), + layer.fd_config.model_config.model, + ) ) up_gate_proj_in_scale.append( - get_tensor(state_dict.pop(up_gate_proj_expert_in_scale_key.format(expert_idx))) + get_tensor( + ( + state_dict.pop(up_gate_proj_expert_in_scale_key.format(expert_idx)) + if up_gate_proj_expert_in_scale_key.format(expert_idx) in state_dict + else up_gate_proj_expert_in_scale_key.format(expert_idx) + ), + layer.fd_config.model_config.model, + ) + ) + down_proj_in_scale.append( + get_tensor( + ( + state_dict.pop(down_proj_expert_in_scale_key.format(expert_idx)) + if down_proj_expert_in_scale_key.format(expert_idx) in state_dict + else down_proj_expert_in_scale_key.format(expert_idx) + ), + layer.fd_config.model_config.model, + ) ) - down_proj_in_scale.append(get_tensor(state_dict.pop(down_proj_expert_in_scale_key.format(expert_idx)))) up_gate_proj_weight = paddle.stack(up_gate_proj_weights, axis=0) down_proj_weight = paddle.stack(down_proj_weights, axis=0) @@ -427,7 +470,9 @@ def process_loaded_weights(self, layer: nn.Layer, state_dict): """ Paddle cutlass load weight process. """ - up_gate_proj_weights, down_proj_weights = layer.extract_moe_ffn_weights(state_dict) + up_gate_proj_weights, down_proj_weights, logical_expert_ids, ep_rank_to_expert_id_list = ( + layer.extract_moe_ffn_weights(state_dict) + ) self.check(layer, up_gate_proj_weights, down_proj_weights) for idx, weight_tensor in enumerate([up_gate_proj_weights, down_proj_weights]): weight_name = self.added_weight_attrs[idx] @@ -438,7 +483,9 @@ def process_loaded_weights(self, layer: nn.Layer, state_dict): quanted_weight = paddle.stack(weight_list, axis=0) getattr(layer, weight_name).set_value(quanted_weight) - self.load_w4a8_scale_weights(layer, layer.weight_key_map, state_dict) + self.load_w4a8_scale_weights( + layer, layer.weight_key_map, state_dict, logical_expert_ids, ep_rank_to_expert_id_list + ) def create_w4a8_scale_weights(self, layer: nn.Layer, weight_key_map: dict): """ @@ -492,7 +539,14 @@ def create_w4a8_scale_weights(self, layer: nn.Layer, weight_key_map: dict): ), ) - def load_w4a8_scale_weights(self, layer: nn.Layer, weight_key_map: dict, state_dict: dict): + def load_w4a8_scale_weights( + self, + layer: nn.Layer, + weight_key_map: dict, + state_dict: dict, + logical_expert_ids: paddle.Tensor, + ep_rank_to_expert_id_list: list, + ): """ Get w4a8 weights from state dict and process them. Args: @@ -501,8 +555,15 @@ def load_w4a8_scale_weights(self, layer: nn.Layer, weight_key_map: dict, state_d state_dict (dict): The state dict. """ - def _extract_scale_tensor(state_dict, key_template, expert_idx): - return get_tensor(state_dict.pop(key_template.format(expert_idx))) + def _extract_scale_tensor(layer: nn.Layer, state_dict, key_template, expert_idx): + return get_tensor( + ( + state_dict.pop(key_template.format(expert_idx)) + if key_template.format(expert_idx) in state_dict + else key_template.format(expert_idx) + ), + layer.fd_config.model_config.model, + ) def _process_in_scale(name: str, in_scales: list[paddle.Tensor]): processed_in_scale = 1 / paddle.concat(in_scales) @@ -544,17 +605,23 @@ def _process_weight_scale( # 2. Extract scale tensor from state dict if layer.ep_size > 1: - for expert_idx in range(layer.num_experts): - scale_tensor = get_tensor(state_dict[scale_key_map["up_gate_proj_in_scale"].format(expert_idx)]) + for expert_idx in ep_rank_to_expert_id_list: + scale_tensor = get_tensor( + ( + state_dict[scale_key_map["up_gate_proj_in_scale"].format(expert_idx)] + if scale_key_map["up_gate_proj_in_scale"].format(expert_idx) in state_dict + else scale_key_map["up_gate_proj_in_scale"].format(expert_idx) + ), + layer.fd_config.model_config.model, + ) up_gate_proj_in_scales_all_experts.append(1 / scale_tensor) getattr(layer, "up_gate_proj_in_scale_all_experts").set_value( paddle.concat(up_gate_proj_in_scales_all_experts) ) - for local_expert_idx in range(layer.num_local_experts): - expert_idx = local_expert_idx + layer.expert_id_offset + for expert_idx in logical_expert_ids: for name, scale_key_template in scale_key_map.items(): - scale_tensor = _extract_scale_tensor(state_dict, scale_key_template, expert_idx) + scale_tensor = _extract_scale_tensor(layer, state_dict, scale_key_template, expert_idx) scale_weight_map[name].append(scale_tensor) # 3. Process scale tensor and set to layer @@ -581,7 +648,7 @@ def __init__(self, quant_config): self.moe_quant_type = self.quant_config.algo self.pack_num = 1 - def process_prequanted_weights(self, layer: nn.Layer, state_dict): + def process_prequanted_weights(self, layer: nn.Layer, state_dict, is_rearrange: bool = False): """ Paddle cutlass process prequanted weights. """ @@ -591,9 +658,7 @@ def process_prequanted_weights(self, layer: nn.Layer, state_dict): down_proj_expert_weight_scale_key = layer.weight_key_map.get("down_proj_expert_weight_scale_key", None) up_gate_proj_weights, down_proj_weights, logical_expert_ids, _ = layer.load_experts_weight( - state_dict, - up_gate_proj_expert_weight_key, - down_proj_expert_weight_key, + state_dict, up_gate_proj_expert_weight_key, down_proj_expert_weight_key, is_rearrange ) # self.check(layer, up_gate_proj_weights, down_proj_weights) up_gate_proj_weight_scale = [] @@ -695,7 +760,7 @@ def process_loaded_weights(self, layer: nn.Layer, state_dict): """ Paddle cutlass load weight process. """ - up_gate_proj_weights, down_proj_weights = layer.extract_moe_ffn_weights(state_dict) + up_gate_proj_weights, down_proj_weights, _, _ = layer.extract_moe_ffn_weights(state_dict) self.check(layer, up_gate_proj_weights, down_proj_weights) for idx, weight_tensor in enumerate([up_gate_proj_weights, down_proj_weights]): weight_name = self.added_weight_attrs[idx] diff --git a/fastdeploy/model_executor/layers/moe/fused_moe_deepgemm_backend.py b/fastdeploy/model_executor/layers/moe/fused_moe_deepgemm_backend.py index a5187973f69..4cec9393455 100644 --- a/fastdeploy/model_executor/layers/moe/fused_moe_deepgemm_backend.py +++ b/fastdeploy/model_executor/layers/moe/fused_moe_deepgemm_backend.py @@ -99,7 +99,7 @@ def process_loaded_weights(self, layer: nn.Layer, state_dict): """ deepgemm create weight process. """ - up_gate_proj_weights, down_proj_weights = layer.extract_moe_ffn_weights(state_dict) + up_gate_proj_weights, down_proj_weights, _, _ = layer.extract_moe_ffn_weights(state_dict) self.check(layer, up_gate_proj_weights, down_proj_weights) @@ -124,7 +124,7 @@ def process_loaded_weights(self, layer: nn.Layer, state_dict): quanted_weight_scale = quanted_weight_scale.transpose([0, 2, 1]).contiguous() getattr(layer, scale_name).set_value(quanted_weight_scale) - def process_prequanted_weights(self, layer: nn.Layer, state_dict): + def process_prequanted_weights(self, layer: nn.Layer, state_dict, is_rearrange: bool = False): """ Paddle cutlass process prequanted weights. """ @@ -134,9 +134,7 @@ def process_prequanted_weights(self, layer: nn.Layer, state_dict): down_proj_expert_weight_scale_key = layer.weight_key_map.get("down_proj_expert_weight_scale_key", None) up_gate_proj_weights, down_proj_weights, logical_expert_ids, _ = layer.load_experts_weight( - state_dict, - up_gate_proj_expert_weight_key, - down_proj_expert_weight_key, + state_dict, up_gate_proj_expert_weight_key, down_proj_expert_weight_key, is_rearrange ) # self.check(layer, up_gate_proj_weights, down_proj_weights) up_gate_proj_weight_scale = [] diff --git a/fastdeploy/model_executor/layers/moe/fused_moe_marlin_backend.py b/fastdeploy/model_executor/layers/moe/fused_moe_marlin_backend.py index fb05fff0923..f41c1614e5a 100644 --- a/fastdeploy/model_executor/layers/moe/fused_moe_marlin_backend.py +++ b/fastdeploy/model_executor/layers/moe/fused_moe_marlin_backend.py @@ -197,7 +197,7 @@ def process_loaded_weights(self, layer: nn.Layer, state_dict): """ Marlin MoE load weight process. """ - up_gate_proj_weights, down_proj_weights = layer.extract_moe_ffn_weights(state_dict) + up_gate_proj_weights, down_proj_weights, _, _ = layer.extract_moe_ffn_weights(state_dict) assert len(up_gate_proj_weights) == layer.num_local_experts assert len(down_proj_weights) == layer.num_local_experts assert up_gate_proj_weights[0].shape == [ diff --git a/fastdeploy/model_executor/layers/moe/fused_moe_triton_backend.py b/fastdeploy/model_executor/layers/moe/fused_moe_triton_backend.py index 8af8e9859d6..909203f80f4 100644 --- a/fastdeploy/model_executor/layers/moe/fused_moe_triton_backend.py +++ b/fastdeploy/model_executor/layers/moe/fused_moe_triton_backend.py @@ -48,7 +48,7 @@ def __init__(self, quant_config=None): "down_proj_weight_scale", ] - def process_prequanted_weights(self, layer: nn.Layer, state_dict) -> None: + def process_prequanted_weights(self, layer: nn.Layer, state_dict, is_rearrange: bool = False) -> None: """process_prequanted_weights""" pass @@ -112,7 +112,7 @@ def process_loaded_weights(self, layer: nn.Layer, state_dict): """ Triton MoE load weight process. """ - up_gate_proj_weights, down_proj_weights = layer.extract_moe_ffn_weights(state_dict) + up_gate_proj_weights, down_proj_weights, _, _ = layer.extract_moe_ffn_weights(state_dict) assert len(up_gate_proj_weights) == layer.num_local_experts assert len(down_proj_weights) == layer.num_local_experts @@ -311,10 +311,10 @@ def __init__(self, quant_method=None): "down_proj_in_scale", ] - def process_prequanted_weights(self, layer: nn.Layer, state_dict) -> None: + def process_prequanted_weights(self, layer: nn.Layer, state_dict, is_rearrange: bool = False) -> None: """process_prequanted_weights""" - up_gate_proj_tensor, down_proj_tensor = layer.extract_moe_ffn_weights(state_dict) + up_gate_proj_tensor, down_proj_tensor, _, _ = layer.extract_moe_ffn_weights(state_dict) assert up_gate_proj_tensor[0].shape == [ layer.hidden_size, layer.moe_intermediate_size * 2, @@ -595,7 +595,7 @@ def __init__(self, quant_config): "down_proj_weight_scale", ] - def process_prequanted_weights(self, layer: nn.Layer, state_dict) -> None: + def process_prequanted_weights(self, layer: nn.Layer, state_dict, is_rearrange: bool = False) -> None: """process_prequanted_weights""" raise NotImplementedError @@ -667,7 +667,7 @@ def process_loaded_weights(self, layer: nn.Layer, state_dict): """ Triton MoE create weight process. """ - up_gate_proj_weights, down_proj_weights = layer.extract_moe_ffn_weights(state_dict) + up_gate_proj_weights, down_proj_weights, _, _ = layer.extract_moe_ffn_weights(state_dict) self.check(layer, up_gate_proj_weights, down_proj_weights) for idx, weight_tensor in enumerate([up_gate_proj_weights, down_proj_weights]): diff --git a/fastdeploy/model_executor/layers/moe/fused_moe_wint2_backend.py b/fastdeploy/model_executor/layers/moe/fused_moe_wint2_backend.py index b230d9e5edd..20d8e5196e7 100644 --- a/fastdeploy/model_executor/layers/moe/fused_moe_wint2_backend.py +++ b/fastdeploy/model_executor/layers/moe/fused_moe_wint2_backend.py @@ -73,7 +73,7 @@ def process_loaded_weights(self, layer, weights) -> None: """ pass - def process_prequanted_weights(self, layer: nn.Layer, state_dict): + def process_prequanted_weights(self, layer: nn.Layer, state_dict, is_rearrange: bool = False): """ Paddle cutlass process prequanted weights. """ diff --git a/fastdeploy/model_executor/layers/moe/fused_moe_xpu_backend.py b/fastdeploy/model_executor/layers/moe/fused_moe_xpu_backend.py index 190e8d425b4..80de3d3a0cb 100644 --- a/fastdeploy/model_executor/layers/moe/fused_moe_xpu_backend.py +++ b/fastdeploy/model_executor/layers/moe/fused_moe_xpu_backend.py @@ -34,7 +34,7 @@ class XPUMoEMethod(UnquantizedFusedMoEMethod): def process_loaded_weights(self, layer: nn.Layer, state_dict): - up_gate_proj_weights, down_proj_weights = layer.extract_moe_ffn_weights(state_dict) + up_gate_proj_weights, down_proj_weights, _, _ = layer.extract_moe_ffn_weights(state_dict) for weights in [up_gate_proj_weights, down_proj_weights]: for idx, weight in enumerate(weights): weights[idx] = weight.transpose([1, 0]) @@ -119,7 +119,7 @@ def create_weights(self, layer: nn.Layer, state_dict: Dict[str, paddle.Tensor]): """ Paddle cutlass create weight process. """ - up_gate_proj_weights, down_proj_weights = layer.extract_moe_ffn_weights(state_dict) + up_gate_proj_weights, down_proj_weights, _, _ = layer.extract_moe_ffn_weights(state_dict) assert len(up_gate_proj_weights) == layer.num_local_experts assert len(down_proj_weights) == layer.num_local_experts assert up_gate_proj_weights[0].shape == [ diff --git a/fastdeploy/model_executor/layers/moe/moe.py b/fastdeploy/model_executor/layers/moe/moe.py index 28b9afdbe12..061d1c71de1 100644 --- a/fastdeploy/model_executor/layers/moe/moe.py +++ b/fastdeploy/model_executor/layers/moe/moe.py @@ -78,6 +78,7 @@ def __init__( routed_scaling_factor: float = 1.0, layer_idx: int = -1, moe_tag: str = "", + redundant_table_manger: RedundantExpertManger = None, weight_key_map: dict = {}, ): """ @@ -140,15 +141,8 @@ def __init__( self.moe_quant_type = moe_quant_config.name() else: self.quant_method = get_moe_method() - self.redundant_table_manger = None + self.redundant_table_manger = redundant_table_manger if self.ep_size > 1: - if fd_config.model_config.enable_redundant_experts is True: - self.redundant_table_manger = RedundantExpertManger( - n_routed_experts=fd_config.model_config.moe_num_experts, - num_hidden_layers=fd_config.model_config.num_hidden_layers, - redundant_experts_num=fd_config.model_config.redundant_experts_num, - ep_size=self.ep_size, - ) self.quant_method.init_ep(self) if fd_config.load_config.dynamic_load_weight: @@ -400,6 +394,7 @@ def load_experts_weight( state_dict: dict, up_gate_proj_expert_weight_key: str, down_proj_expert_weight_key: str, + is_rearrange: bool = False, ): """ Load experts weight from state_dict. @@ -428,7 +423,12 @@ def load_experts_weight( ] up_gate_proj_weights = [] down_proj_weights = [] - is_ffn_merged = up_gate_proj_expert_weight_key.format(self.expert_id_offset) in state_dict + if isinstance(state_dict, list): + state_dict = dict(state_dict) + is_ffn_merged = ( + up_gate_proj_expert_weight_key.format(logical_expert_ids[0] if is_rearrange else self.expert_id_offset) + in state_dict + ) if is_ffn_merged: for expert_idx in logical_expert_ids: down_proj_expert_weight_key_name = down_proj_expert_weight_key.format(expert_idx) @@ -510,10 +510,12 @@ def extract_moe_ffn_weights(self, state_dict: dict): assert up_gate_proj_expert_weight_key is not None, "up_gate_proj_expert_weight_key should not be none." assert down_proj_expert_weight_key is not None, "down_proj_expert_weight_key should not be none." - up_gate_proj_weights, down_proj_weights, logical_expert_ids, _ = self.load_experts_weight( - state_dict, - up_gate_proj_expert_weight_key, - down_proj_expert_weight_key, + up_gate_proj_weights, down_proj_weights, logical_expert_ids, ep_rank_to_expert_id_list = ( + self.load_experts_weight( + state_dict, + up_gate_proj_expert_weight_key, + down_proj_expert_weight_key, + ) ) assert ( len(up_gate_proj_weights) == self.num_local_experts @@ -522,7 +524,7 @@ def extract_moe_ffn_weights(self, state_dict: dict): len(down_proj_weights) == self.num_local_experts ), "down_proj_weights length should be equal to num_local_experts." - return up_gate_proj_weights, down_proj_weights + return up_gate_proj_weights, down_proj_weights, logical_expert_ids, ep_rank_to_expert_id_list def extract_gate_correction_bias(self, gate_correction_bias_key, state_dict): """ @@ -551,7 +553,7 @@ def load_state_dict(self, state_dict, is_rearrange: bool = False): if is_supported_moe_backend is not None and is_supported_moe_backend(self.quant_method): if self.fd_config.model_config.is_quantized: if getattr(self.fd_config.quant_config, "is_permuted", True): - self.quant_method.process_prequanted_weights(self, state_dict) + self.quant_method.process_prequanted_weights(self, state_dict, is_rearrange) else: self.quant_method.process_loaded_weights(self, state_dict) else: @@ -559,7 +561,7 @@ def load_state_dict(self, state_dict, is_rearrange: bool = False): else: if self.fd_config.model_config.is_quantized: if getattr(self.fd_config.quant_config, "is_permuted", True): - self.quant_method.process_prequanted_weights(self, state_dict) + self.quant_method.process_prequanted_weights(self, state_dict, is_rearrange) else: self.quant_method.create_weights(self, state_dict) else: diff --git a/fastdeploy/model_executor/layers/quantization/block_wise_fp8.py b/fastdeploy/model_executor/layers/quantization/block_wise_fp8.py index a003e1888e4..f76ff8ca4b5 100644 --- a/fastdeploy/model_executor/layers/quantization/block_wise_fp8.py +++ b/fastdeploy/model_executor/layers/quantization/block_wise_fp8.py @@ -108,7 +108,7 @@ def process_loaded_weights(self, layer, weights) -> None: layer.weight.copy_(quanted_weight_tensor, False) layer.weight_scale.set_value(weight_block_scale_tensor) - def process_prequanted_weights(self, layer, state_dict): + def process_prequanted_weights(self, layer, state_dict, is_rearrange: bool = False): """ process_prequanted_weights """ diff --git a/fastdeploy/model_executor/layers/quantization/tensor_wise_fp8.py b/fastdeploy/model_executor/layers/quantization/tensor_wise_fp8.py index 9576882ec9c..cf716143701 100644 --- a/fastdeploy/model_executor/layers/quantization/tensor_wise_fp8.py +++ b/fastdeploy/model_executor/layers/quantization/tensor_wise_fp8.py @@ -90,7 +90,7 @@ def create_weights(self, layer, **extra_weight_attrs): default_initializer=paddle.nn.initializer.Constant(0), ) - def process_prequanted_weights(self, layer, state_dict) -> None: + def process_prequanted_weights(self, layer, state_dict, is_rearrange: bool = False) -> None: """ Process pre-quantized weights before applying them to the model Args: diff --git a/fastdeploy/model_executor/layers/quantization/weight_only.py b/fastdeploy/model_executor/layers/quantization/weight_only.py index 4825faaf77e..43a9dab873f 100644 --- a/fastdeploy/model_executor/layers/quantization/weight_only.py +++ b/fastdeploy/model_executor/layers/quantization/weight_only.py @@ -240,7 +240,7 @@ def __init__( ) -> None: super().__init__(quant_config) - def process_prequanted_weights(self, layer, state_dict) -> None: + def process_prequanted_weights(self, layer, state_dict, is_rearrange: bool = False) -> None: """ Process pre-quantized weights before applying them to the model Args: diff --git a/fastdeploy/model_executor/load_weight_utils.py b/fastdeploy/model_executor/load_weight_utils.py index 6aacb3a59c2..7a18462a19f 100644 --- a/fastdeploy/model_executor/load_weight_utils.py +++ b/fastdeploy/model_executor/load_weight_utils.py @@ -126,7 +126,11 @@ def get_expert_ranges(fd_config): num_local_ffn_keys.append(down_proj_in_scale_key) # for EP w4a8, we need all expert's activation_scale for up_gate_proj - for j in range(fd_config.model_config.moe_num_experts): + num_experts = fd_config.model_config.moe_num_experts + if isinstance(num_experts, list): + num_experts = num_experts[0] + + for j in range(num_experts): up_gate_proj_in_scale_key = f"ernie.layers.{i}.mlp.experts.{j}.up_gate_proj.activation_scale" num_local_ffn_keys.append(up_gate_proj_in_scale_key) diff --git a/fastdeploy/model_executor/models/ernie4_5_moe.py b/fastdeploy/model_executor/models/ernie4_5_moe.py index c4f8b087202..12197d6ab57 100644 --- a/fastdeploy/model_executor/models/ernie4_5_moe.py +++ b/fastdeploy/model_executor/models/ernie4_5_moe.py @@ -48,6 +48,7 @@ from fastdeploy.model_executor.models.tp_utils import TensorSplitMode as tsm from fastdeploy.model_executor.models.utils import LayerIdPlaceholder as layerid from fastdeploy.model_executor.models.utils import WeightMeta +from fastdeploy.worker.experts_manager import RedundantExpertManger class Ernie4_5_MLP(nn.Layer): @@ -96,7 +97,9 @@ def forward(self, hidden_states: paddle.Tensor): class Ernie4_5_MoE(nn.Layer): - def __init__(self, fd_config: FDConfig, layer_id: int, prefix: str) -> None: + def __init__( + self, fd_config: FDConfig, layer_id: int, prefix: str, redundant_table_manger: RedundantExpertManger = None + ) -> None: super().__init__() moe_quant_type = "" if hasattr(fd_config.quant_config, "moe_quant_type"): @@ -156,6 +159,7 @@ def __init__(self, fd_config: FDConfig, layer_id: int, prefix: str) -> None: top_k=fd_config.model_config.moe_k, layer_idx=layer_id, weight_key_map=weight_key_map, + redundant_table_manger=redundant_table_manger, ) self.gate = ReplicatedLinear( @@ -183,6 +187,9 @@ def load_state_dict(self, state_dict): if self.num_shared_experts > 0: self.shared_experts.load_state_dict(state_dict) + def update_state_dict(self, state_dict): + self.fused_moe.load_state_dict(state_dict, True) + def forward(self, hidden_states: paddle.Tensor): out = self.experts(hidden_states, self.gate) if self.num_shared_experts > 0: @@ -239,6 +246,7 @@ class Ernie4_5_DecoderLayer(nn.Layer): def __init__( self, fd_config: FDConfig, + redundant_table_manger: RedundantExpertManger = None, prefix: str = "", ) -> None: super().__init__() @@ -247,6 +255,7 @@ def __init__( self.self_attn = Ernie4_5_Attention( fd_config=fd_config, layer_id=layer_id, + redundant_table_manger=redundant_table_manger, prefix=f"{prefix}.self_attn", ) @@ -286,6 +295,9 @@ def load_state_dict(self, state_dict): self.input_layernorm.load_state_dict(state_dict) self.post_attention_layernorm.load_state_dict(state_dict) + def update_state_dict(self, state_dict): + self.mlp.update_state_dict(state_dict) + def forward( self, forward_meta: ForwardMeta, @@ -326,6 +338,15 @@ def __init__( self.num_layers = fd_config.model_config.num_hidden_layers fd_config.model_config.pretrained_config.prefix_name = "ernie" + self.fd_config = fd_config + self.redundant_table_manger = None + if fd_config.model_config.enable_redundant_experts is True: + self.redundant_table_manger = RedundantExpertManger( + n_routed_experts=fd_config.model_config.moe_num_experts, + num_hidden_layers=fd_config.model_config.num_hidden_layers, + redundant_experts_num=fd_config.model_config.redundant_experts_num, + ep_size=fd_config.parallel_config.expert_parallel_size, + ) self.embed_tokens = VocabParallelEmbedding( fd_config=fd_config, @@ -339,6 +360,7 @@ def __init__( [ Ernie4_5_DecoderLayer( fd_config=fd_config, + redundant_table_manger=self.redundant_table_manger, prefix=f"{fd_config.model_config.pretrained_config.prefix_name}.layers.{i}", ) for i in range(self.num_layers) @@ -367,6 +389,22 @@ def load_state_dict(self, state_dict): logger.info(f"Start load layer {i}") self.layers[i].load_state_dict(state_dict) + def update_state_dict(self, state_dict): + """ + Update model parameters from a given state dictionary. + + Args: + state_dict (dict[str, np.ndarray | paddle.Tensor]): + A dictionary containing model parameters, where keys are parameter names + and values are NumPy arrays or PaddlePaddle tensors. + """ + for i in range( + self.fd_config.model_config.moe_layer_start_index, + self.fd_config.model_config.num_hidden_layers, + ): + logger.info(f"Start update layer {i}") + self.layers[i].update_state_dict(state_dict) + def forward( self, ids_remove_padding: paddle.Tensor, diff --git a/fastdeploy/model_executor/models/ernie4_5_vl/ernie4_5_vl_moe.py b/fastdeploy/model_executor/models/ernie4_5_vl/ernie4_5_vl_moe.py index 92146b19a4e..07c03c178a1 100644 --- a/fastdeploy/model_executor/models/ernie4_5_vl/ernie4_5_vl_moe.py +++ b/fastdeploy/model_executor/models/ernie4_5_vl/ernie4_5_vl_moe.py @@ -78,8 +78,8 @@ class Ernie4_5_VLMoeBlock(nn.Layer): def __init__(self, fd_config: FDConfig, layer_id: int, prefix: str, moe_tag: str, expert_id_offset: int) -> None: super().__init__() moe_quant_type = "" - if hasattr(fd_config, "quant_config") and fd_config.quant_config is not None: - moe_quant_type = getattr(fd_config.quant_config, "name", lambda: "")() + if hasattr(fd_config.quant_config, "moe_quant_type"): + moe_quant_type = fd_config.quant_config.moe_quant_type if moe_quant_type == "tensor_wise_fp8" or ( moe_quant_type == "block_wise_fp8" and fd_config.model_config.is_quantized diff --git a/scripts/run_ci_xpu.sh b/scripts/run_ci_xpu.sh index 04d4b8699e0..3b0c4252a0d 100644 --- a/scripts/run_ci_xpu.sh +++ b/scripts/run_ci_xpu.sh @@ -152,7 +152,7 @@ ps -efww | grep -E 'api_server' | grep -v grep | awk '{print $2}' | xargs kill - ps -efww | grep -E '8188' | grep -v grep | awk '{print $2}' | xargs kill -9 || true lsof -t -i :8188 | xargs kill -9 || true -if [ ${exit_code} -ne 0 ]; then +if [ ${kv_block_test_exit_code} -ne 0 ]; then echo "log/workerlog.0" cat log/workerlog.0 echo "kv block相关测试失败,请检查pr代码"