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

Partial function specified through keyword on first position in tf.function #26602

Closed
vbardiovskyg opened this issue Mar 12, 2019 · 8 comments
Closed
Assignees
Labels
comp:ops OPs related issues TF 2.0 Issues relating to TensorFlow 2.0 type:bug Bug

Comments

@vbardiovskyg
Copy link
Contributor

Wrapping in tf.function a partial with first argument specified:

def f(x, y):
  return x + y

partial_func = functools.partial(f, x=5)
tf_func = tf.function(partial_func)

print(tf_func(5))

This does not work in Python2.x, because tf_inspect.getfullargspec cannot represent such construct using Argspec.

Unfortunately this also does not work in Python3, where Argspecs are already capable of representing this:

TypeError: tf__f() got multiple values for argument 'x'
@alextp
Copy link
Contributor

alextp commented Mar 12, 2019

Can we isolate this issue to misbehavior of getfullargspec instead of tf.function?

I ask because as far as tf.function is concerned nothing breaks if we just wrap the partial call into a lambda *args, **kwds: partial_call(*args, **kwds) so we should probably just do that at intake time if the user passes a functools.partial function.

@vbardiovskyg
Copy link
Contributor Author

I am not actually sure that this is a misbehavior of getfullargspec.

Currently we are using ArgSpec to be able to bind arguments to (partial or normal) function later. We want to represent partial function fully with ArgSpec, but this is not possible with Python2.x: it is not possible to have arguments with defaults before arguments without defaults.

I would consider this more an integration issue. For example, do we really need to represent partial function using getfullargspec on the inner function (the one with more arguments)? Could we somehow forget about the inner partial and work with the outer only? I haven't tried it myself.

@alextp
Copy link
Contributor

alextp commented Mar 14, 2019 via email

@jvishnuvardhan jvishnuvardhan self-assigned this Mar 25, 2019
@jvishnuvardhan jvishnuvardhan added comp:ops OPs related issues type:bug Bug labels Mar 25, 2019
@alextp alextp assigned vbardiovskyg and unassigned alextp Mar 25, 2019
@alextp
Copy link
Contributor

alextp commented Mar 25, 2019

@vbardiovskyg I think you're working on this, right?

tensorflow-copybara pushed a commit that referenced this issue Apr 10, 2019
#26602 -> this will not be fixed, because as I learned during this refactoring, it is a limitation of functools.partial.

PiperOrigin-RevId: 242826003
@vbardiovskyg
Copy link
Contributor Author

Since we are now depending on partial to do the argument binding, this becomes infeasible (i.e. we don't want to provide more functionality than partial already does).

@tensorflow-bot
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

@mmilosav
Copy link

mmilosav commented Apr 3, 2020

@vbardiovskyg Please see comments in the following code (TF 2.2.0rc2). Why does the first case succeed and second fail? Is this a bug? Thanks.

import tensorflow as tf
import functools

def f(x, y):
    return x + y

bind_x = functools.partial(f, x=1)
bind_y = functools.partial(f, y=1)

dataset = tf.data.Dataset.range(1)

print(next(iter(dataset.map(bind_y))))  # This works
print(next(iter(dataset.map(bind_x))))  # TypeError: tf__f() got multiple values for argument 'x'

@vbardiovskyg
Copy link
Contributor Author

Hi @mmilosav,

this is due to how partial works. See the following snippet using partial outside of TensorFlow context:

def f(x,y):
  print(x+y)

bind_x = functools.partial(f, x=1)

bind_x(3)  # TypeError: f() got multiple values for argument 'x'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:ops OPs related issues TF 2.0 Issues relating to TensorFlow 2.0 type:bug Bug
Projects
None yet
Development

No branches or pull requests

4 participants