Skip to content

Commit

Permalink
parse option in64_t (k2-fsa#1089)
Browse files Browse the repository at this point in the history
Signed-off-by: Manix <[email protected]>
  • Loading branch information
manickavela29 authored Jul 8, 2024
1 parent 0f699f4 commit d874da9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
36 changes: 36 additions & 0 deletions sherpa-onnx/csrc/parse-options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ void ParseOptions::Register(const std::string &name, int32_t *ptr,
RegisterTmpl(name, ptr, doc);
}

void ParseOptions::Register(const std::string &name, int64_t *ptr,
const std::string &doc) {
RegisterTmpl(name, ptr, doc);
}

void ParseOptions::Register(const std::string &name, uint32_t *ptr,
const std::string &doc) {
RegisterTmpl(name, ptr, doc);
Expand Down Expand Up @@ -125,6 +130,15 @@ void ParseOptions::RegisterSpecific(const std::string &name,
doc_map_[idx] = DocInfo(name, ss.str(), is_standard);
}

void ParseOptions::RegisterSpecific(const std::string &name,
const std::string &idx, int64_t *i,
const std::string &doc, bool is_standard) {
int64_map_[idx] = i;
std::ostringstream ss;
ss << doc << " (int64, default = " << *i << ")";
doc_map_[idx] = DocInfo(name, ss.str(), is_standard);
}

void ParseOptions::RegisterSpecific(const std::string &name,
const std::string &idx, uint32_t *u,
const std::string &doc, bool is_standard) {
Expand Down Expand Up @@ -172,6 +186,7 @@ void ParseOptions::DisableOption(const std::string &name) {
}
bool_map_.erase(name);
int_map_.erase(name);
int64_map_.erase(name);
uint_map_.erase(name);
float_map_.erase(name);
double_map_.erase(name);
Expand Down Expand Up @@ -411,6 +426,8 @@ void ParseOptions::PrintConfig(std::ostream &os) const {
os << (*bool_map_.at(key) ? "true" : "false");
} else if (int_map_.end() != int_map_.find(key)) {
os << (*int_map_.at(key));
} else if (int64_map_.end() != int64_map_.find(key)) {
os << (*int64_map_.at(key));
} else if (uint_map_.end() != uint_map_.find(key)) {
os << (*uint_map_.at(key));
} else if (float_map_.end() != float_map_.find(key)) {
Expand Down Expand Up @@ -533,6 +550,8 @@ bool ParseOptions::SetOption(const std::string &key, const std::string &value,
*(bool_map_[key]) = ToBool(value);
} else if (int_map_.end() != int_map_.find(key)) {
*(int_map_[key]) = ToInt(value);
} else if (int64_map_.end() != int64_map_.find(key)) {
*(int64_map_[key]) = ToInt64(value);
} else if (uint_map_.end() != uint_map_.find(key)) {
*(uint_map_[key]) = ToUint(value);
} else if (float_map_.end() != float_map_.find(key)) {
Expand Down Expand Up @@ -580,6 +599,15 @@ int32_t ParseOptions::ToInt(const std::string &str) const {
return ret;
}

int64_t ParseOptions::ToInt64(const std::string &str) const {
int64_t ret = 0;
if (!ConvertStringToInteger(str, &ret)) {
SHERPA_ONNX_LOGE("Invalid integer int64 option \"%s\"", str.c_str());
exit(-1);
}
return ret;
}

uint32_t ParseOptions::ToUint(const std::string &str) const {
uint32_t ret = 0;
if (!ConvertStringToInteger(str, &ret)) {
Expand Down Expand Up @@ -612,6 +640,8 @@ template void ParseOptions::RegisterTmpl(const std::string &name, bool *ptr,
const std::string &doc);
template void ParseOptions::RegisterTmpl(const std::string &name, int32_t *ptr,
const std::string &doc);
template void ParseOptions::RegisterTmpl(const std::string &name, int64_t *ptr,
const std::string &doc);
template void ParseOptions::RegisterTmpl(const std::string &name, uint32_t *ptr,
const std::string &doc);
template void ParseOptions::RegisterTmpl(const std::string &name, float *ptr,
Expand All @@ -627,6 +657,9 @@ template void ParseOptions::RegisterStandard(const std::string &name, bool *ptr,
template void ParseOptions::RegisterStandard(const std::string &name,
int32_t *ptr,
const std::string &doc);
template void ParseOptions::RegisterStandard(const std::string &name,
int64_t *ptr,
const std::string &doc);
template void ParseOptions::RegisterStandard(const std::string &name,
uint32_t *ptr,
const std::string &doc);
Expand All @@ -646,6 +679,9 @@ template void ParseOptions::RegisterCommon(const std::string &name, bool *ptr,
template void ParseOptions::RegisterCommon(const std::string &name,
int32_t *ptr, const std::string &doc,
bool is_standard);
template void ParseOptions::RegisterCommon(const std::string &name,
int64_t *ptr, const std::string &doc,
bool is_standard);
template void ParseOptions::RegisterCommon(const std::string &name,
uint32_t *ptr,
const std::string &doc,
Expand Down
6 changes: 6 additions & 0 deletions sherpa-onnx/csrc/parse-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class ParseOptions {

void Register(const std::string &name, bool *ptr, const std::string &doc);
void Register(const std::string &name, int32_t *ptr, const std::string &doc);
void Register(const std::string &name, int64_t *ptr, const std::string &doc);
void Register(const std::string &name, uint32_t *ptr, const std::string &doc);
void Register(const std::string &name, float *ptr, const std::string &doc);
void Register(const std::string &name, double *ptr, const std::string &doc);
Expand Down Expand Up @@ -134,6 +135,9 @@ class ParseOptions {
/// Register int32_t variable
void RegisterSpecific(const std::string &name, const std::string &idx,
int32_t *i, const std::string &doc, bool is_standard);
/// Register int64_t variable
void RegisterSpecific(const std::string &name, const std::string &idx,
int64_t *i, const std::string &doc, bool is_standard);
/// Register unsigned int32_t variable
void RegisterSpecific(const std::string &name, const std::string &idx,
uint32_t *u, const std::string &doc, bool is_standard);
Expand Down Expand Up @@ -163,13 +167,15 @@ class ParseOptions {

bool ToBool(std::string str) const;
int32_t ToInt(const std::string &str) const;
int64_t ToInt64(const std::string &str) const;
uint32_t ToUint(const std::string &str) const;
float ToFloat(const std::string &str) const;
double ToDouble(const std::string &str) const;

// maps for option variables
std::unordered_map<std::string, bool *> bool_map_;
std::unordered_map<std::string, int32_t *> int_map_;
std::unordered_map<std::string, int64_t *> int64_map_;
std::unordered_map<std::string, uint32_t *> uint_map_;
std::unordered_map<std::string, float *> float_map_;
std::unordered_map<std::string, double *> double_map_;
Expand Down

0 comments on commit d874da9

Please sign in to comment.