This file contains hidden or 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
import zipfile | |
bytecode_pkl = None | |
with zipfile.ZipFile('AddTensorsModelOptimized.ptl', 'r') as myzip: | |
bytecode_pkl = myzip.open("AddTensorsModelOptimized/bytecode.pkl") | |
from torch.utils import show_pickle | |
show_pickle.DumpUnpickler.dump(bytecode_pkl, None) | |
(5, | |
('__torch__.___torch_mangle_12.AddtensorsModel.forward', |
This file contains hidden or 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
graph(%self : __torch__.___torch_mangle_47.AddtensorsModel, | |
%x.1 : Tensor, | |
%y.1 : Tensor): | |
%3 : int = prim::Constant[value=1]() | |
%self.t1 : Tensor = prim::Constant[value= 0 5 10 [ CPUFloatType{3} ]]() | |
%5 : Tensor = aten::add(%self.t1, %x.1, %3) # <ipython-input-98-b27af2abc3c5>:8:8 | |
%z.5 : Tensor = aten::add(%5, %y.1, %3) # <ipython-input-98-b27af2abc3c5>:8:8 | |
return (%z.5) |
This file contains hidden or 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
graph(%self : __torch__.___torch_mangle_41.AddtensorsModel, | |
%x.1 : Tensor, | |
%y.1 : Tensor): | |
%6 : int = prim::Constant[value=1]() | |
%z.1 : Tensor = prim::GetAttr[name="t1"](%self) | |
%7 : Tensor = aten::add(%z.1, %x.1, %6) # <ipython-input-98-b27af2abc3c5>:8:8 | |
%z.5 : Tensor = aten::add(%7, %y.1, %6) # <ipython-input-98-b27af2abc3c5>:8:8 | |
return (%z.5) |
This file contains hidden or 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
graph(%self : __torch__.___torch_mangle_41.AddtensorsModel, | |
%x.1 : Tensor, | |
%y.1 : Tensor): | |
%5 : Tensor = prim::CallMethod[name="helper"](%self, %x.1, %y.1) # <ipython-input-98-b27af2abc3c5>:12:11 | |
return (%5) |
This file contains hidden or 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
#include <iostream> | |
#include <vector> | |
#include <torch/csrc/jit/mobile/import.h> | |
#include <torch/csrc/jit/mobile/module.h> | |
int main() { | |
auto model = torch::jit::_load_for_mobile("AddTensorsModelOptimized.ptl")); | |
std::vector<at::IValue> inputs, ret; | |
inputs.push_back(at::zeros({3})); |
This file contains hidden or 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
optimized_model._save_for_lite_interpreter("AddTensorsModelOptimized.ptl") |
This file contains hidden or 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
from torch.utils.mobile_optimizer import optimize_for_mobile | |
optimized_model = optimize_for_mobile(scripted) |
This file contains hidden or 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
scripted = torch.jit.script(m) |
This file contains hidden or 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
m = AddtensorsModel() | |
res = m(torch.Tensor([1, 2, 3]), torch.Tensor([3, 4, 5])) | |
print(res) |
This file contains hidden or 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
import torch | |
class AddTensorsModel(torch.nn.Module): | |
def __init__(self): | |
super().__init__(); | |
self.t1 = torch.Tensor([0, 5, 10]) | |
def helper(self, x, y): | |
z = self.t1 | |
z = z + x + y |