-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python bindings for registering check dialect (#2445)
Check dialect is an auxiliary dialect used in StableHLO repository for validation of StableHLO program evaluation. Currently there is no cleaner way to parse a module containing check dialect operations or to create them. One way to go around this is to textually modify the check dialect ops to normalize them to generic MLIR text and allow unregistered dialect to pars that text. However, this PR provodes a cleaner way to process check dialect. This PR prepares for open sourcing some of the utilities, leveraged in #2404, for auto-generating testdata formatted test files.
- Loading branch information
Showing
9 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* Copyright 2024 The StableHLO Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#include "stablehlo/integrations/c/CheckDialect.h" | ||
|
||
#include "mlir/CAPI/Registration.h" | ||
#include "stablehlo/tests/CheckOps.h" | ||
|
||
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Check, check, | ||
mlir::stablehlo::check::CheckDialect) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* Copyright 2024 The StableHLO Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#ifndef STABLEHLO_INTEGRATIONS_C_CHECK_DIALECT_H | ||
#define STABLEHLO_INTEGRATIONS_C_CHECK_DIALECT_H | ||
|
||
#include "mlir-c/RegisterEverything.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(Check, check); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // STABLEHLO_INTEGRATIONS_C_CHECK_DIALECT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* Copyright 2024 The StableHLO Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#include "mlir-c/IR.h" | ||
#include "mlir/Bindings/Python/PybindAdaptors.h" | ||
#include "stablehlo/integrations/c/CheckDialect.h" | ||
|
||
namespace py = pybind11; | ||
|
||
PYBIND11_MODULE(_check, m) { | ||
m.doc() = "check main python extension"; | ||
|
||
// | ||
// Dialects. | ||
// | ||
|
||
m.def( | ||
"register_dialect", | ||
[](MlirContext context, bool load) { | ||
MlirDialectHandle dialect = mlirGetDialectHandle__check__(); | ||
mlirDialectHandleRegisterDialect(dialect, context); | ||
if (load) { | ||
mlirDialectHandleLoadDialect(dialect, context); | ||
} | ||
}, | ||
py::arg("context"), py::arg("load") = true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* Copyright 2024 The StableHLO Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#ifndef STABLEHLO_INTEGRATIONS_PYTHON_CHECK_OPS | ||
#define STABLEHLO_INTEGRATIONS_PYTHON_CHECK_OPS | ||
|
||
include "stablehlo/tests/CheckOps.td" | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Copyright 2024 The StableHLO Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
|
||
# pylint: disable=wildcard-import,relative-beyond-top-level,g-import-not-at-top | ||
from ._check_ops_gen import * | ||
from .._mlir_libs._check import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright 2024 The StableHLO Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
"""Tests for CHECK Python APIs.""" | ||
|
||
# pylint: disable=wildcard-import,undefined-variable | ||
|
||
from mlir import ir | ||
from mlir.dialects import check as check_dialect | ||
from mlir.dialects import stablehlo as stablehlo_dialect | ||
|
||
|
||
def run(f): | ||
with ir.Context() as context: | ||
check_dialect.register_dialect(context) | ||
stablehlo_dialect.register_dialect(context) | ||
f() | ||
return f | ||
|
||
@run | ||
def test_parse(): | ||
asm = """ | ||
module { | ||
func.func @main() { | ||
%cst = stablehlo.constant dense<[1.0, 2.0]> : tensor<2xf32> | ||
%cst_0 = stablehlo.constant dense<[3.0, 4.0]> : tensor<2xf32> | ||
%0 = stablehlo.add %cst, %cst_0 : tensor<2xf32> | ||
check.expect_eq_const %0, dense<[4.0, 6.0]> : tensor<2xf32> | ||
return | ||
} | ||
} | ||
""" | ||
ir.Module.parse(asm) |