Merge pull request #4402 from reyoung/feature/unify_clang_format

Unify clang-format and add some missing clang-format
update-doc-pybind
Yu Yang 8 years ago committed by GitHub
commit e3f242e082

@ -1,5 +0,0 @@
---
Language: Cpp
BasedOnStyle: Google
Standard: Cpp11
...

@ -0,0 +1 @@
../framework/.clang-format

@ -1,5 +0,0 @@
---
Language: Cpp
BasedOnStyle: Google
Standard: Cpp11
...

@ -0,0 +1 @@
../framework/.clang-format

@ -0,0 +1 @@
../framework/.clang-format

@ -46,8 +46,7 @@ struct variant_caster<V<Ts...>> {
template <typename T> template <typename T>
typename std::enable_if< typename std::enable_if<
!std::is_same<T, boost::detail::variant::void_>::value, !std::is_same<T, boost::detail::variant::void_>::value, bool>::type
bool>::type
try_load(handle src, bool convert) { try_load(handle src, bool convert) {
auto caster = make_caster<T>(); auto caster = make_caster<T>();
if (!load_success_ && caster.load(src, convert)) { if (!load_success_ && caster.load(src, convert)) {
@ -71,8 +70,7 @@ struct variant_caster<V<Ts...>> {
return load_success_; return load_success_;
} }
static handle cast(Type const &src, static handle cast(Type const &src, return_value_policy policy,
return_value_policy policy,
handle parent) { handle parent) {
variant_caster_visitor visitor(policy, parent); variant_caster_visitor visitor(policy, parent);
return boost::apply_visitor(visitor, src); return boost::apply_visitor(visitor, src);
@ -101,8 +99,8 @@ inline std::vector<T> RepeatedToVector(
const google::protobuf::RepeatedField<T> &repeated_field) { const google::protobuf::RepeatedField<T> &repeated_field) {
std::vector<T> ret; std::vector<T> ret;
ret.reserve(repeated_field.size()); ret.reserve(repeated_field.size());
std::copy( std::copy(repeated_field.begin(), repeated_field.end(),
repeated_field.begin(), repeated_field.end(), std::back_inserter(ret)); std::back_inserter(ret));
return ret; return ret;
} }
@ -174,8 +172,8 @@ public:
const std::vector<std::string> &Input(const std::string &name) const { const std::vector<std::string> &Input(const std::string &name) const {
auto it = inputs_.find(name); auto it = inputs_.find(name);
PADDLE_ENFORCE( PADDLE_ENFORCE(it != inputs_.end(), "Input %s cannot be found in Op %s",
it != inputs_.end(), "Input %s cannot be found in Op %s", name, Type()); name, Type());
return it->second; return it->second;
} }
@ -196,10 +194,8 @@ public:
const std::vector<std::string> &Output(const std::string &name) const { const std::vector<std::string> &Output(const std::string &name) const {
auto it = outputs_.find(name); auto it = outputs_.find(name);
PADDLE_ENFORCE(it != outputs_.end(), PADDLE_ENFORCE(it != outputs_.end(), "Output %s cannot be found in Op %s",
"Output %s cannot be found in Op %s", name, Type());
name,
Type());
return it->second; return it->second;
} }
@ -349,8 +345,8 @@ public:
VarDescBind *Var(py::bytes name_bytes) const { VarDescBind *Var(py::bytes name_bytes) const {
std::string name = name_bytes; std::string name = name_bytes;
auto it = vars_.find(name); auto it = vars_.find(name);
PADDLE_ENFORCE( PADDLE_ENFORCE(it != vars_.end(),
it != vars_.end(), "Can not find variable %s in current block.", name); "Can not find variable %s in current block.", name);
return it->second.get(); return it->second.get();
} }
@ -492,8 +488,7 @@ void BindProgramDesc(py::module &m) {
return &ProgramDescBind::Instance(prog_desc); return &ProgramDescBind::Instance(prog_desc);
}, },
py::return_value_policy::reference) py::return_value_policy::reference)
.def("append_block", .def("append_block", &ProgramDescBind::AppendBlock,
&ProgramDescBind::AppendBlock,
py::return_value_policy::reference) py::return_value_policy::reference)
.def("block", &ProgramDescBind::Block, py::return_value_policy::reference) .def("block", &ProgramDescBind::Block, py::return_value_policy::reference)
.def("__str__", &ProgramDescBind::DebugString) .def("__str__", &ProgramDescBind::DebugString)
@ -504,20 +499,16 @@ void BindBlockDesc(py::module &m) {
py::class_<BlockDescBind>(m, "BlockDesc", "") py::class_<BlockDescBind>(m, "BlockDesc", "")
.def_property_readonly("id", &BlockDescBind::ID) .def_property_readonly("id", &BlockDescBind::ID)
.def_property_readonly("parent", &BlockDescBind::Parent) .def_property_readonly("parent", &BlockDescBind::Parent)
.def("append_op", .def("append_op", &BlockDescBind::AppendOp,
&BlockDescBind::AppendOp, py::return_value_policy::reference)
.def("prepend_op", &BlockDescBind::PrependOp,
py::return_value_policy::reference) py::return_value_policy::reference)
.def("prepend_op", .def("new_var", &BlockDescBind::NewVar,
&BlockDescBind::PrependOp,
py::return_value_policy::reference) py::return_value_policy::reference)
.def(
"new_var", &BlockDescBind::NewVar, py::return_value_policy::reference)
.def("var", &BlockDescBind::Var, py::return_value_policy::reference) .def("var", &BlockDescBind::Var, py::return_value_policy::reference)
.def("all_vars", .def("all_vars", &BlockDescBind::AllVars,
&BlockDescBind::AllVars,
py::return_value_policy::reference) py::return_value_policy::reference)
.def("all_ops", .def("all_ops", &BlockDescBind::AllOps,
&BlockDescBind::AllOps,
py::return_value_policy::reference); py::return_value_policy::reference);
} }

@ -161,8 +161,7 @@ All parameter, weight, gradient are variables in Paddle.
py::return_value_policy::reference) py::return_value_policy::reference)
.def("find_var", &Scope::FindVar, py::return_value_policy::reference) .def("find_var", &Scope::FindVar, py::return_value_policy::reference)
.def(py::init<>()) .def(py::init<>())
.def("new_scope", .def("new_scope", [](Scope &self) -> Scope * { return &self.NewScope(); },
[](Scope &self) -> Scope * { return &self.NewScope(); },
py::return_value_policy::reference) py::return_value_policy::reference)
.def("drop_kids", &Scope::DropKids); .def("drop_kids", &Scope::DropKids);
@ -230,8 +229,7 @@ All parameter, weight, gradient are variables in Paddle.
}) })
.def("infer_shape", &OperatorBase::InferShape) .def("infer_shape", &OperatorBase::InferShape)
.def("run", .def("run",
[](OperatorBase &self, [](OperatorBase &self, const Scope &scope,
const Scope &scope,
const platform::DeviceContext &dev_ctx) { const platform::DeviceContext &dev_ctx) {
self.Run(scope, dev_ctx); self.Run(scope, dev_ctx);
dev_ctx.Wait(); dev_ctx.Wait();
@ -259,10 +257,8 @@ All parameter, weight, gradient are variables in Paddle.
retv->SetType("plain_net"); retv->SetType("plain_net");
return retv; return retv;
}) })
.def("append_op", .def("append_op", [](operators::NetOp &self,
[](operators::NetOp &self, const OperatorBase &op) { const OperatorBase &op) { self.AppendOp(op); })
self.AppendOp(op);
})
.def("complete_add_op", &operators::NetOp::CompleteAddOp) .def("complete_add_op", &operators::NetOp::CompleteAddOp)
.def("complete_add_op", [](std::shared_ptr<operators::NetOp> &self) { .def("complete_add_op", [](std::shared_ptr<operators::NetOp> &self) {
self->CompleteAddOp(); self->CompleteAddOp();
@ -282,9 +278,10 @@ All parameter, weight, gradient are variables in Paddle.
auto rnn_op = OpRegistry::CreateOp(desc); auto rnn_op = OpRegistry::CreateOp(desc);
return static_cast<operators::RecurrentOp *>(rnn_op.release()); return static_cast<operators::RecurrentOp *>(rnn_op.release());
}) })
.def("set_stepnet", .def("set_stepnet", [](operators::RecurrentOp &self,
[](operators::RecurrentOp &self, const operators::NetOp &net) const operators::NetOp &net) -> void {
-> void { self.set_stepnet(net.Clone()); }); self.set_stepnet(net.Clone());
});
// cond_op // cond_op
py::class_<operators::CondOp, OperatorBase>(m, "CondOp") py::class_<operators::CondOp, OperatorBase>(m, "CondOp")

