arange.cu 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. #include "arange.cuh"
  2. static __global__ void arange_f32(float * dst, const int ne0, const float start, const float step) {
  3. // blockIDx.x: idx of ne0 / BLOCK_SIZE
  4. int nidx = threadIdx.x + blockIdx.x * blockDim.x;
  5. if (nidx >= ne0) {
  6. return;
  7. }
  8. dst[nidx] = start + step * nidx;
  9. }
  10. static void arange_f32_cuda(float * dst, const int ne0, const float start, const float step, cudaStream_t stream) {
  11. int num_blocks = (ne0 + CUDA_ARANGE_BLOCK_SIZE - 1) / CUDA_ARANGE_BLOCK_SIZE;
  12. arange_f32<<<num_blocks, CUDA_ARANGE_BLOCK_SIZE, 0, stream>>>(dst, ne0, start, step);
  13. }
  14. void ggml_cuda_op_arange(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  15. float * dst_d = (float *)dst->data;
  16. cudaStream_t stream = ctx.stream();
  17. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  18. float start;
  19. float stop;
  20. float step;
  21. memcpy(&start, (float *)dst->op_params + 0, sizeof(float));
  22. memcpy(&stop, (float *)dst->op_params + 1, sizeof(float));
  23. memcpy(&step, (float *)dst->op_params + 2, sizeof(float));
  24. int64_t steps = (int64_t)ceil((stop - start) / step);
  25. GGML_ASSERT(ggml_nelements(dst) == steps);
  26. arange_f32_cuda(dst_d, dst->ne[0], start, step, stream);
  27. }