Skip to content
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 S6922 #3624

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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 rules/S6922/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
24 changes: 24 additions & 0 deletions rules/S6922/python/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"title": "arguments of \"tf.divide\" should have the same shape or be broadcastable to the same shape",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6922",
"sqKey": "S6922",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "HIGH",
"RELIABILITY": "MEDIUM"
},
"attribute": "CONVENTIONAL"
}
}
63 changes: 63 additions & 0 deletions rules/S6922/python/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
This rule raises an issue when the `tf.divide` function arguments have incompatible shapes.

== Why is this an issue?

The `tf.divide` function in TensorFlow is used to perform element-wise division between two tensors. It divides the corresponding elements of the first tensor by the corresponding elements of the second tensor, e.g.:

[source,python]
----
x = tf.constant([4,6,8])
y = tf.constant([2,3,4])
----
When you use `tf.divide(x, y)`, TensorFlow will perform element-wise division between `x` and `y`. So, the output would be:
[source]
----
[4 / 2, 6 / 3, 8 / 4] = [2.0 2.0, 2.0]
----
Here, each element of `x` is divided by the corresponding element of `y`.

One limitation to be aware of when using `tf.divide` is that both `x` and `y` must be of compatible shapes. In other words, they must have the same shape or be broadcastable to the same shape. Otherwise it will lead to `InvalidArgumentError`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we could add a word on broadcasting or a link to the documentation for broadcasting.

Choose a reason for hiding this comment

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

added a link to numpy documentation explaining broadcastable matrices


== How to fix it
Make sure that the `tf.divide` function arguments have compatible shapes.

=== Code examples

==== Noncompliant code example

[source,python,diff-id=1,diff-type=noncompliant]
----
import tensorflow as tf

x = tf.constant([4, 6, 8])
y = tf.constant([2, 3])
z = tf.divide(x, y) # Noncompliant: InvalidArgumentError: Incompatible shapes: [3] vs. [2]
----

==== Compliant solution

[source,python,diff-id=1,diff-type=compliant]
----
import tensorflow as tf

x = tf.constant([4, 6, 8])
y = tf.constant([2, 3, 4])
z = tf.divide(x, y) # OK: [2, 2, 2]
----

//=== How does this work?

//=== Pitfalls

//=== Going the extra mile


== Resources
=== Documentation
* Tensorflow documentation - https://www.tensorflow.org/api_docs/python/tf/math/divide[tf.divide]
* Numpy documentation - https://numpy.org/doc/stable/user/basics.broadcasting.html#broadcasting[Broadcasting]
//=== Articles & blog posts
//=== Conference presentations
//=== Standards
//=== External coding guidelines
//=== Benchmarks
Loading