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

Couple questions RE tensor stuff #3263

Open
danpovey opened this issue Apr 24, 2019 · 5 comments
Open

Couple questions RE tensor stuff #3263

danpovey opened this issue Apr 24, 2019 · 5 comments
Labels
discussion stale Stale bot on the loose

Comments

@danpovey
Copy link
Contributor

I have a couple of questions RE the Tensor stuff I'm working on for kaldi10.

It wasn't really possible to avoid the use of shared_ptr (although if we don't need
multi-threaded operation, we could of course always replace them later with something
that doesn't use atomics).

When I write code involving passing shared_ptr around, sometimes I do

    void Foo (const std::shared_ptr<Bar> &bar);

with the logic that passing a const reference to the shared_ptr should be much faster than
copying the shared_ptr (which would involve changing its ref-count, hence atomics).
Is this reasonable? @kkm000? It just looks odd to me.

Secondly, I want to augment Variable with a mechanism to (optionally) store
arbitrary configuration information. It's in case we need any of those types of
mechanisms we use in Kaldi, where, for instance, we set different amounts of
l2 on different parameters, or specify an orthonormal constraint for certain
parameters, etc. (Parameter groups, like PyTorch uses, are
super-inconvenient and also don't really scale beyond a single configurable
parameter). One possibility is a map from string to string, but it seems wrong to
have to parse strings anytime you need a configuration variable. Another
possibility is to map from string to some kind of struct that can store a bool,
an int or a string (for instance; could maybe be augmented later). Of course
we'd have to figure out how to reflect this to Python, but it should be fairly easy
since it's pretty much in the spirit of how Python works. I just wondered whether
anyone has any comments, and whether there is a standard way to do this?

@kkm000
Copy link
Contributor

kkm000 commented Apr 25, 2019

It wasn't really possible to avoid the use of shared_ptr

The main reason the warning about using shared_ptr judiciously is usually given is because it is the direct replacement of the STL ill-concieved auto_ptr. There is absolutely nothing wrong about using shared pointers where reference counting is needed.

replace them later with something that doesn't use atomics.

Atomics generally should not be expensive. AFAIK, atomic loads and saves cost nothing on Intel CPUs. Atomic increment/decrements cost more in the case of contention, but I do not think that absent contention they would add anything at all to the cost of operations on things we are usually pointing to with smart pointers, like matrix slices. I would not worry about atomic vs non atomic even for single thread access (and it's better to be correct than fast, anyway).

void Foo (const std::shared_ptr &bar);

So the idea here is that Foo() decides whether or not it wants to keep a copy of the shared pointer? (Otherwise a const Bar* would do). I see nothing wrong with that.

augment Variable with a mechanism to (optionally) store arbitrary configuration information [...] whether there is a standard way to do this?

C++17 has std::any. Maybe it's time to bite the bullet and bump the required C++ language version; all major compilers have supported it for quite a while. As a second option, there is also boost::any, which comes pretty much in a single header under a permissive license, so it can be just checked in.

@kkm000
Copy link
Contributor

kkm000 commented Apr 25, 2019

Regarding the shared_ptr by const reference, there is a good answer: https://stackoverflow.com/a/8385581/1149924

Another one summarizes Meyers and Sutter (and who comes by first name Anderi?) response as “Shortly, there is no reason to pass [shared_ptr] by value, unless the goal is to share ownership of an object.”

So it's a totally valid thing to do.

@kkm000
Copy link
Contributor

kkm000 commented May 1, 2019

There is also std::variant, also C++17. This thing allows only one of the specified types, like std::variant<string, int, float>.

@danpovey
Copy link
Contributor Author

danpovey commented May 1, 2019 via email

@stale
Copy link

stale bot commented Jun 19, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Stale bot on the loose label Jun 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion stale Stale bot on the loose
Projects
None yet
Development

No branches or pull requests

2 participants