scale.cu 1021 B

12345678910111213141516171819202122232425262728293031
  1. #include "scale.cuh"
  2. static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
  3. const int i = blockDim.x*blockIdx.x + threadIdx.x;
  4. if (i >= k) {
  5. return;
  6. }
  7. dst[i] = scale * x[i];
  8. }
  9. static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
  10. const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
  11. scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
  12. }
  13. void ggml_cuda_op_scale(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  14. const ggml_tensor * src0 = dst->src[0];
  15. const float * src0_d = (const float *)src0->data;
  16. float * dst_d = (float *)dst->data;
  17. cudaStream_t stream = ctx.stream();
  18. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  19. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  20. float scale;
  21. memcpy(&scale, dst->op_params, sizeof(float));
  22. scale_f32_cuda(src0_d, dst_d, scale, ggml_nelements(src0), stream);
  23. }