device_worker_test.cc 2.4 KB
Newer Older
D
dongdaxiang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2019 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.

15
#include "paddle/fluid/framework/device_worker.h"
D
dongdaxiang 已提交
16
#include <gtest/gtest.h>
17
#include "paddle/fluid/framework/lod_tensor.h"
D
dongdaxiang 已提交
18 19 20 21
#include "paddle/fluid/framework/trainer.h"

namespace paddle {
namespace framework {
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
TEST(LodTensor, PrintLodTensor) {
  LoDTensor tensor1;
  tensor1.Resize({2});
  tensor1.mutable_data<float>(platform::CPUPlace());
  tensor1.data<float>()[0] = 0.2;
  tensor1.data<float>()[1] = 0.5;
  std::string res = PrintLodTensor(&tensor1, -1, 2);
  ASSERT_EQ(res, "access violation");
  res = PrintLodTensor(&tensor1, 0, 2);
  ASSERT_EQ(res, ":0.2:0.5");

  LoDTensor tensor2;
  tensor2.Resize({2});
  tensor2.mutable_data<int64_t>(platform::CPUPlace());
  tensor2.data<int64_t>()[0] = 1;
  tensor2.data<int64_t>()[1] = 2;
  res = PrintLodTensor(&tensor2, -1, 2);
  ASSERT_EQ(res, "access violation");
  res = PrintLodTensor(&tensor2, 0, 2);
  ASSERT_EQ(res, ":1:2");

  LoDTensor tensor3;
  tensor3.Resize({2});
  tensor3.mutable_data<double>(platform::CPUPlace());
  tensor3.data<double>()[0] = 0.1;
  tensor3.data<double>()[1] = 0.2;
  res = PrintLodTensor(&tensor3, 0, 2);
  ASSERT_EQ(res, ":0.1:0.2");
D
dongdaxiang 已提交
50
}
51 52 53 54 55 56 57 58 59 60 61 62

TEST(LodTensor, GetTensorBound) {
  LoD lod{{0, 2}};
  LoDTensor tensor;
  tensor.set_lod(lod);
  tensor.Resize({2, 1});
  tensor.mutable_data<float>(platform::CPUPlace());
  tensor.data<float>()[0] = 0;
  tensor.data<float>()[1] = 1;
  std::pair<int64_t, int64_t> res = GetTensorBound(&tensor, 0);
  ASSERT_EQ(res.first, 0);
  ASSERT_EQ(res.second, 2);
D
dongdaxiang 已提交
63
}
64 65 66 67 68 69 70 71 72 73

TEST(LodTensor, CheckValidOutput) {
  LoD lod{{0, 1, 2}};
  LoDTensor tensor;
  tensor.set_lod(lod);
  tensor.Resize({2, 1});
  tensor.mutable_data<float>(platform::CPUPlace());
  tensor.data<float>()[0] = 0;
  tensor.data<float>()[1] = 1;
  ASSERT_TRUE(CheckValidOutput(&tensor, 2));
D
dongdaxiang 已提交
74
}
75 76 77

}  // namespace framework
}  // namespace paddle