Skip to content
Open

Qgy #1054

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
953f87b
feat: add native Hygon operators for DeepSeek V4 Flash
Nov 16, 2026
2b0cd1a
fix: support device graph capture for Hygon operators
Nov 16, 2026
e058c3a
fix: align Hygon RMSNorm rounding with SGLang
Nov 16, 2026
0f88982
fix: align Hygon rotary embedding rounding with SGLang
Nov 16, 2026
07f976b
chore: merge latest master updates
Nov 16, 2026
35685ee
perf: optimize DeepSeek V4 Flash mHC on Hygon
Nov 18, 2026
02d009f
fix: enable device graph capture for Hygon Argmax
Nov 18, 2026
e8698a4
feat: add native Hygon MoE block alignment
Nov 18, 2026
c0cdcb5
fix: preserve expert remapping and bias rounding in Torch MoE
Nov 18, 2026
9baca9f
fix: link Python for Hygon FlashAttention builds
Nov 18, 2026
0aeb6cb
chore: merge origin master into szl
Nov 18, 2026
945af9a
feat(hygon): add optimized VideoNSA operators
2506363862 Nov 19, 2026
cbb11ed
feat: add optimized Hygon Mamba conv and scan kernels
zhangxk11 Nov 20, 2026
36fbfcc
Merge pull request #2 from szl1369117765/zxk
szl1369117765 Nov 20, 2026
1eff391
chore: remove the VideoNSA migration document
2506363862 Nov 20, 2026
5df0b17
refactor: move the shared VideoNSA validation header
2506363862 Nov 20, 2026
a46b969
Merge pull request #1 from szl1369117765/yxd
szl1369117765 Nov 24, 2026
eab14ff
perf(hygon): migrate validated routing and INT8 kernels
qinguoyong Nov 24, 2026
974f94f
fix(ops): preserve scalar metadata and reject unsafe CPU casts
qinguoyong Nov 24, 2026
c116490
perf(hygon): accelerate routed and shared W8A8 experts
qinguoyong Nov 24, 2026
ed023dc
perf(hygon): dispatch large low-precision matmul to BLAS
qinguoyong Nov 25, 2026
64d2a84
perf(hygon): accelerate small-batch low-precision matmul
qinguoyong Nov 25, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ endif()
# consume DLPack views and their own runtime. Discover Torch lazily only when
# native Torch wrappers or a selected linked Torch transport need it.
macro(infiniops_find_torch)
if(WITH_MARS AND WITH_TORCH AND WITH_LINKED)
if((WITH_MARS OR WITH_HYGON) AND WITH_TORCH AND WITH_LINKED)
find_package(Python COMPONENTS Interpreter Development.Embed REQUIRED)
else()
find_package(Python COMPONENTS Interpreter REQUIRED)
Expand Down Expand Up @@ -351,10 +351,10 @@ macro(infiniops_find_torch)
-Wl,--no-as-needed ${TORCH_HIP_LIB} ${C10_HIP_LIB} -Wl,--as-needed)
endif()

if(WITH_MARS AND WITH_TORCH AND WITH_LINKED)
# The Mars FlashAttention wheel loads libtorch_python without declaring
if((WITH_MARS OR WITH_HYGON) AND WITH_TORCH AND WITH_LINKED)
# The Mars/Hygon FlashAttention wheels load libtorch_python without declaring
# its libpython dependency, so keep the embedding library in our chain.
list(APPEND TORCH_LIBRARIES Python::Python)
list(APPEND TORCH_LIBRARIES -Wl,--no-as-needed Python::Python -Wl,--as-needed)
endif()

