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

Autograph decorated functions with tf.map_fn #39541

Merged
merged 10 commits into from May 18, 2020

Conversation

bhack
Copy link
Contributor

@bhack bhack commented May 14, 2020

This is a test to verify the failure of the case described in #26091 (comment)

/cc @dthkao

@google-ml-butler google-ml-butler bot added the size:S CL Change Size: Small label May 14, 2020
mdanatg
mdanatg previously approved these changes May 14, 2020
@google-ml-butler google-ml-butler bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels May 14, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label May 14, 2020
@bhack
Copy link
Contributor Author

bhack commented May 14, 2020

Mhh.. why test Is passing?

@mdanatg
Copy link

mdanatg commented May 14, 2020

I think the issue was fixed a while ago (either in 2.0 or in newer versions, not sure), but the issue ID was never tagged by the fix. I definitely expect the test to pass. Have you seen recent cases when it doesn't work?

@bhack
Copy link
Contributor Author

bhack commented May 14, 2020

We had problems yesterday with 2.2.0 see tensorflow/addons#1830 (comment) and the colab in that ticket.

@bhack
Copy link
Contributor Author

bhack commented May 14, 2020

And also with tf-nightly

@mdanatg
Copy link

mdanatg commented May 14, 2020

I see. Do you have a small reproducer in a colab, using @tf.function as decorator? Something simple like this test would do. It might be caused by keyword arguments, if I read through the comments correctly.

@gbaned gbaned self-assigned this May 14, 2020
@gbaned gbaned added this to Assigned Reviewer in PR Queue via automation May 14, 2020
@bhack
Copy link
Contributor Author

bhack commented May 14, 2020

def test_function(x):
    cond = tf.constant(-1)

    if cond == 0:
        result = x
    else:
        result = x

    return result

@tf.function
def partial_call(x):
    fn = partial(test_function)
    tf.map_fn(fn, x)
X = tf.random.uniform(shape=(10, 224, 224, 3))
partial_call(X)

@mdanatg
Copy link

mdanatg commented May 14, 2020

I don't think that's caused by partial. The problem is that map_fn doesn't call autograph (see below). That's definitely a bug - feel free to file an issue, and if you're interested to help fixing it, I can give a few pointers as well.

A workaround is to wrap the target of map_fn in tf.function:

@tf.function  # Fails without this, but shouldn't.
def test_function(x):
    cond = tf.constant(-1)

    if cond == 0:
        result = x
    else:
        result = x

    return result

@tf.function
def partial_call(x):
    tf.map_fn(test_function, x)

X = tf.random.uniform(shape=(10, 224, 224, 3))
partial_call(X)

PR Queue automation moved this from Assigned Reviewer to Reviewer Requested Changes May 14, 2020
@google-ml-butler google-ml-butler bot removed the ready to pull PR ready for merge process label May 14, 2020
Copy link

@mdanatg mdanatg left a comment

Choose a reason for hiding this comment

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

Let's put this test in https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/kernel_tests/map_fn_test.py, since the fix needs to be applied to map_fn.

@bhack
Copy link
Contributor Author

bhack commented May 14, 2020

@mdanatg I was posting the same if you see the last commit 😸
In the referenced PR yesterday we have solved with the same workaround.
We could try to solve but do you think that it could be a frequent enough case to fix it?

@bhack bhack changed the title Add a decorated function test that call partial Add a decorated function test that call tf.map_fn May 14, 2020
@mdanatg
Copy link

mdanatg commented May 14, 2020

It's probably not that frequent, since the functions passed to map_fn are usually simple and don't use control flow. I think it's useful to fix though.

@bhack
Copy link
Contributor Author

bhack commented May 15, 2020

I don't think that we could just use result_value = autograph.tf_convert(fn(elems_value), autograph_ctx.control_status_ctx())

Cause we get:
TypeError: Could not build a TypeSpec for <function convert.<locals>.decorator.<locals>.wrapper at 0x7f651f655e18> with type function

@mdanatg
Copy link

mdanatg commented May 15, 2020

I don't think that we could just use result_value = autograph.tf_convert(fn(elems_value), autograph_ctx.control_status_ctx())

Cause we get:
TypeError: Could not build a TypeSpec for <function convert.<locals>.decorator.<locals>.wrapper at 0x7f651f655e18> with type function

Correct, tf_convert doesn't call the function, it just translates it.

So we need something like:

autographed_fn = autograph.tf_convert(fn, autograph_ctx.control_status_ctx())
result_value = autographed_fn(elems_value)

@bhack
Copy link
Contributor Author

bhack commented May 15, 2020

Sorry I needed to restartthe container after a system upgrade and I got:

INFO: Deleting stale sandbox base /root/.cache/bazel/_bazel_root/68a62076e91007a7908bc42a32e4cff9/sandbox

So I will need again hours of recompiling (see tensorflow/build#5 and #39560) 😞

Can you use the Google remote or your local cache to test this a push directly here?

@mdanatg
Copy link

mdanatg commented May 15, 2020

I can trigger a build which should finish faster.

@bhack
Copy link
Contributor Author

bhack commented May 15, 2020

Ok I've done a blind commit. If you can start a CI run.

tensorflow/python/ops/map_fn.py Outdated Show resolved Hide resolved
@mdanatg mdanatg added the kokoro:force-run Tests on submitted change label May 15, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label May 15, 2020
@bhack bhack requested a review from mdanatg May 15, 2020 16:22
@mdanatg mdanatg added the kokoro:force-run Tests on submitted change label May 15, 2020
@bhack
Copy link
Contributor Author

bhack commented May 15, 2020

Kokoro we invoke you こころ 🙏 🍖

@bhack
Copy link
Contributor Author

bhack commented May 15, 2020

I don't know if you can disclose this but is the Kokoro waiting list only related to the public queue visible with kokoro:force-run Tests on submitted change or is there an hidden internal one?

PR Queue automation moved this from Reviewer Requested Changes to Approved by Reviewer May 15, 2020
@google-ml-butler google-ml-butler bot added the ready to pull PR ready for merge process label May 15, 2020
@mdanatg
Copy link

mdanatg commented May 15, 2020

I'm not sure, actually. Usually it picks up immediately.

@mdanatg mdanatg added kokoro:force-run Tests on submitted change and removed kokoro:force-run Tests on submitted change labels May 15, 2020
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label May 15, 2020
@bhack
Copy link
Contributor Author

bhack commented May 15, 2020

import/copybara Pending — Waiting for internal safe review approval is it a manual approval pass right?

@mdanatg
Copy link

mdanatg commented May 15, 2020

Yep - looks like the tests passed! Consider updating the PR title.

@bhack bhack changed the title Add a decorated function test that call tf.map_fn Autograph decorated functions with tf.map_fn May 15, 2020
@bhack
Copy link
Contributor Author

bhack commented May 15, 2020

Yep - looks like the tests passed! Consider updating the PR title.

I hope that the title is clear now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes ready to pull PR ready for merge process size:S CL Change Size: Small
Projects
PR Queue
  
Approved by Reviewer
Development

Successfully merging this pull request may close these issues.

None yet

6 participants