module llutil.config
function configurable
This decorator delays the initialization of cls until freeze().
Returns:
decorated class which is now configurable.
Example:
import ice
@ice.configurable
class AClass:
def __init__(self, a, b, c, d):
self.a = a
self.b = b
self.c = c
self.d = d
# partial initialization.
i = AClass(b=0)
# alter positional and keyword arguments afterwards.
i[0] = 2
i['b'] = 1
i.update({'c': 3, 'd': 4})
i.update(d=5)
# unfrozen configurable can be printed as a legal construction python statement.
assert repr(i) == "AClass(a=2, b=1, c=3, d=5)"
# real initialization of original object.
i.freeze()
assert i.a == 2 and i.b == 1
function is_configurable
check if a class or an object is configurable.
Returns:
bool
Example:
import ice
import torch.nn as nn
ice.make_configurable(nn.Conv2d, nn.Linear)
assert ice.is_configurable(nn.Conv2d)
function has_builder
function frozen
function make_configurable
This function converts multiple existing classes to configurables.
Note:
This have exactly the same effects of decorate each class with
@configurablewhen defining the class. Each class only need to be decorated once, extra calling of conversion is ignored and has no side effects.
Example:
import ice
import torch.nn as nn
ice.make_configurable(nn.Conv2d, nn.Linear)
assert ice.is_configurable(nn.Conv2d)
function clone
clone configurables, containers, and ordinary objects recursively.
Args:
-
obj(configurable or list/dict of configurables): the configurable object to be cloned. -
deepcopy(bool, optional): copy resources by value. Defaults to True.
Returns:
Unfrozen copy of the original configurable.
import ice
import torch.nn as nn
ice.make_configurable(nn.Conv2d, nn.Linear)
convcfg = nn.Conv2d(16, 8)
conv1x1 = convcfg.clone() # or ice.clone(convcfg)
conv1x1['kernel_size'] = 1
conv1x1.freeze() # or ice.freeze(conv1x1)
assert conv1x1.kernel_size == (1, 1)
conv3x3 = convcfg.clone()
conv3x3['kernel_size'] = 3
conv3x3.freeze()
assert conv3x3.kernel_size == (3, 3)
function freeze
freeze configurables recursively.
Freezing is the process of building the configuration into real objects.
Original __init__() functions of configurable classes declared by configurable
or make_configurable now will be called recursively to initialize the real instance,
also known as the frozen version of a configurable.
Args:
obj(configurable or list/dict of configurables): the configurable object to be freeze.
Returns:
Frozen version of the original configurable.
Note:
Freezing happens in-place, ignoring the returned value is safe. If a user wants to reuse the configuration feature, he can clone() the object before or after frozen with the same effect.
Example:
See examples for configurable and clone.