Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:InternLM Lmdeploy DataType

From Leeroopedia


Knowledge Sources
Domains Type_System, Core_Infrastructure
Last Updated 2026-02-07 15:00 GMT

Overview

Defines the TurboMind data type enumeration and compile-time type traits for mapping between C++ types and runtime data type identifiers.

Description

The DataType enum encodes numeric types using a scheme based on sign bit, exponent bits, and mantissa bits via encode_data_type(sign, exponent, mantissa). It covers standard integer types (int8 through int64, uint8 through uint64), floating-point types (float16, float32, float64, bfloat16), low-bit quantization types (uint2, uint4, uint6, fp4_e2m1, fp8_e4m3, fp8_e5m2), booleans, and pointers. The header provides bidirectional compile-time mapping between C++ types and DataType values through to_data_type<T> / data_type_v<T> and from_data_type<D> / data_type_t<D>. Utility functions byte_size() and numel() compute byte sizes and element counts at compile time. The file also provides preprocessor macros (TM_DISPATCH_DTYPES, TM_DISPATCH_PRIMARY_DTYPES) for generating type-dispatch switch statements.

Usage

Used throughout the TurboMind engine whenever runtime data type information is needed: allocating memory, computing buffer sizes, dispatching type-specific kernels, and serializing/deserializing tensors.

Code Reference

Source Location

Signature

namespace turbomind {

enum class DataType : int {
    kNull, kBool, kUint8, kUint16, kUint32, kUint64,
    kInt8, kInt16, kInt32, kInt64,
    kFloat16, kFloat32, kFloat64, kBfloat16,
    kFloat4_e2m1, kFloat8_e4m3, kFloat8_e5m2,
    kUint2, kUint4, kUint6, kPointer,
    // ... aliases: kUint, kInt, kFloat, kHalf, kDouble, kE4m3, kE5m2
};

template<class T>
inline constexpr auto data_type_v = to_data_type<std::remove_cv_t<T>>::value;

template<DataType D>
using data_type_t = typename from_data_type<D>::type;

constexpr std::ptrdiff_t byte_size(DataType type, std::ptrdiff_t size = 1);
constexpr std::ptrdiff_t numel(DataType type, std::ptrdiff_t size = 1);
constexpr const char* to_string(DataType type);

}  // namespace turbomind

Import

#include "src/turbomind/core/data_type.h"

I/O Contract

Inputs

Name Type Required Description
type DataType Yes The runtime data type identifier
size std::ptrdiff_t No Number of elements (default 1) for byte_size/numel calculations

Outputs

Name Type Description
byte_size std::ptrdiff_t Number of bytes required for the given count of elements
numel std::ptrdiff_t Number of elements that fit in the given byte count
to_string const char* Short string representation of the data type (e.g. "f16", "i32")

Usage Examples

#include "src/turbomind/core/data_type.h"

using namespace turbomind;

// Compile-time type mapping
constexpr auto dt = data_type_v<float>;  // kFloat32

// Compute byte size for 1024 float16 elements
auto bytes = byte_size(kFloat16, 1024);  // 2048

// Type dispatch macro
TM_DISPATCH_PRIMARY_DTYPES(dtype, [&](auto t) {
    using T = decltype(t);
    // T is half_t, bfloat16_t, or float depending on dtype
});

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment