Skip to content

Commit

Permalink
[serialization] Add Halide version and serialization version in seria…
Browse files Browse the repository at this point in the history
…lization format (#7905)

* halide version

* serialization version

* format

* Fix Makefile

* trigger buildbots

---------

Co-authored-by: Andrew Adams <andrew.b.adams@gmail.com>
Co-authored-by: Steven Johnson <srj@google.com>
  • Loading branch information
3 people authored Nov 30, 2023
1 parent ad5dd20 commit 3136819
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ CXX_FLAGS += $(WEBASSEMBLY_CXX_FLAGS)
# On ubuntu, this requires packages flatbuffers-compiler and libflatbuffers-dev
ifneq (,$(shell which flatc))
CXX_FLAGS += -DWITH_SERIALIZATION -I $(BUILD_DIR) -I $(shell which flatc | sed 's/bin.flatc/include/')
# Note: if updating here, be sure to update in CMakeLists.txt as well
HALIDE_SERIALIZATION_VERSION_MAJOR ?= 0
HALIDE_SERIALIZATION_VERSION_MINOR ?= 1
HALIDE_SERIALIZATION_VERSION_PATCH ?= 0
HALIDE_SERIALIZATION_VERSION=$(HALIDE_SERIALIZATION_VERSION_MAJOR).$(HALIDE_SERIALIZATION_VERSION_MINOR).$(HALIDE_SERIALIZATION_VERSION_PATCH)
CXX_FLAGS += -DHALIDE_SERIALIZATION_VERSION_MAJOR=$(HALIDE_SERIALIZATION_VERSION_MAJOR)
CXX_FLAGS += -DHALIDE_SERIALIZATION_VERSION_MINOR=$(HALIDE_SERIALIZATION_VERSION_MINOR)
CXX_FLAGS += -DHALIDE_SERIALIZATION_VERSION_PATCH=$(HALIDE_SERIALIZATION_VERSION_PATCH)
endif

# This is required on some hosts like powerpc64le-linux-gnu because we may build
Expand Down
6 changes: 6 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,12 @@ if (WITH_SERIALIZATION)
target_include_directories(Halide PRIVATE "$<BUILD_INTERFACE:${fb_dir}>")
target_link_libraries(Halide PRIVATE Halide_flatbuffers)
target_compile_definitions(Halide PRIVATE WITH_SERIALIZATION)
# Note: if updating here, be sure to update in Makefile as well
target_compile_definitions(Halide PUBLIC
HALIDE_SERIALIZATION_VERSION_MAJOR=0
HALIDE_SERIALIZATION_VERSION_MINOR=1
HALIDE_SERIALIZATION_VERSION_PATCH=0
)
endif ()

# Enable serialization testing by intercepting JIT compilation with a serialization roundtrip;
Expand Down
19 changes: 19 additions & 0 deletions src/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,25 @@ Pipeline Deserializer::deserialize(const std::vector<uint8_t> &data) {
user_warning << "deserialized pipeline is empty\n";
return Pipeline();
}

std::string deserialized_halide_version = deserialize_string(pipeline_obj->halide_version());
std::string halide_version = std::to_string(HALIDE_VERSION_MAJOR) + "." +
std::to_string(HALIDE_VERSION_MINOR) + "." +
std::to_string(HALIDE_VERSION_PATCH);
if (deserialized_halide_version != halide_version) {
user_warning << "deserialized pipeline is built with Halide version " << deserialized_halide_version
<< ", but current Halide version is " << halide_version << "\n";
}

std::string deserialized_serialization_version = deserialize_string(pipeline_obj->serialization_version());
std::string serialization_version = std::to_string(HALIDE_SERIALIZATION_VERSION_MAJOR) + "." +
std::to_string(HALIDE_SERIALIZATION_VERSION_MINOR) + "." +
std::to_string(HALIDE_SERIALIZATION_VERSION_PATCH);
if (deserialized_serialization_version != serialization_version) {
user_error << "deserialized pipeline is built with Halide serialization version " << deserialized_serialization_version
<< ", but current Halide serialization version is " << serialization_version << "\n";
}

const std::vector<std::string> func_names_in_order =
deserialize_vector<flatbuffers::String, std::string>(pipeline_obj->func_names_in_order(),
&Deserializer::deserialize_string);
Expand Down
12 changes: 11 additions & 1 deletion src/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,14 @@ void Serializer::serialize(const Pipeline &pipeline, std::vector<uint8_t> &resul
buffers_serialized.push_back(serialize_buffer(builder, buffer.second));
}

std::string halide_version = std::to_string(HALIDE_VERSION_MAJOR) + "." +
std::to_string(HALIDE_VERSION_MINOR) + "." +
std::to_string(HALIDE_VERSION_PATCH);

std::string serialization_version = std::to_string(HALIDE_SERIALIZATION_VERSION_MAJOR) + "." +
std::to_string(HALIDE_SERIALIZATION_VERSION_MINOR) + "." +
std::to_string(HALIDE_SERIALIZATION_VERSION_PATCH);

auto pipeline_obj = Serialize::CreatePipeline(builder,
builder.CreateVector(funcs_serialized),
builder.CreateVector(output_names_serialized),
Expand All @@ -1509,7 +1517,9 @@ void Serializer::serialize(const Pipeline &pipeline, std::vector<uint8_t> &resul
builder.CreateVector(func_names_in_order_serialized),
builder.CreateVector(parameters_serialized),
builder.CreateVector(external_parameters_serialized),
builder.CreateVector(buffers_serialized));
builder.CreateVector(buffers_serialized),
serialize_string(builder, halide_version),
serialize_string(builder, serialization_version));
builder.Finish(pipeline_obj);

uint8_t *buf = builder.GetBufferPointer();
Expand Down
6 changes: 4 additions & 2 deletions src/halide_ir.fbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Halide.Serialize;

// This corresponds to the corresponding Halide version.
file_identifier "HL17";
// This identifies the serialized data being a Halide pipeline. Should be exactly 4 bytes.
file_identifier "HLDE";

// File extension of any written files. "hlpipe" stands for Halide Pipeline.
file_extension "hlpipe";
Expand Down Expand Up @@ -710,6 +710,8 @@ table Pipeline {
parameters: [Parameter];
external_parameters: [ExternalParameter];
buffers: [Buffer];
halide_version: string;
serialization_version: string;
}

root_type Pipeline;

0 comments on commit 3136819

Please sign in to comment.