MCPcopy Create free account
hub / github.com/pytorch/pytorch / testPartialClone

Method testPartialClone

caffe2/python/core_test.py:126–199  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

124
125class TestCloneNet(test_util.TestCase):
126 def testPartialClone(self):
127 params = core.Net('params')
128 p1 = params.ConstantFill([], ['p1'])
129 workspace.CreateNet(params)
130 workspace.RunNetOnce(params)
131
132 n = core.Net('original')
133 a1 = n.AddExternalInput('a1')
134 a2 = n.AddExternalInput('a2')
135 b1, b2 = n.Concat([a1, a2], ['b1', 'b2'], axis=0)
136 c1 = n.Sum([b1, p1], ['c1'])
137 c2 = n.Sum([b2], ['c2'])
138 d = n.Sum([c1, c2], ['d'])
139
140 # test that gradient ops are ignored when partial-cloning
141 n.AddGradientOperators([d])
142
143 # test some in-place ops
144 k = n.Sum([p1], ['k'])
145 e = n.Sum([d], ['e'])
146 e = n.Sum([e, k], [e])
147 e = n.Sum([e], [e])
148 f = n.Sum(e, ['f'])
149
150 def net_assert(net, num_ops, inputs, outputs, internals):
151 self.assertEqual(len(net.Proto().op), num_ops)
152 self.assertEqual(set(net.Proto().external_input), inputs)
153 self.assertEqual(set(net.Proto().external_output), outputs)
154 all_blobs = set(net.Proto().external_input)
155 all_blobs |= set(net.Proto().external_output)
156 for op in net.Proto().op:
157 all_blobs |= set(op.input) | set(op.output)
158 self.assertEqual(all_blobs, inputs | outputs | internals)
159 # create net to make sure its valid
160 for input in inputs:
161 workspace.FeedBlob(input, np.array([]))
162 workspace.CreateNet(net)
163
164 n2, (d22, ) = n.ClonePartial('f1', {a1: 'a11', a2: 'a22'}, [d])
165 net_assert(
166 n2, 4, {'p1', 'a11', 'a22'}, {'f1/d'},
167 {'f1/b1', 'f1/b2', 'f1/c1', 'f1/c2', 'p1'})
168 self.assertTrue(isinstance(d22, core.BlobReference))
169 self.assertEqual(d22.Net(), n2)
170 self.assertEqual(str(d22), 'f1/d')
171
172 n3, (d22, ) = n.ClonePartial('f2', [b1, b2], [d])
173 net_assert(
174 n3, 3, {'p1', 'b1', 'b2'}, {'f2/d'}, {'f2/c1', 'f2/c2', 'p1'})
175 self.assertEqual(str(d22), 'f2/d')
176
177 n4, (c22, ) = n.ClonePartial('f3', [b1], [c1])
178 net_assert(n4, 1, {'p1', 'b1'}, {'f3/c1'}, {'p1'})
179 self.assertEqual(str(c22), 'f3/c1')
180
181 n5, (c11, c22) = n.ClonePartial('f4', [b1, b2], [c1, c2])
182 net_assert(n5, 2, {'p1', 'b1', 'b2'}, {'f4/c1', 'f4/c2'}, {'p1'})
183 self.assertEqual(str(c11), 'f4/c1')

Callers

nothing calls this directly

Calls 10

AddExternalInputMethod · 0.95
AddGradientOperatorsMethod · 0.95
ClonePartialMethod · 0.95
_CheckLookupTablesMethod · 0.95
isinstanceFunction · 0.85
NetMethod · 0.80
ConcatMethod · 0.80
SumMethod · 0.80
assertRaisesMethod · 0.80
assertEqualMethod · 0.45

Tested by

no test coverage detected