Skip to content

Update roc bert docs #38835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Jun 17, 2025
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
af9090c
Moved the sources to the right
Jun 6, 2025
f1630af
small Changes
Jun 6, 2025
abc9cf3
Some Changes to moonshine
Jun 6, 2025
4d513eb
Added the install to pipline
Jun 7, 2025
63dc460
updated the monshine model card
Jun 10, 2025
b20e589
Update docs/source/en/model_doc/moonshine.md
SohamPrabhu Jun 10, 2025
5b5ff7d
Update docs/source/en/model_doc/moonshine.md
SohamPrabhu Jun 10, 2025
520ff68
Update docs/source/en/model_doc/moonshine.md
SohamPrabhu Jun 10, 2025
615cd50
Update docs/source/en/model_doc/moonshine.md
SohamPrabhu Jun 10, 2025
4ea0a6f
Updated Documentation According to changes
Jun 10, 2025
f30014f
Fixed the model with the commits
Jun 11, 2025
d61a4e1
Changes to the roc_bert
Jun 13, 2025
6d00cd9
Final Update to the branch
Jun 14, 2025
1947877
Adds Quantizaiton to the model
Jun 14, 2025
072770e
Finsihed Fixing the Roc_bert docs
Jun 16, 2025
9d1ece5
Fixed Moshi
Jun 16, 2025
ce1aabd
Fixed Problems
Jun 16, 2025
1a2d93d
Fixed Problems
Jun 17, 2025
b94b95d
Fixed Problems
Jun 17, 2025
5edb679
Fixed Problems
Jun 17, 2025
e1864a2
Fixed Problems
Jun 17, 2025
ff2b582
Fixed Problems
Jun 17, 2025
794ed87
Added the install to pipline
Jun 7, 2025
113ad25
updated the monshine model card
Jun 10, 2025
feb8600
Update docs/source/en/model_doc/moonshine.md
SohamPrabhu Jun 10, 2025
ae85f92
Update docs/source/en/model_doc/moonshine.md
SohamPrabhu Jun 10, 2025
67c3c36
Update docs/source/en/model_doc/moonshine.md
SohamPrabhu Jun 10, 2025
6ead6f8
Updated Documentation According to changes
Jun 10, 2025
bcc72ab
Fixed the model with the commits
Jun 11, 2025
0c02798
Fixed the problems
Jun 17, 2025
29edf00
Final Fix
Jun 17, 2025
f07135d
Final Fix
Jun 17, 2025
5665dd1
Final Fix
Jun 17, 2025
2981927
Update roc_bert.md
stevhliu Jun 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 63 additions & 24 deletions docs/source/en/model_doc/roc_bert.md
Original file line number Diff line number Diff line change
@@ -14,39 +14,78 @@ rendered properly in your Markdown viewer.

-->

<div style="float: right;">
<div class="flex flex-wrap space-x-1">
<img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-DE3412?style=flat&logo=pytorch&logoColor=white">
</div>
</div>

# RoCBert

<div class="flex flex-wrap space-x-1">
<img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-DE3412?style=flat&logo=pytorch&logoColor=white">
</div>
[RoCBert](https://aclanthology.org/2022.acl-long.65.pdf) is a pretrained Chinese [BERT](./bert) model designed against adversarial attacks like typos and synonyms. It is pretrained with a contrastive learning objective to align normal and adversarial text examples. The examples include different semantic, phonetic, and visual features of Chinese. This makes RoCBert more robust against manipulation.

You can find all the original RoCBert checkpoints under the [weiweishi](https://huggingface.co/weiweishi) profile.

> [!TIP]
> This model was contributed by [weiweishi](https://huggingface.co/weiweishi).
>
> Click on the RoCBert models in the right sidebar for more examples of how to apply RoCBert to different Chinese language tasks.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> Click on the RoCBert models in the right sidebar for more examples of how to apply RoCBert to different Chinese language tasks.
> Click on the RoCBert models in the right sidebar for more examples of how to apply RoCBert to different Chinese language tasks.
The example below demonstrates how to predict the [MASK] token with [`Pipeline`], [`AutoModel`], and from the command line.


The example below demonstrates how to predict the [MASK] token with [`Pipeline`], [`AutoModel`], and from the command line.

<hfoptions id="usage">
<hfoption id="Pipeline">

```py
import torch
from transformers import pipeline

pipeline = pipeline(
task="fill-mask",
model="weiweishi/roc-bert-base-zh",
torch_dtype=torch.float16,
device=0
)
pipeline("這家餐廳的拉麵是我[MASK]過的最好的拉麵之")
```

</hfoption>
<hfoption id="AutoModel">

```py
import torch
from transformers import AutoModelForMaskedLM, AutoTokenizer

## Overview
tokenizer = AutoTokenizer.from_pretrained(
"weiweishi/roc-bert-base-zh",
)
model = AutoModelForMaskedLM.from_pretrained(
"weiweishi/roc-bert-base-zh",
torch_dtype=torch.float16,
device_map="auto",
)
inputs = tokenizer("這家餐廳的拉麵是我[MASK]過的最好的拉麵之", return_tensors="pt").to("cuda")

The RoCBert model was proposed in [RoCBert: Robust Chinese Bert with Multimodal Contrastive Pretraining](https://aclanthology.org/2022.acl-long.65.pdf) by HuiSu, WeiweiShi, XiaoyuShen, XiaoZhou, TuoJi, JiaruiFang, JieZhou.
It's a pretrained Chinese language model that is robust under various forms of adversarial attacks.
with torch.no_grad():
outputs = model(**inputs)
predictions = outputs.logits

The abstract from the paper is the following:
masked_index = torch.where(inputs['input_ids'] == tokenizer.mask_token_id)[1]
predicted_token_id = predictions[0, masked_index].argmax(dim=-1)
predicted_token = tokenizer.decode(predicted_token_id)

*Large-scale pretrained language models have achieved SOTA results on NLP tasks. However, they have been shown
vulnerable to adversarial attacks especially for logographic languages like Chinese. In this work, we propose
ROCBERT: a pretrained Chinese Bert that is robust to various forms of adversarial attacks like word perturbation,
synonyms, typos, etc. It is pretrained with the contrastive learning objective which maximizes the label consistency
under different synthesized adversarial examples. The model takes as input multimodal information including the
semantic, phonetic and visual features. We show all these features are important to the model robustness since the
attack can be performed in all the three forms. Across 5 Chinese NLU tasks, ROCBERT outperforms strong baselines under
three blackbox adversarial algorithms without sacrificing the performance on clean testset. It also performs the best
in the toxic content detection task under human-made attacks.*
print(f"The predicted token is: {predicted_token}")
```

This model was contributed by [weiweishi](https://huggingface.co/weiweishi).
</hfoption>
<hfoption id="transformers CLI">

## Resources
```bash
echo -e "這家餐廳的拉麵是我[MASK]過的最好的拉麵之" | transformers-cli run --task fill-mask --model weiweishi/roc-bert-base-zh --device 0
```

- [Text classification task guide](../tasks/sequence_classification)
- [Token classification task guide](../tasks/token_classification)
- [Question answering task guide](../tasks/question_answering)
- [Causal language modeling task guide](../tasks/language_modeling)
- [Masked language modeling task guide](../tasks/masked_language_modeling)
- [Multiple choice task guide](../tasks/multiple_choice)
</hfoption>
</hfoptions>

## RoCBertConfig