Skip to content

Commit 449e339

Browse files
authored
Revert "[Inductor] Improve materialization heuristic for a chain of computaions (#176994)" (#182184)
Revert "[Inductor] Improve materialization heuristic for a chain of computaio…" This reverts commit d91fbdc.
1 parent dea39b1 commit 449e339

10 files changed

Lines changed: 48 additions & 199 deletions

‎test/inductor/pallas_expected_failures/CpuTests.test_nll_loss_backward_1d_input_cpu‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎test/inductor/test_compiled_optimizers.py‎

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -201,41 +201,28 @@ class KernelCounts(NamedTuple):
201201
KERNEL_COUNT_OVERRIDES = {
202202
"test_rmsprop_foreach_weight_decay_cpu": lambda x: assert_expected_inline(x, """12""") ,
203203
"test_nadam_foreach_weight_decay_momentum_decay_cpu": lambda x: assert_expected_inline(x, """20"""),
204-
"test_adam_tensor_lr_amsgrad_capturable_foreach_cuda": lambda x: assert_expected_inline(x, """3"""),
205-
"test_adam_capturable_foreach_cuda": lambda x: assert_expected_inline(x, """2"""),
206-
"test_adam_foreach_cuda": lambda x: assert_expected_inline(x, """2"""),
207-
"test_adam_weight_decay_amsgrad_capturable_foreach_cuda": lambda x: assert_expected_inline(x, """2"""),
208-
"test_adam_weight_decay_amsgrad_foreach_cuda": lambda x: assert_expected_inline(x, """2"""),
209-
"test_adam_weight_decay_foreach_cuda": lambda x: assert_expected_inline(x, """2"""),
210-
"test_adam_weight_decay_maximize_foreach_cuda": lambda x: assert_expected_inline(x, """2"""),
211204
"test_adamw_amsgrad_capturable_foreach_cuda": lambda x: assert_expected_inline(x, """3"""),
212-
"test_adamw_tensor_lr_amsgrad_capturable_foreach_cuda": lambda x: assert_expected_inline(x, """3"""),
213-
"test_adamw_tensor_lr_tensor_betas_amsgrad_capturable_foreach_cuda": lambda x: assert_expected_inline(x, """3"""),
214-
"test_adamw_tensor_lr_tensor_betas_capturable_foreach_cuda": lambda x: assert_expected_inline(x, """3"""),
215205
"test_adamw_amsgrad_capturable_foreach_xpu": lambda x: assert_expected_inline(x, """3"""),
216206
"test_adamw_amsgrad_capturable_cuda": lambda x: assert_expected_inline(x, """6"""),
217207
"test_adamw_amsgrad_capturable_xpu": lambda x: assert_expected_inline(x, """6"""),
218-
"test_adamw_tensor_lr_tensor_betas_amsgrad_capturable_cuda": lambda x: assert_expected_inline(x, """3"""),
208+
"test_adamw_tensor_lr_tensor_betas_amsgrad_capturable_cuda": lambda x: assert_expected_inline(x, """6"""),
219209
"test_adamw_tensor_lr_tensor_betas_amsgrad_capturable_xpu": lambda x: assert_expected_inline(x, """6"""),
220-
"test_adamw_tensor_lr_tensor_betas_capturable_cuda": lambda x: assert_expected_inline(x, """3"""),
210+
"test_adamw_tensor_lr_tensor_betas_capturable_cuda": lambda x: assert_expected_inline(x, """6"""),
221211
"test_adamw_tensor_lr_tensor_betas_capturable_xpu": lambda x: assert_expected_inline(x, """6"""),
222-
"test_adamw_tensor_lr_amsgrad_capturable_cuda": lambda x: assert_expected_inline(x, """3"""),
212+
"test_adamw_tensor_lr_amsgrad_capturable_cuda": lambda x: assert_expected_inline(x, """6"""),
223213
"test_adamw_tensor_lr_amsgrad_capturable_xpu": lambda x: assert_expected_inline(x, """6"""),
224-
"test_adam_tensor_lr_amsgrad_capturable_cuda": lambda x: assert_expected_inline(x, """5"""),
214+
"test_adam_tensor_lr_amsgrad_capturable_cuda": lambda x: assert_expected_inline(x, """6"""),
225215
"test_adam_tensor_lr_amsgrad_capturable_xpu": lambda x: assert_expected_inline(x, """6"""),
226-
"test_adam_tensor_lr_tensor_betas_amsgrad_capturable_cuda": lambda x: assert_expected_inline(x, """3"""),
216+
"test_adam_tensor_lr_tensor_betas_amsgrad_capturable_cuda": lambda x: assert_expected_inline(x, """6"""),
227217
"test_adam_tensor_lr_tensor_betas_amsgrad_capturable_xpu": lambda x: assert_expected_inline(x, """6"""),
228-
"test_adam_tensor_lr_tensor_betas_capturable_cuda": lambda x: assert_expected_inline(x, """3"""),
218+
"test_adam_tensor_lr_tensor_betas_capturable_cuda": lambda x: assert_expected_inline(x, """6"""),
229219
"test_adam_tensor_lr_tensor_betas_capturable_xpu": lambda x: assert_expected_inline(x, """6"""),
230220
"test_adam_amsgrad_capturable_cuda": lambda x: assert_expected_inline(x, """6"""),
231221
"test_adam_amsgrad_capturable_xpu": lambda x: assert_expected_inline(x, """6"""),
232222
"test_adadelta_tensor_lr_capturable_cuda": lambda x: assert_expected_inline(x, """6"""),
233223
"test_adadelta_tensor_lr_capturable_xpu": lambda x: assert_expected_inline(x, """6"""),
234224
"test_rmsprop_tensor_lr_capturable_cuda": lambda x: assert_expected_inline(x, """6"""),
235225
"test_rmsprop_tensor_lr_capturable_xpu": lambda x: assert_expected_inline(x, """6"""),
236-
"test_rmsprop_weight_decay_centered_foreach_cuda": lambda x: assert_expected_inline(x, """5"""),
237-
"test_rmsprop_weight_decay_centered_momentum_foreach_cuda": lambda x: assert_expected_inline(x, """5"""),
238-
"test_rmsprop_weight_decay_centered_momentum_maximize_foreach_cuda": lambda x: assert_expected_inline(x, """5"""),
239226
"test_adadelta_foreach_weight_decay_maximize_cpu": lambda x: assert_expected_inline(x, """12"""),
240227
"test_adadelta_foreach_rho_weight_decay_cpu": lambda x: assert_expected_inline(x, """12"""),
241228
"test_adadelta_foreach_weight_decay_cpu": lambda x: assert_expected_inline(x, """12"""),
@@ -265,18 +252,13 @@ class KernelCounts(NamedTuple):
265252
"test_adagrad_tensor_lr_cpu": lambda x: assert_expected_inline(x, """6"""),
266253
"test_adagrad_tensor_lr_cuda": lambda x: assert_expected_inline(x, """6"""),
267254
"test_adagrad_tensor_lr_xpu": lambda x: assert_expected_inline(x, """6"""),
268-
"test_adamax_tensor_lr_weight_decay_capturable_cuda": lambda x: assert_expected_inline(x, """5"""),
269-
"test_adamax_tensor_lr_weight_decay_capturable_foreach_cuda": lambda x: assert_expected_inline(x, """3"""),
255+
"test_adamax_tensor_lr_weight_decay_capturable_cuda": lambda x: assert_expected_inline(x, """6"""),
270256
"test_adamax_tensor_lr_weight_decay_capturable_xpu": lambda x: assert_expected_inline(x, """6"""),
271257
"test_asgd_tensor_lr_weight_decay_maximize_capturable_cuda": lambda x: assert_expected_inline(x, """5"""),
272258
"test_asgd_tensor_lr_weight_decay_maximize_capturable_xpu": lambda x: assert_expected_inline(x, """5"""),
273-
"test_nadam_tensor_lr_weight_decay_momentum_decay_decoupled_weight_decay_capturable_cuda": lambda x: assert_expected_inline(x, """7"""),
274-
"test_nadam_tensor_lr_weight_decay_momentum_decay_decoupled_weight_decay_capturable_xpu": lambda x: assert_expected_inline(x, """6"""),
275-
"test_nadam_weight_decay_foreach_cuda": lambda x: assert_expected_inline(x, """5"""),
276-
"test_nadam_weight_decay_maximize_foreach_cuda": lambda x: assert_expected_inline(x, """5"""),
277-
"test_nadam_weight_decay_momentum_decay_capturable_foreach_cuda": lambda x: assert_expected_inline(x, """5"""),
278-
"test_nadam_weight_decay_momentum_decay_foreach_cuda": lambda x: assert_expected_inline(x, """5"""),
279-
"test_radam_tensor_lr_capturable_weight_decay_decoupled_weight_decay_cuda": lambda x: assert_expected_inline(x, """10"""),
259+
"test_nadam_tensor_lr_weight_decay_momentum_decay_decoupled_weight_decay_capturable_cuda": lambda x: assert_expected_inline(x, """6"""), # noqa: B950
260+
"test_nadam_tensor_lr_weight_decay_momentum_decay_decoupled_weight_decay_capturable_xpu": lambda x: assert_expected_inline(x, """6"""), # noqa: B950
261+
"test_radam_tensor_lr_capturable_weight_decay_decoupled_weight_decay_cuda": lambda x: assert_expected_inline(x, """6"""),
280262
"test_radam_tensor_lr_capturable_weight_decay_decoupled_weight_decay_xpu": lambda x: assert_expected_inline(x, """6"""),
281263
"test_sgd_tensor_lr_cpu": lambda x: assert_expected_inline(x, """2"""),
282264
"test_sgd_tensor_lr_cuda": lambda x: assert_expected_inline(x, """2"""),
@@ -286,16 +268,16 @@ class KernelCounts(NamedTuple):
286268

287269
# also tracks currently supported optimizers
288270
KERNEL_COUNTS = {
289-
Adam: KernelCounts(multitensor=3, singletensor=8),
271+
Adam: KernelCounts(multitensor=2, singletensor=8),
290272
AdamW: KernelCounts(multitensor=2, singletensor=8),
291-
NAdam: KernelCounts(multitensor=2, singletensor=12),
273+
NAdam: KernelCounts(multitensor=2, singletensor=8),
292274
Rprop: KernelCounts(multitensor=2, singletensor=8),
293275
RMSprop: KernelCounts(multitensor=2, singletensor=8),
294276
Adadelta: KernelCounts(multitensor=2, singletensor=8),
295277
Adagrad: KernelCounts(multitensor=2, singletensor=8),
296278
SGD: KernelCounts(multitensor=1, singletensor=8),
297279
ASGD: KernelCounts(multitensor=2, singletensor=8),
298-
RAdam: KernelCounts(multitensor=2, singletensor=12),
280+
RAdam: KernelCounts(multitensor=2, singletensor=8),
299281
Adamax: KernelCounts(multitensor=2, singletensor=8),
300282
}
301283

‎test/inductor/test_cpu_repro.py‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3111,9 +3111,6 @@ def fn(*args):
31113111
eps = torch.tensor(0.9, dtype=torch.float64)
31123112
self.common(fn, (input, eps))
31133113

3114-
# todo(@boyuan): Effective user count change inlines more ops and leads to
3115-
# higher compilation time.
3116-
@unittest.skip("timeout")
31173114
@requires_vectorization
31183115
@patch("torch.cuda.is_available", lambda: False)
31193116
def test_vec_compare_op_cpu_only(self):
@@ -3476,7 +3473,7 @@ def fn(x):
34763473
)
34773474
self.assertEqual(
34783475
metrics.cpp_outer_loop_fused_inner_counts[0].local_buffer_number,
3479-
0,
3476+
1,
34803477
)
34813478
# Check the number of global buffer allocation
34823479
torch._dynamo.reset()
@@ -3525,7 +3522,15 @@ def fn(x):
35253522
self.common(fn, (x,), atol=atol, rtol=rtol)
35263523
self.assertEqual(
35273524
len(metrics.cpp_outer_loop_fused_inner_counts),
3528-
0,
3525+
1,
3526+
)
3527+
self.assertEqual(
3528+
metrics.cpp_outer_loop_fused_inner_counts[0].inner_kernel_number,
3529+
5,
3530+
)
3531+
self.assertEqual(
3532+
metrics.cpp_outer_loop_fused_inner_counts[0].local_buffer_number,
3533+
2,
35293534
)
35303535

35313536
@config.patch({"fx_graph_cache": False, "fx_graph_remote_cache": False})
@@ -3551,7 +3556,7 @@ def fn(x):
35513556
)
35523557
self.assertEqual(
35533558
metrics.cpp_outer_loop_fused_inner_counts[0].local_buffer_number,
3554-
0, # 2 global bufs share 1 local buf
3559+
1, # 2 global bufs share 1 local buf
35553560
)
35563561

