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

Fix the TF tutorial to run against TF2.0 and TF1.x #4104

Merged
merged 15 commits into from
Nov 12, 2019
Merged

Conversation

ic
Copy link
Contributor

@ic ic commented Oct 11, 2019

TF 2.0 is now the stable release and breaks a few operations (different names, attributes, namespaces, etc). TF 1.x still evolves a little bit (1.15 coming), and the user community is likely to expect 1.x to still be supported.

This PR fixes the TF tutorial, as well as the TF frontend to Relay, so as to address #4101 . It is also the first part in supporting TF2.x completely.

Note for CR:

  • Individual commits contain errors and wrong contents, fixed in the end result. I hope we can squash the whole thing at merge time to hide my hesitation and early misunderstanding of the code base.
  • There is no new test. I do not see test coverage on the kind of change made here (basically in tests/python/frontend/tensorflow/test_forward.py around line 1103), so I guess it is more valuable for the community to get a fixed tutorial sooner. I am looking at how to test such change effectively, though, so may push something before the CR is completed (or require to complete).
  • For some reason the linter does not work for me. I am trying different container versions, but no avail on Darwin for now.

The following is the original contents for this PR, but way too broad. The target to support TF2.0 is tracked with issue #4102

This PR is WIP (read "not working yet"), as suggested by @srkreddy1238 to get started with TF 2 support

TF 2.0 is now the stable release and breaks a few operations (different names, attributes, namespaces, etc). TF 1.x still evolves a little bit (1.15 coming), and the user community is likely to expect 1.x to still be supported.

Here is a very early attempt at supporting TF 2.0, still compatible with TF 1.x, based on a first target: Get the TF tutorial back on track. There will be more to do.

  • Get the tutorial to run with TF 2.0.
  • [] Support operations at the intersection of TF 2.0 and TVM 0.6 (i.e. no new operation from TF yet).
  • [] Add test coverage against new APIs that have to be supported (e.g. resize has a new attribute, and no choice but to add it).

@ic ic changed the title Support for TF 2.0, backward compatible with Tf 1.x [WIP] Support for TF 2.0, backward compatible with Tf 1.x Oct 11, 2019
ic added 7 commits October 11, 2019 22:22
TF2.0 adds a `half_pixel_centers` attribute to the `resize` function in
the image API. This commit completes the hooks in Relay's TF frontend.

At the point of this commit, no new test yet. Also, this commit
addresses solely the `resize` change. Other commits address other
changes in TF2.0.
This looks cleaner than trying to detect the TF version.
This is a direct change, relying on the compat API provided by the TF
team.

This code will last as long as the compat API exists, so a
"proper" support for TF1.x and 2.x will require more work in some
future.
Explicit padding is a special case in TF2.0 (see reference linked
below). Some models are serialized with that mode, and break TF support
in TVM.

Support is *partial* as EXPLICIT falls back to set padding on the
Relay op, which only supports 2 values. At some point, padding may need
to be extended to support 4 values, but that is out of scope of this
support commit.

Reference on EXPLICIT padding: tensorflow/tensorflow@ec81825#diff-1d1c0bb0a880f85b6164f71dbb2f446e
The `half_pixel_centers` attribute is a new feature in TF2.0. Earlier
commits of mine mistakenly introduce them in the Relay API. This is
probably not what Relay is expected to support, and the semantics of
`half_pixel_centers` is unclear (to me, at least) at this point.
@ic ic changed the title [WIP] Support for TF 2.0, backward compatible with Tf 1.x Fix the TF tutorial to run against TF2.0 and TF1.x Oct 18, 2019
@ic
Copy link
Contributor Author

ic commented Oct 22, 2019

@srkreddy1238 Not sure who to ask for reviews. Would you guide me on this one?

@yzhliu
Copy link
Member

yzhliu commented Oct 22, 2019

@yongwww @soiferj Could you also help to take a look?

Copy link
Contributor

@jwfromm jwfromm left a comment

Choose a reason for hiding this comment

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

Left one comment but otherwise LGTM.

