mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 00:21:07 +01:00
Summary: There is a module called `2to3` which you can target for future specifically to remove these, the directory of `caffe2` has the most redundant imports: ```2to3 -f future -w caffe2``` Pull Request resolved: https://github.com/pytorch/pytorch/pull/45033 Reviewed By: seemethere Differential Revision: D23808648 Pulled By: bugra fbshipit-source-id: 38971900f0fe43ab44a9168e57f2307580d36a38
78 lines
2.0 KiB
Python
78 lines
2.0 KiB
Python
|
|
|
|
|
|
|
|
from caffe2.python import core, workspace
|
|
from caffe2.python.test_util import TestCase
|
|
import numpy as np
|
|
import unittest
|
|
|
|
|
|
class DoOpTest(TestCase):
|
|
def test_operator(self):
|
|
def make_net():
|
|
subnet = core.Net('subnet')
|
|
subnet.Add(["X", "Y"], "Z")
|
|
|
|
net = core.Net("net")
|
|
net.CreateScope([], "W")
|
|
|
|
net.Do(
|
|
["outer_X", "outer_Y", "W"],
|
|
["outer_Z", "W"],
|
|
net=subnet.Proto(),
|
|
inner_blobs=["X", "Y", "Z"],
|
|
outer_blobs_idx=[0, 1, 2],
|
|
)
|
|
|
|
return net
|
|
|
|
net = make_net()
|
|
|
|
workspace.ResetWorkspace()
|
|
workspace.FeedBlob("outer_X", np.asarray([1, 2]))
|
|
workspace.FeedBlob("outer_Y", np.asarray([3, 4]))
|
|
|
|
workspace.RunNetOnce(net)
|
|
outer_Z_val = workspace.FetchBlob("outer_Z")
|
|
self.assertTrue(np.all(outer_Z_val == np.asarray([4, 6])))
|
|
|
|
def test_reuse_workspace(self):
|
|
def make_net():
|
|
param_init_subnet = core.Net('param_init_subnet')
|
|
param_init_subnet.ConstantFill([], "X", shape=[1], value=1)
|
|
param_init_subnet.ConstantFill([], "Y", shape=[1], value=2)
|
|
|
|
subnet = core.Net("subnet")
|
|
subnet.Add(["X", "Y"], "Z")
|
|
|
|
net = core.Net("net")
|
|
net.CreateScope([], "W")
|
|
net.Do(
|
|
"W", "W",
|
|
net=param_init_subnet.Proto(),
|
|
inner_blobs=[],
|
|
outer_blobs_idx=[],
|
|
)
|
|
|
|
net.Do(
|
|
"W", ["outer_Z", "W"],
|
|
net=subnet.Proto(),
|
|
inner_blobs=["Z"],
|
|
outer_blobs_idx=[0],
|
|
reuse_workspace=True,
|
|
)
|
|
|
|
return net
|
|
|
|
net = make_net()
|
|
|
|
workspace.ResetWorkspace()
|
|
workspace.RunNetOnce(net)
|
|
outer_Z_val = workspace.FetchBlob("outer_Z")
|
|
self.assertTrue(np.all(outer_Z_val == np.asarray([3])))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|