reader_torch.go 1.03 KB
Newer Older
Michael Yang's avatar
Michael Yang committed
1
2
3
4
package convert

import (
	"io"
5
	"io/fs"
Michael Yang's avatar
Michael Yang committed
6
	"strings"
Michael Yang's avatar
Michael Yang committed
7
8
9
10
11

	"github.com/nlpodyssey/gopickle/pytorch"
	"github.com/nlpodyssey/gopickle/types"
)

Michael Yang's avatar
Michael Yang committed
12
func parseTorch(fsys fs.FS, replacer *strings.Replacer, ps ...string) ([]Tensor, error) {
Michael Yang's avatar
Michael Yang committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
	var ts []Tensor
	for _, p := range ps {
		pt, err := pytorch.Load(p)
		if err != nil {
			return nil, err
		}

		for _, k := range pt.(*types.Dict).Keys() {
			t := pt.(*types.Dict).MustGet(k)

			var shape []uint64
			for dim := range t.(*pytorch.Tensor).Size {
				shape = append(shape, uint64(dim))
			}

			ts = append(ts, torch{
				storage: t.(*pytorch.Tensor).Source,
				tensorBase: &tensorBase{
Michael Yang's avatar
Michael Yang committed
31
					name:  replacer.Replace(k.(string)),
Michael Yang's avatar
Michael Yang committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
					shape: shape,
				},
			})
		}
	}

	return ts, nil
}

type torch struct {
	storage pytorch.StorageInterface
	*tensorBase
}

Michael Yang's avatar
llama4  
Michael Yang committed
46
47
48
49
50
51
52
53
54
55
56
func (t torch) Clone() Tensor {
	return torch{
		storage: t.storage,
		tensorBase: &tensorBase{
			name:     t.name,
			shape:    t.shape,
			repacker: t.repacker,
		},
	}
}

Michael Yang's avatar
Michael Yang committed
57
58
59
func (pt torch) WriteTo(w io.Writer) (int64, error) {
	return 0, nil
}