35573562
@config.patch({"fx_graph_cache": False, "fx_graph_remote_cache": False})
@@ -3585,7 +3590,7 @@ def fn(x):
35853590
)
35863591
self.assertEqual(
35873592
metrics.cpp_outer_loop_fused_inner_counts[0].local_buffer_number,
3588-
0,
3593+
2,
35893594
)
35903595

35913596
@config.patch({"fx_graph_cache": False, "fx_graph_remote_cache": False})
@@ -3611,7 +3616,7 @@ def fn(x, y):
36113616
)
36123617
self.assertEqual(
36133618
metrics.cpp_outer_loop_fused_inner_counts[0].local_buffer_number,
3614-
0,
3619+
1,
36153620
)
36163621

36173622
@requires_vectorization
@@ -4848,7 +4853,7 @@ def fn(x):
48484853
_, code = run_and_get_cpp_code(opt_fn, x)
48494854
self.assertTrue(same(fn(x), opt_fn(x)))
48504855
# 4 kernels for max, exp, sum and div
4851-
check_metrics_vec_kernel_count(3)
4856+
check_metrics_vec_kernel_count(4)
48524857
FileCheck().check_count(
48534858
"Vectorized<int>::loadu(tmpbuf.data())", 0, exactly=True
48544859
).run(code)
@@ -5373,7 +5378,7 @@ def fn():
53735378

