mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
Add Module::replace_module to C++ api (#22546)
Summary: This adds a replace_module method to the C++ api. This is needed to be able to replace modules. The primary use case I am aware of is to enable finetuning of models. Given that finetuning is fairly popular these days, I think it would be good to facilitate this in the C++ api as well. This has been reported by Jean-Christophe Lombardo on the [forums](https://discuss.pytorch.org/t/finetuning-a-model-on-multiple-gpu-in-c/49195). Pull Request resolved: https://github.com/pytorch/pytorch/pull/22546 Differential Revision: D16440289 Pulled By: yf225 fbshipit-source-id: c136f914b8fc5c0f1975d877ea817fda5c851cda
This commit is contained in:
committed by
Facebook Github Bot
parent
f112c522af
commit
0dabaad819
@@ -99,6 +99,26 @@ TEST_F(ModuleTest, RegisterModuleThrowsForDuplicateModuleName) {
|
||||
"Submodule 'linear' already defined");
|
||||
}
|
||||
|
||||
TEST_F(ModuleTest, ReplaceModuleThrowsForUnknownModuleName) {
|
||||
torch::nn::Module model;
|
||||
ASSERT_THROWS_WITH(
|
||||
model.replace_module("linear", torch::nn::Linear(3, 4)),
|
||||
"Submodule 'linear' is not defined");
|
||||
}
|
||||
|
||||
TEST_F(ModuleTest, ReplaceModule) {
|
||||
struct TestModel : public torch::nn::Module {
|
||||
torch::nn::Linear l1{nullptr};
|
||||
TestModel() {
|
||||
l1 = register_module("l1", torch::nn::Linear(3, 4));
|
||||
}
|
||||
};
|
||||
auto model = std::make_shared<TestModel>();
|
||||
model->l1 = model->replace_module("l1", torch::nn::Linear(5, 6));
|
||||
ASSERT_EQ(model->named_parameters()["l1.weight"].size(0), 6);
|
||||
ASSERT_EQ(model->l1.get(), model->named_modules()["l1"]->as<Linear>());
|
||||
}
|
||||
|
||||
TEST_F(ModuleTest, RegisterParameterThrowsForEmptyOrDottedName) {
|
||||
struct TestModel : public torch::nn::Module {
|
||||
using torch::nn::Module::register_parameter;
|
||||
|
||||
Reference in New Issue
Block a user