feat(free C pointer): added 'defer C.free(unsafe.Pointer(ptr))' to all missing. Except for CMalloc func and Nll func
This commit is contained in:
parent
20f338014d
commit
5f7aec5061
|
@ -66,9 +66,6 @@ func runLinear() {
|
||||||
* bs.ZeroGrad()
|
* bs.ZeroGrad()
|
||||||
* loss.MustBackward()
|
* loss.MustBackward()
|
||||||
*
|
*
|
||||||
* // TODO: why `loss` need to print out to get updated?
|
|
||||||
* fmt.Printf("loss (epoch %v): %v\n", epoch, loss.MustToString(0))
|
|
||||||
*
|
|
||||||
* ts.NoGrad(func() {
|
* ts.NoGrad(func() {
|
||||||
* ws.MustAdd_(ws.MustGrad().MustMul1(ts.FloatScalar(-1.0)))
|
* ws.MustAdd_(ws.MustGrad().MustMul1(ts.FloatScalar(-1.0)))
|
||||||
* bs.MustAdd_(bs.MustGrad().MustMul1(ts.FloatScalar(-1.0)))
|
* bs.MustAdd_(bs.MustGrad().MustMul1(ts.FloatScalar(-1.0)))
|
||||||
|
|
|
@ -221,6 +221,7 @@ func AtSaveMulti(tensors []Ctensor, tensor_names []string, ntensors int, filenam
|
||||||
|
|
||||||
cpointerSize := 4
|
cpointerSize := 4
|
||||||
cnamesPtr := (*[1 << 30]**C.char)(C.malloc(C.size_t(cpointerSize * len(tensor_names))))
|
cnamesPtr := (*[1 << 30]**C.char)(C.malloc(C.size_t(cpointerSize * len(tensor_names))))
|
||||||
|
defer C.free(unsafe.Pointer(cnamesPtr))
|
||||||
for i := 0; i < len(tensor_names); i++ {
|
for i := 0; i < len(tensor_names); i++ {
|
||||||
cname := C.CString(tensor_names[i])
|
cname := C.CString(tensor_names[i])
|
||||||
cnamesPtr[i] = &cname
|
cnamesPtr[i] = &cname
|
||||||
|
|
|
@ -583,6 +583,8 @@ func (ts Tensor) MustLogSoftmax(dim int64, dtype int32) (retVal Tensor) {
|
||||||
|
|
||||||
func (ts Tensor) NllLoss(target Tensor) (retVal Tensor, err error) {
|
func (ts Tensor) NllLoss(target Tensor) (retVal Tensor, err error) {
|
||||||
ptr := (*lib.Ctensor)(unsafe.Pointer(C.malloc(0)))
|
ptr := (*lib.Ctensor)(unsafe.Pointer(C.malloc(0)))
|
||||||
|
// NOTE: uncomment this causes panic
|
||||||
|
// defer C.free(unsafe.Pointer(ptr))
|
||||||
|
|
||||||
weight := NewTensor()
|
weight := NewTensor()
|
||||||
|
|
||||||
|
|
|
@ -466,7 +466,7 @@ func RunBackward(tensors []Tensor, inputs []Tensor, keepGraphB bool, createGraph
|
||||||
// TODO: Are they allocated continouslly???
|
// TODO: Are they allocated continouslly???
|
||||||
for i := 0; i < len(inputs); i++ {
|
for i := 0; i < len(inputs); i++ {
|
||||||
outputPtr := (*lib.Ctensor)(unsafe.Pointer(C.malloc(0)))
|
outputPtr := (*lib.Ctensor)(unsafe.Pointer(C.malloc(0)))
|
||||||
// defer C.free(unsafe.Pointer(outputPtr))
|
defer C.free(unsafe.Pointer(outputPtr))
|
||||||
outputsPtr = append(outputsPtr, outputPtr)
|
outputsPtr = append(outputsPtr, outputPtr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,6 +53,8 @@ func init() {
|
||||||
func CMalloc(nbytes int) (dataPtr unsafe.Pointer, buf *bytes.Buffer) {
|
func CMalloc(nbytes int) (dataPtr unsafe.Pointer, buf *bytes.Buffer) {
|
||||||
|
|
||||||
dataPtr = C.malloc(C.size_t(nbytes))
|
dataPtr = C.malloc(C.size_t(nbytes))
|
||||||
|
// NOTE: uncomment this cause panic!
|
||||||
|
// defer C.free(unsafe.Pointer(dataPtr))
|
||||||
|
|
||||||
// Recall: 1 << 30 = 1 * 2 * 30
|
// Recall: 1 << 30 = 1 * 2 * 30
|
||||||
// Ref. See more at https://stackoverflow.com/questions/48756732
|
// Ref. See more at https://stackoverflow.com/questions/48756732
|
||||||
|
|
Loading…
Reference in New Issue
Block a user