forked from snuspl/nimble
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add train() / eval() / is_training() to C++ ScriptModule API (#16044)
Summary: This PR aims to fix https://discuss.pytorch.org/t/how-to-change-a-loaded-model-to-evaluation-mode-in-c/32330, by adding `train()` / `eval()` / `is_training()` to C++ ScriptModule API. Pull Request resolved: pytorch/pytorch#16044 Differential Revision: D13857724 Pulled By: yf225 fbshipit-source-id: 16d3969fb5840ff7e66c7f72e800e6c75db8d2ff
- Loading branch information
1 parent
6d373c0
commit a40e8ce
Showing
11 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import sys | ||
import os | ||
import torch | ||
|
||
testEvalModeForLoadedModule_module_path = 'dropout_model.pt' | ||
|
||
|
||
def testEvalModeForLoadedModule_setup(): | ||
class Model(torch.jit.ScriptModule): | ||
def __init__(self): | ||
super(Model, self).__init__() | ||
self.dropout = torch.nn.Dropout(0.1) | ||
|
||
def forward(self, x): | ||
x = self.dropout(x) | ||
return x | ||
|
||
model = Model() | ||
model = model.train() | ||
model.save(testEvalModeForLoadedModule_module_path) | ||
|
||
|
||
def testEvalModeForLoadedModule_shutdown(): | ||
if os.path.exists(testEvalModeForLoadedModule_module_path): | ||
os.remove(testEvalModeForLoadedModule_module_path) | ||
|
||
|
||
def setup(): | ||
testEvalModeForLoadedModule_setup() | ||
|
||
|
||
def shutdown(): | ||
testEvalModeForLoadedModule_shutdown() | ||
|
||
|
||
if __name__ == "__main__": | ||
command = sys.argv[1] | ||
if command == "setup": | ||
setup() | ||
elif command == "shutdown": | ||
shutdown() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters