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.

lsq.cpp 2.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * \file dnn/src/common/lsq.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
  10. * implied.
  11. */
  12. #include "megdnn/oprs.h"
  13. #include "src/common/utils.h"
  14. namespace megdnn {
  15. void LSQBase::deduce_layout_fwd(const TensorLayout& input, TensorLayout& output) {
  16. output = TensorLayout(input, input.dtype);
  17. }
  18. void LSQBase::check_layout_fwd(
  19. const TensorLayout& input, const TensorLayout& scale,
  20. const TensorLayout& zero_point, const TensorLayout& grad_scale,
  21. const TensorLayout& output) {
  22. megdnn_assert(input.dtype == dtype::Float32());
  23. megdnn_assert(scale.dtype == dtype::Float32());
  24. megdnn_assert(zero_point.dtype == dtype::Float32());
  25. megdnn_assert(grad_scale.dtype == dtype::Float32());
  26. TensorLayout expected;
  27. deduce_layout_fwd(input, expected);
  28. megdnn_assert_eq_layout(expected, output);
  29. }
  30. void LSQForward::deduce_layout(
  31. const TensorLayout& input, const TensorLayout& /* scale */,
  32. const TensorLayout& /*zero_point*/, const TensorLayout& /*grad_scale*/,
  33. TensorLayout& output) {
  34. deduce_layout_fwd(input, output);
  35. }
  36. void LSQForward::check_exec(
  37. const TensorLayout& input, const TensorLayout& scale,
  38. const TensorLayout& zero_point, const TensorLayout& grad_scale,
  39. const TensorLayout& output, size_t workspace_in_bytes) {
  40. check_layout_fwd(input, scale, zero_point, grad_scale, output);
  41. auto required_workspace_space =
  42. get_workspace_in_bytes(input, scale, zero_point, grad_scale, output);
  43. megdnn_assert(workspace_in_bytes >= required_workspace_space);
  44. }
  45. void LSQBackward::check_exec(
  46. const TensorLayout& diff, const TensorLayout& input, const TensorLayout& scale,
  47. const TensorLayout& zero_point, const TensorLayout& grad_scale,
  48. const TensorLayout& grad_x, const TensorLayout& grad_s,
  49. size_t workspace_in_bytes) {
  50. megdnn_assert_eq_shape(diff, input);
  51. megdnn_assert_eq_shape(grad_x, input);
  52. auto required_worspace_space = get_workspace_in_bytes(
  53. diff, input, scale, zero_point, grad_scale, grad_x, grad_s);
  54. megdnn_assert(workspace_in_bytes >= required_worspace_space);
  55. }
  56. } // namespace megdnn