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 IDA bugs #43

Merged
merged 7 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,87 @@ struct VariableSetOpInterface
}
};

struct QualifiedVariableGetOpInterface
: public DerivableOpInterface::ExternalModel<
QualifiedVariableGetOpInterface, QualifiedVariableGetOp> {
mlir::LogicalResult createPartialDerivative(mlir::Operation *op,
mlir::OpBuilder &builder,
State &state) const {
return createDerivative(op, builder, state);
}

mlir::LogicalResult createTimeDerivative(mlir::Operation *op,
mlir::OpBuilder &builder,
State &state,
bool deriveDependencies) const {
return createDerivative(op, builder, state);
}

mlir::LogicalResult createDerivative(mlir::Operation *op,
mlir::OpBuilder &builder,
State &state) const {
auto castedOp = mlir::cast<QualifiedVariableGetOp>(op);

mlir::OpBuilder::InsertionGuard guard(builder);
builder.setInsertionPointAfter(castedOp);

if (auto tensorType =
castedOp.getResult().getType().cast<mlir::TensorType>()) {
auto elementType = tensorType.getElementType();

auto materializableType =
mlir::dyn_cast<ConstantMaterializableTypeInterface>(elementType);

if (!materializableType) {
return mlir::failure();
}

mlir::Value zero = materializableType.materializeIntConstant(
builder, castedOp.getLoc(), 0);

auto derivedOp = builder.create<TensorBroadcastOp>(castedOp.getLoc(),
tensorType, zero);

state.mapDerivative(castedOp, derivedOp);
return mlir::success();
}

if (auto arrayType = castedOp.getResult().getType().cast<ArrayType>()) {
auto elementType = arrayType.getElementType();

auto materializableType =
mlir::dyn_cast<ConstantMaterializableTypeInterface>(elementType);

if (!materializableType) {
return mlir::failure();
}

mlir::Value zero = materializableType.materializeIntConstant(
builder, castedOp.getLoc(), 0);

auto derivedOp =
builder.create<ArrayBroadcastOp>(castedOp.getLoc(), arrayType, zero);

state.mapDerivative(castedOp, derivedOp);
return mlir::success();
}

auto materializableType =
mlir::dyn_cast<ConstantMaterializableTypeInterface>(
castedOp.getResult().getType());

if (!materializableType) {
return mlir::failure();
}

mlir::Value zero = materializableType.materializeIntConstant(
builder, castedOp.getLoc(), 0);

state.mapDerivative(castedOp, zero);
return mlir::success();
}
};

struct GlobalVariableGetOpInterface
: public DerivableOpInterface::ExternalModel<GlobalVariableGetOpInterface,
GlobalVariableGetOp> {
Expand Down Expand Up @@ -3210,6 +3291,9 @@ void registerDerivableOpInterfaceExternalModels(
VariableGetOp::attachInterface<::VariableGetOpInterface>(*context);
VariableSetOp::attachInterface<::VariableSetOpInterface>(*context);

// Qualified variable operations.
QualifiedVariableGetOp::attachInterface<::QualifiedVariableGetOpInterface>(*context);

// Global variable operations.
GlobalVariableGetOp::attachInterface<::GlobalVariableGetOpInterface>(*context);

Expand Down
Loading