transformers 模型指导文件(t5forconditionalgeneration与t5model的差异)

读transformer代码前,请先看对应的模型文件
transformer t5模型指导文件
备注:t5forconditionalgeneration与t5model有差异,两者不大一样
t5forconditionalgeneration中间有一个将encoder部分降维的过程

hidden_states = encoder_outputs[0]
if self.model_parallel:
    torch.cuda.set_device(self.decoder.first_device)

if labels is not None and decoder_input_ids is None and decoder_inputs_embeds is None:
    # get decoder inputs from shifting lm labels to the right
    decoder_input_ids = self._shift_right(labels)

原本的hidden_states可能为(1,11,512),而现在的hidden_states为经过_shift_right之后变为(1,1,512)
而t5model中间没有这么多转移,直接运转过来

hidden_states = encoder_outputs[0]
if self.model_parallel:
    torch.cuda.set_device(self.decoder.first_device)
# Set device for model parallelism
if self.model_parallel:
    torch.cuda.set_device(self.decoder.first_device)
    hidden_states = hidden_states.to(self.decoder.first_device)
    if decoder_input_ids is not None:
        decoder_input_ids = decoder_input_ids.to(self.decoder.first_device)
    if attention_mask is not None:
        attention_mask = attention_mask.to(self.decoder.first_device)
    if decoder_attention_mask is not None:
        decoder_attention_mask = decoder_attention_mask.to(self.decoder.first_device)

# Decode
decoder_outputs = self.decoder(
    input_ids=decoder_input_ids,
    attention_mask=decoder_attention_mask,
    inputs_embeds=decoder_inputs_embeds,
    past_key_values=past_key_values,
    encoder_hidden_states=hidden_states,
    encoder_attention_mask=attention_mask,
    head_mask=decoder_head_mask,
    cross_attn_head_mask=cross_attn_head_mask,
    use_cache=use_cache,
    output_attentions=output_attentions,
    output_hidden_states=output_hidden_states,
    return_dict=return_dict,
)

hidden_states的形状不变
但是同样来说,t5model需要encoder_inputs和decoder_inputs的两部分输入过程
比如t5文档的示例

from transformers import T5Tokenizer, T5Model

tokenizer = T5Tokenizer.from_pretrained('t5-small')
model = T5Model.from_pretrained('t5-small')

input_ids = tokenizer("Studies have been shown that owning a dog is good for you", return_tensors="pt").input_ids  # Batch size 1
decoder_input_ids = tokenizer("Studies show that", return_tensors="pt").input_ids  # Batch size 1

# forward pass
outputs = model(input_ids=input_ids, decoder_input_ids=decoder_input_ids)
last_hidden_states = outputs.last_hidden_state

而t5forconditionalgeneration只需要一个输入即可,所以这里还是需要好好研究一下t5forconditionalgeneration的调用过程。

你可能感兴趣的:(bert源码解读,nezha源码解读,python,深度学习,pytorch)