muellerzr HF staff commited on
Commit
a791472
1 Parent(s): c170b32
.gitignore ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ pip-wheel-metadata/
24
+ share/python-wheels/
25
+ *.egg-info/
26
+ .installed.cfg
27
+ *.egg
28
+ MANIFEST
29
+
30
+ # PyInstaller
31
+ # Usually these files are written by a python script from a template
32
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
33
+ *.manifest
34
+ *.spec
35
+
36
+ # Installer logs
37
+ pip-log.txt
38
+ pip-delete-this-directory.txt
39
+
40
+ # Unit test / coverage reports
41
+ htmlcov/
42
+ .tox/
43
+ .nox/
44
+ .coverage
45
+ .coverage.*
46
+ .cache
47
+ nosetests.xml
48
+ coverage.xml
49
+ *.cover
50
+ *.py,cover
51
+ .hypothesis/
52
+ .pytest_cache/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ target/
76
+
77
+ # Jupyter Notebook
78
+ .ipynb_checkpoints
79
+
80
+ # IPython
81
+ profile_default/
82
+ ipython_config.py
83
+
84
+ # pyenv
85
+ .python-version
86
+
87
+ # pipenv
88
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
90
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
91
+ # install all needed dependencies.
92
+ #Pipfile.lock
93
+
94
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
95
+ __pypackages__/
96
+
97
+ # Celery stuff
98
+ celerybeat-schedule
99
+ celerybeat.pid
100
+
101
+ # SageMath parsed files
102
+ *.sage.py
103
+
104
+ # Environments
105
+ .env
106
+ .venv
107
+ env/
108
+ venv/
109
+ ENV/
110
+ env.bak/
111
+ venv.bak/
112
+
113
+ # Spyder project settings
114
+ .spyderproject
115
+ .spyproject
116
+
117
+ # Rope project settings
118
+ .ropeproject
119
+
120
+ # mkdocs documentation
121
+ /site
122
+
123
+ # mypy
124
+ .mypy_cache/
125
+ .dmypy.json
126
+ dmypy.json
127
+
128
+ # Pyre type checker
129
+ .pyre/
130
+
131
+ # VSCode
132
+ .vscode
133
+
134
+ # IntelliJ
135
+ .idea
136
+
137
+ # Mac .DS_Store
138
+ .DS_Store
139
+
140
+ # More test things
141
+ wandb
README.md CHANGED
@@ -4,9 +4,84 @@ emoji: 🌖
4
  colorFrom: indigo
5
  colorTo: purple
6
  sdk: gradio
7
- sdk_version: 3.13.2
8
- app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  colorFrom: indigo
5
  colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 3.14.0
8
+ app_file: src/app.py
9
  pinned: false
10
  ---
11
 
