未验证 提交 c31cbae5 编写于 作者: D dzhwinter 提交者: GitHub

Fix/transform (#7079)

* "fix data transform"

* "split into next PR"
上级 f9a12296
...@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and ...@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include "paddle/framework/data_transform.h" #include "paddle/framework/data_transform.h"
#include "paddle/framework/lod_tensor.h"
namespace paddle { namespace paddle {
namespace framework { namespace framework {
......
...@@ -11,35 +11,62 @@ distributed under the License is distributed on an "AS IS" BASIS, ...@@ -11,35 +11,62 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include <array>
#include <vector>
#include "paddle/framework/data_transform.h"
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "paddle/framework/data_transform.h"
namespace paddle { namespace paddle {
namespace framework { namespace framework {
using namespace platform; 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<proto::DataType, 2> kDataType = {proto::DataType::FP32,
proto::DataType::FP64};
std::array<Place, 2> kPlace = {CPUPlace(), CUDAPlace(0)};
std::array<DataLayout, 2> kDataLayout = {
DataLayout::kNHWC, DataLayout::kNCHW,
};
std::array<LibraryType, 2> kLibraryType = {
LibraryType::kPlain, LibraryType::kMKLDNN,
};
OpKernelType GenFromBit(const std::vector<bool> bits) {
return OpKernelType(kDataType[bits[0]], kPlace[bits[1]], kDataLayout[bits[2]],
kLibraryType[bits[3]]);
}
int test_value = 0; int test_value = 0;
OpKernelType kernel_type_1(proto::DataType::FP32, CPUPlace(), DataLayout::kNCHW, auto kernel0 = GenFromBit({0, 0, 0, 0});
LibraryType::kCUDNN); auto kernel1 = GenFromBit({0, 0, 0, 1});
OpKernelType kernel_type_2(proto::DataType::FP32, CUDAPlace(0), auto kernel2 = GenFromBit({0, 0, 1, 0});
DataLayout::kNCHW, LibraryType::kCUDNN); auto kernel3 = GenFromBit({0, 0, 1, 1});
OpKernelType kernel_type_3(proto::DataType::FP16, CUDAPlace(0),
DataLayout::kNCHW, LibraryType::kCUDNN);
void type1_to_type2(std::vector<platform::DeviceContext*> ctx, void TransDataType_t(std::vector<platform::DeviceContext*> ctx,
const Variable& in, Variable* out) { const Variable& in, Variable* out) {
test_value++; test_value++;
} }
void type2_to_type3(std::vector<platform::DeviceContext*> ctx, void TransDataLayout_t(std::vector<platform::DeviceContext*> ctx,
const Variable& in, Variable* out) { const Variable& in, Variable* out) {
test_value--; test_value--;
} }
void type1_to_type3(std::vector<platform::DeviceContext*> ctx, void TransLibraryType_t(std::vector<platform::DeviceContext*> ctx,
const Variable& in, Variable* out) { const Variable& in, Variable* out) {
test_value += 2; test_value += 2;
} }
...@@ -49,30 +76,25 @@ void type1_to_type3(std::vector<platform::DeviceContext*> ctx, ...@@ -49,30 +76,25 @@ void type1_to_type3(std::vector<platform::DeviceContext*> ctx,
namespace frw = paddle::framework; namespace frw = paddle::framework;
REGISTER_DATA_TRANSFORM_FN(frw::kernel_type_1, frw::kernel_type_2, REGISTER_DATA_TRANSFORM_FN(frw::kernel0, frw::kernel1, frw::TransDataType_t);
frw::type1_to_type2); REGISTER_DATA_TRANSFORM_FN(frw::kernel1, frw::kernel2, frw::TransDataLayout_t);
REGISTER_DATA_TRANSFORM_FN(frw::kernel_type_2, frw::kernel_type_3, REGISTER_DATA_TRANSFORM_FN(frw::kernel0, frw::kernel2, frw::TransLibraryType_t);
frw::type2_to_type3);
REGISTER_DATA_TRANSFORM_FN(frw::kernel_type_1, frw::kernel_type_3,
frw::type1_to_type3);
TEST(DataTransform, Register) { TEST(DataTransform, Register) {
using namespace paddle::framework; using namespace paddle::framework;
using namespace paddle::platform; using namespace paddle::platform;
auto& instance = DataTransformFnMap::Instance(); auto& instance = DataTransformFnMap::Instance();
ASSERT_EQ(instance.Map().size(), 3UL);
std::vector<DeviceContext*> ctx; std::vector<DeviceContext*> ctx;
paddle::framework::Variable in; paddle::framework::Variable in;
paddle::framework::Variable out; paddle::framework::Variable out;
instance.Get(std::make_pair(frw::kernel_type_1, frw::kernel_type_2))(ctx, in, instance.Get(std::make_pair(frw::kernel0, frw::kernel1))(ctx, in, &out);
&out);
ASSERT_EQ(test_value, 1); 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); 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); ASSERT_EQ(test_value, 2);
} }
...@@ -178,7 +178,7 @@ class Tensor { ...@@ -178,7 +178,7 @@ class Tensor {
DDim dims_; 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 * @note the memory allocation order, describe how weight/data is stored
* For example, in 4-D Tensor(rank=4), there are three commonly * For example, in 4-D Tensor(rank=4), there are three commonly
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册