* \brief like TensorFormats, but only including common formats and DEFAULT. * */
| 9 | * |
| 10 | */ |
| 11 | class Format { |
| 12 | public: |
| 13 | enum class Type { |
| 14 | DEFAULT = 0, |
| 15 | NCHW = 1, ///< [N, C, H, W] |
| 16 | NHWC = 2, ///< [N, H, W, C] |
| 17 | }; |
| 18 | std::string to_string() const { |
| 19 | switch (m_type) { |
| 20 | case Type::DEFAULT: |
| 21 | return "default"; |
| 22 | case Type::NCHW: |
| 23 | return "nchw"; |
| 24 | case Type::NHWC: |
| 25 | return "nhwc"; |
| 26 | default: |
| 27 | mgb_throw(MegBrainError, "bad format type"); |
| 28 | } |
| 29 | } |
| 30 | Format() : m_type(Type::DEFAULT) {} |
| 31 | Format(std::string str) { |
| 32 | if (str == "default") { |
| 33 | m_type = Type::DEFAULT; |
| 34 | } else if (str == "nchw") { |
| 35 | m_type = Type::NCHW; |
| 36 | } else if (str == "nhwc") { |
| 37 | m_type = Type::NHWC; |
| 38 | } else { |
| 39 | mgb_throw( |
| 40 | MegBrainError, |
| 41 | "Invalid format type." |
| 42 | " Only support \"default\", \"nchw\" and \"nhwc\""); |
| 43 | } |
| 44 | } |
| 45 | Format(Type type) : m_type(type) {} |
| 46 | Type type() const { return m_type; } |
| 47 | bool operator==(const Format& b) const { return m_type == b.type(); } |
| 48 | bool operator==(const Format::Type& b) const { return m_type == b; } |
| 49 | bool operator!=(const Format& b) const { return m_type != b.type(); } |
| 50 | bool operator!=(const Format::Type& b) const { return m_type != b; } |
| 51 | |
| 52 | private: |
| 53 | Type m_type = Type::DEFAULT; |
| 54 | }; |
| 55 | |
| 56 | } // namespace mgb::imperative |