Fix and improve CTRL doctests
Author: jeremyadamsfisher
Pull request: yes

- Improve CTRL doctests and fix test assertions, where appropriate

# What does this PR do?

This PR addresses the CTRL doc test failures and replaces the example text with one that is more appropriate for CTRL specifically (i.e., by prefacing it with a control code)

Motivated as part of the doctest sprint: https://github.com/huggingface/transformers/issues/16292


## Before submitting
- [x] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests),
      Pull Request section?
- [x] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [x] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [x] Did you write any new necessary tests?


## Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.


@patrickvonplaten @ydshieh @patil-suraj

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- albert, bert, xlm: @LysandreJik
- blenderbot, bart, marian, pegasus, encoderdecoder,  t5: @patrickvonplaten, @patil-suraj
- longformer, reformer, transfoxl, xlnet: @patrickvonplaten
- fsmt: @stas00
- funnel: @sgugger
- gpt2: @patrickvonplaten, @LysandreJik
- rag: @patrickvonplaten, @lhoestq
- tensorflow: @LysandreJik

Library:

- benchmarks: @patrickvonplaten
- deepspeed: @stas00
- ray/raytune: @richardliaw, @amogkam
- text generation: @patrickvonplaten
- tokenizers: @n1t0, @LysandreJik
- trainer: @sgugger
- pipelines: @LysandreJik

Documentation: @sgugger

HF projects:

- datasets: [different repo](https://github.com/huggingface/datasets)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Examples:

- maintained examples (not research project or legacy): @sgugger, @patil-suraj
- research_projects/bert-loses-patience: @JetRunner
- research_projects/distillation: @VictorSanh

 -->


Comments:
_The documentation is not available anymore as the PR was closed or merged._

@patrickvonplaten @sgugger 

Could you also take a look when you have some time?

In particular, I don't find any existing documentation mentioning the usage of `Opinion ...` (put the control code as the first word), although I **feel** this is the way to go.

(still don't feel very comfortable without seeing this usage)

Let's ping @LysandreJik as he might know more on CTRL ;-)

Thanks for the review! I'll address these comments asap.

As for the control code coming first, there's actually an example right here:
https://github.com/huggingface/transformers/blob/77321481247787c97568c3b9f64b19e22351bab8/examples/pytorch/text-generation/run_generation.py#L88

> Thanks for the review! I'll address these comments asap.
> 
> As for the control code coming first, there's actually an example right here:
> 
> https://github.com/huggingface/transformers/blob/77321481247787c97568c3b9f64b19e22351bab8/examples/pytorch/text-generation/run_generation.py#L88

Thank you for this info. @jeremyadamsfisher 

Thanks again for the feedback.

I've added assertions on lines 383 and 562-565 and changed the model from from `sshleifer/tiny-ctrl` to `ctrl`

Sure thing, those are easy changes.

To clarify the control code coming first, would it make sense to add something like this?
```python
>>> # CTRL was trained with control codes as the first token
>>> inputs = tokenizer("Opinion my dog is cute", return_tensors="pt")
>>> assert inputs[0] in tokenizer.control_codes.values()
```

> To clarify the control code coming first, would it make sense to add something like this?
> 
> ```python
> >>> # CTRL was trained with control codes as the first token
> >>> inputs = tokenizer("Opinion my dog is cute", return_tensors="pt")
> >>> assert inputs[0] in tokenizer.control_codes.values()
> ```

That doesn't seem to work, will tinker with this a bit more:

```
UNEXPECTED EXCEPTION: KeyError('Indexing with integers (to access backend Encoding for a given batch index) is not available when using Python based tokenizers')
Traceback (most recent call last):
  File "/Users/jeremyfisher/.pyenv/versions/3.8.12/lib/python3.8/doctest.py", line 1336, in __run
    exec(compile(example.source, filename, "single",
  File "<doctest transformers.models.ctrl.modeling_ctrl.CTRLModel.forward[5]>", line 1, in <module>
  File "/Users/jeremyfisher/Documents/transformers/src/transformers/tokenization_utils_base.py", line 239, in __getitem__
    raise KeyError(
KeyError: 'Indexing with integers (to access backend Encoding for a given batch index) is not available when using Python based tokenizers'
```

Aha! This works:

```
>>> # CTRL was trained with control codes as the first token
>>> inputs = tokenizer("Opinion my dog is cute", return_tensors="pt")
>>> assert inputs["input_ids"][0,0].item() in tokenizer.control_codes.values()
```

Added this to the doctest wherever there was a ` inputs = tokenizer(...)`

@ydshieh heads up -- I've addressed your second set of comments and the checks have passed  :)

Still waiting on @LysandreJik would love to hear your thoughts

Hi, @jeremyadamsfisher Could you try to  resolve the conflicts in

```
src/transformers/models/ctrl/modeling_ctrl.py
```

Then we are ready to merge :-) Thanks!

(I can help on this if you need, just let me know)

Hi, @jeremyadamsfisher Just to let you know: I resolved the conflict lines, and pushed to this PR branch.
I think we are ready to merge (once the CI tests are green).

If you need to make some more changes (if any), don't forget to `git pull` first. Thanks.

Merged! Thank you again, @jeremyadamsfisher !

> Merged! Thank you again, @jeremyadamsfisher !

Thank you @ydshieh! Apologies I wasn't able to fix the merge conflicts myself, but it is much appreciated!