@ -63,11 +63,8 @@ struct CastToPyBufferImpl<true, I, ARGS...> {
} }
return py::buffer_info( return py::buffer_info(
dst_tensor.mutable_data<CUR_TYPE>(dst_tensor.holder_->place()), dst_tensor.mutable_data<CUR_TYPE>(dst_tensor.holder_->place()),
sizeof(CUR_TYPE), sizeof(CUR_TYPE), py::format_descriptor<CUR_TYPE>::format(),
py::format_descriptor<CUR_TYPE>::format(), (size_t)framework::arity(dst_tensor.dims()), dims_outside, strides);
(size_t)framework::arity(dst_tensor.dims()),
dims_outside,
strides);
} else { } else {
constexpr bool less = I + 1 < std::tuple_size<std::tuple<ARGS...>>::value; constexpr bool less = I + 1 < std::tuple_size<std::tuple<ARGS...>>::value;
return CastToPyBufferImpl<less, I + 1, ARGS...>()(tensor); return CastToPyBufferImpl<less, I + 1, ARGS...>()(tensor);
@ -110,8 +107,8 @@ void PyCUDATensorSetFromArray(
self.Resize(framework::make_ddim(dims)); self.Resize(framework::make_ddim(dims));
auto *dst = self.mutable_data<T>(place); auto *dst = self.mutable_data<T>(place);
paddle::platform::GpuMemcpySync( paddle::platform::GpuMemcpySync(dst, array.data(), sizeof(T) * array.size(),
dst, array.data(), sizeof(T) * array.size(), cudaMemcpyHostToDevice); cudaMemcpyHostToDevice);
} }
#endif #endif

