Implementation:InternLM Lmdeploy Module
| Knowledge Sources | |
|---|---|
| Domains | Neural_Network, Core_Infrastructure |
| Last Updated | 2026-02-07 15:00 GMT |
Overview
Provides a PyTorch-style module base class for organizing neural network layers into a hierarchy with named sub-modules and registered parameters.
Description
The Module class serves as the base class for all neural network components in TurboMind. It maintains a list of named child modules (modules_) and named parameter tensors (params_), along with a parent_ pointer for automatic cleanup. register_module(name, module, index) adds a child module, optionally appending a numeric index to the name (for layers in a list). register_parameter(name, param) registers a Tensor reference as a named parameter. remove_module() and remove_parameter() handle deregistration. The destructor automatically removes the module from its parent. get_parameters() recursively collects all parameters in the module hierarchy using dot-separated naming (e.g., "encoder.layer.0.weight"), returning an unordered_map<string, Tensor*>.
The class is non-copyable and non-movable to ensure stable parent-child pointer relationships.
Usage
Used as the base class for all model layers (attention, MLP, normalization, etc.) in TurboMind. During weight loading, get_parameters() maps parameter names to tensor pointers for bulk initialization.
Code Reference
Source Location
- Repository: InternLM_Lmdeploy
- File (header): src/turbomind/core/module.h
- File (impl): src/turbomind/core/module.cc
- Lines: module.h 1-36, module.cc 1-78
Signature
namespace turbomind::core {
class Module {
public:
virtual ~Module();
Module();
Module(const Module&) = delete;
Module& operator=(const Module&) = delete;
Module(Module&&) noexcept = delete;
Module& operator=(Module&&) noexcept = delete;
void register_module(std::string name, Module& module,
std::optional<int> index = {});
void register_parameter(std::string name, Tensor& param);
void remove_module(Module& module);
void remove_parameter(Tensor& param);
std::unordered_map<std::string, Tensor*> get_parameters() const;
protected:
Module* parent_;
std::vector<std::pair<std::string, Module*>> modules_;
std::vector<std::pair<std::string, Tensor*>> params_;
};
} // namespace turbomind::core
Import
#include "src/turbomind/core/module.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| name | std::string | Yes | Name for the sub-module or parameter |
| module | Module& | register_module | Reference to the child module |
| param | Tensor& | register_parameter | Reference to the parameter tensor |
| index | std::optional<int> | No | Optional numeric index appended to the name |
Outputs
| Name | Type | Description |
|---|---|---|
| get_parameters() | unordered_map<string, Tensor*> | All parameters in the hierarchy with dot-separated names |
Usage Examples
#include "src/turbomind/core/module.h"
using namespace turbomind::core;
class LinearLayer : public Module {
public:
Tensor weight;
Tensor bias;
LinearLayer() {
register_parameter("weight", weight);
register_parameter("bias", bias);
}
};
class Model : public Module {
public:
LinearLayer layer0;
LinearLayer layer1;
Model() {
register_module("layer", layer0, 0); // "layer.0"
register_module("layer", layer1, 1); // "layer.1"
}
};
Model model;
auto params = model.get_parameters();
// params contains: "layer.0.weight", "layer.0.bias",
// "layer.1.weight", "layer.1.bias"