Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError when attempting to remove inductor buffers twice #102857

Closed
mixarcid opened this issue Jun 2, 2023 · 6 comments
Closed

AttributeError when attempting to remove inductor buffers twice #102857

mixarcid opened this issue Jun 2, 2023 · 6 comments
Labels
good first issue module: dynamic shapes module: inductor oncall: pt2 triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@mixarcid
Copy link
Contributor

mixarcid commented Jun 2, 2023

🐛 Describe the bug

I'm having difficultly creating a minimal reproducible example of this bug, but I'm hoping it's pretty clear what's going on. Basically, I'm compiling a complex function with dynamic=True. This function returns a value U. When I call U.backward(), I'm getting an error where the inductor attempts to remove a buffer twice. Here's the relevant part of the stack trace:

  File "/home/boris/Repos/plantain/models/diffusion_v3.py", line 183, in closure
    U.backward()
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_tensor.py", line 488, in backward
    torch.autograd.backward(
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/autograd/__init__.py", line 204, in backward
    Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/autograd/function.py", line 274, in apply
    return user_fn(self, *args)
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_functorch/aot_autograd.py", line 3027, in backward
    out = call_compiled_backward()
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_functorch/aot_autograd.py", line 2997, in call_compiled_backward
    CompiledFunction.compiled_bw = aot_config.bw_compiler(
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_dynamo/backends/common.py", line 33, in _wrapped_bw_compiler
    return eval_frame.disable(eval_frame.disable(bw_compiler)(*args, **kwargs))
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py", line 289, in _fn
    return fn(*args, **kwargs)
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_dynamo/external_utils.py", line 17, in inner
    return fn(*args, **kwargs)
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_dynamo/utils.py", line 180, in time_wrapper
    r = func(*args, **kwargs)
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_inductor/compile_fx.py", line 720, in bw_compiler
    return inner_compile(
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_dynamo/repro/after_aot.py", line 80, in debug_wrapper
    inner_compiled_fn = compiler_fn(gm, example_inputs)
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_inductor/debug.py", line 220, in inner
    return fn(*args, **kwargs)
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_inductor/compile_fx.py", line 46, in newFunction
    return old_func(*args, **kwargs)
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_inductor/compile_fx.py", line 291, in compile_fx_inner
    compiled_fn = graph.compile_to_fn()
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_inductor/graph.py", line 719, in compile_to_fn
    return self.compile_to_module().call
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_dynamo/utils.py", line 180, in time_wrapper
    r = func(*args, **kwargs)
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_inductor/graph.py", line 693, in compile_to_module
    code, linemap = self.codegen()
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_inductor/graph.py", line 644, in codegen
    self.scheduler.codegen()
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_dynamo/utils.py", line 180, in time_wrapper
    r = func(*args, **kwargs)
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_inductor/scheduler.py", line 1344, in codegen
    self.get_backend(device).codegen_nodes(node.get_nodes())
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_inductor/codegen/cpp.py", line 2569, in codegen_nodes
    cpp_kernel_proxy.codegen_nodes(nodes)
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_inductor/codegen/cpp.py", line 2484, in codegen_nodes
    codegen_kernel(
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_inductor/codegen/cpp.py", line 2375, in codegen_kernel
    with kernel_group.new_kernel(cls, *args) as kernel:
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_inductor/codegen/common.py", line 830, in __exit__
    V.graph.scheduler.remove_kernel_local_buffers()
  File "/home/boris/miniconda3/envs/plantain-nightly/lib/python3.10/site-packages/torch/_inductor/scheduler.py", line 1229, in remove_kernel_local_buffers
    remove = all(n in names_to_remove for n in buf.other_names)
AttributeError: 'str' object has no attribute 'other_names'

When I check out _inductor/scheduler.py:1229, it's pretty clear what the issue is. It's trying to access the other_names property of buf, but buf is just the string "REMOVED":

        for name in names_to_remove:
            if name in V.kernel.args.inplace_buffers:
                buf = V.kernel.args.inplace_buffers[name]
                remove = all(n in names_to_remove for n in buf.other_names)
                if remove:
                    self.remove_inplace_buffer(name)
                V.graph.inplaced_to_remove.add(name)
            else:
                self.remove_buffer(name)

    def remove_buffer(self, name):
        # Assign a special value instead of deleting the entry
        # because we still rely on output_buffers's length to
        # generate unique arg name.
        log.debug("remove_buffer(%r)", name)
        V.kernel.args.output_buffers[name] = "REMOVED"
        V.graph.removed_buffers.add(name)

    def remove_inplace_buffer(self, name):
        log.debug("removing_inplace_buffer(%r)", name)
        V.kernel.args.inplace_buffers[name] = "REMOVED"
        V.graph.removed_buffers.add(name)

If I simply skip any already removed buffers in this code, everything seems to be working fine:

        for name in names_to_remove:
            if name in V.kernel.args.inplace_buffers:
                buf = V.kernel.args.inplace_buffers[name]
                if buf == "REMOVED": 
                    continue
                remove = all(n in names_to_remove for n in buf.other_names)
                if remove:
                    self.remove_inplace_buffer(name)
                V.graph.inplaced_to_remove.add(name)
            else:
                self.remove_buffer(name)

I have no idea what this code is supposed to be doing, so not sure if this is a reasonable solution to the actual problem. I'll keep trying to come up with a reproducible example for this bug, but I wanted to put this up in case the solution really is this simple.

Versions

PyTorch version: 2.1.0.dev20230601
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Arch Linux (x86_64)
GCC version: (GCC) 13.1.1 20230429
Clang version: 15.0.7
CMake version: version 3.26.4
Libc version: glibc-2.37

Python version: 3.10.11 (main, Apr 20 2023, 19:02:41) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-6.3.3-arch1-1-x86_64-with-glibc2.37
Is CUDA available: True
CUDA runtime version: 11.0.221
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 2060
Nvidia driver version: 530.41.03
cuDNN version: Probably one of the following:
/usr/lib/libcudnn.so.8.2.1
/usr/lib/libcudnn_adv_infer.so.8.2.1
/usr/lib/libcudnn_adv_train.so.8.2.1
/usr/lib/libcudnn_cnn_infer.so.8.2.1
/usr/lib/libcudnn_cnn_train.so.8.2.1
/usr/lib/libcudnn_ops_infer.so.8.2.1
/usr/lib/libcudnn_ops_train.so.8.2.1
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 39 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 12
On-line CPU(s) list: 0-11
Vendor ID: GenuineIntel
Model name: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
CPU family: 6
Model: 158
Thread(s) per core: 2
Core(s) per socket: 6
Socket(s): 1
Stepping: 10
CPU(s) scaling MHz: 44%
CPU max MHz: 4500.0000
CPU min MHz: 800.0000
BogoMIPS: 5202.65
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp md_clear flush_l1d
Virtualization: VT-x
L1d cache: 192 KiB (6 instances)
L1i cache: 192 KiB (6 instances)
L2 cache: 1.5 MiB (6 instances)
L3 cache: 12 MiB (1 instance)
NUMA node(s): 1
NUMA node0 CPU(s): 0-11
Vulnerability Itlb multihit: KVM: Mitigation: VMX disabled
Vulnerability L1tf: Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown: Mitigation; PTI
Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed: Mitigation; IBRS
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; IBRS, IBPB conditional, STIBP conditional, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds: Vulnerable: No microcode
Vulnerability Tsx async abort: Not affected

Versions of relevant libraries:
[pip3] numpy==1.24.3
[pip3] pytorch-lightning==1.9.1
[pip3] torch==2.1.0.dev20230601
[pip3] torchaudio==2.1.0.dev20230601
[pip3] torchmetrics==0.11.4
[pip3] torchvision==0.16.0.dev20230601
[pip3] triton==2.1.0
[conda] blas 1.0 mkl
[conda] mkl 2023.1.0 h6d00ec8_46342
[conda] mkl-fft 1.3.6 pypi_0 pypi
[conda] mkl-random 1.2.2 pypi_0 pypi
[conda] mkl-service 2.4.0 pypi_0 pypi
[conda] mkl_fft 1.3.6 py310h1128e8f_1
[conda] mkl_random 1.2.2 py310h1128e8f_1
[conda] numpy 1.24.3 pypi_0 pypi
[conda] numpy-base 1.24.3 py310hb5e798b_1
[conda] pytorch 2.1.0.dev20230601 py3.10_cuda12.1_cudnn8.8.1_0 pytorch-nightly
[conda] pytorch-cuda 12.1 ha16c6d3_5 pytorch-nightly
[conda] pytorch-lightning 1.9.1 pypi_0 pypi
[conda] pytorch-mutex 1.0 cuda pytorch-nightly
[conda] torch 2.1.0.dev20230601 pypi_0 pypi
[conda] torchaudio 2.1.0.dev20230601 pypi_0 pypi
[conda] torchmetrics 0.11.4 pypi_0 pypi
[conda] torchtriton 2.1.0+9820899b38 py310 pytorch-nightly
[conda] torchvision 0.16.0.dev20230601 pypi_0 pypi
[conda] triton 2.1.0 pypi_0 pypi

cc @ezyang @msaroufim @wconstab @bdhirsh @anijain2305 @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @Xia-Weiwen @wenzhe-nrv @jiayisunx @peterbell10 @ipiszy @ngimel @yf225 @bertmaher

@janeyx99 janeyx99 added triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module module: inductor oncall: pt2 labels Jun 5, 2023
@ezyang
Copy link
Contributor

ezyang commented Jun 5, 2023

I don't think I'd know how to fix this without the repro. A non-minimal repro is fine but it looks like plantain is not a public repo.

Did TORCHDYNAMO_REPRO_AFTER="aot" work per https://pytorch.org/docs/stable/dynamo/troubleshooting.html ? I'm at a 30% success rate but if it works it helps a lot. If it didn't paste the error here and I can also look

@mixarcid
Copy link
Contributor Author

mixarcid commented Jun 6, 2023

Thanks for looking at this! The TORCHDYNAMO_REPRO_AFTER functionality is awesome -- it successfully wrote the following script that reproduces the error: https://gist.github.com/mixarcid/7f836054a277500ac104caef62a4210f.

@ngimel
Copy link
Collaborator

ngimel commented Jun 6, 2023

The fix is something like https://github.com/pytorch/pytorch/compare/main...ngimel:pytorch:remove_buf?expand=1, but I won't be able to create a testcase any time soon
Edit: oh, right that's the proposed fix already

@ezyang
Copy link
Contributor

ezyang commented Jun 7, 2023

Good first issue to create the test case + the proposed fix.

@MBora
Copy link

MBora commented Jun 7, 2023

I'd like to work on this issue.

@mixarcid
Copy link
Contributor Author

mixarcid commented Jul 3, 2023

@Potato-Cracker any luck with this? If not, I can take it. I'm releasing some code in the next couple weeks that depends on this being fixed.

mixarcid added a commit to mixarcid/pytorch that referenced this issue Jul 10, 2023
pytorchmergebot pushed a commit to mixarcid/pytorch that referenced this issue Jul 14, 2023
speediedan pushed a commit to speediedan/pytorch that referenced this issue Jul 19, 2023
…ice" (pytorch#104901)

Fixes pytorch#102857

I added the proposed fix and found a reasonably small test case. I don't have any insight into why this test case was causing the error, but it is fixed now.

Pull Request resolved: pytorch#104901
Approved by: https://github.com/eellison
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue module: dynamic shapes module: inductor oncall: pt2 triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants