Skip to content

Commit 32e05b0

Browse files
committed
test=develop
1 parent c8801e1 commit 32e05b0

File tree

4 files changed

+40
-31
lines changed

4 files changed

+40
-31
lines changed

paddle/fluid/operators/hierarchical_sigmoid_op.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class HierarchicalSigmoidOpKernel : public framework::OpKernel<T> {
8686
trans(ctx.template device_context<DeviceContext>(), pre_out_data,
8787
pre_out_data + pre_out->numel(), pre_out_data,
8888
ClipFunctor<T>(static_cast<T>(-40.0), static_cast<T>(40.0)));
89+
pre_out_mat = -1 * pre_out_mat;
8990
bit_code->Sum(*pre_out, out, static_cast<T>(-1));
9091
// use softrelu to calculate cross entropy
9192
pre_out_mat.device(place) = (static_cast<T>(1.0) + pre_out_mat.exp()).log();
@@ -146,6 +147,7 @@ class HierarchicalSigmoidGradOpKernel : public framework::OpKernel<T> {
146147
auto pre_out_mat = EigenMatrix<T>::From(*pre_out);
147148
auto pre_out_grad_mat = EigenMatrix<T>::From(pre_out_grad);
148149
auto out_grad_mat = EigenMatrix<T>::From(*out_grad);
150+
149151
Eigen::array<int, 2> bcast({{1, static_cast<int>(pre_out_grad.dims()[1])}});
150152

151153
// softrelu derivative
@@ -160,9 +162,16 @@ class HierarchicalSigmoidGradOpKernel : public framework::OpKernel<T> {
160162
bias_grad->mutable_data<T>(ctx.GetPlace());
161163
zero(dev_ctx, bias_grad, static_cast<T>(0.0));
162164
bit_code->AddGrad(pre_out_grad, bias_grad);
165+
auto bias_grad_mat = EigenMatrix<T>::From(*bias_grad);
166+
bias_grad_mat = -1 * bias_grad_mat;
163167
}
164168
bit_code->MulGradWeight(pre_out_grad, w_grad, *in);
165169
bit_code->MulGradError(pre_out_grad, *w, in_grad);
170+
auto w_grad_mat = EigenMatrix<T>::From(*w_grad);
171+
auto in_grad_mat = EigenMatrix<T>::From(*in_grad);
172+
173+
w_grad_mat = -1 * w_grad_mat;
174+
in_grad_mat = -1 * in_grad_mat;
166175
}
167176
};
168177

