Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions src/enzyme_ad/jax/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,6 @@ SymmetricResultAnalysis::State SymmetricResultAnalysis::localGuaranteed(
return perm.size() == 2 && perm[0] == 1 && perm[1] == 0;
};

// TODO: check for dot_general as well

// commutative operation with A and A^T will always be symmetric
// op(A, A^T) will also always be symmetric
if (stablehlo::hasTraitElementwise(op) &&
Expand Down Expand Up @@ -660,6 +658,39 @@ SymmetricResultAnalysis::State SymmetricResultAnalysis::localGuaranteed(
}
}

// A(A^T) will always be symmetric
if (auto dotOp = dyn_cast_or_null<stablehlo::DotGeneralOp>(op)) {
auto lhs = dotOp.getLhs();
auto rhs = dotOp.getRhs();

auto lhsDims = dotOp.getDotDimensionNumbers().getLhsContractingDimensions();
auto rhsDims = dotOp.getDotDimensionNumbers().getRhsContractingDimensions();

if (auto lhsType = dyn_cast_or_null<ShapedType>(lhs.getType());
lhsType && lhsType.hasRank() && lhsType.getRank() == 2) {

if (rhs == lhs) {
if (lhsDims.size() == 1 && lhsDims == rhsDims) {
return State::GUARANTEED;
}
}

if (lhsDims.size() == 1 && lhsDims != rhsDims) {
if (auto rhsT = rhs.getDefiningOp<stablehlo::TransposeOp>()) {
auto rhsInput = rhsT.getOperand();
if (rhsInput == lhs && isTrueTranspose(rhsT))
return State::GUARANTEED;
}

if (auto lhsT = dyn_cast_or_null<stablehlo::TransposeOp>(lhs.getDefiningOp())) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if (auto lhsT = dyn_cast_or_null<stablehlo::TransposeOp>(lhs.getDefiningOp())) {
if (auto lhsT = lhs.getDefiningOp<stablehlo::TransposeOp>()) {

auto lhsInput = lhsT.getOperand();
if (lhsInput == rhs && isTrueTranspose(lhsT))
return State::GUARANTEED;
}
}
}
}

bool recursiveCheck = false;

// elementwise ops
Expand Down
Loading