1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
# Owner(s): ["module: functionalization"]
import torch
from torch.testing._internal.common_utils import TestCase, run_tests
from torch.fx.passes.reinplace import reinplace
from torch.fx.experimental.proxy_tensor import make_fx
try:
from functorch.experimental import functionalize
HAS_FUNCTIONALIZATION = True
except Exception as e:
HAS_FUNCTIONALIZATION = False
class TestReinplacePass(TestCase):
def test_reinplace_basic(self):
# Basic test: the out-of-place add() call should be converted
# into add_()
def f(x):
a = x.clone()
b = a.add(1)
return b
inpt = torch.ones(2)
f2 = reinplace(make_fx(f)(inpt), inpt)
expected_out = f(inpt)
actual_out = f2(inpt)
self.assertEqual(actual_out, expected_out)
self.assertExpectedInline(f2.code, """\
def forward(self, x_1):
clone = torch.ops.aten.clone.default(x_1); x_1 = None
add = torch.ops.aten.add_.Tensor(clone, 1)
return clone
""")
def test_reinplace_with_view(self):
def f(x):
a = x.clone()
a_view = a.view(-1)
# We shouldn't re-inplace the first add(), because an alias of a is re-used later in the program
b = a.add(1)
# Second add() is fine to re-inplace
c = a_view.add(1)
return c
inpt = torch.ones(2)
f2 = reinplace(make_fx(f)(inpt), inpt)
expected_out = f(inpt)
actual_out = f2(inpt)
self.assertEqual(actual_out, expected_out)
self.assertExpectedInline(f2.code, """\
def forward(self, x_1):
clone = torch.ops.aten.clone.default(x_1); x_1 = None
view = torch.ops.aten.view.default(clone, [-1])
add = torch.ops.aten.add.Tensor(clone, 1); clone = None
add_1 = torch.ops.aten.add_.Tensor(view, 1)
return view
""")
def test_reinplace_different_metadata(self):
def f(a_):
a = a_.clone()
b = a + 1
# Naively, we shouldn't try to inplace the .ge() call,
# because that would require resizing "b" (from a float to a bool tensor).
c = torch.ge(b, a)
return c
inpt = torch.ones(4)
f2 = reinplace(make_fx(f)(inpt), inpt)
expected_out = f(inpt)
actual_out = f2(inpt)
self.assertEqual(actual_out, expected_out)
# The .ge() should not be reinplaced.
self.assertExpectedInline(f2.code, """\
def forward(self, a__1):
clone = torch.ops.aten.clone.default(a__1); a__1 = None
add = torch.ops.aten.add.Tensor(clone, 1)
ge = torch.ops.aten.ge.Tensor(add, clone); add = clone = None
return ge
""")
def test_reinplace_overlapping_memory(self):
def f(a_):
a = a_.clone()
b = a.expand(4, 4)
# Can't reinplace because b has overlapping memory.
c = b.add(1)
return c
inpt = torch.ones(1)
f2 = reinplace(make_fx(f)(inpt), inpt)
expected_out = f(inpt)
actual_out = f2(inpt)
self.assertEqual(actual_out, expected_out)
self.assertExpectedInline(f2.code, """\
def forward(self, a__1):
clone = torch.ops.aten.clone.default(a__1); a__1 = None
expand = torch.ops.aten.expand.default(clone, [4, 4]); clone = None
add = torch.ops.aten.add.Tensor(expand, 1); expand = None
return add
""")
# This test won't actually run in CI, because it requires functionalize() from functorch.
# I'm planning on testing more comprehensively with torchbench models,
# but we can make this testing better once functorch moves into pytorch/pytorch.
def test_reinplace_scatter_op(self):
def f(a_):
# for now, don't test mutations to inputs
a = a_.clone()
e = a.view(-1)
b = a.view(-1)
c = b[0]
d = c.view(-1)
d.add_(1)
return a + e
if not HAS_FUNCTIONALIZATION:
return
inpt = torch.ones(4)
f2 = reinplace(make_fx(functionalize(f))(inpt), inpt)
expected_out = f(inpt)
actual_out = f2(inpt)
self.assertEqual(actual_out, expected_out)
# NOTE: one slight pessimization here is the fact that
# there are a bunch of redundant views in the graph.
# Technically, half of these views are duplicates that we could de-dup.
# This shouldn't really hurt performance though, since creating an extra view
# is effectively just moving some metadata around (and allocating a new TensorImpl).
# We can/should update the pass in the future to clean this up.
self.assertExpectedInline(f2.code, """\
def forward(self, a__1):
clone = torch.ops.aten.clone.default(a__1); a__1 = None
view = torch.ops.aten.view.default(clone, [-1])
view_1 = torch.ops.aten.view.default(clone, [-1])
select = torch.ops.aten.select.int(view_1, 0, 0); view_1 = None
view_2 = torch.ops.aten.view.default(select, [-1]); select = None
add = torch.ops.aten.add_.Tensor(view_2, 1)
view_3 = torch.ops.aten.view.default(clone, [-1]); clone = None
select_1 = torch.ops.aten.select.int(view_3, 0, 0)
view_4 = torch.ops.aten.view.default(view_2, []); view_2 = None
view_5 = torch.ops.aten.view.default(view_3, [4]); view_3 = None
view_6 = torch.ops.aten.view.default(view_5, [-1])
add_1 = torch.ops.aten.add_.Tensor(view_5, view_6); view_6 = None
return view_5
""")
def test_reinplace_scatter_twice(self):
def f(a_):
# for now, don't test mutations to inputs
a = a_.clone()
b = a[:, 1]
c = b[1]
c.add_(1)
return a
if not HAS_FUNCTIONALIZATION:
return
inpt = torch.ones(4, 4)
f2 = reinplace(make_fx(functionalize(f))(inpt), inpt)
expected_out = f(inpt)
actual_out = f2(inpt)
self.assertEqual(actual_out, expected_out)
self.assertExpectedInline(f2.code, """\
def forward(self, a__1):
clone = torch.ops.aten.clone.default(a__1); a__1 = None
slice_1 = torch.ops.aten.slice.Tensor(clone, 0, 0, 9223372036854775807)
select = torch.ops.aten.select.int(slice_1, 1, 1); slice_1 = None
select_1 = torch.ops.aten.select.int(select, 0, 1); select = None
add = torch.ops.aten.add_.Tensor(select_1, 1); select_1 = None
slice_2 = torch.ops.aten.slice.Tensor(clone, 0, 0, 9223372036854775807)
select_2 = torch.ops.aten.select.int(slice_2, 1, 1); slice_2 = None
return clone
""")
def test_reinplace_scatter_twice_with_different_view_op_valid(self):
def f(a_):
a = a_.clone()
b = a[:, 1]
c = b[1]
c_updated = c.add(1)
good_mirror_of_b = a.as_strided((4,), (4,), 1)
# good_mirror_of_b points to the same region of memory as b.
# and this scatter op below tries to scatter c_updated into the same region
# that c currently takes up.
# reinplacing logic checks this by confirming that:
# c_updated
# good_mirror_of_b.select(0, 1)
# have the same size/stride/storage_offset.
b_updated = torch.select_scatter(good_mirror_of_b, c_updated, 0, 1)
return b_updated
inpt = torch.ones(4, 4)
f2 = reinplace(make_fx(f)(inpt), inpt)
expected_out = f(inpt)
actual_out = f2(inpt)
self.assertEqual(actual_out, expected_out)
self.assertExpectedInline(f2.code, """\
def forward(self, a__1):
clone = torch.ops.aten.clone.default(a__1); a__1 = None
slice_1 = torch.ops.aten.slice.Tensor(clone, 0, 0, 9223372036854775807)
select = torch.ops.aten.select.int(slice_1, 1, 1); slice_1 = None
select_1 = torch.ops.aten.select.int(select, 0, 1); select = None
add = torch.ops.aten.add_.Tensor(select_1, 1); select_1 = None
as_strided = torch.ops.aten.as_strided.default(clone, [4], [4], 1); clone = None
return as_strided
""")
# Test example where we have a scatter op, where the base tensor
# has the same size/stride/storage offset (even though it is a different view),
# making it valid to re-inplace
def test_reinplace_scatter_twice_with_different_view_op_invalid(self):
def f(a_):
a = a_.clone()
b = a[:, 1]
c = b[1]
c_updated = c.add(1)
good_mirror_of_b = a.as_strided((4,), (4,), 1)
# The first arg to select_scatter is an equivalent view to b.
# However, the select_scatter call below tries to put c_updated
# into a different slice of "b" than what "c" currently occupies.
#
b_updated = torch.select_scatter(good_mirror_of_b, c_updated, 0, 0)
return b_updated
inpt = torch.ones(4, 4)
f2 = reinplace(make_fx(f)(inpt), inpt)
expected_out = f(inpt)
actual_out = f2(inpt)
self.assertEqual(actual_out, expected_out)
self.assertExpectedInline(f2.code, """\
def forward(self, a__1):
clone = torch.ops.aten.clone.default(a__1); a__1 = None
slice_1 = torch.ops.aten.slice.Tensor(clone, 0, 0, 9223372036854775807)
select = torch.ops.aten.select.int(slice_1, 1, 1); slice_1 = None
select_1 = torch.ops.aten.select.int(select, 0, 1); select = None
add = torch.ops.aten.add.Tensor(select_1, 1); select_1 = None
as_strided = torch.ops.aten.as_strided.default(clone, [4], [4], 1); clone = None
select_int = torch.ops.aten.select.int(as_strided, 0, 0)
copy__default = torch.ops.aten.copy_.default(select_int, add); select_int = add = None
return as_strided
""") # noqa: B950
def test_reinplace_scatter_twice_with_different_view_op_invalid2(self):
def f(a_):
a = a_.clone()
b = a[:, 1]
c = b[1]
c_updated = c.add(1)
bad_mirror_of_b = a.as_strided((4,), (4,), 0)
# The first arg to select_scatter points to a different than c's base.
# This makes it invalid to re-inplace.
b_updated = torch.select_scatter(bad_mirror_of_b, c_updated, 0, 1)
return b_updated
inpt = torch.ones(4, 4)
f2 = reinplace(make_fx(f)(inpt), inpt)
expected_out = f(inpt)
actual_out = f2(inpt)
# self.assertEqual(actual_out, expected_out)
self.assertExpectedInline(f2.code, """\
def forward(self, a__1):
clone = torch.ops.aten.clone.default(a__1); a__1 = None
slice_1 = torch.ops.aten.slice.Tensor(clone, 0, 0, 9223372036854775807)
select = torch.ops.aten.select.int(slice_1, 1, 1); slice_1 = None
select_1 = torch.ops.aten.select.int(select, 0, 1); select = None
add = torch.ops.aten.add.Tensor(select_1, 1); select_1 = None
as_strided = torch.ops.aten.as_strided.default(clone, [4], [4], 0); clone = None
select_int = torch.ops.aten.select.int(as_strided, 0, 1)
copy__default = torch.ops.aten.copy_.default(select_int, add); select_int = add = None
return as_strided
""") # noqa: B950
def test_out_node_updated(self):
def f():
x = torch.zeros(2, 2)
y = x.diagonal()
y_updated = y.add(1)
z = torch.diagonal_scatter(x, y_updated)
# reinplace needs to know to replace output [z] with [x]
return [z]
if not HAS_FUNCTIONALIZATION:
return
f2 = reinplace(make_fx(functionalize(f))())
expected_out = f()
actual_out = f2()
self.assertEqual(actual_out, expected_out)
self.assertExpectedInline(f2.code, """\
def forward(self):
zeros = torch.ops.aten.zeros.default([2, 2], device = device(type='cpu'), pin_memory = False)
diagonal = torch.ops.aten.diagonal.default(zeros)
add = torch.ops.aten.add_.Tensor(diagonal, 1); diagonal = None
return [zeros]
""")
def test_reinplace_index_mutation(self):
def f():
a = torch.zeros(4, 4, 4)
a[:, 2:] = torch.ones(4, 2, 4)
return a
if not HAS_FUNCTIONALIZATION:
return
f2 = reinplace(make_fx(functionalize(f))())
expected_out = f()
actual_out = f2()
self.assertEqual(actual_out, expected_out)
self.assertExpectedInline(f2.code, """\
def forward(self):
zeros = torch.ops.aten.zeros.default([4, 4, 4], device = device(type='cpu'), pin_memory = False)
ones = torch.ops.aten.ones.default([4, 2, 4], device = device(type='cpu'), pin_memory = False)
slice_1 = torch.ops.aten.slice.Tensor(zeros, 0, 0, 9223372036854775807)
slice_2 = torch.ops.aten.slice.Tensor(slice_1, 1, 2, 9223372036854775807); slice_1 = None
slice_3 = torch.ops.aten.slice.Tensor(zeros, 0, 0, 9223372036854775807)
slice_tensor = torch.ops.aten.slice.Tensor(slice_3, 1, 2, 9223372036854775807); slice_3 = None
copy__default = torch.ops.aten.copy_.default(slice_tensor, ones); slice_tensor = ones = None
return zeros
""")
if __name__ == '__main__':
run_tests()
|