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
#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
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
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_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
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
def fill_inorder_recursive_bst_array(bst, idx, val): | |
"""Recursively fill an array "bst" with values so that values | |
increment by 1 every time. "idx" is the index in "bst" that | |
the method should look up to write out the next value in sequence. | |
The first invocation is expected to pass idx == 0, with enough | |
space in the BST array (bst) to hold a complete and balanced | |
binary tree. | |
This method returns the next value in sequence that need to be |
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 | |
import zipfile | |
class AddTensorsModel(torch.nn.Module): | |
def __init__(self): | |
super().__init__(); | |
def forward(self, x, y): | |
return x + y |
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
zf = zipfile.ZipFile("AddTensorsModel.ptl") | |
zf.infolist() | |
[<ZipInfo filename='AddTensorsModel/extra/model_info.txt' file_size=70>, | |
<ZipInfo filename='AddTensorsModel/data.pkl' file_size=86>, | |
<ZipInfo filename='AddTensorsModel/code/__torch__.py' compress_type=deflate file_size=247 compress_size=166>, | |
<ZipInfo filename='AddTensorsModel/code/__torch__.py.debug_pkl' file_size=145>, | |
<ZipInfo filename='AddTensorsModel/constants.pkl' file_size=4>, | |
<ZipInfo filename='AddTensorsModel/bytecode.pkl' file_size=452>, |
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
print(zf.read("AddTensorsModel/extra/model_info.txt")) | |
b"This model's forward() method accepts 2 tensors, and returns their sum" |