Skip to content
Snippets Groups Projects
Commit ce29a7d2 authored by Marwa ABDELOUINISSE's avatar Marwa ABDELOUINISSE
Browse files

Remove duplicate file: DropoutImpl_forward_kernels.hpp

Rename to DropoutImpl_kernels.hpp and update content
parent ec80f204
No related branches found
No related tags found
No related merge requests found
Pipeline #65123 failed
/********************************************************************************
* Copyright (c) 2024 CEA-List
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
********************************************************************************/
#ifndef AIDGE_CPU_OPERATOR_DROPOUTIMPL_FORWARD_KERNEL_H_
#define AIDGE_CPU_OPERATOR_DROPOUTIMPL_FORWARD_KERNEL_H_
#include <cmath>
#include <cstddef>
#include <numeric>
#include <random>
#include "aidge/backend/cpu/operator/DropoutImpl.hpp"
#include "aidge/data/Data.hpp"
#include "aidge/utils/Registrar.hpp"
#include "aidge/utils/Types.h"
namespace Aidge {
template <class I, class O>
void DropoutImpl_cpu_forward_kernel(const typename Dropout_Op::Attrs& attr, const std::vector<DimSize_t>& dims, const void* input_, void* output_) {
// Cast input and output to appropriate types
const I* input = static_cast<const I*>(input_);
O* output = static_cast<O*>(output_);
// Extract dropout rate from attributes
// const float dropoutRate = attr.dropout_ratio; // Assuming Attrs has dropout_ratio field
// const float dropoutRate = attr.probability; // Assuming Attrs has Probability field
const float dropoutRate = std::get<0>(attr);
const float scale = 1.0f / (1.0f - dropoutRate);
// Initialize random number generator
std::random_device rd;
std::mt19937 gen(rd());
std::bernoulli_distribution dist(1.0f - dropoutRate);
// Calculate total number of elements in the input tensor
std::size_t totalElements = std::accumulate(dims.begin(), dims.end(), std::size_t(1), std::multiplies<std::size_t>());
// Apply dropout
for (std::size_t i = 0; i < totalElements; ++i) {
if (dist(gen)) {
output[i] = input[i] * scale; // Retain and scale input
} else {
output[i] = 0; // Dropout
}
}
}
// Register kernel implementations for different input/output types
namespace {
static Registrar<DropoutImplForward_cpu> registrarDropoutImplForward_cpu_Float32(
{DataType::Float32, DataType::Float32}, Aidge::DropoutImpl_cpu_forward_kernel<float, float>);
static Registrar<DropoutImplForward_cpu> registrarDropoutImplForward_cpu_Int32(
{DataType::Int32, DataType::Int32}, Aidge::DropoutImpl_cpu_forward_kernel<int, int>);
static Registrar<DropoutImplForward_cpu> registrarDropoutImplForward_cpu_Float64(
{DataType::Float64, DataType::Float64}, Aidge::DropoutImpl_cpu_forward_kernel<double, double>);
} // namespace
} // namespace Aidge
#endif /* AIDGE_CPU_OPERATOR_DROPOUTIMPL_FORWARD_KERNEL_H_ */
\ No newline at end of file
  • Author Owner

    Hello,

    I removed the redundant file DropoutImpl_forward_kernels.hpp and updated its content in DropoutImpl_kernels.hpp file.

    Edited by Marwa ABDELOUINISSE
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment