-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
75 lines (59 loc) · 1.75 KB
/
example_test.go
1
2
3
4
5
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2023 The NLP Odyssey Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package safetensors_test
import (
"encoding/binary"
"fmt"
"log"
"math"
"github.com/nlpodyssey/safetensors"
)
func ExampleDeserialize() {
serialized := []byte("\x59\x00\x00\x00\x00\x00\x00\x00" +
`{"test":{"dtype":"I32","shape":[2,2],"data_offsets":[0,16]},"__metadata__":{"foo":"bar"}}` +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
loaded, err := safetensors.Deserialize(serialized)
if err != nil {
log.Fatal(err)
}
fmt.Printf("len = %d\n", loaded.Len())
fmt.Printf("names = %+v\n", loaded.Names())
tensor, ok := loaded.Tensor("test")
if !ok {
log.Fatal(`tensor "test" not found`)
}
fmt.Printf("tensor type = %s\n", tensor.DType())
fmt.Printf("tensor shape = %+v\n", tensor.Shape())
fmt.Printf("tensor data len = %+v\n", tensor.DataLen())
// Output:
// len = 1
// names = [test]
// tensor type = I32
// tensor shape = [2 2]
// tensor data len = 16
}
func ExampleSerialize() {
floatData := []float32{0, 1, 2, 3, 4, 5}
data := make([]byte, 0, len(floatData)*4)
for _, v := range floatData {
data = binary.LittleEndian.AppendUint32(data, math.Float32bits(v))
}
shape := []uint64{1, 2, 3}
tensor, err := safetensors.NewTensorView(safetensors.F32, shape, data)
if err != nil {
log.Fatal(err)
}
metadata := map[string]safetensors.TensorView{
"foo": tensor,
}
serialized, err := safetensors.Serialize(metadata, nil)
if err != nil {
log.Fatal(err)
}
fmt.Printf("data len = %d\n", len(serialized))
fmt.Printf("data excerpt: ...%s...\n", serialized[8:30])
// Output:
// data len = 96
// data excerpt: ...{"foo":{"dtype":"F32",...
}