Test int8 models (#107)
* Test int8 models * Fix displaying help messages * small fixes * Fix jni test
This commit is contained in:
@@ -7,7 +7,11 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
// This file is copied/modified from
|
||||
@@ -15,6 +19,102 @@
|
||||
|
||||
namespace sherpa_onnx {
|
||||
|
||||
// copied from kaldi/src/util/text-util.cc
|
||||
template <class T>
|
||||
class NumberIstream {
|
||||
public:
|
||||
explicit NumberIstream(std::istream &i) : in_(i) {}
|
||||
|
||||
NumberIstream &operator>>(T &x) {
|
||||
if (!in_.good()) return *this;
|
||||
in_ >> x;
|
||||
if (!in_.fail() && RemainderIsOnlySpaces()) return *this;
|
||||
return ParseOnFail(&x);
|
||||
}
|
||||
|
||||
private:
|
||||
std::istream &in_;
|
||||
|
||||
bool RemainderIsOnlySpaces() {
|
||||
if (in_.tellg() != std::istream::pos_type(-1)) {
|
||||
std::string rem;
|
||||
in_ >> rem;
|
||||
|
||||
if (rem.find_first_not_of(' ') != std::string::npos) {
|
||||
// there is not only spaces
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
in_.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
NumberIstream &ParseOnFail(T *x) {
|
||||
std::string str;
|
||||
in_.clear();
|
||||
in_.seekg(0);
|
||||
// If the stream is broken even before trying
|
||||
// to read from it or if there are many tokens,
|
||||
// it's pointless to try.
|
||||
if (!(in_ >> str) || !RemainderIsOnlySpaces()) {
|
||||
in_.setstate(std::ios_base::failbit);
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, T> inf_nan_map;
|
||||
// we'll keep just uppercase values.
|
||||
inf_nan_map["INF"] = std::numeric_limits<T>::infinity();
|
||||
inf_nan_map["+INF"] = std::numeric_limits<T>::infinity();
|
||||
inf_nan_map["-INF"] = -std::numeric_limits<T>::infinity();
|
||||
inf_nan_map["INFINITY"] = std::numeric_limits<T>::infinity();
|
||||
inf_nan_map["+INFINITY"] = std::numeric_limits<T>::infinity();
|
||||
inf_nan_map["-INFINITY"] = -std::numeric_limits<T>::infinity();
|
||||
inf_nan_map["NAN"] = std::numeric_limits<T>::quiet_NaN();
|
||||
inf_nan_map["+NAN"] = std::numeric_limits<T>::quiet_NaN();
|
||||
inf_nan_map["-NAN"] = -std::numeric_limits<T>::quiet_NaN();
|
||||
// MSVC
|
||||
inf_nan_map["1.#INF"] = std::numeric_limits<T>::infinity();
|
||||
inf_nan_map["-1.#INF"] = -std::numeric_limits<T>::infinity();
|
||||
inf_nan_map["1.#QNAN"] = std::numeric_limits<T>::quiet_NaN();
|
||||
inf_nan_map["-1.#QNAN"] = -std::numeric_limits<T>::quiet_NaN();
|
||||
|
||||
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
|
||||
|
||||
if (inf_nan_map.find(str) != inf_nan_map.end()) {
|
||||
*x = inf_nan_map[str];
|
||||
} else {
|
||||
in_.setstate(std::ios_base::failbit);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
/// ConvertStringToReal converts a string into either float or double
|
||||
/// and returns false if there was any kind of problem (i.e. the string
|
||||
/// was not a floating point number or contained extra non-whitespace junk).
|
||||
/// Be careful- this function will successfully read inf's or nan's.
|
||||
template <typename T>
|
||||
bool ConvertStringToReal(const std::string &str, T *out) {
|
||||
std::istringstream iss(str);
|
||||
|
||||
NumberIstream<T> i(iss);
|
||||
|
||||
i >> *out;
|
||||
|
||||
if (iss.fail()) {
|
||||
// Number conversion failed.
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template bool ConvertStringToReal<float>(const std::string &str, float *out);
|
||||
|
||||
template bool ConvertStringToReal<double>(const std::string &str, double *out);
|
||||
|
||||
void SplitStringToVector(const std::string &full, const char *delim,
|
||||
bool omit_empty_strings,
|
||||
std::vector<std::string> *out) {
|
||||
@@ -43,7 +143,9 @@ bool SplitStringToFloats(const std::string &full, const char *delim,
|
||||
out->resize(split.size());
|
||||
for (size_t i = 0; i < split.size(); ++i) {
|
||||
// assume atof never fails
|
||||
(*out)[i] = atof(split[i].c_str());
|
||||
F f = 0;
|
||||
if (!ConvertStringToReal(split[i], &f)) return false;
|
||||
(*out)[i] = f;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user