text: /**
   * Copyright 2021-2022 Huawei Technologies Co., Ltd
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  #ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_GPU_NN_HSWISH_GRAD_GPU_KERNEL_H_
  #define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_GPU_NN_HSWISH_GRAD_GPU_KERNEL_H_
  
  #include <vector>
  #include "plugin/device/gpu/kernel/gpu_kernel.h"
  #include "plugin/device/gpu/kernel/gpu_kernel_factory.h"
  #include "plugin/device/gpu/kernel/cuda_impl/cuda_ops/hswish_impl.cuh"
  
  namespace mindspore {
  namespace kernel {
  template <typename T>
  class HSwishGradKernelMod : public DeprecatedNativeGpuKernelMod {
   public:
    HSwishGradKernelMod() { ResetResource(); }
    ~HSwishGradKernelMod() override = default;
  
    bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
                const std::vector<AddressPtr> &outputs, void *stream_ptr) override {
      if (is_null_input_) {
        return true;
      }
      VARIABLE_NOT_USED(workspace);
      T *input = GetDeviceAddress<T>(inputs, 0);
      T *x = GetDeviceAddress<T>(inputs, 1);
      T *output = GetDeviceAddress<T>(outputs, 0);
      CalHSwishGrad(input_size_, input, x, output, reinterpret_cast<cudaStream_t>(stream_ptr));
      return true;
    }
  
    bool Init(const CNodePtr &kernel_node) override {
      auto kernel_name = common::AnfAlgo::GetCNodeName(kernel_node);
      kernel_node_ = kernel_node;
      size_t input_num = common::AnfAlgo::GetInputTensorNum(kernel_node);
      if (input_num != 2) {
        MS_LOG(EXCEPTION) << "For '" << kernel_name << "', the number of inputs should be 2, but got " << input_num;
      }
      size_t output_num = common::AnfAlgo::GetOutputTensorNum(kernel_node);
      if (output_num != 1) {
        MS_LOG(EXCEPTION) << "For '" << kernel_name << "', the number of outputs should be 1, but got " << output_num;
      }
      auto input_shape = common::AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0);
      is_null_input_ = CHECK_SHAPE_NULL(input_shape, kernel_name, "input");
      if (is_null_input_) {
        InitSizeLists();
        return true;
      }
      input_size_ = 1;
      for (size_t i = 0; i < input_shape.size(); i++) {
        input_size_ *= input_shape[i];
      }
      InitSizeLists();
      return true;
    }
  
    void ResetResource() noexcept override {
      input_size_ = 1;
      is_null_input_ = false;
      input_size_list_.clear();
      output_size_list_.clear();
      workspace_size_list_.clear();
    }
  
   protected:
    void InitSizeLists() override {
      input_size_list_.push_back(input_size_ * sizeof(T));
      input_size_list_.push_back(input_size_ * sizeof(T));
      output_size_list_.push_back(input_size_ * sizeof(T));
    }
  
   private:
    size_t input_size_;
    bool is_null_input_;
  };
  }  // namespace kernel
  }  // namespace mindspore
  
  #endif  // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_GPU_NN_HSWISH_GRAD_GPU_KERNEL_H_
  