-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IFRT] Add IFRT IR program serialize options and proto to store seria…
…lized IFRT IR programs. PiperOrigin-RevId: 695576399
- Loading branch information
1 parent
9e0fe15
commit 66d6f1d
Showing
10 changed files
with
331 additions
and
19 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
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
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,42 @@ | ||
/* Copyright 2024 The OpenXLA 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. | ||
==============================================================================*/ | ||
|
||
syntax = "proto3"; | ||
|
||
package xla.ifrt; | ||
|
||
message IfrtIrAtomProgramProto { | ||
string name = 1; | ||
// String of the form "major.minor.patch", representing the atom program | ||
// version. Currently, only used to denote VHLO version. | ||
optional string version = 2; | ||
// Serialized atom program. If version is set then the serialized program | ||
// is in the VHLO dialect, otherwise it is in its original dialect. | ||
bytes program = 3; | ||
} | ||
|
||
// Proto for storing a serialized IFRT IR program. | ||
message IfrtIrProgramProto { | ||
bytes ifrt_program = 1; | ||
|
||
// String of the form "major.minor.patch", representing the IFRT IR version. | ||
// If ifrt version is not set, then the program is not versioned and the | ||
// whole program will be serialized into the `ifrt_program` field. | ||
optional string ifrt_version = 2; | ||
|
||
// List of atom programs that are used by the IFRT IR program. It is empty | ||
// if the IFRT IR program is not versioned (i.e., `ifrt_version` is not set). | ||
repeated IfrtIrAtomProgramProto atom_programs = 3; | ||
} |
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
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,93 @@ | ||
/* Copyright 2024 The OpenXLA 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 "xla/python/ifrt/ir/version.h" | ||
|
||
#include <cstdint> | ||
|
||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/Support/ErrorHandling.h" | ||
#include "llvm/Support/Regex.h" | ||
#include "mlir/IR/Diagnostics.h" | ||
#include "mlir/Support/LLVM.h" | ||
|
||
namespace xla { | ||
namespace ifrt { | ||
|
||
namespace { | ||
|
||
static int64_t parseNumber(llvm::StringRef num_ref) { | ||
int64_t num; | ||
if (num_ref.getAsInteger(/*radix=*/10, num)) { | ||
llvm::report_fatal_error("failed to parse version number"); | ||
} | ||
return num; | ||
} | ||
|
||
/// Validate version argument is `#.#.#` (ex: 0.9.0, 0.99.0, 1.2.3) | ||
/// Returns the vector of 3 matches (major, minor, patch) if successful, | ||
/// else returns failure. | ||
static mlir::FailureOr<llvm::SmallVector<int64_t, 3>> extractVersionNumbers( | ||
llvm::StringRef version_ref) { | ||
llvm::Regex versionRegex("^([0-9]+)\\.([0-9]+)\\.([0-9]+)$"); | ||
llvm::SmallVector<llvm::StringRef> matches; | ||
if (!versionRegex.match(version_ref, &matches)) { | ||
return mlir::failure(); | ||
} | ||
return llvm::SmallVector<int64_t, 3>{parseNumber(matches[1]), | ||
parseNumber(matches[2]), | ||
parseNumber(matches[3])}; | ||
} | ||
|
||
} // namespace | ||
|
||
mlir::FailureOr<Version> Version::fromString(llvm::StringRef version_ref) { | ||
auto failOrVersionArray = extractVersionNumbers(version_ref); | ||
if (mlir::failed(failOrVersionArray)) { | ||
return mlir::failure(); | ||
} | ||
auto versionArr = *failOrVersionArray; | ||
return Version(versionArr[0], versionArr[1], versionArr[2]); | ||
} | ||
|
||
mlir::FailureOr<int64_t> Version::getBytecodeVersion() const { | ||
if (*this <= getCurrentVersion()) return 0; | ||
return mlir::failure(); | ||
} | ||
|
||
Version Version::fromCompatibilityRequirement( | ||
CompatibilityRequirement requirement) { | ||
switch (requirement) { | ||
case CompatibilityRequirement::NONE: | ||
return Version::getCurrentVersion(); | ||
case CompatibilityRequirement::WEEK_4: | ||
return Version(0, 1, 0); // v0.1.0 - Nov 05, 2024 | ||
case CompatibilityRequirement::WEEK_12: | ||
return Version(0, 1, 0); // v0.1.0 - Nov 05, 2024 | ||
case CompatibilityRequirement::MAX: | ||
return Version::getMinimumVersion(); | ||
} | ||
llvm::report_fatal_error("Unsupported compatibility requirement"); | ||
} | ||
|
||
mlir::Diagnostic& operator<<(mlir::Diagnostic& diag, const Version& version) { | ||
return diag << version.toString(); | ||
} | ||
llvm::raw_ostream& operator<<(llvm::raw_ostream& os, const Version& version) { | ||
return os << version.toString(); | ||
} | ||
|
||
} // namespace ifrt | ||
} // namespace xla |
Oops, something went wrong.