Skip to content

Commit

Permalink
Merge pull request #165 from sashafrey/master
Browse files Browse the repository at this point in the history
Improved reporting of errors and exceptions
  • Loading branch information
bigartm committed Mar 19, 2015
2 parents 19b63b8 + e2a41a1 commit 36ba9a8
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 58 deletions.
2 changes: 2 additions & 0 deletions docs/ref/windows_distribution.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ This chapter describes content of BigARTM distribution package for Windows, avai

* `example11_get_theta_matrix.py <https://raw.githubusercontent.com/bigartm/bigartm/master/src/python/examples/example11_get_theta_matrix.py>`_

* `example12_get_topic_model.py <https://raw.githubusercontent.com/bigartm/bigartm/master/src/python/examples/example12_get_topic_model.py>`_

| Files ``docword.kos.txt`` and ``vocab.kos.txt`` represent
| a simple collection of text files in Bag-Of-Words format.
| The files are taken from UCI Machine Learning Repository
Expand Down
2 changes: 2 additions & 0 deletions docs/tutorials/windows_basic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ Folder ``C:\BigARTM\python\examples`` contains several toy examples:
* `example09_regularizers.py <https://raw.githubusercontent.com/bigartm/bigartm/master/src/python/examples/example09_regularizers.py>`_
* `example10_multimodal.py <https://raw.githubusercontent.com/bigartm/bigartm/master/src/python/examples/example10_multimodal.py>`_
* `example11_get_theta_matrix.py <https://raw.githubusercontent.com/bigartm/bigartm/master/src/python/examples/example11_get_theta_matrix.py>`_
* `example12_get_topic_model.py <https://raw.githubusercontent.com/bigartm/bigartm/master/src/python/examples/example12_get_topic_model.py>`_


All examples does not have any parameters, and you may run them without arguments:

Expand Down
6 changes: 3 additions & 3 deletions src/artm/core/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ inline bool make_rpcz_call(std::function<void()> f, const std::string& log_messa
ss << "Network comminication timeout";
if (!log_message.empty()) ss << " in " << log_message;
LOG(ERROR) << ss.str();
if (!no_throw) throw artm::core::NetworkException(ss.str());
if (!no_throw) BOOST_THROW_EXCEPTION(artm::core::NetworkException(ss.str()));
return false;
}

Expand All @@ -126,15 +126,15 @@ inline bool make_rpcz_call(std::function<void()> f, const std::string& log_messa
ss << ", code = " << error.get_application_error_code();
if (!error.get_error_message().empty()) ss << ", error_message = " << error.get_error_message();
LOG(ERROR) << ss.str();
if (!no_throw) throw artm::core::NetworkException(ss.str());
if (!no_throw) BOOST_THROW_EXCEPTION(artm::core::NetworkException(ss.str()));
return false;
}

ss << "Network error";
if (!log_message.empty()) ss << " in " << log_message;
ss << ", rpcz_error_status = " << error.get_status();
LOG(ERROR) << ss.str();
if (!no_throw) throw artm::core::NetworkException(ss.str());
if (!no_throw) BOOST_THROW_EXCEPTION(artm::core::NetworkException(ss.str()));
return false;
}

Expand Down
22 changes: 4 additions & 18 deletions src/artm/core/data_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <vector>
#include <fstream> // NOLINT

#include "boost/exception/diagnostic_information.hpp"
#include "boost/lexical_cast.hpp"
#include "boost/uuid/uuid_io.hpp"
#include "boost/uuid/uuid_generators.hpp"
Expand Down Expand Up @@ -328,17 +329,8 @@ void LocalDataLoader::ThreadFunction() {
DataLoader::PopulateDataStreams(*batch, pi.get());
instance()->processor_queue()->push(pi);
}
}
catch(boost::thread_interrupted&) {
LOG(WARNING) << "thread_interrupted exception in LocalDataLoader::ThreadFunction() function";
return;
}
catch (std::runtime_error& ex) {
LOG(ERROR) << ex.what();
throw;
} catch(...) {
LOG(FATAL) << "Fatal exception in LocalDataLoader::ThreadFunction() function";
throw;
LOG(FATAL) << boost::current_exception_diagnostic_information();
}
}

