-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create rule S6983 #3973
Merged
Merged
Create rule S6983 #3973
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"title": "The \"num_workers\" parameter should be specified for \"torch.utils.data.DataLoader\"", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "2min" | ||
}, | ||
"tags": [ | ||
"pytorch", | ||
"machine-learning" | ||
], | ||
"defaultSeverity": "Minor", | ||
"ruleSpecification": "RSPEC-6983", | ||
"sqKey": "S6983", | ||
"scope": "All", | ||
"defaultQualityProfiles": ["Sonar way"], | ||
"quickfix": "targeted", | ||
"code": { | ||
"impacts": { | ||
"RELIABILITY": "LOW" | ||
}, | ||
"attribute": "COMPLETE" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
This rule raises an issue when a `torch.utils.data.Dataloader` is instantiated without specifying the `num_workers` parameter. | ||
|
||
== Why is this an issue? | ||
|
||
In the PyTorch library, the data loaders are used to provide an interface where common operations such as batching can be implemented. | ||
It is also possible to parallelize the data loading process by using multiple worker processes. | ||
This can improve performance by increasing the number of batches being fetched in parallel, at the cost of higher memory usage. | ||
This performance increase can also be attributed to avoiding the Global Interpreter Lock (GIL) in the Python interpreter. | ||
|
||
|
||
== How to fix it | ||
Specify the `num_workers` parameter when instantiating the `torch.utils.data.Dataloader` object. | ||
|
||
The default value of `0` will use the main process to load the data, and might be faster for small datasets that can fit completely in memory. | ||
|
||
For larger datasets, it is recommended to use a value of `1` or higher to parallelize the data loading process. | ||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,python,diff-id=1,diff-type=noncompliant] | ||
---- | ||
from torch.utils.data import DataLoader | ||
from torchvision import datasets | ||
from torchvision.transforms import ToTensor | ||
|
||
train_dataset = datasets.MNIST(root='data', train=True, transform=ToTensor()) | ||
train_data_loader = DataLoader(train_dataset, batch_size=32)# Noncompliant: the num_workers parameter is not specified | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,python,diff-id=1,diff-type=compliant] | ||
---- | ||
from torch.utils.data import DataLoader | ||
from torchvision import datasets | ||
from torchvision.transforms import ToTensor | ||
|
||
train_dataset = datasets.MNIST(root='data', train=True, transform=ToTensor()) | ||
train_data_loader = DataLoader(train_dataset, batch_size=32, num_workers=4) | ||
---- | ||
|
||
== Resources | ||
=== Documentation | ||
|
||
* PyTorch documentation - https://pytorch.org/docs/stable/data.html#single-and-multi-process-data-loading[Single- and Multi-process Data Loading] | ||
|
||
* PyTorch documentation - https://pytorch.org/tutorials/beginner/basics/data_tutorial.html[Datasets and DataLoaders] | ||
|
||
ifdef::env-github,rspecator-view[] | ||
|
||
(visible only on this page) | ||
|
||
== Implementation specification | ||
|
||
|
||
=== Message | ||
|
||
Primary : Specify the `num_workers` parameter. | ||
|
||
=== Issue location | ||
|
||
Primary : Name of the instantiation | ||
|
||
=== Quickfix | ||
|
||
Fill in with the default parameter | ||
|
||
endif::env-github,rspecator-view[] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not 100% sure here but I think
worker
should plurializedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should not be pluralized