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

[Feature] Add Ops of StyleGAN3 #2290

Merged
merged 53 commits into from
Mar 13, 2023
Merged

Conversation

plyfager
Copy link
Contributor

@plyfager plyfager commented Sep 27, 2022

Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.

Motivation

Transfer stylegan3' ops to MMCV.

Modification

Add following ops.

  • bias_act
  • filtered_lrelu_ns
  • filtered_lrelu_rd
  • filtered_lrelu_wr
  • upfirdn2d

Sources:
https://github.com/NVlabs/stylegan3/tree/main/torch_utils/ops

Notes:

  1. The new added bias_act may cover fused_bias_leakyrelu.
  2. upfirdn2d differs a lot from the one in MMCV.

BC-breaking (Optional)

  1. The new added bias_act may cover fused_bias_leakyrelu.
  2. upfirdn2d differs a lot from the one in MMCV.

If we remove fused_bias_leakyrelu and update upfirdn2d, BC-breaking may appear.

Use cases (Optional)

If this PR introduces a new feature, it is better to list some use cases here, and update the documentation.

Checklist

Before PR:

  • I have read and followed the workflow indicated in the CONTRIBUTING.md to create this PR.
  • Pre-commit or linting tools indicated in CONTRIBUTING.md are used to fix the potential lint issues.
  • Bug fixes are covered by unit tests, the case that causes the bug should be added in the unit tests.
  • New functionalities are covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  • The documentation has been modified accordingly, including docstring or example tutorials.

After PR:

  • If the modification has potential influence on downstream or other related projects, this PR should be tested with some of those projects, like MMDet or MMCls.
  • CLA has been signed and all committers have signed the CLA in this PR.

@plyfager plyfager changed the title [Feature] Add Ops of StyleGAN3 [WIP] Add Ops of StyleGAN3 Sep 27, 2022
@zengyh1900 zengyh1900 linked an issue Oct 14, 2022 that may be closed by this pull request
@plyfager plyfager changed the title [WIP] Add Ops of StyleGAN3 [Ops] Add Ops of StyleGAN3 Oct 31, 2022
@grimoire
Copy link
Member

grimoire commented Nov 3, 2022

It is recommended to place the kernels in mmcv/ops/csrc/common/cuda so different frameworks can share the same code.

};
template <>
struct InternalType<c10::Half> {
typedef float scalar_t;
Copy link
Member

Choose a reason for hiding this comment

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

Is it expected to used float as half?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

__device__ float g_fbuf[2 * MAX_FILTER_SIZE *
MAX_FILTER_SIZE]; // Filters in global memory,
// written by setup kernel.
__device__ __constant__ float
Copy link
Member

Choose a reason for hiding this comment

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

Constant memory size is small, only 64kb. I am not sure if it is a good idea to use constant memory for this op.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This part is directly from https://github.com/NVlabs/stylegan3/blob/main/torch_utils/ops/filtered_lrelu.cu#L77. Maybe keeping it as origin is fine.

#include "pytorch_cpp_helper.hpp"
#include "pytorch_device_registry.hpp"

std::tuple<torch::Tensor, torch::Tensor, int> filtered_lrelu_op_impl(
Copy link
Member

Choose a reason for hiding this comment

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

Parrots only support return single tensor or return by args.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We may not support parrots for now.

weight_gradients_disabled = old


def conv2d(input,
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure if we should put this in mmcv.ops. It seems like an advance conv2d with no custom ops. Any advice @zhouzaida ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

He said it's ok.

mmcv/ops/upfirdn2d.py Outdated Show resolved Hide resolved
mmcv/ops/upfirdn2d.py Outdated Show resolved Hide resolved
mmcv/ops/upfirdn2d.py Outdated Show resolved Hide resolved
mmcv/ops/upfirdn2d.py Outdated Show resolved Hide resolved
mmcv/ops/upfirdn2d.py Outdated Show resolved Hide resolved
@zhouzaida zhouzaida requested a review from grimoire March 9, 2023 03:40
mmcv/ops/bias_act.py Outdated Show resolved Hide resolved
mmcv/ops/upfirdn2d.py Outdated Show resolved Hide resolved
@plyfager
Copy link
Contributor Author

Move ops out of MMEdit: open-mmlab/mmagic#1383

@zhouzaida zhouzaida changed the title [Ops] Add Ops of StyleGAN3 [Feature] Add Ops of StyleGAN3 Mar 13, 2023
@zhouzaida zhouzaida merged commit 869dbf1 into open-mmlab:2.x Mar 13, 2023
tyomj pushed a commit to tyomj/mmcv that referenced this pull request May 8, 2023
* add bias_act

* support bias_act

* support filtered_lrelu

* support filtered_lrelu and upfirdn2d

* support conv2d_gradfix and fix filtered_lrelu

* fix lint

* fix lint

* fix c++ lint

* fix part comments

* fix lint

* rm redundant header

* fix upgrade pip

* fix as comment

* fix c++ lint

* fix ci

* fix-ut

* fix as comments

* add grad check

* remove redundant template

* Update mmcv/ops/bias_act.py

Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>

* add typehint

* fix as comment:

* complete type hints

* fix lint

* add test for conv_gradfix

* add test for conv_gradfix

* fix lint

* modify licenses and ops.md

* add zh op md

* add torch version policy for conv2d_gradfix

* fix lint

* fix as comments

* rename impl

* rm redudant function and add ut

* fix as comment

* fix lint

* fix lint

* fix as comments

* fix lint

* fix ut

* fix as comment

* fix as comment

* fix as comment

---------

Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
MMCV 1.x
TO PICK
Development

Successfully merging this pull request may close these issues.

[Feature] Move Ops of StyleGAN3 to mmcv
4 participants