From c31cbae510344492d34b81e6840c69424493037c Mon Sep 17 00:00:00 2001 From: dzhwinter Date: Wed, 27 Dec 2017 22:07:57 +0800 Subject: [PATCH] Fix/transform (#7079) * "fix data transform" * "split into next PR" --- paddle/framework/data_transform.cc | 1 + paddle/framework/data_transform_test.cc | 76 ++++++++++++++++--------- paddle/framework/tensor.h | 2 +- 3 files changed, 51 insertions(+), 28 deletions(-) diff --git a/paddle/framework/data_transform.cc b/paddle/framework/data_transform.cc index 35f16025a9a..376268888e7 100644 --- a/paddle/framework/data_transform.cc +++ b/paddle/framework/data_transform.cc @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/framework/data_transform.h" +#include "paddle/framework/lod_tensor.h" namespace paddle { namespace framework { diff --git a/paddle/framework/data_transform_test.cc b/paddle/framework/data_transform_test.cc index f93a47eeb56..0825673546d 100644 --- a/paddle/framework/data_transform_test.cc +++ b/paddle/framework/data_transform_test.cc @@ -11,36 +11,63 @@ 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 +#include -#include "paddle/framework/data_transform.h" #include +#include "paddle/framework/data_transform.h" + namespace paddle { namespace framework { - using namespace platform; +/** + * @brief cross validation of different kernel type transform + * We use four bit map represent different combination. + * If the field has multiple possible value, only choose two of them. + * For DataType, only test the FP32(float), FP64(double). + * e.g. 0000 -> FP32, CPUPlace, kNHWC, kPlain + * 1111 -> FP64, GPUPlace, kNCHW, kMKLDNN + */ + +std::array kDataType = {proto::DataType::FP32, + proto::DataType::FP64}; + +std::array kPlace = {CPUPlace(), CUDAPlace(0)}; + +std::array kDataLayout = { + DataLayout::kNHWC, DataLayout::kNCHW, +}; + +std::array kLibraryType = { + LibraryType::kPlain, LibraryType::kMKLDNN, +}; + +OpKernelType GenFromBit(const std::vector bits) { + return OpKernelType(kDataType[bits[0]], kPlace[bits[1]], kDataLayout[bits[2]], + kLibraryType[bits[3]]); +} + 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); +auto kernel0 = GenFromBit({0, 0, 0, 0}); +auto kernel1 = GenFromBit({0, 0, 0, 1}); +auto kernel2 = GenFromBit({0, 0, 1, 0}); +auto kernel3 = GenFromBit({0, 0, 1, 1}); -void type1_to_type2(std::vector ctx, - const Variable& in, Variable* out) { +void TransDataType_t(std::vector ctx, + const Variable& in, Variable* out) { test_value++; } -void type2_to_type3(std::vector ctx, - const Variable& in, Variable* out) { +void TransDataLayout_t(std::vector ctx, + const Variable& in, Variable* out) { test_value--; } -void type1_to_type3(std::vector ctx, - const Variable& in, Variable* out) { +void TransLibraryType_t(std::vector ctx, + const Variable& in, Variable* out) { test_value += 2; } @@ -49,30 +76,25 @@ void type1_to_type3(std::vector ctx, 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); +REGISTER_DATA_TRANSFORM_FN(frw::kernel0, frw::kernel1, frw::TransDataType_t); +REGISTER_DATA_TRANSFORM_FN(frw::kernel1, frw::kernel2, frw::TransDataLayout_t); +REGISTER_DATA_TRANSFORM_FN(frw::kernel0, frw::kernel2, frw::TransLibraryType_t); TEST(DataTransform, Register) { using namespace paddle::framework; using namespace paddle::platform; auto& instance = DataTransformFnMap::Instance(); - ASSERT_EQ(instance.Map().size(), 3UL); std::vector 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); + instance.Get(std::make_pair(frw::kernel0, frw::kernel1))(ctx, in, &out); ASSERT_EQ(test_value, 1); - instance.Get(std::make_pair(frw::kernel_type_2, frw::kernel_type_3))(ctx, in, - &out); + + instance.Get(std::make_pair(frw::kernel1, frw::kernel2))(ctx, in, &out); ASSERT_EQ(test_value, 0); - instance.Get(std::make_pair(frw::kernel_type_1, frw::kernel_type_3))(ctx, in, - &out); + + instance.Get(std::make_pair(frw::kernel0, frw::kernel2))(ctx, in, &out); ASSERT_EQ(test_value, 2); } diff --git a/paddle/framework/tensor.h b/paddle/framework/tensor.h index b9f6884f7c4..341a6949bee 100644 --- a/paddle/framework/tensor.h +++ b/paddle/framework/tensor.h @@ -178,7 +178,7 @@ class Tensor { DDim dims_; /** - * @brief the layout of memory block, default is NCHW. + * @brief the layout of memory block, default is NHWC. * * @note the memory allocation order, describe how weight/data is stored * For example, in 4-D Tensor(rank=4), there are three commonly -- GitLab