GCI107 UseTorchFromNumpy #AI #Python #DLG #Build#86
GCI107 UseTorchFromNumpy #AI #Python #DLG #Build#86cleophass wants to merge 13 commits intogreen-code-initiative:mainfrom
Conversation
Co-authored-by: DataLabGroupe-CreditAgricole <GITHUB.DATALABGROUPE@CREDIT-AGRICOLE-SA.FR>
Co-authored-by: DataLabGroupe-CreditAgricole <GITHUB.DATALABGROUPE@CREDIT-AGRICOLE-SA.FR>
Co-authored-by: DataLabGroupe-CreditAgricole <GITHUB.DATALABGROUPE@CREDIT-AGRICOLE-SA.FR>
Co-authored-by: DataLabGroupe-CreditAgricole <GITHUB.DATALABGROUPE@CREDIT-AGRICOLE-SA.FR>
Co-authored-by: DataLabGroupe-CreditAgricole <GITHUB.DATALABGROUPE@CREDIT-AGRICOLE-SA.FR>
|
This PR has been automatically marked as stale because it has no activity for 30 days. |
| @@ -0,0 +1,102 @@ | |||
| /* | |||
There was a problem hiding this comment.
delete this class and use UtilsAST already available on "main"
|
|
||
| ### Added | ||
|
|
||
| - [#86](https://github.com/green-code-initiative/creedengo-python/pull/86) Add rule GCI107 Torch from numpy, the rule isn't finished yet |
There was a problem hiding this comment.
- use GCI109 rule id in all your PR
- please add an IT
- add a new PR with GCI109 in rules-spec projet
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a new static analysis rule GCI107 that detects inefficient patterns when creating PyTorch tensors from NumPy arrays. The rule enforces using torch.from_numpy() instead of torch.tensor() for better memory efficiency and performance.
- Implements rule GCI107 to encourage memory-efficient tensor creation from NumPy arrays
- Adds comprehensive test coverage for various import patterns and usage scenarios
- Creates utility functions to support AST analysis for the new rule
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/main/java/org/greencodeinitiative/creedengo/python/checks/UseTorchFromNumpy.java | Main rule implementation that detects torch.tensor() calls with NumPy arrays |
| src/main/java/org/greencodeinitiative/creedengo/python/checks/Utils.java | Utility class providing helper methods for AST analysis |
| src/test/java/org/greencodeinitiative/creedengo/python/checks/UseTorchFromNumpyTest.java | Unit test class for the new rule |
| src/test/resources/checks/useTorchFromNumpy.py | Test file with various scenarios for compliant and non-compliant code |
| src/main/java/org/greencodeinitiative/creedengo/python/PythonRuleRepository.java | Registers the new rule in the repository |
| CHANGELOG.md | Documents the addition of the new rule |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| private void visitCallExpression(SubscriptionContext ctx) { | ||
| var callExpression = (CallExpression) ctx.syntaxNode(); | ||
|
|
||
| if (!TORCH_TENSOR_FUNCTION.equals(Utils.getQualifiedName(callExpression)) && !TORCH_TENSOR_FUNCTION.equals(callExpression.callee().firstToken().value()+"."+callExpression.calleeSymbol().name())) { |
There was a problem hiding this comment.
The logic is inverted - this condition will return early when it should continue processing. It should use OR (||) instead of AND (&&) to check if NEITHER condition matches torch.tensor.
| if (!TORCH_TENSOR_FUNCTION.equals(Utils.getQualifiedName(callExpression)) && !TORCH_TENSOR_FUNCTION.equals(callExpression.callee().firstToken().value()+"."+callExpression.calleeSymbol().name())) { | |
| if (!TORCH_TENSOR_FUNCTION.equals(Utils.getQualifiedName(callExpression)) || !TORCH_TENSOR_FUNCTION.equals(callExpression.callee().firstToken().value()+"."+callExpression.calleeSymbol().name())) { |
| private void visitCallExpression(SubscriptionContext ctx) { | ||
| var callExpression = (CallExpression) ctx.syntaxNode(); | ||
|
|
||
| if (!TORCH_TENSOR_FUNCTION.equals(Utils.getQualifiedName(callExpression)) && !TORCH_TENSOR_FUNCTION.equals(callExpression.callee().firstToken().value()+"."+callExpression.calleeSymbol().name())) { |
There was a problem hiding this comment.
Potential NullPointerException when callExpression.calleeSymbol() returns null. The code should check for null before calling .name().
| if (!TORCH_TENSOR_FUNCTION.equals(Utils.getQualifiedName(callExpression)) && !TORCH_TENSOR_FUNCTION.equals(callExpression.callee().firstToken().value()+"."+callExpression.calleeSymbol().name())) { | |
| var calleeSymbol = callExpression.calleeSymbol(); | |
| if (!TORCH_TENSOR_FUNCTION.equals(Utils.getQualifiedName(callExpression)) && | |
| !(calleeSymbol != null && TORCH_TENSOR_FUNCTION.equals(callExpression.callee().firstToken().value() + "." + calleeSymbol.name()))) { |
|
|
||
| ### Added | ||
|
|
||
| - [#86](https://github.com/green-code-initiative/creedengo-python/pull/86) Add rule GCI107 Torch from numpy, the rule isn't finished yet |
There was a problem hiding this comment.
The changelog entry states 'the rule isn't finished yet' which appears inconsistent with a completed implementation. This should be updated to reflect the actual state.
| - [#86](https://github.com/green-code-initiative/creedengo-python/pull/86) Add rule GCI107 Torch from numpy, the rule isn't finished yet | |
| - [#86](https://github.com/green-code-initiative/creedengo-python/pull/86) Add rule GCI107 Torch from numpy. This rule checks for efficient use of Torch tensors created from NumPy arrays. |
|
This PR has been automatically marked as stale because it has no activity for 30 days. |
Verified that the rule does not exist.
Confirmed that the rule is not listed in Rules.MD, so a new ID GCI107 was created.
Added a corresponding Python unit test.
Updated the CHANGELOG accordingly.