def METHOD_NAME(self, model: nn.Module, cfg: mmengine.Config,
                    recursive: bool, **kwargs):
    """DFS and replace target models."""
    def _replace_module_impl(model, cfg, **kwargs):
        # disable patching if model is already patched.
        if type(model) in self._cls_set:
            return model
        if recursive and hasattr(model, 'named_children'):
            for name, module in model.named_children():
                model._modules[name] = _replace_module_impl(
                    module, cfg, **kwargs)
        return self._replace_one_module(model, cfg, **kwargs)
    return _replace_module_impl(model, cfg, **kwargs)