Replies: 1 comment 12 replies
-
The OmegaConf container types (ListConfig and DictConfig) quack like lists and dicts, respectively. However, as you pointed out, many libraries rely on python-native lists and dicts, and do not play as well with MutableSequences and MutableMappings. For these situations, I'd recommend calling >>> d = {'a': 1, 'b': [1, 2, 3]}
>>> d_cfg = DictConfig(d)
>>> d_obj = OmegaConf.to_object(d_cfg)
>>> print(type(d_obj))
<class 'dict'>
>>> print(type(d_obj["b"]))
<class 'list'>
I have just opened up a feature request (#849) to address this.
It's not possible to do this with OmegaConf currently. If you want a list (not a ListConfig), just use The motivation for converting lists to ListConfigs is so that extra "metadata" can be tracked (beyond what a plain list would be able to do). Examples of this metadata include typing information (enabling runtime type-checking for structured configs) and keeping track of the parent container (which is necessary so that interpolations to a parent node will work). In practice, I find that OmegaConf is most useful during the "configuration" phase; often, practitioners end up converting "back to plain python" when it's time to run the business logic. |
Beta Was this translation helpful? Give feedback.
-
Hi,
Using omegaconf as a wrapper around config dicts led to a few problems with respect to the ListConfig type. Some well known external libraries, i.e. huggingface's transformers check specifically for the inbuild list type for certain function inputs.
For example, when adding additional special tokens to a Tokenizer object, these special tokens have to be of type list. Since I register these in a config, the resulting type within the parsed DictConfig object is ListConfig (does not inherit from list) and the transformer library raises an error.
I saw a related issue wrt PyTorch Lighting and the answer was that it is not recommended to inherit from primitive containers and that it is the responsibility of external libraries to check for e.g. the MutualSequence type instead of the inbuild list type. Unfortunately many libraries don't do that.
An other problem I encountered was the following:
In my config I register a list of hidden layer dimensions for a simple MLP model, e.g.
In my model I have to add the input and output dimensions (data and task dependent) to this object. However a simple
hidden_dims = [128] + hidden_dims + [2]
does not work since
hidden_dims
is of type ListConfig and it cannot be added to a list type object.Since I don't want to manually convert normal list type hyper params to the inbuild list types every time some error is raised, I look for a way to convert all simple ListConfig objects that don't contain a DictConfig structure to the normal list type.
Hence, I tried this simple test, but got:
In the second print statement I expected to see type list, but apperantly the list object is wrapped dynamically to a ListConfig object when it is newly assigned.
So is there any other way of avoiding the ListConfig type for e.g. lists that don't have any further nesting?
I think an option like this would be great because it would allow the user to use all the benefits of DictConfig but he can expect that his list hyperparameters don't cause any problems in external libraries or with operations that work with the list type but not with the ListConfig type.
Thanks for your help and answers :)
Beta Was this translation helpful? Give feedback.
All reactions