提交 2321a37b 编写于 作者: W wanghaoshuang

fix issues

上级 6e964ad5
...@@ -27,7 +27,13 @@ using Tensor = framework::Tensor; ...@@ -27,7 +27,13 @@ using Tensor = framework::Tensor;
template <typename T> template <typename T>
__global__ void ClipGradientKernel(const int N, const T min, const T max, __global__ void ClipGradientKernel(const int N, const T min, const T max,
const T* Y, const T* dY, T* dX) { const T* Y, const T* dY, T* dX) {
CUDA_1D_KERNEL_LOOP(i, N) { dX[i] = dY[i] * (Y[i] > min && Y[i] < max); } CUDA_1D_KERNEL_LOOP(i, N) {
if (Y[i] > min && Y[i] < max) {
dX[i] = dY[i];
} else {
dX[i] = 0;
}
}
} }
template <typename T> template <typename T>
...@@ -38,7 +44,7 @@ class ClipGradientOpCUDAKernel : public framework::OpKernel { ...@@ -38,7 +44,7 @@ class ClipGradientOpCUDAKernel : public framework::OpKernel {
auto min = context.op().Attr<float>("min"); auto min = context.op().Attr<float>("min");
auto* d_out = context.Input<Tensor>(framework::GradVarName("Out")); auto* d_out = context.Input<Tensor>(framework::GradVarName("Out"));
auto* d_x = context.Output<Tensor>(framework::GradVarName("X")); auto* d_x = context.Output<Tensor>(framework::GradVarName("X"));
auto* x = context.Output<Tensor>("X"); auto* x = context.Input<Tensor>("X");
auto dims = d_x->dims(); auto dims = d_x->dims();
size_t count = 1; size_t count = 1;
for (int i = 0; i < dims.size(); ++i) { for (int i = 0; i < dims.size(); ++i) {
......
...@@ -50,7 +50,7 @@ class ClipGradKernel : public framework::OpKernel { ...@@ -50,7 +50,7 @@ class ClipGradKernel : public framework::OpKernel {
auto min = context.op().Attr<float>("min"); auto min = context.op().Attr<float>("min");
auto* d_out = context.Input<Tensor>(framework::GradVarName("Out")); auto* d_out = context.Input<Tensor>(framework::GradVarName("Out"));
auto* d_x = context.Output<Tensor>(framework::GradVarName("X")); auto* d_x = context.Output<Tensor>(framework::GradVarName("X"));
auto* x = context.Output<Tensor>("X"); auto* x = context.Input<Tensor>("X");
auto dims = d_x->dims(); auto dims = d_x->dims();
size_t count = 1; size_t count = 1;
for (int i = 0; i < dims.size(); ++i) { for (int i = 0; i < dims.size(); ++i) {
......
...@@ -238,6 +238,7 @@ class GradientChecker(unittest.TestCase): ...@@ -238,6 +238,7 @@ class GradientChecker(unittest.TestCase):
:type msf_prefix: string :type msf_prefix: string
""" """
for a, b, name in itertools.izip(numeric_grads, analytic_grads, names): for a, b, name in itertools.izip(numeric_grads, analytic_grads, names):
print "a=%s ; b=%s" % (a, b)
abs_a = numpy.abs(a) abs_a = numpy.abs(a)
# if abs_a is nearly zero, then use abs error for a, not relative # if abs_a is nearly zero, then use abs error for a, not relative
# error. # error.
......
...@@ -34,10 +34,8 @@ class OpTestMeta(type): ...@@ -34,10 +34,8 @@ class OpTestMeta(type):
arr = self.inputs[in_name] arr = self.inputs[in_name]
var.set_dims(arr.shape) var.set_dims(arr.shape)
var.set(arr, place) var.set(arr, place)
print "var: %s" % in_name
else: else:
kwargs[in_name] = "@EMPTY@" kwargs[in_name] = "@EMPTY@"
print "var: %s=EMPTY" % in_name
for out_name in Operator.get_op_output_names(self.type): for out_name in Operator.get_op_output_names(self.type):
if not hasattr(self, "outputs"): if not hasattr(self, "outputs"):
...@@ -48,7 +46,6 @@ class OpTestMeta(type): ...@@ -48,7 +46,6 @@ class OpTestMeta(type):
(out_name)) (out_name))
kwargs[out_name] = out_name kwargs[out_name] = out_name
scope.new_var(out_name).get_tensor() scope.new_var(out_name).get_tensor()
print "var: %s" % out_name
for attr_name in Operator.get_op_attr_names(self.type): for attr_name in Operator.get_op_attr_names(self.type):
if hasattr(self, "attrs") and attr_name in self.attrs: if hasattr(self, "attrs") and attr_name in self.attrs:
...@@ -65,9 +62,7 @@ class OpTestMeta(type): ...@@ -65,9 +62,7 @@ class OpTestMeta(type):
for out_name in Operator.get_op_output_names(self.type): for out_name in Operator.get_op_output_names(self.type):
actual = numpy.array(scope.find_var(out_name).get_tensor()) actual = numpy.array(scope.find_var(out_name).get_tensor())
print "actual: %s" % actual
expect = self.outputs[out_name] expect = self.outputs[out_name]
print "expect: %s" % expect
self.assertTrue( self.assertTrue(
numpy.allclose( numpy.allclose(
actual, expect, atol=1e-05), actual, expect, atol=1e-05),
......
...@@ -5,12 +5,13 @@ from gradient_checker import GradientChecker ...@@ -5,12 +5,13 @@ from gradient_checker import GradientChecker
from op_test_util import OpTestMeta from op_test_util import OpTestMeta
class TestClipOp(unittest.TestCase): class ClipOp(unittest.TestCase):
__metaclass__ = OpTestMeta __metaclass__ = OpTestMeta
def setUp(self): def setUp(self):
input = np.random.random((16, 16)).astype("float32") input = np.random.random((16, 16)).astype("float32")
print "input: %s" % input input[np.abs(input - 0.1) < 0.05] = 0.5
input[np.abs(input - 0.9) < 0.05] = 0.5
self.type = "clip" self.type = "clip"
self.inputs = {'X': input, } self.inputs = {'X': input, }
self.attrs = {} self.attrs = {}
...@@ -24,14 +25,16 @@ class TestClipOp(unittest.TestCase): ...@@ -24,14 +25,16 @@ class TestClipOp(unittest.TestCase):
class TestClipGradOp(GradientChecker): class TestClipGradOp(GradientChecker):
def setUp(self): def setUp(self):
input = np.random.random((8, 8)).astype("float32")
print "input: %s" % input
self.op = Operator(type="clip", X="X", Out="Out", min=0.1, max=0.9) self.op = Operator(type="clip", X="X", Out="Out", min=0.1, max=0.9)
self.inputs = {'X': np.random.random((16, 16)).astype("float32"), } self.inputs = {'X': input, }
def test_normal(self): def test_normal(self):
self.check_grad( self.check_grad(
self.op, self.inputs, set(["X"]), "Out", max_relative_error=0.5) self.op, self.inputs, set(["X"]), "Out", max_relative_error=0.5)
def test_cpu_gpu_compare(self): def t_cpu_gpu_compare(self):
self.compare_grad(self.op, self.inputs) self.compare_grad(self.op, self.inputs)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册