Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support free eager tensor #89

Merged
merged 1 commit into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions examples/oneflow2onnx/models/CPU/test_free_eager_tensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import oneflow as flow
import oneflow.nn as nn
from oneflow_onnx.oneflow2onnx.util import convert_to_onnx_and_check


class ConvModule(nn.Module):
def __init__(self):
super(ConvModule, self).__init__()
self.zero = flow.zeros(1, 2, 3, 3)
self.conv = nn.Conv2d(1, 2, 1, 1)

def forward(self, x):
return self.conv(x) + self.zero


m = ConvModule()


class YOLOGraph(flow.nn.Graph):
def __init__(self, m):
super().__init__()
self.model = m

def build(self, x):
return self.model(x)


yolo_graph = YOLOGraph(m)
yolo_graph._compile(flow.randn(1, 1, 3, 3))

convert_to_onnx_and_check(yolo_graph, onnx_model_path="/tmp", print_outlier=True)
32 changes: 32 additions & 0 deletions examples/oneflow2onnx/models/GPU/test_free_eager_tensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import oneflow as flow
import oneflow.nn as nn
from oneflow_onnx.oneflow2onnx.util import convert_to_onnx_and_check


class ConvModule(nn.Module):
def __init__(self):
super(ConvModule, self).__init__()
self.zero = flow.zeros(1, 2, 3, 3).to("cuda")
self.conv = nn.Conv2d(1, 2, 1, 1)

def forward(self, x):
return self.conv(x) + self.zero


m = ConvModule()
m.to("cuda")


class YOLOGraph(flow.nn.Graph):
def __init__(self, m):
super().__init__()
self.model = m

def build(self, x):
return self.model(x)


yolo_graph = YOLOGraph(m)
yolo_graph._compile(flow.randn(1, 1, 3, 3).to("cuda"))

convert_to_onnx_and_check(yolo_graph, onnx_model_path="/tmp", device="gpu")
16 changes: 14 additions & 2 deletions oneflow_onnx/onnx_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ def UpdateNodeShapeDtype(self, node, override=False):
if tensor_value.size * tensor_value.itemsize > 128 * 1024 * 1024: # 128 MB
tmp_dir = tempfile.TemporaryDirectory()
tmp_dirs.append(tmp_dir)
tensor = util.TensorProtoFromNumpy(tensor_value, name=inp.output_tensor_names[0], external_data=True, export_path=tmp_dir.name)
tensor = util.TensorProtoFromNumpy(tensor_value, name=inp.output_tensor_names[0], external_data=True, export_path=tmp_dir.name,)
else:
tensor = util.TensorProtoFromNumpy(tensor_value, name=inp.output_tensor_names[0])
initializers.append(tensor)
Expand Down Expand Up @@ -783,8 +783,20 @@ def get_saved_tensor(self, node):
# TODO(daquexian): node.output_tensor_names[0] is "node_name/output_name", so this pathjoin doesn't work
# on windows (where path separator is "\")
key = ".".join(node.output_tensor_names[0].split(".")[1:])
# For Free Eager Tensor
if key == "":
key = node.output_tensor_names[0]
if len(list(self._param_dict.keys())) > 1:
tensor_value = self._param_dict[key[: key.rfind("out") - 1]].numpy().reshape(self.get_shape(tensor_name)).astype(dtype=util.Onnx2NumpyDtype(self.get_dtype(tensor_name)))
try:
tensor_value = self._param_dict[key[: key.rfind("out") - 1]].numpy().reshape(self.get_shape(tensor_name)).astype(dtype=util.Onnx2NumpyDtype(self.get_dtype(tensor_name)))
except:
# For Eager Free Tensor
tensor_value = (
self._param_dict[list(self._param_dict.keys())[0]][key[: key.rfind("out") - 1]]
Copy link
Contributor

@Flowingsun007 Flowingsun007 Sep 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里(捕获free eager tensor)是什么原理?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

根据free eager tensor的name(key)来获取tensor(value)。例子:model-FreeEagerTensor-3/out

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

.numpy()
.reshape(self.get_shape(tensor_name))
.astype(dtype=util.Onnx2NumpyDtype(self.get_dtype(tensor_name)))
)
else:
try:
tensor_value = (
Expand Down