forked from HazyResearch/hippo-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
28 lines (19 loc) · 777 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import torch
from omegaconf.dictconfig import DictConfig
from munch import Munch
def remove_postfix(text, postfix):
if text.endswith(postfix):
return text[:-len(postfix)]
return text
# pytorch-lightning returns pytorch 0-dim tensor instead of python scalar
def to_scalar(x):
return x.item() if isinstance(x, torch.Tensor) else x
def dictconfig_to_munch(d):
"""Convert object of type OmegaConf to Munch so Wandb can log properly
Support nested dictionary.
"""
return Munch({k: dictconfig_to_munch(v) if isinstance(v, DictConfig)
else v for k, v in d.items()})
def munch_to_dictconfig(m):
return DictConfig({k: munch_to_dictconfig(v) if isinstance(v, Munch)
else v for k, v in m.items()})