@staticmethod
def remove_layers_from_model(teacher, num_layers, is_gpt):
    if (not is_gpt):
        teacher_config = teacher.config
        student_config = copy(teacher.config)
        student_config.num_hidden_layers = num_layers
        student = RobertaModel(config=student_config)
        student.embeddings.word_embeddings = teacher.embeddings.word_embeddings
        student.embeddings.position_embeddings = teacher.embeddings.position_embeddings
        student.embeddings.token_type_embeddings = teacher.embeddings.token_type_embeddings
        student.embeddings.LayerNorm = teacher.embeddings.LayerNorm
        student.embeddings.dropout = teacher.embeddings.dropout
        step = ((teacher_config.num_hidden_layers - 1) / (student_config.num_hidden_layers - 1))
        for (student_layer, teacher_layer) in enumerate((int((i * step)) for i in range(student_config.num_hidden_layers))):
            student.encoder.layer[student_layer] = teacher.encoder.layer[teacher_layer]
    else:
        teacher_config = teacher.config
        student_config = copy(teacher.config)
        student_config.n_layer = num_layers
        student = GPT2LMHeadModel(config=student_config)
        student.transformer.wte = teacher.transformer.wte
        student.transformer.wpe = teacher.transformer.wpe
        student.transformer.drop = teacher.transformer.drop
        student.tie_weights()
        step = ((teacher_config.n_layer - 1) / (student_config.n_layer - 1))
        for (student_layer, teacher_layer) in enumerate((int((i * step)) for i in range(student_config.n_layer))):
            student.transformer.h[student_layer] = teacher.transformer.h[teacher_layer]
    return student
@staticmethod
def remove_layers_from_model(teacher, num_layers, is_gpt):
    if (not is_gpt):
        teacher_config = teacher.config
        student_config = copy(teacher.config)
        student_config.num_hidden_layers = num_layers
        student = RobertaModel(config=student_config)
        student.embeddings.word_embeddings = teacher.embeddings.word_embeddings
        student.embeddings.position_embeddings = teacher.embeddings.position_embeddings
        student.embeddings.token_type_embeddings = teacher.embeddings.token_type_embeddings
        student.embeddings.LayerNorm = teacher.embeddings.LayerNorm
        student.embeddings.dropout = teacher.embeddings.dropout
        step = ((teacher_config.num_hidden_layers - 1) / (student_config.num_hidden_layers - 1))
        for (student_layer, teacher_layer) in enumerate((int((i * step)) for i in range(student_config.num_hidden_layers))):
            student.encoder.layer[student_layer] = teacher.encoder.layer[teacher_layer]
    else:
        teacher_config = teacher.config
        student_config = copy(teacher.config)
        student_config.n_layer = num_layers
        student = GPT2LMHeadModel(config=student_config)
        student.transformer.wte = teacher.transformer.wte
        student.transformer.wpe = teacher.transformer.wpe
        student.transformer.drop = teacher.transformer.drop
        student.tie_weights()
        step = ((teacher_config.n_layer - 1) / (student_config.n_layer - 1))
        for (student_layer, teacher_layer) in enumerate((int((i * step)) for i in range(student_config.n_layer))):
            student.transformer.h[student_layer] = teacher.transformer.h[teacher_layer]
    return student