Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
175 changes: 175 additions & 0 deletions scripts/configs/infiniops_ops_mars_llama.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
{
"add": {
"implementations": "all"
},
"argmax": {
"implementations": [
8
]
},
"cast": {
"public_api_only": true
},
"cat": {
"public_api_only": true
},
"causal_conv1d": {
"public_api_only": true
},
"chunk_gated_delta_rule": {
"public_api_only": true
},
"convolution": {
"public_api_only": true
},
"copy": {
"implementations": "all"
},
"deepseek_moe": {
"public_api_only": true
},
"embedding": {
"implementations": "all"
},
"fill": {
"implementations": "all"
},
"flash_attn_varlen_func": {
"implementations": "all"
},
"flash_attn_with_kvcache": {
"implementations": "all"
},
"fused_add_rms_norm": {
"implementations": "all"
},
"fused_gated_delta_net_gating": {
"public_api_only": true
},
"fused_qk_norm": {
"public_api_only": true
},
"fused_qk_rms_norm": {
"public_api_only": true
},
"gelu": {
"public_api_only": true
},
"gemm": {
"implementations": "all"
},
"i8_gemm": {
"public_api_only": true
},
"layer_norm": {
"public_api_only": true
},
"mamba_selective_scan": {
"public_api_only": true
},
"moe_align_block_size": {
"public_api_only": true
},
"moe_fused_dense": {
"public_api_only": true
},
"moe_fused_gate": {
"public_api_only": true
},
"mrope": {
"public_api_only": true
},
"mul": {
"implementations": "all"
},
"pad": {
"public_api_only": true
},
"paged_attention_infinilm": {
"implementations": "all"
},
"paged_attention_prefill_infinilm": {
"implementations": "all"
},
"per_channel_quant_i8": {
"public_api_only": true
},
"per_tensor_dequant_i8": {
"public_api_only": true
},
"per_tensor_quant_i8": {
"public_api_only": true
},
"quick_gelu": {
"public_api_only": true
},
"recurrent_gated_delta_rule": {
"public_api_only": true
},
"relu": {
"public_api_only": true
},
"reshape_and_cache_flash": {
"implementations": "all"
},
"rms_norm": {
"implementations": "all"
},
"rotary_embedding": {
"implementations": "all"
},
"row_l2_normalize": {
"public_api_only": true
},
"separate_table_rope": {
"public_api_only": true
},
"sigmoid": {
"public_api_only": true
},
"silu": {
"public_api_only": true
},
"silu_and_mul": {
"implementations": "all"
},
"softmax": {
"public_api_only": true
},
"sum": {
"public_api_only": true
},
"take": {
"public_api_only": true
},
"top_k_top_p_sampling_from_logits": {
"public_api_only": true
},
"topk_sigmoid": {
"public_api_only": true
},
"topk_softmax": {
"public_api_only": true
},
"tril": {
"public_api_only": true
},
"triu": {
"public_api_only": true
},
"upsample_bicubic2d": {
"public_api_only": true
},
"upsample_bilinear2d": {
"public_api_only": true
},
"w4_marlin_gemm": {
"public_api_only": true
},
"w4_marlin_parameters": {
"public_api_only": true
},
"w4_marlin_repack": {
"public_api_only": true
}
}
21 changes: 20 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ endfunction()

include(GNUInstallDirs)

if(NOT DEFINED INFINI_OPS_JSON_SOURCE_DIR)
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../../../../json/CMakeLists.txt")
set(INFINI_OPS_JSON_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../json"
CACHE PATH "Offline nlohmann_json source" FORCE)
else()
set(INFINI_OPS_JSON_SOURCE_DIR ""
CACHE PATH "Offline nlohmann_json source" FORCE)
endif()
endif()
if(INFINI_OPS_JSON_SOURCE_DIR)
set(FETCHCONTENT_SOURCE_DIR_NLOHMANN_JSON "${INFINI_OPS_JSON_SOURCE_DIR}"
CACHE PATH "Offline nlohmann_json source" FORCE)
endif()

find_package(nlohmann_json 3.12.0 CONFIG QUIET)
if(NOT TARGET nlohmann_json::nlohmann_json)
if(POLICY CMP0135)
Expand Down Expand Up @@ -377,9 +391,10 @@ if(WITH_MARS)
"native/cuda/mars/*.cu"
)

