Skip to content

Commit

Permalink
[inductor] share cse cache during vectorized indirect load (pytorch#1…
Browse files Browse the repository at this point in the history
…24597)

Fix pytorch#123502

`swap_buffer` in not needed in vectorized indirect load, remove it to share cse buffer.
```
auto tmp8 =
[&]
{
    __at_align__ std::array<int64_t, 16> tmpbuf;
    tmp7.store(tmpbuf.data());
    return tmpbuf;
}
()
;
//
// other codes
//
// also store tmp7 here (redundant tmp16)
auto tmp16 =
[&]
{
    __at_align__ std::array<int64_t, 16> tmpbuf;
    tmp7.store(tmpbuf.data());
    return tmpbuf;
}
()
;
```

Pull Request resolved: pytorch#124597
Approved by: https://github.com/jgong5, https://github.com/jansel
  • Loading branch information
zhuhaozhe authored and petrex committed May 3, 2024
1 parent 51c42f0 commit 59b9ac1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
33 changes: 33 additions & 0 deletions test/inductor/test_cpu_repro.py
Original file line number Diff line number Diff line change
Expand Up @@ -3683,6 +3683,39 @@ def forward(self, x):
x = torch.randn(1, 4, 2, 2)
self.common(fn, (x,))

@requires_vectorization
def test_vec_indirect_load_cse_cache(self):
# https://github.com/pytorch/pytorch/issues/123502
from math import inf

def fn(arg0_1):
full_default = torch.ops.aten.full.default([209985], 1)
select = torch.ops.aten.select.int(arg0_1, 0, 0)
select_1 = torch.ops.aten.select.int(arg0_1, 0, 1)
view = torch.ops.aten.reshape.default(select_1, [-1])
expand = torch.ops.aten.expand.default(view, [209985])
full_default_1 = torch.ops.aten.full.default([10000], 0)
scatter_add = torch.ops.aten.scatter_add.default(
full_default_1, 0, expand, full_default
)
pow_1 = torch.ops.aten.pow.Tensor_Scalar(scatter_add, -0.5)
eq = torch.ops.aten.eq.Scalar(pow_1, inf)
full_default_2 = torch.ops.aten.full.default([], 0.0)
where = torch.ops.aten.where.self(eq, full_default_2, pow_1)
index = torch.ops.aten.index.Tensor(where, [select])
index_1 = torch.ops.aten.index.Tensor(where, [select_1])
mul_1 = torch.ops.aten.mul.Tensor(index, index_1)
return (mul_1,)

x = torch.zeros(2, 209985).to(torch.int64)
opt_fn = torch._dynamo.optimize("inductor")(fn)
_, code = run_and_get_cpp_code(opt_fn, x)
FileCheck().check_count(
"return at::vec::VectorizedN<int64_t,2>::loadu(tmpbuf.data(),",
2,
exactly=True,
).run(code)


if __name__ == "__main__":
from torch._inductor.test_case import run_tests
Expand Down
4 changes: 2 additions & 2 deletions torch/_inductor/codegen/cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2336,7 +2336,7 @@ def vec_to_array(vec_var: CppCSEVariable) -> CppCSEVariable:
assert vec_var.is_vec
code = BracesBuffer()
code.writeline("[&]")
with self.swap_buffers(code), code.indent():
with code.indent():
vec_dtype = vec_var.dtype
assert vec_dtype is not None
if vec_dtype == torch.bool:
Expand All @@ -2357,7 +2357,7 @@ def vec_to_array(vec_var: CppCSEVariable) -> CppCSEVariable:
assert opt_ctx is not None
code = BracesBuffer()
code.writeline("[&]")
with self.swap_buffers(code), code.indent():
with code.indent():
result_size = get_result_size(dtype)
result_declare = (
f"__at_align__ std::array<{DTYPE_TO_CPP[dtype]}, {result_size}> tmpbuf;"
Expand Down

0 comments on commit 59b9ac1

Please sign in to comment.