Class: TensorStream::Packer

Inherits:
Object
  • Object
show all
Defined in:
lib/tensor_stream/graph_serializers/packer.rb

Overview

Utility class to handle data type serialization

Class Method Summary collapse

Class Method Details

.pack(value, data_type) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tensor_stream/graph_serializers/packer.rb', line 6

def self.pack(value, data_type)
  value = value.is_a?(Array) ? value.flatten : [value]
  byte_value = case data_type
               when :float64
                 value.pack("d*")
               when :float32, :float16, :float
                 value.pack("f*")
               when :uint32
                 value.pack("L*")
               when :int32, :int
                 value.pack("l*")
               when :int64
                 value.pack("q*")
               when :uint64
                 value.pack("Q*")
               when :uint8
                 value.pack("C*")
               when :boolean
                 value.map { |v| v ? 1 : 0 }.pack("C*")
               when :string
                 if value.is_a?(Array)
                   value.to_yaml
                 else
                   value
                 end
               else
                 raise "unknown type #{data_type}"
  end

  byte_value
end

.pack_to_str(value, data_type) ⇒ Object



38
39
40
# File 'lib/tensor_stream/graph_serializers/packer.rb', line 38

def self.pack_to_str(value, data_type)
  pack(value, data_type).bytes.map { |b| /[^[:print:]]/.match?(b.chr) ? "\\#{sprintf("%o", b).rjust(3, "0")}" : b.chr }.join
end

.unpack(unpacked, data_type) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/tensor_stream/graph_serializers/packer.rb', line 47

def self.unpack(unpacked, data_type)
  case data_type
  when :float32, :float, :float16
    unpacked.unpack("f*")
  when :float64
    unpacked.unpack("d*")
  when :int32, :int
    unpacked.unpack("L*")
  when :uint32
    unpacked.unpack("l*")
  when :int64
    unpacked.unpack("q*")
  when :uint64
    unpacked.unpack("Q*")
  when :uint8
    unpacked.unpack("C*")
  when :boolean
    unpacked.unpack("C*").map { |v| v == 1 }
  end
end

.unpack_from_str(content, data_type) ⇒ Object



42
43
44
45
# File 'lib/tensor_stream/graph_serializers/packer.rb', line 42

def self.unpack_from_str(content, data_type)
  unpacked = eval(%("#{content}"), __FILE__)
  unpack(unpacked, data_type)
end