12
+ ## Accelerate Integration Examples
13
+
14
+ This is an interactive utility to show users how to incorporate parts of 🤗 Accelerate into their code.
15
+ To use it select a feature to add and a github-like `diff` will be rendered showing what code to remove
16
+ and add based on the initial template code.
17
+
18
+ These are more simplified versions of examples that exist in the [accelerate](https://github.com/huggingface/accelerate) and [transformers](https://github.com/huggingface/transforms) repositories.
19
+
20
+ ## How each example is made
21
+
22
+ In the `code_examples` folder are basic text-like files which contain a much simplified version of some integration. For example:
23
+
24
+ ```
25
+ <pre>
26
+ from accelerate import Accelerator
27
+ accelerator = Accelerator()
28
+ train_dataloader, model, optimizer scheduler = accelerator.prepare(
29
+ dataloader, model, optimizer, scheduler
30
+ )
31
+
32
+ model.train()
33
+ for batch in train_dataloader:
34
+ &nbsp;&nbsp;&nbsp;&nbsp;optimizer.zero_grad()
35
+ inputs, targets = batch
36
+ outputs = model(inputs)
37
+ loss = loss_function(outputs, targets)
38
+ accelerator.backward(loss)
39
+ optimizer.step()
40
+ scheduler.step()
41
+ </pre>
42
+ ```
43
+
44
+ These are done in an HTML-like syntax, with `pre` being used instead of three back-ticks for formatting purposes.
45
+
46
+ ## Creating a `diff`
47
+
48
+ To create a diff, a similar `pre` tag should be wrapped around the code, and a single `+` or `-` (showing an addition or subtraction) should be added to the code with no extra spacing or formatting. The tool will automatically know how to render these properly.
49
+
50
+ For example:
51
+
52
+ ```
53
+ <pre>
54
+ +from accelerate import Accelerator
55
+ +accelerator = Accelerator()
56
+ +dataloader, model, optimizer scheduler = accelerator.prepare(
57
+ + dataloader, model, optimizer, scheduler
58
+ +)
59
+
60
+ for batch in dataloader:
61
+ optimizer.zero_grad()
62
+ inputs, targets = batch
63
+ - inputs = inputs.to(device)
64
+ - targets = targets.to(device)
65
+ outputs = model(inputs)
66
+ loss = loss_function(outputs, targets)
67
+ - loss.backward()
68
+ + accelerator.backward(loss)
69
+ optimizer.step()
70
+ scheduler.step()</pre>
71
+ ```
72
+
73
+ Also note that the initial `pre` is on a newline, and the latter `</pre>` is not.
74
+
75
+ ## Rendering the diff
76
+
77
+ After a diff and starter (if needed) has been made, if a new template was created add it to `TEMPLATES` in `src/template.py`. Otherwise in `app.py` modify the `change` function to properly point to the new integration example to show on a particular selection:
78
+
79
+ ```
80
+ def change(inp):
81
+ if inp == "Basic":
82
+ return (templates["initial"], highlight(inp), "## Accelerate Code (Base Integration)")
83
+ elif inp == "Calculating Metrics":
84
+ return (templates["initial_with_metrics"], highlight(inp), f"## Accelerate Code ({inp})")
85
+ else:
86
+ return (templates["accelerate"], highlight(inp), f"## Accelerate Code ({inp})")
87
+ ```
code_samples/accelerate CHANGED
@@ -1,4 +1,4 @@
1
- ```
2
  from accelerate import Accelerator
3
  accelerator = Accelerator()
4
  train_dataloader, model, optimizer scheduler = accelerator.prepare(
@@ -7,11 +7,11 @@ train_dataloader, model, optimizer scheduler = accelerator.prepare(
7
 
8
  model.train()
9
  for batch in train_dataloader:
10
- optimizer.zero_grad()
11
  inputs, targets = batch
12
  outputs = model(inputs)
13
  loss = loss_function(outputs, targets)
14
  accelerator.backward(loss)
15
  optimizer.step()
16
  scheduler.step()
17
- ```
 
1
+ <pre>
2
  from accelerate import Accelerator
3
  accelerator = Accelerator()
4
  train_dataloader, model, optimizer scheduler = accelerator.prepare(
 
7
 
8
  model.train()
9
  for batch in train_dataloader:
10
+ &nbsp;&nbsp;&nbsp;&nbsp;optimizer.zero_grad()
11
  inputs, targets = batch
12
  outputs = model(inputs)
13
  loss = loss_function(outputs, targets)
14
  accelerator.backward(loss)
15
  optimizer.step()
16
  scheduler.step()
17
+ </pre>
code_samples/basic CHANGED
@@ -15,5 +15,4 @@ for batch in dataloader:
15
  - loss.backward()
16
  + accelerator.backward(loss)
17
  optimizer.step()
18
- scheduler.step()
19
- </pre>
 
15
  - loss.backward()
16
  + accelerator.backward(loss)
17
  optimizer.step()
18
+ scheduler.step()</pre>
 
code_samples/calculating_metrics CHANGED
@@ -32,5 +32,4 @@ for batch in eval_dataloader:
32
  predictions = predictions,
33
  references = references
34
  )
35
- print(metric.compute())
36
- <pre>
 
32
  predictions = predictions,
33
  references = references
34
  )
35
+ print(metric.compute())</pre>
 
