Skip to content

Commit 022c3b8

Browse files
committed
Fixed assumption of single byte size
This allows the key (hyperparameter name) to be much longer than 127 bytes.
1 parent f6f9ca9 commit 022c3b8

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/hparams.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ end
8282
# Dictionary serialisation in ProtoBuf does not work for this specific map type
8383
# and must be overloaded so that it can be parsed. The format was derived by
8484
# looking at the binary output of a log file created by tensorboardX.
85+
# These protobuf overloads should be removed once https://github.com/JuliaIO/ProtoBuf.jl/pull/234 is merged.
8586
function PB.encode(e::ProtoEncoder, i::Int, x::Dict{String,HValue})
8687
for (k, v) in x
8788
PB.Codecs.encode_tag(e, 1, PB.Codecs.LENGTH_DELIMITED)
@@ -92,12 +93,14 @@ function PB.encode(e::ProtoEncoder, i::Int, x::Dict{String,HValue})
9293
end
9394
return nothing
9495
end
96+
9597
# Similarly, we must overload the size calculation to take into account the new
9698
# format.
97-
function PB.Codecs._encoded_size(x::Dict{String,HValue}, i::Int)
98-
# Field number and length is another 2 bytes
99-
# There are two bytes for each key value pair extra
100-
return mapreduce((xi) -> 2 + PB.Codecs._encoded_size(xi.first, 1) + PB.Codecs._encoded_size(xi.second, 2), +, x, init=0)
99+
function PB.Codecs._encoded_size(d::Dict{String,HValue}, i::Int)
100+
mapreduce(x->begin
101+
total_size = PB.Codecs._encoded_size(x.first, 1) + PB.Codecs._encoded_size(x.second, 2)
102+
return 1 + PB.Codecs._varint_size(total_size) + total_size
103+
end, +, d, init=0)
101104
end
102105

103106

0 commit comments

Comments
 (0)