if(WITH_METAX)
Expand Down
43 changes: 43 additions & 0 deletions cmake/NativeFlashMlaHygon.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Build the pinned Hygon FlashMLA kernels with their HIP compiler mode.
function(infiniops_add_hygon_flashmla target)
set(_root "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/..")
set(_source "${_root}/third_party/flashmla_hygon/csrc")
set(_bridge "${_root}/src/native/cuda/hygon/flash_mla")
find_program(_flashmla_aicc NAMES aicc HINTS "${DTK_ROOT}/bin" REQUIRED)
set(_hip_sources
"${_source}/gfx9/decode/combine/combine.hip"
"${_source}/gfx9/decode/get_decoding_sched_meta/get_decoding_sched_meta.hip"
"${_source}/gfx93/decode/sparse_fp8/instantiations/model1_persistent_h64.hip"
"${_bridge}/bridge.hip")
file(GLOB_RECURSE _headers CONFIGURE_DEPENDS "${_source}/*.h" "${_source}/*.hpp" "${_source}/*.cuh")
set(_objects)
foreach(_src IN LISTS _hip_sources)
get_filename_component(_name "${_src}" NAME_WE)
set(_object "${CMAKE_CURRENT_BINARY_DIR}/hygon-flashmla/${_name}.o")
add_custom_command(OUTPUT "${_object}"
COMMAND "${CMAKE_COMMAND}" -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/hygon-flashmla"
COMMAND "${CMAKE_COMMAND}" -E env CPLUS_INCLUDE_PATH= C_INCLUDE_PATH= CPATH=
"${_flashmla_aicc}" --rocm-path=${DTK_ROOT} --hip-path=${DTK_ROOT}/hip
-c "${_src}" -o "${_object}"
-O3 -std=c++20 -fPIC -w -D__HIP_PLATFORM_AMD__ -D__HIP_PLATFORM_HCC__
-DUSE_ROCM -DFLASH_MLA_BF16_TYPE=1 -DHIP_ENABLE_WARP_SYNC_BUILTINS=1 -DDCU_ASM
-ffast-math -fno-finite-math-only --offload-arch=${HYGON_ARCH}
-mcode-object-version=5 -fno-gpu-rdc --gpu-max-threads-per-block=256
-mllvm=-support-768-vgprs=true -mllvm=-disable-machine-sink -mllvm=-disable-code-sink
-mllvm=-amdgpu-enable-rewrite-partial-reg-uses=false
-mllvm=-allow-gvn-convergent-call=true -mllvm=-disallow-uniform-vmed3-combine=true
-mllvm=-hcu-pre-emit-load-store-opt=false -mllvm=-amdgpu-early-inline-all=true
-mllvm=-amdgpu-function-calls=false
"-I${_source}" "-I${_source}/kerutils/include" "-I${_source}/gfx93"
"-I${_source}/cutlass/cutlass_3.2.1/include" "-I${DTK_ROOT}/include" "-I${_bridge}"
DEPENDS "${_src}" "${_bridge}/bridge.hpp" ${_headers}
VERBATIM)
list(APPEND _objects "${_object}")
endforeach()
add_custom_target(${target} DEPENDS ${_objects})
set_property(TARGET ${target} PROPERTY INFINIOPS_OBJECTS "${_objects}")
install(FILES "${_root}/third_party/flashmla_hygon/LICENSE"
DESTINATION share/infiniops/licenses RENAME FlashMLA-Hygon.txt)
install(FILES "${_root}/third_party/flashmla_hygon/CUTLASS_LICENSE.txt"
DESTINATION share/infiniops/licenses RENAME FlashMLA-CUTLASS.txt)
endfunction()
20 changes: 20 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ if(WITH_HYGON)

target_compile_definitions(infiniops PUBLIC WITH_HYGON=1)
target_sources(infiniops PRIVATE ${HYGON_SOURCES})
# Preserve the qualified VideoNSA kernel compile mode from the old build.
foreach(_nsa_src IN LISTS HYGON_SOURCES)
if(_nsa_src MATCHES "/nsa_videonsa_prefill/legacy/.*[.]cu$")
set_property(SOURCE "${_nsa_src}" APPEND PROPERTY COMPILE_OPTIONS
-Xclang -fno-cuda-host-device-constexpr)
endif()
endforeach()


find_package(CUDAToolkit REQUIRED)
target_link_libraries(infiniops PUBLIC CUDA::cudart CUDA::cublas)
Expand All @@ -310,6 +318,18 @@ if(WITH_HYGON)
CUDA_STANDARD_REQUIRED ON
)

if(HYGON_ARCH MATCHES "^gfx93[68]$")
include("${PROJECT_SOURCE_DIR}/cmake/NativeFlashMlaHygon.cmake")
infiniops_add_hygon_flashmla(infiniops_hygon_flashmla)
add_dependencies(infiniops infiniops_hygon_flashmla)
get_target_property(_flashmla_objects infiniops_hygon_flashmla INFINIOPS_OBJECTS)
set_source_files_properties(${_flashmla_objects} PROPERTIES EXTERNAL_OBJECT TRUE GENERATED TRUE)
target_sources(infiniops PRIVATE ${_flashmla_objects})
target_compile_definitions(infiniops PRIVATE INFINIOPS_HAVE_HYGON_FLASHMLA=1)
target_link_directories(infiniops PRIVATE "${DTK_ROOT}/lib")
target_link_libraries(infiniops PRIVATE amdhip64)
endif()

