Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fix in the function update_probs and cuda device issue in sparseconvunet and bug fix in cuda device issue in pointtransformer #644

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 47 additions & 24 deletions ml3d/torch/models/point_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class PointTransformer(BaseModel):
"""

def __init__(self,
device,
name="PointTransformer",
blocks=[2, 2, 2, 2, 2],
in_channels=6,
Expand All @@ -52,6 +53,7 @@ def __init__(self,
batcher=batcher,
augment=augment,
**kwargs)
self.device = torch.device(device)
cfg = self.cfg
self.in_channels = in_channels
self.augmenter = SemsegAugmentation(cfg.augment)
Expand Down Expand Up @@ -108,12 +110,13 @@ def _make_enc(self,
"""
layers = []
layers.append(
TransitionDown(self.in_planes, planes * block.expansion, stride,
TransitionDown(self.device, self.in_planes, planes * block.expansion, stride,
nsample))
self.in_planes = planes * block.expansion
for _ in range(1, blocks):
layers.append(
block(self.in_planes,
block(self.device,
self.in_planes,
self.in_planes,
share_planes,
nsample=nsample))
Expand Down Expand Up @@ -141,12 +144,14 @@ def _make_dec(self,
"""
layers = []
layers.append(
TransitionUp(self.in_planes,
TransitionUp(self.device,
self.in_planes,
None if is_head else planes * block.expansion))
self.in_planes = planes * block.expansion
for _ in range(1, blocks):
layers.append(
block(self.in_planes,
block(self.device,
self.in_planes,
self.in_planes,
share_planes,
nsample=nsample))
Expand Down Expand Up @@ -377,7 +382,7 @@ def get_optimizer(self, cfg_pipeline):
class Transformer(nn.Module):
"""Transformer layer of the model, uses self attention."""

def __init__(self, in_planes, out_planes, share_planes=8, nsample=16):
def __init__(self, device, in_planes, out_planes, share_planes=8, nsample=16):
"""Constructor for Transformer Layer.

Args:
Expand All @@ -388,6 +393,7 @@ def __init__(self, in_planes, out_planes, share_planes=8, nsample=16):

"""
super().__init__()
self.device = device
self.mid_planes = mid_planes = out_planes // 1
self.out_planes = out_planes
self.share_planes = share_planes
Expand Down Expand Up @@ -427,15 +433,17 @@ def forward(self, pxo):
point, feat, row_splits = pxo # (n, 3), (n, c), (b)
feat_q, feat_k, feat_v = self.linear_q(feat), self.linear_k(
feat), self.linear_v(feat) # (n, c)
feat_k = queryandgroup(self.nsample,
feat_k = queryandgroup(self.device,
self.nsample,
point,
point,
feat_k,
None,
row_splits,
row_splits,
use_xyz=True) # (n, nsample, 3+c)
feat_v = queryandgroup(self.nsample,
feat_v = queryandgroup(self.device,
self.nsample,
point,
point,
feat_v,
Expand Down Expand Up @@ -473,7 +481,7 @@ class TransitionDown(nn.Module):
Subsamples points and increase receptive field.
"""

def __init__(self, in_planes, out_planes, stride=1, nsample=16):
def __init__(self, device, in_planes, out_planes, stride=1, nsample=16):
"""Constructor for TransitionDown Layer.

Args:
Expand All @@ -484,6 +492,7 @@ def __init__(self, in_planes, out_planes, stride=1, nsample=16):

"""
super().__init__()
self.device = device
self.stride, self.nsample = stride, nsample
if stride != 1:
self.linear = nn.Linear(3 + in_planes, out_planes, bias=False)
Expand All @@ -504,7 +513,13 @@ def forward(self, pxo):
List of point, feat, row_splits.

"""

point, feat, row_splits = pxo # (n, 3), (n, c), (b+1)
feat = torch.tensor(feat, device=self.device)
row_splits = torch.tensor(row_splits, device=self.device)
point = torch.tensor(point, device=self.device)


if self.stride != 1:
new_row_splits = [0]
count = 0
Expand All @@ -513,12 +528,13 @@ def forward(self, pxo):
row_splits[i - 1].item()) // self.stride
new_row_splits.append(count)

new_row_splits = torch.LongTensor(new_row_splits).to(
row_splits.device)
new_row_splits = torch.LongTensor(new_row_splits).to(self.device)
#new_row_splits = torch.LongTensor(new_row_splits).to(row_splits.device)
idx = furthest_point_sample_v2(point, row_splits,
new_row_splits) # (m)
new_point = point[idx.long(), :] # (m, 3)
feat = queryandgroup(self.nsample,
feat = queryandgroup(self.device,
self.nsample,
point,
new_point,
feat,
Expand All @@ -532,7 +548,8 @@ def forward(self, pxo):
feat = self.pool(feat).squeeze(-1) # (m, c)
point, row_splits = new_point, new_row_splits
else:
feat = self.relu(self.bn(self.linear(feat))) # (n, c)
feat = torch.tensor(feat, device=self.device)
feat = self.relu(self.bn(self.linear(feat))) # (n, c)
return [point, feat, row_splits]


Expand All @@ -542,7 +559,7 @@ class TransitionUp(nn.Module):
Interpolate points based on corresponding encoder layer.
"""

def __init__(self, in_planes, out_planes=None):
def __init__(self, device, in_planes, out_planes=None):
"""Constructor for TransitionUp Layer.

Args:
Expand All @@ -551,6 +568,7 @@ def __init__(self, in_planes, out_planes=None):

"""
super().__init__()
self.device = device
if out_planes is None:
self.linear1 = nn.Sequential(nn.Linear(2 * in_planes, in_planes),
nn.BatchNorm1d(in_planes),
Expand Down Expand Up @@ -595,7 +613,7 @@ def forward(self, pxo1, pxo2=None):
point_1, feat_1, row_splits_1 = pxo1
point_2, feat_2, row_splits_2 = pxo2
feat = self.linear1(feat_1) + interpolation(
point_2, point_1, self.linear2(feat_2), row_splits_2,
self.device, point_2, point_1, self.linear2(feat_2), row_splits_2,
row_splits_1)
return feat

Expand All @@ -607,7 +625,7 @@ class Bottleneck(nn.Module):
"""
expansion = 1

def __init__(self, in_planes, planes, share_planes=8, nsample=16):
def __init__(self, device, in_planes, planes, share_planes=8, nsample=16):
"""Constructor for Bottleneck Layer.

Args:
Expand All @@ -620,7 +638,7 @@ def __init__(self, in_planes, planes, share_planes=8, nsample=16):
super(Bottleneck, self).__init__()
self.linear1 = nn.Linear(in_planes, planes, bias=False)
self.bn1 = nn.BatchNorm1d(planes)
self.transformer2 = Transformer(planes, planes, share_planes, nsample)
self.transformer2 = Transformer(device, planes, planes, share_planes, nsample)
self.bn2 = nn.BatchNorm1d(planes)
self.linear3 = nn.Linear(planes, planes * self.expansion, bias=False)
self.bn3 = nn.BatchNorm1d(planes * self.expansion)
Expand All @@ -647,7 +665,8 @@ def forward(self, pxo):
return [point, feat, row_splits]


def queryandgroup(nsample,
def queryandgroup(device,
nsample,
points,
queries,
feat,
Expand Down Expand Up @@ -677,7 +696,8 @@ def queryandgroup(nsample,
if queries is None:
queries = points
if idx is None:
idx = knn_batch(points,
idx = knn_batch(device,
points,
queries,
k=nsample,
points_row_splits=points_row_splits,
Expand All @@ -697,7 +717,8 @@ def queryandgroup(nsample,
return grouped_feat


def knn_batch(points,
def knn_batch(device,
points,
queries,
k,
points_row_splits,
Expand Down Expand Up @@ -729,12 +750,13 @@ def knn_batch(points,
return_distances=True)
if return_distances:
return ans.neighbors_index.reshape(
-1, k).long().cuda(), ans.neighbors_distance.reshape(-1, k).cuda()
-1, k).long().to(device), ans.neighbors_distance.reshape(-1, k).to(device)
else:
return ans.neighbors_index.reshape(-1, k).long().cuda()
return ans.neighbors_index.reshape(-1, k).long().to(device)


def interpolation(points,
def interpolation(device,
points,
queries,
feat,
points_row_splits,
Expand All @@ -756,7 +778,8 @@ def interpolation(points,
if not (points.is_contiguous and queries.is_contiguous() and
feat.is_contiguous()):
raise ValueError("Interpolation (points/queries/feat not contiguous)")
idx, dist = knn_batch(points,
idx, dist = knn_batch(device,
points,
queries,
k=k,
points_row_splits=points_row_splits,
Expand All @@ -770,7 +793,7 @@ def interpolation(points,
weight = dist_recip / norm # (n, k)

new_feat = torch.FloatTensor(queries.shape[0],
feat.shape[1]).zero_().to(feat.device)
feat.shape[1]).zero_().to(device)
for i in range(k):
new_feat += feat[idx[:, i].long(), :] * weight[:, i].unsqueeze(-1)
return new_feat
Loading