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

sync #1

Merged
merged 21 commits into from
Mar 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
aeb5c45
[Docs] Fix typo in hook document (#980)
acdart Mar 7, 2023
44f30f6
[Enhancement] Add `FUNCTIONS` root Registry (#983)
HAOCHENYE Mar 8, 2023
4585c36
[Enhancement] Save the memory to visualization backends (#974)
enkilee Mar 8, 2023
12e3d4e
Fix changelog date (#986)
HAOCHENYE Mar 8, 2023
6e58c0d
[Docs] Translate "how to set random seed" (#930)
xin-li-67 Mar 9, 2023
8beacd3
[Fix] Support calculate the flops of `matmul` with single dimension m…
HAOCHENYE Mar 9, 2023
7a074fa
[Enhancement] Silence error when `ManagerMixin` built instance with d…
HAOCHENYE Mar 10, 2023
6ea23a2
[Fix] Fix duplicated warning (#992)
HAOCHENYE Mar 10, 2023
0d25625
[Feature] Support torch.compile since PyTorch2.0 (#976)
C1rN09 Mar 12, 2023
8063d2c
[Enhancement] Support writing data to `vis_backend` with prefix (#972)
HAOCHENYE Mar 13, 2023
330985d
[Docs] Translate "Model Complexity Analysis" to Chinese (#969)
VoyagerXvoyagerx Mar 13, 2023
395ebf8
[Enhancement] Support dumping logs of different ranks (#968)
HAOCHENYE Mar 13, 2023
60872c3
fix lint (#993)
zhouzaida Mar 13, 2023
8177ef2
[Fix] Fix AMP in Ascend and support using NPUJITCompile environment (…
luomaoling Mar 13, 2023
789330e
Add citation in README (#995)
zhouzaida Mar 13, 2023
ad33a7d
[Fix] Fix inferencer gets wrong configs path (#996)
HAOCHENYE Mar 14, 2023
67ce04d
[Fix] Fix pytorch2.0 does not support sys._getframe used to remove pr…
Harold-lkk Mar 14, 2023
ef4c68d
Bump version to v0.7.0 (#999)
HAOCHENYE Mar 16, 2023
2955ddb
[Enhance] Add `mmpretrain` to the `MODULE2PACKAGE` (#1002)
mzr1996 Mar 17, 2023
ff27b72
[Enhancement] Unify the use of print_log and logger (#997)
LEFTeyex Mar 17, 2023
8280e6c
Merge branch 'feature-support-mlflow' into feature-sync
sh0622-kim Mar 18, 2023
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
63 changes: 62 additions & 1 deletion .circleci/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,57 @@ jobs:
command: |
docker exec mmengine python -m pytest tests/

build_integration_test:
parameters:
torch:
type: string
cuda:
type: string
cudnn:
type: integer
default: 7
machine:
image: ubuntu-2004-cuda-11.4:202110-01
docker_layer_caching: true
resource_class: gpu.nvidia.small
steps:
- checkout
- run:
name: Build Docker image
command: |
docker build .circleci/docker -t mmengine:gpu --build-arg PYTORCH=<< parameters.torch >> --build-arg CUDA=<< parameters.cuda >> --build-arg CUDNN=<< parameters.cudnn >>
docker run --gpus all -t -d -v /home/circleci/project:/mmengine -w /mmengine --name mmengine mmengine:gpu
- run:
name: Build MMEngine from source
command: |
docker exec mmengine pip install -e . -v
- run:
name: Install unit tests dependencies
command: |
docker exec mmengine pip install -r requirements/tests.txt
docker exec mmengine pip install openmim
docker exec mmengine mim install 'mmcv>=2.0.0rc1'
- run:
name: Install down stream repositories
command: |
docker exec mmengine mim install 'mmdet>=3.0.0rc0'
- run:
name: Run integration tests
command: |
docker exec mmengine pytest tests/test_infer/test_infer.py
- run:
name: Install down stream repositories from source
# TODO: Switch to master branch
command: |
docker exec mmengine pip uninstall mmdet -y
docker exec mmengine apt install git -y
docker exec mmengine mkdir downstream_repos
docker exec mmengine git clone -b 3.x https://github.com/open-mmlab/mmdetection.git ./downstream_repos/mmdetection
- run:
name: Run inferencer tests
command: |
docker exec -e PYTHONPATH=./downstream_repos/mmdetection mmengine pytest tests/test_infer/test_infer.py

workflows:
pr_stage_lint:
when: << pipeline.parameters.lint_only >>
Expand Down Expand Up @@ -173,10 +224,20 @@ workflows:
python: 3.9.0
requires:
- minimum_version_cpu
- hold_integration_test:
type: approval
requires:
- lint
- build_integration_test:
name: integration_test
torch: 1.8.1
cuda: "10.2"
requires:
- hold_integration_test
- hold:
type: approval
requires:
- maximum_version_cpu
- lint
- build_cuda:
name: mainstream_version_gpu
torch: 1.8.1
Expand Down
8 changes: 8 additions & 0 deletions CITATION.cff
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cff-version: 1.2.0
message: "If you use this software, please cite it as below."
authors:
- name: "MMEngine Contributors"
title: "OpenMMLab Foundational Library for Training Deep Learning Models"
date-released: 2022-09-01
url: "https://github.com/open-mmlab/mmengine"
license: Apache-2.0
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,14 @@ Major features:

## What's New

v0.6.0 was released on 2023-02-24.
v0.7.0 was released on 2023-03-16.

Highlights:

- Support `Apex` with `ApexOptimWrapper`
- Support analyzing model complexity
- Add `Lion` optimizer
- Support using environment variables in the config file
- Support PyTorch 2.0! Accelerate training by compiling models. See the tutorial [Model Compilation](https://mmengine.readthedocs.io/en/latest/common_usage/speed_up_training.html#model-compilation) for details
- Add `EarlyStoppingHook` to stop training when the metric does not improve

Read [Changelog](./docs/en/notes/changelog.md#v060-02242023) for more details.
Read [Changelog](./docs/en/notes/changelog.md#v070-03162023) for more details.

## Installation

Expand Down Expand Up @@ -308,6 +306,19 @@ runner.train()

We appreciate all contributions to improve MMEngine. Please refer to [CONTRIBUTING.md](CONTRIBUTING.md) for the contributing guideline.

## Citation

If you find this project useful in your research, please consider cite:

```
@article{mmengine2022,
title = {{MMEngine}: OpenMMLab Foundational Library for Training Deep Learning Models},
author = {MMEngine Contributors},
howpublished = {\url{https://github.com/open-mmlab/mmengine}},
year={2022}
}
```

## License

This project is released under the [Apache 2.0 license](LICENSE).
Expand Down
23 changes: 17 additions & 6 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,14 @@ MMEngine 是一个基于 PyTorch 实现的,用于训练深度学习模型的

## 最近进展

最新版本 v0.6.0 在 2023.02.24 发布。
最新版本 v0.7.0 在 2023.03.16 发布。

亮点:

- 新增 `ApexOptimWrapper` 支持 `Apex` 的混合精度训练功能
- 支持计算模型复杂度
- 新增 Lion 优化器
- 支持在配置文件使用环境变量
- 支持 PyTorch 2.0!通过编译模型实现训练加速,参考[编译模型文档](https://mmengine.readthedocs.io/en/latest/common_usage/speed_up_training.html#model-compilation)抢先体验
- 新增 `EarlyStoppingHook`,当监控的指标不再提升时,自动停止训练

如果想了解更多版本更新细节和历史信息,请阅读[更新日志](./docs/en/notes/changelog.md#v060-02242023)
如果想了解更多版本更新细节和历史信息,请阅读[更新日志](./docs/en/notes/changelog.md#v070-03162023)

## 安装

Expand Down Expand Up @@ -294,6 +292,19 @@ runner.train()

我们感谢所有的贡献者为改进和提升 MMEngine 所作出的努力。请参考[贡献指南](CONTRIBUTING_zh-CN.md)来了解参与项目贡献的相关指引。

## 引用

如果您觉得 MMEngine 对您的研究有所帮助,请考虑引用它:

```
@article{mmengine2022,
title = {{MMEngine}: OpenMMLab Foundational Library for Training Deep Learning Models},
author = {MMEngine Contributors},
howpublished = {\url{https://github.com/open-mmlab/mmengine}},
year={2022}
}
```

## 开源许可证

该项目采用 [Apache 2.0 license](LICENSE) 开源许可证。
Expand Down
150 changes: 81 additions & 69 deletions docs/en/advanced_tutorials/model_analysis.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
# Model Complexity Analysis

We provide a tool to help with the complexity analysis for the network. We borrow the idea from the implementation of [fvcore](https://github.com/facebookresearch/fvcore) to build this tool, and plan to support more custom operators in the future. Currently, it provides the interfaces to compute "parameter", "activation" and "flops" of the given model, and supports printing the related information layer-by-layer in terms of network structure or table. The analysis tool provides both operator-level and module-level flop counts simultaneously. Please refer to [Flop Count](https://github.com/facebookresearch/fvcore/blob/main/docs/flop_count.md) for implementation details of how to accurately measure the flops of one operator if interested.
We provide a tool to help with the complexity analysis for the network. We borrow the idea from the implementation of [fvcore](https://github.com/facebookresearch/fvcore) to build this tool, and plan to support more custom operators in the future. Currently, it provides the interfaces to compute "FLOPs", "Activations" and "Parameters", of the given model, and supports printing the related information layer-by-layer in terms of network structure or table. The analysis tool provides both operator-level and module-level flop counts simultaneously. Please refer to [Flop Count](https://github.com/facebookresearch/fvcore/blob/main/docs/flop_count.md) for implementation details of how to accurately measure the flops of one operator if interested.

## What's FLOPs
## Definition

Flop is not a well-defined metric in complexity analysis, we follow [detectron2](https://detectron2.readthedocs.io/en/latest/modules/fvcore.html#fvcore.nn.FlopCountAnalysis) to use one fused multiple-add as one flop.
The model complexity has three indicators, namely floating-point operations (FLOPs), activations, and parameters. Their definitions are as follows:

## What's Activation
- FLOPs

Activation is used to measure the feature quantity produced from one layer.
Floating-point operations (FLOPs) is not a clearly defined indicator. Here, we refer to the description in [detectron2](https://detectron2.readthedocs.io/en/latest/modules/fvcore.html#fvcore.nn.FlopCountAnalysis), which defines a set of multiply-accumulate operations as 1 FLOP.

For example, given the inputs with shape `inputs = torch.randn((1, 3, 10, 10))`, and one linear layer with `conv = nn.Conv2d(in_channels=3, out_channels=10, kernel_size=1)`.
- Activations

We get the `output` with shape `(1, 10, 10, 10)` after feeding the `inputs` into `conv`. The activation quantity of `output` of this `conv` layer is `1000=10*10*10`
Activation is used to measure the feature quantity produced from one layer.

Let's start with the following examples.
- Parameters

## Usage Example 1: Model built with native nn.Module
The parameter count of a model.

### Code
For example, given an input size of `inputs = torch.randn((1, 3, 10, 10))` and a convolutional layer `conv = nn.Conv2d(in_channels=3, out_channels=10, kernel_size=3)`, if the output feature map size is `(1, 10, 8, 8)`, then its FLOPs are `17280 = 10*8*8*3*3*3` (where `10*8*8` represents the output feature map size, and `3*3*3` represents the computation for each output), activations are `640 = 10*8*8`, and the parameter count is `280 = 3*10*3*3 + 10` (where `3*10*3*3` represents the size of weights, and 10 represents the size of bias).

## Usage

### Model built with native nn.Module

Build a model

```python
import torch
from torch import nn
from mmengine.analysis import get_model_complexity_info


# return a dict of analysis results, including:
# ['flops', 'flops_str', 'activations', 'activations_str', 'params', 'params_str', 'out_table', 'out_arch']

Expand All @@ -49,17 +56,9 @@ input_shape = (1, 10)
model = TestNet()

analysis_results = get_model_complexity_info(model, input_shape)

print(analysis_results['out_table'])
print(analysis_results['out_arch'])

print("Model Flops:{}".format(analysis_results['flops_str']))
print("Model Parameters:{}".format(analysis_results['params_str']))
```

### Description of Results

The return outputs is dict, which contains the following keys:
The `analysis_results` returned by `get_model_complexity_info` is a dict, which contains the following keys:

- `flops`: number of total flops, e.g., 10000, 10000
- `flops_str`: with formatted string, e.g., 1.0G, 100M
Expand All @@ -69,42 +68,44 @@ The return outputs is dict, which contains the following keys:
- `activations_str`: with formatted string, e.g., 1.0G, 100M
- `out_table`: print related information by table

```
+---------------------+----------------------+--------+--------------+
| module | #parameters or shape | #flops | #activations |
+---------------------+----------------------+--------+--------------+
| model | 0.44K | 0.4K | 40 |
| fc1 | 0.11K | 100 | 10 |
| fc1.weight | (10, 10) | | |
| fc1.bias | (10,) | | |
| fc2 | 0.11K | 100 | 10 |
| fc2.weight | (10, 10) | | |
| fc2.bias | (10,) | | |
| inner | 0.22K | 0.2K | 20 |
| inner.fc1 | 0.11K | 100 | 10 |
| inner.fc1.weight | (10, 10) | | |
| inner.fc1.bias | (10,) | | |
| inner.fc2 | 0.11K | 100 | 10 |
| inner.fc2.weight | (10, 10) | | |
| inner.fc2.bias | (10,) | | |
+---------------------+----------------------+--------+--------------+
```

- `out_arch`: print related information by network layers

```bash
TestNet(
#params: 0.44K, #flops: 0.4K, #acts: 40
(fc1): Linear(
in_features=10, out_features=10, bias=True
#params: 0.11K, #flops: 100, #acts: 10
)
(fc2): Linear(
in_features=10, out_features=10, bias=True
#params: 0.11K, #flops: 100, #acts: 10
)
(inner): InnerNet(
#params: 0.22K, #flops: 0.2K, #acts: 20
Print the results

- print related information by table

```python
print(analysis_results['out_table'])
```

```text
+---------------------+----------------------+--------+--------------+
| module | #parameters or shape | #flops | #activations |
+---------------------+----------------------+--------+--------------+
| model | 0.44K | 0.4K | 40 |
| fc1 | 0.11K | 100 | 10 |
| fc1.weight | (10, 10) | | |
| fc1.bias | (10,) | | |
| fc2 | 0.11K | 100 | 10 |
| fc2.weight | (10, 10) | | |
| fc2.bias | (10,) | | |
| inner | 0.22K | 0.2K | 20 |
| inner.fc1 | 0.11K | 100 | 10 |
| inner.fc1.weight | (10, 10) | | |
| inner.fc1.bias | (10,) | | |
| inner.fc2 | 0.11K | 100 | 10 |
| inner.fc2.weight | (10, 10) | | |
| inner.fc2.bias | (10,) | | |
+---------------------+----------------------+--------+--------------+
```

- print related information by network layers

```python
print(analysis_results['out_arch'])
```

```bash
TestNet(
#params: 0.44K, #flops: 0.4K, #acts: 40
(fc1): Linear(
in_features=10, out_features=10, bias=True
#params: 0.11K, #flops: 100, #acts: 10
Expand All @@ -113,13 +114,30 @@ TestNet(
in_features=10, out_features=10, bias=True
#params: 0.11K, #flops: 100, #acts: 10
)
(inner): InnerNet(
#params: 0.22K, #flops: 0.2K, #acts: 20
(fc1): Linear(
in_features=10, out_features=10, bias=True
#params: 0.11K, #flops: 100, #acts: 10
)
(fc2): Linear(
in_features=10, out_features=10, bias=True
#params: 0.11K, #flops: 100, #acts: 10
)
)
)
)
```
```

## Usage Example 2: Model built with mmengine
- print results with formatted string

### Code
```python
print("Model Flops:{}".format(analysis_results['flops_str']))
# Model Flops:0.4K
print("Model Parameters:{}".format(analysis_results['params_str']))
# Model Parameters:0.44K
```

### Model built with mmengine

```python
import torch.nn.functional as F
Expand Down Expand Up @@ -148,16 +166,10 @@ model = MMResNet50()

analysis_results = get_model_complexity_info(model, input_shape)


print("Model Flops:{}".format(analysis_results['flops_str']))
# Model Flops:4.145G
print("Model Parameters:{}".format(analysis_results['params_str']))
```

### Output

```bash
Model Flops:4.145G
Model Parameters:25.557M
# Model Parameters:25.557M
```

## Interface
Expand All @@ -168,4 +180,4 @@ We provide more options to support custom output
- `input_shape`: (tuple) the shape of the input, e.g., (3, 224, 224)
- `inputs`: (optional: torch.Tensor), if given, `input_shape` will be ignored
- `show_table`: (bool) whether return the statistics in the form of table, default: True
- `show_arch`: (bool) whether return the statistics in the form of table, default: True
- `show_arch`: (bool) whether return the statistics by network layers, default: True
4 changes: 3 additions & 1 deletion docs/en/advanced_tutorials/registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func_res = FUNCTION.build(func_cfg)

The registry in MMEngine supports hierarchical registration, which enables cross-project calls, meaning that modules from one project can be used in another project. Though there are other ways to implement this, the registry provides a much easier solution.

To easily make cross-library calls, MMEngine provides twenty root registries, including:
To easily make cross-library calls, MMEngine provides twenty two root registries, including:

- RUNNERS: the registry for Runner.
- RUNNER_CONSTRUCTORS: the constructors for Runner.
Expand All @@ -191,6 +191,8 @@ To easily make cross-library calls, MMEngine provides twenty root registries, in
- VISUALIZERS: the management drawing module that draws prediction boxes on images, such as `DetVisualizer`.
- VISBACKENDS: the backend for storing training logs, such as `LocalVisBackend`, and `TensorboardVisBackend`.
- LOG_PROCESSORS: controls the log statistics window and statistics methods, by default we use `LogProcessor`. You may customize `LogProcessor` if you have special needs.
- FUNCTIONS: registers various functions, such as `collate_fn` in `DataLoader`.
- INFERENCERS: registers inferencers of different tasks, such as `DetInferencer`, which is used to perform inference on the detection task.

### Use the module of the parent node

Expand Down
Loading