-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[export] Add support for serialization and deserialization of Exported
At the moment we can export a JAX function into an Exported and we can invoke an Exported from another JAX function, but there is no way to serialize an Exported to be able to use it in another process. Exported now has all the features we had in mind, so it is a reasonable time to add a serialization method. The intention is for the serialization to have backwards compatibility guarantees, meaning that we can deserialize an Exported that has been serialized with an older version of JAX. This PR does not add explicit support for versioning, nor backwards compatibility tests. Those will follow. Here we add serialization and deserialization to bytearray, using the flatbuffers package. We use flatbuffers because it is simple, it has backwards and forwards compatibility guarantees, it is a lightweight dependency that does not require additional build steps, and it is fast (deserialization simply indexes into the bytearray rather than creating a Python structure). In the process of implementing this we have done some small cleanup of the Exported structure: * renamed serialization_version to mlir_module_serialization_version * renamed disabled_checks to disabled_safety_checks This code is tested by changing export_test.py to interpose a serialization followed by a deserialization every time we export.export. There is a known bug with the serialization of effects, so I disabled one of the export tests. Will fix in a subsequent PR. PiperOrigin-RevId: 590078785
- Loading branch information
Showing
11 changed files
with
1,504 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ absl-py | |
build | ||
cloudpickle | ||
colorama>=0.4.4 | ||
flatbuffers | ||
hypothesis | ||
numpy>=1.22 | ||
pillow>=9.1.0 | ||
|
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ sphinx-design | |
myst-nb>=1.0.0 | ||
|
||
# Packages used for CI tests. | ||
flatbuffers | ||
pytest | ||
pytest-xdist | ||
|
||
|
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,129 @@ | ||
// Copyright 2023 The JAX 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 | ||
// | ||
// https://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. | ||
|
||
// To regenerate the serialization_generated.py, install flatc (e.g., | ||
// from Homebrew) and then: | ||
// | ||
// 1. Run flatc --python --gen-onefile serialization.fbs | ||
// 2. Delete the trailing newlines at the end | ||
// 3. Add back the licence comment at the start | ||
// | ||
|
||
namespace jax.experimental.export.serialization; | ||
|
||
enum PyTreeDefKind: byte { | ||
leaf = 0, | ||
none = 1, | ||
tuple = 2, | ||
list = 3, | ||
dict = 4, | ||
} | ||
|
||
table PyTreeDef { | ||
kind: PyTreeDefKind; | ||
children: [PyTreeDef]; | ||
children_names: [string]; // only for "dict" | ||
} | ||
|
||
enum AbstractValueKind: byte { | ||
shapedArray = 0, | ||
abstractToken = 1, | ||
} | ||
|
||
enum DType: byte { | ||
bool, | ||
i8, | ||
i16, | ||
i32, | ||
i64, | ||
ui8, | ||
ui16, | ||
ui32, | ||
ui64, | ||
f16, | ||
f32, | ||
f64, | ||
c64, | ||
c128, | ||
|
||
bf16, | ||
|
||
i4, | ||
ui4, | ||
|
||
f8_e4m3b11fnuz, | ||
f8_e4m3fn, | ||
f8_e4m3fnuz, | ||
f8_e5m2, | ||
f8_e5m2fnuz, | ||
} | ||
|
||
table AbstractValue { | ||
kind: AbstractValueKind; | ||
shape: [string]; // we support shape polymorphism | ||
dtype: DType; | ||
} | ||
|
||
enum ShardingKind: byte { | ||
unspecified, | ||
hlo_sharding, | ||
} | ||
|
||
table Sharding { | ||
kind: ShardingKind; | ||
hlo_sharding_proto: [byte]; | ||
} | ||
|
||
table Effect { | ||
type_name: string; | ||
} | ||
|
||
enum DisabledSafetyCheckKind: byte { | ||
platform, | ||
custom_call, | ||
shape_assertions, | ||
} | ||
|
||
table DisabledSafetyCheck { | ||
kind: DisabledSafetyCheckKind; | ||
custom_call_target: string; | ||
} | ||
|
||
table Exported { | ||
serialization_version: uint16; | ||
|
||
function_name: string; | ||
in_tree: PyTreeDef; | ||
in_avals: [AbstractValue]; | ||
out_tree: PyTreeDef; | ||
out_avals: [AbstractValue]; | ||
nr_devices: short; | ||
in_shardings: [Sharding]; | ||
out_shardings: [Sharding]; | ||
|
||
lowering_platforms: [string]; | ||
|
||
ordered_effects: [Effect]; | ||
unordered_effects: [Effect]; | ||
disabled_checks: [DisabledSafetyCheck]; | ||
|
||
mlir_module_serialized: [byte]; | ||
mlir_module_serialization_version: uint16; | ||
module_kept_var_idx: [uint16]; | ||
uses_shape_polymorphism: bool; | ||
|
||
vjp: Exported; | ||
} | ||
|
||
root_type Exported; |
Oops, something went wrong.