Expand Down Expand Up @@ -386,7 +378,6 @@ void RemoteDataLoader::ThreadFunction() {
int processor_queue_size = instance()->processor_queue()->size();
int max_queue_size = config.processor_queue_max_size();
if (processor_queue_size >= max_queue_size) {
// Sleep and check for interrupt.
boost::this_thread::sleep(boost::posix_time::milliseconds(kIdleLoopFrequency));
continue;
}
Expand Down Expand Up @@ -444,13 +435,8 @@ void RemoteDataLoader::ThreadFunction() {
}
}
}
catch(boost::thread_interrupted&) {
LOG(WARNING) << "thread_interrupted exception in RemoteDataLoader::ThreadFunction() function";
return;
}
catch(...) {
LOG(FATAL) << "Fatal exception in RemoteDataLoader::ThreadFunction() function";
throw;
catch (...) {
LOG(FATAL) << boost::current_exception_diagnostic_information();
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/artm/core/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ try {
#include <stdexcept>
#include <string>

#include "boost/exception/diagnostic_information.hpp"
#include "boost/exception/get_error_info.hpp"
#include "boost/lexical_cast.hpp"
#include "boost/throw_exception.hpp"
Expand Down Expand Up @@ -97,12 +98,9 @@ catch (const ::artm::core::InternalError& e) { \
} catch (const rpcz::rpc_error& e) { \
set_last_error("rpc_error : " + std::string(e.what())); \
return ARTM_NETWORK_ERROR; \
} catch (const std::runtime_error& e) { \
set_last_error("InternalError : " + std::string(e.what())); \
return ARTM_INTERNAL_ERROR; \
} catch (...) { \
LOG(ERROR) << "unknown critical error."; \
set_last_error("Unknown critical error. "); \
LOG(ERROR) << boost::current_exception_diagnostic_information(); \
set_last_error(boost::current_exception_diagnostic_information()); \
return ARTM_INTERNAL_ERROR; \
}

Expand Down
6 changes: 3 additions & 3 deletions src/artm/core/instance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
} \
if (!need_hot_reconfigure) { \
regularizer.reset(new RegularizerType(regularizer_config)); \
LOG(INFO) << "Regularizer '" + regularizer_name + "' was cold-reconfigured!"; \
LOG(INFO) << "Regularizer '" + regularizer_name + "' was cold-reconfigured"; \
} else { \
LOG(INFO) << "Regularizer '" + regularizer_name + "' was hot-reconfigured!"; \
LOG(INFO) << "Regularizer '" + regularizer_name + "' was hot-reconfigured"; \
} \
} \

Expand Down Expand Up @@ -157,7 +157,7 @@ void Instance::CreateOrReconfigureRegularizer(const RegularizerConfig& config) {
if (need_hot_reconfigure) {
regularizer = schema_.get()->regularizer(regularizer_name);
} else {
LOG(INFO) << "Regularizer '" + regularizer_name + "' will be created!";
LOG(INFO) << "Regularizer '" + regularizer_name + "' will be created";
}

std::string config_blob; // Used by CREATE_OR_RECONFIGURE_REGULARIZER marco
Expand Down
17 changes: 7 additions & 10 deletions src/artm/core/merger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <sstream>

#include "boost/lexical_cast.hpp"
#include "boost/exception/diagnostic_information.hpp"

#include "glog/logging.h"

Expand Down Expand Up @@ -190,10 +191,6 @@ void Merger::ThreadFunction() {
break;
}

// Sleep and check for interrupt.
// To check for interrupt without sleep,
// use boost::this_thread::interruption_point()
// which also throws boost::thread_interrupted
is_idle_ = true;
boost::this_thread::sleep(boost::posix_time::milliseconds(kIdleLoopFrequency));
is_idle_ = false;
Expand Down Expand Up @@ -264,12 +261,8 @@ void Merger::ThreadFunction() {
} // MAIN FOR LOOP
}
}
catch(boost::thread_interrupted&) {
LOG(WARNING) << "thread_interrupted exception in Merger::ThreadFunction() function";
return;
} catch(...) {
LOG(FATAL) << "Fatal exception in Merger::ThreadFunction() function";
throw;
catch(...) {
LOG(FATAL) << boost::current_exception_diagnostic_information();
}
}

