TransForm_Kit/ModelConvert/pytorch2caffe/code/ConvertModel.py
def FindMultiTops(func):
    """
        Precount nodes with number of tops(indegree)>1,
        which could be Slice or Split(only in ncnn, for making multiple copies)
    """
    if func in visited:
        return tops_dict[func]

    visited.add(func)
    layer_type = str(type(func).__name__)
    bottoms = []

    if hasattr(func, 'next_functions'):
        for u in func.next_functions:
            if u[0] is not None:
                child_type = str(type(u[0]).__name__)
                if child_type != 'AccumulateGrad' and (layer_type != 'AddmmBackward' or child_type != 'TransposeBackward'):
                    child_name = FindMultiTops(u[0])
                    bottoms.append(child_name)

    """ Gen layer name """
    layer_type_name = layer_type.replace('Backward', '')
    if layer_type_name in layer_type_count:
        layer_type_count[layer_type_name] += 1
    else:
        layer_type_count[layer_type_name] = 1

    name = layer_type_name + '_' + str(layer_type_count[layer_type_name])

    """  Skip some pytorch layers  """
    
    if layer_type_name in ['Clone', 'Threshold', 'Dropout', 'SetItem']:
        tops_dict[func] = bottoms[0]
    elif (layer_type_name == 'Index') and (not isinstance(func.index, tuple)):
        tops_dict[func] = bottoms[0]
    else:
        tops_dict[func] = name
    

    if hasattr(func, 'next_functions'):
        for u in func.next_functions:
            if u[0] is not None:
                child_type = str(type(u[0]).__name__)
                if child_type != 'AccumulateGrad' and (layer_type != 'AddmmBackward' or child_type != 'TransposeBackward'):
                    father_func = u[0]
                    if father_func not in multi_tops:
                        multi_tops[father_func] = []
                    multi_tops[father_func].append(tops_dict[father_func] + '_' + tops_dict[func])

                    if (layer_type == 'IndexBackward') and isinstance(func.index, tuple):
                        if father_func not in slice_point:
                            slice_point[father_func] = []
                        start, stop, dim_size, axis = GetLayerParam_Index(func)

                        """ Persume the visit of Index layers will be ascending """
                        if start > 0:
                            slice_point[father_func].append(start)
                            axis_dict[father_func] = axis

                            """ Last slice """
                            # if stop == dim_size

    return tops_dict[func]

        Precount nodes with number of tops(indegree)>1,
        which could be Slice or Split(only in ncnn, for making multiple copies)
    
Precount nodes with number of tops(indegree)>1,
which could be Slice or Split(only in ncnn, for making multiple copies)
Precount nodes with number of tops ( indegree ) > 1 which could be Slice or Split ( only in ncnn for making multiple copies )
def FindMultiTops(func):
    if func in visited:
        return tops_dict[func]
    visited.add(func)
    layer_type = str(type(func).__name__)
    bottoms = []
    if hasattr(func, 'next_functions'):
        for u in func.next_functions:
            if u[0] is not None:
                child_type = str(type(u[0]).__name__)
                if child_type != 'AccumulateGrad' and (layer_type != 'AddmmBackward' or child_type != 'TransposeBackward'):
                    child_name = FindMultiTops(u[0])
                    bottoms.append(child_name)
    layer_type_name = layer_type.replace('Backward', '')
    if layer_type_name in layer_type_count:
        layer_type_count[layer_type_name] += 1
    else:
        layer_type_count[layer_type_name] = 1
    name = layer_type_name + '_' + str(layer_type_count[layer_type_name])
    if layer_type_name in ['Clone', 'Threshold', 'Dropout', 'SetItem']:
        tops_dict[func] = bottoms[0]
    elif (layer_type_name == 'Index') and (not isinstance(func.index, tuple)):
        tops_dict[func] = bottoms[0]
    else:
        tops_dict[func] = name
    if hasattr(func, 'next_functions'):
        for u in func.next_functions:
            if u[0] is not None:
                child_type = str(type(u[0]).__name__)
                if child_type != 'AccumulateGrad' and (layer_type != 'AddmmBackward' or child_type != 'TransposeBackward'):
                    father_func = u[0]
                    if father_func not in multi_tops:
                        multi_tops[father_func] = []
                    multi_tops[father_func].append(tops_dict[father_func] + '_' + tops_dict[func])
                    if (layer_type == 'IndexBackward') and isinstance(func.index, tuple):
                        if father_func not in slice_point:
                            slice_point[father_func] = []
                        start, stop, dim_size, axis = GetLayerParam_Index(func)
                        if start > 0:
                            slice_point[father_func].append(start)
                            axis_dict[father_func] = axis
    return tops_dict[func]
def FindMultiTops ( func ) : if func in visited : return tops_dict [ func ] visited . add ( func ) layer_type = str ( type ( func ) . __name__ ) bottoms = [ ] if hasattr ( func , 'next_functions' ) : for u in func . next_functions : if u [ 0 ] is not None : child_type = str ( type ( u [ 0 ] ) . __name__ ) if child_type != 'AccumulateGrad' and ( layer_type != 'AddmmBackward' or child_type != 'TransposeBackward' ) : child_name = FindMultiTops ( u [ 0 ] ) bottoms . append ( child_name ) """ Gen layer name """ layer_type_name = layer_type . replace ( 'Backward' , '' ) if layer_type_name in layer_type_count : layer_type_count [ layer_type_name ] += 1 else : layer_type_count [ layer_type_name ] = 1 name = layer_type_name + '_' + str ( layer_type_count [ layer_type_name ] ) """  Skip some pytorch layers  """ if layer_type_name in [ 'Clone' , 'Threshold' , 'Dropout' , 'SetItem' ] : tops_dict [ func ] = bottoms [ 0 ] elif ( layer_type_name == 'Index' ) and ( not isinstance ( func . index , tuple ) ) : tops_dict [ func ] = bottoms [ 0 ] else : tops_dict [ func ] = name if hasattr ( func , 'next_functions' ) : for u in func . next_functions : if u [ 0 ] is not None : child_type = str ( type ( u [ 0 ] ) . __name__ ) if child_type != 'AccumulateGrad' and ( layer_type != 'AddmmBackward' or child_type != 'TransposeBackward' ) : father_func = u [ 0 ] if father_func not in multi_tops : multi_tops [ father_func ] = [ ] multi_tops [ father_func ] . append ( tops_dict [ father_func ] + '_' + tops_dict [ func ] ) if ( layer_type == 'IndexBackward' ) and isinstance ( func . index , tuple ) : if father_func not in slice_point : slice_point [ father_func ] = [ ] start , stop , dim_size , axis = GetLayerParam_Index ( func ) """ Persume the visit of Index layers will be ascending """ if start > 0 : slice_point [ father_func ] . append ( start ) axis_dict [ father_func ] = axis """ Last slice """ return tops_dict [ func ]
Precount nodes with number of tops(indegree)>1,
which could be Slice or Split(only in ncnn, for making multiple copies)
Precount nodes with number of tops ( indegree ) > 1 which could be Slice or Split ( only in ncnn for making multiple copies )
"""
        Precount nodes with number of tops(indegree)>1,
        which could be Slice or Split(only in ncnn, for making multiple copies)
    """ """ Gen layer name """ """  Skip some pytorch layers  """ """ Persume the visit of Index layers will be ascending """ """ Last slice """ # if stop == dim_size
func