Skip to content

Commit

Permalink
Fix win32 loading 64-bit model bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oneplus committed May 24, 2015
1 parent 5be4d4d commit 381a8bb
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/framework/featurespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ class ViterbiFeatureSpace {
strncpy(chunk, "featurespace", 16);

ofs.write(chunk, 16);
ofs.write(reinterpret_cast<const char *>(&_offset), sizeof(size_t));
ofs.write(reinterpret_cast<const char *>(&sz), sizeof(size_t));
ofs.write(reinterpret_cast<const char *>(&_offset), sizeof(unsigned long long));
ofs.write(reinterpret_cast<const char *>(&sz), sizeof(unsigned long long));

for (size_t i = 0; i < _num_dicts; ++ i) {
dicts[i].dump(ofs);
Expand All @@ -199,14 +199,14 @@ class ViterbiFeatureSpace {
*/
bool load(std::istream& ifs) {
char chunk[16];
size_t sz;
unsigned long long sz;
ifs.read(chunk, 16);
if (strcmp(chunk, "featurespace")) {
return false;
}

ifs.read(reinterpret_cast<char *>(&_offset), sizeof(size_t));
ifs.read(reinterpret_cast<char *>(&sz), sizeof(size_t));
ifs.read(reinterpret_cast<char *>(&_offset), sizeof(unsigned long long));
ifs.read(reinterpret_cast<char *>(&sz), sizeof(unsigned long long));

if (sz != _num_dicts) {
return false;
Expand Down
11 changes: 7 additions & 4 deletions src/framework/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ class LineCountsReader: public Reader {
size_t nr_lines;
size_t cursor;
size_t interval;
static const int size = 1024*1024;
char* buffer;
public:
LineCountsReader(std::istream& _is): cursor(0), Reader(_is) {
LineCountsReader(std::istream& _is): cursor(0), buffer(0), Reader(_is) {
nr_lines = number_of_lines();
interval = nr_lines / 10;
}

~LineCountsReader() { if (buffer) { delete[](buffer); } }

size_t number_of_lines() {
const int size = 1024*1024;
char buffer[1024*1024];
if (buffer == 0) { buffer = new char[size]; }
size_t retval = 0;

while (true) {
is.read(buffer, 1024*1024);
is.read(buffer, size);
std::streamsize cc = is.gcount();
if (0 == cc) { break; }
for (std::streamsize i = 0; i < cc; ++ i) {
Expand Down
8 changes: 4 additions & 4 deletions src/framework/parameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,13 @@ class Parameters {
strncpy(chunk, "param-nonavg", 16);
}
out.write(chunk, 16);
out.write(reinterpret_cast<const char*>(&_dim), sizeof(size_t));
out.write(reinterpret_cast<const char*>(&_dim), sizeof(unsigned long long));

if (_dim > 0) {
if (opt == kDumpDetails) {
out.write(reinterpret_cast<const char*>(_W), sizeof(double) * _dim);
out.write(reinterpret_cast<const char*>(_W_sum), sizeof(double) * _dim);
out.write(reinterpret_cast<const char*>(&_last_timestamp), sizeof(size_t));
out.write(reinterpret_cast<const char*>(&_last_timestamp), sizeof(unsigned long long));
} else if (opt == kDumpAveraged) {
out.write(reinterpret_cast<const char*>(_W_sum), sizeof(double) * _dim);
} else if (opt == kDumpNonAveraged) {
Expand All @@ -278,14 +278,14 @@ class Parameters {
return false;
}

in.read(reinterpret_cast<char *>(&_dim), sizeof(size_t));
in.read(reinterpret_cast<char *>(&_dim), sizeof(unsigned long long));
if (_dim > 0) {
if (!strncmp(body, "details", 11)) {
_W = new double[_dim];
_W_sum = new double[_dim];
in.read(reinterpret_cast<char *>(_W), sizeof(double)* _dim);
in.read(reinterpret_cast<char *>(_W_sum), sizeof(double)* _dim);
in.read(reinterpret_cast<char *>(&_last_timestamp), sizeof(size_t));
in.read(reinterpret_cast<char *>(&_last_timestamp), sizeof(unsigned long long));
_enable_wrapper = false;
} else if (!strncmp(body, "avg", 11)) {
_W_sum = new double[_dim];
Expand Down
20 changes: 10 additions & 10 deletions src/parser.n/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,29 +294,29 @@ void NeuralNetworkParser::get_cluster_from_dependency(const Dependency& data,
}

template<class Matrix> void NeuralNetworkParser::write_matrix(std::ostream& os, const Matrix& mat) {
typename Matrix::Index rows = mat.rows(), cols = mat.cols();
os.write((char*) (&rows), sizeof(typename Matrix::Index));
os.write((char*) (&cols), sizeof(typename Matrix::Index));
unsigned long long rows = mat.rows(), cols = mat.cols();
os.write((char*) (&rows), sizeof(unsigned long long));
os.write((char*) (&cols), sizeof(unsigned long long));
os.write((char*) mat.data(), rows * cols * sizeof(typename Matrix::Scalar) );
}

template<class Matrix> void NeuralNetworkParser::read_matrix(std::istream& is, Matrix& mat) {
typename Matrix::Index rows=0, cols=0;
is.read((char*) (&rows),sizeof(typename Matrix::Index));
is.read((char*) (&cols),sizeof(typename Matrix::Index));
unsigned long long rows=0, cols=0;
is.read((char*) (&rows),sizeof(unsigned long long));
is.read((char*) (&cols),sizeof(unsigned long long));
mat.resize(rows, cols);
is.read((char *) mat.data() , rows * cols * sizeof(typename Matrix::Scalar));
}

template<class Vector> void NeuralNetworkParser::write_vector(std::ostream& os, const Vector& vec) {
typename Vector::Index rows = vec.rows();
os.write((char*) (&rows), sizeof(typename Vector::Index));
unsigned long long rows = vec.rows();
os.write((char*) (&rows), sizeof(unsigned long long));
os.write((char*) vec.data(), rows * sizeof(typename Vector::Scalar) );
}

template<class Vector> void NeuralNetworkParser::read_vector(std::istream& is, Vector& vec) {
typename Vector::Index rows = 0;
is.read((char*) (&rows), sizeof(typename Vector::Index));
unsigned long long rows = 0;
is.read((char*) (&rows), sizeof(unsigned long long));
vec.resize(rows);
is.read((char *) vec.data() , rows * sizeof(typename Vector::Scalar));
}
Expand Down
2 changes: 1 addition & 1 deletion src/postagger/otpos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ int test(int argc, const char* argv[]) {
std::string output_file = "";
if (vm.count("output")) { output_file = vm["output"].as<std::string>(); }

bool evaluate;
bool evaluate = false;
if (vm.count("evaluate")) { evaluate = vm["evaluate"].as<bool>(); }

PostaggerFrontend frontend(input_file, model_file, lexicon_file, evaluate);
Expand Down
8 changes: 5 additions & 3 deletions src/postagger/postag_dll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ class __ltp_dll_postagger_wrapper : public ltp::postagger::Postagger {
return false;
}

std::ifstream lfs(lexicon_file);
if (lfs.good()) {
lex.load(lfs, model->labels);
if (NULL != lexicon_file) { // MSVC need check this.
std::ifstream lfs(lexicon_file);
if (lfs.good()) {
lex.load(lfs, model->labels);
}
}

return true;
Expand Down
4 changes: 2 additions & 2 deletions src/segmentor/otcws.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ int test(int argc, const char* argv[]) {
"The lexicon file, (optional, if configured, constrained decoding will be performed).")
("input", value<std::string>(), "The path to the reference file.")
("evaluate", value<bool>(),
"if configured, perform evaluation, input words in sentence should be separated by space.")
"if configured, perform evaluation, input words in sentence should be separated by space [default=false].")
("help,h", "Show help information");

if (argc == 1) { std::cerr << optparser << std::endl; return 1; }
Expand Down Expand Up @@ -145,7 +145,7 @@ int test(int argc, const char* argv[]) {
std::string output_file = "";
if (vm.count("output")) { output_file = vm["output"].as<std::string>(); }

bool evaluate;
bool evaluate = false;
if (vm.count("evaluate")) { evaluate = vm["evaluate"].as<bool>(); }

SegmentorFrontend frontend(input_file, model_file, evaluate);
Expand Down

0 comments on commit 381a8bb

Please sign in to comment.