提交 5d7a4caa 编写于 作者: R Ruilong Liu 提交者: GitHub

Merge pull request #184 from codeWorm2015/develop

fix #183 git log printer
......@@ -50,3 +50,7 @@ ADD_EXECUTABLE(paddle-mobile-test test/main.cpp test/test_helper.h
test/elementwise_add_op_test.h test/test_include.h
test/mul_op_test.h)
target_link_libraries(paddle-mobile-test paddle-mobile)
# gen test log
ADD_EXECUTABLE(test-log test/unit-test/test_log.cpp)
target_link_libraries(test-log paddle-mobile)
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
==============================================================================*/
#pragma once
#ifdef PADDLE_MOBILE_DEBUG
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
namespace paddle_mobile {
enum LogLevel {
kNO_LOG,
kLOG_ERROR,
kLOG_WARNING,
kLOG_INFO,
kLOG_DEBUG,
kLOG_DEBUG1,
kLOG_DEBUG2,
kLOG_DEBUG3,
kLOG_DEBUG4
};
// log level
static LogLevel log_level = kLOG_DEBUG4;
static std::vector<std::string> logs{"NO", "ERROR ", "WARNING",
"INFO ", "DEBUG ", "DEBUG1 ",
"DEBUG2 ", "DEBUG3 ", "DEBUG4 "};
struct ToLog;
struct Print {
friend struct ToLog;
template <typename T> Print &operator<<(T const &value) {
buffer_ << value;
return *this;
}
private:
void print(LogLevel level) {
buffer_ << std::endl;
if (level == kLOG_ERROR) {
std::cerr << buffer_.str();
} else {
std::cout << buffer_.str();
}
}
std::ostringstream buffer_;
};
struct ToLog {
ToLog(LogLevel level = kLOG_DEBUG, const std::string &info = "")
: level_(level) {
unsigned blanks =
(unsigned)(level > kLOG_DEBUG ? (level - kLOG_DEBUG) * 4 : 1);
printer_ << logs[level] << " " << info << ":"
<< std::string(blanks, ' ');
}
template <typename T> ToLog &operator<<(T const &value) {
printer_ << value;
return *this;
}
~ToLog() { printer_.print(level_); }
private:
LogLevel level_;
Print printer_;
};
#define LOG(level) \
if (level > paddle_mobile::log_level) { \
} else \
paddle_mobile::ToLog( \
level, \
(std::stringstream() \
<< "[file: " \
<< (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1) : __FILE__) \
<< "] [line: " << __LINE__ << "] ") \
.str())
#define DLOG \
paddle_mobile::ToLog( \
paddle_mobile::kLOG_DEBUG, \
(std::stringstream() \
<< "[file: " \
<< (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1) : __FILE__) \
<< "] [line: " << __LINE__ << "] ") \
.str())
}
#else
namespace paddle_mobile {
enum LogLevel {
kNO_LOG,
kLOG_ERROR,
kLOG_WARNING,
kLOG_INFO,
kLOG_DEBUG,
kLOG_DEBUG1,
kLOG_DEBUG2,
kLOG_DEBUG3,
kLOG_DEBUG4
};
struct ToLog;
struct Print {
friend struct ToLog;
template <typename T> Print &operator<<(T const &value) {}
private:
};
struct ToLog {
ToLog(LogLevel level) {}
template <typename T> ToLog &operator<<(T const &value) {
return *this;
}
};
#define LOG(level) \
if (true) { \
} else \
paddle_mobile::ToLog(level)
#define DLOG \
if (true) { \
} else \
paddle_mobile::ToLog(paddle_mobile::kLOG_DEBUG)
}
#endif
......@@ -35,36 +35,18 @@ namespace paddle_mobile {
const std::vector<std::shared_ptr<BlockDesc>> blocks =
to_predict_program_->Blocks();
// std::cout << " **block size " << blocks.size() << std::endl;
for (int i = 0; i < blocks.size(); ++i) {
std::shared_ptr<BlockDesc> block_desc = blocks[i];
std::vector<std::shared_ptr<OpDesc>> ops = block_desc->Ops();
// std::cout << " ops " << ops.size() << std::endl;
for (int j = 0; j < ops.size(); ++j) {
std::shared_ptr<OpDesc> op = ops[j];
// std::cout << " input 0 " << op->Input("Input")[0]
// << std::endl;
if (op->Type() == "conv2d" &&
op->Input("Input")[0] == "pixel") {
// std::cout << " conv2d attr size: " <<
// op->GetAttrMap().size()
// << std::endl;
// std::cout << " input size: " <<
// op->GetInputs().size() <<
// std::endl;
// std::cout << " output size: " <<
// op->GetOutputs().size() <<
// std::endl;
Attribute strides_attr = op->GetAttrMap().at("strides");
std::vector<int> stride =
strides_attr.Get<std::vector<int>>();
for (int k = 0; k < stride.size(); ++k) {
// std::cout << " stride " << stride[k] <<
// std::endl;
}
std::shared_ptr<operators::ConvOp<Dtype, float>> conv =
std::make_shared<operators::ConvOp<Dtype, float>>(
op->Type(), op->GetInputs(), op->GetOutputs(),
......
......@@ -75,7 +75,6 @@ namespace paddle_mobile {
std::shared_ptr<Scope> scope)
: OperatorBase<Dtype>(type, inputs, outputs, attrs, scope) {}
virtual void InferShape() const = 0;
virtual void Run() const = 0;
};
......
......@@ -19,6 +19,7 @@ SOFTWARE.
#include <fstream>
#include <iostream>
#include "common/log.h"
#include "framework/framework.pb.h"
#include "framework/lod_tensor.h"
#include "framework/program_desc.h"
......@@ -41,25 +42,27 @@ namespace paddle_mobile {
template <typename Dtype, Precision P>
void Loader<Dtype, P>::LoadVar(framework::LoDTensor *tensor,
const std::string &file_path) {
// std::cout << " to load " << file_path << std::endl;
LOG(kLOG_DEBUG) << " to load " << file_path;
// Log(kLOG_DEBUG) << "123";
std::ifstream is(file_path);
std::streampos pos = is.tellg(); // save current position
is.seekg(0, std::ios::end);
// std::cout << " file length = " << is.tellg() << std::endl;
LOG(kLOG_DEBUG) << " file length = " << is.tellg();
is.seekg(pos); // restore saved position
// 1. version
uint32_t version;
is.read(reinterpret_cast<char *>(&version), sizeof(version));
// std::cout << " version: " << version << std::endl;
LOG(kLOG_INFO) << " version: " << version;
// 2 Lod information
uint64_t lod_level;
is.read(reinterpret_cast<char *>(&lod_level), sizeof(lod_level));
// std::cout << " load level: " << lod_level << std::endl;
// std::cout << " lod info: " << std::endl;
LOG(kLOG_DEBUG) << " load level: " << lod_level;
LOG(kLOG_DEBUG) << " lod info: ";
auto &lod = *tensor->mutable_lod();
lod.resize(lod_level);
for (uint64_t i = 0; i < lod_level; ++i) {
......@@ -69,7 +72,7 @@ namespace paddle_mobile {
is.read(reinterpret_cast<char *>(tmp.data()),
static_cast<std::streamsize>(size));
for (int j = 0; j < tmp.size(); ++j) {
// std::cout << " lod - " << tmp[j] << std::endl;
LOG(kLOG_DEBUG1) << " lod - " << tmp[j];
}
lod[i] = tmp;
}
......
......@@ -40,9 +40,9 @@ namespace paddle_mobile {
template <>
void ConvKernel<CPU, float, ConvParam>::Compute(
const ConvParam &param) const {
const Tensor *input = param.Input();
LOG(kLOG_DEBUG) << param;
std::cout << " conv param " << param << std::endl;
const Tensor *input = param.Input();
// The filter will be reshaped in the calculations,
// so here use an assignment operation,
......@@ -57,7 +57,7 @@ namespace paddle_mobile {
std::vector<int> paddings = param.Paddings();
std::vector<int> dilations = param.Dilations();
std::cout << " compute end get Attrs " << strides[0] << std::endl;
DLOG << " compute end get Attrs " << strides[0];
const int batch_size = static_cast<int>(input->dims()[0]);
......@@ -110,10 +110,6 @@ namespace paddle_mobile {
filter.dims()[0], filter.numel() / filter.dims()[0]};
filter.Resize(filter_matrix_shape);
std::cout << " input dim " << input->dims() << std::endl;
std::cout << " output dim " << output->dims() << std::endl;
framework::DDim output_matrix_shape = {
output->dims()[1],
output->numel() / (output->dims()[0] * output->dims()[1])};
......
......@@ -20,25 +20,26 @@ SOFTWARE.
namespace paddle_mobile {
namespace operators {
std::ostream &operator<<(std::ostream &os,
const ConvParam &conv_param) {
os << "parameter of conv: " << std::endl;
os << " stride: "
<< " (" << conv_param.Strides()[0] << conv_param.Strides()[1]
<< ") " << std::endl;
os << " paddings: "
<< " (" << conv_param.Paddings()[0] << conv_param.Paddings()[1]
<< ") " << std::endl;
os << " dilations: "
<< " (" << conv_param.Dilations()[0] << conv_param.Dilations()[1]
<< ") " << std::endl;
os << " groups: " << conv_param.Groups() << std::endl;
os << " input dims: " << conv_param.Input()->dims() << std::endl;
os << " filter dims: " << conv_param.Filter()->dims() << std::endl;
os << " output dims: " << conv_param.Output()->dims() << std::endl;
return os;
Print &operator<<(Print &printer, const ConvParam &conv_param) {
printer << "parameter of conv: "
<< "\n";
printer << " stride: "
<< " (" << conv_param.Strides()[0]
<< conv_param.Strides()[1] << ") "
<< "\n";
printer << " paddings: "
<< " (" << conv_param.Paddings()[0]
<< conv_param.Paddings()[1] << ") "
<< "\n";
printer << " dilations: "
<< " (" << conv_param.Dilations()[0]
<< conv_param.Dilations()[1] << ") "
<< "\n";
printer << " groups: " << conv_param.Groups() << "\n";
printer << " input dims: " << conv_param.Input()->dims() << "\n";
printer << " filter dims: " << conv_param.Filter()->dims() << "\n";
printer << " output dims: " << conv_param.Output()->dims();
return printer;
}
} // namespace operators
} // namespace paddle_mobile
......@@ -18,6 +18,7 @@ SOFTWARE.
#pragma once;
#include "common/log.h"
#include "common/type_define.h"
#include "framework/lod_tensor.h"
#include "framework/scope.h"
......@@ -148,7 +149,7 @@ namespace paddle_mobile {
int groups;
};
std::ostream &operator<<(std::ostream &os, const ConvParam &conv_param);
Print &operator<<(Print &printer, const ConvParam &conv_param);
class ElementwiseAddParam : OpParam {
public:
......
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
==============================================================================*/
#include "common/log.h"
int main() {
LOG(paddle_mobile::kLOG_DEBUG) << "test debug"
<< " next log";
LOG(paddle_mobile::kLOG_DEBUG1) << "test debug1"
<< " next log";
LOG(paddle_mobile::kLOG_DEBUG2) << "test debug2"
<< " next log";
DLOG << "test DLOG";
LOG(paddle_mobile::kLOG_ERROR) << " error occur !";
return 0;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册