mitsuba-renderer / mitsuba3

Mitsuba 3: A Retargetable Forward and Inverse Renderer
https://www.mitsuba-renderer.org/
Other
2.09k stars 246 forks source link

How can I resolve these memory leak issues? #1341

Closed free7187 closed 1 month ago

free7187 commented 1 month ago

Summary

I just tried to git clone the latest code from the master branch, compiled it, and called it in Python, but I encountered a memory leak issue.

Description

I tried to test the code from the master branch. Since this is just a preliminary test, the code is quite simple. The specific code is as follows:

import sys
sys.path.insert(0, "./mitsuba3/build/Release/python")
import mitsuba as mi
import drjit as dr 
import torch
import torch.nn.functional as F
import torch.nn as nn
import torch.optim as optim
import numpy as np
import os
import math

os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
mi.set_variant('cuda_ad_rgb')

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

class real_data_integrator(mi.SamplingIntegrator):
    def __init__(self, props):
        super().__init__(props)

    def sample(self, 
               scene: mi.Scene, 
               sampler:mi.Sampler, 
               ray: mi.RayDifferential3f, 
               medium: mi.Medium = None,
               active: bool = True) -> tuple[mi.Color3f, bool, list[float]]:

        L = mi.Spectrum(0.0)

        # ---------------------- Direct emission ----------------------
        si = scene.ray_intersect(ray, active)        

        bsdf_ctx = mi.BSDFContext()
        sample1 = sampler.next_1d()
        sample2 = sampler.next_2d()
        bsdf: mi.BSDF = si.bsdf()

        bsdf_sample, bsdf_weight = bsdf.sample(
                    bsdf_ctx, si, sample1, sample2)

        # self.shape_confirm(bsdf_weight)

        valid_ray = active & si.is_valid()
        active &= si.is_valid()

        cosThetaI = mi.Frame3f.cos_theta(si.wi)
        active &= (cosThetaI>0)        

        # Sample the emitter
        ds, em_weight = scene.sample_emitter_direction(
            si, sampler.next_2d(), True, active
        )
        active &= ds.pdf != 0.0

        # self.shape_confirm(em_weight)
        return dr.select(True, L, 0.0), valid_ray, []

    # @dr.wrap(source='drjit', target='torch')
    # def shape_confirm(self,em_weight):
    #     print(em_weight.shape)

mi.register_integrator('My_integrator', lambda props: real_data_integrator(props))
xml_path= "spectrum_test.xml"
scene = mi.load_file(xml_path)
image=mi.render(scene)

However, after running the program, the error message displayed as follows:

