in lingvo/core/hyperparams.py [0:0]
def ToText(self, include_types: bool = False, separator: str = ':'):
"""Encodes params into a simple text format.
Each param is represented as a single line in the output. The param
name and value is separated by a ":". The nest param name is
separated by ".". For values of non-trivial types (types other than
int, float, bool, str, and a few, etc.), we just print out the name
of its type.
Note that strings are enclosed in appropriate single or double quotes
(whichever would involve the least escaping) and will have some characters
backslash escaped. String properties can span multiple lines.
Args:
include_types: Should we return types of the values. If True, the types
dict will be returned as a second val in a return tuple
separator: Punctuation symbol used to separate param name and value.
Returns:
The encoded text or (encoded text, types dict) if include_types is True.
"""
def GetRepr(val: Any):
"""Get the representation of `val`."""
if isinstance(val, Params):
return _SortedDict({k: GetRepr(v) for k, v in val.IterParams()})
if isinstance(val, dict):
return _SortedDict({k: GetRepr(v) for k, v in val.items()})
if dataclasses.is_dataclass(val):
return _SortedDict({k: GetRepr(v) for k, v in val.__dict__.items()})
if _IsNamedTuple(val):
return _SortedDict({k: GetRepr(v) for k, v in val._asdict().items()})
if isinstance(val, (list, tuple)):
return type(val)([GetRepr(v) for v in val])
if isinstance(val, (int, float, bool, str, enum.Enum)):
return val
if isinstance(val, tf.DType):
return val.name
if isinstance(val, message.Message):
proto_str = text_format.MessageToString(val, as_one_line=True)
return 'proto/%s/%s/%s' % (inspect.getmodule(val).__name__,
type(val).__name__, proto_str)
if isinstance(val, type):
return 'type/' + inspect.getmodule(val).__name__ + '/' + val.__name__
return type(val).__name__
def _Enter(key: str, p: Any) -> bool:
del key
if isinstance(p, Params):
return True
elif (isinstance(p, (list, tuple)) and
all(isinstance(x, Params) for x in p)):
return True
# TODO(jiahuiyu): Create single-direction DebugString for
# List[(str, Params)] pattern and remove redundancies.
elif (isinstance(p, (list, tuple)) and all(
isinstance(x, tuple) and len(x) == 2 and isinstance(x[0], str) and
isinstance(x[1], Params) for x in p)):
return True
return False
kv = {}
types = {}
def _Visit(key: str, p: Any) -> None:
"""Inserts key-value pairs to 'kv'."""
if isinstance(p, str):
kv[key] = _QuoteString(p)
types[key] = 'str'
else:
kv[key] = str(GetRepr(p))
types[key] = type(p).__name__
self.Visit(_Visit, enter_fn=_Enter)
ret = ''
for (k, v) in sorted(kv.items()):
ret += k + f' {separator} ' + v + '\n'
return (ret, types) if include_types else ret