feat(wrapper/tensor): Drop method

This commit is contained in:
sugarme 2020-06-11 07:37:09 +10:00
parent 91ecd562f6
commit 23150953d9
3 changed files with 27 additions and 0 deletions

View File

@ -62,4 +62,8 @@ func main() {
wrapper.MustCopy_(dst, ts)
dst.Print()
ts.MustDrop()
// The below statement will be panic as `ts` has been dropped.
// ts.Print()
}

View File

@ -305,3 +305,9 @@ func AtToString(ts Ctensor, lineSize int64) string {
return goString
}
// void at_free(tensor);
func AtFree(ts Ctensor) {
ctensor := (C.tensor)(ts)
C.at_free(ctensor)
}

View File

@ -814,3 +814,20 @@ func (ts Tensor) MustToString(lw int64) (retVal string) {
return retVal
}
// Drop drops (frees) the tensor
func (ts Tensor) Drop() (err error) {
lib.AtFree(ts.ctensor)
if err = TorchErr(); err != nil {
return err
}
return nil
}
// MustDrop drops the tensor. It will be panic if error
func (ts Tensor) MustDrop() {
if err := ts.Drop(); err != nil {
log.Fatal(err)
}
}