sparkleman commited on
Commit ·
3b5e872
1
Parent(s): aeaf225
UPDATE
Browse filesFiles changed (17)
- .gitignore+3-1
- app.py+10-8
- pyproject.toml+1-1
- rwkv/__init__.pyAdded
- cuda/gemm_fp16_cublas.cpp → rwkv/cuda/gemm_fp16_cublas.cppRenamed+75-75
- cuda/operators.cu → rwkv/cuda/operators.cuRenamed+246-246
- cuda/rwkv5.cu → rwkv/cuda/rwkv5.cuRenamed+88-88
- cuda/rwkv5_op.cpp → rwkv/cuda/rwkv5_op.cppRenamed+34-34
- cuda/rwkv6.cu → rwkv/cuda/rwkv6.cuRenamed+87-87
- cuda/rwkv6_op.cpp → rwkv/cuda/rwkv6_op.cppRenamed+34-34
- rwkv/cuda/rwkv7.cuAdded+77
- rwkv/cuda/rwkv7_op.cppAdded+26
- cuda/wrapper.cpp → rwkv/cuda/wrapper.cppRenamed+141-141
- rwkv/model.pyAdded
- rwkv/rwkv_tokenizer.pyAdded+103
- rwkv/rwkv_vocab_v20230424.txtAdded
- rwkv/utils.pyAdded+135
15 unmodified lines1617181915 unmodified lines*.st*local*dist-frontend/15 unmodified lines16171819202115 unmodified lines*.st*local*dist-frontend/.vscode/
98 unmodified lines9910010110210310410510610710810911012 unmodified lines12312412512612712812913098 unmodified lines)logger.info(f"Load Model - Path - {model_config.MODEL_FILE_PATH}")tmp_model = RWKV(model=model_config.MODEL_FILE_PATH.replace(".pth", ""),strategy=CONFIG.STRATEGY,)tmp_pipeline = PIPELINE(tmp_model, model_config.VOCAB)if model_config.DEFAULT_CHAT:if DEFALUT_MODEL_NAME != None:logger.info(12 unmodified linesMODEL_STORAGE[model_config.SERVICE_NAME] = ModelStorage()MODEL_STORAGE[model_config.SERVICE_NAME].MODEL_CONFIG = model_configMODEL_STORAGE[model_config.SERVICE_NAME].model = tmp_modelMODEL_STORAGE[model_config.SERVICE_NAME].pipeline = tmp_pipelinelogGPUState()98 unmodified lines9910010110210310412 unmodified lines11711811912012112212312412512612712812913013113298 unmodified lines)logger.info(f"Load Model - Path - {model_config.MODEL_FILE_PATH}")if model_config.DEFAULT_CHAT:if DEFALUT_MODEL_NAME != None:logger.info(12 unmodified linesMODEL_STORAGE[model_config.SERVICE_NAME] = ModelStorage()MODEL_STORAGE[model_config.SERVICE_NAME].MODEL_CONFIG = model_configMODEL_STORAGE[model_config.SERVICE_NAME].model = RWKV(model=model_config.MODEL_FILE_PATH.replace(".pth", ""),strategy=CONFIG.STRATEGY,)MODEL_STORAGE[model_config.SERVICE_NAME].pipeline = PIPELINE(MODEL_STORAGE[model_config.SERVICE_NAME].model, model_config.VOCAB)if "cuda" in CONFIG.STRATEGY:# torch.cuda.empty_cache()gc.collect()logGPUState()
13 unmodified lines1415161718192013 unmodified lines"pydantic-settings>=2.8.1","pynvml>=12.0.0","rich>=13.9.4","rwkv==0.8.28","setuptools>=75.8.2","snowflake-id>=1.0.2",]13 unmodified lines1415161718192013 unmodified lines"pydantic-settings>=2.8.1","pynvml>=12.0.0","rich>=13.9.4",# "rwkv==0.8.28","setuptools>=75.8.2","snowflake-id>=1.0.2",]
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475#include <cublas_v2.h>#include <cuda.h>#include <cuda_fp16.h>#include <cuda_runtime.h>#include <torch/extension.h>#include <c10/cuda/CUDAGuard.h>#include <ATen/cuda/CUDAContext.h>#define CUBLAS_CHECK(condition) \for (cublasStatus_t _cublas_check_status = (condition); \_cublas_check_status != CUBLAS_STATUS_SUCCESS;) \throw std::runtime_error("cuBLAS error " + \std::to_string(_cublas_check_status) + " at " + \std::to_string(__LINE__));#define CUDA_CHECK(condition) \for (cudaError_t _cuda_check_status = (condition); \_cuda_check_status != cudaSuccess;) \throw std::runtime_error( \"CUDA error " + std::string(cudaGetErrorString(_cuda_check_status)) + \" at " + std::to_string(__LINE__));/*NOTE: blas gemm is column-major by default, but we need row-major output.The data of row-major, transposed matrix is exactly the same as thecolumn-major, non-transposed matrix, and C = A * B ---> C^T = B^T * A^T*/void gemm_fp16_cublas(torch::Tensor a, torch::Tensor b, torch::Tensor c) {const at::cuda::OptionalCUDAGuard device_guard(device_of(a));const auto cuda_data_type = CUDA_R_16F;const auto cuda_c_data_type =c.dtype() == torch::kFloat32 ? CUDA_R_32F : CUDA_R_16F;const auto compute_type = CUDA_R_32F;const float sp_alpha = 1.f;// swap a and b, and use CUBLAS_OP_N. see the notes abovestd::swap(a, b);const cublasOperation_t cublas_trans_a = CUBLAS_OP_N;const cublasOperation_t cublas_trans_b = CUBLAS_OP_N;// m = (B^T).size(0) = B.size(1), and = A.size(1) after swap,// negative axis is used because of the existence of batch matmul.const int m = a.size(-1);const int k = a.size(-2);const int n = b.size(-2);const int cublas_lda = m;const int cublas_ldb = k;const int cublas_ldc = m;cublasHandle_t cublas_handle = at::cuda::getCurrentCUDABlasHandle();#if CUDA_VERSION >= 11000cublasGemmAlgo_t algo = CUBLAS_GEMM_DEFAULT;#elsecublasGemmAlgo_t algo = CUBLAS_GEMM_DFALT_TENSOR_OP;#endifconst float sp_beta = 0.f;if (a.sizes().size() == 2 && b.sizes().size() == 2) {CUBLAS_CHECK(cublasGemmEx(cublas_handle, cublas_trans_a, cublas_trans_b, m, n, k, &sp_alpha,a.data_ptr(), cuda_data_type, cublas_lda, b.data_ptr(), cuda_data_type,cublas_ldb, &sp_beta, c.data_ptr(), cuda_c_data_type, cublas_ldc,compute_type, algo));} else {// batch matmulassert(a.sizes().size() == 3 && b.sizes().size() == 3);const long long int cublas_stride_a = m * k;const long long int cublas_stride_b = k * n;const long long int cublas_stride_c = m * n;CUBLAS_CHECK(cublasGemmStridedBatchedEx(cublas_handle, cublas_trans_a, cublas_trans_b, m,n, k, &sp_alpha, a.data_ptr(), cuda_data_type, cublas_lda,cublas_stride_a, b.data_ptr(), cuda_data_type, cublas_ldb, cublas_stride_b,&sp_beta, c.data_ptr(), cuda_c_data_type, cublas_ldc, cublas_stride_c,a.size(0), compute_type, algo));}}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475#include <cublas_v2.h>#include <cuda.h>#include <cuda_fp16.h>#include <cuda_runtime.h>#include <torch/extension.h>#include <c10/cuda/CUDAGuard.h>#include <ATen/cuda/CUDAContext.h>#define CUBLAS_CHECK(condition) \for (cublasStatus_t _cublas_check_status = (condition); \_cublas_check_status != CUBLAS_STATUS_SUCCESS;) \throw std::runtime_error("cuBLAS error " + \std::to_string(_cublas_check_status) + " at " + \std::to_string(__LINE__));#define CUDA_CHECK(condition) \for (cudaError_t _cuda_check_status = (condition); \_cuda_check_status != cudaSuccess;) \throw std::runtime_error( \"CUDA error " + std::string(cudaGetErrorString(_cuda_check_status)) + \" at " + std::to_string(__LINE__));/*NOTE: blas gemm is column-major by default, but we need row-major output.The data of row-major, transposed matrix is exactly the same as thecolumn-major, non-transposed matrix, and C = A * B ---> C^T = B^T * A^T*/void gemm_fp16_cublas(torch::Tensor a, torch::Tensor b, torch::Tensor c) {const at::cuda::OptionalCUDAGuard device_guard(device_of(a));const auto cuda_data_type = CUDA_R_16F;const auto cuda_c_data_type =c.dtype() == torch::kFloat32 ? CUDA_R_32F : CUDA_R_16F;const auto compute_type = CUDA_R_32F;const float sp_alpha = 1.f;// swap a and b, and use CUBLAS_OP_N. see the notes abovestd::swap(a, b);const cublasOperation_t cublas_trans_a = CUBLAS_OP_N;const cublasOperation_t cublas_trans_b = CUBLAS_OP_N;// m = (B^T).size(0) = B.size(1), and = A.size(1) after swap,// negative axis is used because of the existence of batch matmul.const int m = a.size(-1);const int k = a.size(-2);const int n = b.size(-2);const int cublas_lda = m;const int cublas_ldb = k;const int cublas_ldc = m;cublasHandle_t cublas_handle = at::cuda::getCurrentCUDABlasHandle();#if CUDA_VERSION >= 11000cublasGemmAlgo_t algo = CUBLAS_GEMM_DEFAULT;#elsecublasGemmAlgo_t algo = CUBLAS_GEMM_DFALT_TENSOR_OP;#endifconst float sp_beta = 0.f;if (a.sizes().size() == 2 && b.sizes().size() == 2) {CUBLAS_CHECK(cublasGemmEx(cublas_handle, cublas_trans_a, cublas_trans_b, m, n, k, &sp_alpha,a.data_ptr(), cuda_data_type, cublas_lda, b.data_ptr(), cuda_data_type,cublas_ldb, &sp_beta, c.data_ptr(), cuda_c_data_type, cublas_ldc,compute_type, algo));} else {// batch matmulassert(a.sizes().size() == 3 && b.sizes().size() == 3);const long long int cublas_stride_a = m * k;const long long int cublas_stride_b = k * n;const long long int cublas_stride_c = m * n;CUBLAS_CHECK(cublasGemmStridedBatchedEx(cublas_handle, cublas_trans_a, cublas_trans_b, m,n, k, &sp_alpha, a.data_ptr(), cuda_data_type, cublas_lda,cublas_stride_a, b.data_ptr(), cuda_data_type, cublas_ldb, cublas_stride_b,&sp_beta, c.data_ptr(), cuda_c_data_type, cublas_ldc, cublas_stride_c,a.size(0), compute_type, algo));}}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246#include <stdio.h>#include <assert.h>#include "ATen/ATen.h"#include <cuda_fp16.h>#define MIN_VALUE (-1e38)typedef at::Half fp16;__half *cast(fp16 *ptr) {return reinterpret_cast<__half *>(ptr);}template <typename F>__global__ void kernel_wkv_forward(const int B, const int T, const int C,const float *__restrict__ const _w, const float *__restrict__ const _u, const F *__restrict__ const _k, const F *__restrict__ const _v,F *__restrict__ const _y, float *__restrict__ const _aa, float *__restrict__ const _bb, float *__restrict__ const _pp) {const int idx = blockIdx.x * blockDim.x + threadIdx.x;const int _b = idx / C;const int _c = idx % C;const int _offset = _b * T * C + _c;const int _state_offset = _b * C + _c;float u = _u[_c];float w = _w[_c];const F *__restrict__ const k = _k + _offset;const F *__restrict__ const v = _v + _offset;F *__restrict__ const y = _y + _offset;float aa = _aa[_state_offset];float bb = _bb[_state_offset];float pp = _pp[_state_offset];for (int i = 0; i < T; i++) {const int ii = i * C;const float kk = float(k[ii]);const float vv = float(v[ii]);float ww = u + kk;float p = max(pp, ww);float e1 = exp(pp - p);float e2 = exp(ww - p);y[ii] = F((e1 * aa + e2 * vv) / (e1 * bb + e2));ww = w + pp;p = max(ww, kk);e1 = exp(ww - p);e2 = exp(kk - p);aa = e1 * aa + e2 * vv;bb = e1 * bb + e2;pp = p;}_aa[_state_offset] = aa;_bb[_state_offset] = bb;_pp[_state_offset] = pp;}template <typename F>void cuda_wkv_forward(int B, int T, int C, float *w, float *u, F *k, F *v, F *y, float *aa, float *bb, float *pp) {dim3 threadsPerBlock( min(C, 32) );assert(B * C % threadsPerBlock.x == 0);dim3 numBlocks(B * C / threadsPerBlock.x);kernel_wkv_forward<<<numBlocks, threadsPerBlock>>>(B, T, C, w, u, k, v, y, aa, bb, pp);}template void cuda_wkv_forward<fp16>(int B, int T, int C,float *w, float *u, fp16 *k, fp16 *v, fp16 *y,float *aa, float *bb, float *pp);template void cuda_wkv_forward<float>(int B, int T, int C,float *w, float *u, float *k, float *v, float *y,float *aa, float *bb, float *pp);__global__ void kernel_mm_seq_fp32i8(const int B, const int N, const int M,const float *__restrict__ const x, const int x_stride,const uint8_t *__restrict__ const w, const int w_stride,const float *__restrict__ const mx,const float *__restrict__ const rx,const float *__restrict__ const my,const float *__restrict__ const ry,float *__restrict__ const y, const int y_stride) {const int i = blockIdx.x * blockDim.x + threadIdx.x;const int k = blockIdx.y * blockDim.y + threadIdx.y;if (i < B && k < M) {float y_local = 0;for (int j = 0; j < N; ++j) {y_local += x[i * x_stride + j] * ((float(w[j * w_stride + k]) + 0.5f)* rx[k] * ry[j] + mx[k] + my[j]);}y[i * y_stride + k] = y_local;}}template <typename F>void cuda_mm8_seq(int B, int N, int M,F *x, int x_stride,uint8_t *w, int w_stride,F *mx, F *rx,F *my, F *ry,F *y, int y_stride);template <>void cuda_mm8_seq<float>(int B, int N, int M,float *x, int x_stride,uint8_t *w, int w_stride,float *mx, float *rx,float *my, float *ry,float *y, int y_stride) {dim3 blockSize(1, 128);dim3 gridSize((B + blockSize.x - 1) / blockSize.x, (M + blockSize.y - 1) / blockSize.y);kernel_mm_seq_fp32i8<<<gridSize, blockSize>>>(B, N, M, x, x_stride, w, w_stride,mx, rx, my, ry, y, y_stride);}__global__ void kernel_mm_seq_fp16i8(const int B, const int N, const int M,const __half *__restrict__ const x, const int x_stride,const uint8_t *__restrict__ const w, const int w_stride,const __half *__restrict__ const mx,const __half *__restrict__ const rx,const __half *__restrict__ const my,const __half *__restrict__ const ry,__half *__restrict__ const y, const int y_stride) {const int i = blockIdx.x * blockDim.x + threadIdx.x;const int k = blockIdx.y * blockDim.y + threadIdx.y;if (i < B && k < M) {float y_local = 0;for (int j = 0; j < N; ++j) {y_local += __half2float(x[i * x_stride + j]) * ((float(w[j * w_stride + k]) + 0.5f)* __half2float(rx[k]) * __half2float(ry[j])+ __half2float(mx[k]) + __half2float(my[j]));}y[i * y_stride + k] = __float2half(y_local);}}template <>void cuda_mm8_seq<fp16>(int B, int N, int M,fp16 *x, int x_stride,uint8_t *w, int w_stride,fp16 *mx, fp16 *rx,fp16 *my, fp16 *ry,fp16 *y, int y_stride) {dim3 blockSize(1, 128);dim3 gridSize((B + blockSize.x - 1) / blockSize.x, (M + blockSize.y - 1) / blockSize.y);kernel_mm_seq_fp16i8<<<gridSize, blockSize>>>(B, N, M, cast(x), x_stride, w, w_stride,cast(mx), cast(rx), cast(my), cast(ry), cast(y), y_stride);}#define MM8_ONE_JSPLIT 24#define MM8_ONE_TILE 1024__global__ void kernel_mm_one_fp32i8(const int N, const int M,const float *__restrict__ const x,const uint8_t *__restrict__ const w, const int w_stride,const float *__restrict__ const mx,const float *__restrict__ const rx,const float *__restrict__ const my,const float *__restrict__ const ry,float *__restrict__ const y) {const int k = blockIdx.y * blockDim.y + threadIdx.y;const int j0 = min(N, blockIdx.x * ((N + MM8_ONE_JSPLIT - 1) / MM8_ONE_JSPLIT));const int j1 = min(N, (blockIdx.x + 1) * ((N + MM8_ONE_JSPLIT - 1) / MM8_ONE_JSPLIT));if (k < M) {float y_local = 0;for (int j = j0; j < j1; ++j) {y_local += x[j] * ((float(w[j * w_stride + k]) + 0.5f)* rx[k] * ry[j] + mx[k] + my[j]);}atomicAdd(&y[k], y_local);}}template <typename F>void cuda_mm8_one(int N, int M,F *x,uint8_t *w, int w_stride,F *mx, F *rx,F *my, F *ry,float *y);template <>void cuda_mm8_one<float>(int N, int M,float *x,uint8_t *w, int w_stride,float *mx, float *rx,float *my, float *ry,float *y) {dim3 blockSize(1, MM8_ONE_TILE);dim3 gridSize(MM8_ONE_JSPLIT, (M + blockSize.y - 1) / blockSize.y);kernel_mm_one_fp32i8<<<gridSize, blockSize>>>(N, M, x, w, w_stride,mx, rx, my, ry, y);}__global__ void kernel_mm_one_fp16i8(const int N, const int M,const __half *__restrict__ const x,const uint8_t *__restrict__ const w, const int w_stride,const __half *__restrict__ const mx,const __half *__restrict__ const rx,const __half *__restrict__ const my,const __half *__restrict__ const ry,float *__restrict__ const y) {const int k = blockIdx.y * blockDim.y + threadIdx.y;const int j0 = min(N, blockIdx.x * ((N + MM8_ONE_JSPLIT - 1) / MM8_ONE_JSPLIT));const int j1 = min(N, (blockIdx.x + 1) * ((N + MM8_ONE_JSPLIT - 1) / MM8_ONE_JSPLIT));if (k < M) {float y_local = 0;for (int j = j0; j < j1; ++j) {y_local += __half2float(x[j]) * ((float(w[j * w_stride + k]) + 0.5f)* __half2float(rx[k]) * __half2float(ry[j])+ __half2float(mx[k]) + __half2float(my[j]));}atomicAdd(&y[k], y_local);}}template <>void cuda_mm8_one<fp16>(int N, int M,fp16 *x,uint8_t *w, int w_stride,fp16 *mx, fp16 *rx,fp16 *my, fp16 *ry,float *y) {dim3 blockSize(1, MM8_ONE_TILE);dim3 gridSize(MM8_ONE_JSPLIT, (M + blockSize.y - 1) / blockSize.y);kernel_mm_one_fp16i8<<<gridSize, blockSize>>>(N, M, cast(x), w, w_stride,cast(mx), cast(rx), cast(my), cast(ry), y);}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246#include <stdio.h>#include <assert.h>#include "ATen/ATen.h"#include <cuda_fp16.h>#define MIN_VALUE (-1e38)typedef at::Half fp16;__half *cast(fp16 *ptr) {return reinterpret_cast<__half *>(ptr);}template <typename F>__global__ void kernel_wkv_forward(const int B, const int T, const int C,const float *__restrict__ const _w, const float *__restrict__ const _u, const F *__restrict__ const _k, const F *__restrict__ const _v,F *__restrict__ const _y, float *__restrict__ const _aa, float *__restrict__ const _bb, float *__restrict__ const _pp) {const int idx = blockIdx.x * blockDim.x + threadIdx.x;const int _b = idx / C;const int _c = idx % C;const int _offset = _b * T * C + _c;const int _state_offset = _b * C + _c;float u = _u[_c];float w = _w[_c];const F *__restrict__ const k = _k + _offset;const F *__restrict__ const v = _v + _offset;F *__restrict__ const y = _y + _offset;float aa = _aa[_state_offset];float bb = _bb[_state_offset];float pp = _pp[_state_offset];for (int i = 0; i < T; i++) {const int ii = i * C;const float kk = float(k[ii]);const float vv = float(v[ii]);float ww = u + kk;float p = max(pp, ww);float e1 = exp(pp - p);float e2 = exp(ww - p);y[ii] = F((e1 * aa + e2 * vv) / (e1 * bb + e2));ww = w + pp;p = max(ww, kk);e1 = exp(ww - p);e2 = exp(kk - p);aa = e1 * aa + e2 * vv;bb = e1 * bb + e2;pp = p;}_aa[_state_offset] = aa;_bb[_state_offset] = bb;_pp[_state_offset] = pp;}template <typename F>void cuda_wkv_forward(int B, int T, int C, float *w, float *u, F *k, F *v, F *y, float *aa, float *bb, float *pp) {dim3 threadsPerBlock( min(C, 32) );assert(B * C % threadsPerBlock.x == 0);dim3 numBlocks(B * C / threadsPerBlock.x);kernel_wkv_forward<<<numBlocks, threadsPerBlock>>>(B, T, C, w, u, k, v, y, aa, bb, pp);}template void cuda_wkv_forward<fp16>(int B, int T, int C,float *w, float *u, fp16 *k, fp16 *v, fp16 *y,float *aa, float *bb, float *pp);template void cuda_wkv_forward<float>(int B, int T, int C,float *w, float *u, float *k, float *v, float *y,float *aa, float *bb, float *pp);__global__ void kernel_mm_seq_fp32i8(const int B, const int N, const int M,const float *__restrict__ const x, const int x_stride,const uint8_t *__restrict__ const w, const int w_stride,const float *__restrict__ const mx,const float *__restrict__ const rx,const float *__restrict__ const my,const float *__restrict__ const ry,float *__restrict__ const y, const int y_stride) {const int i = blockIdx.x * blockDim.x + threadIdx.x;const int k = blockIdx.y * blockDim.y + threadIdx.y;if (i < B && k < M) {float y_local = 0;for (int j = 0; j < N; ++j) {y_local += x[i * x_stride + j] * ((float(w[j * w_stride + k]) + 0.5f)* rx[k] * ry[j] + mx[k] + my[j]);}y[i * y_stride + k] = y_local;}}template <typename F>void cuda_mm8_seq(int B, int N, int M,F *x, int x_stride,uint8_t *w, int w_stride,F *mx, F *rx,F *my, F *ry,F *y, int y_stride);template <>void cuda_mm8_seq<float>(int B, int N, int M,float *x, int x_stride,uint8_t *w, int w_stride,float *mx, float *rx,float *my, float *ry,float *y, int y_stride) {dim3 blockSize(1, 128);dim3 gridSize((B + blockSize.x - 1) / blockSize.x, (M + blockSize.y - 1) / blockSize.y);kernel_mm_seq_fp32i8<<<gridSize, blockSize>>>(B, N, M, x, x_stride, w, w_stride,mx, rx, my, ry, y, y_stride);}__global__ void kernel_mm_seq_fp16i8(const int B, const int N, const int M,const __half *__restrict__ const x, const int x_stride,const uint8_t *__restrict__ const w, const int w_stride,const __half *__restrict__ const mx,const __half *__restrict__ const rx,const __half *__restrict__ const my,const __half *__restrict__ const ry,__half *__restrict__ const y, const int y_stride) {const int i = blockIdx.x * blockDim.x + threadIdx.x;const int k = blockIdx.y * blockDim.y + threadIdx.y;if (i < B && k < M) {float y_local = 0;for (int j = 0; j < N; ++j) {y_local += __half2float(x[i * x_stride + j]) * ((float(w[j * w_stride + k]) + 0.5f)* __half2float(rx[k]) * __half2float(ry[j])+ __half2float(mx[k]) + __half2float(my[j]));}y[i * y_stride + k] = __float2half(y_local);}}template <>void cuda_mm8_seq<fp16>(int B, int N, int M,fp16 *x, int x_stride,uint8_t *w, int w_stride,fp16 *mx, fp16 *rx,fp16 *my, fp16 *ry,fp16 *y, int y_stride) {dim3 blockSize(1, 128);dim3 gridSize((B + blockSize.x - 1) / blockSize.x, (M + blockSize.y - 1) / blockSize.y);kernel_mm_seq_fp16i8<<<gridSize, blockSize>>>(B, N, M, cast(x), x_stride, w, w_stride,cast(mx), cast(rx), cast(my), cast(ry), cast(y), y_stride);}#define MM8_ONE_JSPLIT 24#define MM8_ONE_TILE 1024__global__ void kernel_mm_one_fp32i8(const int N, const int M,const float *__restrict__ const x,const uint8_t *__restrict__ const w, const int w_stride,const float *__restrict__ const mx,const float *__restrict__ const rx,const float *__restrict__ const my,const float *__restrict__ const ry,float *__restrict__ const y) {const int k = blockIdx.y * blockDim.y + threadIdx.y;const int j0 = min(N, blockIdx.x * ((N + MM8_ONE_JSPLIT - 1) / MM8_ONE_JSPLIT));const int j1 = min(N, (blockIdx.x + 1) * ((N + MM8_ONE_JSPLIT - 1) / MM8_ONE_JSPLIT));if (k < M) {float y_local = 0;for (int j = j0; j < j1; ++j) {y_local += x[j] * ((float(w[j * w_stride + k]) + 0.5f)* rx[k] * ry[j] + mx[k] + my[j]);}atomicAdd(&y[k], y_local);}}template <typename F>void cuda_mm8_one(int N, int M,F *x,uint8_t *w, int w_stride,F *mx, F *rx,F *my, F *ry,float *y);template <>void cuda_mm8_one<float>(int N, int M,float *x,uint8_t *w, int w_stride,float *mx, float *rx,float *my, float *ry,float *y) {dim3 blockSize(1, MM8_ONE_TILE);dim3 gridSize(MM8_ONE_JSPLIT, (M + blockSize.y - 1) / blockSize.y);kernel_mm_one_fp32i8<<<gridSize, blockSize>>>(N, M, x, w, w_stride,mx, rx, my, ry, y);}__global__ void kernel_mm_one_fp16i8(const int N, const int M,const __half *__restrict__ const x,const uint8_t *__restrict__ const w, const int w_stride,const __half *__restrict__ const mx,const __half *__restrict__ const rx,const __half *__restrict__ const my,const __half *__restrict__ const ry,float *__restrict__ const y) {const int k = blockIdx.y * blockDim.y + threadIdx.y;const int j0 = min(N, blockIdx.x * ((N + MM8_ONE_JSPLIT - 1) / MM8_ONE_JSPLIT));const int j1 = min(N, (blockIdx.x + 1) * ((N + MM8_ONE_JSPLIT - 1) / MM8_ONE_JSPLIT));if (k < M) {float y_local = 0;for (int j = j0; j < j1; ++j) {y_local += __half2float(x[j]) * ((float(w[j * w_stride + k]) + 0.5f)* __half2float(rx[k]) * __half2float(ry[j])+ __half2float(mx[k]) + __half2float(my[j]));}atomicAdd(&y[k], y_local);}}template <>void cuda_mm8_one<fp16>(int N, int M,fp16 *x,uint8_t *w, int w_stride,fp16 *mx, fp16 *rx,fp16 *my, fp16 *ry,float *y) {dim3 blockSize(1, MM8_ONE_TILE);dim3 gridSize(MM8_ONE_JSPLIT, (M + blockSize.y - 1) / blockSize.y);kernel_mm_one_fp16i8<<<gridSize, blockSize>>>(N, M, cast(x), w, w_stride,cast(mx), cast(rx), cast(my), cast(ry), y);}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788#include <stdio.h>#include <assert.h>#include "ATen/ATen.h"typedef at::BFloat16 bf16;typedef at::Half fp16;typedef float fp32;template <typename F>__global__ void kernel_forward(const int B, const int T, const int C, const int H, float *__restrict__ _state,const F *__restrict__ const _r, const F *__restrict__ const _k, const F *__restrict__ const _v, const float *__restrict__ _w, const F *__restrict__ _u,F *__restrict__ const _y){const int b = blockIdx.x / H;const int h = blockIdx.x % H;const int i = threadIdx.x;_w += h*_N_;_u += h*_N_;_state += h*_N_*_N_ + i*_N_; // wrong if B > 1 !!!__shared__ float r[_N_], k[_N_], u[_N_], w[_N_];float state[_N_];#pragma unrollfor (int j = 0; j < _N_; j++)state[j] = _state[j];__syncthreads();u[i] = float(_u[i]);w[i] = _w[i];__syncthreads();for (int t = b*T*C + h*_N_ + i; t < (b+1)*T*C + h*_N_ + i; t += C){__syncthreads();r[i] = float(_r[t]);k[i] = float(_k[t]);__syncthreads();const float v = float(_v[t]);float y = 0;#pragma unrollfor (int j = 0; j < _N_; j+=4){const float4& r_ = (float4&)(r[j]);const float4& k_ = (float4&)(k[j]);const float4& w_ = (float4&)(w[j]);const float4& u_ = (float4&)(u[j]);float4& s = (float4&)(state[j]);float4 x;x.x = k_.x * v;x.y = k_.y * v;x.z = k_.z * v;x.w = k_.w * v;y += r_.x * (u_.x * x.x + s.x);y += r_.y * (u_.y * x.y + s.y);y += r_.z * (u_.z * x.z + s.z);y += r_.w * (u_.w * x.w + s.w);s.x = s.x * w_.x + x.x;s.y = s.y * w_.y + x.y;s.z = s.z * w_.z + x.z;s.w = s.w * w_.w + x.w;}_y[t] = F(y);}#pragma unrollfor (int j = 0; j < _N_; j++)_state[j] = state[j];}void cuda_forward_bf16(int B, int T, int C, int H, float *state, bf16 *r, bf16 *k, bf16 *v, float *w, bf16 *u, bf16 *y){assert(H*_N_ == C);kernel_forward<<<dim3(B * H), dim3(_N_)>>>(B, T, C, H, state, r, k, v, w, u, y);}void cuda_forward_fp16(int B, int T, int C, int H, float *state, fp16 *r, fp16 *k, fp16 *v, float *w, fp16 *u, fp16 *y){assert(H*_N_ == C);kernel_forward<<<dim3(B * H), dim3(_N_)>>>(B, T, C, H, state, r, k, v, w, u, y);}void cuda_forward_fp32(int B, int T, int C, int H, float *state, fp32 *r, fp32 *k, fp32 *v, float *w, fp32 *u, fp32 *y){assert(H*_N_ == C);kernel_forward<<<dim3(B * H), dim3(_N_)>>>(B, T, C, H, state, r, k, v, w, u, y);}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788#include <stdio.h>#include <assert.h>#include "ATen/ATen.h"typedef at::BFloat16 bf16;typedef at::Half fp16;typedef float fp32;template <typename F>__global__ void kernel_forward(const int B, const int T, const int C, const int H, float *__restrict__ _state,const F *__restrict__ const _r, const F *__restrict__ const _k, const F *__restrict__ const _v, const float *__restrict__ _w, const F *__restrict__ _u,F *__restrict__ const _y){const int b = blockIdx.x / H;const int h = blockIdx.x % H;const int i = threadIdx.x;_w += h*_N_;_u += h*_N_;_state += h*_N_*_N_ + i*_N_; // wrong if B > 1 !!!__shared__ float r[_N_], k[_N_], u[_N_], w[_N_];float state[_N_];#pragma unrollfor (int j = 0; j < _N_; j++)state[j] = _state[j];__syncthreads();u[i] = float(_u[i]);w[i] = _w[i];__syncthreads();for (int t = b*T*C + h*_N_ + i; t < (b+1)*T*C + h*_N_ + i; t += C){__syncthreads();r[i] = float(_r[t]);k[i] = float(_k[t]);__syncthreads();const float v = float(_v[t]);float y = 0;#pragma unrollfor (int j = 0; j < _N_; j+=4){const float4& r_ = (float4&)(r[j]);const float4& k_ = (float4&)(k[j]);const float4& w_ = (float4&)(w[j]);const float4& u_ = (float4&)(u[j]);float4& s = (float4&)(state[j]);float4 x;x.x = k_.x * v;x.y = k_.y * v;x.z = k_.z * v;x.w = k_.w * v;y += r_.x * (u_.x * x.x + s.x);y += r_.y * (u_.y * x.y + s.y);y += r_.z * (u_.z * x.z + s.z);y += r_.w * (u_.w * x.w + s.w);s.x = s.x * w_.x + x.x;s.y = s.y * w_.y + x.y;s.z = s.z * w_.z + x.z;s.w = s.w * w_.w + x.w;}_y[t] = F(y);}#pragma unrollfor (int j = 0; j < _N_; j++)_state[j] = state[j];}void cuda_forward_bf16(int B, int T, int C, int H, float *state, bf16 *r, bf16 *k, bf16 *v, float *w, bf16 *u, bf16 *y){assert(H*_N_ == C);kernel_forward<<<dim3(B * H), dim3(_N_)>>>(B, T, C, H, state, r, k, v, w, u, y);}void cuda_forward_fp16(int B, int T, int C, int H, float *state, fp16 *r, fp16 *k, fp16 *v, float *w, fp16 *u, fp16 *y){assert(H*_N_ == C);kernel_forward<<<dim3(B * H), dim3(_N_)>>>(B, T, C, H, state, r, k, v, w, u, y);}void cuda_forward_fp32(int B, int T, int C, int H, float *state, fp32 *r, fp32 *k, fp32 *v, float *w, fp32 *u, fp32 *y){assert(H*_N_ == C);kernel_forward<<<dim3(B * H), dim3(_N_)>>>(B, T, C, H, state, r, k, v, w, u, y);}
12345678910111213141516171819202122232425262728293031323334#include <torch/extension.h>#include "ATen/ATen.h"#include <c10/cuda/CUDAGuard.h>typedef at::BFloat16 bf16;typedef at::Half fp16;typedef float fp32;void cuda_forward_bf16(int B, int T, int C, int H, float *state, bf16 *r, bf16 *k, bf16 *v, float *w, bf16 *u, bf16 *y);void cuda_forward_fp16(int B, int T, int C, int H, float *state, fp16 *r, fp16 *k, fp16 *v, float *w, fp16 *u, fp16 *y);void cuda_forward_fp32(int B, int T, int C, int H, float *state, fp32 *r, fp32 *k, fp32 *v, float *w, fp32 *u, fp32 *y);void forward_bf16(int64_t B, int64_t T, int64_t C, int64_t H, torch::Tensor &state, torch::Tensor &r, torch::Tensor &k, torch::Tensor &v, torch::Tensor &w, torch::Tensor &u, torch::Tensor &y) {const at::cuda::OptionalCUDAGuard device_guard(device_of(state));cuda_forward_bf16(B, T, C, H, state.data_ptr<float>(), r.data_ptr<bf16>(), k.data_ptr<bf16>(), v.data_ptr<bf16>(), w.data_ptr<float>(), u.data_ptr<bf16>(), y.data_ptr<bf16>());}void forward_fp16(int64_t B, int64_t T, int64_t C, int64_t H, torch::Tensor &state, torch::Tensor &r, torch::Tensor &k, torch::Tensor &v, torch::Tensor &w, torch::Tensor &u, torch::Tensor &y) {const at::cuda::OptionalCUDAGuard device_guard(device_of(state));cuda_forward_fp16(B, T, C, H, state.data_ptr<float>(), r.data_ptr<fp16>(), k.data_ptr<fp16>(), v.data_ptr<fp16>(), w.data_ptr<float>(), u.data_ptr<fp16>(), y.data_ptr<fp16>());}void forward_fp32(int64_t B, int64_t T, int64_t C, int64_t H, torch::Tensor &state, torch::Tensor &r, torch::Tensor &k, torch::Tensor &v, torch::Tensor &w, torch::Tensor &u, torch::Tensor &y) {const at::cuda::OptionalCUDAGuard device_guard(device_of(state));cuda_forward_fp32(B, T, C, H, state.data_ptr<float>(), r.data_ptr<fp32>(), k.data_ptr<fp32>(), v.data_ptr<fp32>(), w.data_ptr<float>(), u.data_ptr<fp32>(), y.data_ptr<fp32>());}PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {m.def("forward_bf16", &forward_bf16, "rwkv5 forward_bf16");m.def("forward_fp16", &forward_fp16, "rwkv5 forward_fp16");m.def("forward_fp32", &forward_fp32, "rwkv5 forward_fp32");}TORCH_LIBRARY(rwkv5, m) {m.def("forward_bf16", forward_bf16);m.def("forward_fp16", forward_fp16);m.def("forward_fp32", forward_fp32);}12345678910111213141516171819202122232425262728293031323334#include <torch/extension.h>#include "ATen/ATen.h"#include <c10/cuda/CUDAGuard.h>typedef at::BFloat16 bf16;typedef at::Half fp16;typedef float fp32;void cuda_forward_bf16(int B, int T, int C, int H, float *state, bf16 *r, bf16 *k, bf16 *v, float *w, bf16 *u, bf16 *y);void cuda_forward_fp16(int B, int T, int C, int H, float *state, fp16 *r, fp16 *k, fp16 *v, float *w, fp16 *u, fp16 *y);void cuda_forward_fp32(int B, int T, int C, int H, float *state, fp32 *r, fp32 *k, fp32 *v, float *w, fp32 *u, fp32 *y);void forward_bf16(int64_t B, int64_t T, int64_t C, int64_t H, torch::Tensor &state, torch::Tensor &r, torch::Tensor &k, torch::Tensor &v, torch::Tensor &w, torch::Tensor &u, torch::Tensor &y) {const at::cuda::OptionalCUDAGuard device_guard(device_of(state));cuda_forward_bf16(B, T, C, H, state.data_ptr<float>(), r.data_ptr<bf16>(), k.data_ptr<bf16>(), v.data_ptr<bf16>(), w.data_ptr<float>(), u.data_ptr<bf16>(), y.data_ptr<bf16>());}void forward_fp16(int64_t B, int64_t T, int64_t C, int64_t H, torch::Tensor &state, torch::Tensor &r, torch::Tensor &k, torch::Tensor &v, torch::Tensor &w, torch::Tensor &u, torch::Tensor &y) {const at::cuda::OptionalCUDAGuard device_guard(device_of(state));cuda_forward_fp16(B, T, C, H, state.data_ptr<float>(), r.data_ptr<fp16>(), k.data_ptr<fp16>(), v.data_ptr<fp16>(), w.data_ptr<float>(), u.data_ptr<fp16>(), y.data_ptr<fp16>());}void forward_fp32(int64_t B, int64_t T, int64_t C, int64_t H, torch::Tensor &state, torch::Tensor &r, torch::Tensor &k, torch::Tensor &v, torch::Tensor &w, torch::Tensor &u, torch::Tensor &y) {const at::cuda::OptionalCUDAGuard device_guard(device_of(state));cuda_forward_fp32(B, T, C, H, state.data_ptr<float>(), r.data_ptr<fp32>(), k.data_ptr<fp32>(), v.data_ptr<fp32>(), w.data_ptr<float>(), u.data_ptr<fp32>(), y.data_ptr<fp32>());}PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {m.def("forward_bf16", &forward_bf16, "rwkv5 forward_bf16");m.def("forward_fp16", &forward_fp16, "rwkv5 forward_fp16");m.def("forward_fp32", &forward_fp32, "rwkv5 forward_fp32");}TORCH_LIBRARY(rwkv5, m) {m.def("forward_bf16", forward_bf16);m.def("forward_fp16", forward_fp16);m.def("forward_fp32", forward_fp32);}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687#include <stdio.h>#include <assert.h>#include "ATen/ATen.h"typedef at::BFloat16 bf16;typedef at::Half fp16;typedef float fp32;template <typename F>__global__ void kernel_forward(const int B, const int T, const int C, const int H, float *__restrict__ _state,const F *__restrict__ const _r, const F *__restrict__ const _k, const F *__restrict__ const _v, const float *__restrict__ _w, const F *__restrict__ _u,F *__restrict__ const _y){const int b = blockIdx.x / H;const int h = blockIdx.x % H;const int i = threadIdx.x;_u += h*_N_;_state += h*_N_*_N_ + i*_N_; // wrong if B > 1 !!!__shared__ float r[_N_], k[_N_], u[_N_], w[_N_];float state[_N_];#pragma unrollfor (int j = 0; j < _N_; j++)state[j] = _state[j];__syncthreads();u[i] = float(_u[i]);__syncthreads();for (int t = b*T*C + h*_N_ + i; t < (b+1)*T*C + h*_N_ + i; t += C){__syncthreads();w[i] = _w[t];r[i] = float(_r[t]);k[i] = float(_k[t]);__syncthreads();const float v = float(_v[t]);float y = 0;#pragma unrollfor (int j = 0; j < _N_; j+=4){const float4& r_ = (float4&)(r[j]);const float4& k_ = (float4&)(k[j]);const float4& w_ = (float4&)(w[j]);const float4& u_ = (float4&)(u[j]);float4& s = (float4&)(state[j]);float4 x;x.x = k_.x * v;x.y = k_.y * v;x.z = k_.z * v;x.w = k_.w * v;y += r_.x * (u_.x * x.x + s.x);y += r_.y * (u_.y * x.y + s.y);y += r_.z * (u_.z * x.z + s.z);y += r_.w * (u_.w * x.w + s.w);s.x = s.x * w_.x + x.x;s.y = s.y * w_.y + x.y;s.z = s.z * w_.z + x.z;s.w = s.w * w_.w + x.w;}_y[t] = F(y);}#pragma unrollfor (int j = 0; j < _N_; j++)_state[j] = state[j];}void cuda_forward_bf16(int B, int T, int C, int H, float *state, bf16 *r, bf16 *k, bf16 *v, float *w, bf16 *u, bf16 *y){assert(H*_N_ == C);kernel_forward<<<dim3(B * H), dim3(_N_)>>>(B, T, C, H, state, r, k, v, w, u, y);}void cuda_forward_fp16(int B, int T, int C, int H, float *state, fp16 *r, fp16 *k, fp16 *v, float *w, fp16 *u, fp16 *y){assert(H*_N_ == C);kernel_forward<<<dim3(B * H), dim3(_N_)>>>(B, T, C, H, state, r, k, v, w, u, y);}void cuda_forward_fp32(int B, int T, int C, int H, float *state, fp32 *r, fp32 *k, fp32 *v, float *w, fp32 *u, fp32 *y){assert(H*_N_ == C);kernel_forward<<<dim3(B * H), dim3(_N_)>>>(B, T, C, H, state, r, k, v, w, u, y);}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687#include <stdio.h>#include <assert.h>#include "ATen/ATen.h"typedef at::BFloat16 bf16;typedef at::Half fp16;typedef float fp32;template <typename F>__global__ void kernel_forward(const int B, const int T, const int C, const int H, float *__restrict__ _state,const F *__restrict__ const _r, const F *__restrict__ const _k, const F *__restrict__ const _v, const float *__restrict__ _w, const F *__restrict__ _u,F *__restrict__ const _y){const int b = blockIdx.x / H;const int h = blockIdx.x % H;const int i = threadIdx.x;_u += h*_N_;_state += h*_N_*_N_ + i*_N_; // wrong if B > 1 !!!__shared__ float r[_N_], k[_N_], u[_N_], w[_N_];float state[_N_];#pragma unrollfor (int j = 0; j < _N_; j++)state[j] = _state[j];__syncthreads();u[i] = float(_u[i]);__syncthreads();for (int t = b*T*C + h*_N_ + i; t < (b+1)*T*C + h*_N_ + i; t += C){__syncthreads();w[i] = _w[t];r[i] = float(_r[t]);k[i] = float(_k[t]);__syncthreads();const float v = float(_v[t]);float y = 0;#pragma unrollfor (int j = 0; j < _N_; j+=4){const float4& r_ = (float4&)(r[j]);const float4& k_ = (float4&)(k[j]);const float4& w_ = (float4&)(w[j]);const float4& u_ = (float4&)(u[j]);float4& s = (float4&)(state[j]);float4 x;x.x = k_.x * v;x.y = k_.y * v;x.z = k_.z * v;x.w = k_.w * v;y += r_.x * (u_.x * x.x + s.x);y += r_.y * (u_.y * x.y + s.y);y += r_.z * (u_.z * x.z + s.z);y += r_.w * (u_.w * x.w + s.w);s.x = s.x * w_.x + x.x;s.y = s.y * w_.y + x.y;s.z = s.z * w_.z + x.z;s.w = s.w * w_.w + x.w;}_y[t] = F(y);}#pragma unrollfor (int j = 0; j < _N_; j++)_state[j] = state[j];}void cuda_forward_bf16(int B, int T, int C, int H, float *state, bf16 *r, bf16 *k, bf16 *v, float *w, bf16 *u, bf16 *y){assert(H*_N_ == C);kernel_forward<<<dim3(B * H), dim3(_N_)>>>(B, T, C, H, state, r, k, v, w, u, y);}void cuda_forward_fp16(int B, int T, int C, int H, float *state, fp16 *r, fp16 *k, fp16 *v, float *w, fp16 *u, fp16 *y){assert(H*_N_ == C);kernel_forward<<<dim3(B * H), dim3(_N_)>>>(B, T, C, H, state, r, k, v, w, u, y);}void cuda_forward_fp32(int B, int T, int C, int H, float *state, fp32 *r, fp32 *k, fp32 *v, float *w, fp32 *u, fp32 *y){assert(H*_N_ == C);kernel_forward<<<dim3(B * H), dim3(_N_)>>>(B, T, C, H, state, r, k, v, w, u, y);}
12345678910111213141516171819202122232425262728293031323334#include <torch/extension.h>#include "ATen/ATen.h"#include <c10/cuda/CUDAGuard.h>typedef at::BFloat16 bf16;typedef at::Half fp16;typedef float fp32;void cuda_forward_bf16(int B, int T, int C, int H, float *state, bf16 *r, bf16 *k, bf16 *v, float *w, bf16 *u, bf16 *y);void cuda_forward_fp16(int B, int T, int C, int H, float *state, fp16 *r, fp16 *k, fp16 *v, float *w, fp16 *u, fp16 *y);void cuda_forward_fp32(int B, int T, int C, int H, float *state, fp32 *r, fp32 *k, fp32 *v, float *w, fp32 *u, fp32 *y);void forward_bf16(int64_t B, int64_t T, int64_t C, int64_t H, torch::Tensor &state, torch::Tensor &r, torch::Tensor &k, torch::Tensor &v, torch::Tensor &w, torch::Tensor &u, torch::Tensor &y) {const at::cuda::OptionalCUDAGuard device_guard(device_of(state));cuda_forward_bf16(B, T, C, H, state.data_ptr<float>(), r.data_ptr<bf16>(), k.data_ptr<bf16>(), v.data_ptr<bf16>(), w.data_ptr<float>(), u.data_ptr<bf16>(), y.data_ptr<bf16>());}void forward_fp16(int64_t B, int64_t T, int64_t C, int64_t H, torch::Tensor &state, torch::Tensor &r, torch::Tensor &k, torch::Tensor &v, torch::Tensor &w, torch::Tensor &u, torch::Tensor &y) {const at::cuda::OptionalCUDAGuard device_guard(device_of(state));cuda_forward_fp16(B, T, C, H, state.data_ptr<float>(), r.data_ptr<fp16>(), k.data_ptr<fp16>(), v.data_ptr<fp16>(), w.data_ptr<float>(), u.data_ptr<fp16>(), y.data_ptr<fp16>());}void forward_fp32(int64_t B, int64_t T, int64_t C, int64_t H, torch::Tensor &state, torch::Tensor &r, torch::Tensor &k, torch::Tensor &v, torch::Tensor &w, torch::Tensor &u, torch::Tensor &y) {const at::cuda::OptionalCUDAGuard device_guard(device_of(state));cuda_forward_fp32(B, T, C, H, state.data_ptr<float>(), r.data_ptr<fp32>(), k.data_ptr<fp32>(), v.data_ptr<fp32>(), w.data_ptr<float>(), u.data_ptr<fp32>(), y.data_ptr<fp32>());}PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {m.def("forward_bf16", &forward_bf16, "rwkv6 forward_bf16");m.def("forward_fp16", &forward_fp16, "rwkv6 forward_fp16");m.def("forward_fp32", &forward_fp32, "rwkv6 forward_fp32");}TORCH_LIBRARY(rwkv6, m) {m.def("forward_bf16", forward_bf16);m.def("forward_fp16", forward_fp16);m.def("forward_fp32", forward_fp32);}12345678910111213141516171819202122232425262728293031323334#include <torch/extension.h>#include "ATen/ATen.h"#include <c10/cuda/CUDAGuard.h>typedef at::BFloat16 bf16;typedef at::Half fp16;typedef float fp32;void cuda_forward_bf16(int B, int T, int C, int H, float *state, bf16 *r, bf16 *k, bf16 *v, float *w, bf16 *u, bf16 *y);void cuda_forward_fp16(int B, int T, int C, int H, float *state, fp16 *r, fp16 *k, fp16 *v, float *w, fp16 *u, fp16 *y);void cuda_forward_fp32(int B, int T, int C, int H, float *state, fp32 *r, fp32 *k, fp32 *v, float *w, fp32 *u, fp32 *y);void forward_bf16(int64_t B, int64_t T, int64_t C, int64_t H, torch::Tensor &state, torch::Tensor &r, torch::Tensor &k, torch::Tensor &v, torch::Tensor &w, torch::Tensor &u, torch::Tensor &y) {const at::cuda::OptionalCUDAGuard device_guard(device_of(state));cuda_forward_bf16(B, T, C, H, state.data_ptr<float>(), r.data_ptr<bf16>(), k.data_ptr<bf16>(), v.data_ptr<bf16>(), w.data_ptr<float>(), u.data_ptr<bf16>(), y.data_ptr<bf16>());}void forward_fp16(int64_t B, int64_t T, int64_t C, int64_t H, torch::Tensor &state, torch::Tensor &r, torch::Tensor &k, torch::Tensor &v, torch::Tensor &w, torch::Tensor &u, torch::Tensor &y) {const at::cuda::OptionalCUDAGuard device_guard(device_of(state));cuda_forward_fp16(B, T, C, H, state.data_ptr<float>(), r.data_ptr<fp16>(), k.data_ptr<fp16>(), v.data_ptr<fp16>(), w.data_ptr<float>(), u.data_ptr<fp16>(), y.data_ptr<fp16>());}void forward_fp32(int64_t B, int64_t T, int64_t C, int64_t H, torch::Tensor &state, torch::Tensor &r, torch::Tensor &k, torch::Tensor &v, torch::Tensor &w, torch::Tensor &u, torch::Tensor &y) {const at::cuda::OptionalCUDAGuard device_guard(device_of(state));cuda_forward_fp32(B, T, C, H, state.data_ptr<float>(), r.data_ptr<fp32>(), k.data_ptr<fp32>(), v.data_ptr<fp32>(), w.data_ptr<float>(), u.data_ptr<fp32>(), y.data_ptr<fp32>());}PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {m.def("forward_bf16", &forward_bf16, "rwkv6 forward_bf16");m.def("forward_fp16", &forward_fp16, "rwkv6 forward_fp16");m.def("forward_fp32", &forward_fp32, "rwkv6 forward_fp32");}TORCH_LIBRARY(rwkv6, m) {m.def("forward_bf16", forward_bf16);m.def("forward_fp16", forward_fp16);m.def("forward_fp32", forward_fp32);}
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677#include <stdio.h>#include <assert.h>#include "ATen/ATen.h"
typedef at::Half fp16;typedef at::BFloat16 bf16;typedef float fp32;
template <typename F>__global__ void kernel_forward(const int B, const int T, const int C, const int H, float *__restrict__ _state, const F *__restrict__ const _r, const F *__restrict__ const _w, const F *__restrict__ const _k, const F *__restrict__ const _v, const F *__restrict__ const _a, const F *__restrict__ const _b, F *__restrict__ const _y){ const int e = blockIdx.x / H; const int h = blockIdx.x % H; const int i = threadIdx.x; _state += h*_N_*_N_ + i*_N_; // wrong if B > 1 !!!
float state[_N_]; #pragma unroll for (int j = 0; j < _N_; j++) state[j] = _state[j];
__shared__ float r[_N_], k[_N_], w[_N_], a[_N_], b[_N_];
for (int _t = 0; _t < T; _t++) { const int t = e*T*C + h*_N_ + i + _t * C; __syncthreads(); r[i] = float(_r[t]); w[i] = __expf(-__expf(float(_w[t]))); k[i] = float(_k[t]); a[i] = float(_a[t]); b[i] = float(_b[t]); __syncthreads();
float sa = 0; #pragma unroll for (int j = 0; j < _N_; j++) { sa += a[j] * state[j]; }
float vv = float(_v[t]); float y = 0; #pragma unroll for (int j = 0; j < _N_; j++) { float& s = state[j]; s = s * w[j] + k[j] * vv + sa * b[j]; y += s * r[j]; } _y[t] = F(y); } #pragma unroll for (int j = 0; j < _N_; j++) _state[j] = state[j]; }
void cuda_forward_bf16(int B, int T, int C, int H, float *state, bf16 *r, bf16* w, bf16 *k, bf16 *v, bf16 *a, bf16 *b, bf16 *y){ assert(H*_N_ == C); assert(B == 1); // only for B=1 kernel_forward<<<dim3(B * H), dim3(_N_)>>>(B, T, C, H, state, r, w, k, v, a, b, y);}void cuda_forward_fp16(int B, int T, int C, int H, float *state, fp16 *r, fp16* w, fp16 *k, fp16 *v, fp16 *a, fp16 *b, fp16 *y){ assert(H*_N_ == C); assert(B == 1); // only for B=1 kernel_forward<<<dim3(B * H), dim3(_N_)>>>(B, T, C, H, state, r, w, k, v, a, b, y);}void cuda_forward_fp32(int B, int T, int C, int H, float *state, fp32 *r, fp32* w, fp32 *k, fp32 *v, fp32 *a, fp32 *b, fp32 *y){ assert(H*_N_ == C); assert(B == 1); // only for B=1 kernel_forward<<<dim3(B * H), dim3(_N_)>>>(B, T, C, H, state, r, w, k, v, a, b, y);}1234567891011121314151617181920212223242526#include <torch/extension.h>#include "ATen/ATen.h"
typedef at::Half fp16;typedef at::BFloat16 bf16;typedef float fp32;
void cuda_forward_bf16(int B, int T, int C, int H, float *state, bf16 *r, bf16 *w, bf16 *k, bf16 *v, bf16 *a, bf16 *b, bf16 *y);void cuda_forward_fp16(int B, int T, int C, int H, float *state, fp16 *r, fp16 *w, fp16 *k, fp16 *v, fp16 *a, fp16 *b, fp16 *y);void cuda_forward_fp32(int B, int T, int C, int H, float *state, fp32 *r, fp32 *w, fp32 *k, fp32 *v, fp32 *a, fp32 *b, fp32 *y);
void forward_bf16(int64_t B, int64_t T, int64_t C, int64_t H, torch::Tensor &state, torch::Tensor &r, torch::Tensor &w, torch::Tensor &k, torch::Tensor &v, torch::Tensor &a, torch::Tensor &b, torch::Tensor &y) { cuda_forward_bf16(B, T, C, H, state.data_ptr<float>(), r.data_ptr<bf16>(), w.data_ptr<bf16>(), k.data_ptr<bf16>(), v.data_ptr<bf16>(), a.data_ptr<bf16>(), b.data_ptr<bf16>(), y.data_ptr<bf16>());}void forward_fp16(int64_t B, int64_t T, int64_t C, int64_t H, torch::Tensor &state, torch::Tensor &r, torch::Tensor &w, torch::Tensor &k, torch::Tensor &v, torch::Tensor &a, torch::Tensor &b, torch::Tensor &y) { cuda_forward_fp16(B, T, C, H, state.data_ptr<float>(), r.data_ptr<fp16>(), w.data_ptr<fp16>(), k.data_ptr<fp16>(), v.data_ptr<fp16>(), a.data_ptr<fp16>(), b.data_ptr<fp16>(), y.data_ptr<fp16>());}void forward_fp32(int64_t B, int64_t T, int64_t C, int64_t H, torch::Tensor &state, torch::Tensor &r, torch::Tensor &w, torch::Tensor &k, torch::Tensor &v, torch::Tensor &a, torch::Tensor &b, torch::Tensor &y) { cuda_forward_fp32(B, T, C, H, state.data_ptr<float>(), r.data_ptr<fp32>(), w.data_ptr<fp32>(), k.data_ptr<fp32>(), v.data_ptr<fp32>(), a.data_ptr<fp32>(), b.data_ptr<fp32>(), y.data_ptr<fp32>());}
TORCH_LIBRARY(wkv7s, m) { m.def("forward_bf16", forward_bf16); m.def("forward_fp16", forward_fp16); m.def("forward_fp32", forward_fp32);}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141#include <torch/extension.h>#include "ATen/ATen.h"#include <iostream>#include <c10/cuda/CUDAGuard.h>typedef at::Half fp16;template <typename F>void cuda_wkv_forward(int B, int T, int C,float *w, float *u, F *k, F *v, F *y,float *aa, float *bb, float *pp);template <typename F>void cuda_mm8_seq(int B, int N, int M,F *x, int x_stride,uint8_t *w, int w_stride,F *mx, F *rx,F *my, F *ry,F *y, int y_stride);template <typename F>void cuda_mm8_one(int N, int M,F *x,uint8_t *w, int w_stride,F *mx, F *rx,F *my, F *ry,float *y);void wkv_forward(int64_t B, int64_t T, int64_t C,torch::Tensor &w, torch::Tensor &u,torch::Tensor &k, torch::Tensor &v, torch::Tensor &y,torch::Tensor &aa, torch::Tensor &bb, torch::Tensor &pp) {const at::cuda::OptionalCUDAGuard device_guard(device_of(w));switch (k.scalar_type()) {case c10::ScalarType::Half:cuda_wkv_forward(B, T, C,w.data_ptr<float>(), u.data_ptr<float>(),k.data_ptr<fp16>(), v.data_ptr<fp16>(), y.data_ptr<fp16>(),aa.data_ptr<float>(), bb.data_ptr<float>(), pp.data_ptr<float>());break;case c10::ScalarType::Float:cuda_wkv_forward(B, T, C,w.data_ptr<float>(), u.data_ptr<float>(),k.data_ptr<float>(), v.data_ptr<float>(), y.data_ptr<float>(),aa.data_ptr<float>(), bb.data_ptr<float>(), pp.data_ptr<float>());break;default:assert(false && "Only FP16 and FP32 are currently supported");}}void mm8_seq(int64_t B, int64_t N, int64_t M,torch::Tensor &x, torch::Tensor &w,torch::Tensor &mx, torch::Tensor &rx,torch::Tensor &my, torch::Tensor &ry,torch::Tensor &y) {assert(x.stride(1) == 1);assert(w.stride(1) == 1);assert(mx.stride(0) == 1 && rx.stride(0) == 1);assert(my.stride(0) == 1 && ry.stride(0) == 1);assert(y.stride(1) == 1);const at::cuda::OptionalCUDAGuard device_guard(device_of(w));switch (x.scalar_type()) {case c10::ScalarType::Half:cuda_mm8_seq(B, N, M,x.data_ptr<fp16>(), x.stride(0),w.data_ptr<uint8_t>(), w.stride(0),mx.data_ptr<fp16>(), rx.data_ptr<fp16>(),my.data_ptr<fp16>(), ry.data_ptr<fp16>(),y.data_ptr<fp16>(), y.stride(0));break;case c10::ScalarType::Float:cuda_mm8_seq(B, N, M,x.data_ptr<float>(), x.stride(0),w.data_ptr<uint8_t>(), w.stride(0),mx.data_ptr<float>(), rx.data_ptr<float>(),my.data_ptr<float>(), ry.data_ptr<float>(),y.data_ptr<float>(), y.stride(0));break;default:assert(false && "Only FP16 and FP32 are currently supported");}}void mm8_one(int64_t N, int64_t M,torch::Tensor &x, torch::Tensor &w,torch::Tensor &mx, torch::Tensor &rx,torch::Tensor &my, torch::Tensor &ry,torch::Tensor &y) {assert(x.stride(0) == 1);assert(w.stride(1) == 1);assert(mx.stride(0) == 1 && rx.stride(0) == 1);assert(my.stride(0) == 1 && ry.stride(0) == 1);assert(y.stride(0) == 1);const at::cuda::OptionalCUDAGuard device_guard(device_of(w));switch (x.scalar_type()) {case c10::ScalarType::Half:cuda_mm8_one(N, M,x.data_ptr<fp16>(),w.data_ptr<uint8_t>(), w.stride(0),mx.data_ptr<fp16>(), rx.data_ptr<fp16>(),my.data_ptr<fp16>(), ry.data_ptr<fp16>(),y.data_ptr<float>());break;case c10::ScalarType::Float:cuda_mm8_one(N, M,x.data_ptr<float>(),w.data_ptr<uint8_t>(), w.stride(0),mx.data_ptr<float>(), rx.data_ptr<float>(),my.data_ptr<float>(), ry.data_ptr<float>(),y.data_ptr<float>());break;default:assert(false && "Only FP16 and FP32 are currently supported");}}using torch::Tensor;#ifndef DISABLE_CUBLAS_GEMMvoid gemm_fp16_cublas(Tensor a, Tensor b, Tensor c);#endifPYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {m.def("wkv_forward", &wkv_forward, "wkv forward");m.def("mm8_seq", &mm8_seq, "mm8 seq");m.def("mm8_one", &mm8_one, "mm8 one");#ifndef DISABLE_CUBLAS_GEMMm.def("gemm_fp16_cublas", &gemm_fp16_cublas, "gemv fp16 cublas");#endif}TORCH_LIBRARY(rwkv, m) {m.def("wkv_forward", wkv_forward);m.def("mm8_seq", mm8_seq);m.def("mm8_one", mm8_one);#ifndef DISABLE_CUBLAS_GEMMm.def("gemm_fp16_cublas", gemm_fp16_cublas);#endif}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141#include <torch/extension.h>#include "ATen/ATen.h"#include <iostream>#include <c10/cuda/CUDAGuard.h>typedef at::Half fp16;template <typename F>void cuda_wkv_forward(int B, int T, int C,float *w, float *u, F *k, F *v, F *y,float *aa, float *bb, float *pp);template <typename F>void cuda_mm8_seq(int B, int N, int M,F *x, int x_stride,uint8_t *w, int w_stride,F *mx, F *rx,F *my, F *ry,F *y, int y_stride);template <typename F>void cuda_mm8_one(int N, int M,F *x,uint8_t *w, int w_stride,F *mx, F *rx,F *my, F *ry,float *y);void wkv_forward(int64_t B, int64_t T, int64_t C,torch::Tensor &w, torch::Tensor &u,torch::Tensor &k, torch::Tensor &v, torch::Tensor &y,torch::Tensor &aa, torch::Tensor &bb, torch::Tensor &pp) {const at::cuda::OptionalCUDAGuard device_guard(device_of(w));switch (k.scalar_type()) {case c10::ScalarType::Half:cuda_wkv_forward(B, T, C,w.data_ptr<float>(), u.data_ptr<float>(),k.data_ptr<fp16>(), v.data_ptr<fp16>(), y.data_ptr<fp16>(),aa.data_ptr<float>(), bb.data_ptr<float>(), pp.data_ptr<float>());break;case c10::ScalarType::Float:cuda_wkv_forward(B, T, C,w.data_ptr<float>(), u.data_ptr<float>(),k.data_ptr<float>(), v.data_ptr<float>(), y.data_ptr<float>(),aa.data_ptr<float>(), bb.data_ptr<float>(), pp.data_ptr<float>());break;default:assert(false && "Only FP16 and FP32 are currently supported");}}void mm8_seq(int64_t B, int64_t N, int64_t M,torch::Tensor &x, torch::Tensor &w,torch::Tensor &mx, torch::Tensor &rx,torch::Tensor &my, torch::Tensor &ry,torch::Tensor &y) {assert(x.stride(1) == 1);assert(w.stride(1) == 1);assert(mx.stride(0) == 1 && rx.stride(0) == 1);assert(my.stride(0) == 1 && ry.stride(0) == 1);assert(y.stride(1) == 1);const at::cuda::OptionalCUDAGuard device_guard(device_of(w));switch (x.scalar_type()) {case c10::ScalarType::Half:cuda_mm8_seq(B, N, M,x.data_ptr<fp16>(), x.stride(0),w.data_ptr<uint8_t>(), w.stride(0),mx.data_ptr<fp16>(), rx.data_ptr<fp16>(),my.data_ptr<fp16>(), ry.data_ptr<fp16>(),y.data_ptr<fp16>(), y.stride(0));break;case c10::ScalarType::Float:cuda_mm8_seq(B, N, M,x.data_ptr<float>(), x.stride(0),w.data_ptr<uint8_t>(), w.stride(0),mx.data_ptr<float>(), rx.data_ptr<float>(),my.data_ptr<float>(), ry.data_ptr<float>(),y.data_ptr<float>(), y.stride(0));break;default:assert(false && "Only FP16 and FP32 are currently supported");}}void mm8_one(int64_t N, int64_t M,torch::Tensor &x, torch::Tensor &w,torch::Tensor &mx, torch::Tensor &rx,torch::Tensor &my, torch::Tensor &ry,torch::Tensor &y) {assert(x.stride(0) == 1);assert(w.stride(1) == 1);assert(mx.stride(0) == 1 && rx.stride(0) == 1);assert(my.stride(0) == 1 && ry.stride(0) == 1);assert(y.stride(0) == 1);const at::cuda::OptionalCUDAGuard device_guard(device_of(w));switch (x.scalar_type()) {case c10::ScalarType::Half:cuda_mm8_one(N, M,x.data_ptr<fp16>(),w.data_ptr<uint8_t>(), w.stride(0),mx.data_ptr<fp16>(), rx.data_ptr<fp16>(),my.data_ptr<fp16>(), ry.data_ptr<fp16>(),y.data_ptr<float>());break;case c10::ScalarType::Float:cuda_mm8_one(N, M,x.data_ptr<float>(),w.data_ptr<uint8_t>(), w.stride(0),mx.data_ptr<float>(), rx.data_ptr<float>(),my.data_ptr<float>(), ry.data_ptr<float>(),y.data_ptr<float>());break;default:assert(false && "Only FP16 and FP32 are currently supported");}}using torch::Tensor;#ifndef DISABLE_CUBLAS_GEMMvoid gemm_fp16_cublas(Tensor a, Tensor b, Tensor c);#endifPYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {m.def("wkv_forward", &wkv_forward, "wkv forward");m.def("mm8_seq", &mm8_seq, "mm8 seq");m.def("mm8_one", &mm8_one, "mm8 one");#ifndef DISABLE_CUBLAS_GEMMm.def("gemm_fp16_cublas", &gemm_fp16_cublas, "gemv fp16 cublas");#endif}TORCH_LIBRARY(rwkv, m) {m.def("wkv_forward", wkv_forward);m.def("mm8_seq", mm8_seq);m.def("mm8_one", mm8_one);#ifndef DISABLE_CUBLAS_GEMMm.def("gemm_fp16_cublas", gemm_fp16_cublas);#endif}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103########################################################################################################
# The RWKV Language Model - https://github.com/BlinkDL/RWKV-LM
########################################################################################################
class TRIE:
__slots__ = tuple("ch,to,values,front".split(","))
to:list
values:set
def __init__(self, front=None, ch=None):
self.ch = ch
self.to = [None for ch in range(256)]
self.values = set()
self.front = front
def __repr__(self):
fr = self
ret = []
while(fr!=None):
if(fr.ch!=None):
ret.append(fr.ch)
fr = fr.front
return "<TRIE %s %s>"%(ret[::-1], self.values)
def add(self, key:bytes, idx:int=0, val=None):
if(idx == len(key)):
if(val is None):
val = key
self.values.add(val)
return self
ch = key[idx]
if(self.to[ch] is None):
self.to[ch] = TRIE(front=self, ch=ch)
return self.to[ch].add(key, idx=idx+1, val=val)
def find_longest(self, key:bytes, idx:int=0):
u:TRIE = self
ch:int = key[idx]
while(u.to[ch] is not None):
u = u.to[ch]
idx += 1
if(u.values):
ret = idx, u, u.values
if(idx==len(key)):
break
ch = key[idx]
return ret
class TRIE_TOKENIZER():
def __init__(self, file_name):
self.idx2token = {}
sorted = [] # must be already sorted
with open(file_name, "r", encoding="utf-8") as f:
lines = f.readlines()
for l in lines:
idx = int(l[:l.index(' ')])
x = eval(l[l.index(' '):l.rindex(' ')])
x = x.encode("utf-8") if isinstance(x, str) else x
assert isinstance(x, bytes)
assert len(x) == int(l[l.rindex(' '):])
sorted += [x]
self.idx2token[idx] = x
self.token2idx = {}
for k,v in self.idx2token.items():
self.token2idx[v] = int(k)
self.root = TRIE()
for t, i in self.token2idx.items():
_ = self.root.add(t, val=(t, i))
def encodeBytes(self, src:bytes):
idx:int = 0
tokens = []
while (idx < len(src)):
_idx:int = idx
idx, _, values = self.root.find_longest(src, idx)
assert(idx != _idx)
_, token = next(iter(values))
tokens.append(token)
return tokens
def decodeBytes(self, tokens):
return b''.join(map(lambda i: self.idx2token[i], tokens))
def encode(self, src):
return self.encodeBytes(src.encode("utf-8"))
def decode(self, tokens):
try:
return self.decodeBytes(tokens).decode('utf-8')
except:
return '\ufffd' # bad utf-8
def printTokens(self, tokens):
for i in tokens:
s = self.idx2token[i]
try:
s = s.decode('utf-8')
except:
pass
print(f'{repr(s)}{i}', end=' ')
print()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135########################################################################################################
# The RWKV Language Model - https://github.com/BlinkDL/RWKV-LM
########################################################################################################
import os, sys
import numpy as np
import torch
from torch.nn import functional as F
class PIPELINE_ARGS():
def __init__(self, temperature=1.0, top_p=0.85, top_k=0, alpha_frequency=0.2, alpha_presence=0.2, alpha_decay=0.996, token_ban=[], token_stop=[], chunk_len=256):
self.temperature = temperature
self.top_p = top_p
self.top_k = top_k
self.alpha_frequency = alpha_frequency # Frequency Penalty (as in GPT-3)
self.alpha_presence = alpha_presence # Presence Penalty (as in GPT-3)
self.alpha_decay = alpha_decay # gradually decay the penalty
self.token_ban = token_ban # ban the generation of some tokens
self.token_stop = token_stop # stop generation whenever you see any token here
self.chunk_len = chunk_len # split input into chunks to save VRAM (shorter -> slower)
class PIPELINE():
def __init__(self, model, WORD_NAME):
self.model = model
if WORD_NAME == 'cl100k_base':
import tiktoken
self.tokenizer = tiktoken.get_encoding(WORD_NAME)
elif WORD_NAME == 'rwkv_vocab_v20230424':
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from rwkv_tokenizer import TRIE_TOKENIZER
self.tokenizer = TRIE_TOKENIZER(os.path.dirname(os.path.abspath(__file__)) + '/rwkv_vocab_v20230424.txt')
else:
from tokenizers import Tokenizer
self.tokenizer = Tokenizer.from_file(WORD_NAME)
def refine_context(self, context):
context = context.strip().split('\n')
for c in range(len(context)):
context[c] = context[c].strip().strip('\u3000').strip('\r')
context = list(filter(lambda c: c != '', context))
context = '\n' + ('\n'.join(context)).strip()
if context == '':
context = '\n'
return context
def encode(self, x):
if 'Tokenizer' in str(type(self.tokenizer)):
return self.tokenizer.encode(x).ids
else:
return self.tokenizer.encode(x)
def decode(self, x):
return self.tokenizer.decode(x)
def sample_logits(self, logits, temperature=1.0, top_p=0.85, top_k=0):
if temperature == 0:
temperature = 1.0
top_p = 0
probs = F.softmax(logits.float(), dim=-1)
top_k = int(top_k)
# 'privateuseone' is the type of custom devices like `torch_directml.device()`
if probs.device.type in ['cpu', 'privateuseone']:
probs = probs.cpu().numpy()
sorted_ids = np.argsort(probs)
sorted_probs = probs[sorted_ids][::-1]
cumulative_probs = np.cumsum(sorted_probs)
cutoff = float(sorted_probs[np.argmax(cumulative_probs >= top_p)])
probs[probs < cutoff] = 0
if top_k < len(probs) and top_k > 0:
probs[sorted_ids[:-top_k]] = 0
if temperature != 1.0:
probs = probs ** (1.0 / temperature)
probs = probs / np.sum(probs)
out = np.random.choice(a=len(probs), p=probs)
return int(out)
else:
sorted_ids = torch.argsort(probs)
sorted_probs = probs[sorted_ids]
sorted_probs = torch.flip(sorted_probs, dims=(0,))
cumulative_probs = torch.cumsum(sorted_probs, dim=-1).cpu().numpy()
cutoff = float(sorted_probs[np.argmax(cumulative_probs >= top_p)])
probs[probs < cutoff] = 0
if top_k < len(probs) and top_k > 0:
probs[sorted_ids[:-top_k]] = 0
if temperature != 1.0:
probs = probs ** (1.0 / temperature)
out = torch.multinomial(probs, num_samples=1)[0]
return int(out)
def generate(self, ctx, token_count=100, args=PIPELINE_ARGS(), callback=None, state=None):
all_tokens = []
out_last = 0
out_str = ''
occurrence = {}
for i in range(token_count):
# forward & adjust prob.
tokens = self.encode(ctx) if i == 0 else [token]
while len(tokens) > 0:
out, state = self.model.forward(tokens[:args.chunk_len], state)
tokens = tokens[args.chunk_len:]
for n in args.token_ban:
out[n] = -float('inf')
for n in occurrence:
out[n] -= (args.alpha_presence + occurrence[n] * args.alpha_frequency)
# sampler
token = self.sample_logits(out, temperature=args.temperature, top_p=args.top_p, top_k=args.top_k)
if token in args.token_stop:
break
all_tokens += [token]
for xxx in occurrence:
occurrence[xxx] *= args.alpha_decay
ttt = self.decode([token])
www = 1
if ttt in ' \t0123456789':
www = 0
# elif ttt in '\r\n,.;?!"\':+-*/=#@$%^&_`~|<>\\()[]{},。;“”:?!()【】':
# www = 0.5
if token not in occurrence:
occurrence[token] = www
else:
occurrence[token] += www
# print(occurrence) # debug
# output
tmp = self.decode(all_tokens[out_last:])
if '\ufffd' not in tmp: # is valid utf-8 string?
if callback:
callback(tmp)
out_str += tmp
out_last = i + 1
return out_str.gitignore+3-1
app.py+10-8
pyproject.toml+1-1
rwkv/__init__.pyAdded
cuda/gemm_fp16_cublas.cpp → rwkv/cuda/gemm_fp16_cublas.cppRenamed+75-75
cuda/operators.cu → rwkv/cuda/operators.cuRenamed+246-246
cuda/rwkv5.cu → rwkv/cuda/rwkv5.cuRenamed+88-88
cuda/rwkv5_op.cpp → rwkv/cuda/rwkv5_op.cppRenamed+34-34
cuda/rwkv6.cu → rwkv/cuda/rwkv6.cuRenamed+87-87
cuda/rwkv6_op.cpp → rwkv/cuda/rwkv6_op.cppRenamed+34-34
rwkv/cuda/rwkv7.cuAdded+77
rwkv/cuda/rwkv7_op.cppAdded+26
cuda/wrapper.cpp → rwkv/cuda/wrapper.cppRenamed+141-141
rwkv/model.pyAdded
rwkv/rwkv_tokenizer.pyAdded+103
rwkv/rwkv_vocab_v20230424.txtAdded
rwkv/utils.pyAdded+135