Implementation:InternLM Lmdeploy Serdes
| Knowledge Sources | |
|---|---|
| Domains | Serialization, Core_Infrastructure |
| Last Updated | 2026-02-07 15:00 GMT |
Overview
Provides a CRTP-based binary serialization/deserialization framework with archives for size calculation, binary output, and binary input, plus built-in support for vectors, strings, shared pointers, arrays, and tuples.
Description
The serialization framework uses CRTP (Curiously Recurring Template Pattern) to define OutputArchive<Derived> and InputArchive<Derived> base classes. The operator& method dispatches to user-defined save/load functions (found via ADL), serdes functions (bidirectional), or falls through to the derived class's write/read for trivially-copyable types. Detection traits (has_save_v, has_load_v, has_serdes_v) use SFINAE to select the correct overload.
Three concrete archives are provided:
- BinarySizeArchive -- computes total serialized size without writing data
- BinaryOutputArchive -- writes serialized bytes into an
ArrayWrapper<std::byte> - BinaryInputArchive -- reads serialized bytes from an
ArrayWrapper<std::byte>
The ArrayWrapper<T> class wraps a raw pointer and count for bulk binary I/O of trivially-copyable arrays. Built-in specializations handle std::vector<T>, std::string, std::shared_ptr<T>, std::array<T, N>, and std::tuple<Ts...>.
Usage
Used for inter-process communication serialization in the host communication layer and for checkpointing/transferring tensors, layouts, and buffers between processes that do not share memory.
Code Reference
Source Location
- Repository: InternLM_Lmdeploy
- File: src/turbomind/core/serdes.h
- Lines: 1-278
Signature
namespace turbomind::core {
template<class Derived>
struct OutputArchive {
static constexpr bool is_loading = false;
template<class T> void operator&(T&& x);
};
template<class Derived>
struct InputArchive {
static constexpr bool is_loading = true;
template<class T> void operator&(T&& x);
};
struct BinarySizeArchive : OutputArchive<BinarySizeArchive> {
size_t size();
template<class T> void write(const T& x);
template<class T> void write(const ArrayWrapper<T>& arr);
};
struct BinaryOutputArchive : OutputArchive<BinaryOutputArchive> {
BinaryOutputArchive(ArrayWrapper<std::byte> external);
template<class T> void write(const T& x);
template<class T> void write(const ArrayWrapper<T>& arr);
};
struct BinaryInputArchive : InputArchive<BinaryInputArchive> {
BinaryInputArchive(ArrayWrapper<std::byte> external);
template<class T> void read(T& x);
template<class T> void read(ArrayWrapper<T>&& arr);
};
template<typename T>
class ArrayWrapper {
public:
ArrayWrapper(T* t, std::size_t size);
T* data() const;
std::size_t count() const;
};
// Built-in specializations for std::vector, std::string,
// std::shared_ptr, std::array, std::tuple
} // namespace turbomind::core
Import
#include "src/turbomind/core/serdes.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| external | ArrayWrapper<std::byte> | BinaryOutputArchive/BinaryInputArchive | Byte buffer for reading/writing serialized data |
| x | T&& | operator& | Object to serialize or deserialize |
Outputs
| Name | Type | Description |
|---|---|---|
| size() | size_t | Total serialized byte count (BinarySizeArchive) |
| (deserialized objects) | T& | Objects populated from the byte buffer |
Usage Examples
#include "src/turbomind/core/serdes.h"
using namespace turbomind::core;
// Compute serialized size
BinarySizeArchive sa;
sa & my_vector;
sa & my_string;
size_t total = sa.size();
// Serialize to a byte buffer
std::vector<std::byte> buf(total);
BinaryOutputArchive oa(ArrayWrapper(buf.data(), buf.size()));
oa & my_vector;
oa & my_string;
// Deserialize from the byte buffer
std::vector<int> deserialized_vec;
std::string deserialized_str;
BinaryInputArchive ia(ArrayWrapper(buf.data(), buf.size()));
ia & deserialized_vec;
ia & deserialized_str;