@ -0,0 +1 @@
../framework/.clang-format

@ -11,6 +11,6 @@ TEST(StringPrintf, StringPrintf) {
long hour = 14; long hour = 14;
int min = 44; int min = 44;
EXPECT_EQ(std::string("Wednesday, July 27, 14:44"), EXPECT_EQ(std::string("Wednesday, July 27, 14:44"),
paddle::string::Sprintf( paddle::string::Sprintf("%s, %s %d, %.2d:%.2d", weekday, month, day,
"%s, %s %d, %.2d:%.2d", weekday, month, day, hour, min)); hour, min));
} }

@ -156,8 +156,7 @@ public:
// Format the value by casting to type fmtT. This default implementation // Format the value by casting to type fmtT. This default implementation
// should never be called. // should never be called.
template <typename T, template <typename T, typename fmtT,
typename fmtT,
bool convertible = is_convertible<T, fmtT>::value> bool convertible = is_convertible<T, fmtT>::value>
struct formatValueAsType { struct formatValueAsType {
static void invoke(std::ostream & /*out*/, const T & /*value*/) { assert(0); } static void invoke(std::ostream & /*out*/, const T & /*value*/) { assert(0); }
@ -227,11 +226,8 @@ TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(char)
/// operator<< to format the type T, with special cases for the %c and %p /// operator<< to format the type T, with special cases for the %c and %p
/// conversions. /// conversions.
template <typename T> template <typename T>
inline void formatValue(std::ostream &out, inline void formatValue(std::ostream &out, const char * /*fmtBegin*/,
const char * /*fmtBegin*/, const char *fmtEnd, int ntrunc, const T &value) {
const char *fmtEnd,
int ntrunc,
const T &value) {
// The mess here is to support the %c and %p conversions: if these // The mess here is to support the %c and %p conversions: if these
// conversions are active we try to convert the type to a char or const // conversions are active we try to convert the type to a char or const
// void* respectively and format that instead of the value itself. For the // void* respectively and format that instead of the value itself. For the
@ -254,11 +250,8 @@ inline void formatValue(std::ostream &out,
// Overloaded version for char types to support printing as an integer // Overloaded version for char types to support printing as an integer
#define TINYFORMAT_DEFINE_FORMATVALUE_CHAR(charType) \ #define TINYFORMAT_DEFINE_FORMATVALUE_CHAR(charType) \
inline void formatValue(std::ostream &out, \ inline void formatValue(std::ostream &out, const char * /*fmtBegin*/, \
const char * /*fmtBegin*/, \ const char *fmtEnd, int /**/, charType value) { \
const char *fmtEnd, \
int /**/, \
charType value) { \
switch (*(fmtEnd - 1)) { \ switch (*(fmtEnd - 1)) { \
case 'u': \ case 'u': \
case 'd': \ case 'd': \
@ -477,9 +470,7 @@ public:
m_formatImpl(&formatImpl<T>), m_formatImpl(&formatImpl<T>),
m_toIntImpl(&toIntImpl<T>) {} m_toIntImpl(&toIntImpl<T>) {}
void format(std::ostream &out, void format(std::ostream &out, const char *fmtBegin, const char *fmtEnd,
const char *fmtBegin,
const char *fmtEnd,
int ntrunc) const { int ntrunc) const {
m_formatImpl(out, fmtBegin, fmtEnd, ntrunc, m_value); m_formatImpl(out, fmtBegin, fmtEnd, ntrunc, m_value);
} }
@ -488,11 +479,8 @@ public:
private: private:
template <typename T> template <typename T>
static void formatImpl(std::ostream &out, static void formatImpl(std::ostream &out, const char *fmtBegin,
const char *fmtBegin, const char *fmtEnd, int ntrunc, const void *value) {
const char *fmtEnd,
int ntrunc,
const void *value) {
formatValue(out, fmtBegin, fmtEnd, ntrunc, *static_cast<const T *>(value)); formatValue(out, fmtBegin, fmtEnd, ntrunc, *static_cast<const T *>(value));
} }
@ -502,11 +490,8 @@ private:
} }
const void *m_value; const void *m_value;
void (*m_formatImpl)(std::ostream &out, void (*m_formatImpl)(std::ostream &out, const char *fmtBegin,
const char *fmtBegin, const char *fmtEnd, int ntrunc, const void *value);
const char *fmtEnd,
int ntrunc,
const void *value);
int (*m_toIntImpl)(const void *value); int (*m_toIntImpl)(const void *value);
}; };
@ -555,12 +540,10 @@ inline const char *printFormatStringLiteral(std::ostream &out,
// necessary to pull out variable width and precision . The function returns a // necessary to pull out variable width and precision . The function returns a
// pointer to the character after the end of the current format spec. // pointer to the character after the end of the current format spec.
inline const char *streamStateFromFormat(std::ostream &out, inline const char *streamStateFromFormat(std::ostream &out,
bool &spacePadPositive, bool &spacePadPositive, int &ntrunc,
int &ntrunc,
const char *fmtStart, const char *fmtStart,
const detail::FormatArg *formatters, const detail::FormatArg *formatters,
int &argIndex, int &argIndex, int numFormatters) {
int numFormatters) {
if (*fmtStart != '%') { if (*fmtStart != '%') {
TINYFORMAT_ERROR( TINYFORMAT_ERROR(
"tinyformat: Not enough conversion specifiers in format string"); "tinyformat: Not enough conversion specifiers in format string");
@ -736,10 +719,8 @@ inline const char *streamStateFromFormat(std::ostream &out,
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
inline void formatImpl(std::ostream &out, inline void formatImpl(std::ostream &out, const char *fmt,
const char *fmt, const detail::FormatArg *formatters, int numFormatters) {
const detail::FormatArg *formatters,
int numFormatters) {
// Saved stream state // Saved stream state
std::streamsize origWidth = out.width(); std::streamsize origWidth = out.width();
std::streamsize origPrecision = out.precision(); std::streamsize origPrecision = out.precision();
@ -751,13 +732,9 @@ inline void formatImpl(std::ostream &out,
fmt = printFormatStringLiteral(out, fmt); fmt = printFormatStringLiteral(out, fmt);
bool spacePadPositive = false; bool spacePadPositive = false;
int ntrunc = -1; int ntrunc = -1;
const char *fmtEnd = streamStateFromFormat(out, const char *fmtEnd =
spacePadPositive, streamStateFromFormat(out, spacePadPositive, ntrunc, fmt, formatters,
ntrunc, argIndex, numFormatters);
fmt,
formatters,
argIndex,
numFormatters);
if (argIndex >= numFormatters) { if (argIndex >= numFormatters) {
// Check args remain after reading any variable width/precision // Check args remain after reading any variable width/precision
TINYFORMAT_ERROR("tinyformat: Not enough format arguments"); TINYFORMAT_ERROR("tinyformat: Not enough format arguments");
@ -810,8 +787,7 @@ public:
FormatList(detail::FormatArg *formatters, int N) FormatList(detail::FormatArg *formatters, int N)
: m_formatters(formatters), m_N(N) {} : m_formatters(formatters), m_N(N) {}
friend void vformat(std::ostream &out, friend void vformat(std::ostream &out, const char *fmt,
const char *fmt,
const FormatList &list); const FormatList &list);
private: private:

Loading…
Cancel
Save