You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sigmoid.cc 1.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * Copyright 2022 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <memory>
  17. #include <vector>
  18. #include "common/graph_kernel/expanders/expander_factory.h"
  19. #include "ir/dtype.h"
  20. namespace mindspore::graphkernel::expanders {
  21. class Sigmoid : public OpDesc {
  22. public:
  23. Sigmoid() = default;
  24. ~Sigmoid() = default;
  25. static NodePtr Exec(const inner::LiteGraph::GraphBuilder &gb, const NodePtrList &inputs) {
  26. const auto &input_x = inputs[0];
  27. auto dtype = input_x->type;
  28. tensor::TensorPtr data_one = std::make_shared<tensor::Tensor>(static_cast<double>(1.0), TypeIdToType(dtype));
  29. auto const_one = gb.Value(data_one);
  30. auto neg_x = gb.Emit("Neg", {input_x});
  31. auto exp_neg_x = gb.Emit("Exp", {neg_x});
  32. auto add_exp = gb.Emit("Add", {const_one, exp_neg_x});
  33. auto result = gb.Emit("RealDiv", {const_one, add_exp});
  34. return result;
  35. }
  36. protected:
  37. NodePtrList Expand() override { return {Exec(gb, gb.Get()->inputs())}; }
  38. };
  39. OP_EXPANDER_REGISTER("Sigmoid", Sigmoid);
  40. NodePtr SigmoidExpand(const inner::LiteGraph::GraphBuilder &gb, const NodePtrList &inputs) {
  41. return Sigmoid::Exec(gb, inputs);
  42. }
  43. } // namespace mindspore::graphkernel::expanders