onnxruntime/onnxruntime/core/flatbuffers/schema
Scott McKay 9372e9a0a3
Support >2GB of Tensor data in training checkpoint (#20077)
### Description
<!-- Describe your changes. -->
Add ability to store initializer data in an external file.
Update training checkpoint code to use external file if data > ~2GB.

I don't see a way for the flatbuffers 64-bit offsets to be used, as they
don't support storing 'table' types with 64-bit offsets (and our Tensor
is a 'table' type not a simple struct).


0cfb7eb80b/tests/64bit/test_64bit.fbs (L38-L39)

Allowing a Tensor to have its raw_data in an external file should
hopefully work with the least friction. As it's an extra field it's
backwards compatible.

Please feel free to suggest alternative approaches. 

Side note: the diffs in the generated *.fbs.h files are unexpectedly
large. Maybe they weren't re-generated when the new flatbuffers version
was checked in. I updated by running:
`python .\compile_schema.py -f <build output
dir>\_deps\flatbuffers-build\Debug\flatc.exe`
from onnxruntime\core\flatbuffers\schema which I thought was the correct
way but maybe that's out of date.

I think you can ignore all the diffs in the generated files and just
worry about the changes to the .fbs files in
onnxruntime/core/flatbuffers/schema. Basically start at the bottom of
the files changed and work up as all the 'real' diffs are there.

### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->

---------

Co-authored-by: carzh <wolfivyaura@gmail.com>
2024-04-22 15:17:43 -07:00
..
compile_schema.py Support >2GB of Tensor data in training checkpoint (#20077) 2024-04-22 15:17:43 -07:00
ort.fbs Support >2GB of Tensor data in training checkpoint (#20077) 2024-04-22 15:17:43 -07:00
ort.fbs.h Support >2GB of Tensor data in training checkpoint (#20077) 2024-04-22 15:17:43 -07:00
ort_training_checkpoint.fbs Support >2GB of Tensor data in training checkpoint (#20077) 2024-04-22 15:17:43 -07:00
ort_training_checkpoint.fbs.h Support >2GB of Tensor data in training checkpoint (#20077) 2024-04-22 15:17:43 -07:00
README.md [On-Device-Training] Upgrade Flatbuffers to Support 2GB+ Checkpoints. (#19770) 2024-03-14 16:36:24 -07:00

ORT Flatbuffer Schemas

This directory contains the ORT file format schema and the generated C++ header file for the ORT file format. This directory also contains the on-device training checkpoint schema and the corresponding auto generated C++ header file for the checkpoint format.

The ORT file format schema and the on-device training checkpoint schema uses the FlatBuffers serialization library.

Please do not directly modify the generated C++ header file for the ORT file format or for the training checkpoint file format, or the generated Python binding files.

The flatbuffers compiler (flatc) is built as part of an ONNX Runtime build. It is located in the _deps/flatbuffers-build/ subdirectory of the build output directory.

e.g.

  • Windows Debug build
    • \build\Windows\Debug_deps\flatbuffers-build\Debug\flatc.exe
  • Linux Debug build
    • /build/Linux/Debug/_deps/flatbuffers-build/flatc

It is possible to use another flatc as well, e.g., from a separate installation. Note that ONNX Runtime uses FlatBuffers 23.5.26.

To update the flatbuffers schemas and generated files:

  1. Modify the ORT file format schema or training checkpoint schema.

  2. Run compile_schema.py to generate the C++ and Python bindings.

    python onnxruntime/core/flatbuffers/schema/compile_schema.py --flatc <path to flatc>
    
  3. Update the version history and record the changes. Changes made to the ORT file format schema warrants not only updating the ort format version, but also the checkpoint version since the checkpoint schema depends on the ort format schema.

ORT FB format version history

In ort_format_version.h, see IsOrtModelVersionSupported() for the supported versions and kOrtModelVersion for the current version.

Version 1

History begins.

Initial support for FlatBuffers that includes Model support. Graph support including Attributes, Tensors, Tensor Sequences, Maps and Sequences. Constant initializers are also supported. Constant nodes are converted to constant initializers in the ORT format.

Version 2

Support for sparse initializers. Sparse intializers are stored within ORT FlatBuffers format, which includes sparse initializers converted from a Constant node attribute.

Version 3

Support for storing graph_doc_string field in Model (ORT FlatBuffers format).

Version 4

Update kernel def hashing to not depend on ordering of type constraint types (NOT BACKWARDS COMPATIBLE).

Version 5

Remove kernel def hashes and add KernelTypeStrResolver info to replace them (LIMITED BACKWARDS COMPATIBILITY). The change to the ORT format itself is not backwards compatibility-breaking, but ORT provides limited backwards compatibility for processing older models with missing KernelTypeStrResolver info. The motivation for this update is to support additional execution providers with statically registered kernels. The original approach of using kernel def hashes was not so extensible as it required the execution provider providing hashes to be enabled at model conversion time.

Version 6

Support for float 8 types. See Float stored in 8 bits for further details about their format and usage.

Checkpoint format version history

In checkpoint_version.h, see IsCheckpointVersionSupported() for the supported versions and kCheckpointVersion for the current version.

Version 1

Initial support for the On-Device Training Checkpoint format. The format includes support for the ModuleState (stores the module parameters), OptimizerGroups (stores the optimizer states), and PropertyBag (stores custom user properties with support for int64, float and strings).