Skip to content

Commit

Permalink
Add Span to Config
Browse files Browse the repository at this point in the history
  • Loading branch information
itsubaki committed Oct 4, 2023
1 parent c563b56 commit 2d4d3dd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
20 changes: 14 additions & 6 deletions variable/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@ var Config = config{
Train: true,
}

func Nograd() func() {
type Span struct {
End func()
}

func Nograd() *Span {
Config.EnableBackprop = false
return func() {
Config.EnableBackprop = true
return &Span{
End: func() {
Config.EnableBackprop = true
},
}
}

func TestMode() func() {
func TestMode() *Span {
Config.Train = false
return func() {
Config.Train = true
return &Span{
End: func() {
Config.Train = true
},
}
}
4 changes: 2 additions & 2 deletions variable/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func ExampleNograd() {
f()

func() {
defer variable.Nograd()()
defer variable.Nograd().End()

fmt.Println("backprop:", variable.Config.EnableBackprop)
f()
Expand All @@ -44,7 +44,7 @@ func ExampleTestMode() {
fmt.Println("train:", variable.Config.Train)

func() {
defer variable.TestMode()()
defer variable.TestMode().End()

fmt.Println("train:", variable.Config.Train)
}()
Expand Down
2 changes: 1 addition & 1 deletion variable/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (v *Variable) Backward(opts ...Opts) {
// backward
func() {
if len(opts) == 0 || !opts[0].CreateGraph {
defer Nograd()()
defer Nograd().End()
}

gxs := f.Backward(gys...)
Expand Down

0 comments on commit 2d4d3dd

Please sign in to comment.