| #ifdef __HIP_PLATFORM_AMD__ |
| #include "ds4_rocm.h" |
| #include <hipblaslt/hipblaslt.h> |
|
|
| #define FULL_WARP_MASK 0xFFFFFFFFFFFFFFFFULL |
| #define MASK_T uint64_t |
| #define DS4_GPU_BACKEND_NAME "ROCm" |
| #define DS4_GPU_LOG_PREFIX "ds4: ROCm " |
| #define DS4_GPU_BLAS_NAME "hipBLAS" |
| #else |
| #include <cuda_runtime.h> |
| #include <cuda_fp16.h> |
| #include <mma.h> |
| #include <cublas_v2.h> |
| #include <cub/block/block_radix_sort.cuh> |
|
|
| #define FULL_WARP_MASK 0xFFFFFFFFu |
| #define MASK_T uint32_t |
| #define DS4_GPU_BACKEND_NAME "CUDA" |
| #define DS4_GPU_LOG_PREFIX "ds4: CUDA " |
| #define DS4_GPU_BLAS_NAME "cuBLAS" |
| #endif |
|
|
| #include <stdint.h> |
| #include <ctype.h> |
| #include <errno.h> |
| #include <limits.h> |
| #include <math.h> |
| #include <fcntl.h> |
| #include <pthread.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include <sys/stat.h> |
| #include <time.h> |
| #include <unistd.h> |
| #include <unordered_map> |
| #include <vector> |
|
|
| #include "ds4_gpu.h" |
|
|
| #ifndef M_PI |
| #define M_PI 3.14159265358979323846 |
| #endif |
|
|
| #define CUDA_QK_K 256 |
| #define DS4_ROCM_UNUSED __attribute__((unused)) |
|
|
| enum { |
| |
| |
| |
| |
| DS4_ROCM_ATTENTION_SCORE_CAP = 8192u, |
| DS4_ROCM_ATTENTION_RAW_SCORE_CAP = 256u, |
| DS4_ROCM_TOPK_MERGE_GROUP = 8u |
| }; |
|
|
| struct ds4_gpu_tensor { |
| void *ptr; |
| uint64_t bytes; |
| int owner; |
| }; |
|
|
| typedef struct { |
| uint8_t scales[CUDA_QK_K / 16]; |
| uint8_t qs[CUDA_QK_K / 4]; |
| uint16_t d; |
| uint16_t dmin; |
| } cuda_block_q2_K; |
|
|
| typedef struct { |
| uint16_t d; |
| uint16_t dmin; |
| uint8_t scales[12]; |
| uint8_t qs[CUDA_QK_K / 2]; |
| } cuda_block_q4_K; |
|
|
| typedef struct { |
| float d; |
| int8_t qs[CUDA_QK_K]; |
| int16_t bsums[CUDA_QK_K / 16]; |
| } cuda_block_q8_K; |
|
|
| typedef struct { |
| uint16_t d; |
| uint16_t qs[CUDA_QK_K / 8]; |
| } cuda_block_iq2_xxs; |
|
|
| #include "ds4_iq2_tables_cuda.inc" |
|
|
| #include "rocm/ds4_rocm_runtime.cuh" |
|
|
| #include "rocm/ds4_rocm_common.cuh" |
|
|
| #include "rocm/ds4_rocm_q8.cuh" |
|
|
| #include "rocm/ds4_rocm_norm_rope.cuh" |
|
|
| #include "rocm/ds4_rocm_fp8_kv.cuh" |
|
|
| #include "rocm/ds4_rocm_attention.cuh" |
|
|
| #include "rocm/ds4_rocm_hc.cuh" |
|
|
| #include "rocm/ds4_rocm_output.cuh" |
|
|
| #include "rocm/ds4_rocm_indexer.cuh" |
|
|
| #include "rocm/ds4_rocm_embedding_launch.cuh" |
|
|
| #include "rocm/ds4_rocm_matmul.cuh" |
|
|
| #include "rocm/ds4_rocm_fp8_kv_launch.cuh" |
|
|
| #include "rocm/ds4_rocm_compressor.cuh" |
|
|
| #include "rocm/ds4_rocm_attention_launch.cuh" |
|
|
| #include "rocm/ds4_rocm_shared_expert.cuh" |
|
|
| #include "rocm/ds4_rocm_misc_launch.cuh" |
| #include "rocm/ds4_rocm_router.cuh" |
|
|
| #include "rocm/ds4_rocm_moe.cuh" |
|
|
| #include "rocm/ds4_rocm_moe_launch.cuh" |
|
|
| #include "rocm/ds4_rocm_glm.cuh" |
|
|
| #include "rocm/ds4_rocm_hc_output_launch.cuh" |
|
|
| #include "rocm/ds4_rocm_current_api_compat.cuh" |
|
|
| |
| |
| extern "C" int ds4_gpu_tp_gate_encode(uint32_t layer, uint32_t gate) { |
| (void)layer; (void)gate; |
| fprintf(stderr, DS4_GPU_LOG_PREFIX "tensor parallelism is Metal-only\n"); |
| return 0; |
| } |
|
|
| extern "C" void ds4_gpu_tp_set_batch_exchange(ds4_gpu_tp_batch_exchange_fn fn) { |
| (void)fn; |
| } |
|
|
| extern "C" void ds4_gpu_tp_suspend_expert_sharding(int suspend) { |
| (void)suspend; |
| } |
|
|
| extern "C" void ds4_gpu_tp_keepalive_pause(int paused) { |
| (void)paused; |
| } |
|
|
| extern "C" void ds4_gpu_tp_set_attn_head_split(int enabled) { |
| (void)enabled; |
| } |
|
|
| extern "C" void ds4_gpu_model_residency_skip(int skip) { |
| (void)skip; |
| } |
|
|
| extern "C" void ds4_gpu_tp_set_big_exchange(ds4_gpu_tp_big_exchange_fn fn) { |
| (void)fn; |
| } |
|
|
| extern "C" int ds4_gpu_tp_big_gate_encode(uint32_t layer, uint32_t rows, |
| const ds4_gpu_tensor *out_t, |
| ds4_gpu_tensor *in_t, |
| uint64_t bytes) { |
| (void)layer; (void)rows; (void)out_t; (void)in_t; (void)bytes; |
| return 0; |
| } |
|
|
| extern "C" int ds4_gpu_tp_batch_gate_encode(uint32_t layer, uint32_t rows) { |
| (void)layer; (void)rows; |
| fprintf(stderr, DS4_GPU_LOG_PREFIX "tensor parallelism is Metal-only\n"); |
| return 0; |
| } |
|
|
| extern "C" int ds4_gpu_matmul_q8_0_kslice_tensor( |
| ds4_gpu_tensor *out, const void *model_map, uint64_t model_size, |
| uint64_t weight_offset, uint64_t full_in_dim, uint64_t k_off, |
| uint64_t k_cnt, uint64_t out_dim, const ds4_gpu_tensor *x, |
| uint64_t x_elem_off) { |
| (void)out; (void)model_map; (void)model_size; (void)weight_offset; |
| (void)full_in_dim; (void)k_off; (void)k_cnt; (void)out_dim; (void)x; |
| (void)x_elem_off; |
| fprintf(stderr, DS4_GPU_LOG_PREFIX "tensor parallelism is Metal-only\n"); |
| return 0; |
| } |
|
|
| extern "C" int ds4_gpu_attention_output_q8_tp_tensor( |
| ds4_gpu_tensor *out, ds4_gpu_tensor *low, const void *model_map, |
| uint64_t model_size, uint64_t out_a_offset, uint64_t out_b_offset, |
| uint64_t group_dim, uint64_t rank, uint32_t n_groups_total, |
| uint32_t group0, uint32_t group_cnt, uint64_t out_dim, |
| const ds4_gpu_tensor *heads) { |
| (void)out; (void)low; (void)model_map; (void)model_size; |
| (void)out_a_offset; (void)out_b_offset; (void)group_dim; (void)rank; |
| (void)n_groups_total; (void)group0; (void)group_cnt; (void)out_dim; |
| (void)heads; |
| fprintf(stderr, DS4_GPU_LOG_PREFIX "tensor parallelism is Metal-only\n"); |
| return 0; |
| } |
|
|
| extern "C" int ds4_gpu_hc_expand_add_tensor( |
| ds4_gpu_tensor *out_hc, const ds4_gpu_tensor *block_out, |
| const ds4_gpu_tensor *block_add, const ds4_gpu_tensor *residual_hc, |
| const ds4_gpu_tensor *post, const ds4_gpu_tensor *comb, |
| uint32_t n_embd, uint32_t n_hc) { |
| (void)out_hc; (void)block_out; (void)block_add; (void)residual_hc; |
| (void)post; (void)comb; (void)n_embd; (void)n_hc; |
| fprintf(stderr, DS4_GPU_LOG_PREFIX "tensor parallelism is Metal-only\n"); |
| return 0; |
| } |
|
|