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

Refine hardcoded attr setting/getting in ir #8420

Merged
merged 7 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
21 changes: 12 additions & 9 deletions oneflow/ir/lib/OneFlow/OneFlowOpFolders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ OpFoldResult UnaryFold(MLIRContext* ctx, ArrayRef<Attribute> operands,
const auto attr_dict = operands.front().cast<mlir::DictionaryAttr>();
auto attrs = NamedAttrList(attr_dict);
const auto tensor = support::DenseElementsAttrToTensor(
attr_dict.get("value"), attr_dict.get("device_tag"), attr_dict.get("device_name"));
attr_dict.get("value"), attr_dict.get(OpTrait::IsOpConfCompatible<void>::getDeviceTagAttr()),
attr_dict.get(OpTrait::IsOpConfCompatible<void>::getDeviceNameAttr()));
const auto result = f(tensor).GetPtrOrThrow();
attrs.set("value", support::TensorToDenseElementsAttr(result, ctx));
attrs.set("op_name", GenNewVariableOpName(ctx));
attrs.set(OpTrait::IsOpConfCompatible<void>::getOpNameAttr(), GenNewVariableOpName(ctx));

return attrs.getDictionary(ctx);
}
Expand All @@ -67,17 +68,19 @@ OpFoldResult BinaryFold(MLIRContext* ctx, ArrayRef<Attribute> operands,
auto rhs_attr_dict = operands.back().cast<mlir::DictionaryAttr>();

auto attrs = NamedAttrList(lhs_attr_dict);
const auto lhs_tensor = support::DenseElementsAttrToTensor(lhs_attr_dict.get("value"),
lhs_attr_dict.get("device_tag"),
lhs_attr_dict.get("device_name"));
const auto rhs_tensor = support::DenseElementsAttrToTensor(rhs_attr_dict.get("value"),
rhs_attr_dict.get("device_tag"),
rhs_attr_dict.get("device_name"));
const auto lhs_tensor = support::DenseElementsAttrToTensor(
lhs_attr_dict.get("value"),
lhs_attr_dict.get(OpTrait::IsOpConfCompatible<void>::getDeviceTagAttr()),
lhs_attr_dict.get(OpTrait::IsOpConfCompatible<void>::getDeviceNameAttr()));
const auto rhs_tensor = support::DenseElementsAttrToTensor(
rhs_attr_dict.get("value"),
rhs_attr_dict.get(OpTrait::IsOpConfCompatible<void>::getDeviceTagAttr()),
rhs_attr_dict.get(OpTrait::IsOpConfCompatible<void>::getDeviceNameAttr()));

const auto result = f(lhs_tensor, rhs_tensor).GetPtrOrThrow();

attrs.set("value", support::TensorToDenseElementsAttr(result, ctx));
attrs.set("op_name", GenNewVariableOpName(ctx));
attrs.set(OpTrait::IsOpConfCompatible<void>::getOpNameAttr(), GenNewVariableOpName(ctx));

return attrs.getDictionary(ctx);
}
Expand Down
12 changes: 7 additions & 5 deletions oneflow/ir/lib/OneFlow/Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,9 @@ ::llvm::SmallVector<::mlir::Value, 4> CreateConv2dAndErasePad(::mlir::PatternRew

NamedAttrList GetUserOpCommonAttrs(MLIRContext* ctx, const std::string& op_name) {
NamedAttrList attrs;
attrs.set("op_name", StringAttr::get(ctx, op_name));
attrs.set("device_tag", StringAttr::get(ctx, "cpu"));
attrs.set("device_name",
attrs.set(OpTrait::IsOpConfCompatible<void>::getOpNameAttr(), StringAttr::get(ctx, op_name));
attrs.set(OpTrait::IsOpConfCompatible<void>::getDeviceTagAttr(), StringAttr::get(ctx, "cpu"));
attrs.set(OpTrait::IsOpConfCompatible<void>::getDeviceNameAttr(),
ArrayAttr::get(ctx, llvm::to_vector<8>(llvm::map_range(ArrayRef<StringRef>({"@0:0"}),
[&](StringRef v) -> Attribute {
return StringAttr::get(ctx, v);
Expand Down Expand Up @@ -569,7 +569,8 @@ llvm::SmallVector<mlir::Value, 4> getInputOperandTransposeOp(NCHWCompatible op,
PatternRewriter& rewriter) {
std::string transpose_name = OpTrait::IsOpConfCompatible<void>::getOpName(op).str()
+ "_transpose_input_" + std::to_string(num_transposed_operand);
transpose_attributes.set(llvm::StringRef("op_name"), rewriter.getStringAttr(transpose_name));
transpose_attributes.set(llvm::StringRef(OpTrait::IsOpConfCompatible<void>::getOpNameAttr()),
rewriter.getStringAttr(transpose_name));
SmallVector<Value, 4> input_operands;
input_operands.push_back(val);
auto res = rewriter
Expand All @@ -583,7 +584,8 @@ TransposeOp getResultTransposeOp(NCHWCompatible op, Value val, NamedAttrList tra
int num_transposed_result, PatternRewriter& rewriter) {
std::string transpose_name = OpTrait::IsOpConfCompatible<void>::getOpName(op).str()
+ "_transpose_output_" + std::to_string(num_transposed_result);
transpose_attributes.set(llvm::StringRef("op_name"), rewriter.getStringAttr(transpose_name));
transpose_attributes.set(llvm::StringRef(OpTrait::IsOpConfCompatible<void>::getOpNameAttr()),
rewriter.getStringAttr(transpose_name));
SmallVector<Value, 4> operands;
operands.push_back(val);
TransposeOp transpose_op = rewriter.create<oneflow::TransposeOp>(op.getLoc(), val.getType(),
Expand Down
16 changes: 6 additions & 10 deletions oneflow/ir/oneflow-translate/lib/OneFlow/Importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,7 @@ LogicalResult ConvertCtrlInputs(Operation* op, ::oneflow::OperatorConf& op_conf)
if (auto ctrl_ins = GetCtrlIntputOperands(op)) {
for (auto ctrl_in : ctrl_ins.getValue()) {
op_conf.add_ctrl_in_op_name(
ctrl_in.getDefiningOp()
->getAttrOfType<StringAttr>(OpTrait::IsOpConfCompatible<void>::getOpNameAttr())
.getValue()
.str());
OpTrait::IsOpConfCompatible<void>::getOpName(ctrl_in.getDefiningOp()).str());
}
}
return success();
Expand Down Expand Up @@ -675,9 +672,8 @@ llvm::Optional<std::string> GetOutputLbn(OpResult result) {
auto size = std::get<1>(name_size_tuple);
if ((size_sum + size) > result_number) {
const uint32_t bn_i = result_number - size_sum;
return def_op->getAttrOfType<StringAttr>(OpTrait::IsOpConfCompatible<void>::getOpNameAttr())
.str()
+ "/" + name + "_" + std::to_string(bn_i);
return OpTrait::IsOpConfCompatible<void>::getOpName(def_op).str() + "/" + name + "_"
+ std::to_string(bn_i);
}
size_sum += size;
}
Expand Down Expand Up @@ -946,7 +942,7 @@ LogicalResult ConvertVariableOpConf(VariableOp op, ::oneflow::OperatorConf* op_c
// all operands are ctrl_inputs
for (const auto& operand : op->getOperands()) {
op_conf->add_ctrl_in_op_name(
operand.getDefiningOp()->getAttrOfType<StringAttr>("op_name").getValue().str());
OpTrait::IsOpConfCompatible<void>::getOpName(operand.getDefiningOp()).str());
}
if (auto floatInit = op.float_initializer()) {
var_op_conf->mutable_initializer()->mutable_constant_conf()->set_value(
Expand Down Expand Up @@ -1002,7 +998,7 @@ LogicalResult ConvertInputOpConf(InputOp op, ::oneflow::OperatorConf* op_conf) {
// operand 0 is block argument, others are ctrl_inputs
for (size_t i = 1; i < op->getNumOperands(); ++i) {
op_conf->add_ctrl_in_op_name(
op->getOperand(i).getDefiningOp()->getAttrOfType<StringAttr>("op_name").getValue().str());
OpTrait::IsOpConfCompatible<void>::getOpName(op->getOperand(i).getDefiningOp()).str());
}

return success();
Expand Down Expand Up @@ -1054,7 +1050,7 @@ LogicalResult ConvertOutputOpConf(OutputOp op, ::oneflow::OperatorConf* op_conf)
output_op_conf->set_in(output_lbn);
for (size_t i = 1; i < op->getNumOperands(); ++i) {
op_conf->add_ctrl_in_op_name(
op->getOperand(i).getDefiningOp()->getAttrOfType<StringAttr>("op_name").getValue().str());
OpTrait::IsOpConfCompatible<void>::getOpName(op->getOperand(i).getDefiningOp()).str());
}
return success();
}
Expand Down