-
Notifications
You must be signed in to change notification settings - Fork 22
GCI107 UseTorchFromNumpy #AI #Python #DLG #Build #86
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: DataLabGroupe-CreditAgricole <[email protected]>
Co-authored-by: DataLabGroupe-CreditAgricole <[email protected]>
Co-authored-by: DataLabGroupe-CreditAgricole <[email protected]>
Co-authored-by: DataLabGroupe-CreditAgricole <[email protected]>
Co-authored-by: DataLabGroupe-CreditAgricole <[email protected]>
|
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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())) { |
Copilot
AI
Aug 27, 2025
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.
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())) { |
Copilot
AI
Aug 27, 2025
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.
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 |
Copilot
AI
Aug 27, 2025
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.
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.