Files
TheRock/rocm-libraries/projects/miopen/driver/mloPReLUHost.hpp
T
derek.holloway 7502bf64a4
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
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

104 lines
4.3 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 <miopen/ford.hpp>
#include <miopen/tensor.hpp>
#include <miopen/tensor_view_utils.hpp>
#include <miopen/prelu/utils.hpp>
template <typename Tgpu, typename Tcheck>
int32_t mloPReLUBackwardRunHost(const miopenTensorDescriptor_t inputDesc,
const miopenTensorDescriptor_t weightDesc,
const miopenTensorDescriptor_t doutputDesc,
const miopenTensorDescriptor_t dinputDesc,
const Tgpu* input,
const Tgpu* weight,
const Tgpu* doutput,
Tcheck* dinput_host,
Tcheck* dweight_host)
{
auto input_tv = miopen::get_inner_expanded_tv<5>(miopen::deref(inputDesc));
auto doutput_tv = miopen::get_inner_expanded_tv<5>(miopen::deref(doutputDesc));
auto dinput_tv = miopen::get_inner_expanded_tv<5>(miopen::deref(dinputDesc));
auto input_sz = miopen::deref(inputDesc).GetElementSize();
auto weight_sz = miopen::deref(weightDesc).GetElementSize();
auto weight_grad_collector = std::vector<float>(input_sz);
miopen::par_ford(input_sz)([&](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>(doutput[doutput_tv.get_tensor_view_idx(tensor_layout)]);
if(dinput_host)
{
float weight_v;
if(weight_sz == 1)
weight_v = static_cast<float>(weight[0]);
else
weight_v = static_cast<float>(weight[tensor_layout.layout[1]]);
float input_grad_v = input_v > 0 ? grad_v : weight_v * grad_v;
dinput_host[dinput_tv.get_tensor_view_idx(tensor_layout)] =
static_cast<Tcheck>(input_grad_v);
}
if(dweight_host)
{
weight_grad_collector[gid] = input_v > 0 ? 0 : input_v * grad_v;
}
});
if(dweight_host)
{
if(weight_sz == 1)
{
double sum = 0;
for(int i = 0; i < input_sz; ++i)
sum += static_cast<double>(weight_grad_collector[i]);
dweight_host[0] = static_cast<Tcheck>(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_sz)([&](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]);
});
});
dweight_host[i] = static_cast<Tcheck>(sum);
});
}
}
return miopenStatusSuccess;
}