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

[NNVM] Add ONNX upsample converter #1591

Merged
merged 7 commits into from
Aug 16, 2018
Merged

[NNVM] Add ONNX upsample converter #1591

merged 7 commits into from
Aug 16, 2018

Conversation

Jokeren
Copy link
Contributor

@Jokeren Jokeren commented Aug 13, 2018

I suppose NNVM doesn't support the bilinear (trilinear) mode of upsample operator. And I am not sure if I need to check the of dimension of the input tensor, because ONNX supports the input tensor to be arbitrary dimension while NNVM assumes the input tensor is 4D. @tqchen

@Jokeren Jokeren changed the title Add ONNX upsample converter (nearest mode) [NNVM] Add ONNX upsample converter (nearest mode) Aug 13, 2018
@srkreddy1238
Copy link
Contributor

srkreddy1238 commented Aug 13, 2018

@Jokeren

Few comments in the context of Upsampling

  • We do support bi-linear.
  • Current implementations of upsampling, resize ops are more specialized towards image formats (NCHW, NHWC).
  • Generalized scaling with independent scaling factors (resize value) can be considered to make it more generic
  • We could leave one of these two versions(upsampling) to handle layouts and other can be a layout independent operator (with scaling information to be passed for each dimension).

@tqchen, @masahi welcome to comment on this.

@Jokeren
Copy link
Contributor Author

Jokeren commented Aug 13, 2018

@srkreddy1238, Thanks for your comment!

I got a problem in finding the accurate definition of upsample in NNVM's doc.

https://docs.tvm.ai/nnvm_top.html#overview-of-operators

On the link above, I could not find instructions for upsample.

@masahi
Copy link
Member

masahi commented Aug 13, 2018

I think ONNX definition of upsampling op is too generic (arbitrary dimension/scaling factors) without good use cases. In practice, upsampling op is almost always used for image-like inputs with the same scaling factor in spatial dimensions.

@Jokeren @srkreddy1238 I think for now, supporting 4d inputs with nearest/bilinear should be enough.

@srkreddy1238
Copy link
Contributor

Great.

So we could limit the ONNX support as below for now.

  • Support up to order of 4 with a constraint of scaling factors being 1 for (N and C).
  • Support NN and Bilinear.
  • Support symmetric scale (all scale values same).

Hint: for order 3 and 2 we could handle expand dims and use existing upsampling followed by a squeeze.

@masahi
Copy link
Member

masahi commented Aug 13, 2018

Of course, if there is indeed a need for such generic upsampling op, we can add support for it later. But then we need to update our TOPI upsampling op anyway.

So for this PR, supporting @srkreddy1238's list above should be good.

@srkreddy1238
Copy link
Contributor

@Jokeren

Thanks for pointing the incomplete docs. We will work on updating.
Mean while you can refer upsample

@Jokeren
Copy link
Contributor Author

Jokeren commented Aug 13, 2018

Thanks, I will fix the problem and update features in the next few days.

@Jokeren
Copy link
Contributor Author

Jokeren commented Aug 13, 2018

@srkreddy1238 Can you take a look at the failed check? I double checked my local tests with both python3 and python2 and there's no such a problem.

File "/workspace/nnvm/python/nnvm/frontend/onnx.py", line 417, in _impl_v7
assert mode == 'nearest'

@srkreddy1238
Copy link
Contributor

ONNX version ?

@srkreddy1238
Copy link
Contributor

Also try printing attr

@masahi
Copy link
Member

masahi commented Aug 14, 2018

@Jokeren Please tell us when this is ready for review.

@Jokeren
Copy link
Contributor Author

Jokeren commented Aug 14, 2018

@masahi , Sure

@Jokeren
Copy link
Contributor Author

Jokeren commented Aug 15, 2018

Note there's a warning in a cpp file after adding the method param in upsample:

tvm/src/arithmetic/int_set.cc:514: cannot evaluate set type Cast

@Jokeren Jokeren changed the title [NNVM] Add ONNX upsample converter (nearest mode) [NNVM] Add ONNX upsample converter Aug 15, 2018
@Jokeren
Copy link
Contributor Author

Jokeren commented Aug 15, 2018

@masahi , it looks good, except for the warning I mentioned before. Could you take a look? Thank you!

in_array = np.random.uniform(size=in_shape).astype('float32')
out_array = np.zeros(out_shape).astype('float32')

def upsample_bilinear(out_shape, out_array, in_shape, in_array):
Copy link
Member

@masahi masahi Aug 15, 2018

Choose a reason for hiding this comment

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

Use function in topi/python/topi/testing/bilinear_resize_python.py instead of duplicating here. It's fine to import topi.

in_array = np.random.uniform(size=in_shape).astype('float32')
out_array = np.zeros(out_shape).astype('float32')

def upsample_nearest(out_shape, out_array, in_array):
Copy link
Member

Choose a reason for hiding this comment

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

Use function in topi/python/topi/testing/upsampling_python.py


@classmethod
def _impl_v7(cls, inputs, attr, params):
scales = attr.get('scales')
Copy link
Member

Choose a reason for hiding this comment

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

Add assert here to check

len(scales) == 4
scales[0] == 1.0 && scales[1] == 1.0
scales[2] == scales[3]

This is the only case we can support.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, we support only this case.

Should assert if it goes out of this assumption.

scale = 2
in_shape = (1, 1, 3, 3)
out_shape = (1, 1, 3 * scale, 3 * scale)
scale = float(scale)
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure the result of float(scale) is float32? The "Cast" warning you got might be due to this.

scale = 2
in_shape = (1, 1, 3, 3)
out_shape = (1, 1, 3 * scale, 3 * scale)
scale = float(scale)
Copy link
Member

Choose a reason for hiding this comment

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

The same comment as in upsample nearest. Make sure scale is floar32.

Copy link
Member

@masahi masahi left a comment

Choose a reason for hiding this comment

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

I'm not sure why you got the warning, but it should be related to type casting and not the method parameter.

@masahi
Copy link
Member

masahi commented Aug 15, 2018

@srkreddy1238 Can you give your review?


if __name__ == '__main__':
# verify_super_resolution_example()
# verify_squeezenet1_1()
# verify_lenet()
verify_resnet18()
#verify_resnet18()
Copy link
Member

Choose a reason for hiding this comment

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

why disable this?

@masahi
Copy link
Member

masahi commented Aug 16, 2018

@Jokeren Have you solved the warning issue?

@tqchen
Copy link
Member

tqchen commented Aug 16, 2018

The warning might be false alarm from integer analysis and it is fine to ignore it for now

@Jokeren
Copy link
Contributor Author

Jokeren commented Aug 16, 2018

@masahi, @tqchen , Thank you guys!

@tqchen tqchen merged commit 093dc74 into apache:master Aug 16, 2018
@tqchen
Copy link
Member

tqchen commented Aug 16, 2018

Thanks, @Jokeren , @masahi ! This is merged

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