-
Notifications
You must be signed in to change notification settings - Fork 18.7k
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
NetSpec: don't require lists to specify single-element repeated fields #2959
NetSpec: don't require lists to specify single-element repeated fields #2959
Conversation
I did consider this interface, but decided against it. Happy to hear what others think, though.
On the other hand, this behavior mirrors the protobuf behavior (as text, but not the API), and is similar to the way |
Yeah, I understand the concerns; I think the best argument is that this mirrors the prototxt behavior and gives the |
I'd side with @jeffdonahue here. It would be a lot more convenient to be able to specify repeated fields with a scalar. If that's not possible, it would be nice to at least make the error handling a bit better. For example throw an exception the moment someone tries to assign a single value to a repeated field and not once to_proto is called. |
@philkr as I'd expect :) To be clear, what we're discussing here is type coercion. For shapes, this form of weak typing is accepted in the Net spec, in my view, need not merely wrap protobuf; it's an opportunity to write the language we most want (within the constraints of being a Python EDSL, which has its own set of issues, which are different from those of protobuf). So the argument of mirroring protobuf behavior doesn't resonate very loudly with me; if we forget protobuf entirely, what should the language of nets look like? Now I totally agree that exception handling needs dramatic improvements (PRs welcome!); that is one area that I just didn't get to on the first pass. At the very least, common errors ought to produce sensible messages. I also agree that it would be better to fail earlier, although that is a bit tricky in the current implementation, because protobuf generation is delayed as long as possible. (This is intentional, so that there is a complete non-protobuf representation of the net (suitable, the intention is, for easy processing and transformation), which means the specification doesn't meet the protobuf types until generation time.) Perhaps the types should be parsed and drawn into the Python graph IR to meet the specification, or perhaps we should just accept the protobuf IR from the beginning and generate it at once. For my own workflow, this hasn't been a huge deal, since I am never specifying a net without immediately calling |
@@ -58,6 +58,9 @@ def assign_proto(proto, name, val): | |||
type (in recursive fashion). Lists become repeated fields/messages, dicts | |||
become messages, and other types are assigned directly.""" | |||
|
|||
is_repeated_field = hasattr(getattr(proto, name), 'extend') | |||
if is_repeated_field and not isinstance(val, list): | |||
val = [val] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we note this behavior in the docstring?
Okay, to look a little closer at this, let's enumerate the cases where repeated fields are actually used in layer parameters. I count:
In addition, we'll have soon A. N-D conv parameters Of these, 6 and 7 have "broadcast" behavior, where specifying one is the same as specifying the same value
The first behavior is not maintained by either implementation, while the second and third would be implemented by this PR. Given that (1) the distinction in these behaviors is not specified by protobuf, (2) neither implementation gives possibly "ideal" behavior in all cases, (3) the unary shorthand is widely accepted in the (I'm still unclear on how A is meant to work in the 1-dimensional case, though; shouldn't The patch looks good except documentation as noted. |
Thanks for the detailed discussion @longjon! I'll add a note on the behavior to the docstring.
For 1D conv, |
Thanks for the research and argument @longjon! I agree with everything here and think this is ready to merge following the docstring update by @jeffdonahue. Merge of #2049 can follow. |
5789ee2
to
c248474
Compare
…repeated NetSpec: don't require lists to specify single-element repeated fields
This makes NetSpec slightly friendlier by allowing single-element repeated fields to be specified without lists. I like this because it's similar to how the protobuf text format works: a single-element
repeated
field can be specified just like anoptional
field, and backwards compatibility is maintained when anoptional
field is promoted to arepeated
field. (To test this I modified an unrelated existing test -- happy to create a new one instead if that's desired.)