53745379
metrics.reset()
53755380
self.common(fn, ())
5376-
check_metrics_vec_kernel_count(0)
5381+
check_metrics_vec_kernel_count(1)
53775382

53785383
def test_highp_to_lowp_cse_var_cache_with_store(self):
53795384
# Fix issue: https://github.com/pytorch/pytorch/issues/128263
@@ -5798,8 +5803,10 @@ def fn(x):
57985803
x = torch.randn(1000, 1000)
57995804
opt_fn = torch.compile(fn)
58005805
_, code = run_and_get_cpp_code(opt_fn, x)
5801-
FileCheck().check(
5806+
FileCheck().check_count(
58025807
".exp()",
5808+
1,
5809+
exactly=True,
58035810
).run(code)
58045811

58055812
def test_convert_fp32_int64_oob_vec(self):

‎test/inductor/test_cpu_select_algorithm.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ def forward(self, view_368):
929929
rtol=rtol,
930930
)
931931
self.assertEqual(counters["inductor"]["cpp_templated_kernel_counter"], 1)
932-
self.assertEqual(counters["inductor"]["cpp_epilogue_fusion_counter"], 1)
932+
self.assertEqual(counters["inductor"]["cpp_epilogue_fusion_counter"], 2)
933933

934934
@inductor_config.patch({"freezing": True})
935935
@patches

