diff --git a/docs/header_names/allowed_framework_names.adoc b/docs/header_names/allowed_framework_names.adoc index d746ac56a4..f531e25504 100644 --- a/docs/header_names/allowed_framework_names.adoc +++ b/docs/header_names/allowed_framework_names.adoc @@ -123,6 +123,7 @@ * SignXML * SQLAlchemy * ssl +* TensorFlow // Docker * Wget // Cloudformation diff --git a/rules/S6929/python/metadata.json b/rules/S6929/python/metadata.json index 3b898974b1..015831d3d2 100644 --- a/rules/S6929/python/metadata.json +++ b/rules/S6929/python/metadata.json @@ -1,5 +1,5 @@ { - "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": { @@ -7,13 +7,17 @@ "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", diff --git a/rules/S6929/python/rule.adoc b/rules/S6929/python/rule.adoc index 83c46a414e..72c12a1479 100644 --- a/rules/S6929/python/rule.adoc +++ b/rules/S6929/python/rule.adoc @@ -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] @@ -61,7 +63,11 @@ 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... @@ -88,6 +94,32 @@ tf.math.reduce_sum(x, axis=0) # Compliant: the reduction will happen only on the ---- +== 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... + +=== Code examples + +==== Noncompliant code example + +[source,python,diff-id=2,diff-type=noncompliant] +---- +import torch + +x = torch.tensor([[1, 1, 1], [1, 1, 1]]) +torch.sum(x) # Noncompliant: the dim argument defaults to None +---- + +==== Compliant solution + +[source,python,diff-id=2,diff-type=compliant] +---- +import torch + +x = torch.tensor([[1, 1, 1], [1, 1, 1]]) +torch.sum(x, dim=None) # Compliant: all dimensions will be reduced +---- + == Resources === Documentation @@ -99,6 +131,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]