file(GLOB_RECURSE MARS_SOURCES CONFIGURE_DEPENDS ${MARS_PATTERNS})
file(GLOB_RECURSE MARS_SOURCES ${MARS_PATTERNS})
list(FILTER MARS_SOURCES EXCLUDE REGEX
".*/native/cuda/(hygon|iluvatar|metax|moore|nvidia|thead)/.*")
list(FILTER MARS_SOURCES EXCLUDE REGEX "\\.(cc|cpp|cxx)\\.cu$")

set_source_files_properties(${MARS_SOURCES} PROPERTIES LANGUAGE CXX)

Expand All @@ -389,11 +404,14 @@ if(WITH_MARS)
target_include_directories(infiniops PUBLIC
"${HPCC_PATH}/include"
"${HPCC_PATH}/tools/cu-bridge/include")
find_library(HPCC_FLASH_ATTN_LIB NAMES hcFlashAttn
HINTS "${HPCC_PATH}/lib" REQUIRED)
target_link_libraries(infiniops PUBLIC
${HPCC_RUNTIME_LIB}
${HPCC_DNN_LIB}
${HPCC_BLAS_LIB}
${HPCC_OMP_LIB}
${HPCC_FLASH_ATTN_LIB}
)
set_property(TARGET infiniops APPEND PROPERTY
INSTALL_RPATH "${HPCC_PATH}/htgpu_llvm/lib")
Expand Down Expand Up @@ -1226,6 +1244,7 @@ if(TORCH_SOURCES)

# Vendor-specific defines required by forked `torch` headers.
set(_torch_extra_flags "")
list(APPEND _torch_extra_flags "-DWITH_TORCH=1")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果instantiate.h不改,这里应该也可以不改

if(WITH_METAX)
list(APPEND _torch_extra_flags "-DUSE_MACA=1" "-DWITH_METAX=1")
endif()
Expand Down
86 changes: 86 additions & 0 deletions src/native/cuda/mars/ops/flash_attn_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#ifndef INFINI_OPS_MARS_FLASH_ATTN_COMMON_H_
#define INFINI_OPS_MARS_FLASH_ATTN_COMMON_H_

#include <flash_attn/flash_attn.h>

#include <cmath>
#include <cstdint>
#include <optional>
#include <stdexcept>
#include <string>

#include "data_type.h"
#include "tensor.h"

namespace infini::ops::mars_flash_attn {

inline hcflashattnDataType_t ToHcDataType(DataType dtype) {
switch (dtype) {
case DataType::kFloat16:
return HCFLASHATTN_DATATYPE_FP16;
case DataType::kBFloat16:
return HCFLASHATTN_DATATYPE_BF16;
case DataType::kInt32:
return HCFLASHATTN_DATATYPE_INT32;
case DataType::kInt64:
return HCFLASHATTN_DATATYPE_INT64;
case DataType::kFloat32:
return HCFLASHATTN_DATATYPE_FP32;
case DataType::kFloat64:
return HCFLASHATTN_DATATYPE_FP64;
default:
throw std::invalid_argument("unsupported HPCC FlashAttention dtype");
}
}

inline Tensor_t MakeHcTensor(const Tensor& tensor) {
void* data = const_cast<void*>(tensor.data());
const auto dtype = ToHcDataType(tensor.dtype());
const auto size = [&tensor](std::size_t index) {
return static_cast<int>(tensor.size(index));
};
const auto stride = [&tensor](std::size_t index) {
return static_cast<int>(tensor.stride(index));
};
switch (tensor.ndim()) {
case 1:
return make_tensor1d(data, dtype, size(0), stride(0));
case 2:
return make_tensor2d(data, dtype, size(0), size(1), stride(0), stride(1));
case 3:
return make_tensor3d(data, dtype, size(0), size(1), size(2), stride(0),
stride(1), stride(2));
case 4:
return make_tensor4d(data, dtype, size(0), size(1), size(2), size(3),
stride(0), stride(1), stride(2), stride(3));
default:
throw std::invalid_argument("HPCC FlashAttention requires 1D-4D tensors");
}
}

inline void ReleaseHcTensor(Tensor_t& tensor) {
if (tensor) {
release_tensor(tensor);
tensor = nullptr;
}
}

inline hcStream_t ToHcStream(void* stream) {
return reinterpret_cast<hcStream_t>(stream);
}

inline void CheckHcStatus(hcflashattnStatus_t status, const char* call) {
if (status != HCFLASHATTN_STATUS_SUCCESS)
throw std::runtime_error(std::string{"HPCC FlashAttention failed: "} +
call);
}

inline float SoftmaxScaleOrDefault(const std::optional<double>& scale,
std::size_t head_size) {
return static_cast<float>(
scale.value_or(1.0 / std::sqrt(static_cast<double>(head_size))));
}

} // namespace infini::ops::mars_flash_attn

