tensorflow: tf.function and tf.nest break for valid Mapping instances

tf.function makes invalid assumptions about arguments that are Mapping instances. In general, there are no requirements for Mapping instances to have constructors that accept [(key, value)] initializers, as assumed here.

This leads to cryptic exceptions when used with perfectly valid Mapping subclasses such as this one:

class CustomMapping(Mapping):

  def __init__(self, **kwargs):
    self.mapping = kwargs
  
  def __getitem__(self, key):
    return self.mapping[key]
  
  def __iter__(self):
    return iter(self.mapping)

  def __len__(self):
    return len(self.mapping)

See this Colab notebook for an example.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 3
  • Comments: 33 (15 by maintainers)

Commits related to this issue

Most upvoted comments

Hey @Joey155 ! PR #36186 has been opened regarding the same.

I think the error you see is expected - it’s the bug described in the OP.

@namedtoaster see the guide: https://www.tensorflow.org/install/source; you may want to skip to the Docker section for a ready-made setup.

Issue replicating in tf-nightly. Thanks.