tensor_util.cpp 2.7 KB
Newer Older
W
wangliu 已提交
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
朔-望's avatar
朔-望 已提交
2

W
wangliu 已提交
3 4 5
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
朔-望's avatar
朔-望 已提交
6

W
wangliu 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
朔-望's avatar
朔-望 已提交
8

W
wangliu 已提交
9 10 11 12 13
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. */
朔-望's avatar
朔-望 已提交
14 15 16 17 18 19 20

#include "tensor_util.h"
#include <algorithm>
#include <limits>
#include <vector>

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
21 22 23
namespace framework {

void TensorCopy(const Tensor &src, Tensor *dst) {
24 25 26 27 28
  //  VLOG(3) << "TensorCopy " << src.dims() << " from " <<
  //  src.place() << " to
  //  "
  //          << dst_place;
  src.check_memory_size();
朔-望's avatar
朔-望 已提交
29

30 31 32
  dst->Resize(src.dims());
  dst->set_layout(src.layout());
  auto src_ptr = src.data<void>();
朔-望's avatar
朔-望 已提交
33

34
  auto dst_ptr = dst->mutable_data(src.type());
朔-望's avatar
朔-望 已提交
35

36
  auto size = src.numel() * SizeOfType(src.type());
朔-望's avatar
朔-望 已提交
37

38
  memory::Copy(dst_ptr, src_ptr, size);
朔-望's avatar
朔-望 已提交
39 40 41
}

void TensorCopySync(const Tensor &src, Tensor *dst) {
42 43 44 45 46 47 48
  src.check_memory_size();
  dst->Resize(src.dims());
  dst->set_layout(src.layout());
  auto src_ptr = src.data<void>();
  auto dst_ptr = dst->mutable_data(src.type());
  auto size = src.numel() * SizeOfType(src.type());
  memory::Copy(dst_ptr, src_ptr, size);
朔-望's avatar
朔-望 已提交
49 50
}

朔-望's avatar
朔-望 已提交
51 52
template <typename Predicate>
struct AnyDTypeVisitor {
53 54 55 56 57 58 59
  Predicate predicate_;
  const Tensor &tensor_;
  Tensor *out_;

  AnyDTypeVisitor(Predicate predicate, const Tensor &tensor, Tensor *out)
      : predicate_(predicate), tensor_(tensor), out_(out) {}

朔-望's avatar
朔-望 已提交
60 61
  template <typename T>
  void operator()() const {
62 63 64 65 66
    //    auto t = EigenVector<T>::Flatten(tensor_);
    //    auto o = EigenScalar<bool>::From(*out_);
    // return any of predicate_(t) is true.
    //    o.device(*ctx_.eigen_device()) = predicate_(t).any();
  }
朔-望's avatar
朔-望 已提交
67 68 69
};

struct ContainsNANPredicate {
70 71 72 73 74 75
  template <typename T>
  auto operator()(const T &eigen_vec) const
      -> decltype(std::declval<T>().isnan()) {
    // Cast eigen_vector to vector of bool. true if is inf.
    return eigen_vec.isnan();
  }
朔-望's avatar
朔-望 已提交
76 77 78
};

struct ContainsInfPredicate {
79 80 81 82 83 84
  template <typename T>
  auto operator()(const T &eigen_vec) const
      -> decltype(std::declval<T>().isinf()) {
    // Cast eigen_vector to vector of bool. true if is inf.
    return eigen_vec.isinf();
  }
朔-望's avatar
朔-望 已提交
85 86 87
};

struct DeserializedDataFunctor {
88 89
  DeserializedDataFunctor(void **buf, Tensor *tensor)
      : buf_(buf), tensor_(tensor) {}
朔-望's avatar
朔-望 已提交
90

朔-望's avatar
朔-望 已提交
91 92
  template <typename T>
  void operator()() {
93 94
    *buf_ = tensor_->mutable_data<T>();
  }
朔-望's avatar
朔-望 已提交
95

96 97
  void **buf_;
  Tensor *tensor_;
朔-望's avatar
朔-望 已提交
98 99
};

朔-望's avatar
朔-望 已提交
100 101
}  // namespace framework
}  // namespace paddle_mobile