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

Add NNPA level compatibility check #2533

Merged
merged 11 commits into from
Oct 3, 2023
Merged

Conversation

mikeessen
Copy link
Collaborator

Add NNPA level compatibility check to validate operations for the specified --mcpu value.

Signed-off-by: Mike Essenmacher <[email protected]>
Signed-off-by: Mike Essenmacher <[email protected]>
Signed-off-by: Mike Essenmacher <[email protected]>
Signed-off-by: Mike Essenmacher <[email protected]>
Signed-off-by: Mike Essenmacher <[email protected]>
Copy link
Collaborator

@tungld tungld left a comment

Choose a reason for hiding this comment

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

@mikeessen thanks for the patch!

I see we have another locations that need isCompatibleWithNNPALevel, those are inside addDynamicallyLegalOpFor functions in https://github.com/onnx/onnx-mlir/blob/main/src/Accelerators/NNPA/Conversion/ONNXToZHigh/RewriteONNXForZHigh.cpp.

// The NNPA levels.
static constexpr const char *NNPA_Z13 = "z13";
static constexpr const char *NNPA_Z14 = "z14";
static constexpr const char *NNPA_Z15 = "z15";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we use NNPA_Z{13, 14, 15} in practice?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, I removed the definitions for NNPA_Z{13, 14, 15}

/// A function to check whether the input NNPA level, ie. "z16", is compatible
/// with the current NNPA level.
bool isCompatibleWithNNPALevel(std::string inputNNPALevel) {
if (convertNNPALevel(inputNNPALevel) <= convertNNPALevel(mcpu)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

For brevity, we can use return convertNNPALevel(inputNNPALevel) <= convertNNPALevel(mcpu);

BTW, a corner case, if both convertNNPALevel(inputNNPALevel) and convertNNPALevel(cpu) return 0 (because of some failure), this condition is true that is not what we want. Perhaps, you could check if convertNNPALevel succeeded or not before doing comparison.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, I updated for the corner case and also brevity.

// Check NNPA level.
if (!isCompatibleWithNNPALevel(NNPA_Z16)) {
return false;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks redundant. Just returning false for unsupported ops is enough.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, I updated to just return false.

// Check NNPA level.
if (!isCompatibleWithNNPALevel(NNPA_Z16)) {
return false;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you please remove { and } for this simple if? Same for other locations.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@tungld unless it's a standard in onnx-mlir to drop the {}'s for one line if statements, I lean towards keeping them. Functionally nothing changes but the braces are less error prone during maintenance or debugging scenarios.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, good to know. I defer to the established norm for the repo on such things.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, updated to remove { } from these statements.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The only places I think it's ok to have extra {} is in the presence of nested ifs, as dangling else are dangerous. Otherwise, best to keep to the LLVM formatting rules

Signed-off-by: Mike Essenmacher <[email protected]>
@mikeessen
Copy link
Collaborator Author

@tungld I updated the addDynamicallyLegalOpFor functions in https://github.com/onnx/onnx-mlir/blob/main/src/Accelerators/NNPA/Conversion/ONNXToZHigh/RewriteONNXForZHigh.cpp with isCompatibleWithNNPALevel. Please take a look.

@@ -499,6 +499,10 @@ void getRewriteONNXForZHighDynamicallyLegal(
// broadcasting.
addDynamicallyLegalOpFor<ONNXAddOp>(
target, dimAnalysis, [](ONNXAddOp op, const DimAnalysis *dimAnalysis) {
// Check NNPA level.
if (!isCompatibleWithNNPALevel(NNPA_Z16)) {
return false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

@mikeessen I am sorry that the logic here is a bit confusing. false means the operation is illegal and must be rewritten to run on NNPA, while true means the operation is legal and will remain on CPU. So I think we should return true. Same for the other changes in this file.

It's contrary to the logic in isSuitableForZDNN. Actually it's like calling ! isSuitableForZDNN().

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the clarification. I updated false to true.

Signed-off-by: Mike Essenmacher <[email protected]>
Copy link
Collaborator

@tungld tungld left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for the patch!

mikeessen and others added 3 commits October 3, 2023 09:34
@mikeessen mikeessen merged commit 7592edf into onnx:main Oct 3, 2023
8 checks passed
@jenkins-droid
Copy link
Collaborator

Jenkins Linux ppc64le Build #11900 [push] Add NNPA level compatibi... started at 11:50

@jenkins-droid
Copy link
Collaborator

Jenkins Linux amd64 Build #12883 [push] Add NNPA level compatibi... started at 10:42

@jenkins-droid
Copy link
Collaborator

Jenkins Linux s390x Build #12907 [push] Add NNPA level compatibi... started at 11:42

@jenkins-droid
Copy link
Collaborator

Jenkins Linux amd64 Build #12883 [push] Add NNPA level compatibi... passed after 1 hr 16 min

@jenkins-droid
Copy link
Collaborator

Jenkins Linux s390x Build #12907 [push] Add NNPA level compatibi... passed after 1 hr 21 min

@jenkins-droid
Copy link
Collaborator

Jenkins Linux ppc64le Build #11900 [push] Add NNPA level compatibi... passed after 1 hr 46 min

@AlexandreEichenberger
Copy link
Collaborator

@mikeessen Can you confirm: we now need the -mcpu=z16 in order to have -maccel=NNPA take effect, or does the -maccel=NNPA grandfather the -mcpu=z16 option.

@mikeessen
Copy link
Collaborator Author

@AlexandreEichenberger Yes, mcpu z16 is required for maccel NNPA to take effect.

@cjvolzka
Copy link
Collaborator

cjvolzka commented Oct 4, 2023

we now need the -mcpu=z16 in order to have -maccel=NNPA take effect,

This is my expectation. Somewhere llvm has to be using something as a default mcpu when one isn't specified. Something makes me think that's z13 but I might be off. Whatever that default is, it's not z16. So an unspecified mcpu means "use z13 (or whatever the llvm default is) instructions". NNPA instructions won't be available for anything but the latest processor.

So before this change, specifying only -maccel=NNPA, we would create a model that targeted (by implied default) for z13 which couldn't possibly run on z13.

After this change, specifying only -maccel=NNPA, we compile a model that will run on z13 using all available NNPA instructions for z13 (which is zero).

@AlexandreEichenberger
Copy link
Collaborator

@mikeessen would you mind checking the doc that all our examples that have NNPA also have the -mcpu option? Tx

@mikeessen mikeessen deleted the essen-updates branch October 4, 2023 17:19
@mikeessen
Copy link
Collaborator Author

@AlexandreEichenberger Thanks, there are a couple examples which needed updating. I created a PR to address: #2551

@AlexandreEichenberger
Copy link
Collaborator

Thanks for the quick fixes on the doc :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants