Skip to content

Commit 9902e6e

Browse files
jurahulTensorFlow MLIR Team
authored andcommitted
[HLO] Add LMHLO CollectivePermute verification.
- Extract verification of source target pairs attached to collective permute into a common helper function and use that to verify both MHLO and LMHLO variants. - Change MlirGpuTestBase::ParseMlirModule to allow returning back a failure, and use that to update the mlir_gpu_compile_test to check the new behavior. PiperOrigin-RevId: 362156962
1 parent 4f16b10 commit 9902e6e

8 files changed

Lines changed: 155 additions & 28 deletions

File tree

‎BUILD‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,17 @@ gentbl(
232232
deps = [":hlo_ops_td_files"],
233233
)
234234

235+
cc_library(
236+
name = "hlo_ops_common",
237+
srcs = ["lib/Dialect/mhlo/IR/hlo_ops_common.cc"],
238+
hdrs = ["include/mlir-hlo/Dialect/mhlo/IR/hlo_ops_common.h"],
239+
includes = ["include"],
240+
deps = [
241+
"@llvm-project//mlir:IR",
242+
"@llvm-project//mlir:Support",
243+
],
244+
)
245+
235246
cc_library(
236247
name = "lhlo_gpu_ops_structs",
237248
srcs = [
@@ -399,14 +410,15 @@ cc_library(
399410
],
400411
includes = ["include"],
401412
deps = [
402-
"hlo_ops_pattern_gen",
403413
":canonicalize_inc_gen",
404414
":chlo_ops_inc_gen",
405415
":convert_op_folder",
406416
":hlo_ops_base_enums",
407417
":hlo_ops_base_inc_gen",
408418
":hlo_ops_base_structs",
419+
":hlo_ops_common",
409420
":hlo_ops_inc_gen",
421+
":hlo_ops_pattern_gen",
410422
":infer_fusibility_op_interface",
411423
"@llvm-project//llvm:Support",
412424
"@llvm-project//mlir:Analysis",
@@ -443,6 +455,7 @@ cc_library(
443455
":hlo_ops_base_enums",
444456
":hlo_ops_base_inc_gen",
445457
":hlo_ops_base_structs",
458+
":hlo_ops_common",
446459
":lhlo_ops_inc_gen",
447460
":lhlo_ops_structs_inc_gen",
448461
"@llvm-project//llvm:Support",
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
16+
#ifndef TENSORFLOW_COMPILER_MLIR_HLO_INCLUDE_MLIR_HLO_DIALECT_MHLO_IR_HLO_OPS_COMMON_H_
17+
#define TENSORFLOW_COMPILER_MLIR_HLO_INCLUDE_MLIR_HLO_DIALECT_MHLO_IR_HLO_OPS_COMMON_H_
18+
19+
// This file defines functionality shared between chlo/mhlo/lhlo dialects.
20+
21+
#include "mlir/IR/BuiltinAttributes.h"
22+
#include "mlir/IR/Operation.h"
23+
24+
namespace mlir {
25+
namespace hlo {
26+
27+
// Verifies the source target pairs attached to collective permute.
28+
LogicalResult VerifyCollectivePermuteSourceTargetPairs(
29+
Operation *op, DenseIntElementsAttr attr);
30+
31+
} // namespace hlo
32+
} // namespace mlir
33+
34+
#endif // TENSORFLOW_COMPILER_MLIR_HLO_INCLUDE_MLIR_HLO_DIALECT_MHLO_IR_HLO_OPS_COMMON_H_

‎include/mlir-hlo/Dialect/mhlo/IR/lhlo_ops.td‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ def LHLO_CollectivePermuteOp: LHLO_Op<"collective_permute", [SameTypeOperands]>,
584584
I64ElementsAttr:$source_target_pairs,
585585
OptionalAttr<ChannelHandle>:$channel_id
586586
);
587+
let verifier = [{ return Verify(*this); }];
587588
}
588589

589590
def LHLO_FftOp: LHLO_Op<"fft", []>, BASE_HLO_FftOp {

‎lib/Dialect/mhlo/IR/CMakeLists.txt‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ add_mlir_library(MhloInferFusibilityOpInterface
4040
MLIRinfer_fusibility_op_interfaceIncGen
4141
)
4242

43+
add_mlir_library(HloOpsCommon
44+
hlo_ops_common.cc
45+
)
4346

4447
add_mlir_dialect_library(MhloDialect
4548
hlo_ops.cc
@@ -57,6 +60,7 @@ target_link_libraries(MhloDialect
5760
MLIRIR
5861
MhloInferFusibilityOpInterface
5962
MLIRMhloUtils
63+
HloOpsCommon
6064
)
6165

6266

@@ -67,7 +71,11 @@ add_mlir_dialect_library(LmhloDialect
6771
DEPENDS
6872
MLIRlhlo_opsIncGen
6973
)
70-
target_link_libraries(LmhloDialect PUBLIC MLIRIR)
74+
target_link_libraries(LmhloDialect
75+
PUBLIC
76+
MLIRIR
77+
HloOpsCommon
78+
)
7179

7280
add_mlir_dialect_library(LmhloGPUDialect
7381
lhlo_gpu_ops.cc

‎lib/Dialect/mhlo/IR/hlo_ops.cc‎

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ limitations under the License.
3636
#include "llvm/Support/FormatVariadic.h"
3737
#include "llvm/Support/MathExtras.h"
3838
#include "mlir-hlo/Dialect/mhlo/IR/hlo_ops.h.inc"
39+
#include "mlir-hlo/Dialect/mhlo/IR/hlo_ops_common.h"
3940
#include "mlir-hlo/utils/convert_op_folder.h"
4041
#include "mlir-hlo/utils/hlo_utils.h"
4142
#include "mlir/Dialect/Shape/IR/Shape.h"
@@ -479,32 +480,8 @@ LogicalResult AbsOp::inferReturnTypes(
479480
//===----------------------------------------------------------------------===//
480481

481482
static LogicalResult Verify(CollectivePermuteOp op) {
482-
// Check that source target pair is Nx2 tensor.
483-
auto type = op.source_target_pairs().getType().dyn_cast<RankedTensorType>();
484-
if (type.getRank() != 2)
485-
return op.emitError() << "expect source_target_pairs attribute to be of "
486-
"rank 2, but got rank "
487-
<< type.getRank();
488-
if (type.getShape()[1] != 2)
489-
return op.emitError()
490-
<< "expect source_target_pairs attribute of shape (N, 2), but got ("
491-
<< type.getShape() << ")";
492-
// Check source target pairs for duplicate sources or targets
493-
llvm::DenseSet<int64_t> sources;
494-
llvm::DenseSet<int64_t> targets;
495-
for (auto i = op.source_target_pairs().begin(),
496-
e = op.source_target_pairs().end();
497-
i != e; ++i) {
498-
auto val = (*i).getSExtValue();
499-
if (i.getIndex() % 2 == 0) {
500-
bool is_unique = sources.insert(val).second;
501-
if (!is_unique) return op.emitError() << "duplicate sources not allowed.";
502-
} else {
503-
bool is_unique = targets.insert(val).second;
504-
if (!is_unique) return op.emitError() << "duplicate targets not allowed.";
505-
}
506-
}
507-
return success();
483+
return mlir::hlo::VerifyCollectivePermuteSourceTargetPairs(
484+
op, op.source_target_pairs());
508485
}
509486

510487
//===----------------------------------------------------------------------===//
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
16+
#include "mlir-hlo/Dialect/mhlo/IR/hlo_ops_common.h"
17+
18+
#include "mlir/IR/BuiltinTypes.h"
19+
20+
namespace mlir {
21+
namespace hlo {
22+
23+
// Verifies the source target pairs attached to collective permute.
24+
LogicalResult VerifyCollectivePermuteSourceTargetPairs(
25+
Operation *op, DenseIntElementsAttr attr) {
26+
auto type = attr.getType().dyn_cast<RankedTensorType>();
27+
if (type.getRank() != 2)
28+
return op->emitError() << "expect source_target_pairs attribute to be of "
29+
"rank 2, but got rank "
30+
<< type.getRank();
31+
if (type.getShape()[1] != 2)
32+
return op->emitError()
33+
<< "expect source_target_pairs attribute of shape (N, 2), but got ("
34+
<< type.getShape() << ")";
35+
// Check source target pairs for duplicate sources or targets.
36+
llvm::DenseSet<int64_t> sources;
37+
llvm::DenseSet<int64_t> targets;
38+
for (auto i = attr.begin(), e = attr.end(); i != e; ++i) {
39+
auto val = (*i).getSExtValue();
40+
if (i.getIndex() % 2 == 0) {
41+
bool is_unique = sources.insert(val).second;
42+
if (!is_unique)
43+
return op->emitError() << "duplicate sources not allowed.";
44+
} else {
45+
bool is_unique = targets.insert(val).second;
46+
if (!is_unique)
47+
return op->emitError() << "duplicate targets not allowed.";
48+
}
49+
}
50+
return success();
51+
}
52+
53+
} // namespace hlo
54+
} // namespace mlir

‎lib/Dialect/mhlo/IR/lhlo_ops.cc‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ limitations under the License.
3131
#include "llvm/ADT/SmallVector.h"
3232
#include "llvm/ADT/StringRef.h"
3333
#include "llvm/Support/FormatVariadic.h"
34+
#include "mlir-hlo/Dialect/mhlo/IR/hlo_ops_common.h"
3435
#include "mlir-hlo/Dialect/mhlo/IR/lhlo_ops.h.inc"
3536
#include "mlir/Dialect/StandardOps/IR/Ops.h"
3637
#include "mlir/IR/Attributes.h"
@@ -132,6 +133,15 @@ static LogicalResult Verify(AllReduceOp op) {
132133
return success();
133134
}
134135

136+
//===----------------------------------------------------------------------===//
137+
// CollectivePermuteOp
138+
//===----------------------------------------------------------------------===//
139+
140+
static LogicalResult Verify(CollectivePermuteOp op) {
141+
return mlir::hlo::VerifyCollectivePermuteSourceTargetPairs(
142+
op, op.source_target_pairs());
143+
}
144+
135145
//===----------------------------------------------------------------------===//
136146
// ConstOp.
137147
//===----------------------------------------------------------------------===//

‎tests/lhlo_ops.mlir‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,36 @@ func @collective_permute_memrefs(%arg0: memref<128x32xf32>, %arg_out: memref<128
788788

789789
// -----
790790

791+
func @invalid_collective_permute(%arg0: memref<128x32xf32>, %arg_out: memref<128x32xf32>) -> () {
792+
// expected-error@+1{{expect source_target_pairs attribute of shape (N, 2), but got (1, 3)}}
793+
"lmhlo.collective_permute"(%arg0, %arg_out) {
794+
source_target_pairs = dense<[[2, 3, 4]]> : tensor<1x3xi64>
795+
} : (memref<128x32xf32>, memref<128x32xf32>) -> ()
796+
return
797+
}
798+
799+
// -----
800+
801+
func @invalid_collective_permute(%arg0: memref<128x32xf32>, %arg_out: memref<128x32xf32>) -> () {
802+
// expected-error@+1{{duplicate sources not allowed.}}
803+
"lmhlo.collective_permute"(%arg0, %arg_out) {
804+
source_target_pairs = dense<[[1,2], [1,3]]> : tensor<2x2xi64>
805+
} : (memref<128x32xf32>, memref<128x32xf32>) -> ()
806+
return
807+
}
808+
809+
// -----
810+
811+
func @invalid_collective_permute(%arg0: memref<128x32xf32>, %arg_out: memref<128x32xf32>) -> () {
812+
// expected-error@+1{{duplicate targets not allowed.}}
813+
"lmhlo.collective_permute"(%arg0, %arg_out) {
814+
source_target_pairs = dense<[[1,2], [0,2]]> : tensor<2x2xi64>
815+
} : (memref<128x32xf32>, memref<128x32xf32>) -> ()
816+
return
817+
}
818+
819+
// -----
820+
791821
// CHECK-LABEL: func @fft_memrefs
792822
func @fft_memrefs(%arg0: memref<3x9xf32>, %arg_out: memref<3x5xcomplex<f32>>) -> () {
793823
"lmhlo.fft"(%arg0, %arg_out) {fft_length = dense<9> : tensor<1xi64>, fft_type = "RFFT"} : (memref<3x9xf32>, memref<3x5xcomplex<f32>>) -> ()

0 commit comments

Comments
 (0)