1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| std::vector<data_t> ascend_softmax(std::vector<data_t> input) { std::size_t input_sz = input.size(); std::size_t byte_count = input_sz * sizeof(data_t);
if (byte_count < 32) { return softmax(input); }
std::size_t elem_per_group = 0; if (byte_count >= PHYSICAL_CORES * UB_MAX_BYTES) elem_per_group = UB_MAX_BYTES / sizeof(data_t); else if (byte_count >= PHYSICAL_CORES * 51200) elem_per_group = 51200 / sizeof(data_t); else if (byte_count >= PHYSICAL_CORES * 25600) elem_per_group = 25600 / sizeof(data_t); else if (byte_count >= PHYSICAL_CORES * 12800) elem_per_group = 12800 / sizeof(data_t); else if (byte_count >= PHYSICAL_CORES * 5120) elem_per_group = 5120 / sizeof(data_t); else if (byte_count >= PHYSICAL_CORES * 2560) elem_per_group = 2560 / sizeof(data_t); else elem_per_group = 1280 / sizeof(data_t); const std::size_t repeat_per_group = elem_per_group * sizeof(data_t) / 256; const std::size_t tail_elem_count = input_sz % elem_per_group; const std::size_t group_num = (tail_elem_count > 0) ? ((input_sz / elem_per_group) + 1) : (input_sz / elem_per_group);
sycl::queue Q(sycl::ascend_selector{});
auto dev_buf = sycl::malloc_device<data_t>(group_num * elem_per_group, Q); auto sum_res_buf = sycl::malloc_device<data_t>(group_num * (32 / sizeof(data_t)), Q);
std::vector<data_t> sum_res(group_num * (32 / sizeof(data_t)), 0.0f); std::vector<data_t> res(input_sz, 0.0f);
Q.memcpy(dev_buf, input.data(), byte_count);
Q.launch<class Summary>(group_num, [=](sycl::group<1> group) { bisheng::vector<data_t, UB_MAX_BYTES / sizeof(data_t)> input_vec; bisheng::vector<data_t, UB_MAX_BYTES / 256> sum_vec; std::size_t group_id = group.get_group_id();
input_vec.load( sycl::global_ptr<data_t>(dev_buf + group_id * elem_per_group).get(), elem_per_group);
if (tail_elem_count > 0 && group_id == group_num - 1) { bisheng::vector_view<data_t> input_vec_v(input_vec.data(), tail_elem_count);
bisheng::vec_exp(input_vec_v, input_vec_v); for (int i = 0; i < tail_elem_count; ++i) sum_res_buf[group_id * (32 / sizeof(data_t))] += input_vec_v[i]; } else { bisheng::vector_view<data_t> input_vec_v(input_vec.data(), elem_per_group); bisheng::vector_view<data_t> sum_vec_v(sum_vec.data(), repeat_per_group * 8);
bisheng::vec_exp(input_vec_v, input_vec_v); bisheng::vec_cross_add(sum_vec_v, input_vec_v); for (int i = 0; i < repeat_per_group * 8; i += 8) { sum_res_buf[group_id * (32 / sizeof(data_t))] += sum_vec_v[i]; } }
input_vec.store( sycl::global_ptr<data_t>(dev_buf + group_id * elem_per_group).get(), elem_per_group); });
Q.memcpy(sum_res.data(), sum_res_buf, group_num * (32 / sizeof(data_t)) * sizeof(data_t)); Q.wait();
data_t sum; for (int i = 0; i < sum_res.size(); i += 32 / sizeof(data_t)) sum += sum_res[i];
Q.launch<class Softmax>(group_num, [=](sycl::group<1> group) { bisheng::vector<data_t, UB_MAX_BYTES / sizeof(data_t)> exp_res_vec; bisheng::vector<data_t, UB_MAX_BYTES / sizeof(data_t)> divisor_vec(sum); bisheng::vector<data_t, UB_MAX_BYTES / sizeof(data_t)> res_vec; std::size_t group_id = group.get_group_id();
exp_res_vec.load( sycl::global_ptr<data_t>(dev_buf + group_id * elem_per_group).get(), elem_per_group);
if (tail_elem_count > 0 && group_id == group_num - 1) { bisheng::vector_view<data_t> exp_res_vec_v(exp_res_vec.data(), tail_elem_count); bisheng::vector_view<data_t> divisor_vec_v(divisor_vec.data(), tail_elem_count); bisheng::vector_view<data_t> res_vec_v(res_vec.data(), tail_elem_count);
bisheng::vec_div(res_vec_v, exp_res_vec_v, divisor_vec_v); } else { bisheng::vector_view<data_t> exp_res_vec_v(exp_res_vec.data(), elem_per_group); bisheng::vector_view<data_t> divisor_vec_v(divisor_vec.data(), elem_per_group); bisheng::vector_view<data_t> res_vec_v(res_vec.data(), elem_per_group); bisheng::vec_div(res_vec_v, exp_res_vec_v, divisor_vec_v); }
res_vec.store( sycl::global_ptr<data_t>(dev_buf + group_id * elem_per_group).get(), elem_per_group); });
Q.memcpy(res.data(), dev_buf, byte_count); Q.wait();
sycl::free(dev_buf, Q); sycl::free(sum_res_buf, Q);
return res; }
|