MCPcopy Index your code
hub / github.com/pytorch/pytorch / testTensorAccess

Method testTensorAccess

caffe2/python/workspace_test.py:108–152  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

106 self.assertEqual(workspace.HasBlob("testblob"), False)
107
108 def testTensorAccess(self):
109 ws = workspace.C.Workspace()
110
111 """ test in-place modification """
112 ws.create_blob("tensor").feed(np.array([1.1, 1.2, 1.3]))
113 tensor = ws.blobs["tensor"].tensor()
114 tensor.data[0] = 3.3
115 val = np.array([3.3, 1.2, 1.3])
116 np.testing.assert_array_equal(tensor.data, val)
117 np.testing.assert_array_equal(ws.blobs["tensor"].fetch(), val)
118
119 """ test in-place initialization """
120 tensor.init([2, 3], core.DataType.INT32)
121 for x in range(2):
122 for y in range(3):
123 tensor.data[x, y] = 0
124 tensor.data[1, 1] = 100
125 val = np.zeros([2, 3], dtype=np.int32)
126 val[1, 1] = 100
127 np.testing.assert_array_equal(tensor.data, val)
128 np.testing.assert_array_equal(ws.blobs["tensor"].fetch(), val)
129
130 """ strings cannot be initialized from python """
131 with self.assertRaises(RuntimeError):
132 tensor.init([3, 4], core.DataType.STRING)
133
134 """ feed (copy) data into tensor """
135 val = np.array([[b"abc", b"def"], [b"ghi", b"jkl"]], dtype=object)
136 tensor.feed(val)
137 self.assertEqual(tensor.data[0, 0], b"abc")
138 np.testing.assert_array_equal(ws.blobs["tensor"].fetch(), val)
139
140 val = np.array([1.1, 10.2])
141 tensor.feed(val)
142 val[0] = 5.2
143 self.assertEqual(tensor.data[0], 1.1)
144
145 """ fetch (copy) data from tensor """
146 val = np.array([1.1, 1.2])
147 tensor.feed(val)
148 val2 = tensor.fetch()
149 tensor.data[0] = 5.2
150 val3 = tensor.fetch()
151 np.testing.assert_array_equal(val, val2)
152 self.assertEqual(val3[0], 5.2)
153
154 def testFetchFeedBlob(self):
155 self.assertEqual(

Callers

nothing calls this directly

Calls 7

assertRaisesMethod · 0.80
rangeFunction · 0.50
tensorMethod · 0.45
fetchMethod · 0.45
initMethod · 0.45
zerosMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected