Program Listing for File dtype.hpp#

Return to documentation for file (python/morpheus/morpheus/_lib/include/morpheus/objects/dtype.hpp)

/*
 * SPDX-FileCopyrightText: Copyright (c) 2021-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include "morpheus/export.h"

#include <cudf/types.hpp>

#include <climits>  // for CHAR_BIT
#include <cstddef>  // for size_t
#include <cstdint>  // for int32_t
#include <string>   // for string

namespace morpheus {

// Pulled from cuDF

template <typename T>
constexpr std::size_t size_in_bits()
{
    static_assert(CHAR_BIT == 8, "Size of a byte must be 8 bits.");
    return sizeof(T) * CHAR_BIT;
}

enum class MORPHEUS_EXPORT TypeId : int32_t
{
    EMPTY,
    INT8,
    INT16,
    INT32,
    INT64,
    UINT8,
    UINT16,
    UINT32,
    UINT64,
    FLOAT32,
    FLOAT64,
    BOOL8,
    STRING,

    //   TIMESTAMP_DAYS,          ///< point in time in days since Unix Epoch in int32
    //   TIMESTAMP_SECONDS,       ///< point in time in seconds since Unix Epoch in int64
    //   TIMESTAMP_MILLISECONDS,  ///< point in time in milliseconds since Unix Epoch in int64
    //   TIMESTAMP_MICROSECONDS,  ///< point in time in microseconds since Unix Epoch in int64
    //   TIMESTAMP_NANOSECONDS,   ///< point in time in nanoseconds since Unix Epoch in int64
    //   DURATION_DAYS,           ///< time interval of days in int32
    //   DURATION_SECONDS,        ///< time interval of seconds in int64
    //   DURATION_MILLISECONDS,   ///< time interval of milliseconds in int64
    //   DURATION_MICROSECONDS,   ///< time interval of microseconds in int64
    //   DURATION_NANOSECONDS,    ///< time interval of nanoseconds in int64
    //   DICTIONARY32,            ///< Dictionary type using int32 indices
    //   LIST,                    ///< List elements
    //   DECIMAL32,               ///< Fixed-point type with int32_t
    //   DECIMAL64,               ///< Fixed-point type with int64_t
    //   STRUCT,                  ///< Struct elements

    // `NUM_TYPE_IDS` must be last!
    NUM_TYPE_IDS
};

struct MORPHEUS_EXPORT DType
{
    DType(TypeId tid);

    DType(const DType& dtype) = default;

    bool operator==(const DType& other) const;

    TypeId type_id() const;

    size_t item_size() const;

    std::string name() const;

    std::string type_str() const;

    cudf::type_id cudf_type_id() const;

    std::string triton_str() const;

    static DType from_cudf(cudf::type_id tid);

    static DType from_numpy(const std::string& numpy_str);

    static DType from_triton(const std::string& type_str);

    bool is_fully_supported() const;

    template <typename T>
    static DType create()
    {
        if constexpr (std::is_integral_v<T> && std::is_signed_v<T> && size_in_bits<T>() == 8)
        {
            return {TypeId::INT8};
        }
        else if constexpr (std::is_integral_v<T> && std::is_signed_v<T> && size_in_bits<T>() == 16)
        {
            return {TypeId::INT16};
        }
        else if constexpr (std::is_integral_v<T> && std::is_signed_v<T> && size_in_bits<T>() == 32)
        {
            return {TypeId::INT32};
        }
        else if constexpr (std::is_integral_v<T> && std::is_signed_v<T> && size_in_bits<T>() == 64)
        {
            return {TypeId::INT64};
        }
        else if constexpr (std::is_integral_v<T> && std::is_unsigned_v<T> && size_in_bits<T>() == 8)
        {
            return {TypeId::UINT8};
        }
        else if constexpr (std::is_integral_v<T> && std::is_unsigned_v<T> && size_in_bits<T>() == 16)
        {
            return {TypeId::UINT16};
        }
        else if constexpr (std::is_integral_v<T> && std::is_unsigned_v<T> && size_in_bits<T>() == 32)
        {
            return {TypeId::UINT32};
        }
        else if constexpr (std::is_integral_v<T> && std::is_unsigned_v<T> && size_in_bits<T>() == 64)
        {
            return {TypeId::UINT64};
        }
        else if constexpr (std::is_floating_point_v<T> && size_in_bits<T>() == 32)
        {
            return {TypeId::FLOAT32};
        }
        else if constexpr (std::is_floating_point_v<T> && size_in_bits<T>() == 64)
        {
            return {TypeId::FLOAT64};
        }
        else if constexpr (std::is_same_v<T, bool>)
        {
            return {TypeId::BOOL8};
        }
        else if constexpr (std::is_same_v<T, std::string>)
        {
            return {TypeId::STRING};
        }
        else
        {
            static_assert(!sizeof(T), "Type not implemented");
        }

        // To hide compiler warnings
        return {TypeId::EMPTY};
    }

  private:
    char byte_order_char() const;
    char type_char() const;

    TypeId m_type_id;
};  // end of group
}  // namespace morpheus