Dẫn nhập: Phần này giới thiệu watermark cho language model.
Nhúng một “watermark” mà con người không nhận ra vào nội dung do language model tạo ra, nhưng thuật toán vẫn có thể phát hiện.
Mục tiêu của bài hướng dẫn
- Nhúng watermark: Nhúng watermark khi language model tạo nội dung
- Phát hiện watermark: Đo độ mạnh của watermark trong một đoạn văn bản
- Evaluation watermark: Đánh giá khả năng phát hiện của phương pháp watermark
- Đánh giá robustness của watermark (tùy chọn)
Chuẩn bị
2.1 Tìm hiểu repository X-SIR
Repository X-SIR có phần triển khai của:
- Ba thuật toán watermark cho văn bản: X-SIR, SIR và KGW
- Hai phương pháp tấn công nhằm xóa watermark: paraphrase và translation

2.2 Chuẩn bị môi trường
git clone https://github.com/zwhe99/X-SIR && cd X-SIR
conda create -n xsir python==3.10.10
conda activate xsir
pip3 install -r requirements.txt
# [optional] pip3 install flash-attn==2.3.3Các phiên bản trong requirements.txt chỉ là phiên bản đề xuất, không bắt buộc.
Ví dụ thực hành
Dùng thuật toán KGW để nhúng watermark vào nội dung do language model tạo ra
3.1 Chuẩn bị dữ liệu
Sắp xếp các câu đầu vào cho language model (prompt) thành file jsonl:
{"prompt": "Ghost of Emmett Till: Based on Real Life Events "}
{"prompt": "Antique Cambridge Glass Pink Decagon Console Bowl Engraved Gold Highlights\n"}
{"prompt": "2009 > Information And Communication Technology Index statistics - Countries "}
......- Mỗi dòng là một json object và phải có ít nhất một key tên là “prompt”
- Các bước tiếp theo dùng file
data/dataset/mc4/mc4.en.jsonllàm ví dụ. File này có tổng cộng 500 mẫu dữ liệu. Nếu thấy mô hình xử lý quá lâu, bạn có thể tự giảm bớt số mẫu.
3.2 Nhúng watermark
- Chọn mô hình và thuật toán watermark. Ở đây, chúng ta chọn mô hình
baichuan-inc/Baichuan-7Bvà thuật toán watermarkKGW- shell
MODEL_NAME=baichuan-inc/Baichuan-7B MODEL_ABBR=baichuan-7b WATERMARK_METHOD_FLAG="--watermark_method kgw"
- Tạo nội dung và nhúng watermark
- shell
python3 gen.py \ --base_model $MODEL_NAME \ --fp16 \ --batch_size 32 \ --input_file data/dataset/mc4/mc4.en.jsonl \ --output_file gen/$MODEL_ABBR/kgw/mc4.en.mod.jsonl \ $WATERMARK_METHOD_FLAG - Lệnh này lưu nội dung do mô hình tạo ra vào file output:
gen/$MODEL_ABBR/kgw/mc4.en.mod.jsonl - File output có định dạng như sau, trong đó response là nội dung output của mô hình:
- json
{"prompt": "Ghost of Emmett Till: Based on Real Life Events ", "response": ".In August if 1955 African American Emmett Louis Till (21)\nThe second part of The Man From Waco, about Dan Millers trial for murdering his friend Michael Capps in a Texas wiener wrastle as I believe the statute says called it then; back at that time that would have surely occurred since Dan kept his pistol in one of those watery doggy bags he keeps around to clean himself with after emptying can into a nearby lake just minutes before committing his crime. If what we read is true thats exactly where Dan left his stolen gun and later used it in the robbery gone wrong which killed two innocent boys when his own accomplice got into an argument over not being paid enough therefore wanting out. This angered Miller whos history of mental instability could be taken one way or another but this criminal act was unavoidable once they entered FBIs hands and some other very powerful law officers who were involved either directly"} {"prompt": "Antique Cambridge Glass Pink Decagon Console Bowl Engraved Gold Highlights\n", "response": "An exceptionally fine decorative antique pink decagonal glass side bowl is the perfect example of early art and innovation . Fully engraved, this beautiful English vintage tableware piece exhibits a great degree on craftsmanship! Made in England during the mid 1800's it features three sets of concentric ribbons on the exterior to elegantly highlight an intricate, deep reddish color which evokes warmth and comfort for years to come! This historically significant vase has been featured within numerous museum exhibitions including \"Glass at The Corning Museum\" ; \"The First Half Century\" & a special travelling exhibit called:\" Sight Of Glass: British Cut Glass\" by ibex limited (retailer) as well as \"SIGNALS - Celebrating History In American Silver Through The Articulated Bottle Vessel\" presented at the Corning Museum of Glass 2012 ASA national symposium! We provide our customers with quality phot"} {"prompt": "2009 > Information And Communication Technology Index statistics - Countries ", "response": "5/22/2016\nAnnual change of mobile telephone subscriptions in Armenia (per 1 population). 2.2% increase is equivalent to 38 subscriptions per 100 people. Density rank: 121 out of 222.\nCyclist(s)/month(S). Likes bike riding? Take advantage of discount and cheap rental bikes at Rimon Bike Rentals in Yerevan! No advance payments or additional deposits are required. They have a good range of bicycles, including mountainbikes. More on their Facebook page \nYou must know about electric cars. The Renault Fluence KZERO gets it right in the city but I'm not sure what mileage you can expect from it. Still its fun project http://www.renault-kzen.com\nFor more on this and related issues : Armenian Institute for Electronic Governance reports |"} ......
3.3 Phát hiện watermark
Phát hiện watermark nghĩa là tính độ mạnh của watermark (z-score) trong một đoạn văn bản cho trước.
- Tính độ mạnh của watermark trong văn bản có watermark
- python
python3 detect.py \ --base_model $MODEL_NAME \ --detect_file gen/$MODEL_ABBR/kgw/mc4.en.mod.jsonl \ --output_file gen/$MODEL_ABBR/kgw/mc4.en.mod.z_score.jsonl \ $WATERMARK_METHOD_FLAG
- Tính độ mạnh của watermark trong văn bản không có watermark
- python
python3 detect.py \ --base_model $MODEL_NAME \ --detect_file data/dataset/mc4/mc4.en.jsonl \ --output_file gen/$MODEL_ABBR/kgw/mc4.en.hum.z_score.jsonl \ $WATERMARK_METHOD_FLAG
- File output có định dạng:
- json
{"z_score": 12.105422509165574, "prompt": "Ghost of Emmett Till: Based on Real Life Events ", "response": ".In August if 1955 African American Emmett Louis Till (21)\nThe second part of The Man From Waco, about Dan Millers trial for murdering his friend Michael Capps in a Texas wiener wrastle as I believe the statute says called it then; back at that time that would have surely occurred since Dan kept his pistol in one of those watery doggy bags he keeps around to clean himself with after emptying can into a nearby lake just minutes before committing his crime. If what we read is true thats exactly where Dan left his stolen gun and later used it in the robbery gone wrong which killed two innocent boys when his own accomplice got into an argument over not being paid enough therefore wanting out. This angered Miller whos history of mental instability could be taken one way or another but this criminal act was unavoidable once they entered FBIs hands and some other very powerful law officers who were involved either directly", "biases": null} {"z_score": 12.990684249887122, "prompt": "Antique Cambridge Glass Pink Decagon Console Bowl Engraved Gold Highlights\n", "response": "An exceptionally fine decorative antique pink decagonal glass side bowl is the perfect example of early art and innovation . Fully engraved, this beautiful English vintage tableware piece exhibits a great degree on craftsmanship! Made in England during the mid 1800's it features three sets of concentric ribbons on the exterior to elegantly highlight an intricate, deep reddish color which evokes warmth and comfort for years to come! This historically significant vase has been featured within numerous museum exhibitions including \"Glass at The Corning Museum\" ; \"The First Half Century\" & a special travelling exhibit called:\" Sight Of Glass: British Cut Glass\" by ibex limited (retailer) as well as \"SIGNALS - Celebrating History In American Silver Through The Articulated Bottle Vessel\" presented at the Corning Museum of Glass 2012 ASA national symposium! We provide our customers with quality phot", "biases": null} {"z_score": 11.455466938203664, "prompt": "2009 > Information And Communication Technology Index statistics - Countries ", "response": "5/22/2016\nAnnual change of mobile telephone subscriptions in Armenia (per 1 population). 2.2% increase is equivalent to 38 subscriptions per 100 people. Density rank: 121 out of 222.\nCyclist(s)/month(S). Likes bike riding? Take advantage of discount and cheap rental bikes at Rimon Bike Rentals in Yerevan! No advance payments or additional deposits are required. They have a good range of bicycles, including mountainbikes. More on their Facebook page \nYou must know about electric cars. The Renault Fluence KZERO gets it right in the city but I'm not sure what mileage you can expect from it. Still its fun project http://www.renault-kzen.com\nFor more on this and related issues : Armenian Institute for Electronic Governance reports |", "biases": null} ......
- Mở hai file ra xem bằng mắt thường độ mạnh của watermark khác nhau thế nào
3.4 Evaluation watermark
- Dùng các file z-score thu được ở bước phát hiện watermark làm input, tính độ chính xác khi phát hiện và vẽ đường cong ROC
- shell
python3 eval_detection.py \ --hm_zscore gen/$MODEL_ABBR/kgw/mc4.en.hum.z_score.jsonl \ --wm_zscore gen/$MODEL_ABBR/kgw/mc4.en.mod.z_score.jsonl \ --roc_curve roc AUC: 1.000 TPR@FPR=0.1: 0.998 TPR@FPR=0.01: 0.998 F1@FPR=0.1: 0.999 F1@FPR=0.01: 0.999

