test_embedding_api.cc 3.9 KB
Newer Older
Z
zyfncg 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.

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 <gtest/gtest.h>
#include <memory>

#include "paddle/phi/api/backward/backward_api.h"
#include "paddle/phi/api/include/api.h"

#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/kernel_registry.h"

PD_DECLARE_KERNEL(sparse_weight_embedding, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(sparse_weight_embedding_grad, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(sparse_weight_embedding_sparse_grad, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(empty, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(full, CPU, ALL_LAYOUT);

namespace paddle {
namespace tests {

TEST(API, sparse_weight_embedding) {
  auto x = paddle::experimental::empty({4}, DataType::INT32);
  auto* x_data = x.data<int32_t>();
  x_data[0] = 0;
  x_data[1] = 4;
  x_data[2] = 3;
  x_data[3] = 1;

  auto weight_sr = std::make_shared<phi::SelectedRows>(
      std::vector<int64_t>{0, 1, 2, 3, 4, 5, 6}, 16);
  *weight_sr->mutable_value() = *static_cast<phi::DenseTensor*>(
      paddle::experimental::full({7, 3}, 2, DataType::FLOAT32).impl().get());
  paddle::experimental::Tensor weight;
  weight.set_impl(weight_sr);

  auto out = paddle::experimental::embedding(x, weight);

  // 3. check result
  ASSERT_EQ(out.dims().size(), 2);
  ASSERT_EQ(out.dims()[0], 4);
  ASSERT_EQ(out.numel(), 12);
  ASSERT_EQ(out.type(), phi::DataType::FLOAT32);
  ASSERT_EQ(out.layout(), phi::DataLayout::NCHW);
}

TEST(API, sparse_weight_embedding_grad) {
  auto x = paddle::experimental::empty({4}, DataType::INT32);
  auto* x_data = x.data<int32_t>();
  x_data[0] = 0;
  x_data[1] = 4;
  x_data[2] = 3;
  x_data[3] = 1;

  auto weight_sr = std::make_shared<phi::SelectedRows>(
      std::vector<int64_t>{0, 1, 2, 3, 4, 5, 6}, 16);
  *weight_sr->mutable_value() = *static_cast<phi::DenseTensor*>(
      paddle::experimental::full({7, 3}, 2, DataType::FLOAT32).impl().get());
  paddle::experimental::Tensor weight;
  weight.set_impl(weight_sr);

  auto out_grad = paddle::experimental::full({4, 3}, 1, DataType::FLOAT32);

  paddle::experimental::Tensor weight_grad;

  paddle::experimental::embedding_grad(
      x, weight, out_grad, -1, false, &weight_grad);

  // 3. check result
  ASSERT_EQ(weight_grad.dims().size(), 2);
  ASSERT_EQ(weight_grad.dims()[0], 16);
  ASSERT_EQ(weight_grad.numel(), 48);
  ASSERT_EQ(weight_grad.type(), phi::DataType::FLOAT32);
  ASSERT_EQ(weight_grad.layout(), phi::DataLayout::NCHW);
}

TEST(API, sparse_weight_embedding_sparse_grad) {
  auto x = paddle::experimental::empty({4}, DataType::INT32);
  auto* x_data = x.data<int32_t>();
  x_data[0] = 0;
  x_data[1] = 4;
  x_data[2] = 3;
  x_data[3] = 1;

  auto weight_sr = std::make_shared<phi::SelectedRows>(
      std::vector<int64_t>{0, 1, 2, 3, 4, 5, 6}, 16);
  *weight_sr->mutable_value() = *static_cast<phi::DenseTensor*>(
      paddle::experimental::full({7, 3}, 2, DataType::FLOAT32).impl().get());
  paddle::experimental::Tensor weight;
  weight.set_impl(weight_sr);

  auto out_grad = paddle::experimental::full({4, 3}, 1, DataType::FLOAT32);

  paddle::experimental::Tensor weight_grad;

  paddle::experimental::embedding_grad(
      x, weight, out_grad, -1, true, &weight_grad);

  // 3. check result
  ASSERT_EQ(weight_grad.dims().size(), 2);
  ASSERT_EQ(weight_grad.dims()[0], 4);
  ASSERT_EQ(weight_grad.numel(), 12);
  ASSERT_EQ(weight_grad.type(), phi::DataType::FLOAT32);
  ASSERT_EQ(weight_grad.layout(), phi::DataLayout::NCHW);
}

}  // namespace tests
}  // namespace paddle