Module: Safetensors::Numo

Defined in:
lib/safetensors/numo.rb

Constant Summary collapse

DTYPES =
{
  "DFloat" => "float64",
  "SFloat" => "float32"
}
TYPES =
{
  "F64" => :DFloat,
  "F32" => :SFloat,
  "I64" => :Int64,
  "U64" => :UInt64,
  "I32" => :Int32,
  "U32" => :UInt32,
  "I16" => :Int16,
  "U16" => :UInt16,
  "I8" => :Int8,
  "U8" => :UInt8
}

Class Method Summary collapse

Class Method Details

.load(data) ⇒ Object



30
31
32
33
# File 'lib/safetensors/numo.rb', line 30

def load(data)
  flat = Safetensors.deserialize(data)
  _view2numo(flat)
end

.load_file(filename) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/safetensors/numo.rb', line 35

def load_file(filename)
  result = {}
  Safetensors.safe_open(filename, framework: "numo") do |f|
    f.keys.each do |k|
      result[k] = f.get_tensor(k)
    end
  end
  result
end

.save(tensor_dict, metadata: nil) ⇒ Object



22
23
24
# File 'lib/safetensors/numo.rb', line 22

def save(tensor_dict, metadata: nil)
  Safetensors.serialize(_flatten(tensor_dict), metadata: )
end

.save_file(tensor_dict, filename, metadata: nil) ⇒ Object



26
27
28
# File 'lib/safetensors/numo.rb', line 26

def save_file(tensor_dict, filename, metadata: nil)
  Safetensors.serialize_file(_flatten(tensor_dict), filename, metadata: )
end