python/tvm/relay/frontend/tensorflow.py Outdated Show resolved Hide resolved
return AttrCvt(op_name="resize",
ignores=['Tdim'],
ignores=['Tdim', 'half_pixel_centers'],
Copy link
Member

Choose a reason for hiding this comment

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

it would be nice if add half_pixel_centers in tvm resize op. If the result is different from tensorflow, we might need to raise error instead of ignoring

Copy link
Contributor

Choose a reason for hiding this comment

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

I think half_pixel_centers is the new name of align_corners (used in TF 1.X) that we do support in tvm resize.

Copy link
Member

Choose a reason for hiding this comment

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

then add transforms={'half_pixel_centers':'align_corners'} here should be working

Copy link
Contributor Author

@ic ic Oct 25, 2019

Choose a reason for hiding this comment

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

It seems the two attributes serve different purposes. The doc says nothing really. The source code does half_pixel_centers processing is applied optionally on top of align_corners processing. E.g.

Eigen::Index in_y = std::min(
            (align_corners)
                ? static_cast<Eigen::Index>(roundf(scaler(y, height_scale)))
                : static_cast<Eigen::Index>(floorf(scaler(y, height_scale))),
            in_height - 1);
if (half_pixel_centers) {
  in_y = std::max(static_cast<Eigen::Index>(0), in_y);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Idea: How about a unit test that compares a TF model using that attribute to Relay ignoring the attribute? I was reading the test suite to add to this PR, when I saw little coverage on such kind of issue, and fixing the tutorial seemed good to release early.

I'll try a test today, but if you have some guidance/advice/preference, I'd be happy to hear it. It may just be a different PR.

@tqchen
Copy link
Member

tqchen commented Oct 24, 2019

Copy link
Member

@yongwww yongwww left a comment

Choose a reason for hiding this comment

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

left several comments, lgtm

python/tvm/relay/frontend/tensorflow.py Outdated Show resolved Hide resolved
@ic
Copy link
Contributor Author

ic commented Oct 31, 2019

Note: At present tests fail, but I cannot say why (they seem unrelated to changes here). The same errors happen on master. It may be my environment, but not fully sure yet. I'll trying, and if someone finds something that would be great.

@yzhliu
Copy link
Member

yzhliu commented Nov 8, 2019

@ic could you retrigger the CI by push a trivial commit?

@ic
Copy link
Contributor Author

ic commented Nov 12, 2019

@yzhliu Sorry for my late reaction. Just syncing with master will do it!

@yzhliu yzhliu merged commit e541c75 into apache:master Nov 12, 2019
@yzhliu
Copy link
Member

yzhliu commented Nov 12, 2019

Thanks @ic @yongwww @tqchen

zxy844288792 pushed a commit to neo-ai/tvm that referenced this pull request Nov 13, 2019
* WIP Run the TF tutorial on TF2

* Remove debugger statement.

* Complete the support for TF2.0's `resize`.

TF2.0 adds a `half_pixel_centers` attribute to the `resize` function in
the image API. This commit completes the hooks in Relay's TF frontend.

At the point of this commit, no new test yet. Also, this commit
addresses solely the `resize` change. Other commits address other
changes in TF2.0.

* Support TF2.0 in the tutorial by using the compat API.

This looks cleaner than trying to detect the TF version.

* Use the TF compat API, so as to support TF2.0.

This is a direct change, relying on the compat API provided by the TF
team.

This code will last as long as the compat API exists, so a
"proper" support for TF1.x and 2.x will require more work in some
future.

* Partial support for EXPLICIT padding introduced in TF2.0.

Explicit padding is a special case in TF2.0 (see reference linked
below). Some models are serialized with that mode, and break TF support
in TVM.

Support is *partial* as EXPLICIT falls back to set padding on the
Relay op, which only supports 2 values. At some point, padding may need
to be extended to support 4 values, but that is out of scope of this
support commit.

Reference on EXPLICIT padding: tensorflow/tensorflow@ec81825#diff-1d1c0bb0a880f85b6164f71dbb2f446e

* Guard on checking for optional TF2.0 attribute.

* Do not expect Relay to implement TF-specific attributes.

The `half_pixel_centers` attribute is a new feature in TF2.0. Earlier
commits of mine mistakenly introduce them in the Relay API. This is
probably not what Relay is expected to support, and the semantics of
`half_pixel_centers` is unclear (to me, at least) at this point.

* Remove unclear comment.

CR apache#4104 (comment)

Addresses apache#4104

* Changes after review.

Complying without understanding the rationale for now.

* Fix the arguments set mistakenly.

An argument ignored for the wrong operation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants