| 43 | |
| 44 | |
| 45 | def test_gcbag_is_being_emptied(): |
| 46 | # Make sure there's no work scheduled on the stream, it's all ours. |
| 47 | workstream = cvcuda.Stream() |
| 48 | |
| 49 | # In order to test if the GCBag was really emptied, |
| 50 | |
| 51 | # we create a torch tensor, |
| 52 | ttensor = torch.as_tensor(np.ndarray([100, 1500, 1500, 3], np.uint8), device="cuda") |
| 53 | # keep track of its initial refcount. |
| 54 | orig_ttensor_refcount = sys.getrefcount(ttensor) |
| 55 | # and wrap it in a nvcv tensor 'cvwrapper' |
| 56 | cvwrapper = cvcuda.as_tensor(ttensor, cvcuda.TensorLayout.NHWC) |
| 57 | |
| 58 | # We can then indirectly tell if 'cvwrapper' was destroyed by |
| 59 | # monitoring 'ttensor's refcount. |
| 60 | # This works because we know 'cvwrapper' holds a reference to |
| 61 | # 'ttensor', as proved by the following assert: |
| 62 | wrapped_ttensor_refcount = sys.getrefcount(ttensor) |
| 63 | assert wrapped_ttensor_refcount > orig_ttensor_refcount |
| 64 | |
| 65 | # We need now to make sure cvwrapper is in the GCBag. |
| 66 | # For that, we need to use it in operator |
| 67 | with workstream: |
| 68 | cvcuda.median_blur(cvwrapper, [3, 3], stream=workstream) |
| 69 | # And make sure it finishes. |
| 70 | workstream.sync() |
| 71 | # Make sure the auxiliary stream has finished extending cvwrapper's lifetime |
| 72 | cvcuda.internal.syncAuxStream() |
| 73 | |
| 74 | # cvwrapper being referenced by others shouldn't change ttensor's refcount. |
| 75 | assert sys.getrefcount(ttensor) == wrapped_ttensor_refcount |
| 76 | |
| 77 | # Now remove cvwrapper from the cache by clearing it. |
| 78 | cvcuda.clear_cache() |
| 79 | |
| 80 | # We can now release it from python side. We can't track its lifetime |
| 81 | # directly anymore. |
| 82 | del cvwrapper |
| 83 | |
| 84 | # But we know indirectly that it is still alive |
| 85 | assert sys.getrefcount(ttensor) == wrapped_ttensor_refcount |
| 86 | |
| 87 | # To finally destroy cvwrapper, we empty the GCBag by executing a |
| 88 | # cvcuda operator, any would do. |
| 89 | with workstream: |
| 90 | cvcuda.median_blur( |
| 91 | cvcuda.Tensor((3, 64, 32, 3), cvcuda.Type.U8, cvcuda.TensorLayout.NHWC), |
| 92 | [3, 3], |
| 93 | ) |
| 94 | workstream.sync() |
| 95 | cvcuda.internal.syncAuxStream() |
| 96 | |
| 97 | # Lo and behold, cvwrapper is no more. |
| 98 | # The wrapped tensor torch has the same refcount it had when we've created it. |
| 99 | assert sys.getrefcount(ttensor) == orig_ttensor_refcount |
| 100 | |
| 101 | |
| 102 | def test_cache_limit_get_set(): |