Skip to content

Commit c22cf59

Browse files
Merge pull request #6333 from wanghaoshuang/fix_type
Fix nce op warning about comparison of integers of different signs
2 parents 5a1a04f + 83537c7 commit c22cf59

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

paddle/operators/nce_op.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void PrepareSamples(const framework::ExecutionContext& context) {
4949

5050
int num_label = label_dims.size() == 2 ? label_dims[1] : 1;
5151
int index = 0;
52-
for (size_t i = 0; i < label_dims[0]; ++i) {
52+
for (int64_t i = 0; i < label_dims[0]; ++i) {
5353
int j = 0;
5454
for (; j < num_label; ++j) {
5555
sample_labels_data[index++] = label_data[i * num_label + j];
@@ -86,7 +86,7 @@ class NCEKernel : public framework::OpKernel<T> {
8686
T* out_data = out->mutable_data<T>(context.GetPlace());
8787
int num_neg_samples = context.Attr<int>("num_neg_samples");
8888
int num_total_classes = context.Attr<int>("num_total_classes");
89-
int num_true_class = 1;
89+
int64_t num_true_class = 1;
9090
if (label != nullptr) {
9191
num_true_class = label->dims()[1];
9292
}
@@ -95,18 +95,18 @@ class NCEKernel : public framework::OpKernel<T> {
9595
auto bias = context.Input<Tensor>("Bias");
9696
if (bias != nullptr) {
9797
const T* bias_data = bias->data<T>();
98-
for (size_t i = 0; i < sample_labels->numel(); ++i) {
98+
for (int64_t i = 0; i < sample_labels->numel(); ++i) {
9999
sample_out_data[i] = bias_data[sample_labels_data[i]];
100100
}
101101
} else {
102-
for (size_t i = 0; i < sample_labels->numel(); ++i) {
102+
for (int64_t i = 0; i < sample_labels->numel(); ++i) {
103103
sample_out_data[i] = 0;
104104
}
105105
}
106106
// forward mul
107107
auto input_mat = EigenMatrix<T>::From(*(context.Input<Tensor>("Input")));
108108
auto weight_mat = EigenMatrix<T>::From(*(context.Input<Tensor>("Weight")));
109-
for (size_t i = 0; i < sample_labels->numel(); ++i) {
109+
for (int64_t i = 0; i < sample_labels->numel(); ++i) {
110110
Eigen::Tensor<T, 0, Eigen::RowMajor, Eigen::DenseIndex> result =
111111
(input_mat.chip((int)(i / sample_labels->dims()[1]), 0) *
112112
weight_mat.chip(sample_labels_data[i], 0))
@@ -115,8 +115,8 @@ class NCEKernel : public framework::OpKernel<T> {
115115
sample_out_data[i] = (1. / (1. + exp(-sample_out_data[i])));
116116
}
117117
// forward cost
118-
for (size_t i = 0; i < sample_labels->dims()[0]; ++i) {
119-
size_t j = 0;
118+
for (int64_t i = 0; i < sample_labels->dims()[0]; ++i) {
119+
int64_t j = 0;
120120
out_data[i] = 0;
121121
T w = sample_weight == nullptr ? 1. : sample_weight_data[i];
122122
// for true classes
@@ -162,7 +162,7 @@ class NCEGradKernel : public framework::OpKernel<T> {
162162
T* sample_grad_data =
163163
sample_grad.mutable_data<T>(sample_labels->dims(), context.GetPlace());
164164
// backward cost
165-
for (size_t i = 0; i < sample_labels->numel(); ++i) {
165+
for (int64_t i = 0; i < sample_labels->numel(); ++i) {
166166
T o = sample_out_data[i];
167167
T w = sample_weight == nullptr
168168
? 1
@@ -177,7 +177,7 @@ class NCEGradKernel : public framework::OpKernel<T> {
177177
if (d_bias != nullptr) {
178178
T* d_bias_data = d_bias->mutable_data<T>(context.GetPlace());
179179
std::fill(d_bias_data, d_bias_data + d_bias->numel(), 0.0);
180-
for (size_t i = 0; i < sample_labels->numel(); ++i) {
180+
for (int64_t i = 0; i < sample_labels->numel(); ++i) {
181181
d_bias_data[sample_labels_data[i]] += sample_grad_data[i];
182182
}
183183
}
@@ -188,7 +188,7 @@ class NCEGradKernel : public framework::OpKernel<T> {
188188
std::fill(d_w_data, d_w_data + d_w->numel(), 0.0);
189189
auto d_w_matrix = EigenMatrix<T>::From(*d_w);
190190
auto x_matrix = EigenMatrix<T>::From(*(context.Input<Tensor>("Input")));
191-
for (size_t i = 0; i < sample_labels->numel(); ++i) {
191+
for (int64_t i = 0; i < sample_labels->numel(); ++i) {
192192
d_w_matrix.chip(sample_labels_data[i], 0) +=
193193
x_matrix.chip((int)(i / sample_labels->dims()[1]), 0) *
194194
sample_grad_data[i];
@@ -200,7 +200,7 @@ class NCEGradKernel : public framework::OpKernel<T> {
200200
d_x->mutable_data<T>(context.GetPlace());
201201
auto d_x_matrix = EigenMatrix<T>::From(*d_x);
202202
auto w_matrix = EigenMatrix<T>::From(*(context.Input<Tensor>("Weight")));
203-
for (size_t i = 0; i < sample_labels->numel(); ++i) {
203+
for (int64_t i = 0; i < sample_labels->numel(); ++i) {
204204
d_x_matrix.chip((int)(i / sample_labels->dims()[1]), 0) +=
205205
w_matrix.chip(sample_labels_data[i], 0) * sample_grad_data[i];
206206
}

0 commit comments

Comments
 (0)