1 /*
2  * Copyright (c) 2023 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 SRC_DYNAMIC_FUSION_SKETCH_GPU_COMPONENTS_CL_CLCOMPONENTLOGITS1DNORM
25 #define SRC_DYNAMIC_FUSION_SKETCH_GPU_COMPONENTS_CL_CLCOMPONENTLOGITS1DNORM
26 
27 #include "arm_compute/dynamic_fusion/sketch/attributes/SoftmaxAttributes.h"
28 #include "src/dynamic_fusion/sketch/gpu/components/IGpuKernelComponent.h"
29 
30 namespace arm_compute
31 {
32 /** Forward declaration */
33 class ITensorInfo;
34 namespace experimental
35 {
36 namespace dynamic_fusion
37 {
38 /** Forward declaration */
39 template <typename T>
40 class ArgumentPack;
41 
42 /** Forward declaration */
43 class ClTemplateLogits1DNorm;
44 
45 /** Component to calculate the final step of the Softmax Layer
46  * where each logit value is multiplied by the inverse of the sum of the logits.
47  *
48  *  1D example:
49  *
50  *      (input)  src: [x1 x2 ... xn], shape: (1 x d)
51  *      (input)  sum: [x1 + x2 + ... + xn], shape: (1 x 1)
52  *      (output) dst: [x1/sum x2/sum ... xn/sum], shape: (1 x d)
53  *
54  *  This component is used by the softmax operator to get the final result.
55 */
56 class ClComponentLogits1DNorm final : public IGpuKernelComponent
57 {
58 public:
59     /** Attributes are a set of backend-agnostic parameters that define what a component does */
60     using Attributes = SoftmaxAttributes;
61 
62     /** Validate the component
63      *
64      * @param[in] properties Component properties @ref Properties
65      * @param[in] tensors    Tensor arguments to the component
66      * @param[in] attributes Component attributes @ref Attributes
67      *
68      * @return Status        Validation results
69      *
70      * Tensor argument names:
71      * - ACL_SRC_0: Input
72      * - ACL_SRC_1: Input
73      * - ACL_DST_0: Output
74      *
75      * Tensor argument constness:
76      * - ACL_SRC_0: Const
77      * - ACL_SRC_1: Const
78      * - ACL_DST_0: Const
79      *
80      * Valid data layouts:
81      * - All
82      *
83      ** Valid data type configurations:
84      * |ACL_SRC_0  |ACL_SRC_1  |ACL_DST_0  |
85      * |:----------|:----------|:----------|
86      * |F16        | F16       | F16       |
87      * |F32        | F32       | F32       |
88      */
89     static Status validate(
90         const Properties                &properties,
91         const ArgumentPack<ITensorInfo> &tensors,
92         const Attributes                &attributes);
93 
94     /** Constructor
95      *
96      * Similar to @ref ClComponentLogits1DNorm::validate()
97      */
98     ClComponentLogits1DNorm(ComponentId                      id,
99                             const Properties                &properties,
100                             const ArgumentPack<ITensorInfo> &tensors,
101                             const Attributes                &attributes);
102 
103     /** Destructor */
104     ~ClComponentLogits1DNorm() override;
105     /** Prevent instances of this class from being copy constructed */
106     ClComponentLogits1DNorm(const ClComponentLogits1DNorm &component) = delete;
107     /** Prevent instances of this class from being copied */
108     ClComponentLogits1DNorm &operator=(const ClComponentLogits1DNorm &component) = delete;
109     /** Allow instances of this class to be move constructed */
110     ClComponentLogits1DNorm(ClComponentLogits1DNorm &&component) = default;
111     /** Allow instances of this class to be moved */
112     ClComponentLogits1DNorm &operator=(ClComponentLogits1DNorm &&component) = default;
113     /** Get template writer for the component */
114     const IGpuTemplateComponentWriter *template_writer() const override;
115     /** Get component type */
type()116     GpuComponentType type() const override
117     {
118         return GpuComponentType::Unfusable;
119     }
120 
121 private:
122     std::unique_ptr<ClTemplateLogits1DNorm> _component_writer;
123 };
124 } // namespace dynamic_fusion
125 } // namespace experimental
126 } // namespace arm_compute
127 
128 #endif /* SRC_DYNAMIC_FUSION_SKETCH_GPU_COMPONENTS_CL_CLCOMPONENTLOGITS1DNORM */
129