sugarme / gotch

Go binding for Pytorch C++ API (libtorch)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Best way to serialize tensor into bytes?

StrongerXi opened this issue · comments

As title. This is useful for transferring tensor over network, which should be a common thing given golang's typical use cases.

Right now I'm using the following, but it involves some unnecessary file io.

gotch/ts/tensor.go

Lines 800 to 804 in 17f2c49

func (ts *Tensor) MustSave(path string) {
if err := ts.Save(path); err != nil {
log.Fatal(err)
}
}

@StrongerXi ,

Yeah, there're 2 APIs ts.Save() and ts.SaveMultiNew() to save tensor(s). They are just wrappers to libtorch APIs. I have no issue to load/save my trained models by using them. Would you mind sharing what are your use cases? and what issues are you facing?

Sorry I should've clarified. I'm trying to serialize tensors (not models), transmit over network, and deserialize. Use case is distributed inference, for a very naive class project:)

@StrongerXi,

Ok I got you. Basically, tensor data is in C memory, and handling data between C and Go is quite expensive in terms of performance. Also, reconstruct tensor (deserialize) will double the cost. I don't have a clearly optimal way, but you can try to get tensor data with ts.Values() then serialize your data if you want to wire things up with Go.

There must be much better ways and I am happy to know about.

Thanks. Seems like we just have to add a wrapper to the torch::save(tensor, std::ostream) API (see this).

I might send in a PR if this really becomes a performance issue in my project. Else I'll close this issue:).

close this issue for now as no response.