Compatible tokenizer for both pertained models

Omama: <p>I would like to use pertained models such as Roberta as an encoder and GPT2 as a decoder, but each one has a different tokenizer. So how can I pass the output of Roberta as input to GPT2?</p>

dreidizzle: <p>Actually, they both use Byte Pair encoding with the same vocabulary, no? ROBERTA != BERT … So, this seems possible. However, GPT2’s decoder blocks don’t have encoder-decoder self attention: will you add this in and pre train it? Or, will you use ROBERTA to feed a prefix to GPT2 and then start generating with it?</p>
<p>See here: <a href="https://towardsdatascience.com/comparing-transformer-tokenizers-686307856955" class="inline-onebox" rel="noopener nofollow ugc">Comparing Transformer Tokenizers. Comparing Tokenizer vocabularies of… | by Gergely D. Németh | Towards Data Science</a></p>

Omama: <aside class="quote no-group" data-username="dreidizzle" data-post="2" data-topic="168448">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dreidizzle/48/55646_2.png" class="avatar"> dreidizzle:</div>
<blockquote>
<p>Actually, they both use Byte Pair encoding with the same vocabulary, no? ROBERTA != BERT … So, this seems possible. However, GPT2’s decoder blocks don’t have encoder-decoder self attention: will you add this in and pre train it?</p>
</blockquote>
</aside>
<p>Thanks for your reply.<br>
I will use Roberta embedding to feed it to pretrained GPT2 model… But the problem that pertained model has embedding layer which require indices as input … so How I can use Roberta embedding as input to GPT2 ?</p>

dreidizzle: <p>Just some ideas … So I’m thinking you need to add an encoder-decoder attention layer to GPT2? Basically, GPT2 has no encoder, so it has no encoder-decoder attention. You’d need to insert that in. The input to GPT2 would still be tokens, and it gets the embeddings for those tokens and passed them up. As these embedding travel up, there is a masked  These would then be updated eventually using the hidden states passed from Roberta. Basically, (at a high level) in the picture below ROBERTA is the left side and GPT2 is the right side, but GPT2 has no encoder-decoder attention. You might also figure out a way to take the embeddings for each of the tokens to GPT2 and swap in the ROBERTA vectors, for each token. This would also take some digging. You might need to grab parts of the model and do this in PyTorch more directly than using the hugging face APIS. I.e. usually you add a head on top, you now want to kind of add a preprocessing step to the GPT2 model, on the bottom.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/4/84cac54966030aeb715e09b2b4a0deeb84517a2f.png" data-download-href="https://discuss.pytorch.org/uploads/default/84cac54966030aeb715e09b2b4a0deeb84517a2f" title="Screen Shot 2022-12-28 at 10.36.16 AM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/4/84cac54966030aeb715e09b2b4a0deeb84517a2f_2_512x500.png" alt="Screen Shot 2022-12-28 at 10.36.16 AM" data-base62-sha1="iWJsx4lcLlyMs1nS3XzOpGAekBp" width="512" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/4/84cac54966030aeb715e09b2b4a0deeb84517a2f_2_512x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/8/4/84cac54966030aeb715e09b2b4a0deeb84517a2f_2_768x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/8/4/84cac54966030aeb715e09b2b4a0deeb84517a2f.png 2x" data-dominant-color="EEEDEC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screen Shot 2022-12-28 at 10.36.16 AM</span><span class="informations">970×946 85.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

Omama: <aside class="quote no-group" data-username="dreidizzle" data-post="4" data-topic="168448">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dreidizzle/48/55646_2.png" class="avatar"> dreidizzle:</div>
<blockquote>
<p>e embeddings for each of the tokens to GPT</p>
</blockquote>
</aside>
<p>So, can I implement it as follows:</p>
<p>config_decoder = AutoConfig.from_pretrained(“gpt2”, is_decoder=True, add_cross_attention= True)</p>
<p>model_dec = GPT2LMHeadModel.from_pretrained(“gpt2”, config=config_decoder)</p>
<p>model_rob = RobertaModel.from_pretrained(“roberta-base”, output_hidden_states= True)</p>
<p>out_rob = model_rob(**inputs)<br>
out_rob2 = out_rob[0]</p>
<p>out_gpt =  model_dec(inputs_embeds = out_rob2, labels=inputs[‘input_ids’], encoder_hidden_states=out_rob2)</p>
<p>Is this code correct?</p>