Skip to content

Modify rule S6929: add the PyTorch library #3984

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

Merged
merged 5 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/header_names/allowed_framework_names.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@
* python-ldap
* Python SQLite
* Python Standard Library
* PyTorch
* PyYAML
* Requests
* Scrypt
* Scikit-Learn
* SignXML
* SQLAlchemy
* ssl
* TensorFlow
// Docker
* Wget
// Cloudformation
Expand Down
8 changes: 6 additions & 2 deletions rules/S6929/python/metadata.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
{
"title": "The axis argument should be specified when using TensorFlow's reduction operations",
"title": "The reduction axis/dimension should be specified when using reduction operations",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"tensorflow",
"pytorch",
"machine-learning",
"scientific-computing"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6929",
"sqKey": "S6929",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"quickfix": "targeted",
"code": {
"impacts": {
"MAINTAINABILITY": "MEDIUM",
Expand Down
18 changes: 15 additions & 3 deletions rules/S6929/python/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
This rule raises an issue when the axis argument is not provided to TensorFlow's reduction operations.
This rule raises an issue when the `axis`/`dim`` argument is not provided to reduction operations.

== Why is this an issue?

The result of TensorFlow's reduction operations (i.e. ``tf.math.reduce_sum``, ``tf.math.reduce_std``),
=== TensorFlow

The result of reduction operations (i.e. ``tf.math.reduce_sum``, ``tf.math.reduce_std``, ``torch.sum``, ``torch.mean``, etc...),
highly depends on the shape of the Tensor provided.

[source,python]
Expand Down Expand Up @@ -61,10 +63,18 @@ In the example above, specifying the axis clarifies the intent, as the result no
reduce across all dimensions the user should provide the list of axis `axis=[0,1]`
or clearly state the default behavior should be applied with ``axis=None``.

== How to fix it
=== The PyTorch equivalent

The same behavior occurs in PyTorch, but the argument is called `dim` instead of `axis`.

== How to fix it in TensorFlow

To fix this issue provide the axis argument when using a TensorFlow reduction operation such as ``tf.math.reduce_sum``, ``tf.math.reduce_prod``, ``tf.math.reduce_mean``, etc...

== How to fix it in PyTorch

To fix this issue provide the dim argument when using a PyTorch reduction operation such as ``torch.sum``, ``torch.prod``, ``torch.mean``, etc...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I think you need to provide === Code examples for PyTorch. and add another Code example section under Tensorflow.

=== Code examples

==== Noncompliant code example
Expand Down Expand Up @@ -99,6 +109,8 @@ tf.math.reduce_sum(x, axis=0) # Compliant: the reduction will happen only on the
* TensorFlow Documentation - https://www.tensorflow.org/api_docs/python/tf/math/reduce_sum[tf.math.reduce_sum reference]
* TensorFlow Documentation - https://www.tensorflow.org/api_docs/python/tf/math/reduce_variance[tf.math.reduce_variance reference]

* PyTorch Documentation - https://pytorch.org/docs/stable/torch.html#reduction-ops[Reduction operations]

=== Articles & blog posts

* Vahidk Developers Guide - https://github.com/vahidk/EffectiveTensorflow?tab=readme-ov-file#broadcasting-the-good-and-the-ugly[Broadcasting the good and the ugly]