From 31368194a88405741eaf361e7a793cbb1b8f8fd9 Mon Sep 17 00:00:00 2001 From: Xuanda Yang Date: Fri, 1 Dec 2023 01:59:30 +0800 Subject: [PATCH] [serialization] Add Halide version and serialization version in serialization format (#7905) * halide version * serialization version * format * Fix Makefile * trigger buildbots --------- Co-authored-by: Andrew Adams Co-authored-by: Steven Johnson --- Makefile | 8 ++++++++ src/CMakeLists.txt | 6 ++++++ src/Deserialization.cpp | 19 +++++++++++++++++++ src/Serialization.cpp | 12 +++++++++++- src/halide_ir.fbs | 6 ++++-- 5 files changed, 48 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index d1ebace87bda..7364941941a2 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9ef902c27be2..771944b10d42 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -509,6 +509,12 @@ if (WITH_SERIALIZATION) target_include_directories(Halide PRIVATE "$") 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; diff --git a/src/Deserialization.cpp b/src/Deserialization.cpp index c0e9f39de7bf..b27918756886 100644 --- a/src/Deserialization.cpp +++ b/src/Deserialization.cpp @@ -1403,6 +1403,25 @@ Pipeline Deserializer::deserialize(const std::vector &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 func_names_in_order = deserialize_vector(pipeline_obj->func_names_in_order(), &Deserializer::deserialize_string); diff --git a/src/Serialization.cpp b/src/Serialization.cpp index 2928e3b7ebbf..857c963cceab 100644 --- a/src/Serialization.cpp +++ b/src/Serialization.cpp @@ -1501,6 +1501,14 @@ void Serializer::serialize(const Pipeline &pipeline, std::vector &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), @@ -1509,7 +1517,9 @@ void Serializer::serialize(const Pipeline &pipeline, std::vector &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(); diff --git a/src/halide_ir.fbs b/src/halide_ir.fbs index 479e488b6739..8148aca639a9 100644 --- a/src/halide_ir.fbs +++ b/src/halide_ir.fbs @@ -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"; @@ -710,6 +710,8 @@ table Pipeline { parameters: [Parameter]; external_parameters: [ExternalParameter]; buffers: [Buffer]; + halide_version: string; + serialization_version: string; } root_type Pipeline;