Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
0ee0d15
add cast kernel
MingMingShangTian Nov 1, 2021
1ae3fe2
add cast cuda kernel
MingMingShangTian Nov 1, 2021
dc81e06
Merge branch 'develop' into cast_kernel
MingMingShangTian Nov 2, 2021
7cd7966
add cast kernel
MingMingShangTian Nov 2, 2021
a244a0f
merge develop branch
MingMingShangTian Nov 3, 2021
0eaf913
make cast kernel output dtype undefined
MingMingShangTian Nov 8, 2021
dc9b42a
Merge branch 'develop' into cast_kernel
MingMingShangTian Nov 8, 2021
83415da
get cast dtype from vardesc
MingMingShangTian Nov 9, 2021
62cfb21
merge develop branch
MingMingShangTian Nov 9, 2021
b6c3c05
move cast to manipulation and add test case
MingMingShangTian Nov 9, 2021
17913da
add castinfershape
MingMingShangTian Nov 9, 2021
6fbd94d
avoid reinitilaze variable
MingMingShangTian Nov 9, 2021
70d4069
InitializeVariable support datatype
MingMingShangTian Nov 10, 2021
c434610
merge develop branch
MingMingShangTian Nov 10, 2021
a5c234e
merge develop branch
MingMingShangTian Nov 10, 2021
821b6e0
fix merge bug
MingMingShangTian Nov 10, 2021
4538134
revert modify initializeVariable
MingMingShangTian Nov 11, 2021
bbc83c4
revert modify on InitializeVariable
MingMingShangTian Nov 11, 2021
1feb022
revert modify on InitializeVariable
MingMingShangTian Nov 11, 2021
6d55883
mutable support reset dtype
MingMingShangTian Nov 11, 2021
7eea4dd
enable make pten tensor from variable when def_arg.type is undefined
MingMingShangTian Nov 11, 2021
51dc272
fix build pten ctx start_idx error
MingMingShangTian Nov 12, 2021
a927e6f
copy pten out tensor to variable
MingMingShangTian Nov 12, 2021
f893438
merge develop branch
MingMingShangTian Nov 12, 2021
9ba67db
merge develop branch
MingMingShangTian Nov 12, 2021
d8ce4c3
fix non pten kernel cast failed
chenwhql Nov 13, 2021
dce29b1
add reset allocation place for remake tensor
chenwhql Nov 13, 2021
b567179
Merge pull request #1 from chenwhql/tianyu_cast_kernel
MingMingShangTian Nov 13, 2021
4b70d76
fix inplace realloc error
chenwhql Nov 13, 2021
38a4e46
Merge pull request #2 from chenwhql/tianyu_cast_kernel
MingMingShangTian Nov 13, 2021
dedd03e
add mutable on pten kernles and remove unused cast files
MingMingShangTian Nov 13, 2021
ca9a284
rename function names
MingMingShangTian Nov 13, 2021
620960b
fix output type error
MingMingShangTian Nov 13, 2021
f4d6cc6
fix conflict with develop branch
MingMingShangTian Nov 14, 2021
acd5649
fix conflict with develop branch
MingMingShangTian Nov 14, 2021
a12a3a1
set data type to variable with pten's dtype
MingMingShangTian Nov 15, 2021
e22707e
merge develop branch
MingMingShangTian Nov 16, 2021
6adacbe
fix test_cast_api type mismatch
MingMingShangTian Nov 16, 2021
9276daa
densorTensro mutable_data support 0 bytes value
MingMingShangTian Nov 16, 2021
4427386
merge branch develop
MingMingShangTian Nov 16, 2021
dcaa367
fix the inplace bug of reshape kernel
MingMingShangTian Nov 16, 2021
0f6dd13
fix pten.backend != variable.place when moving storage, palce mismatc…
MingMingShangTian Nov 16, 2021
a07cda0
Merge branch 'develop' into cast_kernel
MingMingShangTian Nov 17, 2021
90b05d9
fix conflict with develop branch
MingMingShangTian Nov 17, 2021
b71b964
Fix bug of paddle::experimental::MovesStorage
YuanRisheng Nov 17, 2021
8633603
fix ReMakePtenDenseTensor place mismatch bug
MingMingShangTian Nov 17, 2021
3410498
Revert "fix ReMakePtenDenseTensor place mismatch bug"
MingMingShangTian Nov 18, 2021
6199455
fix ReMakePtenDenseTensor place mismatch bug
MingMingShangTian Nov 18, 2021
c469ffd
reverts the set_lod interface, test=develop
Shixiaowei02 Nov 18, 2021
6d712d0
Merge branch 'xiaowei fix code' into cast_kernel
MingMingShangTian Nov 18, 2021
539ce20
Merge branch 'develop' into cast_kernel
MingMingShangTian Nov 18, 2021
30e057a
modify by the review options
MingMingShangTian Nov 18, 2021
8ca880b
modify error message
MingMingShangTian Nov 18, 2021
55f7cb6
add & for const input arguments
MingMingShangTian Nov 18, 2021
d8873ff
add reference in params
YuanRisheng Nov 18, 2021
7860e78
elementwise_sub add mutable_data
MingMingShangTian Nov 18, 2021
9df1dc4
fix ResetHolderWithType check size bug
MingMingShangTian Nov 19, 2021
37a805d
Merge branch 'develop' into cast_kernel
MingMingShangTian Nov 19, 2021
9a49c51
add dependence pten_tensor to test_cast_api object
MingMingShangTian Nov 19, 2021
9dbfbee
merge develop branch to fix conflict
MingMingShangTian Nov 19, 2021
2a27ce3
remove unused code to pass ci coverage
MingMingShangTian Nov 19, 2021
68aa002
merge pr:37293 Fix bug of paddle::experimental::MovesStorageOD
MingMingShangTian Nov 19, 2021
a112c6d
Merge branch 'develop' into cast_kernel
MingMingShangTian Nov 19, 2021
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
121 changes: 101 additions & 20 deletions paddle/fluid/framework/operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,9 @@ void OperatorWithKernel::RunImpl(const Scope& scope,
}
BuildPtenKernelContext(*runtime_ctx, dev_ctx);
(*pt_kernel_)(pt_kernel_context_.get());