list(APPEND DEVICE_LIST "hygon")
endif()

Expand Down
14 changes: 14 additions & 0 deletions src/base/deepseek_v4_c4_index.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include "operator.h"

namespace infini::ops {
class DeepseekV4C4Index : public Operator<DeepseekV4C4Index> {
public:
DeepseekV4C4Index(const Tensor key, const Tensor slots, const Tensor query,
const Tensor weights, const Tensor pages, const Tensor lengths,
Tensor cache, Tensor out) {}
virtual void operator()(const Tensor key, const Tensor slots, const Tensor query,
const Tensor weights, const Tensor pages, const Tensor lengths,
Tensor cache, Tensor out) const = 0;
};
}
13 changes: 13 additions & 0 deletions src/base/deepseek_v4_cast_rz.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "operator.h"

namespace infini::ops {
// Explicit HCU writeback boundary for FP32 TP sums. Other casts retain their
// normal rounding semantics.
class DeepseekV4CastRz : public Operator<DeepseekV4CastRz> {
public:
DeepseekV4CastRz(const Tensor input, Tensor out) { (void)input; (void)out; }
virtual void operator()(const Tensor input, Tensor out) const = 0;
};
} // namespace infini::ops
31 changes: 31 additions & 0 deletions src/base/deepseek_v4_compress.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef INFINI_OPS_BASE_DEEPSEEK_V4_COMPRESS_H_
#define INFINI_OPS_BASE_DEEPSEEK_V4_COMPRESS_H_

#include "operator.h"

namespace infini::ops {

// sources encodes input rows (>=0), old state rows (-2-row), or padding (-1).
// C4 has 8 sources ordered previous group then current group. C128 has 128.
// C4 APE can be the prepared [8, dim] or raw checkpoint [4, 2 * dim].
// An all-padding output row is zero. state_slots uses unique nonnegative
// destinations, or -1. Pooling observes the state from before the launch.
class DeepseekV4Compress : public Operator<DeepseekV4Compress> {
public:
DeepseekV4Compress(const Tensor projection, const Tensor ape, const Tensor sources,
const Tensor state_slots, Tensor state, Tensor out) {
(void)projection;
(void)ape;
(void)sources;
(void)state_slots;
(void)state;
(void)out;
}

virtual void operator()(const Tensor projection, const Tensor ape, const Tensor sources,
const Tensor state_slots, Tensor state, Tensor out) const = 0;
};

} // namespace infini::ops

#endif
12 changes: 12 additions & 0 deletions src/base/deepseek_v4_mhc_post.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include "operator.h"

namespace infini::ops {
class DeepseekV4MhcPost : public Operator<DeepseekV4MhcPost> {
public:
DeepseekV4MhcPost(const Tensor activation,const Tensor residual,const Tensor post,
const Tensor combination,Tensor out) {}
virtual void operator()(const Tensor activation,const Tensor residual,const Tensor post,
const Tensor combination,Tensor out) const = 0;
};
}
14 changes: 14 additions & 0 deletions src/base/deepseek_v4_mhc_pre.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include "operator.h"

namespace infini::ops {
class DeepseekV4MhcPre : public Operator<DeepseekV4MhcPre> {
public:
DeepseekV4MhcPre(const Tensor residual,const Tensor projection,const Tensor scales,
const Tensor bias,double norm_eps,double mix_eps,int64_t iterations,
Tensor collapsed,Tensor post,Tensor combination) {}
virtual void operator()(const Tensor residual,const Tensor projection,const Tensor scales,
const Tensor bias,double norm_eps,double mix_eps,int64_t iterations,
Tensor collapsed,Tensor post,Tensor combination) const = 0;
};
}
13 changes: 13 additions & 0 deletions src/base/deepseek_v4_rotary_frequencies.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
#include "operator.h"
namespace infini::ops {
class DeepseekV4RotaryFrequencies : public Operator<DeepseekV4RotaryFrequencies> {
public:
DeepseekV4RotaryFrequencies(Tensor out,double theta,bool scaled,double low,double high,
double factor,bool split_cos_sin) {
(void)out;(void)theta;(void)scaled;(void)low;(void)high;(void)factor;(void)split_cos_sin;
}
virtual void operator()(Tensor out,double theta,bool scaled,double low,double high,
double factor,bool split_cos_sin) const = 0;
};
} // namespace infini::ops
14 changes: 14 additions & 0 deletions src/base/deepseek_v4_route.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include "operator.h"

