pg.patching.Patcher

Accessible via pg.patching.Patcher.

class Patcher[source]

Bases: pg.Functor

Class that patches a symbolic value by returning a rebind dict.

To accomondate reusable patching, we introduced the concept of patcher, which is a symbolic function that takes an input value with a list of optional arguments and produces a patching rule (see pg.patch) or a tuple of (patching_rule, validation_rule). Validation rule is a callable object that validate the patched object’s integrity if it’s being patched by others later. A patcher can be created from a URI-like string, to better serve the command-line interface.

A patcher can be created via pg.patcher decorator:

@pg.patcher([
    ('lr', pg.typing.Float(min_value=0.0))
])
def learning_rate(trainer, lr):
  return {
      'training.learning_rate': lr
  }

OR:

@pg.patcher([

(‘lr’, pg.typing.Float(min_value=0.0))

]) def learning_rate(trainer, lr):

def _rebind_fn(k, v, p):
if k and k.key == ‘learning_rate’ and isinstance(v, float):

return lr

return v

return _rebind_fn

OR a composition of rules.

@pg.patcher([

(‘lr’, pg.typing.Float(min_value=0.0)), (‘weight_decay’, pg.typing.Float(min_value=0.0))

]) def complex(trainer, lr, weight_decay):

# change_lr and change_weight_decay are instances of other patchers. return [

change_lr(lr), change_weight_decay(weight_decay)

]

After registration, a patcher object can be obtained from a URL-like string:

patcher = pg.patching.from_uri('cosine_decay?lr=0.1')

Then the user can use the patcher to patch an object:

patched_trainer = patcher.patch(trainer)

A list of patchers can be applied sequentially to accomondate combination of semantic groups. A patcher in the sequence can propose updates to the original value or generate a replacement to the original value. pg.patching.patch is introduced for making it convenient to chain multiple patchers using URI-like strings:

pg.patching.patch(trainer, [
    'cosine_decay?lr=0.1',
    'some_other_patcher_string',
])

The user can lookup all registered patchers via:

print(pg.patching.patcher_names)

Methods:

patch(x)

Patches an input and return the input itself unless fully replaced.

validate(x)

Validates an input's integrity.

patch(x)[source]

Patches an input and return the input itself unless fully replaced.

Return type:

Any

validate(x)[source]

Validates an input’s integrity.

This method will be called in pg.patch when a chain of patchers have been applied, as to validate the patched object in chain still conforms to the patcher’s plan.

Return type:

None

Parameters:

x – The input after modification.