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

Refactor: remove module not required, call function directly #5754

Merged
merged 30 commits into from Aug 8, 2021

Conversation

doombeaker
Copy link
Contributor

@doombeaker doombeaker commented Aug 5, 2021

把不必要的通过 XXXModule()() 先实例化 module 对象,再调用的方式。改为直接调用函数。

修改结果如下:

  • “是” 的表示已经去掉 Module,并且接近于直接调用 flow.F.xxx
  • “是,……” 的表示,去掉了Module,但是 python 端做了比较多的判断,可以搬运到 C++ 端。是接下来性能优化需要做的工作之一

其它没有移除的 Module 及说明在:https://github.com/Oneflow-Inc/OneTeam/issues/521

是否已经去掉 Module
class Abs(Module):
class Acos(Module):
class Acosh(Module):
class Argwhere(Module):
class Atanh(Module):
class BMM(Module):
class ZerosLike(Module):
class OnesLike(Module):
class Diag(Module):
class Eq(Module):
class Gather_nd(Module):
class Gather(Module):
class Asin(Module):
class Asinh(Module):
class Sin(Module):
class Cos(Module):
class Atan(Module):
class Log(Module):
class Subtract(Module):
class Sqrt(Module):
class Rsqrt(Module):
class Square(Module):
class Cosh(Module):
class Erf(Module):
class Ceil(Module):
class Expm1(Module):
class Negative(Module):
class Permute(Module):
class Sum(Module):
class Round(Module):
class Sign(Module):
class Sinh(Module):
class Slice(Module):
class SliceUpdate(Module):
class TypeAs(Module):
class Long(Module):
class Triu(Module):
class Exp(Module):
class Cast(Module):
class Chunk(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class Cat(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class Expand(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class Eye(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class Flip(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class Floor(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class GreaterEqual(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class Greater(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class LessEqual(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class Less(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class MaskedFill(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class MaskedSelect(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class FMod(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class Std(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class Ne(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class Reshape(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class Squeeze(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class Stack(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class Tile(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class Transpose(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class Unsqueeze(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class Where(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class Variance(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑
class Repeat(Module): 是,不过python中代码还是较重,考虑未来用C++写逻辑,貌似接口没对齐

return Eq()(input, other)

if isinstance(other, flow.Tensor) or isinstance(
other, flow._oneflow_internal.Tensor
Copy link
Contributor

Choose a reason for hiding this comment

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

可以写成if isinstance(other, (flow.Tensor,flow._oneflow_internal.Tensor)):

Copy link
Contributor Author

Choose a reason for hiding this comment

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

flow.Tensor 和 flow._oneflow_internal.Tensor 已经合二为一了。我去掉了 flow._oneflow_internal.Tensor

), "The second tensor's shape should broadcastable with the first argument."
if input.dtype != other.dtype:
other = other.to(dtype=input.dtype)
elif isinstance(other, int) or isinstance(other, float):
Copy link
Contributor

Choose a reason for hiding this comment

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

同上

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已经修改



@register_tensor_op("argwhere")
def argwhere_tebsor_op(x, dtype: Optional[flow.dtype] = None):
def argwhere_tebsor_op(input, dtype: Optional[flow.dtype] = None):
Copy link
Contributor

@Ldpe2G Ldpe2G Aug 5, 2021

Choose a reason for hiding this comment

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

这里 dtype 默认改成 flow.int32? argwhere_op 里面没处理 dtype == none 的情况

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已经修改(连带这里有个原有的拼写错误……)

return Cat(dim=dim)(inputs)
if len(inputs) == 1:
return inputs[0]
axis = dim
Copy link
Contributor

Choose a reason for hiding this comment

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

这些python代码太耗时了,安排人都迁移到c++里去?其他地方也一样(比如前面的chunk)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

嗯嗯,是打算迁移。PR描述里备注了要搬运C++的,都要搬运……

@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 6, 2021 03:32
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 6, 2021 05:42
@doombeaker doombeaker requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 6, 2021 07:23
@oneflow-ci-bot oneflow-ci-bot self-requested a review August 7, 2021 14:25
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 7, 2021 16:23
@github-actions
Copy link
Contributor

github-actions bot commented Aug 7, 2021

CI failed, removing label automerge

@github-actions github-actions bot removed the automerge label Aug 7, 2021
@oneflow-ci-bot oneflow-ci-bot removed their request for review August 7, 2021 18:22
@oneflow-ci-bot oneflow-ci-bot removed their request for review August 8, 2021 02:33
@github-actions
Copy link
Contributor

github-actions bot commented Aug 8, 2021

Speed stats:
GPU Name: GeForce GTX 1080 

PyTorch resnet50 time: 138.4ms (= 6921.0ms / 50, input_shape=[16, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 125.9ms (= 6294.6ms / 50, input_shape=[16, 3, 224, 224], backward is enabled)
Relative speed: 1.10 (= 138.4ms / 125.9ms)

PyTorch resnet50 time: 83.6ms (= 4179.9ms / 50, input_shape=[8, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 72.7ms (= 3633.4ms / 50, input_shape=[8, 3, 224, 224], backward is enabled)
Relative speed: 1.15 (= 83.6ms / 72.7ms)

PyTorch resnet50 time: 61.1ms (= 3056.5ms / 50, input_shape=[4, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 47.7ms (= 2383.5ms / 50, input_shape=[4, 3, 224, 224], backward is enabled)
Relative speed: 1.28 (= 61.1ms / 47.7ms)

PyTorch resnet50 time: 50.3ms (= 2514.0ms / 50, input_shape=[2, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 43.4ms (= 2169.6ms / 50, input_shape=[2, 3, 224, 224], backward is enabled)
Relative speed: 1.16 (= 50.3ms / 43.4ms)

PyTorch resnet50 time: 43.0ms (= 2148.3ms / 50, input_shape=[1, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 42.4ms (= 2122.1ms / 50, input_shape=[1, 3, 224, 224], backward is enabled)
Relative speed: 1.01 (= 43.0ms / 42.4ms)

@oneflow-ci-bot oneflow-ci-bot removed their request for review August 8, 2021 04:02
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 8, 2021 05:34
@oneflow-ci-bot oneflow-ci-bot self-requested a review August 8, 2021 06:32
@doombeaker doombeaker mentioned this pull request Aug 8, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Aug 8, 2021

Speed stats:
GPU Name: GeForce GTX 1080 

PyTorch resnet50 time: 138.7ms (= 6932.8ms / 50, input_shape=[16, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 125.9ms (= 6293.5ms / 50, input_shape=[16, 3, 224, 224], backward is enabled)
Relative speed: 1.10 (= 138.7ms / 125.9ms)

PyTorch resnet50 time: 83.5ms (= 4176.6ms / 50, input_shape=[8, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 73.0ms (= 3647.6ms / 50, input_shape=[8, 3, 224, 224], backward is enabled)
Relative speed: 1.15 (= 83.5ms / 73.0ms)

PyTorch resnet50 time: 55.7ms (= 2785.4ms / 50, input_shape=[4, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 47.3ms (= 2367.4ms / 50, input_shape=[4, 3, 224, 224], backward is enabled)
Relative speed: 1.18 (= 55.7ms / 47.3ms)

PyTorch resnet50 time: 46.9ms (= 2347.0ms / 50, input_shape=[2, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 37.2ms (= 1857.7ms / 50, input_shape=[2, 3, 224, 224], backward is enabled)
Relative speed: 1.26 (= 46.9ms / 37.2ms)

PyTorch resnet50 time: 42.5ms (= 2123.9ms / 50, input_shape=[1, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 36.2ms (= 1808.1ms / 50, input_shape=[1, 3, 224, 224], backward is enabled)
Relative speed: 1.17 (= 42.5ms / 36.2ms)

@oneflow-ci-bot oneflow-ci-bot merged commit fc8d535 into master Aug 8, 2021
@oneflow-ci-bot oneflow-ci-bot deleted the refactor_moudle2functional branch August 8, 2021 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants