|
|
@ -11,36 +11,61 @@ 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(const platform::DeviceContext* ctx, const Variable& in,
|
|
|
|
const Variable& in, Variable* out) {
|
|
|
|
Variable* out) {
|
|
|
|
test_value++;
|
|
|
|
test_value++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void type2_to_type3(std::vector<platform::DeviceContext*> ctx,
|
|
|
|
void TransDataLayout_t(const platform::DeviceContext* ctx, const Variable& in,
|
|
|
|
const Variable& in, Variable* out) {
|
|
|
|
Variable* out) {
|
|
|
|
test_value--;
|
|
|
|
test_value--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void type1_to_type3(std::vector<platform::DeviceContext*> ctx,
|
|
|
|
void TransLibraryType_t(const platform::DeviceContext* ctx, const Variable& in,
|
|
|
|
const Variable& in, Variable* out) {
|
|
|
|
Variable* out) {
|
|
|
|
test_value += 2;
|
|
|
|
test_value += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -49,12 +74,9 @@ 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;
|
|
|
@ -62,17 +84,16 @@ TEST(DataTransform, Register) {
|
|
|
|
|
|
|
|
|
|
|
|
auto& instance = DataTransformFnMap::Instance();
|
|
|
|
auto& instance = DataTransformFnMap::Instance();
|
|
|
|
ASSERT_EQ(instance.Map().size(), 3UL);
|
|
|
|
ASSERT_EQ(instance.Map().size(), 3UL);
|
|
|
|
std::vector<DeviceContext*> ctx;
|
|
|
|
DeviceContext* ctx = nullptr;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|