waterfall 0.2.5__tar.gz → 0.2.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: waterfall
3
- Version: 0.2.5
3
+ Version: 0.2.7
4
4
  Summary: Scalable Framework for Robust Text Watermarking and Provenance for LLMs
5
5
  Project-URL: Homepage, https://github.com/aoi3142/Waterfall
6
6
  Project-URL: Issues, https://github.com/aoi3142/Waterfall/issues
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "waterfall"
7
- version = "0.2.5"
7
+ version = "0.2.7"
8
8
  authors = [
9
9
  { name = "Xinyuan Niu", email="aperture@outlook.sg" }
10
10
  ]
@@ -227,6 +227,7 @@ class Watermarker:
227
227
  return_scores : bool = False,
228
228
  use_tqdm : bool = False,
229
229
  batched_generate : bool = True,
230
+ discard_incomplete : bool = True,
230
231
  **kwargs # Other generate parameters
231
232
  ) -> List[str] | dict: # Returns flattened list of query x beam
232
233
 
@@ -305,7 +306,10 @@ class Watermarker:
305
306
  tokenizer=self.tokenizer,
306
307
  **kwargs
307
308
  )
308
- output = output[:,tokd_input_batch["input_ids"].shape[-1]:].to("cpu", non_blocking=True)
309
+ output = output[:,tokd_input_batch["input_ids"].shape[-1]:]
310
+ if discard_incomplete:
311
+ output[output[:, -1] == self.tokenizer.eos_token_id].fill_(self.tokenizer.eos_token_id)
312
+ output = output.to("cpu", non_blocking=True)
309
313
  outputs.append(output)
310
314
  bar.update(tokd_input_batch["input_ids"].shape[0])
311
315
  torch.cuda.synchronize()
