common.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // MIT license
  3. // Copyright (C) 2024 Intel Corporation
  4. // SPDX-License-Identifier: MIT
  5. //
  6. //
  7. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  8. // See https://llvm.org/LICENSE.txt for license information.
  9. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  10. //
  11. #include "common.hpp"
  12. #include "ggml-backend-impl.h"
  13. #include "ggml-impl.h"
  14. int get_current_device_id() {
  15. return dpct::dev_mgr::instance().current_device_id();
  16. }
  17. void* ggml_sycl_host_malloc(size_t size) try {
  18. if (getenv("GGML_SYCL_NO_PINNED") != nullptr) {
  19. return nullptr;
  20. }
  21. void* ptr = nullptr;
  22. // allow to use dpct::get_in_order_queue() for host malloc
  23. dpct::err0 err = CHECK_TRY_ERROR(
  24. ptr = (void*)sycl::malloc_host(size, dpct::get_in_order_queue()));
  25. if (err != 0) {
  26. // clear the error
  27. GGML_LOG_ERROR("WARNING: failed to allocate %.2f MB of pinned memory: %s\n", size / 1024.0 / 1024.0, "syclGetErrorString is not supported");
  28. return nullptr;
  29. }
  30. return ptr;
  31. } catch (sycl::exception const& exc) {
  32. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  33. << ", line:" << __LINE__ << std::endl;
  34. std::exit(1);
  35. }
  36. void ggml_sycl_host_free(void* ptr) try {
  37. // allow to use dpct::get_in_order_queue() for host malloc
  38. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, dpct::get_in_order_queue())));
  39. } catch (sycl::exception const& exc) {
  40. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  41. << ", line:" << __LINE__ << std::endl;
  42. std::exit(1);
  43. }
  44. int64_t downsample_sycl_global_range(int64_t accumulate_block_num, int64_t block_size) {
  45. const int64_t max_range = std::numeric_limits<int>::max();
  46. int64_t sycl_down_blk_size = block_size;
  47. int64_t global_range = accumulate_block_num * sycl_down_blk_size;
  48. while(global_range > max_range) {
  49. sycl_down_blk_size /= 2;
  50. global_range = accumulate_block_num * sycl_down_blk_size;
  51. }
  52. return sycl_down_blk_size;
  53. }
  54. void ggml_sycl_op_flatten(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
  55. const ggml_tensor *src1, ggml_tensor *dst,
  56. const ggml_sycl_op_flatten_t op) try {
  57. const bool use_src1 = src1 != nullptr;
  58. if(use_src1)
  59. GGML_ASSERT(strcmp(src1->buffer->buft->iface.get_name(src1->buffer->buft), GGML_SYCL_NAME "_Split") != 0);
  60. GGML_ASSERT(strcmp(dst->buffer->buft->iface.get_name(dst->buffer->buft), GGML_SYCL_NAME "_Split") != 0);
  61. // dd = data device
  62. float * src0_ddf = (float *) src0->data;
  63. float * src1_ddf = use_src1 ? (float *) src1->data : nullptr;
  64. float * dst_ddf = (float *) dst->data;
  65. ggml_sycl_pool_alloc<float> src0_f(ctx.pool());
  66. ggml_sycl_pool_alloc<float> src1_f(ctx.pool());
  67. ggml_sycl_pool_alloc<float> dst_f(ctx.pool());
  68. ggml_sycl_set_device(ctx.device);
  69. queue_ptr main_stream = ctx.stream();
  70. // GGML_SYCL_DEBUG("ctx.device=%d, main_stream=%p src0_on_device=%d, src1_on_device=%d, dst_on_device=%d\n",
  71. // ctx.device, main_stream, src0_on_device, src1_on_device, dst_on_device);
  72. // do the computation
  73. op(ctx, src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  74. // print_ggml_tensor("tensor", dst);
  75. }
  76. catch (sycl::exception const &exc) {
  77. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  78. << ", line:" << __LINE__ << std::endl;
  79. std::exit(1);
  80. }