-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Add data transform fn #6953
Merged
jacquesqiao
merged 19 commits into
PaddlePaddle:develop
from
jacquesqiao:add-data-transform-fn
Dec 26, 2017
Merged
Add data transform fn #6953
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9fb242f
init data_transform
jacquesqiao c104e07
Merge branch 'develop' of /~https://github.com/PaddlePaddle/Paddle into…
jacquesqiao 75b6fbc
complete DataTransform
jacquesqiao b5a5842
fix build error
jacquesqiao a954cac
Merge branch 'develop' of /~https://github.com/PaddlePaddle/Paddle into…
jacquesqiao 501b3cc
add data_transform_test
jacquesqiao c6c0a12
add a register test for data_transform_fn
jacquesqiao 549ebea
use function to simulate registration macro
jacquesqiao df4930b
add register macro
jacquesqiao 8789644
Merge branch 'develop' of /~https://github.com/PaddlePaddle/Paddle into…
jacquesqiao f087a07
update test
jacquesqiao d9be464
clean code
jacquesqiao 583c71c
restore unrelated code
jacquesqiao d8011f8
update data transform test
jacquesqiao b84b30b
generate unique name for REGISTER_DATA_TRANSFORM_FN
jacquesqiao 30c58f5
add const
jacquesqiao 1053649
follow comment
jacquesqiao 49291a3
update KernelTypePair hash function
jacquesqiao 957200e
Merge branch 'develop' of /~https://github.com/PaddlePaddle/Paddle into…
jacquesqiao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. */ | ||
|
||
#include "paddle/framework/data_transform.h" | ||
|
||
namespace paddle { | ||
namespace framework { | ||
|
||
DataTransformFnMap& DataTransformFnMap::Instance() { | ||
static DataTransformFnMap data_transform_map; | ||
return data_transform_map; | ||
} | ||
|
||
} // namespace framework | ||
} // namespace paddle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. */ | ||
|
||
#pragma once | ||
|
||
#include <functional> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "paddle/framework/op_kernel_type.h" | ||
#include "paddle/framework/tensor.h" | ||
#include "paddle/framework/variable.h" | ||
#include "paddle/platform/device_context.h" | ||
#include "paddle/platform/macros.h" | ||
|
||
namespace paddle { | ||
namespace framework { | ||
|
||
using DataTransformFN = | ||
std::function<void(const std::vector<platform::DeviceContext*> ctx, | ||
const Variable& in, Variable* out)>; | ||
using KernelTypePair = std::pair<OpKernelType, OpKernelType>; | ||
|
||
static void hash_combine(std::size_t& seed, const OpKernelType& t) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code style is:
We can fix it later. |
||
OpKernelType::Hash kernel_type_hasher; | ||
seed ^= kernel_type_hasher(t) + 0x9e3779b9 + (seed << 6) + (seed >> 2); | ||
} | ||
|
||
struct KernelTypePairHash { | ||
size_t operator()(const KernelTypePair& kernel_pair) const { | ||
std::size_t seed = 0; | ||
hash_combine(seed, kernel_pair.first); | ||
hash_combine(seed, kernel_pair.second); | ||
|
||
return seed; | ||
} | ||
}; | ||
|
||
using DataTransformMap = | ||
std::unordered_map<KernelTypePair, DataTransformFN, KernelTypePairHash>; | ||
|
||
class DataTransformFnMap { | ||
public: | ||
static DataTransformFnMap& Instance(); | ||
|
||
bool Has(const KernelTypePair& key_pair) const { | ||
return map_.find(key_pair) != map_.end(); | ||
} | ||
|
||
void Insert(const OpKernelType& left, const OpKernelType& right, | ||
const DataTransformFN& data_tranform_fn) { | ||
Insert(std::make_pair(left, right), data_tranform_fn); | ||
} | ||
|
||
void Insert(const KernelTypePair& kernel_type_pair, | ||
const DataTransformFN& data_tranform_fn) { | ||
PADDLE_ENFORCE(!Has(kernel_type_pair), | ||
"KernelTypePair %s has been registered", ""); | ||
map_.insert({kernel_type_pair, data_tranform_fn}); | ||
} | ||
|
||
const DataTransformFN& Get(const KernelTypePair& key_pair) const { | ||
auto data_transformer = GetNullable(key_pair); | ||
PADDLE_ENFORCE_NOT_NULL(data_transformer, | ||
"DataTransformFN should not be NULL"); | ||
return *data_transformer; | ||
} | ||
|
||
const DataTransformFN* GetNullable(const KernelTypePair& key_pair) const { | ||
auto it = map_.find(key_pair); | ||
if (it == map_.end()) { | ||
return nullptr; | ||
} else { | ||
return &(it->second); | ||
} | ||
} | ||
|
||
const DataTransformMap& Map() const { return map_; } | ||
|
||
private: | ||
DataTransformFnMap() = default; | ||
DataTransformMap map_; | ||
DISABLE_COPY_AND_ASSIGN(DataTransformFnMap); | ||
}; | ||
|
||
// generate unique name with __LINE__ | ||
// refs https://stackoverflow.com/questions/1597007 | ||
#define TOKENPASTE(x, y) x##y | ||
#define TOKENPASTE2(x, y) TOKENPASTE(x, y) | ||
#define REGISTER_DATA_TRANSFORM_FN(from, to, fn) \ | ||
static int TOKENPASTE2(fn_, __LINE__)() { \ | ||
::paddle::framework::DataTransformFnMap::Instance().Insert(from, to, fn); \ | ||
return 0; \ | ||
} \ | ||
static int TOKENPASTE2(var_, __LINE__) __attribute__((unused)) = \ | ||
TOKENPASTE2(fn_, __LINE__)() | ||
|
||
} // namespace framework | ||
} // namespace paddle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. */ | ||
|
||
#include "paddle/framework/data_transform.h" | ||
#include <gtest/gtest.h> | ||
|
||
namespace paddle { | ||
namespace framework { | ||
|
||
using namespace platform; | ||
|
||
int test_value = 0; | ||
|
||
OpKernelType kernel_type_1(proto::DataType::FP32, CPUPlace(), DataLayout::kNCHW, | ||
LibraryType::kCUDNN); | ||
OpKernelType kernel_type_2(proto::DataType::FP32, CUDAPlace(0), | ||
DataLayout::kNCHW, LibraryType::kCUDNN); | ||
OpKernelType kernel_type_3(proto::DataType::FP16, CUDAPlace(0), | ||
DataLayout::kNCHW, LibraryType::kCUDNN); | ||
|
||
void type1_to_type2(std::vector<platform::DeviceContext*> ctx, | ||
const Variable& in, Variable* out) { | ||
test_value++; | ||
} | ||
|
||
void type2_to_type3(std::vector<platform::DeviceContext*> ctx, | ||
const Variable& in, Variable* out) { | ||
test_value--; | ||
} | ||
|
||
void type1_to_type3(std::vector<platform::DeviceContext*> ctx, | ||
const Variable& in, Variable* out) { | ||
test_value += 2; | ||
} | ||
|
||
} // namespace framework | ||
} // namespace paddle | ||
|
||
namespace frw = paddle::framework; | ||
|
||
REGISTER_DATA_TRANSFORM_FN(frw::kernel_type_1, frw::kernel_type_2, | ||
frw::type1_to_type2); | ||
REGISTER_DATA_TRANSFORM_FN(frw::kernel_type_2, frw::kernel_type_3, | ||
frw::type2_to_type3); | ||
REGISTER_DATA_TRANSFORM_FN(frw::kernel_type_1, frw::kernel_type_3, | ||
frw::type1_to_type3); | ||
|
||
TEST(DataTransform, Register) { | ||
using namespace paddle::framework; | ||
using namespace paddle::platform; | ||
|
||
auto& instance = DataTransformFnMap::Instance(); | ||
ASSERT_EQ(instance.Map().size(), 3UL); | ||
std::vector<DeviceContext*> ctx; | ||
paddle::framework::Variable in; | ||
paddle::framework::Variable out; | ||
|
||
instance.Get(std::make_pair(frw::kernel_type_1, frw::kernel_type_2))(ctx, in, | ||
&out); | ||
ASSERT_EQ(test_value, 1); | ||
instance.Get(std::make_pair(frw::kernel_type_2, frw::kernel_type_3))(ctx, in, | ||
&out); | ||
ASSERT_EQ(test_value, 0); | ||
instance.Get(std::make_pair(frw::kernel_type_1, frw::kernel_type_3))(ctx, in, | ||
&out); | ||
ASSERT_EQ(test_value, 2); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DataTransform should not only use
Tensor
as its argument type.How about
SelectedRows
?LoDTensor
?vector<Scope>
? It should take Variable as its argument.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done