out-prod.cu 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * llama.cpp - commit 3f1ae2e32cde00c39b96be6d01c2997c29bae555 - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #include "out-prod.cuh"
  27. #include <cstdint>
  28. void ggml_cuda_out_prod(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  29. const ggml_tensor * src0 = dst->src[0];
  30. const ggml_tensor * src1 = dst->src[1];
  31. GGML_TENSOR_BINARY_OP_LOCALS
  32. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  33. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  34. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  35. GGML_ASSERT(ggml_is_contiguous(src0));
  36. GGML_ASSERT(ggml_is_contiguous(dst));
  37. GGML_ASSERT(ne01 == ne11);
  38. GGML_ASSERT(ne0 == ne00);
  39. GGML_ASSERT(ne1 == ne10);
  40. GGML_ASSERT(ne2 == src0->ne[2]);
  41. GGML_ASSERT(ne2 == src1->ne[2]);
  42. GGML_ASSERT(ne3 == src0->ne[3]);
  43. GGML_ASSERT(ne3 == src1->ne[3]);
  44. const float * src0_d = (const float *) src0->data;
  45. const float * src1_d = (const float *) src1->data;
  46. float * dst_d = (float *) dst->data;
  47. cudaStream_t stream = ctx.stream();
  48. cublasHandle_t handle = ctx.cublas_handle();
  49. const float alpha = 1.0f;
  50. const float beta = 0.0f;
  51. GGML_ASSERT(ne2 == 1);
  52. GGML_ASSERT(ne3 == 1);
  53. CUBLAS_CHECK(cublasSetStream(handle, stream));
  54. const bool src1_T = ggml_is_transposed(src1);
  55. const cublasOperation_t src1_cublas_op = src1_T ? CUBLAS_OP_N : CUBLAS_OP_T;
  56. const int64_t ldb = (src1_T ? nb10 : nb11) / sizeof(float);
  57. GGML_ASSERT( (src1_T ? nb11 : nb10) == sizeof(float));
  58. CUBLAS_CHECK(
  59. cublasSgemm(handle, CUBLAS_OP_N, src1_cublas_op,
  60. ne0, ne1, ne01,
  61. &alpha, src0_d, ne00,
  62. src1_d, ldb,
  63. &beta, dst_d, ne0));
  64. }