mlfoundations / open_clip

An open source implementation of CLIP.
Other
9.29k stars 923 forks source link

demo code will cause memory leak #803

Closed ltebean closed 5 months ago

ltebean commented 5 months ago

In the demo, this line of code will cause memory leak

image_features /= image_features.norm(dim=-1, keepdim=True)

reason:

The reason behind this is related to the behavior of the PyTorch library. In PyTorch, operations like /= and *= perform in-place operations, meaning they modify the original tensor. On the other hand, operations like = create a new tensor and assign it to the variable.

When you use /= and *= for normalizing the image features, you are modifying the original tensor in place. This can sometimes lead to unexpected memory management issues, especially when dealing with large tensors or when the same code is executed multiple times.

suggestion: update the code to

image_features = image_features / image_features.norm(dim=-1, keepdim=True)
rwightman commented 5 months ago

@ltebean it's not going to cause any problems as used the demo, it's doing an inplace div of a tensor that was just returned by the model, it saves an allocation, the tensor is not used in any other scope in this situation.