Skip to content

[perf] Avoid using property for simple attributes to reduce python overhead.#3910

Merged
ailzhang merged 3 commits into
gh/ailzhang/52/basefrom
gh/ailzhang/52/head
Dec 30, 2021
Merged

[perf] Avoid using property for simple attributes to reduce python overhead.#3910
ailzhang merged 3 commits into
gh/ailzhang/52/basefrom
gh/ailzhang/52/head

Conversation

@ailzhang
Copy link
Copy Markdown
Contributor

@ailzhang ailzhang commented Dec 30, 2021

Stack from ghstack:

This is a part of cprofile result when I profile python launch
overhead for ndarray kernels. Note how dtype/n/m/shape queries takes up a
lot of time in extract_args. These are indeed caused 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}

…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
Copy link
Copy Markdown
Contributor

@qiao-bo qiao-bo left a comment

Choose a reason for hiding this comment

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

Thanks!

@qiao-bo
Copy link
Copy Markdown
Contributor

qiao-bo commented Dec 30, 2021

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 element_shape will not be improved as much as these simple ones. Lets see if property is still bottleneck after this PR ;)

Copy link
Copy Markdown
Contributor

@strongoier strongoier left a comment

Choose a reason for hiding this comment

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

LGTM!

Comment thread python/taichi/lang/_ndarray.py Outdated
Co-authored-by: Yi Xu <xy_xuyi@foxmail.com>
Copy link
Copy Markdown
Collaborator

@k-ye k-ye left a comment

Choose a reason for hiding this comment

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

Yay

@ailzhang ailzhang merged commit 7d0f241 into gh/ailzhang/52/base Dec 30, 2021
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
@ailzhang ailzhang deleted the gh/ailzhang/52/head branch January 5, 2022 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants