Skip to content

Commit c72270c

Browse files
authored
Init tensor directly on device (#6068)
Slightly more efficient than .to(device)
1 parent afa5cfb commit c72270c

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

models/yolo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ def forward(self, x):
7272
def _make_grid(self, nx=20, ny=20, i=0):
7373
d = self.anchors[i].device
7474
if check_version(torch.__version__, '1.10.0'): # torch>=1.10.0 meshgrid workaround for torch>=0.7 compatibility
75-
yv, xv = torch.meshgrid([torch.arange(ny).to(d), torch.arange(nx).to(d)], indexing='ij')
75+
yv, xv = torch.meshgrid([torch.arange(ny, device=d), torch.arange(nx, device=d)], indexing='ij')
7676
else:
77-
yv, xv = torch.meshgrid([torch.arange(ny).to(d), torch.arange(nx).to(d)])
77+
yv, xv = torch.meshgrid([torch.arange(ny, device=d), torch.arange(nx, device=d)])
7878
grid = torch.stack((xv, yv), 2).expand((1, self.na, ny, nx, 2)).float()
7979
anchor_grid = (self.anchors[i].clone() * self.stride[i]) \
8080
.view((1, self.na, 1, 1, 2)).expand((1, self.na, ny, nx, 2)).float()

0 commit comments

Comments
 (0)