Skip to content

[dlpack][runtime] Memory leak with dlpack capsules #20849

Closed

Description

@zjgarvey

What happened?

I'm not entirely sure what is going on, but I noticed in iree.turbine.kernel.wave.utils.run_utils, they need to rename the dlpack capsule from used_dltensor back to dltensor for some reason.

I was running into some memory issues with larger models (with several iree kernels plugged in), and renaming the capsules seemed to resolve the OOM.

My rough guess is that renaming the capsule to used_dltensor somehow invalidates the external deleter (which I assume is provided by pytorch).

Steps to reproduce your issue

install iree-turbine versioned earlier than today (5/19/2025)

run:

import torch
from iree.turbine.ops._jinja_test_ops import test_add

for _ in range(10000):
    x = torch.randn([256,256,256], device="cuda:0")
    y = torch.randn([256,256,256], device="cuda:0")
    _ = test_add(x,y)
    

This will raise:

torch.OutOfMemoryError: HIP out of memory. Tried to allocate 64.00 MiB. GPU 0 has a total capacity of 191.98 GiB of which 40.00 MiB is free. Of the allocated memory 191.25 GiB is allocated by PyTorch, and 0 bytes is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_HIP_ALLOC_CONF=expandable_segments:True to avoid fragmentation.  See documentation for Memory Management  (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)

However, if we rename the capsule back to dltensor after buffer view import in iree.turbine.runtime.device._device_import_torch_tensor_cuda_hip, then the issue no longer persists.

What component(s) does this issue relate to?

Python, Runtime

Version information

No response

Additional context

No response

Activity

  1. added
    bug 🐞
    on May 19, 2025
  2. zjgarvey commented on May 19, 2025

    @zjgarvey
    ContributorAuthor

    @raikonenfnu

    I remember you having some better idea about what we should do to actually fix this.

  3. stellaraccident commented on Jun 3, 2025

    @stellaraccident
    Collaborator

    Repro is good but this needs a root cause. Also see the design that dlpack uses for the ownership handoff: https://dmlc.github.io/dlpack/latest/python_spec.html#implementation

  4. stellaraccident commented on Jun 3, 2025

    @stellaraccident
    Collaborator

    Note that the 843 patch has been reverted, so this likely repros at head.

  5. zjgarvey commented on Jun 3, 2025

    @zjgarvey
    ContributorAuthor

    @stellaraccident I'm sorry for the sloppy patch in turbine. I should have prioritized this higher and found a valid fix.

  6. raikonenfnu commented on Jun 3, 2025

    @raikonenfnu
    Collaborator

    Repro is good but this needs a root cause. Also see the design that dlpack uses for the ownership handoff: https://dmlc.github.io/dlpack/latest/python_spec.html#implementation

    Yeap! there was a typo in the naming of the dlpack capsule tensor haha, will push up a quick fix for that :)

  7. zjgarvey commented on Jun 4, 2025

    @zjgarvey
    ContributorAuthor

    @AWoloszyn @Hardcode84 Is this issue good to close?

  8. AWoloszyn commented on Jun 4, 2025

    @AWoloszyn
    Contributor

    As far as I can tell this should be good to close.
    I found an obvious memory leak, and the sample no longer leaks.

  9. Hardcode84 commented on Jun 4, 2025

    @Hardcode84
    Contributor

    Yes, look like fix works for Wave

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐞

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions