Skip to content

constexpr type ids#26502

Closed
smessmer wants to merge 29 commits intogh/smessmer/52/basefrom
gh/smessmer/52/head
Closed

constexpr type ids#26502
smessmer wants to merge 29 commits intogh/smessmer/52/basefrom
gh/smessmer/52/head

Conversation

@smessmer
Copy link
Contributor

@smessmer smessmer commented Sep 19, 2019

Stack from ghstack:

Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of PRETTY_FUNCTION in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: D17488861

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
@pytorchbot pytorchbot added the module: internals Related to internal abstractions in c10 and ATen label Sep 19, 2019
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
@smessmer smessmer mentioned this pull request Sep 20, 2019
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
@smessmer smessmer mentioned this pull request Sep 22, 2019
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
Copy link
Collaborator

@dzhulgakov dzhulgakov left a comment

Choose a reason for hiding this comment

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

This is fancy :) Is there a measurable impact for compile time? (I'm a bit scared of too many crc compiler computation)

@smessmer
Copy link
Contributor Author

I did not measure but template instantiation rules should make sure that it's only computed once per type. The C++14 computation is an iterative loop and should be very fast, crc is designed to be fast and is a single pass over the data. The C++11 crc computation is recursive, so it might in theory be a little bit slower, but probably also negligible. Also, it's tail recursive, so the compiler might even be able to optimize it into an iterative loop.

@ezyang
Copy link
Contributor

ezyang commented Sep 25, 2019

Please measure the compile time impact before and after. You can do it on a single stressful file, if you like.

@ezyang
Copy link
Contributor

ezyang commented Sep 25, 2019

What happens if the crc collides?

Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
@smessmer
Copy link
Contributor Author

@ezyang if crc collides, we will give the same id to two types, which would be an issue. But it's crc64. This is very unlikely to collide, there is some more info here.

If you think collisions are too likely, I could add a runtime mechanism to check for them (i.e. a runtime type registry that remembers type names and corresponding ids), but I think that's probably not worth it as long as we only need it for collision checking.

I don't expect any compile time issues, but I'll do some measurements.

Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
@smessmer
Copy link
Contributor Author

smessmer commented Oct 14, 2019

I did some compile time measurements.

Setup

  • oss build with GCC8 on a devvm in a Python 2.7 virtualenv.
  • Command: python setup.py clean && git clean -xfd && time python setup.py install

Summary

  • This PR introduces a build size regression of less than 0.2% (20sec in a 210min build), this is probably within build flakiness.

Without this PR

Average user build time over both runs: 210m19s

real	11m5.594s
user	210m1.055s
sys	11m11.972s
real	10m56.386s
user	210m37.528s
sys	10m45.378s

With this PR

Average user build time over both runs: 210m42s

real	11m16.041s
user	211m6.071s
sys	11m17.403s
real	11m13.680s
user	210m18.007s
sys	10m46.595s

Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
smessmer added a commit that referenced this pull request Oct 14, 2019
Pull Request resolved: #26502

Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of `__PRETTY_FUNCTION__` in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.


ghstack-source-id: 91872668

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)
@smessmer smessmer requested a review from dzhulgakov October 14, 2019 19:28

template <typename T>
inline C10_HOST_CONSTEXPR uint64_t type_index_impl() noexcept {
// Idea: __PRETTY_FUNCTION__ (or __FUNCSIG__ on msvc) contains a qualified name
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure how I feel about this lol

Copy link
Contributor Author

@smessmer smessmer Oct 14, 2019

Choose a reason for hiding this comment

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

It's more reliable, portable and easier to understand/change than the ODR hacks we had before.

@ezyang
Copy link
Contributor

ezyang commented Oct 14, 2019

I'm not sure how I feel about this... I guess I'd feel better about this if we had some way (even if it's a runtime, test only check) to see if there any conflicts for any of the type ids we use inside our application...

@smessmer
Copy link
Contributor Author

smessmer commented Oct 14, 2019

@ezyang there's tests in c10/test/util/TypeIndex_test.cpp. They show there aren't conflicts for a few common types.

Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of __PRETTY_FUNCTION__ in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

Differential Revision: [D17488861](https://our.internmc.facebook.com/intern/diff/D17488861/)

[ghstack-poisoned]
@smessmer smessmer requested a review from ezyang October 15, 2019 01:16
@facebook-github-bot
Copy link
Contributor

This pull request has been merged in 6f865c1.

@facebook-github-bot facebook-github-bot deleted the gh/smessmer/52/head branch October 28, 2019 22:19
thiagocrepaldi pushed a commit to thiagocrepaldi/pytorch that referenced this pull request Feb 4, 2020
Summary:
Pull Request resolved: pytorch#26502

Create type ids at compile time instead of incrementing a counter at runtime. This is done by computing a compile time crc64 on the type name. We couldn't do this before, because we still used GCC4 and that compiler didn't support the use of `__PRETTY_FUNCTION__` in a constexpr context. However, since GCC5 this is possible and we can use this trick.

This does not change the semantics of preallocated type ids. I actually think we don't need to preallocate anymore, but I split the removal of preallocation into a separate diff to be able to test it separately.

ghstack-source-id: 91896920

Test Plan: unit tests

Differential Revision: D17488861

fbshipit-source-id: ce7b059d7c8686b69cb091a4a8beaf4b96391343
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

caffe2 Merged module: internals Related to internal abstractions in c10 and ATen

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants