-
Notifications
You must be signed in to change notification settings - Fork 152
Feat/improve_config #595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/improve_config #595
Conversation
然后有几个问题:
|
相关测试我添加在了测试脚本里,但是这只是一个示例,测试脚本可能还需要更改: def test_insert_class(self):
"""
测试插入类
"""
from collections.abc import MutableMapping
class Test(MutableMapping):
def __init__(self, a, b):
self.data = {"a": a, "b": b}
def __setitem__(self, __key, __value):
self.data[__key] = __value
def __delitem__(self, __key):
del self.data[__key]
def __getitem__(self, __key):
return self.data.get(__key, None)
def __len__(self):
return len(self.data)
def __iter__(self):
return iter(self.data)
config_data = {
"a": 1,
"b": "mnist",
"c/d": [1, 2, 3],
"e/f/h": {"a": 1, "b": {"c": 2}},
"test": Test(1, 2)
}
run = SwanLabRun(run_config=config_data)
assert run.config.test.a == 1
assert run.config.test.b == 2
def test_not_json_serializable(self):
"""
测试不可json化的数据
"""
import math, json
config_data = {
"a": 1,
"b": "mnist",
"c/d": [1, 2, 3],
"e/f/h": {"a": 1, "b": {"c": 2}},
"test": math.nan
}
run = SwanLabRun(run_config=config_data)
json_data = json.dumps(SwanLabRun.config) |
其他大致上应该没什么问题,主要是 |
对于config的value是一个类实例的情况,目前统一转成str(类的类型太多,实例化难以统一),所以插入类的操作暂时实现不了。 |
那这样似乎没法解决omegaconfig的问题? |
omegaconfig就特殊处理就可以,当前代码中已经适配(之前的版本已经适配了omegaconf,用json_serializable函数就可以,这次是更细致的适配) |
不用这么复杂,你看omegaconfig的源码就知道返回值继承自 |
不是,是因为omegaconfig直接转成dict的话,和目标效果不一致,所以用了 |
可以针对MutableMapping类型做特定优化 |
第三方处理可以有,但是应该有一个更加通用的处理规则:
2和3属于通用方法,但是不排除外界使用写出bug,所以需做好一些提示 |
因此最终保存在SwanLabConfig里的是一个对象,只有在执行相关魔术方法的时候才会进行json序列化 需要注意的是假如传入了一个test对象: # 此时会触发__setitem__,因此会触发save事件
config["test"] = test
# 此时会触发__setattr__,因此会触发save事件
config.test = test
# 此时不会触发save事件
config["test"].a = 1
config.test.a = 1
# 如果此情况需要触发save事件,应该通过此方法手动触发
config.save() 上述情况需要在相关文档中注明 |
* Feat/improve_config (#595) * Create pytest_config.py * update config location * config clean * update more pytest * add omegaconf pytest * Update pytest_config.py * pytest add get_config * Update pytest_config.py * update run.config.clean * update some details * fmt import * update requriement * rename config * change type hit * Update pytest_config.py * update clean * update config * Update config.py * update check_config and save * __save__ to save --------- Co-authored-by: KAAANG <[email protected]> * update pytest and del init_need --------- Co-authored-by: KAAANG <[email protected]>
Description
Close: #594
其中对于在swanlab.init前定义swanlab.config的逻辑:
swanlab.init
前,使用swanlab.config.update
或swanlab.config.set
等方式设置key, value,swanlab.init
的时候config会被更新;swanlab.config={...}
的方式,相当于把SwanLabConfig
类变成了一个dict,那么swanlab.init
的时候config并不会被swanlab.config的内容更新,且swanlab.config失去了原有的诸多效果,但run.config并不受影响,代码的运行也不受影响