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

Release memory after every mesh optimization iterations without ti.reset() #8591

Open
Szy-Young opened this issue Oct 16, 2024 · 0 comments
Open
Labels
question Question on using Taichi

Comments

@Szy-Young
Copy link

Hi! I am trying to build a mesh optimization pipeline by combining Taichi with PyTorch. My general pipeline looks like the pseudo-code below:

import torch
import taichi as ti

# To-be-optimized geometry parameters
params = torch.randn(..., requires_grad=True)
optimizer = torch.optim.Adam([params], lr=lr)

for it in range(n_iter):
    verts0, faces0 = param_to_mesh(params)   # PyTorch-based operations
    
    ti.init(arch=ti.cuda)

    verts0_ti, faces0_ti = cast_tensor_to_field(verts0, faces0)    # Cast PyTorch tensors to Taichi fields
    verts_ti, faces_ti = process_mesh(verts0_ti, faces0_ti)    # Taichi-based operations

    verts = verts_ti.to_torch()
    verts.requires_grad_(True)

    # Backward propagation
    loss = compute_loss(verts)
    ....

    ti.reset()

As you can see, I create some intermediate variables as Taichi fields in every iteration and they will continuously occupy the memory (I know the simple "del" cannot release the memory) and lead to the OOM error. Since my mesh topology may change during the optimization (shape of verts/faces may change), I cannot create these Taichi fields before the whole optimization either. So I have to repetitively use ti.init() and ti.reset() in every iterations to release the memory, which makes the code run slower.

I wonder if there is another way to release all the memory occupied by Taichi fields without ti.reset(). Note that I don't need to delete the specified fields, since all the Taichi fields are free to be removed at the end of every iteration.

Any suggestions are genuinely appreciated! Thanks

@Szy-Young Szy-Young added the question Question on using Taichi label Oct 16, 2024
@github-project-automation github-project-automation bot moved this to Untriaged in Taichi Lang Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question on using Taichi
Projects
Status: Untriaged
Development

No branches or pull requests

1 participant