jit_shutdown(): detected 272 variable leaks:

  • uint32 r23[1] = evaluated(), refs=1
  • float32 r24[1] = evaluated(), refs=1
  • float32 r25[1] = evaluated(), refs=1
  • float32 r26[1] = evaluated(), refs=1
  • uint32 r27[1] = evaluated(), refs=1
  • float32 r28[1] = evaluated(), refs=1
  • float32 r29[1] = evaluated(), refs=1
  • float32 r30[1] = evaluated(), refs=1
  • float32 r31[1] = evaluated(), refs=1
  • float32 r32[1] = evaluated(), refs=1
  • float32 r33[1] = evaluated(), refs=1
  • float32 r34[1] = evaluated(), refs=1
  • uint64 r35[8388608] = add(r269, r266), refs=1
  • uint64 r36[1] = literal(), refs=2
  • float32 r37[1] = evaluated(), refs=1
  • float32 r38[1] = evaluated(), refs=1
  • float32 r39[1] = evaluated(), refs=1
  • float32 r40[1] = evaluated(), refs=1
  • float32 r41[1] = evaluated(), refs=1
  • float32 r42[1] = evaluated(), refs=1
  • float32 r43[1] = evaluated(), refs=1
  • float32 r44[1] = evaluated(), refs=1
  • float32 r45[1] = evaluated(), refs=1
  • float32 r46[1] = evaluated(), refs=1
  • float32 r47[1] = evaluated(), refs=1
  • float32 r48[1] = evaluated(), refs=1
  • float32 r49[1] = evaluated(), refs=1
  • float32 r50[1] = evaluated(), refs=1
  • float32 r51[1] = evaluated(), refs=1
  • float32 r52[1] = evaluated(), refs=1
  • float32 r53[1] = evaluated(), refs=1
  • float32 r54[1] = evaluated(), refs=1
  • float32 r55[1] = evaluated(), refs=1
  • float32 r56[1] = evaluated(), refs=1
  • float32 r57[1] = evaluated(), refs=1
  • float32 r58[1] = evaluated(), refs=1
  • float32 r59[1] = evaluated(), refs=1
  • float32 r60[1] = evaluated(), refs=1
  • float32 r61[1] = evaluated(), refs=1
  • float32 r62[1] = evaluated(), refs=1
  • float32 r63[1] = evaluated(), refs=1
  • float32 r64[1] = evaluated(), refs=1
  • float32 r65[1] = literal(), refs=16
  • float32 r66[1] = literal(), refs=52
  • uint32 r67[36] = evaluated(), refs=1
  • float32 r68[48] = evaluated(), refs=1
  • float32 r69[48] = evaluated(), refs=1
  • float32 r70[32] = evaluated(), refs=1
  • float32 r71[1] = literal(), refs=1
  • float32 r72[1] = evaluated(), refs=1
  • (skipping remainder) jit_registry_shutdown(): leaking 1 instances of type "mitsuba::BSDF". jit_registry_shutdown(): leaking 1 instances of type "mitsuba::Emitter". jit_registry_shutdown(): leaking 1 instances of type "mitsuba::Shape". jit_registry_shutdown(): leaking 1 instances of type "mitsuba::Sensor". jit_malloc_shutdown(): leaked
  • device memory: 4.012 MiB in 159 allocations nanobind: leaked 7 instances!
  • leaked instance 00000277C8A83E60 of type "mitsuba.Sampler"
  • leaked instance 00000277E801D868 of type "main.real_data_integrator"
  • leaked instance 00000277C756DCC8 of type "mitsuba.Color0f"
  • leaked instance 00000277C7045008 of type "drjit.cuda.ad.TensorXf"
  • leaked instance 00000277C756DC48 of type "mitsuba.Color0f"
  • leaked instance 00000277C7DF84F0 of type "mitsuba.ProjectiveCamera"
  • leaked instance 00000277C63410F0 of type "mitsuba.Scene" nanobind: leaked 22 types!
  • leaked type "mitsuba.Sensor"
  • leaked type "drjit.cuda.ad.Bool"
  • leaked type "mitsuba.Object"
  • leaked type "mitsuba.Medium"
  • leaked type "mitsuba.Scene"
  • leaked type "mitsuba.Integrator"
  • leaked type "mitsuba.Ray3f"
  • leaked type "mitsuba.Color3f"
  • leaked type "mitsuba.Endpoint"
  • leaked type "mitsuba.Point3f"
  • leaked type "mitsuba.Sampler" jit_registry_shutdown(): leaking 1 instances of type "mitsuba::Emitter". jit_registry_shutdown(): leaking 1 instances of type "mitsuba::Shape". jit_registry_shutdown(): leaking 1 instances of type "mitsuba::Sensor". jit_malloc_shutdown(): leaked
  • device memory: 4.012 MiB in 159 allocations nanobind: leaked 7 instances!
  • leaked instance 00000277C8A83E60 of type "mitsuba.Sampler"
  • leaked instance 00000277E801D868 of type "main.real_data_integrator"
  • leaked instance 00000277C756DCC8 of type "mitsuba.Color0f"
  • leaked instance 00000277C7045008 of type "drjit.cuda.ad.TensorXf"
  • leaked instance 00000277C756DC48 of type "mitsuba.Color0f"
  • leaked instance 00000277C7DF84F0 of type "mitsuba.ProjectiveCamera"
  • leaked instance 00000277C63410F0 of type "mitsuba.Scene" nanobind: leaked 22 types!
  • leaked type "mitsuba.Sensor"
  • leaked type "drjit.cuda.ad.Bool"
  • leaked type "mitsuba.Object"
  • leaked type "mitsuba.Medium"
  • leaked type "mitsuba.Scene"
  • leaked type "mitsuba.Integrator"
  • leaked type "mitsuba.Ray3f"
  • leaked type "mitsuba.Color3f"
  • leaked type "mitsuba.Endpoint"
  • leaked type "mitsuba.Point3f"
  • leaked type "mitsuba.Sampler" jit_malloc_shutdown(): leaked
  • device memory: 4.012 MiB in 159 allocations nanobind: leaked 7 instances!
  • leaked instance 00000277C8A83E60 of type "mitsuba.Sampler"
  • leaked instance 00000277E801D868 of type "main.real_data_integrator"
  • leaked instance 00000277C756DCC8 of type "mitsuba.Color0f"
  • leaked instance 00000277C7045008 of type "drjit.cuda.ad.TensorXf"
  • leaked instance 00000277C756DC48 of type "mitsuba.Color0f"
  • leaked instance 00000277C7DF84F0 of type "mitsuba.ProjectiveCamera"
  • leaked instance 00000277C63410F0 of type "mitsuba.Scene" nanobind: leaked 22 types!
  • leaked type "mitsuba.Sensor"
  • leaked type "drjit.cuda.ad.Bool"
  • leaked type "mitsuba.Object"
  • leaked type "mitsuba.Medium"
  • leaked type "mitsuba.Scene"
  • leaked type "mitsuba.Integrator"
  • leaked type "mitsuba.Ray3f"
  • leaked type "mitsuba.Color3f"
  • leaked type "mitsuba.Endpoint"
  • leaked type "mitsuba.Point3f"
  • leaked type "mitsuba.Sampler"
  • ... skipped remainder nanobind: leaked 181 functions! nanobind: leaked 7 instances!
  • leaked instance 00000277C8A83E60 of type "mitsuba.Sampler"
  • leaked instance 00000277E801D868 of type "main.real_data_integrator"
  • leaked instance 00000277C756DCC8 of type "mitsuba.Color0f"
  • leaked instance 00000277C7045008 of type "drjit.cuda.ad.TensorXf"
  • leaked instance 00000277C756DC48 of type "mitsuba.Color0f"
  • leaked instance 00000277C7DF84F0 of type "mitsuba.ProjectiveCamera"
  • leaked instance 00000277C63410F0 of type "mitsuba.Scene" nanobind: leaked 22 types!
  • leaked type "mitsuba.Sensor"
  • leaked type "drjit.cuda.ad.Bool"
  • leaked type "mitsuba.Object"
  • leaked type "mitsuba.Medium"
  • leaked type "mitsuba.Scene"
  • leaked type "mitsuba.Integrator"
  • leaked type "mitsuba.Ray3f"
  • leaked type "mitsuba.Color3f"
  • leaked type "mitsuba.Endpoint"
  • leaked type "mitsuba.Point3f"
  • leaked type "mitsuba.Sampler"
  • ... skipped remainder nanobind: leaked 181 functions! nanobind: leaked 22 types!
  • leaked type "mitsuba.Sensor"
  • leaked type "drjit.cuda.ad.Bool"
  • leaked type "mitsuba.Object"
  • leaked type "mitsuba.Medium"
  • leaked type "mitsuba.Scene"
  • leaked type "mitsuba.Integrator"
  • leaked type "mitsuba.Ray3f"
  • leaked type "mitsuba.Color3f"
  • leaked type "mitsuba.Endpoint"
  • leaked type "mitsuba.Point3f"
  • leaked type "mitsuba.Sampler"
  • ... skipped remainder nanobind: leaked 181 functions!
  • leaked type "mitsuba.Medium"
  • leaked type "mitsuba.Scene"
  • leaked type "mitsuba.Integrator"
  • leaked type "mitsuba.Ray3f"
  • leaked type "mitsuba.Color3f"
  • leaked type "mitsuba.Endpoint"
  • leaked type "mitsuba.Point3f"
  • leaked type "mitsuba.Sampler"
  • ... skipped remainder nanobind: leaked 181 functions!
  • leaked function "repr"
  • leaked type "mitsuba.Integrator"
  • leaked type "mitsuba.Ray3f"
  • leaked type "mitsuba.Color3f"
  • leaked type "mitsuba.Endpoint"
  • leaked type "mitsuba.Point3f"
  • leaked type "mitsuba.Sampler"
  • ... skipped remainder nanobind: leaked 181 functions!
  • leaked function "repr"
  • leaked type "mitsuba.Color3f"
  • leaked type "mitsuba.Endpoint"
  • leaked type "mitsuba.Point3f"
  • leaked type "mitsuba.Sampler"
  • ... skipped remainder nanobind: leaked 181 functions!
  • leaked function "repr"
  • leaked type "mitsuba.Point3f"
  • leaked type "mitsuba.Sampler"
  • ... skipped remainder nanobind: leaked 181 functions!
  • leaked function "repr"
  • leaked type "mitsuba.Sampler"
  • ... skipped remainder nanobind: leaked 181 functions!
  • leaked function "repr"
  • ... skipped remainder nanobind: leaked 181 functions!
  • leaked function "repr"
  • leaked function "pdf_direction"
  • leaked function "repr"
  • leaked function "pdf_direction"
  • leaked function "pdf_direction"
  • leaked function "sample_interaction"
  • leaked function "sample_interaction"
  • leaked function "pdf_emitter"
  • leaked function ""
  • leaked function ""
  • leaked function "sample_direction"
  • leaked function "set_sample_count"
  • leaked function "next_1d"
  • leaked function ""
  • leaked function "x"
  • ... skipped remainder nanobind: this is likely caused by a reference counting issue in the binding code.

How can I resolve these issues?

njroussel commented 1 month ago

You can ignore these. This is due to Python's shutdown procedure that is taking "shortcuts" by not properly destroying all of the objects before exiting.

(We'll most likely be suppressing these warnings)