""""Fuse conv bn file."""
import copy
import paddle
import paddle.nn as nn
def find_parent_layer_and_sub_name(model, name):
"""Given the model and the name of a layer, find the parent layer and
the sub_name of the layer.
For example, if name is 'block_1/convbn_1/conv_1', the parent layer is
'block_1/convbn_1' and the sub_name is `conv_1`.
Args:
model (paddle.nn.Layer): The whole model.
name (str): The layer name.
Returns:
parent_layer (paddle.nn.Layer): The parent layer for the input layer.
sub_name (str): The sub name of the layer in the parent layer.
"""
assert isinstance(model, paddle.nn.Layer), \
"The model must be the instance of paddle.nn.Layer."
assert len(name) > 0, "The input (name) should not be empty."
last_idx = 0
idx = 0
parent_layer = model
while idx < len(name):
if name[idx] == '.':
sub_name = name[last_idx:idx]
if hasattr(parent_layer, sub_name):
parent_layer = getattr(parent_layer, sub_name)
last_idx = idx + 1
idx += 1
sub_name = name[last_idx:idx]
return par