/* Copyright (c) 2021-2022 Intel Corporation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS */ #include #include #include #include #include #include #include #include #include using namespace std; // generate rand int64_t [a, b] #define random_int(a, b) ( rand() % (b - a) + a ) // generate rand float [0, 1] #define random_float() (rand() / double(RAND_MAX)) #define printDetailed 0 #define printTime 1 // This version fused the log_softmax #define version 4 #define tolerance 4e-3 #define iterations 100 // Param for computaiton, tunable #define threadX 64 #define threadY 1 #define threadBS 1 template using __slm__ = cl::sycl::accessor; int64_t errors(0); constexpr int bs = 128; constexpr int W = 81; constexpr size_t H = 8732; constexpr size_t H_32 = (H / 2); constexpr int PredictShape = bs * W * H; constexpr int TargetShape = bs * H; constexpr int OutputShape = bs * H; constexpr int kLocalSize = threadX * threadY; // log_softmax: log(exp(x)/sum(exp(x))) = x - log(sum(exp(x))) template sycl::event loss_fwd_kernel(sycl::queue& queue, scalar_t* predict_xpu, int64_t* target_xpu, int64_t* mask, scalar_t* weight_xpu, scalar_t* log_softmax_xpu, scalar_t* output_xpu, scalar_t* output_neg_xpu) { size_t local_size = kLocalSize; float* predict_xpu_f = reinterpret_cast(predict_xpu); float* weight_xpu_f = reinterpret_cast(weight_xpu); float* log_softmax_xpu_f = reinterpret_cast(log_softmax_xpu); float* output_xpu_f = reinterpret_cast(output_xpu); float* output_neg_xpu_f = reinterpret_cast(output_neg_xpu); size_t num_group = bs; // 2D mapping sycl::range<3> local_range{ threadBS, threadY, threadX }; // divide in X and batchsize direction sycl::range<3> global_range{ bs, threadY /* W */, ((H_32 + threadX) / threadX) * threadX }; auto event = queue.submit([&](cl::sycl::handler& h) { sycl::accessor local_data(sycl::range<2> {W, threadX}, h); h.parallel_for(sycl::nd_range<3> {global_range, local_range}, [=](cl::sycl::nd_item<3> item) { int local_id_bs = item.get_local_id(0); int local_id_x = item.get_local_id(2); int local_id_y = item.get_local_id(1); int group_id_bs = item.get_group(0); int group_id_x = item.get_group(2); int group_id_y = item.get_group(1); sycl::half max0, max1; float data = 0.0f; float acc0 = 0.0f; float acc1 = 0.0f; int offset = group_id_bs * threadBS * W * H_32 + group_id_x * threadX + local_id_x; int offset2d = group_id_bs * threadBS * H_32 + group_id_x * threadX + local_id_x; int64_t idx0 = target_xpu[2 * offset2d]; int64_t idx1 = target_xpu[2 * offset2d + 1]; int64_t mk0 = mask[2 * offset2d]; int64_t mk1 = mask[2 * offset2d + 1]; float wx = weight_xpu_f[offset2d]; // Boundary Check if ((group_id_x * threadX + local_id_x) < H_32) { #pragma unroll for (int i = 0; i < W; i++) { local_data[i][local_id_x] = predict_xpu_f[offset + i * H_32]; } } item.barrier(cl::sycl::access::fence_space::local_space); // Early leave if ((group_id_x * threadX + local_id_x) >= H_32) return; // find max value in local data = local_data[0][local_id_x]; sycl::half* data_fp16 = reinterpret_cast(&data); max0 = data_fp16[0]; max1 = data_fp16[1]; #pragma unroll(8) for (int i = 1; i < W; i++) { data = local_data[i][local_id_x]; sycl::half* data_fp16 = reinterpret_cast(&data); if (data_fp16[0] > max0) max0 = data_fp16[0]; if (data_fp16[1] > max1) max1 = data_fp16[1]; } #pragma unroll(8) for (int i = 0; i < W; i++) { data = local_data[i][local_id_x]; sycl::half* data_fp16 = reinterpret_cast(&data); acc0 += static_cast(cl::sycl::exp(data_fp16[0] - max0)); acc1 += static_cast(cl::sycl::exp(data_fp16[1] - max1)); } // Write back sycl::half log_sum0 = max0 + cl::sycl::log(acc0); sycl::half log_sum1 = max1 + cl::sycl::log(acc1); #pragma unroll(8) for (int i = 0; i < W; i++) { data = local_data[i][local_id_x]; sycl::half* data_fp16 = reinterpret_cast(&data); data_fp16[0] = data_fp16[0] - log_sum0; data_fp16[1] = data_fp16[1] - log_sum1; log_softmax_xpu_f[offset + i * H_32] = data; } // Last Write Back for output { float o_tmp = 0.0f; sycl::half* o_tmph = reinterpret_cast(&o_tmp); float o_neg_tmp = 0.0f; sycl::half* o_neg_tmph = reinterpret_cast(&o_neg_tmp); float data0 = local_data[idx0][local_id_x]; float data1 = local_data[idx1][local_id_x]; sycl::half* wx_tmp = reinterpret_cast(&wx); sycl::half* data0_fp16 = reinterpret_cast(&data0); sycl::half* data1_fp16 = reinterpret_cast(&data1); o_tmph[0] = static_cast(-(data0_fp16[0] - log_sum0) * wx_tmp[0]); o_tmph[1] = static_cast(-(data1_fp16[1] - log_sum1) * wx_tmp[1]); o_neg_tmph[0] = static_cast(mk0) * o_tmph[0]; o_neg_tmph[1] = static_cast(mk1) * o_tmph[1]; output_xpu_f[offset2d] = o_tmp; output_neg_xpu_f[offset2d] = o_neg_tmp; } }); }); return event; } template sycl::event loss_bwd_kernel(sycl::queue& queue, scalar_t* log_softmax, gscalar_t* grad_output, gscalar_t* grad_output_neg, int64_t* target, scalar_t* weight, int64_t* mask, gscalar_t* grad_predict) { sycl::range<3> local_range{ threadBS, threadY, threadX }; sycl::range<3> global_range{ bs, threadY /* W */, ((H + threadX) / threadX) * threadX }; auto event = queue.submit([&](cl::sycl::handler& h) { h.parallel_for(sycl::nd_range<3> {global_range, local_range}, [=](cl::sycl::nd_item<3> item) { int group_id = item.get_group(0); int local_id = item.get_local_id(0); int local_id_bs = item.get_local_id(0); int local_id_x = item.get_local_id(2); int local_id_y = item.get_local_id(1); int group_id_bs = item.get_group(0); int group_id_x = item.get_group(2); int group_id_y = item.get_group(1); int linear_x_id = group_id_x * threadX + local_id_x; if (linear_x_id >= H) return; int offset2d = group_id_bs * H + linear_x_id; int idx = target[offset2d]; int sum_offset = group_id_bs * W * H + idx * H + linear_x_id; float tmp_grad = -(grad_output[offset2d] + grad_output_neg[offset2d] * mask[offset2d]) * weight[offset2d]; float sum_value = tmp_grad * log_softmax[sum_offset]; #pragma unroll for (int i = 0; i < W; ++i) { int in_offset = group_id_bs * W * H + i * H + linear_x_id; float tmp_sfm = cl::sycl::exp(log_softmax[in_offset]) * sum_value; float res = 0.0; if (i == idx) { res = tmp_grad - tmp_sfm; } else { res = -tmp_sfm; } grad_predict[in_offset] = res; } }); }); return event; } //log(x/(exp(max) * sum(exp(x-max)))) = x - max - log(sum(exp(x-max))) template void loss_fwd_cpu(scalar_t* predict, int64_t* target, scalar_t* weight, int64_t* mask, scalar_t* log_softmax, scalar_t* output, scalar_t* output_neg) { // max: Reduce vector> max_value; for (int i = 0; i < bs; ++i) { vector bs_max_value; for (int j = 0; j < H; ++j) { int64_t offset = i * W * H + j; scalar_t max = predict[offset]; for (int k = 0; k < W; ++k) { int64_t in_offset = offset + k * H; max = max > predict[in_offset] ? max : predict[in_offset]; } bs_max_value.push_back(max); } max_value.push_back(bs_max_value); } // log(sum(exp(x-max))) vector> sum_value; for (int i = 0; i < bs; ++i) { vector bs_sum_value; for (int j = 0; j < H; ++j) { float sum = 0.0; int64_t offset = i * W * H + j; for (int k = 0; k < W; ++k) { int64_t in_offset = offset + k * H; sum += std::exp(predict[in_offset] - max_value[i][j]); } bs_sum_value.push_back(sum); } sum_value.push_back(bs_sum_value); } // x - max - log(sum(exp(x-max))) for (int i = 0; i < bs; ++i) { for (int k = 0; k < W; ++k) { for (int j = 0; j < H; ++j) { int64_t offset = i * W * H + k * H + j; log_softmax[offset] = predict[offset] - max_value[i][j] - std::log(sum_value[i][j]); } } } // write out for (int i = 0; i < bs; ++i) { for (int j = 0; j < H; ++j) { int64_t offset = i * H + j; int64_t idx = target[offset]; int64_t in_offset = i * W * H + idx * H + j; auto log_softmax_value = log_softmax[in_offset]; output[offset] = -log_softmax_value * weight[offset]; output_neg[offset] = -log_softmax_value * weight[offset] * mask[offset]; } } } template void loss_bwd_cpu(scalar_t* predict, int64_t* target, scalar_t* weight, int64_t* mask, gscalar_t* grad_output, gscalar_t* grad_output_neg, gscalar_t* grad_predict) { for (int i = 0; i < bs; ++i) { for (int k = 0; k < W; ++k) { for (int j = 0; j < H; ++j) { int64_t offset = i * H + j; int64_t idx = target[offset]; int64_t predict_offset = i * W * H + k * H + j; if (idx == int64_t(k)) { grad_predict[predict_offset] = (-grad_output[offset] * weight[offset]) + (-grad_output_neg[offset] * weight[offset] * mask[offset]); } else { grad_predict[predict_offset] = 0; } } } } vector> sum_value; for (int i = 0; i < bs; ++i) { vector bs_sum_value; for (int j = 0; j < H; ++j) { float sum = 0.0; for (int k = 0; k < W; ++k) { int64_t offset = i * W * H + k * H + j; sum += grad_predict[offset] * predict[offset]; } bs_sum_value.push_back(sum); } sum_value.push_back(bs_sum_value); } for (int i = 0; i < bs; ++i) { for (int k = 0; k < W; ++k) { for (int j = 0; j < H; ++j) { int64_t offset = i * W * H + k * H + j; grad_predict[offset] = grad_predict[offset] - std::exp(predict[offset]) * sum_value[i][j]; } } } } template void verify(scalar_t* output, scalar_t* output_xpu, size_t sz) { int count = 0; for (size_t i = 0; i < sz; ++i) { int64_t offset = i; if (std::abs(output[offset] - output_xpu[offset]) > tolerance) { count++; if (count < 10) std::cout << "Error, output not equal, i=" << i << ", cpu_result = " << output[offset] << ", xpu_result = " << output_xpu[offset] << ", gap = " << (output[offset] - output_xpu[offset]) << std::endl; } } //if (count) printf("\nThere are %d Errors\n", count); errors += count; } template void LossNLL_FWD(sycl::queue& queue) { // malloc host memory scalar_t* predict = sycl::malloc_host(PredictShape, queue); scalar_t* weight = sycl::malloc_host(OutputShape, queue); int64_t* target = sycl::malloc_host(TargetShape, queue); int64_t* mask = sycl::malloc_host(TargetShape, queue); scalar_t* log_softmax = sycl::malloc_host(PredictShape, queue); scalar_t* output = sycl::malloc_host(OutputShape, queue); scalar_t* output_neg = sycl::malloc_host(OutputShape, queue); for (int i = 0; i < PredictShape; ++i) predict[i] = static_cast(random_float()); for (int i = 0; i < TargetShape; ++i) mask[i] = static_cast(random_int(0, 1)); for (int i = 0; i < TargetShape; ++i) target[i] = static_cast(random_int(0, W - 1)); for (int i = 0; i < OutputShape; ++i) weight[i] = static_cast(random_float()); // malloc device memory scalar_t* predict_xpu = sycl::malloc_device(PredictShape, queue); scalar_t* log_softmax_xpu = sycl::malloc_device(PredictShape, queue); int64_t* target_xpu = sycl::malloc_device(TargetShape, queue); int64_t* mask_xpu = sycl::malloc_device(TargetShape, queue); scalar_t* weight_xpu = sycl::malloc_device(OutputShape, queue); scalar_t* output_xpu = sycl::malloc_device(OutputShape, queue); scalar_t* output_neg_xpu = sycl::malloc_device(OutputShape, queue); // Host ----> Device if (printDetailed) std::cout << "begin H2D." << std::endl; queue.memcpy(predict_xpu, predict, PredictShape * sizeof(scalar_t)).wait(); queue.memcpy(target_xpu, target, TargetShape * sizeof(int64_t)).wait(); queue.memcpy(mask_xpu, mask, TargetShape * sizeof(int64_t)).wait(); queue.memcpy(weight_xpu, weight, OutputShape * sizeof(scalar_t)).wait(); vector durations(2, 0.0); int warmup = 10; for (int k = 0; k < warmup + iterations; ++k) { float duration_cpu = 0.0; float duration_gpu = 0.0; std::chrono::high_resolution_clock::time_point s, e; s = std::chrono::high_resolution_clock::now(); auto event = loss_fwd_kernel(queue, predict_xpu, target_xpu, mask_xpu, weight_xpu, log_softmax_xpu, output_xpu, output_neg_xpu); event.wait(); // GPU timer duration_gpu = (event.template get_profiling_info() - event.template get_profiling_info()) / 1000.0f / 1000.0f; // CPU timer e = std::chrono::high_resolution_clock::now(); duration_cpu = std::chrono::duration(e - s).count(); // ms if (k >= warmup) { durations[0] += duration_cpu; durations[1] += duration_gpu; } } if (printTime) std::cout << "LossNLL_FWD CPU time(ms)=. " << (durations[0] / iterations) << std::endl; if (printTime) std::cout << "LossNLL_FWD GPU time(ms)=. " << (durations[1] / iterations) << std::endl; double allBytes = static_cast(sizeof(scalar_t)) * static_cast(PredictShape * 2.0 + OutputShape * 3.0) + static_cast(sizeof(int64_t)) * static_cast(TargetShape * 2.0); printf("FWDBandWidth = %lf (GB / s), %lf\n", allBytes / (durations[1] / iterations) * 1000.0 / 1024.0 / 1024.0 / 1024.0, allBytes); // D2H scalar_t* output_xpu_host = sycl::malloc_host(OutputShape, queue); scalar_t* output_neg_xpu_host = sycl::malloc_host(OutputShape, queue); scalar_t* log_softmax_xpu_host = sycl::malloc_host(PredictShape, queue); queue.memcpy(output_xpu_host, output_xpu, OutputShape * sizeof(scalar_t)).wait(); queue.memcpy(output_neg_xpu_host, output_neg_xpu, OutputShape * sizeof(scalar_t)).wait(); queue.memcpy(log_softmax_xpu_host, log_softmax_xpu, PredictShape * sizeof(scalar_t)).wait(); // accuracy check loss_fwd_cpu(predict, target, weight, mask, log_softmax, output, output_neg); verify(output_neg, output_neg_xpu_host, bs * H); verify(output, output_xpu_host, bs * H); verify(log_softmax, log_softmax_xpu_host, bs * W * H); } template void LossNLL_BWD(sycl::queue& queue) { // malloc host memory gscalar_t* grad_predict = sycl::malloc_host(PredictShape, queue); scalar_t* log_softmax = sycl::malloc_host(PredictShape, queue); gscalar_t* grad_output = sycl::malloc_host(OutputShape, queue); gscalar_t* grad_output_neg = sycl::malloc_host(OutputShape, queue); int64_t* target = sycl::malloc_host(TargetShape, queue); int64_t* mask = sycl::malloc_host(TargetShape, queue); scalar_t* weight = sycl::malloc_host(OutputShape, queue); for (int i = 0; i < PredictShape; ++i) log_softmax[i] = static_cast(random_float()); for (int i = 0; i < OutputShape; ++i) grad_output[i] = static_cast(random_float()); for (int i = 0; i < OutputShape; ++i) grad_output_neg[i] = static_cast(random_float()); for (int i = 0; i < OutputShape; ++i) weight[i] = static_cast(random_float()); for (int i = 0; i < TargetShape; ++i) target[i] = static_cast(random_int(0, W - 1)); for (int i = 0; i < TargetShape; ++i) mask[i] = static_cast(random_int(0, 1)); // malloc device memory gscalar_t* grad_predict_xpu = sycl::malloc_device(PredictShape, queue); scalar_t* log_softmax_xpu = sycl::malloc_device(PredictShape, queue); gscalar_t* grad_output_xpu = sycl::malloc_device(OutputShape, queue); gscalar_t* grad_output_neg_xpu = sycl::malloc_device(OutputShape, queue); int64_t* target_xpu = sycl::malloc_device(TargetShape, queue); int64_t* mask_xpu = sycl::malloc_device(TargetShape, queue); scalar_t* weight_xpu = sycl::malloc_device(OutputShape, queue); // Host ----> Device if (printDetailed) std::cout << "begin H2D." << std::endl; queue.memcpy(log_softmax_xpu, log_softmax, PredictShape * sizeof(scalar_t)).wait(); queue.memcpy(grad_output_xpu, grad_output, OutputShape * sizeof(gscalar_t)).wait(); queue.memcpy(grad_output_neg_xpu, grad_output_neg, OutputShape * sizeof(gscalar_t)).wait(); queue.memcpy(weight_xpu, weight, OutputShape * sizeof(scalar_t)).wait(); queue.memcpy(target_xpu, target, TargetShape * sizeof(int64_t)).wait(); queue.memcpy(mask_xpu, mask, TargetShape * sizeof(int64_t)).wait(); vector durations(2, 0.0); int warmup = 10; for (int k = 0; k < warmup + iterations; ++k) { float duration_cpu = 0.0; float duration_gpu = 0.0; std::chrono::high_resolution_clock::time_point s, e; s = std::chrono::high_resolution_clock::now(); auto event = loss_bwd_kernel(queue, log_softmax_xpu, grad_output_xpu, grad_output_neg_xpu, target_xpu, weight_xpu, mask_xpu, grad_predict_xpu); event.wait(); // GPU timer duration_gpu = (event.template get_profiling_info() - event.template get_profiling_info()) / 1000.0f / 1000.0f; // CPU timer e = std::chrono::high_resolution_clock::now(); duration_cpu = std::chrono::duration(e - s).count(); // ms if (k >= warmup) { durations[0] += duration_cpu; durations[1] += duration_gpu; } } if (printTime) std::cout << "LossNLL_BWD CPU time(ms)=. " << (durations[0] / iterations) << std::endl; if (printTime) std::cout << "LossNLL_BWD GPU time(ms)=. " << (durations[1] / iterations) << std::endl; double allBytes = static_cast(sizeof(scalar_t)) * static_cast(PredictShape * 2.0 + OutputShape * 3.0) + static_cast(sizeof(int64_t)) * static_cast(TargetShape * 2.0); printf("BWDBandWidth = %lf (GB / s), %lf\n", allBytes / (durations[1] / iterations) * 1000.0 / 1000.0 / 1000.0 / 1000.0, allBytes); // D2H gscalar_t* grad_predict_xpu_host = sycl::malloc_host(PredictShape, queue); queue.memcpy(grad_predict_xpu_host, grad_predict_xpu, PredictShape * sizeof(gscalar_t)).wait(); // accuracy check loss_bwd_cpu(log_softmax, target, weight, mask, grad_output, grad_output_neg, grad_predict); verify(grad_predict, grad_predict_xpu_host, bs * W * H); } int main(int argc, char** argv) { auto propList = cl::sycl::property_list{ cl::sycl::property::queue::enable_profiling() }; cl::sycl::queue q(sycl::gpu_selector{}, propList); printf("Matrix size(bs * W * H) = %d * %d * %zu \n", bs, W, H); LossNLL_FWD(q); q.wait(); LossNLL_BWD(q); q.wait(); if (errors == 0) return 0; else return 1; }