Skip to content

Update the onnx Gemm op to FC/FCTransposed logic in caffe2 onnx backend#10108

Closed
JerryShih wants to merge 4 commits intopytorch:masterfrom
JerryShih:use-FC-for-Gemm
Closed

Update the onnx Gemm op to FC/FCTransposed logic in caffe2 onnx backend#10108
JerryShih wants to merge 4 commits intopytorch:masterfrom
JerryShih:use-FC-for-Gemm

Conversation

@JerryShih
Copy link
Contributor

The broadcast is used by default when the opset version is greater then 6.
#9874

@JerryShih
Copy link
Contributor Author

@houseroad @yinghai
How about this update?

@yinghai yinghai requested a review from houseroad August 1, 2018 04:30
@JerryShih
Copy link
Contributor Author

@houseroad
There are some shape mismatch bugs in my patch. Please skip this reviewing request.
I will update for these bugs soon.

@ezyang ezyang changed the title Use broadcast by default for Gemm op when the opset version is greater then 6. [WIP] Use broadcast by default for Gemm op when the opset version is greater then 6. Aug 1, 2018
@JerryShih JerryShih force-pushed the use-FC-for-Gemm branch 2 times, most recently from a09df51 to 26520a7 Compare August 1, 2018 21:31
@JerryShih JerryShih changed the title [WIP] Use broadcast by default for Gemm op when the opset version is greater then 6. [WIP] Update the onnx Gemm op to FC logic in caffe2 onnx backend Aug 1, 2018
@JerryShih
Copy link
Contributor Author

@houseroad
Could you please review these patch?

@JerryShih JerryShih changed the title [WIP] Update the onnx Gemm op to FC logic in caffe2 onnx backend Update the onnx Gemm op to FC logic in caffe2 onnx backend Aug 2, 2018

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

Copy link
Member

@houseroad houseroad left a comment

Choose a reason for hiding this comment

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

Overall looks pretty good.

Nit: use int instead of bool when calling make_node, since transA, transB, and broadcast are all int.

@JerryShih
Copy link
Contributor Author

@houseroad
Thank you. All boolean values are converted to int.
Wait for the CI test again.

@JerryShih
Copy link
Contributor Author

@houseroad
CI checks have passed

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

houseroad has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

This comment was marked as off-topic.

This comment was marked as off-topic.

bddppq
bddppq previously requested changes Aug 6, 2018
Copy link
Contributor

@bddppq bddppq left a comment

Choose a reason for hiding this comment

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

I think what this PR is trying to improve is to use FCTransposed when trans_b is False, this is a good idea. Could you update the pull request's title & summary accordingly?

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@houseroad
Copy link
Member

A potential follow up change (after getting this merged):

Right now, FC only accpet 1d bias, however, we can improve FC's implementation to accept 2d bias.

FC's implementation:
https://github.com/pytorch/pytorch/blob/master/caffe2/operators/fully_connected_op.h#L69

Same to FCTranspose.

When FC accepts 2d bias, we can always turn a Gemm into Scale + FC(Transpose)

@JerryShih JerryShih changed the title Update the onnx Gemm op to FC logic in caffe2 onnx backend Update the onnx Gemm op to FC/FCTransposed logic in caffe2 onnx backend Aug 6, 2018
@JerryShih
Copy link
Contributor Author

@houseroad @bddppq
I update some comments in review page.

@JerryShih
Copy link
Contributor Author

@houseroad @bddppq
What do you think for my comments? Did I miss something?

Update the _known_opset_version value as the opset_version in 'caffe2/python/onnx/backend.py'.
1. support broadcast by default if the opset version is greater than 6.
2. use FC/FCTransposed if we have broadcast and the input_c's dim
matching the FC/FCTransposed's requirement.
If we have broadcast and the input_c's dim is not 1, we still can't use
FC for this case. So, remove this wrong test.
1. The opset7 uses broadcast by default. Set the explicit opset version to test the different behavior.
2. Add the new FCTransposed op test case.
@JerryShih
Copy link
Contributor Author

@houseroad @bddppq
Request review again.

I update the condition for converting to FC.

  1. If c is "1d" tensor and c is not a scalar, use FC.
  2. If c is a scalar and b's shape is not available, skip FC.
  3. If c is a scalar and a*b's last dim is 1, use FC.
  4. If c is a scalar and a*b's last dim is not 1, skip FC.

In most case, we will use FC.

@JerryShih
Copy link
Contributor Author

@houseroad @bddppq
Any feedback?

"pr/caffe2-py2-cuda9.0-cudnn7-windows-build — Build failed"
This test always failed, but that's not related to these patches.

@houseroad
Copy link
Member

I have restarted the build, don’t worry. I will review today later.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

houseroad has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@JerryShih
Copy link
Contributor Author

@houseroad
It already has taken 3 days waiting for merging pr. Is it any test failed?

@houseroad
Copy link
Member

I am running some internal test cases. Will update it soon.

@houseroad
Copy link
Member

@JerryShih are you interested in improving FC and FCTranspose in Caffe2?

@JerryShih
Copy link
Contributor Author

yes, I will do that.

@houseroad
Copy link
Member

Awesome, if you have any question, feel free to ping on gitter.

petrex pushed a commit to petrex/pytorch that referenced this pull request Aug 21, 2018
* upstream/master: (147 commits)
  Support Loading to GPU (pytorch#10710)
  More changes for hidden visibility (pytorch#10692)
  Add arguments __repr__ in Distribution base class
  Add util function from core type to dtype (pytorch#10716)
  Make ONNX_ATEN_FALLBACK as internal default option
  Set the BUILD_ENVIRONMENT variable before installing sccache. (pytorch#10640)
  Avoid shadowing i, j vars in GeneralProposals test (pytorch#10721)
  Move THNN Reduction to ATen/core. (pytorch#10703)
  Completely remove build_aten and use_aten (pytorch#10469)
  Make empty list literals construct empty Tensor[] (pytorch#10705)
  Soumith's last few patches to v0.4.1
  Fix issues link in Caffe2 readme (pytorch#10711)
  Update the onnx Gemm op to FC/FCTransposed logic in caffe2 onnx backend (pytorch#10108)
  Adding ATEN_NO_TEST option to root level cmake for propogation to aten
  Allow method-style casts on tensors (pytorch#10641)
  Fix pytorch#10698 build failure (pytorch#10704)
  Add support for Log()
  Add a bisect percentile operator (pytorch#10563)
  Fix EnsureCPUOutputOp (pytorch#10651)
  Nomnigraph - rename some APIs that invole Subtree to Subgraph (pytorch#10551)
  ...
PenghuiCheng pushed a commit to PenghuiCheng/pytorch that referenced this pull request Sep 11, 2018
…nd (pytorch#10108)

Summary:
The broadcast is used by default when the opset version is greater then 6.
Pull Request resolved: pytorch#10108

Reviewed By: bddppq

Differential Revision: D9176934

Pulled By: houseroad

fbshipit-source-id: b737bd87b0ddc241c657d35856d1273c9950eeba
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.

6 participants