paddle/fluid/operators/math/matrix_bit_code.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class CustomCode : public Code {
157157
int get_length() const {
158158
int length = 0;
159159

160-
for (int i = 0; i < ptable_->dims()[1]; i++) {
160+
for (int i = 0; i < static_cast<int>(ptable_->dims()[1]); i++) {
161161
if (ptable_->data<R>()[index_ * static_cast<int>(ptable_->dims()[1]) +
162162
i] != -1) {
163163
length++;

python/paddle/fluid/tests/unittests/op_test.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,8 @@ def setUpClass(cls):
138138
cls.dtype = "float32"
139139
cls.outputs = {}
140140

141-
# np.random.seed(123)
142-
# random.seed(124)
143-
144-
np.random.seed(190)
145-
random.seed(200)
141+
np.random.seed(123)
142+
random.seed(124)
146143

147144
@classmethod
148145
def tearDownClass(cls):

python/paddle/fluid/tests/unittests/test_hsigmoid_op.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import unittest
1818
import numpy as np
1919
import math
20+
# import paddle.fluid as fluid
21+
# import paddle.fluid.core as core
22+
# from op_builder import OpBuilder
2023
from op_test import OpTest
2124

2225
np.random.seed(100)
@@ -51,7 +54,7 @@ def cal_index(self, bit):
5154

5255
def get_length(self):
5356
length = 0
54-
for ele in self.ptable_[self.index_]:
57+
for ele in self.ptable_[self.index_]: # find the first -1 to stop trace
5558

5659
if ele >= 0:
5760
length = length + 1
@@ -71,12 +74,10 @@ def hsigmoid(x, w, label, bias, num_classes):
7174
pre_sum = np.zeros((batch_size, 1))
7275
out = np.zeros((batch_size, 1)).astype("float32")
7376
for i in range(batch_size):
74-
#print("\n leaf {leaf}: \n".format(leaf = label[i]))
7577
code_table = CodeTable(num_classes, label[i])
7678
length = code_table.get_length()
7779
for j in range(length):
7880
idx = code_table.cal_index(j)
79-
#print("index {index} ".format(index = j))
8081
pre_output[i][j] += bias[0][idx]
8182
for i in range(batch_size):
8283
code_table = CodeTable(num_classes, label[i])
@@ -87,13 +88,12 @@ def hsigmoid(x, w, label, bias, num_classes):
8788
# clip[-40.0, 40.0]
8889
pre_output = np.clip(pre_output, -40.0, 40.0)
8990
# out(i, 0) = \sum_j bit(i, j) * preout(i, j)
91+
pre_output = -1 * pre_output
9092
for i in range(batch_size):
91-
#print("\n leaf {leaf}: \n".format(leaf = label[i]))
9293
code_table = CodeTable(num_classes, label[i])
9394
length = code_table.get_length()
9495
sum = 0.0
9596
for j in range(length):
96-
#print("bit {bit} ".format(bit = code_table.cal_bit(j)))
9797
if code_table.cal_bit(j):
9898
sum += pre_output[i][j]
9999
out[i] = -1.0 * sum
@@ -108,6 +108,7 @@ def hsigmoidWithCustomTree(x, w, ptable, pcode, label, bias, num_classes):
108108
batch_size = x.shape[0]
109109
code_length = len(ptable[0])
110110
code_table = [0 for _ in range(code_length)]
111+
# init pre_out with shape [N, code_length]
111112
pre_output = np.zeros((batch_size, code_length))
112113
pre_sum = np.zeros((batch_size, 1))
113114
out = np.zeros((batch_size, 1)).astype("float32")
@@ -125,6 +126,7 @@ def hsigmoidWithCustomTree(x, w, ptable, pcode, label, bias, num_classes):
125126
pre_output[i][j] += np.dot(w[idx], x[i])
126127
# clip[-40.0, 40.0]
127128
pre_output = np.clip(pre_output, -40.0, 40.0)
129+
pre_output = -1 * pre_output
128130
# out(i, 0) = \sum_j bit(i, j) * preout(i, j)
129131
for i in range(batch_size):
130132
code_table = CodeTableWithCustomTree(ptable, pcode, i)
@@ -141,26 +143,27 @@ def hsigmoidWithCustomTree(x, w, ptable, pcode, label, bias, num_classes):
141143
return pre_output, out
142144

143145

144-
# class TestHSigmoidOp(OpTest):
145-
# def setUp(self):
146-
# self.op_type = "hierarchical_sigmoid"
147-
# num_classes = 6
148-
# feature_size = 8
149-
# batch_size = 7
150-
# x = np.random.random((batch_size, feature_size)).astype("float32")
151-
# w = np.random.random((num_classes - 1, feature_size)).astype("float32")
152-
# label = np.random.randint(0, num_classes, (batch_size, 1))
153-
# bias = np.random.random((1, num_classes - 1)).astype("float32")
154-
# self.attrs = {'num_classes': num_classes}
155-
# self.inputs = {'X': x, 'W': w, 'Label': label, 'Bias': bias}
156-
# pre_output, out = hsigmoid(x, w, label, bias, num_classes)
157-
# self.outputs = {'PreOut': pre_output, 'Out': out}
146+
class TestHSigmoidOp(OpTest):
147+
def setUp(self):
148+
self.op_type = "hierarchical_sigmoid"
149+
num_classes = 6
150+
feature_size = 8
151+
batch_size = 4
152+
x = np.random.random((batch_size, feature_size)).astype("float32") * 2
153+
w = np.random.random(
154+
(num_classes - 1, feature_size)).astype("float32") * 2
155+
label = np.random.randint(0, num_classes, (batch_size, 1))
156+
bias = np.random.random((1, num_classes - 1)).astype("float32")
157+
self.attrs = {'num_classes': num_classes}
158+
self.inputs = {'X': x, 'W': w, 'Label': label, 'Bias': bias}
159+
pre_output, out = hsigmoid(x, w, label, bias, num_classes)
160+
self.outputs = {'PreOut': pre_output, 'Out': out}
158161

159-
# def test_check_output(self):
160-
# self.check_output()
162+
def test_check_output(self):
163+
self.check_output()
161164

162-
# def test_check_grad(self):
163-
# self.check_grad(['Bias', 'X', 'W'], ['Out'], no_grad_set=set('Label'))
165+
def test_check_grad(self):
166+
self.check_grad(['Bias', 'X', 'W'], ['Out'], no_grad_set=set('Label'))
164167

165168

166169
class TestHSigmoidOpWithCostumTree(OpTest):
@@ -169,9 +172,9 @@ def setUp(self):
169172
num_classes = 6 #using 1,2,3,4,5,6 to build a huffman tree and select 1,2,5,6 as sample
170173
feature_size = 8
171174
batch_size = 4
172-
x = np.random.random((batch_size, feature_size)).astype("float32") * 10
175+
x = np.random.random((batch_size, feature_size)).astype("float32") * 2
173176
w = np.random.random(
174-
(num_classes - 1, feature_size)).astype("float32") * 10
177+
(num_classes - 1, feature_size)).astype("float32") * 2
175178
label = np.array([0, 1, 4, 5])
176179
ptable = np.array(
177180
[(0, 2, -1, -1, -1), (0, 1, 3, -1, -1), (0, 1, 4, -1, -1),

0 commit comments

Comments
 (0)