@@ -1,5 +0,0 @@
1
- {
2
- "python-envs.defaultEnvManager": "ms-python.python:conda",
3
- "python-envs.defaultPackageManager": "ms-python.python:conda",
4
- "python-envs.pythonProjects": []
5
- }
waterfall-0.2.5/pypi.txt DELETED
@@ -1,2 +0,0 @@
1
- python3 -m build
2
- python3 -m twine upload dist/*
@@ -1,129 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": 1,
6
- "id": "ddca610d",
7
- "metadata": {},
8
- "outputs": [],
9
- "source": [
10
- "from waterfall.watermark import *\n",
11
- "import torch"
12
- ]
13
- },
14
- {
15
- "cell_type": "code",
16
- "execution_count": 2,
17
- "id": "f0b0a02a",
18
- "metadata": {},
19
- "outputs": [],
20
- "source": [
21
- "model_name_or_path = 'meta-llama/Llama-3.1-8B-Instruct'"
22
- ]
23
- },
24
- {
25
- "cell_type": "code",
26
- "execution_count": 3,
27
- "id": "ca5f7d55",
28
- "metadata": {},
29
- "outputs": [
30
- {
31
- "data": {
32
- "application/vnd.jupyter.widget-view+json": {
33
- "model_id": "2ef1e58174204e879a462a2a829d74fa",
34
- "version_major": 2,
35
- "version_minor": 0
36
- },
37
- "text/plain": [
38
- "Loading checkpoint shards: 0%| | 0/4 [00:00<?, ?it/s]"
39
- ]
40
- },
41
- "metadata": {},
42
- "output_type": "display_data"
43
- }
44
- ],
45
- "source": [
46
- "\n",
47
- "tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)\n",
48
- "model = AutoModelForCausalLM.from_pretrained(\n",
49
- " model_name_or_path,\n",
50
- " torch_dtype=torch.bfloat16,\n",
51
- " device_map=\"cuda:5\",\n",
52
- " )"
53
- ]
54
- },
55
- {
56
- "cell_type": "code",
57
- "execution_count": 4,
58
- "id": "2c475de5",
59
- "metadata": {},
60
- "outputs": [],
61
- "source": [
62
- "watermarker = Watermarker(tokenizer=tokenizer, model=model, id=1)"
63
- ]
64
- },
65
- {
66
- "cell_type": "code",
67
- "execution_count": 5,
68
- "id": "e74a58b5",
69
- "metadata": {},
70
- "outputs": [],
71
- "source": [
72
- "texts = [\"James is a scary man who loves to scare children\", \"There were 3 birds that ate the whole bowl\"]"
73
- ]
74
- },
75
- {
76
- "cell_type": "code",
77
- "execution_count": 8,
78
- "id": "fd0af01e",
79
- "metadata": {},
80
- "outputs": [],
81
- "source": [
82
- "watermarked_text = watermark_texts(texts, id, watermarker=watermarker)"
83
- ]
84
- },
85
- {
86
- "cell_type": "code",
87
- "execution_count": 10,
88
- "id": "4d58ec82",
89
- "metadata": {},
90
- "outputs": [
91
- {
92
- "data": {
93
- "text/plain": [
94
- "['James is an intimidating individual known for taking great pleasure in frightening kids, and this tendency often sends them running in terror.',\n",
95
- " \"A small flock comprised of three birds consumed the entirety of the offered bowl's contents within a single meal session.\"]"
96
- ]
97
- },
98
- "execution_count": 10,
99
- "metadata": {},
100
- "output_type": "execute_result"
101
- }
102
- ],
103
- "source": [
104
- "watermarked_text"
105
- ]
106
- }
107
- ],
108
- "metadata": {
109
- "kernelspec": {
110
- "display_name": "Python 3",
111
- "language": "python",
112
- "name": "python3"
113
- },
114
- "language_info": {
115
- "codemirror_mode": {
116
- "name": "ipython",
117
- "version": 3
118
- },
119
- "file_extension": ".py",
120
- "mimetype": "text/x-python",
121
- "name": "python",
122
- "nbconvert_exporter": "python",
123
- "pygments_lexer": "ipython3",
124
- "version": "3.12.9"
125
- }
126
- },
127
- "nbformat": 4,
128
- "nbformat_minor": 5
129
- }
@@ -1,118 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": 49,
6
- "id": "116f61a2",
7
- "metadata": {},
8
- "outputs": [],
9
- "source": [
10
- "import numpy as np\n",
11
- "from tqdm import tqdm\n",
12
- "from itertools import product, combinations\n",
13
- "from scipy.stats import binom"
14
- ]
15
- },
16
- {
17
- "cell_type": "code",
18
- "execution_count": 50,
19
- "id": "53c66973",
20
- "metadata": {},
21
- "outputs": [
22
- {
23
- "data": {
24
- "text/plain": [
25
- "np.float64(0.38742048899999965)"
26
- ]
27
- },
28
- "execution_count": 50,
29
- "metadata": {},
30
- "output_type": "execute_result"
31
- }
32
- ],
33
- "source": [
34
- "binom.pmf(1, 10, 0.1)"
35
- ]
36
- },
37
- {
38
- "cell_type": "code",
39
- "execution_count": 56,
40
- "metadata": {},
41
- "outputs": [
42
- {
43
- "name": "stderr",
44
- "output_type": "stream",
45
- "text": [
46
- "100%|██████████| 32/32 [00:00<00:00, 10845.88it/s]\n",
47
- "100%|██████████| 32/32 [00:00<00:00, 3035.09it/s]\n",
48
- "100%|██████████| 32/32 [00:00<00:00, 1622.42it/s]\n",
49
- "100%|██████████| 32/32 [00:00<00:00, 2020.93it/s]\n",
50
- "100%|██████████| 32/32 [00:00<00:00, 4749.72it/s]"
51
- ]
52
- },
53
- {
54
- "name": "stdout",
55
- "output_type": "stream",
56
- "text": [
57
- "0.6582226332676197\n"
58
- ]
59
- },
60
- {
61
- "name": "stderr",
62
- "output_type": "stream",
63
- "text": [
64
- "\n"
65
- ]
66
- }
67
- ],
68
- "source": [
69
- "n = 5 # n-gram\n",
70
- "# k = 1 # no of bits modified\n",
71
- "l = 8 # bits of watermark\n",
72
- "p = 0.1 # probability of modification\n",
73
- "\n",
74
- "result = 0\n",
75
- "for k in range(0, n + 1):\n",
76
- " binomial_prob = binom.pmf(k, n, p)\n",
77
- " if k == 0:\n",
78
- " result += binomial_prob * 1\n",
79
- " continue\n",
80
- " results = []\n",
81
- "\n",
82
- " original = np.zeros(n, dtype=bool)\n",
83
- " for original in tqdm(product([True, False], repeat=n), total=2**n):\n",
84
- " original = np.array(original, dtype=bool)\n",
85
- " original_agreement = original.mean() > 0.5\n",
86
- " for modified_indices in combinations(range(n), k):\n",
87
- " modified = original.copy()\n",
88
- " for i in product([True, False], repeat=k):\n",
89
- " modified[np.array(modified_indices)] = i\n",
90
- " modified_agreement = modified.mean() > 0.5\n",
91
- " results.append(original_agreement == modified_agreement)\n",
92
- " result += binomial_prob * np.mean(results) ** l\n",
93
- "print(result)"
94
- ]
95
- }
96
- ],
97
- "metadata": {
98
- "kernelspec": {
99
- "display_name": "Python 3",
100
- "language": "python",
101
- "name": "python3"
102
- },
103
- "language_info": {
104
- "codemirror_mode": {
105
- "name": "ipython",
106
- "version": 3
107
- },
108
- "file_extension": ".py",
109
- "mimetype": "text/x-python",
110
- "name": "python",
111
- "nbconvert_exporter": "python",
112
- "pygments_lexer": "ipython3",
113
- "version": "3.12.9"
114
- }
115
- },
116
- "nbformat": 4,
117
- "nbformat_minor": 5
118
- }
File without changes
File without changes
File without changes
File without changes
File without changes