namespace infini::ops {
class DeepseekV4Route : public Operator<DeepseekV4Route> {
public:
DeepseekV4Route(const Tensor logits,const std::optional<Tensor> bias,
const std::optional<Tensor> tokens,const std::optional<Tensor> hash,
double scale,Tensor weights,Tensor indices) {}
virtual void operator()(const Tensor logits,const std::optional<Tensor> bias,
const std::optional<Tensor> tokens,const std::optional<Tensor> hash,
double scale,Tensor weights,Tensor indices) const = 0;
};
}
39 changes: 39 additions & 0 deletions src/base/deepseek_v4_sparse_mla.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef INFINI_OPS_BASE_DEEPSEEK_V4_SPARSE_MLA_H_
#define INFINI_OPS_BASE_DEEPSEEK_V4_SPARSE_MLA_H_

#include "operator.h"

namespace infini::ops {

// Reference FlashMLA ABI: queries [rows, heads, 512]; KV pages are packed
// FP8 NoPE plus BF16 RoPE and separate per-token exponent bytes.
class DeepseekV4SparseMla : public Operator<DeepseekV4SparseMla> {
public:
DeepseekV4SparseMla(const Tensor query, const Tensor swa, const Tensor indices,
const Tensor lengths, const Tensor sink, const std::optional<Tensor> extra,
const std::optional<Tensor> extra_indices, const std::optional<Tensor> extra_lengths,
int64_t extra_page_size, double scale, Tensor out) {
(void)query;
(void)swa;
(void)indices;
(void)lengths;
(void)sink;
(void)extra;
(void)extra_indices;
(void)extra_lengths;
(void)extra_page_size;
(void)scale;
(void)out;
}

virtual void operator()(const Tensor query, const Tensor swa, const Tensor indices,
const Tensor lengths, const Tensor sink,
const std::optional<Tensor> extra,
const std::optional<Tensor> extra_indices,
const std::optional<Tensor> extra_lengths,
int64_t extra_page_size, double scale, Tensor out) const = 0;
};

} // namespace infini::ops

#endif
24 changes: 24 additions & 0 deletions src/base/deepseek_v4_store_mla.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef INFINI_OPS_BASE_DEEPSEEK_V4_STORE_MLA_H_
#define INFINI_OPS_BASE_DEEPSEEK_V4_STORE_MLA_H_

#include "operator.h"

namespace infini::ops {

// Store BF16 or FP32 KV rows into DeepSeek V4's packed page format. Nonnegative
// slots must be unique within a launch; -1 skips a row.
class DeepseekV4StoreMla : public Operator<DeepseekV4StoreMla> {
public:
DeepseekV4StoreMla(const Tensor kv, const Tensor slots, int64_t page_tokens, Tensor pages) {
(void)kv;
(void)slots;
(void)page_tokens;
(void)pages;
}

virtual void operator()(const Tensor kv, const Tensor slots, int64_t page_tokens,
Tensor pages) const = 0;
};
} // namespace infini::ops

#endif
39 changes: 39 additions & 0 deletions src/base/deepseek_v4_w8a8_experts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef INFINI_OPS_BASE_DEEPSEEK_V4_W8A8_EXPERTS_H_
#define INFINI_OPS_BASE_DEEPSEEK_V4_W8A8_EXPERTS_H_

#include "operator.h"

namespace infini::ops {

// Reference W8A8 path: channelwise weights and per-row symmetric activation
// quantization and INT32 dot products. Routed experts round each weighted
// contribution to the activation dtype before FP32 reduction and scaling.
// A null routed_scale selects the shared-expert activation and epilogue.
// Weight tensors are rank-local. TP reduction is the caller's responsibility.
class DeepseekV4W8a8Experts : public Operator<DeepseekV4W8a8Experts> {
public:
DeepseekV4W8a8Experts(const Tensor x, const Tensor weights, const Tensor indices,
const Tensor gate_up, const Tensor gate_up_scale,
const Tensor down, const Tensor down_scale,
double swiglu_limit, const std::optional<double> routed_scale, Tensor out) {
(void)x;
(void)weights;
(void)indices;
(void)gate_up;
(void)gate_up_scale;
(void)down;
(void)down_scale;
(void)swiglu_limit;
(void)routed_scale;
(void)out;
}

virtual void operator()(const Tensor x, const Tensor weights, const Tensor indices,
const Tensor gate_up, const Tensor gate_up_scale,
const Tensor down, const Tensor down_scale,
double swiglu_limit, const std::optional<double> routed_scale, Tensor out) const = 0;
};

} // namespace infini::ops

