Try using status to handle Paddle Error

avx_docs
Yu Yang 8 years ago
parent 0248031601
commit 6c20e08b04

File diff suppressed because it is too large Load Diff

@ -15,6 +15,7 @@ limitations under the License. */
#pragma once #pragma once
#include <string> #include <string>
#include <vector> #include <vector>
#include "paddle/utils/Status.h"
namespace paddle { namespace paddle {
@ -48,7 +49,7 @@ public:
* *
* Usually, act is Layer::output_ * Usually, act is Layer::output_
*/ */
virtual void forward(Argument& act) = 0; virtual Status forward(Argument& act) = 0;
/** /**
* @brief Backward propagaion * @brief Backward propagaion
@ -57,7 +58,7 @@ public:
* - Before calling backward(), act.grad = dE / dy, where E is the error/cost * - Before calling backward(), act.grad = dE / dy, where E is the error/cost
* - After backward() returns, act.grad = dE / dx = (dE/dy) * (dy/dx) * - After backward() returns, act.grad = dE / dx = (dE/dy) * (dy/dx)
*/ */
virtual void backward(Argument& act) = 0; virtual Status backward(Argument& act) = 0;
virtual const std::string& getName() const = 0; virtual const std::string& getName() const = 0;
}; };

@ -16,6 +16,7 @@ limitations under the License. */
#include "paddle/math/SparseMatrix.h" #include "paddle/math/SparseMatrix.h"
#include "paddle/utils/Logging.h" #include "paddle/utils/Logging.h"
#include "paddle/utils/Status.h"
#include "AddtoLayer.h" #include "AddtoLayer.h"
#include "CRFLayer.h" #include "CRFLayer.h"
@ -334,7 +335,8 @@ void Layer::showOutputStats() {
void Layer::forwardActivation() { void Layer::forwardActivation() {
/* activation */ /* activation */
activation_->forward(output_); auto status = activation_->forward(output_);
CHECK(status.isOK()) << status.what();
/* dropout */ /* dropout */
if (config_.drop_rate() > 0) { if (config_.drop_rate() > 0) {
@ -372,7 +374,8 @@ void Layer::backwardActivation() {
oGrad->dotMul(*oGrad, *dropOutMask_); oGrad->dotMul(*oGrad, *dropOutMask_);
} }
activation_->backward(output_); auto status = activation_->backward(output_);
CHECK(status.isOK()) << status.what();
} }
void Layer::forwardDropOut() { void Layer::forwardDropOut() {

@ -11,18 +11,44 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 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. */
#pragma once
#include <memory> #include <memory>
#include <string> #include <string>
namespace paddle { namespace paddle {
/**
* Status is Paddle error code. It only contain a std::string as error message.
* Although Status inherits the std::exception, but do not throw it except you
* know what you are doing.
*/
class Status final : public std::exception { class Status final : public std::exception {
public: public:
/**
* Default Status. OK
*/
Status() noexcept {} Status() noexcept {}
Status(const std::string& msg) : errMsg_(new std::string(msg)) {} /**
* @brief Create Status with error message
* @param msg
*/
explicit Status(const std::string& msg) : errMsg_(new std::string(msg)) {}
/**
* @brief set a error message for status.
* @param msg
*/
inline void set(const std::string& msg) noexcept {
errMsg_.reset(new std::string(msg));
}
virtual const char* what() const noexcept override { /**
* @brief what will return the error message. If status is OK, return nullptr.
*/
const char* what() const noexcept override {
if (errMsg_) { if (errMsg_) {
return errMsg_->data(); return errMsg_->data();
} else { } else {
@ -30,10 +56,14 @@ public:
} }
} }
/**
* @brief isOK
* @return true if OK.
*/
inline bool isOK() const noexcept { return errMsg_ == nullptr; } inline bool isOK() const noexcept { return errMsg_ == nullptr; }
private: private:
std::unique_ptr<std::string> errMsg_; std::shared_ptr<std::string> errMsg_;
}; };
} // namespace paddle } // namespace paddle

@ -4,6 +4,7 @@ add_simple_unittest(test_CustomStackTrace)
add_simple_unittest(test_ThreadBarrier) add_simple_unittest(test_ThreadBarrier)
add_simple_unittest(test_SpinLock) add_simple_unittest(test_SpinLock)
add_simple_unittest(test_SIMDFlags) add_simple_unittest(test_SIMDFlags)
add_simple_unittest(test_Status)
add_executable( add_executable(
test_CustomStackTracePrint test_CustomStackTracePrint

@ -0,0 +1,29 @@
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
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. */
#include "paddle/utils/Status.h"
#include <gtest/gtest.h>
TEST(Status, testAll) {
paddle::Status status;
ASSERT_TRUE(status.isOK());
status.set("I'm the error");
ASSERT_FALSE(status.isOK());
ASSERT_STREQ("I'm the error", status.what());
paddle::Status status2("error2");
ASSERT_FALSE(status2.isOK());
ASSERT_STREQ("error2", status2.what());
}
Loading…
Cancel
Save