code_samples/checkpointing CHANGED
@@ -13,5 +13,4 @@ for batch in dataloader:
13
  accelerator.backward(loss)
14
  optimizer.step()
15
  scheduler.step()
16
- +accelerator.save_state("checkpoint_dir")
17
- </pre>
 
13
  accelerator.backward(loss)
14
  optimizer.step()
15
  scheduler.step()
16
+ +accelerator.save_state("checkpoint_dir")</pre>
 
code_samples/gradient_accumulation CHANGED
@@ -15,5 +15,4 @@ for batch in dataloader:
15
  loss = loss_function(outputs, targets)
16
  accelerator.backward(loss)
17
  optimizer.step()
18
- scheduler.step()
19
- </pre>
 
15
  loss = loss_function(outputs, targets)
16
  accelerator.backward(loss)
17
  optimizer.step()
18
+ scheduler.step()</pre>
 
code_samples/initial CHANGED
@@ -1,4 +1,4 @@
1
- ```
2
  for batch in dataloader:
3
  optimizer.zero_grad()
4
  inputs, targets = batch
@@ -8,5 +8,4 @@ for batch in dataloader:
8
  loss = loss_function(outputs, targets)
9
  loss.backward()
10
  optimizer.step()
11
- scheduler.step()
12
- ```
 
1
+ <pre>
2
  for batch in dataloader:
3
  optimizer.zero_grad()
4
  inputs, targets = batch
 
8
  loss = loss_function(outputs, targets)
9
  loss.backward()
10
  optimizer.step()
11
+ scheduler.step()</pre>
 
code_samples/initial_with_metrics CHANGED
@@ -1,4 +1,4 @@
1
- ```
2
  import evaluate
3
  metric = evaluate.load("accuracy")
4
  for batch in train_dataloader:
@@ -24,5 +24,4 @@ for batch in eval_dataloader:
24
  predictions = predictions,
25
  references = references
26
  )
27
- print(metric.compute())
28
- ```
 
1
+ <pre>
2
  import evaluate
3
  metric = evaluate.load("accuracy")
4
  for batch in train_dataloader:
 
24
  predictions = predictions,
25
  references = references
26
  )
27
+ print(metric.compute())</pre>
 
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ gradio
src/__init__.py ADDED
File without changes
src/app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from markup import highlight
3
+ from template import get_templates
4
+
5
+ templates = get_templates()
6
+
7
+ def change(inp):
8
+ """Based on an `inp`, render and highlight the appropriate code sample.
9
+
10
+ Args:
11
+ inp (`str`):
12
+ The input button from the interface.
13
+
14
+ Returns:
15
+ `tuple`: A tuple of the initial code, the highlighted code, and the title for the section.
16
+ """
17
+ if inp == "Basic":
18
+ return (templates["initial"], highlight(inp), "## Accelerate Code (Base Integration)")
19
+ elif inp == "Calculating Metrics":
20
+ return (templates["initial_with_metrics"], highlight(inp), f"## Accelerate Code ({inp})")
21
+ else:
22
+ return (templates["accelerate"], highlight(inp), f"## Accelerate Code ({inp})")
23
+
24
+ with gr.Blocks() as demo:
25
+ gr.Markdown(f'''# Accelerate Training Code Template Generator
26
+ Here is a very basic Python training loop.
27
+ Select how you would like to introduce an Accelerate capability to add to it.''')
28
+ inp = gr.Radio(
29
+ ["Basic", "Calculating Metrics", "Checkpointing", "Gradient Accumulation", ],
30
+ label="Select a feature"
31
+ )
32
+ with gr.Row():
33
+ with gr.Column():
34
+ gr.Markdown("## Initial Code")
35
+ code = gr.Markdown(templates["initial"])
36
+ with gr.Column():
37
+ feature = gr.Markdown("## Accelerate Code")
38
+ out = gr.Markdown()
39
+ inp.change(fn=change, inputs=inp, outputs=[code, out, feature])
40
+ demo.launch()
app.py → src/markup.py RENAMED
@@ -1,18 +1,47 @@
1
- import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  _remove_color = "rgb(103,6,12)"
4
  _addition_color = "rgb(6,103,12)"