‎test/inductor/test_native_matmul.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def f(x, y):
9191
# but same() uses the single value to assign both, resulting in
9292
# Accuracy failed: allclose not within tol=0.0001.
9393
self._check_equal(f, (x, y), tol=1e-3)
94-
self._check_code(f, (x, y), 2, 1)
94+
self._check_code(f, (x, y), 1, 1)
9595

9696
def test_reduction_mask_zeroout(self):
9797
def f(x, y):
@@ -199,7 +199,7 @@ def f(inp, weight, val, I_idx, W_idx, O_idx, out):
199199
out = torch.zeros((N_out, J), device=GPU_TYPE)
200200

201201
self._check_equal(f, (inp, weight, val, I_idx, W_idx, O_idx, out))
202-
self._check_code(f, (inp, weight, val, I_idx, W_idx, O_idx, out), 2, 1)
202+
self._check_code(f, (inp, weight, val, I_idx, W_idx, O_idx, out), 1, 1)
203203

204204
def test_bmm_fusion_complex2(self):
205205
# out[Ai[g],m,n] += Av[g,m,k,p] * B[Ak[g,p],k,n]

‎test/inductor/test_perf.py‎

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,59 +1305,6 @@ def scaled_index_add(x, y, scale_y):
13051305
self.assertExpectedInline(count_numel(scaled_index_add, *inp), """250""")
13061306

13071307

