| 1 |
/* |
| 2 |
* Copyright (c) 2019-2021, 2024 Arm Limited. |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and associated documentation files (the "Software"), to |
| 8 |
* deal in the Software without restriction, including without limitation the |
| 9 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 |
* sell copies of the Software, and to permit persons to whom the Software is |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be included in all |
| 14 |
* copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 |
* SOFTWARE. |
| 23 |
*/ |
| 24 |
#ifndef ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NEINSTANCENORMALIZATIONLAYER_H |
| 25 |
#define ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NEINSTANCENORMALIZATIONLAYER_H |
| 26 |
|
| 27 |
#include "arm_compute/runtime/IFunction.h" |
| 28 |
#include "arm_compute/runtime/IMemoryManager.h" |
| 29 |
#include "arm_compute/runtime/MemoryGroup.h" |
| 30 |
#include "arm_compute/runtime/MemoryManagerOnDemand.h" |
| 31 |
#include "arm_compute/runtime/NEON/functions/NEPermute.h" |
| 32 |
#include "arm_compute/runtime/NEON/functions/NEReductionOperation.h" |
| 33 |
#include "arm_compute/runtime/Tensor.h" |
| 34 |
|
| 35 |
#include <memory> |
| 36 |
|
| 37 |
namespace arm_compute |
| 38 |
{ |
| 39 |
class ITensor; |
| 40 |
class NEInstanceNormalizationLayerKernel; |
| 41 |
|
| 42 |
/** Basic function to perform a Instance normalization. |
| 43 |
* |
| 44 |
* This function runs the following kernels: |
| 45 |
* -# NEInstanceNormalizationLayerKernel |
| 46 |
*/ |
| 47 |
class NEInstanceNormalizationLayer : public IFunction |
| 48 |
{ |
| 49 |
public: |
| 50 |
/** Constructor */ |
| 51 |
NEInstanceNormalizationLayer(std::shared_ptr<IMemoryManager> memory_manager); |
| 52 |
NEInstanceNormalizationLayer() : NEInstanceNormalizationLayer(MemoryManagerOnDemand::make_default()) |
| 53 |
{ |
| 54 |
} |
| 55 |
/** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 56 |
NEInstanceNormalizationLayer(const NEInstanceNormalizationLayer &) = delete; |
| 57 |
/** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 58 |
NEInstanceNormalizationLayer &operator=(const NEInstanceNormalizationLayer &) = delete; |
| 59 |
/** Prevent instances of this class from being moved (As this class contains non movable objects) */ |
| 60 |
NEInstanceNormalizationLayer(NEInstanceNormalizationLayer &&) = delete; |
| 61 |
/** Prevent instances of this class from being moved (As this class contains non movable objects) */ |
| 62 |
NEInstanceNormalizationLayer &operator=(NEInstanceNormalizationLayer &&) = delete; |
| 63 |
/** Default destructor */ |
| 64 |
~NEInstanceNormalizationLayer(); |
| 65 |
/** Set the input and output tensors. |
| 66 |
* |
| 67 |
* Valid data layouts: |
| 68 |
* - NHWC |
| 69 |
* - NCHW |
| 70 |
* |
| 71 |
* Valid data type configurations: |
| 72 |
* |src |dst | |
| 73 |
* |:--------|:---------| |
| 74 |
* |F16 |F16 | |
| 75 |
* |F32 |F32 | |
| 76 |
* |
| 77 |
* @param[in, out] input Source tensor. In case of @p output tensor = nullptr this tensor will store the result of the normalization. |
| 78 |
* Data types supported: F16/F32. Data layout supported: NHWC, NCHW |
| 79 |
* @param[out] output Destination tensor. Data types and data layouts supported: same as @p input. |
| 80 |
* @param[in] gamma (Optional) The scale scalar value applied to the normalized tensor. Defaults to 1.0 |
| 81 |
* @param[in] beta (Optional) The offset scalar value applied to the normalized tensor. Defaults to 0.0 |
| 82 |
* @param[in] epsilon (Optional) Lower bound value for the normalization. Defaults to 1e-12 |
| 83 |
*/ |
| 84 |
void configure(ITensor *input, ITensor *output, float gamma = 1.0f, float beta = 0.0f, float epsilon = 1e-12f); |
| 85 |
|
| 86 |
/** Static function to check if given info will lead to a valid configuration of @ref NEInstanceNormalizationLayer. |
| 87 |
* |
| 88 |
* @param[in] input Source tensor info. Data types supported: F16/F32. Data layout supported: NHWC, NCHW |
| 89 |
* @param[in] output Destination tensor info. Data types and data layouts supported: same as @p input. |
| 90 |
* @param[in] gamma (Optional) The scale scalar value applied to the normalized tensor. Defaults to 1.0 |
| 91 |
* @param[in] beta (Optional) The offset scalar value applied to the normalized tensor. Defaults to 0.0 |
| 92 |
* @param[in] epsilon (Optional) Lower bound value for the normalization. Defaults to 1e-12 |
| 93 |
* |
| 94 |
* @return a status |
| 95 |
*/ |
| 96 |
static Status validate(const ITensorInfo *input, |
| 97 |
const ITensorInfo *output, |
| 98 |
float gamma = 1.0f, |
| 99 |
float beta = 0.0f, |
| 100 |
float epsilon = 1e-12f); |
| 101 |
|
| 102 |
// Inherited methods overridden: |
| 103 |
void run() override; |
| 104 |
|
| 105 |
private: |
| 106 |
MemoryGroup _memory_group; |
| 107 |
std::unique_ptr<NEInstanceNormalizationLayerKernel> _normalization_kernel; |
| 108 |
bool _is_nchw; |
| 109 |
NEPermute _permute_input; |
| 110 |
NEPermute _permute_output; |
| 111 |
Tensor _permuted_input; |
| 112 |
Tensor _permuted_output; |
| 113 |
}; |
| 114 |
} // namespace arm_compute |
| 115 |
#endif // ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NEINSTANCENORMALIZATIONLAYER_H |