Expand Down Expand Up @@ -555,6 +548,10 @@ void Merger::InitializeModel(const InitializeModelArgs& args) {
BOOST_THROW_EXCEPTION(InvalidOperation(ss.str()));
}

LOG(INFO) << "InitializeModel() with "
<< model.topics_count() << " topics and "
<< dict->size() << " tokens";

for (auto iter = dict->begin(); iter != dict->end(); ++iter) {
ClassId class_id = iter->second.has_class_id() ? iter->second.class_id() : DefaultClass;
new_ttm->AddToken(Token(class_id, iter->second.key_token()), true);
Expand Down
21 changes: 6 additions & 15 deletions src/artm/core/processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string>
#include <vector>

#include "boost/exception/diagnostic_information.hpp"
#include "boost/lexical_cast.hpp"
#include "boost/uuid/uuid_generators.hpp"
#include "boost/uuid/uuid_io.hpp"
Expand Down Expand Up @@ -640,10 +641,6 @@ void Processor::ThreadFunction() {
pop_retries++;
LOG_IF(INFO, pop_retries == pop_retries_max) << "No data in processing queue, waiting...";

// Sleep and check for interrupt.
// To check for interrupt without sleep,
// use boost::this_thread::interruption_point()
// which also throws boost::thread_interrupted
boost::this_thread::sleep(boost::posix_time::milliseconds(kIdleLoopFrequency));

continue;
Expand All @@ -652,7 +649,9 @@ void Processor::ThreadFunction() {
LOG_IF(INFO, pop_retries >= pop_retries_max) << "Processing queue has data, processing started";
pop_retries = 0;

CuckooWatch cuckoo("ProcessBatch"); // log time from now to destruction

// CuckooWatch logs time from now to destruction
CuckooWatch cuckoo(std::string("ProcessBatch(") + part->batch().id() + std::string(")"));
total_processed_batches++;

const Batch& batch = part->batch();
Expand Down Expand Up @@ -791,16 +790,8 @@ void Processor::ThreadFunction() {
});
}
}
catch (boost::thread_interrupted&) {
LOG(WARNING) << "thread_interrupted exception in Processor::ThreadFunction() function";
return;
}
catch (std::runtime_error& ex) {
LOG(ERROR) << ex.what();
throw;
} catch(...) {
LOG(FATAL) << "Fatal exception in Processor::ThreadFunction() function";
throw;
catch (...) {
LOG(FATAL) << boost::current_exception_diagnostic_information();
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/artm/core/topic_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ void TopicModel::RetrieveExternalTopicModel(
topic_model, get_model_args.topic_name_size(), get_model_args.topic_name());
}

LOG(INFO) << "RetrieveExternalTopicModel() with "
<< (use_all_topics ? topic_size() : topics_to_use.size()) << " topics, "
<< (use_all_tokens ? token_size() : tokens_to_use.size()) << " tokens";

for (int token_index = 0; token_index < token_size(); ++token_index) {
const Token& current_token = token_collection_.token(token_index);
if (use_all_tokens ||
Expand Down
26 changes: 23 additions & 3 deletions src/artm/utility/blas.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
#include <assert.h>
#include <memory>
#include <vector>

#include "boost/exception/diagnostic_information.hpp"
#include "boost/utility.hpp"

#include "glog/logging.h"

#include "artm/utility/ice.h"
Expand All @@ -31,6 +34,13 @@ typedef void blas_scsr2csc_type(int m, int n, int nnz,
const float *csr_val, const int* csr_row_ptr, const int *csr_col_ind,
float *csc_val, int* csc_row_ind, int* csc_col_ptr);

#define CATCH_BIG_ALLOCATION(no_rows, no_cols) \
catch (...) { \
LOG(ERROR) << "no_rows_ = " << no_rows << ", no_columns_ = " << no_cols << ". " \
<< boost::current_exception_diagnostic_information(); \
throw; \
}

namespace artm {
namespace utility {

Expand Down Expand Up @@ -65,7 +75,9 @@ class DenseMatrix {
store_by_rows_(store_by_rows),
data_(nullptr) {
if (no_rows > 0 && no_columns > 0) {
data_ = new T[no_rows_ * no_columns_];
try {
data_ = new T[no_rows_ * no_columns_];
} CATCH_BIG_ALLOCATION(no_rows_, no_columns_)
}
}

Expand All @@ -74,7 +86,10 @@ class DenseMatrix {
no_columns_ = src_matrix.no_columns();
store_by_rows_ = src_matrix.store_by_rows_;
if (no_columns_ >0 && no_rows_ > 0) {
data_ = new T[no_rows_ * no_columns_];
try {
data_ = new T[no_rows_ * no_columns_];
} CATCH_BIG_ALLOCATION(no_rows_, no_columns_)

for (int i = 0; i < no_rows_ * no_columns_; ++i) {
data_[i] = src_matrix.get_data()[i];
}
Expand Down Expand Up @@ -117,7 +132,10 @@ class DenseMatrix {
delete[] data_;
}
if (no_columns_ >0 && no_rows_ > 0) {
data_ = new T[no_rows_ * no_columns_];
try {
data_ = new T[no_rows_ * no_columns_];
} CATCH_BIG_ALLOCATION(no_rows_, no_columns_);

for (int i = 0; i < no_rows_ * no_columns_; ++i) {
data_[i] = src_matrix.get_data()[i];
}
Expand Down Expand Up @@ -238,4 +256,6 @@ void AssignDenseMatrixByDivision(const DenseMatrix<T>& first_matrix,
} // namespace utility
} // namespace artm

#undef CATCH_BIG_ALLOCATION

#endif // SRC_ARTM_UTILITY_BLAS_H_
2 changes: 1 addition & 1 deletion src/artm_tests/rpcz_canary_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// cd to /src folder and execute the following:
// .\protoc.exe --cpp_out=. --rpcz_plugin_out=. .\artm_tests\rpcz_canary_service.proto

const std::string error_message = "Some error had happened!";
const std::string error_message = "Some error had happened";
const int error_code = -999;

class SearchServiceImpl : public SearchService {
Expand Down
32 changes: 32 additions & 0 deletions src/python/examples/example12_get_topic_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This example demonstrates various ways of retrieving Phi matrix from BigARTM
# -*- coding: utf-8 -*-
import glob

import artm.messages_pb2
import artm.library

# Create master component and infer topic model
batches_disk_path = 'kos'
with artm.library.MasterComponent(disk_path=batches_disk_path) as master:
topic_names = []
for topic_index in range(0, 10):
topic_names.append("Topic" + str(topic_index))
model = master.CreateModel(topic_names=topic_names)

for iteration in range(0, 2):
master.InvokeIteration()
master.WaitIdle() # wait for all batches are processed
model.Synchronize() # synchronize model

# The following code retrieves one topic at a time.
# This avoids retrieving large topic models in a single protobuf message.
print "Output p(w|t) values for the first few tokens (alphabetically) in each topic:"
for topic_name in topic_names:
args = artm.messages_pb2.GetTopicModelArgs()
args.model_name = model.name()
args.topic_name.append(topic_name)
topic_model = master.GetTopicModel(args=args) # represents one column in Phi matrix
print topic_model.topic_name[0],
for i in range(0, 5):
print topic_model.token[i], "%.5f" % topic_model.token_weights[i].value[0],
print "..."

0 comments on commit 36ba9a8

Please sign in to comment.