voidism / Lookback-Lens

Code for the EMNLP 2024 paper "Detecting and Mitigating Contextual Hallucinations in Large Language Models Using Only Attention Maps"
https://arxiv.org/abs/2407.07071
105 stars 5 forks source link

The use of teacher_forcing_seq #6

Closed VityaVitalich closed 5 days ago

VityaVitalich commented 1 month ago

Dear authors,

Thank you for your work, I really liked it and started delving into the code. However, it is unclear for me where "teacher_forcing_seq" is used and why.

I can clearly see this as an argument for generation in the vanilla case and that it is loaded only when the argument is passed, but you do not pass this argument in README, that confuses a bit.

It is also a bit unclear what it does and do I really need to understand this :)

            if teacher_forcing_seq is None:
                # argmax
                next_tokens = torch.argmax(next_tokens_scores, dim=-1)
            else:
                if new_token_idx >= teacher_forcing_seq.shape[1]:
                    # use eos token as next token
                    next_tokens = torch.tensor(
                        [eos_token_id[0] if eos_token_id is not None else pad_token_id] * input_ids.shape[0],
                    )
                    new_token_idx += 1
                    this_peer_finished = True
                else:
                    next_tokens = teacher_forcing_seq[:, new_token_idx]
                    new_token_idx += 1

Thank you for your help!

voidism commented 1 month ago

Hi @VityaVitalich

Thanks for your interests in our project! The teacher_forcing_seq was used when we had a given sequence for calculating the lookback ratio. The given sequence may not be the one that would be generated by greedy decoding.

We don't use teacher forcing in the main experiments. We only use it in an experiment of Section 4, where we need to ensure that the 7B and 13B models are generating the same content when extracting lookback ratio features. So, we decode from 7B to get its outputs, and run the 13B model with the 7B outputs as teacher_forcing_seq.

Let me know if you have more questions!