Repository: jina-ai/serve
Pull request: fix(transformer): fix cls embedding

Problem statement:
nlp transformer cls token embedding just support two model names in transformers hub
**Describe the bug**
<!-- A clear and concise description of what the bug is. -->
this code in  `BaseTransformerEncoder`:
```
if self.pooling_strategy == 'cls':
    if self.model_name in ('bert-base-uncased', 'roberta-base'):
        output = self.tensor2array(extra_output[0])
    else:
        output = reduce_cls(_seq_output, _mask_ids_batch, self.cls_pos)
```
the cls embedding just support 2 models, if I implement a model inherit from `BaseTransformerEncoder` and set `hfl/chinese-roberta-wwm-ext` as the model, use cls token embedding as output embedding. it will use this code as output embedding.
```
output = reduce_cls(_seq_output, _mask_ids_batch, self.cls_pos)
```
 but, in parctice, `hfl/chinese-roberta-wwm-ext` with `roberta-base` are same model.
**Describe how you solve it**
<!-- copy past your code/pull request link -->
change this code
```
if self.model_name in ('bert-base-uncased', 'roberta-base'):
```
to this code
```
if getattr(self.tokenizer, 'cls_token', None) is not None:
```
---

<!-- Optional, but really help us locate the problem faster -->

**Environment**
<!-- Run `jina --version-full` and copy paste the output here -->

**Screenshots**
<!-- If applicable, add screenshots to help explain your problem. -->

Hints from issue discussion:

Commit message: fix(transformer): fix cls embedding

Patch to review:
diff --git a/jina/executors/encoders/nlp/transformer.py b/jina/executors/encoders/nlp/transformer.py
index 90ad965c4272f..a4ed36263ea33 100644
--- a/jina/executors/encoders/nlp/transformer.py
+++ b/jina/executors/encoders/nlp/transformer.py
@@ -65,7 +65,7 @@ def encode(self, data: 'np.ndarray', *args, **kwargs) -> 'np.ndarray':
             _mask_ids_batch = self.tensor2array(mask_ids_batch)
             _seq_output = self.tensor2array(seq_output)
             if self.pooling_strategy == 'cls':
-                if self.model_name in ('bert-base-uncased', 'roberta-base'):
+                if self._has_cls_token:
                     output = self.tensor2array(extra_output[0])
                 else:
                     output = reduce_cls(_seq_output, _mask_ids_batch, self.cls_pos)
@@ -94,6 +94,9 @@ def post_init(self):
         self._sess_func = None
         self.tmp_model_path = self.model_abspath if os.path.exists(self.model_abspath) else self.model_name
         self._tokenizer = self.get_tokenizer()
+
+        self._has_cls_token = True if getattr(self._tokenizer, 'cls_token', None) is not None else False
+
         self.cls_pos = 'tail' if self.model_name == 'xlnet-base-cased' else 'head'
 
     def array2tensor(self, array):


Code review comments:
diff_hunk: @@ -94,6 +94,9 @@ def post_init(self):
         self._sess_func = None
         self.tmp_model_path = self.model_abspath if os.path.exists(self.model_abspath) else self.model_name
         self._tokenizer = self.get_tokenizer()
+
+        self._has_cls_token = True if getattr(self._tokenizer, 'cls_token', None) is not None else False
path: jina/executors/encoders/nlp/transformer.py
text: @user1:
very redundant logic

```suggestion
        self._has_cls_token = hasattr(self._tokenizer, 'cls_token')
```