torchfix icon indicating copy to clipboard operation
torchfix copied to clipboard

Use torch.*_like()

Open adsharma opened this issue 1 year ago • 2 comments

b = torch.zeros_like(a)

should be preferred over

b = torch.zeros(...).to(a)

to avoid copies.

adsharma avatar Jul 11 '24 20:07 adsharma

Agree. It might be problematic to figure out statically when a in .to(a) is a tensor, and not a dtype or device. Do you have any real-world examples?

kit1980 avatar Jul 12 '24 00:07 kit1980

https://github.com/threestudio-project/threestudio/blob/main/threestudio/models/background/solid_color_background.py#L37-L39

adsharma avatar Jul 12 '24 14:07 adsharma