Skip to content

Commit 0a71fe2

Browse files
CI: black docs (#8566)
* black docs Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent aadd2a9 commit 0a71fe2

31 files changed

+303
-336
lines changed

.github/CONTRIBUTING.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ See following short example of a sample function taking one position string and
137137
```python
138138
from typing import Optional
139139

140+
140141
def my_func(param_a: int, param_b: Optional[float] = None) -> str:
141142
"""Sample function.
142143
@@ -310,10 +311,7 @@ def test_explain_what_is_being_tested(tmpdir):
310311
# BoringModel is a functional model. You might want to set methods to None to test your behaviour
311312
# Example: model.training_step_end = None
312313

313-
trainer = Trainer(
314-
default_root_dir=tmpdir, # will save everything within a tmpdir generated for this test
315-
...
316-
)
314+
trainer = Trainer(default_root_dir=tmpdir, ...) # will save everything within a tmpdir generated for this test
317315
trainer.fit(model)
318316
trainer.test() # [OPTIONAL]
319317

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ repos:
6464
- id: black
6565
name: Format code
6666

67+
- repo: https://github.com/asottile/blacken-docs
68+
rev: v1.10.0
69+
hooks:
70+
- id: blacken-docs
71+
args: [ --line-length=120 ]
72+
additional_dependencies: [ black==21.7b0 ]
73+
6774
- repo: https://github.com/PyCQA/flake8
6875
rev: 3.9.2
6976
hooks:

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ A LightningModule defines a full *system* (ie: a GAN, autoencoder, BERT or a sim
165165

166166
```python
167167
class LitAutoEncoder(pl.LightningModule):
168-
169168
def __init__(self):
170169
super().__init__()
171170
self.encoder = nn.Sequential(nn.Linear(28 * 28, 128), nn.ReLU(), nn.Linear(128, 3))
@@ -183,7 +182,7 @@ class LitAutoEncoder(pl.LightningModule):
183182
z = self.encoder(x)
184183
x_hat = self.decoder(z)
185184
loss = F.mse_loss(x_hat, x)
186-
self.log('train_loss', loss)
185+
self.log("train_loss", loss)
187186
return loss
188187

189188
def configure_optimizers(self):

docs/source/advanced/ipu.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Specify the number of IPUs to train with. Note that when training with IPUs, you
3434

3535
.. code-block:: python
3636
37-
trainer = pl.Trainer(ipus=8) # Train using data parallel on 8 IPUs
37+
trainer = pl.Trainer(ipus=8) # Train using data parallel on 8 IPUs
3838
3939
IPUs only support specifying a single number to allocate devices, which is handled via the underlying libraries.
4040

@@ -102,10 +102,7 @@ Note that by default we return the last device iteration loss. You can override
102102
training_opts.anchorMode(poptorch.AnchorMode.All)
103103
training_opts.deviceIterations(32)
104104
105-
trainer = Trainer(
106-
ipus=8,
107-
plugins=IPUPlugin(inference_opts=inference_opts, training_opts=training_opts)
108-
)
105+
trainer = Trainer(ipus=8, plugins=IPUPlugin(inference_opts=inference_opts, training_opts=training_opts))
109106
trainer.fit(model)
110107
111108
You can also override all options by passing the ``poptorch.Options`` to the plugin. See `PopTorch options documentation <https://docs.graphcore.ai/projects/poptorch-user-guide/en/latest/batching.html>`__ for more information.
@@ -127,7 +124,7 @@ Lightning supports dumping all reports to a directory to open using the tool.
127124
from pytorch_lightning.plugins import IPUPlugin
128125
129126
model = MyLightningModule()
130-
trainer = pl.Trainer(ipus=8, plugins=IPUPlugin(autoreport_dir='report_dir/'))
127+
trainer = pl.Trainer(ipus=8, plugins=IPUPlugin(autoreport_dir="report_dir/"))
131128
trainer.fit(model)
132129
133130
This will dump all reports to ``report_dir/`` which can then be opened using the Graph Analyser Tool, see `Opening Reports <https://docs.graphcore.ai/projects/graphcore-popvision-user-guide/en/latest/graph/graph.html#opening-reports>`__.
@@ -155,8 +152,8 @@ Below is an example using the block annotation in a LightningModule.
155152
import pytorch_lightning as pl
156153
import poptorch
157154
158-
class MyLightningModule(pl.LightningModule):
159155
156+
class MyLightningModule(pl.LightningModule):
160157
def __init__(self):
161158
super().__init__()
162159
# This will place layer1, layer2+layer3, layer4, softmax on different IPUs at runtime.
@@ -175,6 +172,7 @@ Below is an example using the block annotation in a LightningModule.
175172
176173
...
177174
175+
178176
model = MyLightningModule()
179177
trainer = pl.Trainer(ipus=8, plugins=IPUPlugin(device_iterations=20))
180178
trainer.fit(model)
@@ -187,8 +185,8 @@ You can also use the block context manager within the forward function, or any o
187185
import pytorch_lightning as pl
188186
import poptorch
189187
190-
class MyLightningModule(pl.LightningModule):
191188
189+
class MyLightningModule(pl.LightningModule):
192190
def __init__(self):
193191
super().__init__()
194192
self.layer1 = torch.nn.Linear(5, 10)
@@ -214,8 +212,10 @@ You can also use the block context manager within the forward function, or any o
214212
with poptorch.Block(ipu_id=3):
215213
x = self.softmax(x)
216214
return x
215+
217216
...
218217
218+
219219
model = MyLightningModule()
220220
trainer = pl.Trainer(ipus=8, plugins=IPUPlugin(device_iterations=20))
221221
trainer.fit(model)

docs/source/advanced/lr_finder.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ which can be accessed via ``self.learning_rate`` or ``self.lr``.
4646
.. code-block:: python
4747
4848
class LitModel(LightningModule):
49-
5049
def __init__(self, learning_rate):
5150
self.learning_rate = learning_rate
5251
5352
def configure_optimizers(self):
5453
return Adam(self.parameters(), lr=(self.lr or self.learning_rate))
5554
55+
5656
model = LitModel()
5757
5858
# finds learning rate automatically
@@ -68,7 +68,7 @@ If your model is using an arbitrary value instead of ``self.lr`` or ``self.learn
6868
model = LitModel()
6969
7070
# to set to your own hparams.my_value
71-
trainer = Trainer(auto_lr_find='my_value')
71+
trainer = Trainer(auto_lr_find="my_value")
7272
7373
trainer.tune(model)
7474

docs/source/advanced/sequences.rst

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ Lightning can handle TBTT automatically via this flag.
1313

1414
from pytorch_lightning import LightningModule
1515

16-
class MyModel(LightningModule):
1716

17+
class MyModel(LightningModule):
1818
def __init__(self):
1919
super().__init__()
2020
# Important: This property activates truncated backpropagation through time
@@ -26,10 +26,7 @@ Lightning can handle TBTT automatically via this flag.
2626
# the training step must be updated to accept a ``hiddens`` argument
2727
# hiddens are the hiddens from the previous truncated backprop step
2828
out, hiddens = self.lstm(data, hiddens)
29-
return {
30-
"loss": ...,
31-
"hiddens": hiddens
32-
}
29+
return {"loss": ..., "hiddens": hiddens}
3330

3431
.. note:: If you need to modify how the batch is split,
3532
override :meth:`pytorch_lightning.core.LightningModule.tbptt_split_batch`.

docs/source/advanced/tpu.rst

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,29 +92,18 @@ for TPU use
9292
9393
import torch_xla.core.xla_model as xm
9494
95+
9596
def train_dataloader(self):
96-
dataset = MNIST(
97-
os.getcwd(),
98-
train=True,
99-
download=True,
100-
transform=transforms.ToTensor()
101-
)
97+
dataset = MNIST(os.getcwd(), train=True, download=True, transform=transforms.ToTensor())
10298
10399
# required for TPU support
104100
sampler = None
105101
if use_tpu:
106102
sampler = torch.utils.data.distributed.DistributedSampler(
107-
dataset,
108-
num_replicas=xm.xrt_world_size(),
109-
rank=xm.get_ordinal(),
110-
shuffle=True
103+
dataset, num_replicas=xm.xrt_world_size(), rank=xm.get_ordinal(), shuffle=True
111104
)
112105
113-
loader = DataLoader(
114-
dataset,
115-
sampler=sampler,
116-
batch_size=32
117-
)
106+
loader = DataLoader(dataset, sampler=sampler, batch_size=32)
118107
119108
return loader
120109

docs/source/advanced/training_tricks.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ If the Trainer's ``gradient_clip_algorithm`` is set to ``'value'`` (``'norm'`` b
4646
trainer = Trainer(gradient_clip_val=0.5) # gradient_clip_algorithm='norm' by default
4747

4848
# clip gradients' maximum magnitude to <=0.5
49-
trainer = Trainer(gradient_clip_val=0.5, gradient_clip_algorithm='value')
49+
trainer = Trainer(gradient_clip_val=0.5, gradient_clip_algorithm="value")
5050

5151
----------
5252

@@ -82,7 +82,7 @@ longer training time. Inspired by https://github.com/BlackHC/toma.
8282
trainer = Trainer(auto_scale_batch_size=None)
8383
8484
# Autoscale batch size
85-
trainer = Trainer(auto_scale_batch_size=None|'power'|'binsearch')
85+
trainer = Trainer(auto_scale_batch_size=None | "power" | "binsearch")
8686
8787
# find the batch size
8888
trainer.tune(model)
@@ -107,7 +107,7 @@ search for batch sizes larger than the size of the training dataset.
107107
.. code-block:: python
108108
109109
def train_dataloader(self):
110-
return DataLoader(train_dataset, batch_size=self.batch_size|self.hparams.batch_size)
110+
return DataLoader(train_dataset, batch_size=self.batch_size | self.hparams.batch_size)
111111
112112
.. warning::
113113

docs/source/advanced/transfer_learning.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ Let's use the `AutoEncoder` as a feature extractor in a separate model.
2121
class Encoder(torch.nn.Module):
2222
...
2323

24+
2425
class AutoEncoder(LightningModule):
2526
def __init__(self):
2627
self.encoder = Encoder()
2728
self.decoder = Decoder()
2829

30+
2931
class CIFAR10Classifier(LightningModule):
3032
def __init__(self):
3133
# init the pretrained LightningModule
@@ -50,6 +52,7 @@ Example: Imagenet (computer Vision)
5052

5153
import torchvision.models as models
5254

55+
5356
class ImagenetTransferLearning(LightningModule):
5457
def __init__(self):
5558
super().__init__()
@@ -102,20 +105,16 @@ Here's a model that uses `Huggingface transformers <https://github.com/huggingfa
102105
.. testcode::
103106

104107
class BertMNLIFinetuner(LightningModule):
105-
106108
def __init__(self):
107109
super().__init__()
108110

109-
self.bert = BertModel.from_pretrained('bert-base-cased', output_attentions=True)
111+
self.bert = BertModel.from_pretrained("bert-base-cased", output_attentions=True)
110112
self.W = nn.Linear(bert.config.hidden_size, 3)
111113
self.num_classes = 3
112114

113-
114115
def forward(self, input_ids, attention_mask, token_type_ids):
115116

116-
h, _, attn = self.bert(input_ids=input_ids,
117-
attention_mask=attention_mask,
118-
token_type_ids=token_type_ids)
117+
h, _, attn = self.bert(input_ids=input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids)
119118

120119
h_cls = h[:, 0]
121120
logits = self.W(h_cls)

docs/source/clouds/cluster.rst

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ To train a model using multiple nodes, do the following:
5252
.. code-block:: python
5353
5454
# train on 32 GPUs across 4 nodes
55-
trainer = Trainer(gpus=8, num_nodes=4, accelerator='ddp')
55+
trainer = Trainer(gpus=8, num_nodes=4, accelerator="ddp")
5656
5757
5858
Submit a job to the cluster
@@ -91,7 +91,7 @@ To train a model using multiple nodes, do the following:
9191
.. code-block:: python
9292
9393
# train on 32 GPUs across 4 nodes
94-
trainer = Trainer(gpus=8, num_nodes=4, accelerator='ddp')
94+
trainer = Trainer(gpus=8, num_nodes=4, accelerator="ddp")
9595
9696
3. It's a good idea to structure your training script like this:
9797

@@ -101,16 +101,12 @@ To train a model using multiple nodes, do the following:
101101
def main(hparams):
102102
model = LightningTemplateModel(hparams)
103103

104-
trainer = Trainer(
105-
gpus=8,
106-
num_nodes=4,
107-
accelerator='ddp'
108-
)
104+
trainer = Trainer(gpus=8, num_nodes=4, accelerator="ddp")
109105

110106
trainer.fit(model)
111107

112108

113-
if __name__ == '__main__':
109+
if __name__ == "__main__":
114110
root_dir = os.path.dirname(os.path.realpath(__file__))
115111
parent_parser = ArgumentParser(add_help=False)
116112
hyperparams = parser.parse_args()
@@ -197,45 +193,42 @@ See also the multi-node examples
197193
# grid search 3 values of learning rate and 3 values of number of layers for your net
198194
# this generates 9 experiments (lr=1e-3, layers=16), (lr=1e-3, layers=32),
199195
# (lr=1e-3, layers=64), ... (lr=1e-1, layers=64)
200-
parser = HyperOptArgumentParser(strategy='grid_search', add_help=False)
201-
parser.opt_list('--learning_rate', default=0.001, type=float,
202-
options=[1e-3, 1e-2, 1e-1], tunable=True)
203-
parser.opt_list('--layers', default=1, type=float, options=[16, 32, 64], tunable=True)
196+
parser = HyperOptArgumentParser(strategy="grid_search", add_help=False)
197+
parser.opt_list("--learning_rate", default=0.001, type=float, options=[1e-3, 1e-2, 1e-1], tunable=True)
198+
parser.opt_list("--layers", default=1, type=float, options=[16, 32, 64], tunable=True)
204199
hyperparams = parser.parse_args()
205200
206201
# Slurm cluster submits 9 jobs, each with a set of hyperparams
207202
cluster = SlurmCluster(
208203
hyperparam_optimizer=hyperparams,
209-
log_path='/some/path/to/save',
204+
log_path="/some/path/to/save",
210205
)
211206
212207
# OPTIONAL FLAGS WHICH MAY BE CLUSTER DEPENDENT
213208
# which interface your nodes use for communication
214-
cluster.add_command('export NCCL_SOCKET_IFNAME=^docker0,lo')
209+
cluster.add_command("export NCCL_SOCKET_IFNAME=^docker0,lo")
215210
216211
# see the output of the NCCL connection process
217212
# NCCL is how the nodes talk to each other
218-
cluster.add_command('export NCCL_DEBUG=INFO')
213+
cluster.add_command("export NCCL_DEBUG=INFO")
219214
220215
# setting a master port here is a good idea.
221-
cluster.add_command('export MASTER_PORT=%r' % PORT)
216+
cluster.add_command("export MASTER_PORT=%r" % PORT)
222217
223218
# ************** DON'T FORGET THIS ***************
224219
# MUST load the latest NCCL version
225-
cluster.load_modules(['NCCL/2.4.7-1-cuda.10.0'])
220+
cluster.load_modules(["NCCL/2.4.7-1-cuda.10.0"])
226221
227222
# configure cluster
228223
cluster.per_experiment_nb_nodes = 12
229224
cluster.per_experiment_nb_gpus = 8
230225
231-
cluster.add_slurm_cmd(cmd='ntasks-per-node', value=8, comment='1 task per gpu')
226+
cluster.add_slurm_cmd(cmd="ntasks-per-node", value=8, comment="1 task per gpu")
232227
233228
# submit a script with 9 combinations of hyper params
234229
# (lr=1e-3, layers=16), (lr=1e-3, layers=32), (lr=1e-3, layers=64), ... (lr=1e-1, layers=64)
235230
cluster.optimize_parallel_cluster_gpu(
236-
main,
237-
nb_trials=9, # how many permutations of the grid search to run
238-
job_name='name_for_squeue'
231+
main, nb_trials=9, job_name="name_for_squeue" # how many permutations of the grid search to run
239232
)
240233
241234
@@ -259,8 +252,8 @@ and node rank (node id). Here is an example of a custom
259252
import os
260253
from pytorch_lightning.plugins.environments import ClusterEnvironment
261254
262-
class MyClusterEnvironment(ClusterEnvironment):
263255
256+
class MyClusterEnvironment(ClusterEnvironment):
264257
def creates_children(self) -> bool:
265258
# return True if the cluster is managed (you don't launch processes yourself)
266259
return True

0 commit comments

Comments
 (0)