diff --git a/src/mlpack/bindings/julia/print_param_defn.hpp b/src/mlpack/bindings/julia/print_param_defn.hpp index 1ee6d7d164..6e5d152770 100644 --- a/src/mlpack/bindings/julia/print_param_defn.hpp +++ b/src/mlpack/bindings/julia/print_param_defn.hpp @@ -79,12 +79,14 @@ void PrintParamDefn( // buffer = ccall((:SerializePtr, Library), // Vector{UInt8}, (Ptr{Nothing}, Ptr{UInt8}), model.ptr, // Base.pointer(buf_len)) - // buf = Base.unsafe_wrap(buf_ptr, buf_len[0]; own=true) + // buf = Base.unsafe_wrap(buf_ptr, buf_len[1]; own=true) + // write(stream, buf_len[1]) // write(stream, buf) // end // // function deserialize(stream::IO):: - // buffer = read(stream) + // buf_len = read(stream, UInt) + // buffer = read(stream, buf_len) // (ccall((:DeserializePtr, Library), // Ptr{Nothing}, (Vector{UInt8}, UInt), buffer, length(buffer))) // end @@ -138,6 +140,7 @@ void PrintParamDefn( << "Base.pointer(buf_len))" << std::endl; std::cout << " buf = Base.unsafe_wrap(Vector{UInt8}, buf_ptr, buf_len[1]; " << "own=true)" << std::endl; + std::cout << " write(stream, buf_len[1])" << std::endl; std::cout << " write(stream, buf)" << std::endl; std::cout << "end" << std::endl; @@ -145,7 +148,8 @@ void PrintParamDefn( std::cout << "# Deserialize a model from the given stream." << std::endl; std::cout << "function deserialize" << type << "(stream::IO)::" << type << std::endl; - std::cout << " buffer = read(stream)" << std::endl; + std::cout << " buf_len = read(stream, UInt)" << std::endl; + std::cout << " buffer = read(stream, buf_len)" << std::endl; std::cout << " " << type << "(ccall((:Deserialize" << type << "Ptr, " << programName << "Library), Ptr{Nothing}, (Ptr{UInt8}, UInt), " << "Base.pointer(buffer), length(buffer)))" << std::endl; diff --git a/src/mlpack/bindings/julia/tests/runtests.jl b/src/mlpack/bindings/julia/tests/runtests.jl index bb98c54435..57a6f548e0 100644 --- a/src/mlpack/bindings/julia/tests/runtests.jl +++ b/src/mlpack/bindings/julia/tests/runtests.jl @@ -342,6 +342,27 @@ end model_in=newModel) end +# Test that we can serialize a model as part of a larger tuple. +@testset "TestStreamTupleSerialization" begin + _, _, _, _, _, _, modelOut, _, _, _, _, _, _, _ = + test_julia_binding(4.0, 12, "hello", + build_model=true) + + stream = IOBuffer() + serialize(stream, (modelOut, 3, 4, 5)) + + newStream = IOBuffer(copy(stream.data)) + (newModel, a, b, c) = deserialize(newStream) + + _, _, _, _, _, bwOut, _, _, _, _, _, _, _, _ = + test_julia_binding(4.0, 12, "hello", + model_in=newModel) + + @test a == 3 + @test b == 4 + @test c == 5 +end + @testset "TestFileSerialization" begin _, _, _, _, _, _, modelOut, _, _, _, _, _, _, _ = test_julia_binding(4.0, 12, "hello",