5
 
6
  def mark_text(text, add=True):
 
 
 
 
 
 
 
 
 
 
 
7
  if add:
8
  color = _addition_color
9
  else:
10
  color = _remove_color
11
  return f'<mark style="background-color:{color}!important;color:white!important">{text}</mark>'
12
 
13
- def highlight(option):
 
 
 
 
 
 
14
  filename = option.lower().replace(' ', '_')
15
- with open(f"code_samples/{filename}") as f:
16
  output = f.read()
17
  lines = output.split("\n")
18
  for i,line in enumerate(lines):
@@ -25,38 +54,3 @@ def highlight(option):
25
  else:
26
  lines[i] = " " + line
27
  return "\n".join(lines).rstrip()
28
-
29
- with open("code_samples/initial") as f:
30
- template = f.read()
31
-
32
- with open("code_samples/accelerate") as f:
33
- accelerated_template = f.read()
34
-
35
- with open("code_samples/initial_with_metrics") as f:
36
- metrics_template = f.read()
37
-
38
- def change(inp):
39
- if inp == "Basic":
40
- return (template, highlight(inp), "## Accelerate Code (Base Integration)")
41
- elif inp == "Calculating Metrics":
42
- return (metrics_template, highlight(inp), f"## Accelerate Code ({inp})")
43
- else:
44
- return (accelerated_template, highlight(inp), f"## Accelerate Code ({inp})")
45
-
46
- with gr.Blocks() as demo:
47
- gr.Markdown(f'''# Accelerate Template Generator
48
- Here is a very basic Python training loop.
49
- Select how you would like to introduce an Accelerate capability to add to it.''')
50
- inp = gr.Radio(
51
- ["Basic", "Calculating Metrics", "Checkpointing", "Gradient Accumulation", ],
52
- label="Select a feature"
53
- )
54
- with gr.Row():
55
- with gr.Column():
56
- gr.Markdown("## Initial Code")
57
- code = gr.Markdown(template)
58
- with gr.Column():
59
- feature = gr.Markdown("## Accelerate Code")
60
- out = gr.Markdown()
61
- inp.change(fn=change, inputs=inp, outputs=[code, out, feature])
62
- demo.launch()
 
1
+ # Copyright 2023 The HuggingFace Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
 
15
  _remove_color = "rgb(103,6,12)"
16
  _addition_color = "rgb(6,103,12)"
17
 
18
  def mark_text(text, add=True):
19
+ """Marks text with a highlight color for addition or removal.
20
+
21
+ Args:
22
+ text (`str`):
23
+ Some code to be marked.
24
+ add (`bool`, *optional*, defaults to True):
25
+ Whether to mark the text as an addition or a removal.
26
+
27
+ Returns:
28
+ `str`: The marked text as an HTML `mark` element.
29
+ """
30
  if add:
31
  color = _addition_color
32
  else:
33
  color = _remove_color
34
  return f'<mark style="background-color:{color}!important;color:white!important">{text}</mark>'
35
 
36
+ def highlight(option:str):
37
+ """Takes an option and returns the respective highlighted code sample.
38
+
39
+ Args:
40
+ option (`str`):
41
+ A menu option chosen from the interface.
42
+ """
43
  filename = option.lower().replace(' ', '_')
44
+ with open(f"../code_samples/{filename}") as f:
45
  output = f.read()
46
  lines = output.split("\n")
47
  for i,line in enumerate(lines):
 
54
  else:
55
  lines[i] = " " + line
56
  return "\n".join(lines).rstrip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/template.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2023 The HuggingFace Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ TEMPLATES = ["initial", "initial_with_metrics", "accelerate"]
16
+
17
+ def get_templates() -> dict:
18
+ """
19
+ Returns a dictionary of template type to code content
20
+ """
21
+ return {
22
+ template: open(f"../code_samples/{template}").read()
23
+ for template in TEMPLATES
24
+ }