Skip to content

mrfi

Core module of MRFI.

EasyConfig(config=None)

EasyConfig object.

Use classmethod EasyConfig.load_[file|string|preset]() to construct a EasyConfig.

Properties
  • faultinject: List of fault injectors.
  • observe: List of observers.

Load a dict-like configuration or a EasyConfig preset.

Parameters:

Name Type Description Default
config Optional[dict]

if dict, load the dict config.

if None, make a empty EasyConfig.

None

load_file(filename) classmethod

Load EasyConfig from .yaml File.

load_string(string) classmethod

Load EasyConfig from a yaml string.

load_preset(name) classmethod

Load EasyConfig from presets.

ConfigTreeNodeType

Bases: Enum

Config tree node types.

Note

A config tree has recursion structure:

  • MODULE
    • FI_LIST (e.g. activation_in, weights)
      • FI_ATTR
        • FI_STAGE (e.g. quantization, selector)
          • METHOD_ARGS (e.g. dynamic_range, rate)
    • OBSERVER_LIST
      • OBSERVER_ATTR
    • MODULE_LIST (sub_modules)
      • MODULE
        • FI_LIST
        • ...

ConfigTree(config_node, mrfi, nodetype=ConfigTreeNodeType.MODULE, name='model')

Make a detail config tree.

Detail config tree is a dict-like object.

Parameters:

Name Type Description Default
config_node dict

A dict with ConfigTreeNodeType structrue.

required
mrfi MRFI

MRFI object

required
name str

config node name for visualize.

'model'

append(obj)

Append a object on a list node

remove(index)

Remove object by index

state_dict()

Visualize or export config tree node and all sub node.

hasattr(name)

Determine if an attr is in config node.

getattr(name)

Return an attr in config node, if not exist, return None

set_internal_attr(key, name)

__setattr__ reject bind new attr, use this if necessary.

ConfigItemList

Bases: list

A convient list of detail config tree allow broadcasting assignment.

example
cfg = fi_model.get_configs('weights.0.selector')
cfg.enable = False
cfg[1].enable = True

MRFI(model, config)

MRFI core object, a wrapper of network module.

__change_arg(fiattr, keep_arg=['method'])

Add an args level

golden_run()

A context manager for golden run.

Examples:

>>> with fi_model.golden_run():
>>>    out_golden = fi_model(inputs)

get_configs(configname=None, strict=False, **kwargs)

Find config tree nodes in current detail config.

Parameters:

Name Type Description Default
configname str

A string splited by dot '.' to match ConfigTreeNode, e.g. 'activations.0.selector'.

None
strict bool

Use strict match config name on found modules.

False
module_name Union[str, List[str]]

Found modules by module name, e.g. 'conv'.

required
module_type Union[str, List[str]]

Found modules by module type, e.g. 'Conv2d'.

required
module_fullname Union[str, List[str]]

Found modules by strict module name match, e.g. ['model.conv1', 'model.conv2'].

required

get_activation_configs(fi_configname=None, strict=False, activation_id=0, is_out=False, **kwargs)

A convenient function of get_configs to get activation config.

Equivalent to call get_configs('activation.0.{fi_configname}') or get_configs('activation_out.0.{fi_configname}'). when activation_id=0.

If first parameter fi_configname is not specified, return the root nodes of FI_CONFIG, usually used for settingenabled.

get_weights_configs(fi_configname=None, strict=False, weight_id=0, **kwargs)

A convenient function of get_configs to get weights config.

Equivalent to call get_configs('weights.0.{fi_configname}') when weight_id=0.

If first parameter fi_configname is not specified, return the root nodes of FI_CONFIG, usually used for settingenabled.

save_config(filename)

Save current detail config tree to a file.

observers_reset()

Reset all observers in MRFI, usually executed before an epoch of observation.

observers_result()

A convient function to get all observer results in MRFI.

load_package_functions(name)

Load a package of functions.

Parameters:

Name Type Description Default
name str

module name.

required

add_function(name, function_object)

Add a custom function, may be a observer, quantization, selector or error_mode.

Parameters:

Name Type Description Default
name str

To be used in config file, method field of observers, quantization, selector, error_mode.

required
function_object Callable[..., Any]

A callable object consists with implementions of observers, quantization, selector, error_mode.

required

Examples:

>>> mrfi.add_function('my_empty_selector', lambda shape: [])
Then you can use this empty selector by setting selector.method 

to 'my_empty_selector' in .yaml config file.

find_modules(model, attrdict)

Find by module_type, module_name, module_fullname, also remove these attr in attrdict.