CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
101 lines
4.2 KiB
C++
101 lines
4.2 KiB
C++
/*******************************************************************************
|
|
*
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2024 Advanced Micro Devices, Inc.
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
*******************************************************************************/
|
|
|
|
#pragma once
|
|
|
|
#include "tensor_holder.hpp"
|
|
#include <miopen/tensor_view_utils.hpp>
|
|
|
|
template <class T>
|
|
void cpu_prelu_backward(const tensor<T> input,
|
|
const tensor<T> weight,
|
|
const tensor<T> output_grad,
|
|
tensor<T>& ref_input_grad,
|
|
tensor<T>& ref_weight_grad,
|
|
const bool has_dinput = true,
|
|
const bool has_dweight = true)
|
|
{
|
|
auto N = input.desc.GetElementSize();
|
|
auto input_tv = miopen::get_inner_expanded_tv<5>(input.desc);
|
|
auto weight_tv = miopen::get_inner_expanded_tv<1>(weight.desc);
|
|
auto output_grad_tv = miopen::get_inner_expanded_tv<5>(output_grad.desc);
|
|
auto input_grad_tv = miopen::get_inner_expanded_tv<5>(ref_input_grad.desc);
|
|
auto weight_grad_tv = miopen::get_inner_expanded_tv<1>(ref_weight_grad.desc);
|
|
|
|
auto weight_grad_collector = std::vector<float>(N);
|
|
|
|
miopen::par_ford(N)([&](int gid) {
|
|
auto tensor_layout = tensor_layout_t<5>(input_tv, gid);
|
|
float input_v = static_cast<float>(input[input_tv.get_tensor_view_idx(tensor_layout)]);
|
|
float grad_v =
|
|
static_cast<float>(output_grad[output_grad_tv.get_tensor_view_idx(tensor_layout)]);
|
|
|
|
if(has_dinput)
|
|
{
|
|
float weight_v;
|
|
weight_v = static_cast<float>(
|
|
weight[weight.desc.GetElementSize() == 1
|
|
? 0
|
|
: weight_tv.get_tensor_view_idx({tensor_layout.layout[1]})]);
|
|
|
|
float input_grad_v = input_v > 0 ? grad_v : weight_v * grad_v;
|
|
ref_input_grad[input_grad_tv.get_tensor_view_idx(tensor_layout)] =
|
|
static_cast<T>(input_grad_v);
|
|
}
|
|
if(has_dweight)
|
|
{
|
|
weight_grad_collector[gid] = input_v > 0 ? 0 : input_v * grad_v;
|
|
}
|
|
});
|
|
|
|
if(has_dweight)
|
|
{
|
|
if(weight.desc.GetElementSize() == 1)
|
|
{
|
|
double sum = 0;
|
|
for(int i = 0; i < N; ++i)
|
|
sum += static_cast<double>(weight_grad_collector[i]);
|
|
ref_weight_grad[0] = static_cast<T>(sum);
|
|
}
|
|
else
|
|
{
|
|
size_t inner_size = std::accumulate(
|
|
&input_tv.size[2], &input_tv.size[4], 1ul, std::multiplies<size_t>());
|
|
size_t outer_size = inner_size * input_tv.size[1];
|
|
miopen::par_ford(weight.desc.GetElementSize())([&](int i) {
|
|
double sum = 0;
|
|
miopen::ford(input_tv.size[0])([&](int j) {
|
|
miopen::ford(inner_size)([&](int k) {
|
|
sum += static_cast<double>(
|
|
weight_grad_collector[j * outer_size + i * inner_size + k]);
|
|
});
|
|
});
|
|
ref_weight_grad[weight_grad_tv.get_tensor_view_idx({i})] = static_cast<T>(sum);
|
|
});
|
|
}
|
|
}
|
|
}
|