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.

softmax.cpp 2.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * \file dnn/test/cuda/softmax.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "test/cuda/fixture.h"
  12. #include "megdnn/tensor_iter.h"
  13. #include "test/common/checker.h"
  14. #include "test/common/softmax.h"
  15. #include "src/common/utils.h"
  16. #include "test/cuda/utils.h"
  17. // to check cudnn version
  18. #include <cudnn.h>
  19. #include "test/cuda/benchmark.h"
  20. namespace megdnn {
  21. namespace test {
  22. TEST_F(CUDA, SOFTMAX_FORWARD) {
  23. auto args = softmax::get_args();
  24. std::vector<DType> dtypes{dtype::Float16(), dtype::Float32()};
  25. for (auto dtype : dtypes)
  26. for (auto&& arg : args) {
  27. auto param = arg.param;
  28. auto src = arg.ishape;
  29. Checker<Softmax> checker(handle_cuda());
  30. if (dtype == dtype::BFloat16()) {
  31. checker.set_epsilon(2e-2);
  32. } else {
  33. checker.set_epsilon(1e-2);
  34. }
  35. checker.set_param(param).set_dtype(0, dtype).set_dtype(1, dtype).exec(
  36. TensorShapeArray{src, {}});
  37. }
  38. }
  39. TEST_F(CUDA, SOFTMAX_BACKWARD) {
  40. auto args = softmax::get_args();
  41. for (auto&& arg : args) {
  42. Checker<SoftmaxBackward> checker(handle_cuda());
  43. TensorLayout ilayout = TensorLayout(arg.ishape, dtype::Float32());
  44. TensorLayout olayout;
  45. {
  46. auto opr = handle_cuda()->create_operator<SoftmaxForward>();
  47. opr->param() = arg.param;
  48. opr->deduce_layout(ilayout, olayout);
  49. }
  50. auto set_dtype = [&checker](DType dtype) {
  51. checker.set_dtype(0, dtype).set_dtype(1, dtype).set_dtype(2, dtype);
  52. };
  53. set_dtype(dtype::Float32());
  54. checker.set_epsilon(1e-3).set_param(arg.param).exec(
  55. TensorShapeArray{ilayout, olayout, ilayout});
  56. }
  57. }
  58. } // namespace test
  59. } // namespace megdnn
  60. // vim: syntax=cpp.doxygen