Skip to content

Commit 2cbbe91

Browse files
authored
Merge pull request #385 from cleophass/AvoidConvBiasBeforeBatchNorm
GCI101 AvoidConvBiasBeforeBatchNorm #Python #DLG #RulesSpecifications
2 parents b757fd0 + 7f0fd9a commit 2cbbe91

File tree

5 files changed

+102
-2
lines changed

5 files changed

+102
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- [#385](https://github.com/green-code-initiative/creedengo-rules-specifications/pull/385) Added documentation for the rule : disables bias in convolutional layers preceding Batch Normalization.
1213
- [#384](https://github.com/green-code-initiative/creedengo-rules-specifications/pull/384) Add specifications for rule GCI100, this rule is specific to Python because it's based on the `PyTorch` library, a library used for Deep Learning.
1314
- [#379](https://github.com/green-code-initiative/creedengo-rules-specifications/pull/379) Add rule GCI99 Avoid CSV Format, this rule is designed for Python but it can be implemented in other languages. The rule suggests using more efficient formats like Feather or Parquet instead of CSV.
1415
- [#401](https://github.com/green-code-initiative/creedengo-rules-specifications/pull/401) Add rule GCI98 for Java. Don't catch RuntimeException. They represent a problem in the program that should be fixed, not handled

RULES.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ Some are applicable for different technologies.
7777
| GCI98 | Avoid raising Runtime Exceptions when a check is possible | You should not catch RuntimeException. They produce large objects and represent a problem in the program that should be fixed, not handled. | [Unchecked Exceptions The Controversy](https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html) (9.3. Les classes Exception, RunTimeException et Error)[https://www.jmdoudoux.fr/java/dej/chap-exceptions.htm#exceptions-3] | 🚀 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 |
7878
| GCI99 | Data: Avoid CSV Format | The Parquet format is faster to write to, lighter in weight and faster to read data from. It is suitable for use cases where there would be a lot of data I/O, especially with Cloud storage. | | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 |
7979
| GCI100 | Wrap PyTorch Inference in `torch.no_grad()` | Using a PyTorch model in evaluation mode without wrapping inference in `torch.no_grad()` leads to unnecessary gradient tracking | | 🚫 | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | 🚫 |
80+
| GCI101 | AI: Avoid Bias in Conv Layers Before Batch Norm | Disable bias in convolutional layers when it's followed by a batch norm layer | | 🚫 | 🚫 | 🚫 || 🚫 | 🚫 | 🚫 |
8081
| GCI203 | Detect unoptimized file formats | When it is possible, to use svg format image over other image format | | 🚧 | 🚀 | 🚀 || 🚀 | 🚀 | 🚫 |
8182
| GCI404 | Avoid list comprehension in iterations | Use generator comprehension instead of list comprehension in for loop declaration | | 🚫 | 🚫 | 🚫 || 🚫 | 🚫 | 🚫 |
8283
| GCI522 | Sobriety: Brightness Override | To avoid draining the battery, iOS and Android devices adapt the brightness of the screen depending on the environment light. | | 🚫 | 🚫 || 🚫 | 🚫 | 🚫 | 🚫 |
@@ -91,8 +92,6 @@ Some are applicable for different technologies.
9192
| | Edit DOM elements to make it invisible | When an element of the Document Object Model (DOM) needs to be modified by several properties, each change in style or content will generate a repaint or reflow. | [cnumr best practices (3rd edition) BP_042](https://github.com/cnumr/best-practices/blob/main/chapters/BP_042_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | 🚫 | 🚫 |
9293
| GCI535 | Use native Intl.NumberFormat to format numbers | There's no need to use a library to display formatted numbers in a recent browser. Use Intl.NumberFormat for that use case. | | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚫 |
9394

94-
95-
9695
## Rules to be reworked / measured / clarified
9796

9897
This table lists rules proposed by the community but they have to be reworked / measured / clarified before being

src/main/rules/GCI101/GCI101.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"title": "AI Disable bias in convolutional layers when it's followed by a batch norm layer",
3+
"type": "CODE_SMELL",
4+
"status": "ready",
5+
"remediation": {
6+
"func": "Constant/Issue",
7+
"constantCost": "10min"
8+
},
9+
"tags": [
10+
"creedengo",
11+
"eco-design",
12+
"performance",
13+
"memory",
14+
"ai",
15+
"convolutional"
16+
],
17+
"defaultSeverity": "Minor"
18+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
This rule is specific to Deep Learning architectures built with convolutional layers followed by Batch Normalization.
2+
3+
Using a bias term in a convolutional layer that is immediately followed by a Batch Normalization (BatchNorm) layer is redundant and unnecessary. In such cases, the bias added by the convolution is effectively canceled out during the normalization process, as BatchNorm subtracts the mean and applies its own learnable affine transformation. As a result, the bias from the convolutional layer has no practical effect on the model's output. Removing it reduces the number of parameters—improving model efficiency slightly in terms of memory usage and emissions—while maintaining or even slightly improving training accuracy.
4+
5+
== Non Compliant Code Example
6+
7+
[source,python]
8+
----
9+
nn.Sequential(
10+
nn.Conv2d(in_channels, out_channels, kernel_size, bias=True),
11+
nn.BatchNorm2d(out_channels),
12+
nn.ReLU()
13+
)
14+
----
15+
16+
In this example, a convolutional layer includes a bias term, which is unnecessary when immediately followed by a BatchNorm layer.
17+
18+
== Compliant Solution
19+
20+
[source,python]
21+
----
22+
nn.Sequential(
23+
nn.Conv2d(in_channels, out_channels, kernel_size, bias=False),
24+
nn.BatchNorm2d(out_channels),
25+
nn.ReLU()
26+
)
27+
----
28+
29+
Since `BatchNorm2d` normalizes and shifts the output using learnable parameters, the bias from the preceding convolution becomes redundant.
30+
31+
== Relevance Analysis
32+
33+
Local experiments were conducted to assess the impact of disabling bias in convolutional layers followed by BatchNorm.
34+
35+
=== Configuration
36+
37+
* Processor: Intel(R) Xeon(R) CPU 3.80GHz
38+
* RAM: 64GB
39+
* GPU: NVIDIA Quadro RTX 6000
40+
* CO₂ Emissions Measurement: https://mlco2.github.io/codecarbon/[CodeCarbon]
41+
* Framework: PyTorch
42+
43+
=== Context
44+
45+
Two models were trained under identical settings:
46+
- One with `bias=True` in convolutional layers preceding BatchNorm
47+
- One with `bias=False`
48+
49+
The following metrics were compared:
50+
- Training time per epoch
51+
- GPU memory usage
52+
- Parameter count
53+
- Training and test accuracy
54+
- CO₂ emissions per epoch
55+
56+
=== Impact Analysis
57+
58+
image::convresult.png[]
59+
60+
- **Training Time:** Nearly identical (~30 seconds/epoch) between configurations.
61+
- **Memory Usage:** lower for the "Without Bias" model in terms of reserved GPU memory.
62+
- **Training Accuracy:** We can see that there's no significant difference in training accuracy between the two models, with both converging to similar values.
63+
- **Test Accuracy:** Final accuracy remained the same for both models, confirming no negative impact.
64+
- **Parameter Count:**
65+
- With Bias: 155,850
66+
- Without Bias: 155,626
67+
This shows a real reduction in parameters.
68+
- **Emissions:** Emissions per epoch were fractionally lower without bias, due to a leaner architecture and reduced operations.
69+
70+
== Conclusion
71+
72+
Disabling bias in convolutional layers followed by BatchNorm:
73+
74+
- Reduces the parameter count
75+
- Optimizes memory and emissions
76+
- Maintains accuracy
77+
78+
== References
79+
80+
Credit : https://github.com/AghilesAzzoug/GreenPyData
81+
- https://arxiv.org/pdf/1502.03167
82+
- https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html
225 KB
Loading

0 commit comments

Comments
 (0)