Skip to content

Commit c0bd3a5

Browse files
committed
Fix more minor issues with convolution construction and group norm.
1 parent e27298e commit c0bd3a5

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

lib/nnc/ccv_cnnp_model_addons.c

+17-1
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,23 @@ static void _ccv_cnnp_convolution_build(ccv_cnnp_model_t* const super, ccv_nnc_s
11081108
if (self->format)
11091109
weights_params.format = self->format;
11101110
ccv_nnc_tensor_set_n(&weights_params, self->filters);
1111-
const int c = ccv_nnc_tensor_get_c(params);
1111+
const int a_nd = ccv_nnc_tensor_nd(params.dim);
1112+
int c;
1113+
switch (params.format)
1114+
{
1115+
case CCV_TENSOR_FORMAT_NHWC:
1116+
c = params.dim[a_nd - 1];
1117+
break;
1118+
case CCV_TENSOR_FORMAT_NCHW:
1119+
if (a_nd == k_nd + 1)
1120+
c = params.dim[0];
1121+
else
1122+
c = params.dim[a_nd <= 1 ? 0 : 1];
1123+
break;
1124+
case CCV_TENSOR_FORMAT_CHWN:
1125+
c = params.dim[0];
1126+
break;
1127+
}
11121128
assert(c % self->groups == 0);
11131129
ccv_nnc_tensor_set_c(&weights_params, nd, c / self->groups);
11141130
int hw = -1;

lib/nnc/cmd/norm/gpu/ccv_nnc_group_norm_gpu_cudnn.cu

+8-6
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,13 @@ static int _ccv_nnc_group_norm_forw(const ccv_nnc_cmd_t cmd, const ccv_nnc_hint_
8888
assert(ccv_nnc_tensor_view_check_dim((ccv_nnc_tensor_view_t*)outputs[0], adim));
8989
int x;
9090
int n = 1;
91-
for (x = 0; x < CCV_NNC_MAX_DIM + 2; x++)
91+
const int a_nd = ccv_nnc_tensor_nd(adim);
92+
for (x = 0; x < a_nd; x++)
9293
n *= adim[x];
93-
for (x = 0; x < CCV_NNC_MAX_DIM + 2; x++)
94+
for (x = 0; x < a_nd; x++)
9495
n /= rdim[x];
9596
int rcount = 1;
96-
for (x = 0; x < CCV_NNC_MAX_DIM + 2; x++)
97+
for (x = 0; x < a_nd; x++)
9798
rcount *= rdim[x];
9899
const float inv_n = 1. / n;
99100
cudnnReduceTensorDescriptor_t reduce = ccv_nnc_stream_context_get_reduce_tensor_descriptor(stream_context);
@@ -197,12 +198,13 @@ static int _ccv_nnc_group_norm_back(const ccv_nnc_cmd_t cmd, const ccv_nnc_hint_
197198
static const float one = 1, zero = 0, neg_one = -1;
198199
int x;
199200
int n = 1;
200-
for (x = 0; x < CCV_NNC_MAX_DIM + 2; x++)
201+
const int g_nd = ccv_nnc_tensor_nd(gdim);
202+
for (x = 0; x < g_nd; x++)
201203
n *= gdim[x];
202-
for (x = 0; x < CCV_NNC_MAX_DIM + 2; x++)
204+
for (x = 0; x < g_nd; x++)
203205
n /= rdim[x];
204206
int gcount = 1, rcount = 1;
205-
for (x = 0; x < CCV_NNC_MAX_DIM + 2; x++)
207+
for (x = 0; x < g_nd; x++)
206208
gcount *= gdim[x], rcount *= rdim[x];
207209
const float neg_inv_n = -1. / n;
208210
cudnnReduceTensorDescriptor_t reduce = ccv_nnc_stream_context_get_reduce_tensor_descriptor(stream_context);

0 commit comments

Comments
 (0)