WriteBackToOutputs(runtime_ctx);

pt_kernel_context_->ClearData();
} else {
(*kernel_func_)(
Expand Down Expand Up @@ -1808,50 +1811,98 @@ void OperatorWithKernel::BuildPtenKernelContext(
for (size_t i = 0; i < input_names.size(); ++i) {
auto& in_def = input_defs.at(i);
auto& ins_vector = ctx.inputs.at(input_names[i]);
if (pt_kernel_context_->InputsSize() <= i) {

// calcute the start and end index of the input tensors
size_t start_idx =
(i == 0 ? 0 : pt_kernel_context_->InputRangeAt(i - 1).second);
size_t end_idx = start_idx + ins_vector.size();

// The current size of input/output in pt_kernel_context_ is at least equal
// the start_idx. For the reason of reusing the allocted of inputs or
// outputs in pt_kernel_context_, the current size of input/output can be
// greater then the index of which the tensort wanted to set to, so it will
// use ReMakePtenDenseTensorFromVar to make pten tensor.
if (pt_kernel_context_->InputsSize() == start_idx) {
paddle::SmallVector<std::shared_ptr<pten::TensorBase>> tmp_inputs;
for (auto* var : ins_vector) {
tmp_inputs.emplace_back(
experimental::MakePtenTensorBaseFromVar(*var, in_def));
}
pt_kernel_context_->EmplaceBackInputs(std::move(tmp_inputs));
} else {
} else if (pt_kernel_context_->InputsSize() > start_idx) {
size_t input_size = pt_kernel_context_->InputsSize();
for (size_t j = 0; j < ins_vector.size(); ++j) {
if (input_size > i + j) {
if (input_size > start_idx + j) {
experimental::ReMakePtenDenseTensorFromVar(
*ins_vector[j], in_def,
pt_kernel_context_->MutableInputAt<pten::DenseTensor>(i + j));
pt_kernel_context_->MutableInputAt<pten::DenseTensor>(start_idx +
j));
// TODO(chentianyu03): When multi input kernel, open this code
/*
} else {
pt_kernel_context_->EmplaceBackInputWithoutSetRange(
experimental::MakePtenTensorBaseFromVar(*ins_vector[j],
in_def));
*/
}
// TODO(chenweihang): adapt multi-input case later
}
pt_kernel_context_->MutableInputRangeAt(i) =
std::make_pair(i, i + ins_vector.size());
std::make_pair(start_idx, end_idx);
} else {
PADDLE_THROW(platform::errors::PreconditionNotMet(
"Error start index when trying to set new tensor to inputs, start "
"index is `%d`, but current pt_kernel_context_.inputs.size() is "
"`%d`.",
start_idx, pt_kernel_context_->InputsSize()));
}
}

for (size_t i = 0; i < output_names.size(); ++i) {
auto& out_def = output_defs.at(i);
auto& outs_vector = ctx.outputs.at(output_names[i]);
if (pt_kernel_context_->OutputsSize() <= i) {

size_t start_idx =
(i == 0 ? 0 : pt_kernel_context_->OutputRangeAt(i - 1).second);
size_t end_idx = start_idx + outs_vector.size();

// The current size of input/output in pt_kernel_context_ is at least equal
// the start_idx. For the reason of reusing the allocted of inputs or
// outputs in pt_kernel_context_, the current size of input/output can be
// greater then the index of which the tensort wanted to set to, so it will
// use ReMakePtenDenseTensorFromVar to make pten tensor.
if (pt_kernel_context_->OutputsSize() == start_idx) {
paddle::SmallVector<std::shared_ptr<pten::TensorBase>> tmp_outputs;
for (auto* var : outs_vector) {
tmp_outputs.emplace_back(
experimental::MakePtenTensorBaseFromVar(var, out_def));
}
pt_kernel_context_->EmplaceBackOutputs(std::move(tmp_outputs));
} else {
} else if (pt_kernel_context_->OutputsSize() > start_idx) {
size_t output_size = pt_kernel_context_->OutputsSize();
for (size_t j = 0; j < outs_vector.size(); ++j) {
if (output_size > i + j) {
if (output_size > start_idx + j) {
experimental::ReMakePtenDenseTensorFromVar(
outs_vector[j], out_def,
pt_kernel_context_->MutableOutputAt<pten::DenseTensor>(i + j));
pt_kernel_context_->MutableOutputAt<pten::DenseTensor>(start_idx +
j));

// TODO(chentianyu03): When multi output kernel, open this code
/*
} else {
pt_kernel_context_->EmplaceBackOutputWithoutSetRange(
experimental::MakePtenTensorBaseFromVar(outs_vector[j],
out_def));
*/
}
// TODO(chenweihang): adapt multi-output case later
}
pt_kernel_context_->MutableOutputRangeAt(i) =
std::make_pair(i, i + outs_vector.size());
std::make_pair(start_idx, end_idx);
} else {
PADDLE_THROW(platform::errors::PreconditionNotMet(
"Error start index when trying to set new tensor to inputs, start "
"index is `%d`, but current pt_kernel_context_.outputs.size() is "
"`%d`.",
start_idx, pt_kernel_context_->OutputsSize()));
}
}

Expand Down Expand Up @@ -1883,14 +1934,23 @@ void OperatorWithKernel::BuildPtenKernelContext(
} else if (attr_defs[i].type_index == std::type_index(typeid(bool))) {
pt_kernel_context_->EmplaceBackAttr(BOOST_GET_CONST(bool, attr));
} else if (attr_defs[i].type_index ==
std::type_index(typeid(std::vector<int64_t>)) &&
std::type_index(attr.type()) ==
std::type_index(typeid(std::vector<int>))) {
// Emplace Back Attr according to the type of Pten_Kernel args.
const auto& vector_int_attr = BOOST_GET_CONST(std::vector<int>, attr);
const std::vector<int64_t> vector_int64_attr(vector_int_attr.begin(),
vector_int_attr.end());
pt_kernel_context_->EmplaceBackAttr(vector_int64_attr);
std::type_index(typeid(pten::DataType))) {
auto data_type = pten::TransToPtenDataType(
static_cast<framework::proto::VarType::Type>(
BOOST_GET_CONST(int, attr)));
pt_kernel_context_->EmplaceBackAttr(data_type);
} else if (attr_defs[i].type_index ==
std::type_index(typeid(std::vector<int64_t>))) {
if (std::type_index(attr.type()) ==
std::type_index(typeid(std::vector<int>))) {
// Emplace Back Attr according to the type of Pten_Kernel args.
const auto& vector_int_attr = BOOST_GET_CONST(std::vector<int>, attr);
const std::vector<int64_t> vector_int64_attr(vector_int_attr.begin(),
vector_int_attr.end());
pt_kernel_context_->EmplaceBackAttr(vector_int64_attr);
}
// TODO(YuanRisheng) Need support vector<int64_t> attr

} else {
PADDLE_THROW(platform::errors::Unimplemented(
"unsupported cast op attribute `%s` when construct "
Expand All @@ -1901,5 +1961,26 @@ void OperatorWithKernel::BuildPtenKernelContext(
}
}

void OperatorWithKernel::WriteBackToOutputs(RuntimeContext* ctx) const {
// auto& input_names = std::get<0>(pt_kernel_signature_->args);
Copy link
Contributor

Choose a reason for hiding this comment

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

如果是无用的注释,建议在下个PR移除

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的

// auto& attr_names = std::get<1>(pt_kernel_signature_->args);
auto& output_names = std::get<2>(pt_kernel_signature_->args);

// pt_kernel_context_

for (size_t i = 0; i < output_names.size(); ++i) {
auto& outs_vector = ctx->outputs.at(output_names[i]);

auto& range_pair = pt_kernel_context_->OutputRangeAt(i);
auto pten_outs =
pt_kernel_context_->MutableOutputBetween<pten::DenseTensor>(
range_pair.first, range_pair.second);

for (size_t j = 0; j < pten_outs.size(); ++j) {
experimental::MakeVariableFromPtenTensor(pten_outs[j], outs_vector[j]);
}
}
}

} // namespace framework
} // namespace paddle
2 changes: 2 additions & 0 deletions paddle/fluid/framework/operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,8 @@ class OperatorWithKernel : public OperatorBase {
void BuildPtenKernelContext(const RuntimeContext& ctx,
platform::DeviceContext* dev_ctx) const;

void WriteBackToOutputs(RuntimeContext* ctx) const;

protected:
mutable std::unique_ptr<OpKernelType> kernel_type_;
mutable std::unique_ptr<OpKernelFunc> kernel_func_;
Expand Down
6 changes: 4 additions & 2 deletions paddle/fluid/framework/tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,12 @@ void Tensor::ResetHolder(std::shared_ptr<memory::Allocation> holder) {
}

void Tensor::ResetHolderWithType(std::shared_ptr<memory::Allocation> holder,
const proto::VarType::Type type) {
ResetHolder(holder);
const proto::VarType::Type& type) {
type_ = type;
ResetHolder(holder);
}

void Tensor::set_type(const proto::VarType::Type& type) { type_ = type; }

} // namespace framework
} // namespace paddle
4 changes: 3 additions & 1 deletion paddle/fluid/framework/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ class Tensor {
void ResetHolder(std::shared_ptr<memory::Allocation> holder);

void ResetHolderWithType(std::shared_ptr<memory::Allocation> holder,
const proto::VarType::Type type);
const proto::VarType::Type& type);

void set_type(const proto::VarType::Type& type);

TensorInplaceVersion& InplaceVersionCounter() {
return *inplace_version_counter_;
Expand Down
Loading