File size: 4,214 Bytes
9425aed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#pragma once
#include <stddef.h>
#include <stdint.h>
#include "rowm_cuda_validation.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef uint64_t  CUdeviceptr;
typedef int32_t   CUdevice;
typedef int32_t   CUdevice_attribute;
typedef int32_t   CUresult;
typedef struct CUctx_st*    CUcontext;
typedef struct CUmod_st*    CUmodule;
typedef struct CUfunc_st*   CUfunction;
typedef struct CUstream_st* CUstream;
typedef struct CUevent_st*  CUevent;

typedef struct {
    uint32_t driver_version;
    uint32_t device_ordinal;
    uint32_t compute_capability_major;
    uint32_t compute_capability_minor;
    uint8_t  device_uuid[16];
    char     device_name[64];
    uint64_t context_generation;
} sov_cuda_runtime_identity_t;

enum {
    CUDA_SUCCESS               = 0,
    CUDA_ERROR_INVALID_VALUE   = 1,
    CUDA_ERROR_OUT_OF_MEMORY   = 2,
    CUDA_ERROR_NOT_INITIALIZED = 3,
    CUDA_ERROR_DEINITIALIZED   = 4,
    CUDA_ERROR_NO_DEVICE       = 100,
    CUDA_ERROR_INVALID_DEVICE  = 101,
    CUDA_ERROR_INVALID_IMAGE   = 200,
    CUDA_ERROR_INVALID_CONTEXT = 201,
    CUDA_ERROR_NOT_FOUND       = 500,
    CUDA_ERROR_UNKNOWN         = 999
};

/* Driver and primary-context lifetime */
int      sov_cuda_init(void);
void     sov_cuda_shutdown(void);
int      sov_cuda_is_initialized(void);
uint64_t sov_cuda_context_generation(void);
CUresult sov_cuda_get_runtime_identity(sov_cuda_runtime_identity_t* identity_out);

/* Checked access to the dynamically resolved CUDA Driver API */
CUresult sov_cuda_module_load_data(CUmodule* module, const void* image);
CUresult sov_cuda_module_get_function(CUfunction* function, CUmodule module,

                                      const char* name);
CUresult sov_cuda_module_get_global(CUdeviceptr* device_ptr, size_t* bytes,

                                    CUmodule module, const char* name);
CUresult sov_cuda_module_unload(CUmodule module);
/*

 * The raw launch boundary is fail-closed: even callers that bypass the

 * higher-level dispatchers need a ROWM-NR commit for the active context.

 */
CUresult sov_cuda_launch_kernel(CUfunction function,

                                unsigned int grid_x,

                                unsigned int grid_y,

                                unsigned int grid_z,

                                unsigned int block_x,

                                unsigned int block_y,

                                unsigned int block_z,

                                unsigned int shared_mem_bytes,

                                CUstream stream,

                                void** kernel_params,

                                void** extra);
CUresult sov_cuda_mem_alloc(CUdeviceptr* device_ptr, size_t bytes);
CUresult sov_cuda_mem_free(CUdeviceptr device_ptr);
int      sov_cuda_memcpy_h2d(void* device_dst, const void* host_src, size_t bytes);
CUresult sov_cuda_memcpy_d2h(void* host_dst, CUdeviceptr device_src, size_t bytes);

/* Power state device symbol binding */
CUresult sov_cuda_register_power_state_device(CUdeviceptr device_ptr);
CUresult sov_cuda_sync_power_state(void);

/* PTX module dispatch */
int  sov_cuda_kernels_init(void);
int  sov_cuda_kernels_authorize_rowm(sov_rowm_commit_cuda_validation_fn commit_fn,

                                      void* rowm_context);
void sov_cuda_kernels_shutdown(void);
int  sov_cuda_rmsnorm_fused(CUdeviceptr x, CUdeviceptr weight,

                            CUdeviceptr out, uint32_t element_count);
int  sov_cuda_silu_fused(CUdeviceptr x, CUdeviceptr out, uint32_t element_count);

/* GEMM dispatch */
int  sov_cuda_gemm_init(void);
void sov_cuda_gemm_shutdown(void);
int  sov_cuda_gemm(CUdeviceptr A, CUdeviceptr B, CUdeviceptr C,

                   int M, int N, int K);
int  sov_cuda_gemm_ex(CUdeviceptr A, CUdeviceptr B, CUdeviceptr C,

                      int M, int N, int K, int lda, int ldb, int ldc);

/* Compatibility entry points retained for the existing RTX public API */
int   sov_cuda_load_ptx(const char* ptx_data, unsigned int ptx_size,

                        void** module_out);
void* sov_cuda_malloc(size_t bytes);

#ifdef __cplusplus
}
#endif