im2col.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 "im2col.hpp"
  12. #include <sycl/sycl.hpp>
  13. #include <type_traits> // For std::is_same_v
  14. #include "ggml.h"
  15. template <typename T>
  16. static void im2col_kernel(const float * x, T * dst, int64_t batch_offset, int64_t offset_delta, int64_t IC, int64_t IW,
  17. int64_t IH, int64_t OH, int64_t OW, int64_t KW, int64_t KH, int64_t pelements, int64_t CHW,
  18. int s0, int s1, int p0, int p1, int d0, int d1, const sycl::nd_item<3> & item_ct1) {
  19. const int64_t work_group_size = item_ct1.get_local_range(2);
  20. const int64_t global_id = item_ct1.get_local_id(2) + (work_group_size * item_ct1.get_group(2));
  21. // make each work-item deal with more elements since sycl global range can not exceed max int
  22. for (int64_t i = global_id; i < pelements; i += (work_group_size * item_ct1.get_group_range(2))) {
  23. const int64_t ksize = OW * (KH > 1 ? KW : 1);
  24. const int64_t kx = i / ksize;
  25. const int64_t kd = kx * ksize;
  26. const int64_t ky = (i - kd) / OW;
  27. const int64_t ix = i % OW;
  28. const int64_t oh = item_ct1.get_group(1);
  29. const int64_t batch = item_ct1.get_group(0) / IC;
  30. const int64_t ic = item_ct1.get_group(0) % IC;
  31. const int64_t iiw = (ix * s0) + (kx * d0) - p0;
  32. const int64_t iih = (oh * s1) + (ky * d1) - p1;
  33. const int64_t offset_dst = (((batch * OH + oh) * OW + ix) * CHW) + (ic * (KW * KH) + ky * KW + kx);
  34. const int64_t offset_src_base = (ic * offset_delta) + (batch * batch_offset);
  35. const int64_t offset_src = offset_src_base + (iih * IW) + iiw;
  36. const bool out_of_bounds = (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW);
  37. const float src_val = out_of_bounds ? 0.0f : x[offset_src];
  38. if constexpr (std::is_same_v<T, sycl::half>) {
  39. dst[offset_dst] = sycl::half(src_val);
  40. } else if constexpr (std::is_same_v<T, float>) {
  41. dst[offset_dst] = src_val;
  42. }
  43. }
  44. }
  45. template <typename T>
  46. static void im2col_sycl_internal(const float * x, T * dst, int64_t IW, int64_t IH, int64_t OW, int64_t OH, int64_t KW,
  47. int64_t KH, int64_t IC, int64_t batch, int64_t batch_offset, int64_t offset_delta,
  48. int s0, int s1, int p0, int p1, int d0, int d1, queue_ptr stream) {
  49. const int64_t parallel_elements = OW * KW * KH;
  50. const int64_t num_blocks = (parallel_elements + SYCL_IM2COL_BLOCK_SIZE - 1) / SYCL_IM2COL_BLOCK_SIZE;
  51. // decrease global range when it exceeds the max int
  52. int64_t local_size = downsample_sycl_global_range(batch * IC * OH * num_blocks, SYCL_IM2COL_BLOCK_SIZE);
  53. sycl::range<3> block_nums(batch * IC, OH, num_blocks);
  54. sycl::range<3> local_range(1, 1, local_size);
  55. const int64_t CHW = IC * KH * KW;
  56. stream->parallel_for(sycl::nd_range<3>(block_nums * local_range, local_range), [=](sycl::nd_item<3> item_ct1) {
  57. im2col_kernel<T>(x, dst, batch_offset, offset_delta, IC, IW, IH, OH, OW, KW, KH, parallel_elements, CHW, s0, s1,
  58. p0, p1, d0, d1, item_ct1);
  59. });
  60. }
  61. static void im2col_sycl_f16(const float * x, sycl::half * dst, int64_t IW, int64_t IH, int64_t OW, int64_t OH,
  62. int64_t KW, int64_t KH, int64_t IC, int64_t batch, int64_t batch_offset,
  63. int64_t offset_delta, int s0, int s1, int p0, int p1, int d0, int d1, queue_ptr stream) {
  64. if (!stream->get_device().has(sycl::aspect::fp16)) {
  65. throw sycl::exception(sycl::make_error_code(sycl::errc::kernel_not_supported),
  66. "Device does not support half precision (fp16) operations!");
  67. }
  68. im2col_sycl_internal<sycl::half>(x, dst, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, offset_delta, s0, s1, p0,
  69. p1, d0, d1, stream);
  70. }
  71. static void im2col_sycl_f32(const float * x, float * dst, int64_t IW, int64_t IH, int64_t OW, int64_t OH, int64_t KW,
  72. int64_t KH, int64_t IC, int64_t batch, int64_t batch_offset, int64_t offset_delta, int s0,
  73. int s1, int p0, int p1, int d0, int d1, queue_ptr stream) {
  74. im2col_sycl_internal<float>(x, dst, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, offset_delta, s0, s1, p0, p1,
  75. d0, d1, stream);
  76. }
  77. void ggml_sycl_op_im2col(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
  78. const ggml_tensor * src0 = dst->src[0];
  79. const ggml_tensor * src1 = dst->src[1];
  80. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  81. GGML_ASSERT(dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  82. const int32_t s0 = ((const int32_t *) (dst->op_params))[0];
  83. const int32_t s1 = ((const int32_t *) (dst->op_params))[1];
  84. const int32_t p0 = ((const int32_t *) (dst->op_params))[2];
  85. const int32_t p1 = ((const int32_t *) (dst->op_params))[3];
  86. const int32_t d0 = ((const int32_t *) (dst->op_params))[4];
  87. const int32_t d1 = ((const int32_t *) (dst->op_params))[5];
  88. const bool is_2D = ((const int32_t *) (dst->op_params))[6] == 1;
  89. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  90. const int64_t IH = is_2D ? src1->ne[1] : 1;
  91. const int64_t IW = src1->ne[0];
  92. const int64_t KH = is_2D ? src0->ne[1] : 1;
  93. const int64_t KW = src0->ne[0];
  94. const int64_t OH = is_2D ? dst->ne[2] : 1;
  95. const int64_t OW = dst->ne[1];
  96. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / sizeof(float);
  97. const int64_t batch = src1->ne[is_2D ? 3 : 2];
  98. const size_t batch_offset = src1->nb[is_2D ? 3 : 2] / sizeof(float);
  99. queue_ptr stream = ctx.stream();
  100. if (dst->type == GGML_TYPE_F16) {
  101. im2col_sycl_f16((const float *) src1->data, (sycl::half *) dst->data, IW, IH, OW, OH, KW, KH, IC, batch,
  102. batch_offset, delta_offset, s0, s1, p0, p1, d0, d1, stream);
  103. } else {
  104. im2col_sycl_f32((const float *) src1->data, (float *) dst->data, IW, IH, OW, OH, KW, KH, IC, batch,
  105. batch_offset, delta_offset, s0, s1, p0, p1, d0, d1, stream);
  106. }
  107. }