1308-
class RealizeOnReuseTests(TestCase):
1309-
"""
1310-
The cost model compares memory traffic for inlining vs materializing:
1311-
Inline: total_read_bytes * users
1312-
Materialize: total_read_bytes + output_bytes * (1 + users)
1313-
"""
1314-
1315-
@requires_gpu_and_triton
1316-
def test_broadcast_reads_not_materialized(self):
1317-
# y = x * w1 * w2 + b has 4 reads: x is [100, 100] (large), w1/w2/b
1318-
# are [100] (small broadcast). total_read_bytes ≈ output_bytes, so
1319-
# materializing y would add a costly write for negligible savings.
1320-
# The size-aware model correctly avoids materializing y.
1321-
def f(x, w1, w2, b):
1322-
y = x * w1 * w2 + b
1323-
return y.sum(dim=0), y.sum(dim=1)
1324-
1325-
inp = (T(100, 100), T(100), T(100), T(100))
1326-
# If y were materialized: reads x,w1,w2,b + writes y, then each
1327-
# reduction reads y. Much more traffic than inlining the cheap
1328-
# broadcast ops into each reduction kernel.
1329-
# Inline: 2*read(x,w1,w2,b) + 2*write(y.sum(...)) = 2 * (100 * 100 + 100 + 100 + 100) + 2 * 100 = 20800.
1330-
# Materialize: read(x,w1,w2,b) + write(y) + 2*read(y) + 2*write(y.sum()) =
1331-
# = (100 * 100 + 100 + 100 + 100) + 100 * 100 + 2 * 100 * 100 + 2 * 100 = 40500.
1332-
# so we should inline.
1333-
self.assertExpectedInline(count_numel(f, *inp), """20800""")
1334-
1335-
@requires_gpu_and_triton
1336-
def test_full_size_reads_materialized(self):
1337-
# x = a + b has 2 full-size reads and 3 users. total_read_bytes =
1338-
# 2 * output_bytes, so the cost model favors materializing:
1339-
# 2S * (3-1) = 4S >= S * (1+3) = 4S
1340-
def f(a, b, c, d):
1341-
x = a + b
1342-
return x + c, x + d, x.sum(dim=-1)
1343-
1344-
inp = (T(100, 100), T(100, 100), T(100, 100), T(100, 100))
1345-
self.assertExpectedInline(count_numel(f, *inp), """60100""")
1346-
1347-
@requires_gpu_and_triton
1348-
def test_dtype_promotion_not_materialized(self):
1349-
# x_f32 = x_bf16.float() has 1 bf16 read but fp32 output.
1350-
# total_read_bytes = S*2 (bf16), output_bytes = S*4 (fp32).
1351-
# S*2 * (users-1) < S*4 * (1+users), so materializing a large
1352-
# fp32 intermediate is avoided.
1353-
def f(x_bf16, bias_bf16):
1354-
logits = (x_bf16 + bias_bf16).float()
1355-
return logits.sum(dim=-1), logits.amax(dim=-1)
1356-
1357-
inp = (T(100, 100, dtype=torch.bfloat16), T(100, dtype=torch.bfloat16))
1358-
self.assertExpectedInline(count_numel(f, *inp), """5250""")
1359-
1360-
13611308
# Test cases where we don't do the right thing yet.
13621309
class WouldBeNiceIfItWorked:
13631310
def test_horizontal(self):

‎test/inductor/test_torchinductor.py‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14023,9 +14023,6 @@ def fn(x, y):
1402314023
# Skipped on ROCm until https://github.com/ROCm/triton/issues/443 resolved
1402414024
@slowTest
1402514025
def test_fuse_large_params(self):
14026-
if is_mps_backend(self.device):
14027-
raise unittest.SkipTest("Metal has a 31-buffer argument limit per kernel")
14028-
1402914026
def pt2_optimizer_step(optimizer):
1403014027
@torch.compile()
1403114028
def f():

‎test/inductor/test_torchinductor_opinfo.py‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -699,13 +699,6 @@ def wrapper_noop_set_seed(op, *args, **kwargs):
699699
{("cummin", f16): {"atol": 1e-3, "rtol": 1e-5}}
700700
)
701701

702-
# Ops whose backward uses exact comparisons (e.g. eq) on values that were
703-
# cast to a lower-precision dtype. Without emulate_precision_casts the
704-
# rounding that would happen at a buffer store is skipped when the cast is
705-
# inlined, causing the comparison to fail and producing NaN gradients.
706-
inductor_emulate_precision_casts = {
707-
("linalg.norm", f16),
708-
}
709702

710703
# Test with one sample only for following ops
711704
inductor_one_sample = defaultdict(dict)
@@ -1413,12 +1406,7 @@ def _get_tolerances(dtype):
14131406
for context_fn, kwarg_overrides in get_contexts(
14141407
has_rng_op, args, kwargs
14151408
):
1416-
precision_ctx = (
1417-
torch._inductor.config.patch("emulate_precision_casts", True)
1418-
if (op_name, dtype) in inductor_emulate_precision_casts
1419-
else contextlib.nullcontext()
1420-
)
1421-
with context_fn(), precision_ctx:
1409+
with context_fn():
14221410
# Base kwargs
14231411
adjusted_kwargs = {
14241412
"check_lowp": False,

0 commit comments

Comments
 (0)