Repository navigation
Expand file tree
/
Copy pathtf_sampling.cpp
More file actions
executable file
·135 lines (130 loc) · 6.54 KB
/
Copy pathtf_sampling.cpp
File metadata and controls
executable file
·135 lines (130 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/op_kernel.h"
REGISTER_OP("ProbSample")
.Input("inp: float32")
.Input("inpr: float32")
.Output("out: int32");
REGISTER_OP("FarthestPointSample")
.Input("npoint: int32")
.Input("inp: float32")
.Output("out: int32");
REGISTER_OP("GatherPoint")
.Input("inp: float32")
.Input("idx: int32")
.Output("out: float32");
REGISTER_OP("GatherPointGrad")
.Input("inp: float32")
.Input("idx: int32")
.Input("out_g: float32")
.Output("inp_g: float32");
#include <cuda_runtime.h>
using namespace tensorflow;
void probsampleLauncher(int b,int n,int m,const float * inp_p,const float * inp_r,float * temp,int * out);
class ProbSampleGpuOp: public OpKernel{
public:
explicit ProbSampleGpuOp(OpKernelConstruction* context):OpKernel(context){}
void Compute(OpKernelContext * context)override{
const Tensor& inp_tensor=context->input(0);
const Tensor& inpr_tensor=context->input(1);
auto inp_flat=inp_tensor.flat<float>();
auto inpr_flat=inpr_tensor.flat<float>();
const float * inp=&(inp_flat(0));
const float * inpr=&(inpr_flat(0));
OP_REQUIRES(context,inp_tensor.dims()==2,errors::InvalidArgument("ProbSample expects (batch_size,num_choices) inp shape"));
int b=inp_tensor.shape().dim_size(0);
int n=inp_tensor.shape().dim_size(1);
OP_REQUIRES(context,inpr_tensor.dims()==2 && inpr_tensor.shape().dim_size(0)==b,errors::InvalidArgument("ProbSample expects (batch_size,num_points) inpr shape"));
int m=inpr_tensor.shape().dim_size(1);
//printf("b=%d n=%d m=%d\n",b,n,m);
Tensor * out_tensor=NULL;
OP_REQUIRES_OK(context,context->allocate_output(0,TensorShape{b,m},&out_tensor));
auto out_flat=out_tensor->flat<int>();
int * out=&(out_flat(0));
Tensor temp_tensor;
OP_REQUIRES_OK(context,context->allocate_temp(DataTypeToEnum<float>::value,TensorShape{b,n},&temp_tensor));
auto temp_flat=temp_tensor.flat<float>();
float * temp=&(temp_flat(0));
probsampleLauncher(b,n,m,inp,inpr,temp,out);
}
};
REGISTER_KERNEL_BUILDER(Name("ProbSample").Device(DEVICE_GPU), ProbSampleGpuOp);
void farthestpointsamplingLauncher(int b,int n,int m,const float * inp,float * temp,int * out);
class FarthestPointSampleGpuOp: public OpKernel{
public:
explicit FarthestPointSampleGpuOp(OpKernelConstruction* context):OpKernel(context){}
void Compute(OpKernelContext * context)override{
const Tensor& npoint_tensor=context->input(0);
OP_REQUIRES(context,IsLegacyScalar(npoint_tensor.shape()),errors::InvalidArgument("FarthestPointSample expects scalar npoint"));
int m;
cudaMemcpy(&m,&npoint_tensor.scalar<int>()(),4,cudaMemcpyDeviceToHost);
const Tensor& inp_tensor=context->input(1);
OP_REQUIRES(context,inp_tensor.dims()==3 && inp_tensor.shape().dim_size(2)==3,errors::InvalidArgument("FarthestPointSample expects (batch_size,num_points,3) inp shape"));
int b=inp_tensor.shape().dim_size(0);
int n=inp_tensor.shape().dim_size(1);
auto inp_flat=inp_tensor.flat<float>();
const float * inp=&(inp_flat(0));
Tensor * out_tensor;
OP_REQUIRES_OK(context,context->allocate_output(0,TensorShape{b,m},&out_tensor));
auto out_flat=out_tensor->flat<int>();
int * out=&(out_flat(0));
Tensor temp_tensor;
OP_REQUIRES_OK(context,context->allocate_temp(DataTypeToEnum<float>::value,TensorShape{32,n},&temp_tensor));
auto temp_flat=temp_tensor.flat<float>();
float * temp=&(temp_flat(0));
farthestpointsamplingLauncher(b,n,m,inp,temp,out);
}
};
REGISTER_KERNEL_BUILDER(Name("FarthestPointSample").Device(DEVICE_GPU),FarthestPointSampleGpuOp);
void gatherpointLauncher(int b,int n,int m,const float * inp,const int * idx,float * out);
class GatherPointGpuOp: public OpKernel{
public:
explicit GatherPointGpuOp(OpKernelConstruction * context):OpKernel(context){}
void Compute(OpKernelContext * context)override{
const Tensor& inp_tensor=context->input(0);
OP_REQUIRES(context,inp_tensor.dims()==3 && inp_tensor.shape().dim_size(2)==3,errors::InvalidArgument("GatherPoint expects (batch_size,num_points,3) inp shape"));
int b=inp_tensor.shape().dim_size(0);
int n=inp_tensor.shape().dim_size(1);
const Tensor& idx_tensor=context->input(1);
OP_REQUIRES(context,idx_tensor.dims()==2 && idx_tensor.shape().dim_size(0)==b,errors::InvalidArgument("GatherPoint expects (batch_size,num_result) idx shape"));
int m=idx_tensor.shape().dim_size(1);
auto inp_flat=inp_tensor.flat<float>();
const float * inp=&(inp_flat(0));
auto idx_flat=idx_tensor.flat<int>();
const int * idx=&(idx_flat(0));
Tensor * out_tensor=NULL;
OP_REQUIRES_OK(context,context->allocate_output(0,TensorShape{b,m,3},&out_tensor));
auto out_flat=out_tensor->flat<float>();
float * out=&(out_flat(0));
gatherpointLauncher(b,n,m,inp,idx,out);
}
};
REGISTER_KERNEL_BUILDER(Name("GatherPoint").Device(DEVICE_GPU),GatherPointGpuOp);
void scatteraddpointLauncher(int b,int n,int m,const float * out_g,const int * idx,float * inp_g);
class GatherPointGradGpuOp: public OpKernel{
public:
explicit GatherPointGradGpuOp(OpKernelConstruction * context):OpKernel(context){}
void Compute(OpKernelContext * context)override{
const Tensor& inp_tensor=context->input(0);
OP_REQUIRES(context,inp_tensor.dims()==3 && inp_tensor.shape().dim_size(2)==3,errors::InvalidArgument("GatherPointGradGpuOp expects (batch_size,num_points,3) inp"));
int b=inp_tensor.shape().dim_size(0);
int n=inp_tensor.shape().dim_size(1);
const Tensor& idx_tensor=context->input(1);
OP_REQUIRES(context,idx_tensor.dims()==2 && idx_tensor.shape().dim_size(0)==b,errors::InvalidArgument("GatherPointGradGpuOp expects (batch_size,num_result) idx shape"));
int m=idx_tensor.shape().dim_size(1);
auto inp_flat=inp_tensor.flat<float>();
const float * inp=&(inp_flat(0));
auto idx_flat=idx_tensor.flat<int>();
const int * idx=&(idx_flat(0));
const Tensor& out_g_tensor=context->input(2);
OP_REQUIRES(context,out_g_tensor.dims()==3 && out_g_tensor.shape().dim_size(0)==b && out_g_tensor.shape().dim_size(1)==m && out_g_tensor.shape().dim_size(2)==3,errors::InvalidArgument("GatherPointGradGpuOp expects (batch_size,num_result,3) out_g shape"));
auto out_g_flat=out_g_tensor.flat<float>();
const float * out_g=&(out_g_flat(0));
Tensor * inp_g_tensor=NULL;
OP_REQUIRES_OK(context,context->allocate_output(0,TensorShape{b,n,3},&inp_g_tensor));
auto inp_g_flat=inp_g_tensor->flat<float>();
float * inp_g=&(inp_g_flat(0));
cudaMemset(inp_g,0,b*n*3*4);
scatteraddpointLauncher(b,n,m,out_g,idx,inp_g);
}
};
REGISTER_KERNEL_BUILDER(Name("GatherPointGrad").Device(DEVICE_GPU),GatherPointGradGpuOp);