muellerzr HF staff commited on
Commit
dbf5fb1
1 Parent(s): 78fa635

New sectiosn including doc link and explanation

Browse files
Files changed (4) hide show
  1. README.md +25 -14
  2. code_samples/basic +14 -1
  3. src/app.py +23 -8
  4. src/markup.py +15 -8
README.md CHANGED
@@ -19,29 +19,40 @@ These are more simplified versions of examples that exist in the [accelerate](ht
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
 
 
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
+ ##
26
  <pre>
27
+ +from accelerate import Accelerator
28
+ +accelerator = Accelerator()
29
+ +dataloader, model, optimizer scheduler = accelerator.prepare(
30
+ + dataloader, model, optimizer, scheduler
31
+ +)
32
+
33
+ for batch in dataloader:
34
+ optimizer.zero_grad()
 
35
  inputs, targets = batch
36
+ - inputs = inputs.to(device)
37
+ - targets = targets.to(device)
38
  outputs = model(inputs)
39
  loss = loss_function(outputs, targets)
40
+ - loss.backward()
41
+ + accelerator.backward(loss)
42
  optimizer.step()
43
+ scheduler.step()</pre>
44
+ ##
45
+ Everything around `accelerate` ...
46
+
47
+ ##
48
+ To learn more checkout the related documentation:
49
  ```
50
 
51
+ There are three overall "sections" separated by two "##" and a newline:
52
+
53
+ 1. The code diff that should be rendered with the new code
54
+ 2. An explanation of what the diff represents and what's new. Should be up to a paragraph as a TL;DR
55
+ 3. Link to the documentation users can go to quickly to learn more.
56
 
57
  ## Creating a `diff`
58
 
code_samples/basic CHANGED
@@ -1,3 +1,4 @@
 
1
  <pre>
2
  +from accelerate import Accelerator
3
  +accelerator = Accelerator()
@@ -15,4 +16,16 @@ for batch in dataloader:
15
  - loss.backward()
16
  + accelerator.backward(loss)
17
  optimizer.step()
18
- scheduler.step()</pre>
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ##
2
  <pre>
3
  +from accelerate import Accelerator
4
  +accelerator = Accelerator()
 
16
  - loss.backward()
17
  + accelerator.backward(loss)
18
  optimizer.step()
19
+ scheduler.step()</pre>
20
+ ##
21
+ Everything around `accelerate` occurs with the `Accelerator` class. To use it, first make an object.
22
+ Then call `.prepare` passing in the PyTorch objects that you would normally train with. This will
23
+ return the same objects, but they will be on the correct device and distributed if needed. Then
24
+ you can train as normal, but instead of calling `loss.backward()` you call `accelerator.backward(loss)`.
25
+ Also note that you don't need to call `model.to(device)` or `inputs.to(device)` anymore, as this
26
+ is done automatically by `accelerator.prepare()`.
27
+
28
+ ##
29
+ To learn more checkout the related documentation:
30
+ - [Basic Tutorial](https://huggingface.co/docs/accelerate/basic_tutorials/migration)
31
+ - [The Accelerator](https://huggingface.co/docs/accelerate/package_reference/accelerator)
src/app.py CHANGED
@@ -1,10 +1,10 @@
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:
@@ -14,12 +14,16 @@ def change(inp):
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
@@ -31,10 +35,21 @@ Select how you would like to introduce an Accelerate capability to add to it.'''
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()
 
1
  import gradio as gr
2
+ from markup import highlight, get_text
3
  from template import get_templates
4
 
5
  templates = get_templates()
6
 
7
+ def change(inp, components=[]):
8
  """Based on an `inp`, render and highlight the appropriate code sample.
9
 
10
  Args:
 
14
  Returns:
15
  `tuple`: A tuple of the initial code, the highlighted code, and the title for the section.
16
  """
17
+ code, explanation, docs = get_text(inp)
18
  if inp == "Basic":
19
+ return (templates["initial"], highlight(code), "## Accelerate Code (Base Integration)", explanation, docs)
20
  elif inp == "Calculating Metrics":
21
+ return (templates["initial_with_metrics"], highlight(inp), f"## Accelerate Code ({inp})", explanation, docs)
22
  else:
23
+ return (templates["accelerate"], highlight(inp), f"## Accelerate Code ({inp})", explanation, docs)
24
+
25
+ initial_md = gr.Markdown("## Initial Code")
26
+ initial_code = gr.Markdown(templates["initial"])
27
 
28
  with gr.Blocks() as demo:
29
  gr.Markdown(f'''# Accelerate Training Code Template Generator
 
35
  )
36
  with gr.Row():
37
  with gr.Column():
38
+ initial_md.render()
39
+ initial_code.render()
40
  with gr.Column():
41
  feature = gr.Markdown("## Accelerate Code")
42
  out = gr.Markdown()
43
+ with gr.Row():
44
+ with gr.Column():
45
+ gr.Markdown("## Documentation Links")
46
+ docs = gr.Markdown()
47
+ with gr.Column():
48
+ gr.Markdown("## Explanation")
49
+ explanation = gr.Markdown()
50
+ inp.change(
51
+ fn=change,
52
+ inputs=inp,
53
+ outputs=[initial_code, out, feature, explanation, docs]
54
+ )
55
  demo.launch()
src/markup.py CHANGED
@@ -35,17 +35,14 @@ def mark_text(text, add=True):
35
  color = _remove_color
36
  return f'<mark style="background-color:{color}!important;color:white!important">{text}</mark>'
37
 
38
- def highlight(option:str):
39
- """Takes an option and returns the respective highlighted code sample.
40
 
41
  Args:
42
- option (`str`):
43
- A menu option chosen from the interface.
44
  """
45
- filename = option.lower().replace(' ', '_')
46
- with open(get_filename(filename)) as f:
47
- output = f.read()
48
- lines = output.split("\n")
49
  for i,line in enumerate(lines):
50
  if line.startswith("-"):
51
  lines[i] = "- " + line[1:]
@@ -56,3 +53,13 @@ def highlight(option:str):
56
  else:
57
  lines[i] = " " + line
58
  return "\n".join(lines).rstrip()
 
 
 
 
 
 
 
 
 
 
 
35
  color = _remove_color
36
  return f'<mark style="background-color:{color}!important;color:white!important">{text}</mark>'
37
 
38
+ def highlight(code:str):
39
+ """Takes in code and returns the respective highlighted code sample.
40
 
41
  Args:
42
+ code (`str`):
43
+ Code from a file.
44
  """
45
+ lines = code.split("\n")
 
 
 
46
  for i,line in enumerate(lines):
47
  if line.startswith("-"):
48
  lines[i] = "- " + line[1:]
 
53
  else:
54
  lines[i] = " " + line
55
  return "\n".join(lines).rstrip()
56
+
57
+ def get_text(option):
58
+ """
59
+ Reads in an option and returns the code, explanation, and documentation links
60
+ """
61
+ filename = option.lower().replace(' ', '_')
62
+ with open(get_filename(filename)) as f:
63
+ output = f.read()
64
+ code, explanation, doclink = output.split("##\n")[1:]
65
+ return code, explanation, doclink