Excore

Latest version: v0.1.1

Safety actively analyzes 638452 Python packages for vulnerabilities to keep your Python projects secure.

Scan your dependencies

Page 2 of 2

0.01

If you want to call a class or static method.

toml
[Model.XXX]
$backbone = "A.from_pretained()"


Attributes can also be used.

toml
[Model.XXX]
!channel = "$Block.out_channel"


It also can be chained invoke.

toml
[Model.XXX]
!channel = "$Block.last_conv.out_channels"


This way requsts you to define such methods or attributes in target class and can not pass arguments. So `ExCore` provides `ConfigArgumentHook`.

python
class ConfigArgumentHook(node, enabled)


You need to implements your own class inherited from `ConfigArgumentHook`. For example:

python
from excore.engine.hook import ConfigArgumentHook

from . import HOOKS


HOOKS.register()
class BnWeightDecayHook(ConfigArgumentHook):
def __init__(self, node, enabled: bool, bn_weight_decay: bool, weight_decay: float):
super().__init__(node, enabled)
self.bn_weight_decay = bn_weight_decay
self.weight_decay = weight_decay

def hook(self):
model = self.node()
if self.bn_weight_decay:
optim_params = model.parameters()
else:
p_bn = [p for n, p in model.named_parameters() if "bn" in n]
p_non_bn = [p for n, p in model.named_parameters() if "bn" not in n]
optim_params = [
{"params": p_bn, "weight_decay": 0},
{"params": p_non_bn, "weight_decay": self.weight_decay},
]
return optim_params



toml
[Optimizer.SGD]
params = "$ModelBnWeightDecayHook"

0.0001

bn_weight_decay = false
enabled = true


Use `` to call user defined hooks.

</details>

<details>
<summary>Instance-level hook</summary>

If the logic of module building are too complicated, instance-level hook may be helpful.

TODO

</details>

<details>
<summary>:sparkles:Lazy Config with simple API</summary>
The core conception of LazyConfig is 'Lazy', which represents a status of delay. Before instantiating, all the parameters will be stored in a special dict which additionally contains what the target class is. So It's easy to alter any parameters of the module and control which module should be instantiated and which module should not.

It's also used to address the defects of plain text configs through python lsp which is able to provide code navigation, auto-completion and more.

`ExCore` implements some nodes - `MoudleNode`, `InternNode`, `ReusedNode`, `ClassNode`, `ConfigHookNode`, `ChainedInvocationWrapper` and `VariableReference` and a `LazyConfig` to manage all nodes.

`ExCore` provides only 2 simple API to build moduels -- 'load' and `build_all`.

Typically, we follow the following procedure.

python
from excore import config
layz_cfg = config.load('xxx.toml')
module_dict, run_info = config.build_all(layz_cfg)


The results of `build_all` are respectively `Primary` modules and `Isolated` objects.

If you only want to use a certain module.

python
from excore import config
layz_cfg = config.load('xxx.toml')
model = lazy_cfg.Model() Model is one of `PrimaryFields`
or
model = layz_cfg['Model']()


If you want to follow other logic to build modules, you can still use `LazyConfig` to adjust the arguments of `node`s and more things.

python
from excore import config
layz_cfg = config.load('xxx.toml')
lazy_cfg.Model.add_params(pre_trained='./')

module_dict, run_info = config.build_all(layz_cfg)


</details>

<details>
<summary>Config print</summary>

python
from excore import config
cfg = config.load_config('xx.toml')
print(cfg)


Result:


╒══════════════════════════╤══════════════════════════════════════════════════════════════════════╕
│ size │ 1024 │
├──────────────────────────┼──────────────────────────────────────────────────────────────────────┤
│ TrainData.CityScapes │ ╒═════════════╤════════════════════════════════════════════════════╕ │
│ │ │ &train_size │ size │ │
│ │ ├─────────────┼────────────────────────────────────────────────────┤ │
│ │ │ !transforms │ ['RandomResize', 'RandomFlip', 'Normalize', 'Pad'] │ │
│ │ ├─────────────┼────────────────────────────────────────────────────┤ │
│ │ │ data_path │ xxx │ │
│ │ ╘═════════════╧════════════════════════════════════════════════════╛ │
├──────────────────────────┼──────────────────────────────────────────────────────────────────────┤
│ Transform.RandomFlip │ ╒══════╤═════╕ │

Page 2 of 2

© 2024 Safety CLI Cybersecurity Inc. All Rights Reserved.