! * \brief Information about a data type that can be accessed at runtime */
| 416 | * \brief Information about a data type that can be accessed at runtime |
| 417 | */ |
| 418 | class DType { |
| 419 | private: |
| 420 | MGE_WIN_DECLSPEC_FUC MEGDNN_NORETURN void on_request_lowbit_size() const; |
| 421 | // HACK: This is required in ParameterizedDType::downcast_from |
| 422 | public: |
| 423 | MGE_WIN_DECLSPEC_FUC MEGDNN_NORETURN void on_assert_is_failed( |
| 424 | const char* rname) const; |
| 425 | |
| 426 | protected: |
| 427 | struct Trait { |
| 428 | const char* const name; |
| 429 | const uint16_t size_log; //!< log2 of sizeof(dt) for non-lowbit |
| 430 | const uint16_t low_bit; //!< 0 for non-lowbit; otherwise num bits |
| 431 | DTypeEnum enumv; |
| 432 | DTypeCategory category; |
| 433 | DTypeSignedness signedness; |
| 434 | const bool has_param; |
| 435 | }; |
| 436 | Trait* m_trait; |
| 437 | |
| 438 | explicit DType(Trait* t) : m_trait(t) {} |
| 439 | |
| 440 | public: |
| 441 | DType() : m_trait(nullptr) {} |
| 442 | |
| 443 | bool valid() const { return m_trait != nullptr; } |
| 444 | |
| 445 | /*! |
| 446 | * \brief name of this data type |
| 447 | */ |
| 448 | const char* name() const { return m_trait ? m_trait->name : "invalid"; } |
| 449 | |
| 450 | /*! |
| 451 | * \brief size of elem_num this data type, if fraction form return ceil |
| 452 | */ |
| 453 | size_t size(size_t elem_num) const { |
| 454 | if (m_trait->low_bit != 0) |
| 455 | return static_cast<size_t>((m_trait->low_bit * elem_num + 7) / 8); |
| 456 | return elem_num << m_trait->size_log; |
| 457 | } |
| 458 | |
| 459 | /*! |
| 460 | * \brief max number of elements within representation |
| 461 | * |
| 462 | * The total size of the tensor (in bytes) should not exceed size_t range. |
| 463 | */ |
| 464 | size_t max_elements() const { |
| 465 | if (m_trait->low_bit != 0) |
| 466 | return std::numeric_limits<size_t>::max(); |
| 467 | |
| 468 | return std::numeric_limits<size_t>::max() >> m_trait->size_log; |
| 469 | } |
| 470 | |
| 471 | size_t low_bit() const { return m_trait->low_bit; } |
| 472 | |
| 473 | bool is_low_bit() const { return low_bit() != 0; } |
| 474 | |
| 475 | bool is_complex() const { |
no test coverage detected