未验证 提交 ec11393f 编写于 作者: R Ruilong Liu 提交者: GitHub

Merge pull request #376 from cocodark/develop

shrink so size
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
project(paddle-mobile) project(paddle-mobile)
add_definitions(-DPADDLE_MOBILE_DEBUG) #add_definitions(-DPADDLE_MOBILE_DEBUG)
add_definitions(-DENABLE_EXCEPTION) #add_definitions(-DENABLE_EXCEPTION)
if(IS_MAC) if(IS_MAC)
add_definitions(-DX86) add_definitions(-DX86)
...@@ -15,7 +15,8 @@ else () ...@@ -15,7 +15,8 @@ else ()
add_definitions(-DX86) add_definitions(-DX86)
endif() endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
set(CMAKE_BUILD_TYPE Release) set(CMAKE_BUILD_TYPE Release)
set(CMAKE_VERBOSE_MAKEFILE ON) set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
......
...@@ -12,6 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ...@@ -12,6 +12,7 @@ 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 "common/enforce.h"
#include "common/log.h" #include "common/log.h"
#pragma once #pragma once
...@@ -55,15 +56,11 @@ class RawData { ...@@ -55,15 +56,11 @@ class RawData {
char data[size]; char data[size];
RawData() {} RawData() {}
RawData(const RawData &raw_data) { strcpy(data, raw_data.data); } RawData(const RawData &raw_data) { strcpy(data, raw_data.data); }
// void operator=(const RawData &raw_data){
// strcpy(data, raw_data.data);
// }
}; };
template <typename... Ts> template <typename... Ts>
struct Variant { struct Variant {
Variant(const Variant &variant) { Variant(const Variant &variant) {
// std::cout << " 赋值构造函数 " << std::endl;
type_id = variant.type_id; type_id = variant.type_id;
data = variant.data; data = variant.data;
} }
...@@ -85,8 +82,7 @@ struct Variant { ...@@ -85,8 +82,7 @@ struct Variant {
if (type_id == typeid(T).hash_code()) { if (type_id == typeid(T).hash_code()) {
return *const_cast<T *>(reinterpret_cast<const T *>(&data)); return *const_cast<T *>(reinterpret_cast<const T *>(&data));
} else { } else {
// std::cout << " bad cast in variant " << std::endl; PADDLE_MOBILE_THROW_EXCEPTION(" bad cast in variant ");
throw std::bad_cast();
} }
} }
......
...@@ -127,7 +127,7 @@ class Attribute { ...@@ -127,7 +127,7 @@ class Attribute {
} else if (attr.variant_.TypeId() == typeid(int64_t).hash_code()) { } else if (attr.variant_.TypeId() == typeid(int64_t).hash_code()) {
return vistor(attr.variant_.Get<int64_t>()); return vistor(attr.variant_.Get<int64_t>());
} else { } else {
throw std::bad_exception(); PADDLE_MOBILE_THROW_EXCEPTION("type not support");
} }
} }
......
...@@ -15,7 +15,6 @@ limitations under the License. */ ...@@ -15,7 +15,6 @@ limitations under the License. */
#pragma once #pragma once
#include <cctype> #include <cctype>
#include <iostream>
#include <string> #include <string>
namespace paddle_mobile { namespace paddle_mobile {
...@@ -40,7 +39,7 @@ inline DataLayout StringToDataLayout(const std::string &str) { ...@@ -40,7 +39,7 @@ inline DataLayout StringToDataLayout(const std::string &str) {
} else if (s == "ANYLAYOUT") { } else if (s == "ANYLAYOUT") {
return DataLayout::kAnyLayout; return DataLayout::kAnyLayout;
} else { } else {
// std::cout << "Unknown storage order string: %s", s; PADDLE_MOBILE_THROW_EXCEPTION("Unknown storage order string: %s", s)
} }
} }
......
...@@ -63,9 +63,6 @@ void make_ddim(DDim &ddim, const int64_t *dims, int n) { ...@@ -63,9 +63,6 @@ void make_ddim(DDim &ddim, const int64_t *dims, int n) {
ddim = make_dim<9>(dims); ddim = make_dim<9>(dims);
break; break;
default: default:
// std::cout << "Dynamic dimensions must have between [1,
// 9]
// dimensions.";
break; break;
} }
} }
...@@ -133,9 +130,6 @@ int64_t DDim::operator[](int idx) const { ...@@ -133,9 +130,6 @@ int64_t DDim::operator[](int idx) const {
int DDim::size() const { return arity(*this); } int DDim::size() const { return arity(*this); }
bool DDim::operator==(DDim d) const { bool DDim::operator==(DDim d) const {
// if (var.which() != d.getVar().which()) {
// return false;
// } else {
std::vector<int64_t> v1 = vectorize(*this); std::vector<int64_t> v1 = vectorize(*this);
std::vector<int64_t> v2 = vectorize(d); std::vector<int64_t> v2 = vectorize(d);
...@@ -157,7 +151,7 @@ DDim DDim::operator+(DDim d) const { ...@@ -157,7 +151,7 @@ DDim DDim::operator+(DDim d) const {
std::vector<int64_t> v3; std::vector<int64_t> v3;
assert(v1.size() == v2.size()); PADDLE_MOBILE_ENFORCE(v1.size() == v2.size(), "v1.size() != v2.size()");
for (unsigned int i = 0; i < v1.size(); i++) { for (unsigned int i = 0; i < v1.size(); i++) {
v3.push_back(v1[i] + v2[i]); v3.push_back(v1[i] + v2[i]);
...@@ -172,7 +166,7 @@ DDim DDim::operator*(DDim d) const { ...@@ -172,7 +166,7 @@ DDim DDim::operator*(DDim d) const {
std::vector<int64_t> v3; std::vector<int64_t> v3;
assert(v1.size() == v2.size()); PADDLE_MOBILE_ENFORCE(v1.size() == v2.size(), "v1.size() == v2.size()");
for (unsigned int i = 0; i < v1.size(); i++) { for (unsigned int i = 0; i < v1.size(); i++) {
v3.push_back(v1[i] * v2[i]); v3.push_back(v1[i] * v2[i]);
...@@ -235,13 +229,10 @@ struct SliceVectorizeVisitor : Vistor<void> { ...@@ -235,13 +229,10 @@ struct SliceVectorizeVisitor : Vistor<void> {
SliceVectorizeVisitor(std::vector<int64_t> &v, int b, int e) SliceVectorizeVisitor(std::vector<int64_t> &v, int b, int e)
: vector(v), begin(b), end(e) { : vector(v), begin(b), end(e) {
// PADDLE_ENFORCE(begin < end, PADDLE_MOBILE_ENFORCE(
// "Begin index must be less than end index in begin < end, "Begin index must be less than end index in ddim slice.");
// ddim PADDLE_MOBILE_ENFORCE(begin >= 0,
// slice."); "Begin index can't be less than zero in ddim slice.");
// PADDLE_ENFORCE(begin >= 0,
// "Begin index can't be less than zero in
// ddim slice.");
} }
template <int S> template <int S>
...@@ -267,9 +258,7 @@ DDim slice_ddim(const DDim &ddim, int begin, int end) { ...@@ -267,9 +258,7 @@ DDim slice_ddim(const DDim &ddim, int begin, int end) {
std::vector<int64_t> vec; std::vector<int64_t> vec;
vec.reserve(end - begin); vec.reserve(end - begin);
SliceVectorizeVisitor visitor(vec, begin, end); SliceVectorizeVisitor visitor(vec, begin, end);
// boost::apply_visitor(visitor, dim);
DDim::ApplyVistor(visitor, ddim); DDim::ApplyVistor(visitor, ddim);
// visitor(ddim.var.Get<Dim<4>>());
return make_ddim(vec); return make_ddim(vec);
} }
...@@ -287,31 +276,40 @@ struct ArityVisitor : Vistor<int> { ...@@ -287,31 +276,40 @@ struct ArityVisitor : Vistor<int> {
int arity(const DDim &d) { int arity(const DDim &d) {
ArityVisitor arityVisitor = ArityVisitor(); ArityVisitor arityVisitor = ArityVisitor();
return DDim::ApplyVistor(arityVisitor, d); return DDim::ApplyVistor(arityVisitor, d);
// return arityVisitor(d.var.Get<Dim<4>>());
// return boost::apply_visitor(ArityVisitor(), d); }
} }
/// \cond HIDDEN /// \cond HIDDEN
/// \endcond
struct OSVistor : Vistor<std::ostream &> { /// \endcond
OSVistor(std::ostream &os) : os_(os) {}
template <int D> // struct OSVistor : Vistor<std::ostream &> {
std::ostream &operator()(Dim<D> dim) const { // OSVistor(std::ostream &os) : os_(os) {}
return os_ << dim; //
// template <int D>
// std::ostream &operator()(Dim<D> dim) const {
// return os_ << dim;
// }
//
// private:
// std::ostream &os_;
//};
// std::ostream &operator<<(std::ostream &os, const DDim &ddim) {
// auto vistor = OSVistor(os);
// DDim::ApplyVistor(vistor, ddim);
// return os;
//}
#ifdef PADDLE_MOBILE_DEBUG
inline Print &operator<<(Print &printer, const DDim &ddim) {
for (int j = 0; j < ddim.size(); ++j) {
printer << ddim[j] << " ";
} }
private: return printer;
std::ostream &os_;
};
std::ostream &operator<<(std::ostream &os, const DDim &ddim) {
auto vistor = OSVistor(os);
DDim::ApplyVistor(vistor, ddim);
return os;
} }
#endif
DDim::DDim(std::initializer_list<int64_t> init_list) { DDim::DDim(std::initializer_list<int64_t> init_list) {
*this = make_ddim(init_list); *this = make_ddim(init_list);
} }
......
...@@ -15,8 +15,8 @@ limitations under the License. */ ...@@ -15,8 +15,8 @@ limitations under the License. */
#pragma once #pragma once
#include <initializer_list> #include <initializer_list>
#include <stdexcept>
#include <vector> #include <vector>
#include "common/enforce.h"
#include "common/variant.h" #include "common/variant.h"
#include "dim.h" #include "dim.h"
...@@ -57,9 +57,7 @@ struct DDim { ...@@ -57,9 +57,7 @@ struct DDim {
} else if (d.var.TypeId() == typeid(Dim<9>).hash_code()) { } else if (d.var.TypeId() == typeid(Dim<9>).hash_code()) {
return vistor(d.var.Get<Dim<9>>()); return vistor(d.var.Get<Dim<9>>());
} else { } else {
printf(" dim not support \n"); DLOG << " dim not support";
throw std::bad_exception();
// return typename Vistor::type_t();
} }
} }
...@@ -139,8 +137,6 @@ DDim slice_ddim(const DDim &dim, int begin, int end); ...@@ -139,8 +137,6 @@ DDim slice_ddim(const DDim &dim, int begin, int end);
int arity(const DDim &ddim); int arity(const DDim &ddim);
std::ostream &operator<<(std::ostream &, const DDim &);
// Reshape a tensor to a matrix. The matrix's first dimension(column // Reshape a tensor to a matrix. The matrix's first dimension(column
// length) // length)
// will be the product of tensor's first `num_col_dims` dimensions. // will be the product of tensor's first `num_col_dims` dimensions.
......
...@@ -14,10 +14,7 @@ limitations under the License. */ ...@@ -14,10 +14,7 @@ limitations under the License. */
#pragma once #pragma once
#include <sstream> #include "common/enforce.h"
#include <stdexcept>
#include <type_traits>
namespace paddle_mobile { namespace paddle_mobile {
namespace framework { namespace framework {
...@@ -71,13 +68,9 @@ struct Dim<0> { ...@@ -71,13 +68,9 @@ struct Dim<0> {
Dim() {} Dim() {}
Dim(int idx, const Dim<0> &size) { Dim(int idx, const Dim<0> &size) {
#ifndef __CUDA_ARCH__
if (idx > 0) { if (idx > 0) {
throw std::invalid_argument("Index out of range."); PADDLE_MOBILE_THROW_EXCEPTION("Index out of range.")
} }
#else
PADDLE_ASSERT(idx == 0);
#endif
} }
bool operator==(const Dim<0> &o) const { return true; } bool operator==(const Dim<0> &o) const { return true; }
...@@ -123,13 +116,10 @@ struct DimGetter<0> { ...@@ -123,13 +116,10 @@ struct DimGetter<0> {
template <int D> template <int D>
int64_t &indexer(Dim<D> &dim, int idx) { int64_t &indexer(Dim<D> &dim, int idx) {
#ifndef __CUDA_ARCH__
if (idx < 0) { if (idx < 0) {
throw std::invalid_argument("Tried to access a negative dimension"); PADDLE_MOBILE_THROW_EXCEPTION("Tried to access a negative dimension")
} }
#else
PADDLE_ASSERT(idx >= 0);
#endif
if (idx == 0) { if (idx == 0) {
return dim.head; return dim.head;
} }
...@@ -138,30 +128,14 @@ int64_t &indexer(Dim<D> &dim, int idx) { ...@@ -138,30 +128,14 @@ int64_t &indexer(Dim<D> &dim, int idx) {
template <> template <>
int64_t &indexer<0>(Dim<0> &dim, int idx) { int64_t &indexer<0>(Dim<0> &dim, int idx) {
#ifndef __CUDA_ARCH__ PADDLE_MOBILE_THROW_EXCEPTION("Invalid index")
throw std::invalid_argument("Invalid index");
#else
PADDLE_ASSERT(false);
#if CUDA_VERSION < 8000
// On CUDA versions previous to 8.0, only __shared__ variables
// could be declared as static in the device code.
int64_t head = 0;
#else
static int64_t head = 0;
#endif
return head;
#endif
} }
template <int D> template <int D>
int64_t indexer(const Dim<D> &dim, int idx) { int64_t indexer(const Dim<D> &dim, int idx) {
#ifndef __CUDA_ARCH__
if (idx < 0) { if (idx < 0) {
throw std::invalid_argument("Tried to access a negative dimension"); PADDLE_MOBILE_THROW_EXCEPTION("Tried to access a negative dimension")
} }
#else
PADDLE_ASSERT(idx >= 0);
#endif
if (idx == 0) { if (idx == 0) {
return dim.head; return dim.head;
} }
...@@ -170,19 +144,7 @@ int64_t indexer(const Dim<D> &dim, int idx) { ...@@ -170,19 +144,7 @@ int64_t indexer(const Dim<D> &dim, int idx) {
template <> template <>
int64_t indexer<0>(const Dim<0> &dim, int idx) { int64_t indexer<0>(const Dim<0> &dim, int idx) {
#ifndef __CUDA_ARCH__ PADDLE_MOBILE_THROW_EXCEPTION("Invalid index")
throw std::invalid_argument("Invalid index");
#else
PADDLE_ASSERT(false);
#if CUDA_VERSION < 8000
// On CUDA versions previous to 8.0, only __shared__ variables
// could be declared as static in the device code.
int64_t head = 0;
#else
static int64_t head = 0;
#endif
return head;
#endif
} }
} // namespace } // namespace
...@@ -363,50 +325,5 @@ Dim<sizeof...(Args)> make_dim(Args... idxes) { ...@@ -363,50 +325,5 @@ Dim<sizeof...(Args)> make_dim(Args... idxes) {
return Dim<sizeof...(Args)>(idxes...); return Dim<sizeof...(Args)>(idxes...);
} }
// Allows us to output a Dim
// XXX For some reason, overloading fails to resolve this correctly
template <int i>
typename std::enable_if<(i > 1), std::ostream &>::type operator<<(
std::ostream &os, const Dim<i> &d) {
os << d.head << ", " << d.tail;
return os;
}
// Base case that allows us to output a Dim
// XXX I wish this could be an overload instead of a template
template <int i>
typename std::enable_if<(i == 1), std::ostream &>::type operator<<(
std::ostream &os, const Dim<i> &d) {
os << d.head;
return os;
}
inline std::ostream &operator<<(std::ostream &os, const Dim<0> &d) {
return os;
}
template <int i>
std::string Dim<i>::to_string() const {
std::stringstream stream;
stream << *this;
return stream.str();
}
template <int D>
Dim<D> linear_to_dimension(int linear_index, Dim<D> extents) {
Dim<D> result;
for (int i = 0; i < D - 1; ++i) {
result[i] = linear_index % extents[i];
linear_index /= extents[i];
}
result[D - 1] = linear_index;
return result;
}
} // namespace framework } // namespace framework
} // namespace paddle_mobile } // namespace paddle_mobile
...@@ -13,53 +13,49 @@ See the License for the specific language governing permissions and ...@@ -13,53 +13,49 @@ See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include "lod_tensor.h" #include "lod_tensor.h"
#include <stdint.h>
#include <string.h>
#include <algorithm>
#include <iterator>
namespace paddle_mobile { namespace paddle_mobile {
namespace framework { namespace framework {
std::ostream &operator<<(std::ostream &os, const LoD &lod) { // std::ostream &operator<<(std::ostream &os, const LoD &lod) {
os << "{"; // os << "{";
for (auto &v : lod) { // for (auto &v : lod) {
os << "{"; // os << "{";
bool is_first = true; // bool is_first = true;
for (auto &i : v) { // for (auto &i : v) {
if (is_first) { // if (is_first) {
os << i; // os << i;
is_first = false; // is_first = false;
} else { // } else {
os << ", " << i; // os << ", " << i;
} // }
} // }
os << "}"; // os << "}";
} // }
os << "}"; // os << "}";
//
return os; // return os;
} //}
//
std::ostream &operator<<(std::ostream &os, const LoDTensor &t) { // std::ostream &operator<<(std::ostream &os, const LoDTensor &t) {
PADDLE_MOBILE_ENFORCE(t.type().hash_code() == typeid(float).hash_code(), // PADDLE_MOBILE_ENFORCE(t.type().hash_code() == typeid(float).hash_code(),
"t.type() is not float"); // "t.type() is not float");
os << "dim: " << t.dims() << "\n"; // os << "dim: " << t.dims() << "\n";
os << "lod: " << t.lod() << "\n"; // os << "lod: " << t.lod() << "\n";
// only print first ten elements // // only print first ten elements
int64_t size = t.numel() < 10 ? t.numel() : 10; // int64_t size = t.numel() < 10 ? t.numel() : 10;
for (int64_t i = 0; i < size; ++i) { // for (int64_t i = 0; i < size; ++i) {
os << t.data<float>()[i] << " "; // os << t.data<float>()[i] << " ";
} // }
//
return os; // return os;
} //}
std::string LoDToString(const LoD &lod) { // std::string LoDToString(const LoD &lod) {
std::ostringstream stream; // std::ostringstream stream;
stream << lod; // stream << lod;
return stream.str(); // return stream.str();
} //}
LoD SliceInLevel(const LoD &in, size_t level, size_t elem_begin, LoD SliceInLevel(const LoD &in, size_t level, size_t elem_begin,
size_t elem_end) { size_t elem_end) {
...@@ -139,7 +135,7 @@ bool CheckLoD(const LoD &in, int tensor_height) { ...@@ -139,7 +135,7 @@ bool CheckLoD(const LoD &in, int tensor_height) {
if (a < b) return true; if (a < b) return true;
return false; return false;
})) { })) {
std::cout << "ascending error"; PADDLE_MOBILE_THROW_EXCEPTION("ascending error")
return false; return false;
} }
} }
......
...@@ -144,7 +144,7 @@ void Node::Folder( ...@@ -144,7 +144,7 @@ void Node::Folder(
} }
} }
} }
#ifdef PADDLE_MOBILE_DEBUG
std::string Node::ToString(std::string blank, const Node *node) const { std::string Node::ToString(std::string blank, const Node *node) const {
std::stringstream ss; std::stringstream ss;
ss << type_ << "-> \n"; ss << type_ << "-> \n";
...@@ -175,6 +175,7 @@ Print &operator<<(Print &printer, const Node &node) { ...@@ -175,6 +175,7 @@ Print &operator<<(Print &printer, const Node &node) {
printer << node.ToString(); printer << node.ToString();
return printer; return printer;
} }
#endif
} // namespace framework } // namespace framework
} // namespace paddle_mobile } // namespace paddle_mobile
...@@ -17,7 +17,6 @@ limitations under the License. */ ...@@ -17,7 +17,6 @@ limitations under the License. */
#include <map> #include <map>
#include <string> #include <string>
#include <vector> #include <vector>
#include "common/log.h" #include "common/log.h"
#include "framework/program/op_desc.h" #include "framework/program/op_desc.h"
...@@ -34,7 +33,11 @@ class Node { ...@@ -34,7 +33,11 @@ class Node {
: op_desc_(op_desc), type_(op_desc->Type()) {} : op_desc_(op_desc), type_(op_desc->Type()) {}
Node &operator>(std::shared_ptr<Node> node); Node &operator>(std::shared_ptr<Node> node);
bool operator==(const Node &in); bool operator==(const Node &in);
#ifdef PADDLE_MOBILE_DEBUG
std::string ToString() const; std::string ToString() const;
void Description();
#endif
std::shared_ptr<Node> To(int size); std::shared_ptr<Node> To(int size);
uint Depth(uint begin = 0); uint Depth(uint begin = 0);
Node &Folder( Node &Folder(
...@@ -44,7 +47,6 @@ class Node { ...@@ -44,7 +47,6 @@ class Node {
std::vector<std::shared_ptr<framework::OpDesc>> OpDescs(uint size); std::vector<std::shared_ptr<framework::OpDesc>> OpDescs(uint size);
std::shared_ptr<framework::OpDesc> OpDescOfNode() { return op_desc_; } std::shared_ptr<framework::OpDesc> OpDescOfNode() { return op_desc_; }
std::string Type() { return type_; } std::string Type() { return type_; }
void Description();
private: private:
void OpDescs(uint size, void OpDescs(uint size,
...@@ -56,7 +58,9 @@ class Node { ...@@ -56,7 +58,9 @@ class Node {
std::map<std::string, std::pair<std::string, std::string>> *change, std::map<std::string, std::pair<std::string, std::string>> *change,
Node *begin_node, std::vector<std::shared_ptr<Node>> *removed_nodes); Node *begin_node, std::vector<std::shared_ptr<Node>> *removed_nodes);
std::shared_ptr<framework::OpDesc> op_desc_; std::shared_ptr<framework::OpDesc> op_desc_;
#ifdef PADDLE_MOBILE_DEBUG
std::string ToString(std::string blank, const Node *node) const; std::string ToString(std::string blank, const Node *node) const;
#endif
std::vector<std::shared_ptr<Node>> outputs_; std::vector<std::shared_ptr<Node>> outputs_;
std::vector<Node *> inputs_; std::vector<Node *> inputs_;
std::string type_; std::string type_;
......
...@@ -14,17 +14,17 @@ limitations under the License. */ ...@@ -14,17 +14,17 @@ limitations under the License. */
#pragma once #pragma once
#include <list> //std::list #include <list>
#include <mutex> //std::mutex #include <mutex>
#include <unordered_map> //std::unordered_map #include <unordered_map>
#include "variable.h" #include "variable.h"
namespace paddle_mobile { namespace paddle_mobile {
namespace framework { namespace framework {
class Scope { class Scope {
public: public:
Scope() {} Scope() = default;
~Scope() {} ~Scope() = default;
Scope &NewScope() const; Scope &NewScope() const;
......
...@@ -13,89 +13,19 @@ See the License for the specific language governing permissions and ...@@ -13,89 +13,19 @@ See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include "tensor_util.h" #include "tensor_util.h"
#include <algorithm>
#include <limits>
#include <vector>
namespace paddle_mobile { namespace paddle_mobile {
namespace framework { namespace framework {
void TensorCopy(const Tensor &src, Tensor *dst) { void TensorCopy(const Tensor &src, Tensor *dst) {
// VLOG(3) << "TensorCopy " << src.dims() << " from " <<
// src.place() << " to
// "
// << dst_place;
src.check_memory_size(); src.check_memory_size();
dst->Resize(src.dims()); dst->Resize(src.dims());
dst->set_layout(src.layout()); dst->set_layout(src.layout());
auto src_ptr = src.data<void>(); auto src_ptr = src.data<void>();
auto dst_ptr = dst->mutable_data(src.type()); auto dst_ptr = dst->mutable_data(src.type());
auto size = src.numel() * SizeOfType(src.type()); auto size = src.numel() * SizeOfType(src.type());
memory::Copy(dst_ptr, src_ptr, size); memory::Copy(dst_ptr, src_ptr, size);
} }
void TensorCopySync(const Tensor &src, Tensor *dst) {
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);
}
template <typename Predicate>
struct AnyDTypeVisitor {
Predicate predicate_;
const Tensor &tensor_;
Tensor *out_;
AnyDTypeVisitor(Predicate predicate, const Tensor &tensor, Tensor *out)
: predicate_(predicate), tensor_(tensor), out_(out) {}
template <typename T>
void operator()() const {
// 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();
}
};
struct ContainsNANPredicate {
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();
}
};
struct ContainsInfPredicate {
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();
}
};
struct DeserializedDataFunctor {
DeserializedDataFunctor(void **buf, Tensor *tensor)
: buf_(buf), tensor_(tensor) {}
template <typename T>
void operator()() {
*buf_ = tensor_->mutable_data<T>();
}
void **buf_;
Tensor *tensor_;
};
} // namespace framework } // namespace framework
} // namespace paddle_mobile } // namespace paddle_mobile
...@@ -21,44 +21,6 @@ namespace paddle_mobile { ...@@ -21,44 +21,6 @@ namespace paddle_mobile {
namespace framework { namespace framework {
void TensorCopy(const Tensor &src, Tensor *dst); void TensorCopy(const Tensor &src, Tensor *dst);
void TensorCopySync(const Tensor &src, Tensor *dst);
template <typename T>
void TensorFromVector(const std::vector<T> &src, Tensor *dst);
template <typename T>
void TesnorToVector(const Tensor &src, std::vector<T> *dst);
bool TensorContainsNAN(const framework::Tensor &tensor);
bool TensorContainsInf(const framework::Tensor &tensor);
void TensorToStream(std::ostream &os, const Tensor &tensor);
void TensorFromStream(std::istream &is, Tensor *tensor);
//
// The implementation of template functions.
//
template <typename T>
void TensorFromVector(const std::vector<T> &src, Tensor *dst) {
auto src_ptr = static_cast<const void *>(src.data());
dst->Resize({static_cast<int64_t>(src.size())});
auto dst_ptr = static_cast<void *>(dst->mutable_data<T>());
auto size = src.size() * sizeof(T);
memory::Copy(dst_ptr, src_ptr, size);
}
template <typename T>
void TensorToVector(const Tensor &src, std::vector<T> *dst) {
auto src_ptr = static_cast<const void *>(src.data<T>());
auto size = src.numel() * sizeof(T);
dst->resize(src.numel());
auto dst_ptr = static_cast<void *>(dst->data());
memory::Copy(dst_ptr, src_ptr, size);
}
} // namespace framework } // namespace framework
} // namespace paddle_mobile } // namespace paddle_mobile
...@@ -49,7 +49,7 @@ void BatchNormKernel<CPU, float>::Compute(const BatchNormParam &param) const { ...@@ -49,7 +49,7 @@ void BatchNormKernel<CPU, float>::Compute(const BatchNormParam &param) const {
Tensor inv_std; Tensor inv_std;
auto inv_std_ptr = inv_std.mutable_data<float>(make_ddim({C})); auto inv_std_ptr = inv_std.mutable_data<float>(make_ddim({C}));
if (C != variance->numel()) { if (C != variance->numel()) {
std::cout << "C must equal to variance.numel()" << std::endl; DLOG << "C must equal to variance.numel()";
} }
assert(C == variance->numel()); assert(C == variance->numel());
......
...@@ -46,7 +46,7 @@ build_for_android() { ...@@ -46,7 +46,7 @@ build_for_android() {
if [ "${PLATFORM}" = "arm-v7a" ]; then if [ "${PLATFORM}" = "arm-v7a" ]; then
ABI="armeabi-v7a with NEON" ABI="armeabi-v7a with NEON"
ARM_PLATFORM="V7" ARM_PLATFORM="V7"
CXX_FLAGS="-O3 -std=c++11 -s -march=armv7-a -mfpu=neon -mfloat-abi=softfp -pie -fPIE -w -Wno-error=format-security -llog" CXX_FLAGS="-O3 -std=c++11 -s -march=armv7-a -mfpu=neon -mfloat-abi=softfp -pie -fPIE -w -Wno-error=format-security -fno-exceptions"
elif [ "${PLATFORM}" = "arm-v8a" ]; then elif [ "${PLATFORM}" = "arm-v8a" ]; then
ABI="arm64-v8a" ABI="arm64-v8a"
ARM_PLATFORM="V8" ARM_PLATFORM="V8"
...@@ -62,29 +62,30 @@ build_for_android() { ...@@ -62,29 +62,30 @@ build_for_android() {
TOOLCHAIN_FILE="./tools/android-cmake/android.toolchain.cmake" TOOLCHAIN_FILE="./tools/android-cmake/android.toolchain.cmake"
ANDROID_ARM_MODE="arm" ANDROID_ARM_MODE="arm"
if [ $# -eq 1 ]; then if [ $# -eq 1 ]; then
NET=$1 NET=$1
cmake .. \ cmake .. \
-B"../build/release/${PLATFORM}" \ -B"../build/release/${PLATFORM}" \
-DANDROID_ABI="${ABI}" \ -DANDROID_ABI="${ABI}" \
-DCMAKE_BUILD_TYPE="${MODE}" \ -DCMAKE_BUILD_TYPE="${MODE}" \
-DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \ -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \
-DANDROID_PLATFORM="${ANDROID_PLATFORM_VERSION}" \ -DANDROID_PLATFORM="${ANDROID_PLATFORM_VERSION}" \
-DCMAKE_CXX_FLAGS="${CXX_FLAGS}" \ -DCMAKE_CXX_FLAGS="${CXX_FLAGS}" \
-DANDROID_STL=c++_static \ -DANDROID_STL=c++_static \
-DANDROID=true \ -DANDROID=true \
-D"${NET}"=true \ -D"${NET}=true" \
-D"${ARM_PLATFORM}"=true -D"${ARM_PLATFORM}"=true
else else
cmake .. \
-B"../build/release/${PLATFORM}" \ cmake .. \
-DANDROID_ABI="${ABI}" \ -B"../build/release/${PLATFORM}" \
-DCMAKE_BUILD_TYPE="${MODE}" \ -DANDROID_ABI="${ABI}" \
-DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \ -DCMAKE_BUILD_TYPE="${MODE}" \
-DANDROID_PLATFORM="${ANDROID_PLATFORM_VERSION}" \ -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \
-DCMAKE_CXX_FLAGS="${CXX_FLAGS}" \ -DANDROID_PLATFORM="${ANDROID_PLATFORM_VERSION}" \
-DANDROID_STL=c++_static \ -DCMAKE_CXX_FLAGS="${CXX_FLAGS}" \
-DANDROID=true \ -DANDROID_STL=c++_static \
-D"${ARM_PLATFORM}"=true -DANDROID=true \
-D"${ARM_PLATFORM}"=true
fi fi
cd "../build/release/${PLATFORM}" cd "../build/release/${PLATFORM}"
make -j 8 make -j 8
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册