Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support that the dtype of ValueTensor and Attr(dtype) are different #24961

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions paddle/fluid/operators/fill_constant_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,33 @@ class FillConstantKernel : public framework::OpKernel<T> {
"When use Tensor as value to set Tensor value in fill_cosntant, "
"value input(ValueTensor) size must be 1, but get %d",
value_tensor->numel()));
const T *tensor_data = value_tensor->data<T>();
framework::Tensor cpu_tensor;

auto value_type = value_tensor->type();
framework::Tensor *cpu_tensor = nullptr;
if (platform::is_gpu_place(value_tensor->place())) {
TensorCopySync(*value_tensor, platform::CPUPlace(), &cpu_tensor);
tensor_data = cpu_tensor.data<T>();
TensorCopySync(*value_tensor, platform::CPUPlace(), cpu_tensor);
}
if (value_type == framework::proto::VarType::INT32) {
const auto *tensor_data =
cpu_tensor ? cpu_tensor->data<int>() : value_tensor->data<int>();
value = static_cast<T>(tensor_data[0]);
} else if (value_type == framework::proto::VarType::INT64) {
const auto *tensor_data = cpu_tensor ? cpu_tensor->data<int64_t>()
: value_tensor->data<int64_t>();
value = static_cast<T>(tensor_data[0]);
} else if (value_type == framework::proto::VarType::FP32) {
const auto *tensor_data = cpu_tensor ? cpu_tensor->data<float>()
: value_tensor->data<float>();
value = static_cast<T>(tensor_data[0]);
} else if (value_type == framework::proto::VarType::FP64) {
const auto *tensor_data = cpu_tensor ? cpu_tensor->data<double>()
: value_tensor->data<double>();
value = static_cast<T>(tensor_data[0]);
} else {
const auto *tensor_data =
cpu_tensor ? cpu_tensor->data<T>() : value_tensor->data<T>();
Copy link
Contributor

@wangchaochaohu wangchaochaohu Jun 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里可以考虑改写下 比如tesor_data 在外面声明 然后 最后value 统一赋值?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的精度损失问题?

value = static_cast<T>(tensor_data[0]);
}
value = tensor_data[0];
}
const std::string op_type = "fill_constant";
auto shape = GetShape(ctx, op_type);
Expand Down
51 changes: 31 additions & 20 deletions python/paddle/fluid/tests/unittests/test_fill_constant_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,45 +220,56 @@ def setUp(self):
'''
self.op_type = "fill_constant"
self.init_data()
self.init_inputs()
self.init_attrs()
self.outputs = {'Out': np.full(self.shape, self.value)}

def init_data(self):
self.shape = [123, 92]
self.value = 3.8

def init_inputs(self):
self.inputs = {
"ShapeTensor": np.array(self.shape).astype("int32"),
'ValueTensor': np.array([self.value]).astype("float32")
}
self.attrs = {'value': self.value + 1.0}
self.outputs = {'Out': np.full(self.shape, self.value)}

def init_data(self):
self.shape = [123, 92]
self.value = 3.8
self.dtype = np.float32
def init_attrs(self):
self.attrs = {'value': self.value + 1.0}

def test_check_output(self):
self.check_output()


# Situation 5: value is a tensor
class TestFillConstantOp2_ValueTensor(OpTest):
def setUp(self):
'''Test fill_constant op with specified value
'''
self.op_type = "fill_constant"
self.init_data()
class TestFillConstantOp2_ValueTensor(TestFillConstantOp1_ValueTensor):
def init_data(self):
self.shape = [123, 92]
self.value = 3

def init_inputs(self):
self.inputs = {
"ShapeTensor": np.array(self.shape).astype("int32"),
'ValueTensor': np.array([self.value]).astype("int32")
}

def init_attrs(self):
self.attrs = {'value': self.value, 'dtype': 2}
self.outputs = {'Out': np.full(self.shape, self.value)}

def init_data(self):
self.shape = [123, 92]
self.value = 3
self.dtype = np.int32

def test_check_output(self):
self.check_output()
class TestFillConstantOp3_ValueTensor(TestFillConstantOp1_ValueTensor):
def init_inputs(self):
self.inputs = {
"ShapeTensor": np.array(self.shape).astype("int32"),
'ValueTensor': np.array([self.value]).astype("float64")
}


class TestFillConstantOp4_ValueTensor(TestFillConstantOp2_ValueTensor):
def init_inputs(self):
self.inputs = {
"ShapeTensor": np.array(self.shape).astype("int32"),
'ValueTensor': np.array([self.value]).astype("int64")
}


# Test python API
Expand Down