#endif
15 changes: 13 additions & 2 deletions src/base/mul.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,22 @@ class Mul : public Operator<Mul> {
}

Mul(const Tensor input, const double other, Tensor out)
: output_size_(out.numel()),
: ndim_(out.ndim()),
output_size_(out.numel()),
input_type_(input.dtype()),
other_type_(input.dtype()),
out_type_(out.dtype()),
scalar_mode_(true) {}
input_shape_(input.shape()),
out_shape_(out.shape()),
input_strides_(input.strides()),
out_strides_(out.strides()),
is_input_contiguous_(input.IsContiguous()),
is_out_contiguous_(out.IsContiguous()),
scalar_mode_(true) {
if (input.shape() != out.shape() || input.dtype() != out.dtype())
throw std::invalid_argument(
"scalar Mul requires matching input/output shape and dtype");
}

virtual void operator()(const Tensor input, const Tensor other,
Tensor out) const = 0;
Expand Down
41 changes: 41 additions & 0 deletions src/base/nsa_compress_paged_cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once
#include "operator.h"
#include <array>
#include <stdexcept>
namespace infini::ops {
class NsaCompressPagedCache : public Operator<NsaCompressPagedCache> {
public:
static bool supports(const Tensor kc,const Tensor vc,const Tensor bt,const Tensor lens,
int block,bool tail,Tensor ko,Tensor vo) {
if(kc.ndim()!=4 || vc.shape()!=kc.shape() || ko.ndim()!=3 || vo.shape()!=ko.shape() ||
bt.ndim()!=2 || lens.ndim()!=1 || lens.size(0)!=bt.size(0) ||
(kc.dtype()!=DataType::kFloat16 && kc.dtype()!=DataType::kBFloat16) ||
vc.dtype()!=kc.dtype() || ko.dtype()!=kc.dtype() || vo.dtype()!=kc.dtype() ||
lens.dtype()!=bt.dtype() || (bt.dtype()!=DataType::kInt32 && bt.dtype()!=DataType::kInt64 && bt.dtype()!=DataType::kUInt32) ||
!kc.size(0) || !kc.size(1) || !kc.size(2) || kc.size(3)!=128 || block<=0 ||
kc.size(2)%size_t(block)!=0 || bt.size(1)==0 ||
ko.size(0)!=kc.size(0)*(kc.size(2)/size_t(block)) || ko.size(1)!=kc.size(1) || ko.size(2)!=128 ||
kc.stride(3)!=1 || vc.stride(3)!=1 || ko.stride(2)!=1 || vo.stride(2)!=1 || bt.stride(1)!=1 || lens.stride(0)!=1) return false;
for(const auto& t:{kc,vc,bt,lens,ko,vo}) {
if(t.device()!=kc.device())return false;
for(auto stride:t.strides())if(stride<0)return false;
}
(void)tail; return true;
}
NsaCompressPagedCache(const Tensor kc,const Tensor vc,const Tensor bt,const Tensor lens,
int block,bool tail,Tensor ko,Tensor vo):metadata_{kc,vc,bt,lens,ko,vo},block_{block},tail_{tail} {
if(!supports(kc,vc,bt,lens,block,tail,ko,vo)) throw std::invalid_argument("Invalid NSA compressed paged cache metadata");
}
virtual void operator()(const Tensor kc,const Tensor vc,const Tensor bt,const Tensor lens,
int block,bool tail,Tensor ko,Tensor vo) const=0;
protected:
void Check(const Tensor kc,const Tensor vc,const Tensor bt,const Tensor lens,
int block,bool tail,Tensor ko,Tensor vo) const {
if(block!=block_ || tail!=tail_ || !supports(kc,vc,bt,lens,block,tail,ko,vo))throw std::invalid_argument("NSA compressed cache invocation differs from plan");
const std::array<Tensor,6> values{kc,vc,bt,lens,ko,vo};
for(size_t i=0;i<values.size();++i)if(!std::equal_to<Tensor>{}(values[i],metadata_[i]))throw std::invalid_argument("NSA compressed cache tensor metadata differs from plan");
}
std::array<Tensor,6> metadata_;
int block_; bool tail_;
};
}
Loading