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.

named_tensor.h 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * \file dnn/include/megdnn/named_tensor.h
  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. #pragma once
  13. #include "megdnn/internal/defs.h"
  14. #include "megdnn/opr_param_defs.h"
  15. #include <array>
  16. #include <string>
  17. #include "megdnn/thin/small_vector.h"
  18. #include "megdnn/internal/visibility_prologue.h"
  19. namespace megdnn {
  20. class Dimension {
  21. public:
  22. enum class Name : char {
  23. N = 'N', // Batch size
  24. C = 'C', // input channel
  25. H = 'H', // input height
  26. W = 'W', // input width
  27. G = 'G', // group
  28. K = 'K', // output channel
  29. R = 'R', // filter height
  30. S = 'S', // filter width
  31. P = 'P', // output height
  32. Q = 'Q', // output width
  33. };
  34. static constexpr uint32_t UNDETERMINED_EXTENT =
  35. std::numeric_limits<uint32_t>::max();
  36. static const Name NAME_ALL[];
  37. static const int NR_NAMES;
  38. Dimension() = default;
  39. Dimension(const std::string& expr);
  40. Dimension(Name name, uint32_t stride, uint32_t extent = UNDETERMINED_EXTENT)
  41. : m_name{name}, m_stride{stride}, m_extent{extent} {}
  42. Dimension(const Dimension& rhs) { operator=(rhs); }
  43. Dimension& operator=(const Dimension& rhs);
  44. bool operator==(const Dimension& rhs) const;
  45. bool operator<(const Dimension& rhs) const;
  46. Dimension operator*(const Dimension& rhs) const;
  47. Dimension operator/(const Dimension& rhs) const;
  48. std::string to_string() const;
  49. Name name() const { return m_name; }
  50. uint32_t extent() const { return m_extent; }
  51. uint32_t stride() const { return m_stride; }
  52. private:
  53. Name m_name;
  54. uint32_t m_stride;
  55. uint32_t m_extent;
  56. };
  57. struct NamedTensorShape {
  58. using Format = param::ConvBias::Format;
  59. static constexpr size_t MAX_NDIM = MEGDNN_MAX_NDIM;
  60. std::array<Dimension, MAX_NDIM> dims;
  61. size_t ndim = 0;
  62. NamedTensorShape() = default;
  63. NamedTensorShape(const NamedTensorShape& rhs) = default;
  64. NamedTensorShape(const SmallVector<Dimension>& init_shape);
  65. NamedTensorShape(std::initializer_list<Dimension> init_shape);
  66. std::string to_string() const;
  67. bool eq_shape(const NamedTensorShape& rhs) const;
  68. Dimension& operator[](size_t i) { return dims[i]; }
  69. Dimension operator[](size_t i) const { return dims[i]; }
  70. NamedTensorShape static make_named_tensor_shape(Format format);
  71. };
  72. } // namespace megdnn
  73. #include "megdnn/internal/visibility_epilogue.h"
  74. // vim: syntax=cpp.doxygen

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台