[perf] Avoid using property for simple attributes to reduce python overhead.#3910
Merged
Conversation
…erhead. This is a part of cprofile result when I profile python launch overhead for ndarray kernels. Note how `dtype/n/n/shape` queries takes up a lot of time in extract_args. These are indeed cased by `property` which is super slow. We should just make them normal attributes to speed things up. This PR reduces the time we spent at `extract_args` from 1.6s to 1.01s in my profiled use case. Reference: https://stackoverflow.com/questions/21174590/property-speed-overhead-in-python Note that I didn't get rid of all properties here, there're still some low hanging fruit like `element_shape`. ``` /home/ailing/github/taichi/python/taichi/lang/kernel_impl.py:252(extract_arg) -> 600000 0.344 0.451 /home/ailing/github/taichi/python/taichi/lang/_ndarray.py:53(dtype) 400000 0.150 0.150 /home/ailing/github/taichi/python/taichi/lang/_ndarray.py:251(shape) 200000 0.102 0.102 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1325(n) 200000 0.086 0.086 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1329(m) 200000 0.104 0.104 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1333(shape) 600000 0.351 0.351 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1397(n) 600000 0.335 0.335 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1401(shape) 600000 0.088 0.088 /home/ailing/github/taichi/python/taichi/types/annotations.py:33(check_element_dim) 400000 0.034 0.034 /home/ailing/github/taichi/python/taichi/types/annotations.py:39(check_layout) 600000 0.064 0.064 /home/ailing/github/taichi/python/taichi/types/annotations.py:45(check_element_shape) 600000 0.061 0.061 /home/ailing/github/taichi/python/taichi/types/annotations.py:51(check_field_dim) 2300000 0.153 0.153 {built-in method builtins.isinstance} 1200000 0.076 0.076 {built-in method builtins.len} /home/ailing/github/taichi/python/taichi/lang/kernel_impl.py:300(extract) -> 600000 1.655 3.709 /home/ailing/github/taichi/python/taichi/lang/kernel_impl.py:252(extract_arg) 600000 0.053 0.053 {method 'append' of 'list' objects} ``` [ghstack-poisoned]
ailzhang
pushed a commit
that referenced
this pull request
Dec 30, 2021
…erhead. This is a part of cprofile result when I profile python launch overhead for ndarray kernels. Note how `dtype/n/n/shape` queries takes up a lot of time in extract_args. These are indeed cased by `property` which is super slow. We should just make them normal attributes to speed things up. This PR reduces the time we spent at `extract_args` from 1.6s to 1.01s in my profiled use case. Reference: https://stackoverflow.com/questions/21174590/property-speed-overhead-in-python Note that I didn't get rid of all properties here, there're still some low hanging fruit like `element_shape`. ``` /home/ailing/github/taichi/python/taichi/lang/kernel_impl.py:252(extract_arg) -> 600000 0.344 0.451 /home/ailing/github/taichi/python/taichi/lang/_ndarray.py:53(dtype) 400000 0.150 0.150 /home/ailing/github/taichi/python/taichi/lang/_ndarray.py:251(shape) 200000 0.102 0.102 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1325(n) 200000 0.086 0.086 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1329(m) 200000 0.104 0.104 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1333(shape) 600000 0.351 0.351 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1397(n) 600000 0.335 0.335 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1401(shape) 600000 0.088 0.088 /home/ailing/github/taichi/python/taichi/types/annotations.py:33(check_element_dim) 400000 0.034 0.034 /home/ailing/github/taichi/python/taichi/types/annotations.py:39(check_layout) 600000 0.064 0.064 /home/ailing/github/taichi/python/taichi/types/annotations.py:45(check_element_shape) 600000 0.061 0.061 /home/ailing/github/taichi/python/taichi/types/annotations.py:51(check_field_dim) 2300000 0.153 0.153 {built-in method builtins.isinstance} 1200000 0.076 0.076 {built-in method builtins.len} /home/ailing/github/taichi/python/taichi/lang/kernel_impl.py:300(extract) -> 600000 1.655 3.709 /home/ailing/github/taichi/python/taichi/lang/kernel_impl.py:252(extract_arg) 600000 0.053 0.053 {method 'append' of 'list' objects} ``` ghstack-source-id: 580f1ab Pull Request resolved: #3910
…e python overhead." This is a part of cprofile result when I profile python launch overhead for ndarray kernels. Note how `dtype/n/n/shape` queries takes up a lot of time in extract_args. These are indeed cased by `property` which is super slow. We should just make them normal attributes to speed things up. This PR reduces the time we spent at `extract_args` from 1.6s to 1.01s in my profiled use case. Reference: https://stackoverflow.com/questions/21174590/property-speed-overhead-in-python Note that I didn't get rid of all properties here, there're still some low hanging fruit like `element_shape`. ``` /home/ailing/github/taichi/python/taichi/lang/kernel_impl.py:252(extract_arg) -> 600000 0.344 0.451 /home/ailing/github/taichi/python/taichi/lang/_ndarray.py:53(dtype) 400000 0.150 0.150 /home/ailing/github/taichi/python/taichi/lang/_ndarray.py:251(shape) 200000 0.102 0.102 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1325(n) 200000 0.086 0.086 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1329(m) 200000 0.104 0.104 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1333(shape) 600000 0.351 0.351 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1397(n) 600000 0.335 0.335 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1401(shape) 600000 0.088 0.088 /home/ailing/github/taichi/python/taichi/types/annotations.py:33(check_element_dim) 400000 0.034 0.034 /home/ailing/github/taichi/python/taichi/types/annotations.py:39(check_layout) 600000 0.064 0.064 /home/ailing/github/taichi/python/taichi/types/annotations.py:45(check_element_shape) 600000 0.061 0.061 /home/ailing/github/taichi/python/taichi/types/annotations.py:51(check_field_dim) 2300000 0.153 0.153 {built-in method builtins.isinstance} 1200000 0.076 0.076 {built-in method builtins.len} /home/ailing/github/taichi/python/taichi/lang/kernel_impl.py:300(extract) -> 600000 1.655 3.709 /home/ailing/github/taichi/python/taichi/lang/kernel_impl.py:252(extract_arg) 600000 0.053 0.053 {method 'append' of 'list' objects} ``` [ghstack-poisoned]
ailzhang
pushed a commit
that referenced
this pull request
Dec 30, 2021
…erhead. This is a part of cprofile result when I profile python launch overhead for ndarray kernels. Note how `dtype/n/n/shape` queries takes up a lot of time in extract_args. These are indeed cased by `property` which is super slow. We should just make them normal attributes to speed things up. This PR reduces the time we spent at `extract_args` from 1.6s to 1.01s in my profiled use case. Reference: https://stackoverflow.com/questions/21174590/property-speed-overhead-in-python Note that I didn't get rid of all properties here, there're still some low hanging fruit like `element_shape`. ``` /home/ailing/github/taichi/python/taichi/lang/kernel_impl.py:252(extract_arg) -> 600000 0.344 0.451 /home/ailing/github/taichi/python/taichi/lang/_ndarray.py:53(dtype) 400000 0.150 0.150 /home/ailing/github/taichi/python/taichi/lang/_ndarray.py:251(shape) 200000 0.102 0.102 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1325(n) 200000 0.086 0.086 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1329(m) 200000 0.104 0.104 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1333(shape) 600000 0.351 0.351 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1397(n) 600000 0.335 0.335 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1401(shape) 600000 0.088 0.088 /home/ailing/github/taichi/python/taichi/types/annotations.py:33(check_element_dim) 400000 0.034 0.034 /home/ailing/github/taichi/python/taichi/types/annotations.py:39(check_layout) 600000 0.064 0.064 /home/ailing/github/taichi/python/taichi/types/annotations.py:45(check_element_shape) 600000 0.061 0.061 /home/ailing/github/taichi/python/taichi/types/annotations.py:51(check_field_dim) 2300000 0.153 0.153 {built-in method builtins.isinstance} 1200000 0.076 0.076 {built-in method builtins.len} /home/ailing/github/taichi/python/taichi/lang/kernel_impl.py:300(extract) -> 600000 1.655 3.709 /home/ailing/github/taichi/python/taichi/lang/kernel_impl.py:252(extract_arg) 600000 0.053 0.053 {method 'append' of 'list' objects} ``` ghstack-source-id: b476f31 Pull Request resolved: #3910
Contributor
|
After reading the post and some other info, it seems like when the getter/setter has some logic required, using the property decorator is preferred. Only the simple ones do not require and will have overhead. So maybe |
strongoier
approved these changes
Dec 30, 2021
Co-authored-by: Yi Xu <xy_xuyi@foxmail.com>
ailzhang
pushed a commit
that referenced
this pull request
Dec 30, 2021
…erhead. This is a part of cprofile result when I profile python launch overhead for ndarray kernels. Note how `dtype/n/n/shape` queries takes up a lot of time in extract_args. These are indeed cased by `property` which is super slow. We should just make them normal attributes to speed things up. This PR reduces the time we spent at `extract_args` from 1.6s to 1.01s in my profiled use case. Reference: https://stackoverflow.com/questions/21174590/property-speed-overhead-in-python Note that I didn't get rid of all properties here, there're still some low hanging fruit like `element_shape`. ``` /home/ailing/github/taichi/python/taichi/lang/kernel_impl.py:252(extract_arg) -> 600000 0.344 0.451 /home/ailing/github/taichi/python/taichi/lang/_ndarray.py:53(dtype) 400000 0.150 0.150 /home/ailing/github/taichi/python/taichi/lang/_ndarray.py:251(shape) 200000 0.102 0.102 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1325(n) 200000 0.086 0.086 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1329(m) 200000 0.104 0.104 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1333(shape) 600000 0.351 0.351 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1397(n) 600000 0.335 0.335 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1401(shape) 600000 0.088 0.088 /home/ailing/github/taichi/python/taichi/types/annotations.py:33(check_element_dim) 400000 0.034 0.034 /home/ailing/github/taichi/python/taichi/types/annotations.py:39(check_layout) 600000 0.064 0.064 /home/ailing/github/taichi/python/taichi/types/annotations.py:45(check_element_shape) 600000 0.061 0.061 /home/ailing/github/taichi/python/taichi/types/annotations.py:51(check_field_dim) 2300000 0.153 0.153 {built-in method builtins.isinstance} 1200000 0.076 0.076 {built-in method builtins.len} /home/ailing/github/taichi/python/taichi/lang/kernel_impl.py:300(extract) -> 600000 1.655 3.709 /home/ailing/github/taichi/python/taichi/lang/kernel_impl.py:252(extract_arg) 600000 0.053 0.053 {method 'append' of 'list' objects} ``` ghstack-source-id: b476f31 Pull Request resolved: #3910
quadpixels
pushed a commit
to quadpixels/taichi
that referenced
this pull request
Jan 5, 2022
…erhead. This is a part of cprofile result when I profile python launch overhead for ndarray kernels. Note how `dtype/n/n/shape` queries takes up a lot of time in extract_args. These are indeed cased by `property` which is super slow. We should just make them normal attributes to speed things up. This PR reduces the time we spent at `extract_args` from 1.6s to 1.01s in my profiled use case. Reference: https://stackoverflow.com/questions/21174590/property-speed-overhead-in-python Note that I didn't get rid of all properties here, there're still some low hanging fruit like `element_shape`. ``` /home/ailing/github/taichi/python/taichi/lang/kernel_impl.py:252(extract_arg) -> 600000 0.344 0.451 /home/ailing/github/taichi/python/taichi/lang/_ndarray.py:53(dtype) 400000 0.150 0.150 /home/ailing/github/taichi/python/taichi/lang/_ndarray.py:251(shape) 200000 0.102 0.102 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1325(n) 200000 0.086 0.086 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1329(m) 200000 0.104 0.104 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1333(shape) 600000 0.351 0.351 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1397(n) 600000 0.335 0.335 /home/ailing/github/taichi/python/taichi/lang/matrix.py:1401(shape) 600000 0.088 0.088 /home/ailing/github/taichi/python/taichi/types/annotations.py:33(check_element_dim) 400000 0.034 0.034 /home/ailing/github/taichi/python/taichi/types/annotations.py:39(check_layout) 600000 0.064 0.064 /home/ailing/github/taichi/python/taichi/types/annotations.py:45(check_element_shape) 600000 0.061 0.061 /home/ailing/github/taichi/python/taichi/types/annotations.py:51(check_field_dim) 2300000 0.153 0.153 {built-in method builtins.isinstance} 1200000 0.076 0.076 {built-in method builtins.len} /home/ailing/github/taichi/python/taichi/lang/kernel_impl.py:300(extract) -> 600000 1.655 3.709 /home/ailing/github/taichi/python/taichi/lang/kernel_impl.py:252(extract_arg) 600000 0.053 0.053 {method 'append' of 'list' objects} ``` ghstack-source-id: b476f31 Pull Request resolved: taichi-dev#3910
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack from ghstack:
This is a part of cprofile result when I profile python launch
overhead for ndarray kernels. Note how
dtype/n/m/shapequeries takes up alot of time in extract_args. These are indeed caused by
propertywhichis super slow. We should just make them normal attributes to speed
things up.
This PR reduces the time we spent at
extract_argsfrom 1.6s to 1.01sin my profiled use case.
Reference:
https://stackoverflow.com/questions/21174590/property-speed-overhead-in-python
Note that I didn't get rid of all properties here, there're still some
low hanging fruit like
element_shape.