#endif // INFINI_OPS_MARS_FLASH_ATTN_COMMON_H_
61 changes: 61 additions & 0 deletions src/native/cuda/mars/ops/flash_attn_varlen_func/kernel.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "native/cuda/mars/ops/flash_attn_varlen_func/kernel.h"

namespace infini::ops {

void Operator<FlashAttnVarlenFunc, Device::Type::kMars, 0>::operator()(
const Tensor q, const Tensor k, const Tensor v, const Tensor cu_seqlens_q,
const Tensor cu_seqlens_k, const std::optional<Tensor> alibi_slopes,
const std::optional<Tensor> block_table, const int64_t max_seqlen_q,
const int64_t max_seqlen_k, const double dropout_p,
const std::optional<double> softmax_scale, const bool causal,
const std::vector<int64_t> window_size, const double softcap,
const bool deterministic, const bool return_attn_probs, Tensor out,
std::optional<Tensor> softmax_lse, std::optional<Tensor> s_dmask) const {
if (!block_table.has_value())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请把修改中所有的throw error都改成assert吧,这个代码库目前贡献准则中指定用assert,不用error。link
示例

throw std::invalid_argument(
"Mars FlashAttnVarlenFunc requires a paged block table");
if (alibi_slopes || softmax_lse || s_dmask || return_attn_probs ||
deterministic || dropout_p != 0.0 || softcap != 0.0)
throw std::invalid_argument(
"Mars FlashAttnVarlenFunc supports inference only");
if (q.dtype() != DataType::kFloat16 && q.dtype() != DataType::kBFloat16)
throw std::invalid_argument(
"Mars FlashAttnVarlenFunc requires float16 or bfloat16");

Tensor_t hc_q = mars_flash_attn::MakeHcTensor(q);
Tensor_t hc_k = mars_flash_attn::MakeHcTensor(k);
Tensor_t hc_v = mars_flash_attn::MakeHcTensor(v);
Tensor_t hc_out = mars_flash_attn::MakeHcTensor(out);
Tensor_t hc_cu_q = mars_flash_attn::MakeHcTensor(cu_seqlens_q);
Tensor_t hc_cu_k = mars_flash_attn::MakeHcTensor(cu_seqlens_k);
Tensor_t hc_block_table = mars_flash_attn::MakeHcTensor(*block_table);
hcflashattnExtendParameter_t extend = make_extend_param();
set_extend_parameter_block_table(extend,
const_cast<void*>(block_table->data()),
static_cast<int>(block_table->size(0)),
static_cast<int>(block_table->size(1)));

const int64_t batch_size = cu_seqlens_q.numel() - 1;
const int64_t total_k = k.size(0) * k.size(1);
const float scale = mars_flash_attn::SoftmaxScaleOrDefault(
softmax_scale, static_cast<std::size_t>(q.size(2)));
const auto status = mha_varlen_fwd(
batch_size, q.size(0), q.size(1), total_k, k.size(2), q.size(2), hc_q,
hc_k, hc_v, hc_out, hc_cu_q, hc_cu_k, nullptr, nullptr, nullptr, nullptr,
nullptr, static_cast<int>(max_seqlen_q), static_cast<int>(max_seqlen_k),
0.0f, scale, causal, static_cast<int>(window_size.at(0)),
static_cast<int>(window_size.at(1)),
mars_flash_attn::ToHcStream(stream_ ? stream_ : nullptr), extend);

release_extend_param(extend);
mars_flash_attn::ReleaseHcTensor(hc_block_table);
mars_flash_attn::ReleaseHcTensor(hc_cu_k);
mars_flash_attn::ReleaseHcTensor(hc_cu_q);
mars_flash_attn::ReleaseHcTensor(hc_out);
mars_flash_attn::ReleaseHcTensor(hc_v);
mars_flash_attn::ReleaseHcTensor(hc_k);
mars_flash_attn::ReleaseHcTensor(hc_q);
mars_flash_attn::CheckHcStatus(status, "mha_varlen_fwd");
}

} // namespace infini::ops
Loading
Loading