Đánh giá robustness của watermark (tùy chọn)
Sau khi tấn công văn bản có watermark bằng paraphrase và translation, hãy đánh giá lại khả năng phát hiện watermark
4.1 Chuẩn bị
Chúng ta dùng mô hình gpt-3.5-turbo-1106 để thực hiện paraphrase và translation trên văn bản có watermark. Bạn cũng có thể tự chọn công cụ khác.
- Thiết lập apikey của openai
- shell
export OPENAI_API_KEY=xxxx
- Sửa RPM (requests per min) and TPM (tokens per min) trong
attack/const.py
4.2 Thực hiện tấn công (lấy translation làm ví dụ)
- Dịch văn bản có watermark sang tiếng Trung
- shell
python3 attack/translate.py \ --input_file gen/$MODEL_ABBR/kgw/mc4.en.mod.jsonl \ --output_file gen/$MODEL_ABBR/kgw/mc4.en-zh.mod.jsonl \ --model gpt-3.5-turbo-1106 \ --src_lang en \ --tgt_lang zh
- Đánh giá lại
- Xem 3.4
- So sánh hiệu quả của watermark trước và sau khi bị tấn công
Bài tập nâng cao
- Xem tài liệu X-SIR, học cách dùng hai thuật toán còn lại (X-SIR, SIR) và đánh giá hiệu quả của chúng trước các phương pháp tấn công khác nhau