-
Notifications
You must be signed in to change notification settings - Fork 8
/
ra25.go
862 lines (711 loc) · 27.3 KB
/
ra25.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
// Copyright (c) 2019, The Emergent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// mpi is a version of ra25 that runs under MPI to learn in parallel
// across multiple nodes, sharing DWt changes via MPI.
package main
//go:generate core generate -add-types
import (
"fmt"
"log"
"os"
"cogentcore.org/core/base/mpi"
"cogentcore.org/core/base/randx"
"cogentcore.org/core/core"
"cogentcore.org/core/icons"
"cogentcore.org/core/math32"
"cogentcore.org/core/math32/vecint"
"cogentcore.org/core/tensor"
"cogentcore.org/core/tensor/table"
"cogentcore.org/core/tensor/tensormpi"
"github.com/emer/axon/v2/axon"
"github.com/emer/emergent/v2/econfig"
"github.com/emer/emergent/v2/egui"
"github.com/emer/emergent/v2/elog"
"github.com/emer/emergent/v2/emer"
"github.com/emer/emergent/v2/env"
"github.com/emer/emergent/v2/estats"
"github.com/emer/emergent/v2/etime"
"github.com/emer/emergent/v2/looper"
"github.com/emer/emergent/v2/netview"
"github.com/emer/emergent/v2/patgen"
"github.com/emer/emergent/v2/paths"
)
func main() {
sim := &Sim{}
sim.New()
sim.ConfigAll()
if sim.Config.GUI {
sim.RunGUI()
} else {
sim.RunNoGUI()
}
}
// see params.go for params
// ParamConfig has config parameters related to sim params
type ParamConfig struct {
// network parameters
Network map[string]any
// size of hidden layer -- can use emer.LaySize for 4D layers
Hidden1Size vecint.Vector2i `default:"{'X':10,'Y':10}" nest:"+"`
// size of hidden layer -- can use emer.LaySize for 4D layers
Hidden2Size vecint.Vector2i `default:"{'X':10,'Y':10}" nest:"+"`
// Extra Param Sheet name(s) to use (space separated if multiple) -- must be valid name as listed in compiled-in params or loaded params
Sheet string
// extra tag to add to file names and logs saved from this run
Tag string
// user note -- describe the run params etc -- like a git commit message for the run
Note string
// Name of the JSON file to input saved parameters from.
File string `nest:"+"`
// Save a snapshot of all current param and config settings in a directory named params_<datestamp> (or _good if Good is true), then quit -- useful for comparing to later changes and seeing multiple views of current params
SaveAll bool `nest:"+"`
// for SaveAll, save to params_good for a known good params state. This can be done prior to making a new release after all tests are passing -- add results to git to provide a full diff record of all params over time.
Good bool `nest:"+"`
}
// RunConfig has config parameters related to running the sim
type RunConfig struct {
// use MPI message passing interface for data parallel computation between nodes running identical copies of the same sim, sharing DWt changes
MPI bool
// use the GPU for computation -- generally faster even for small models if NData ~16
GPU bool `default:"false"`
// number of data-parallel items to process in parallel per trial -- works (and is significantly faster) for both CPU and GPU. Results in an effective mini-batch of learning.
NData int `default:"16" min:"1"`
// number of parallel threads for CPU computation -- 0 = use default
NThreads int `default:"0"`
// starting run number -- determines the random seed -- runs counts from there -- can do all runs in parallel by launching separate jobs with each run, runs = 1
Run int `default:"0"`
// total number of runs to do when running Train
NRuns int `default:"5" min:"1"`
// total number of epochs per run
NEpochs int `default:"100"`
// stop run after this number of perfect, zero-error epochs
NZero int `default:"2"`
// total number of trials per epoch. Should be an even multiple of NData.
NTrials int `default:"32"`
// how often to run through all the test patterns, in terms of training epochs -- can use 0 or -1 for no testing
TestInterval int `default:"5"`
// how frequently (in epochs) to compute PCA on hidden representations to measure variance?
PCAInterval int `default:"5"`
}
// LogConfig has config parameters related to logging data
type LogConfig struct {
// if true, save final weights after each run
SaveWts bool
// if true, save train epoch log to file, as .epc.tsv typically
Epoch bool `default:"true" nest:"+"`
// if true, save run log to file, as .run.tsv typically
Run bool `default:"true" nest:"+"`
// if true, save train trial log to file, as .trl.tsv typically. May be large.
Trial bool `default:"false" nest:"+"`
// if true, save testing epoch log to file, as .tst_epc.tsv typically. In general it is better to copy testing items over to the training epoch log and record there.
TestEpoch bool `default:"false" nest:"+"`
// if true, save testing trial log to file, as .tst_trl.tsv typically. May be large.
TestTrial bool `default:"false" nest:"+"`
// if true, save network activation etc data from testing trials, for later viewing in netview
NetData bool
}
// Config is a standard Sim config -- use as a starting point.
type Config struct {
// specify include files here, and after configuration, it contains list of include files added
Includes []string
// open the GUI -- does not automatically run -- if false, then runs automatically and quits
GUI bool `default:"true"`
// log debugging information
Debug bool
// parameter related configuration options
Params ParamConfig `view:"add-fields"`
// sim running related configuration options
Run RunConfig `view:"add-fields"`
// data logging related configuration options
Log LogConfig `view:"add-fields"`
}
func (cfg *Config) IncludesPtr() *[]string { return &cfg.Includes }
// Sim encapsulates the entire simulation model, and we define all the
// functionality as methods on this struct. This structure keeps all relevant
// state information organized and available without having to pass everything around
// as arguments to methods, and provides the core GUI interface (note the view tags
// for the fields which provide hints to how things should be displayed).
type Sim struct {
// simulation configuration parameters -- set by .toml config file and / or args
Config Config
// the network -- click to view / edit parameters for layers, paths, etc
Net *axon.Network `view:"no-inline"`
// network parameter management
Params emer.NetParams `view:"inline"`
// contains looper control loops for running sim
Loops *looper.Manager `view:"no-inline"`
// contains computed statistic values
Stats estats.Stats
// Contains all the logs and information about the logs.'
Logs elog.Logs
// the training patterns to use
Pats *table.Table `view:"no-inline"`
// Environments
Envs env.Envs `view:"no-inline"`
// axon timing parameters and state
Context axon.Context
// netview update parameters
ViewUpdate netview.ViewUpdate `view:"inline"`
// manages all the gui elements
GUI egui.GUI `view:"-"`
// a list of random seeds to use for each run
RandSeeds randx.Seeds `view:"-"`
// mpi communicator
Comm *mpi.Comm `view:"-"`
// buffer of all dwt weight changes -- for mpi sharing
AllDWts []float32 `view:"-"`
}
// New creates new blank elements and initializes defaults
func (ss *Sim) New() {
econfig.Config(&ss.Config, "config.toml")
if ss.Config.Run.MPI {
ss.MPIInit()
}
if mpi.WorldRank() != 0 {
ss.Config.Log.SaveWts = false
ss.Config.Log.NetData = false
}
ss.Net = &axon.Network{}
ss.Params.Config(ParamSets, ss.Config.Params.Sheet, ss.Config.Params.Tag, ss.Net)
ss.Stats.Init()
ss.Pats = &table.Table{}
ss.RandSeeds.Init(100) // max 100 runs
ss.InitRandSeed(0)
ss.Context.Defaults()
}
////////////////////////////////////////////////////////////////////////////////////////////
// Configs
// ConfigAll configures all the elements using the standard functions
func (ss *Sim) ConfigAll() {
// ss.ConfigPats()
ss.OpenPats()
ss.ConfigEnv()
ss.ConfigNet(ss.Net)
ss.ConfigLogs()
ss.ConfigLoops()
if ss.Config.Params.SaveAll {
ss.Config.Params.SaveAll = false
ss.Net.SaveParamsSnapshot(&ss.Params.Params, &ss.Config, ss.Config.Params.Good)
os.Exit(0)
}
}
func (ss *Sim) ConfigEnv() {
// Can be called multiple times -- don't re-create
var trn, tst *env.FixedTable
if len(ss.Envs) == 0 {
trn = &env.FixedTable{}
tst = &env.FixedTable{}
} else {
trn = ss.Envs.ByMode(etime.Train).(*env.FixedTable)
tst = ss.Envs.ByMode(etime.Test).(*env.FixedTable)
}
// note: names must be standard here!
trn.Nm = etime.Train.String()
trn.Dsc = "training params and state"
trn.Config(table.NewIndexView(ss.Pats))
if ss.Config.Run.MPI {
// this is key mpi step: allocate diff inputs to diff procs
st, ed, _ := tensormpi.AllocN(ss.Pats.Rows)
trn.Table.Indexes = trn.Table.Indexes[st:ed]
// mpi.AllPrintf("st: %d ed: %d\n", st, ed)
}
trn.Validate()
tst.Nm = etime.Test.String()
tst.Dsc = "testing params and state"
tst.Config(table.NewIndexView(ss.Pats))
tst.Sequential = true
if ss.Config.Run.MPI {
st, ed, _ := tensormpi.AllocN(ss.Pats.Rows)
tst.Table.Indexes = tst.Table.Indexes[st:ed]
}
tst.Validate()
// note: to create a train / test split of pats, do this:
// all := table.NewIndexView(ss.Pats)
// splits, _ := split.Permuted(all, []float64{.8, .2}, []string{"Train", "Test"})
// trn.Table = splits.Splits[0]
// tst.Table = splits.Splits[1]
trn.Init(0)
tst.Init(0)
// note: names must be in place when adding
ss.Envs.Add(trn, tst)
}
func (ss *Sim) ConfigNet(net *axon.Network) {
ctx := &ss.Context
net.InitName(net, "RA25")
net.SetMaxData(ctx, ss.Config.Run.NData)
net.SetRandSeed(ss.RandSeeds[0]) // init new separate random seed, using run = 0
inp := net.AddLayer2D("Input", 5, 5, axon.InputLayer)
hid1 := net.AddLayer2D("Hidden1", ss.Config.Params.Hidden1Size.Y, ss.Config.Params.Hidden1Size.X, axon.SuperLayer)
hid2 := net.AddLayer2D("Hidden2", ss.Config.Params.Hidden2Size.Y, ss.Config.Params.Hidden2Size.X, axon.SuperLayer)
out := net.AddLayer2D("Output", 5, 5, axon.TargetLayer)
// use this to position layers relative to each other
// hid2.PlaceRightOf(hid1, 2)
// note: see emergent/path module for all the options on how to connect
// NewFull returns a new paths.Full connectivity pattern
full := paths.NewFull()
net.ConnectLayers(inp, hid1, full, axon.ForwardPath)
net.BidirConnectLayers(hid1, hid2, full)
net.BidirConnectLayers(hid2, out, full)
// net.LateralConnectLayerPath(hid1, full, &axon.HebbPath{}).SetType(emer.Inhib)
// note: if you wanted to change a layer type from e.g., Target to Compare, do this:
// out.SetType(emer.Compare)
// that would mean that the output layer doesn't reflect target values in plus phase
// and thus removes error-driven learning -- but stats are still computed.
net.Build(ctx)
net.Defaults()
net.SetNThreads(ss.Config.Run.NThreads)
ss.ApplyParams()
net.InitWts(ctx)
}
func (ss *Sim) ApplyParams() {
ss.Params.SetAll()
if ss.Config.Params.Network != nil {
ss.Params.SetNetworkMap(ss.Net, ss.Config.Params.Network)
}
}
////////////////////////////////////////////////////////////////////////////////
// Init, utils
// Init restarts the run, and initializes everything, including network weights
// and resets the epoch log table
func (ss *Sim) Init() {
if ss.Config.GUI {
ss.Stats.SetString("RunName", ss.Params.RunName(0)) // in case user interactively changes tag
}
ss.Loops.ResetCounters()
ss.InitRandSeed(0)
// ss.ConfigEnv() // re-config env just in case a different set of patterns was
// selected or patterns have been modified etc
ss.GUI.StopNow = false
ss.ApplyParams()
ss.Net.GPU.SyncParamsToGPU()
ss.NewRun()
ss.ViewUpdate.Update()
ss.ViewUpdate.RecordSyns()
}
// InitRandSeed initializes the random seed based on current training run number
func (ss *Sim) InitRandSeed(run int) {
ss.RandSeeds.Set(run)
ss.RandSeeds.Set(run, &ss.Net.Rand)
}
// ConfigLoops configures the control loops: Training, Testing
func (ss *Sim) ConfigLoops() {
man := looper.NewManager()
totND := ss.Config.Run.NData * mpi.WorldSize() // both sources of data parallel
totTrls := int(math32.IntMultipleGE(float32(ss.Config.Run.NTrials), float32(totND)))
trls := totTrls / mpi.WorldSize()
man.AddStack(etime.Train).
AddTime(etime.Run, ss.Config.Run.NRuns).
AddTime(etime.Epoch, ss.Config.Run.NEpochs).
AddTimeIncr(etime.Trial, trls, ss.Config.Run.NData).
AddTime(etime.Cycle, 200)
man.AddStack(etime.Test).
AddTime(etime.Epoch, 1).
AddTimeIncr(etime.Trial, trls, ss.Config.Run.NData).
AddTime(etime.Cycle, 200)
axon.LooperStdPhases(man, &ss.Context, ss.Net, 150, 199) // plus phase timing
axon.LooperSimCycleAndLearn(man, ss.Net, &ss.Context, &ss.ViewUpdate) // std algo code
man.GetLoop(etime.Train, etime.Trial).OnEnd.Replace("UpdateWeights", func() {
ss.Net.DWt(&ss.Context)
if ss.ViewUpdate.IsViewingSynapse() {
ss.Net.GPU.SyncSynapsesFromGPU()
ss.Net.GPU.SyncSynCaFromGPU() // note: only time we call this
ss.ViewUpdate.RecordSyns() // note: critical to update weights here so DWt is visible
}
ss.MPIWtFromDWt()
})
for m, _ := range man.Stacks {
mode := m // For closures
stack := man.Stacks[mode]
stack.Loops[etime.Trial].OnStart.Add("ApplyInputs", func() {
ss.ApplyInputs()
})
}
man.GetLoop(etime.Train, etime.Run).OnStart.Add("NewRun", ss.NewRun)
// Train stop early condition
man.GetLoop(etime.Train, etime.Epoch).IsDone["NZeroStop"] = func() bool {
// This is calculated in TrialStats
stopNz := ss.Config.Run.NZero
if stopNz <= 0 {
stopNz = 2
}
curNZero := ss.Stats.Int("NZero")
stop := curNZero >= stopNz
return stop
}
// Add Testing
trainEpoch := man.GetLoop(etime.Train, etime.Epoch)
trainEpoch.OnStart.Add("TestAtInterval", func() {
if (ss.Config.Run.TestInterval > 0) && ((trainEpoch.Counter.Cur+1)%ss.Config.Run.TestInterval == 0) {
// Note the +1 so that it doesn't occur at the 0th timestep.
ss.TestAll()
}
})
trainEpoch.OnEnd.Add("RandCheck", func() {
if ss.Config.Run.MPI {
tensormpi.RandCheck(ss.Comm) // prints error message
}
})
/////////////////////////////////////////////
// Logging
man.GetLoop(etime.Test, etime.Epoch).OnEnd.Add("LogTestErrors", func() {
axon.LogTestErrors(&ss.Logs)
})
man.GetLoop(etime.Train, etime.Epoch).OnEnd.Add("PCAStats", func() {
trnEpc := man.Stacks[etime.Train].Loops[etime.Epoch].Counter.Cur
if ss.Config.Run.PCAInterval > 0 && trnEpc%ss.Config.Run.PCAInterval == 0 {
if ss.Config.Run.MPI {
ss.Logs.MPIGatherTableRows(etime.Analyze, etime.Trial, ss.Comm)
}
axon.PCAStats(ss.Net, &ss.Logs, &ss.Stats)
ss.Logs.ResetLog(etime.Analyze, etime.Trial)
}
})
man.AddOnEndToAll("Log", ss.Log)
axon.LooperResetLogBelow(man, &ss.Logs)
man.GetLoop(etime.Train, etime.Trial).OnEnd.Add("LogAnalyze", func() {
trnEpc := man.Stacks[etime.Train].Loops[etime.Epoch].Counter.Cur
if (ss.Config.Run.PCAInterval > 0) && (trnEpc%ss.Config.Run.PCAInterval == 0) {
ss.Log(etime.Analyze, etime.Trial)
}
})
man.GetLoop(etime.Train, etime.Run).OnEnd.Add("RunStats", func() {
ss.Logs.RunStats("PctCor", "FirstZero", "LastZero")
})
// Save weights to file, to look at later
man.GetLoop(etime.Train, etime.Run).OnEnd.Add("SaveWeights", func() {
ctrString := ss.Stats.PrintValues([]string{"Run", "Epoch"}, []string{"%03d", "%05d"}, "_")
axon.SaveWeightsIfConfigSet(ss.Net, ss.Config.Log.SaveWts, ctrString, ss.Stats.String("RunName"))
})
////////////////////////////////////////////
// GUI
if !ss.Config.GUI {
if ss.Config.Log.NetData {
man.GetLoop(etime.Test, etime.Trial).Main.Add("NetDataRecord", func() {
ss.GUI.NetDataRecord(ss.ViewUpdate.Text)
})
}
} else {
axon.LooperUpdateNetView(man, &ss.ViewUpdate, ss.Net, ss.NetViewCounters)
axon.LooperUpdatePlots(man, &ss.GUI)
}
if ss.Config.Debug {
mpi.Println(man.DocString())
}
ss.Loops = man
}
// ApplyInputs applies input patterns from given environment.
// It is good practice to have this be a separate method with appropriate
// args so that it can be used for various different contexts
// (training, testing, etc).
func (ss *Sim) ApplyInputs() {
ctx := &ss.Context
net := ss.Net
ev := ss.Envs.ByMode(ctx.Mode).(*env.FixedTable)
lays := net.LayersByType(axon.InputLayer, axon.TargetLayer)
net.InitExt(ctx)
for di := uint32(0); di < ctx.NetIndexes.NData; di++ {
ev.Step()
// note: must save env state for logging / stats due to data parallel re-use of same env
ss.Stats.SetStringDi("TrialName", int(di), ev.TrialName.Cur)
for _, lnm := range lays {
ly := ss.Net.AxonLayerByName(lnm)
pats := ev.State(ly.Nm)
if pats != nil {
ly.ApplyExt(ctx, di, pats)
}
}
}
net.ApplyExts(ctx) // now required for GPU mode
}
// NewRun intializes a new run of the model, using the TrainEnv.Run counter
// for the new run value
func (ss *Sim) NewRun() {
ctx := &ss.Context
ss.InitRandSeed(ss.Loops.GetLoop(etime.Train, etime.Run).Counter.Cur)
ss.Envs.ByMode(etime.Train).Init(0)
ss.Envs.ByMode(etime.Test).Init(0)
ctx.Reset()
ctx.Mode = etime.Train
ss.Net.InitWts(ctx)
ss.InitStats()
ss.StatCounters(0)
ss.Logs.ResetLog(etime.Train, etime.Epoch)
ss.Logs.ResetLog(etime.Test, etime.Epoch)
}
// TestAll runs through the full set of testing items
func (ss *Sim) TestAll() {
ss.Envs.ByMode(etime.Test).Init(0)
ss.Loops.ResetAndRun(etime.Test)
ss.Loops.Mode = etime.Train // Important to reset Mode back to Train because this is called from within the Train Run.
}
/////////////////////////////////////////////////////////////////////////
// Pats
func (ss *Sim) ConfigPats() {
dt := ss.Pats
dt.SetMetaData("name", "TrainPats")
dt.SetMetaData("desc", "Training patterns")
dt.AddStringColumn("Name")
dt.AddFloat64TensorColumn("Input", []int{5, 5}, "Y", "X")
dt.AddFloat64TensorColumn("Output", []int{5, 5}, "Y", "X")
dt.SetNumRows(24)
patgen.PermutedBinaryMinDiff(dt.Columns[1].(*tensor.Float32), 6, 1, 0, 3)
patgen.PermutedBinaryMinDiff(dt.Columns[2].(*tensor.Float32), 6, 1, 0, 3)
dt.SaveCSV("random_5x5_24_gen.tsv", table.Tab, table.Headers)
}
func (ss *Sim) OpenPats() {
dt := ss.Pats
dt.SetMetaData("name", "TrainPats")
dt.SetMetaData("desc", "Training patterns")
err := dt.OpenCSV("random_5x5_24.tsv", table.Tab)
if err != nil {
log.Println(err)
}
}
////////////////////////////////////////////////////////////////////////////////////////////
// Stats
// InitStats initializes all the statistics.
// called at start of new run
func (ss *Sim) InitStats() {
ss.Stats.SetFloat("UnitErr", 0.0)
ss.Stats.SetFloat("CorSim", 0.0)
ss.Stats.SetString("TrialName", "")
ss.Logs.InitErrStats() // inits TrlErr, FirstZero, LastZero, NZero
}
// StatCounters saves current counters to Stats, so they are available for logging etc
// Also saves a string rep of them for ViewUpdate.Text
func (ss *Sim) StatCounters(di int) {
ctx := &ss.Context
mode := ctx.Mode
ss.Loops.Stacks[mode].CtrsToStats(&ss.Stats)
// always use training epoch..
trnEpc := ss.Loops.Stacks[etime.Train].Loops[etime.Epoch].Counter.Cur
ss.Stats.SetInt("Epoch", trnEpc)
trl := ss.Stats.Int("Trial")
ss.Stats.SetInt("Trial", trl+di)
ss.Stats.SetInt("Di", di)
ss.Stats.SetInt("Cycle", int(ctx.Cycle))
ss.Stats.SetString("TrialName", ss.Stats.StringDi("TrialName", di))
}
func (ss *Sim) NetViewCounters(tm etime.Times) {
if ss.ViewUpdate.View == nil {
return
}
di := ss.ViewUpdate.View.Di
if tm == etime.Trial {
ss.TrialStats(di) // get trial stats for current di
}
ss.StatCounters(di)
ss.ViewUpdate.Text = ss.Stats.Print([]string{"Run", "Epoch", "Trial", "Di", "TrialName", "Cycle", "UnitErr", "TrlErr", "CorSim"})
}
// TrialStats computes the trial-level statistics.
// Aggregation is done directly from log data.
func (ss *Sim) TrialStats(di int) {
out := ss.Net.AxonLayerByName("Output")
ss.Stats.SetFloat("CorSim", float64(out.Values[di].CorSim.Cor))
ss.Stats.SetFloat("UnitErr", out.PctUnitErr(&ss.Context)[di])
if ss.Stats.Float("UnitErr") > 0 {
ss.Stats.SetFloat("TrlErr", 1)
} else {
ss.Stats.SetFloat("TrlErr", 0)
}
}
//////////////////////////////////////////////////////////////////////////////
// Logging
func (ss *Sim) ConfigLogs() {
ss.Stats.SetString("RunName", ss.Params.RunName(0)) // used for naming logs, stats, etc
ss.Logs.AddCounterItems(etime.Run, etime.Epoch, etime.Trial, etime.Cycle)
ss.Logs.AddStatIntNoAggItem(etime.AllModes, etime.Trial, "Di")
ss.Logs.AddStatStringItem(etime.AllModes, etime.AllTimes, "RunName")
ss.Logs.AddStatStringItem(etime.AllModes, etime.Trial, "TrialName")
ss.Logs.AddStatAggItem("CorSim", etime.Run, etime.Epoch, etime.Trial)
ss.Logs.AddStatAggItem("UnitErr", etime.Run, etime.Epoch, etime.Trial)
ss.Logs.AddErrStatAggItems("TrlErr", etime.Run, etime.Epoch, etime.Trial)
ss.Logs.AddCopyFromFloatItems(etime.Train, []etime.Times{etime.Epoch, etime.Run}, etime.Test, etime.Epoch, "Tst", "CorSim", "UnitErr", "PctCor", "PctErr")
ss.Logs.AddPerTrlMSec("PerTrlMSec", etime.Run, etime.Epoch, etime.Trial)
layers := ss.Net.LayersByType(axon.SuperLayer, axon.CTLayer, axon.TargetLayer)
axon.LogAddDiagnosticItems(&ss.Logs, layers, etime.Train, etime.Epoch, etime.Trial)
axon.LogInputLayer(&ss.Logs, ss.Net, etime.Train)
axon.LogAddPCAItems(&ss.Logs, ss.Net, etime.Train, etime.Run, etime.Epoch, etime.Trial)
ss.Logs.AddLayerTensorItems(ss.Net, "Act", etime.Test, etime.Trial, "InputLayer", "TargetLayer")
ss.Logs.PlotItems("CorSim", "PctCor", "FirstZero", "LastZero")
ss.Logs.CreateTables()
ss.Logs.SetContext(&ss.Stats, ss.Net)
// don't plot certain combinations we don't use
ss.Logs.NoPlot(etime.Train, etime.Cycle)
ss.Logs.NoPlot(etime.Test, etime.Run)
// note: Analyze not plotted by default
ss.Logs.SetMeta(etime.Train, etime.Run, "LegendCol", "RunName")
}
// Log is the main logging function, handles special things for different scopes
func (ss *Sim) Log(mode etime.Modes, time etime.Times) {
ctx := &ss.Context
if mode != etime.Analyze {
ctx.Mode = mode // Also set specifically in a Loop callback.
}
if ss.Config.Run.MPI && time == etime.Epoch { // gather data for trial level at epoch
ss.Logs.MPIGatherTableRows(mode, etime.Trial, ss.Comm)
}
dt := ss.Logs.Table(mode, time)
if dt == nil {
return
}
row := dt.Rows
switch {
case time == etime.Cycle:
return
case time == etime.Trial:
for di := 0; di < int(ctx.NetIndexes.NData); di++ {
ss.TrialStats(di)
ss.StatCounters(di)
ss.Logs.LogRowDi(mode, time, row, di)
}
return // don't do reg below
}
ss.Logs.LogRow(mode, time, row) // also logs to file, etc
}
////////////////////////////////////////////////////////////////////////////////////////////
// Gui
// ConfigGUI configures the Cogent Core GUI interface for this simulation.
func (ss *Sim) ConfigGUI() {
title := "Axon Random Associator"
ss.GUI.MakeBody(ss, "ra25", title, `This demonstrates a basic Axon model. See <a href="https://github.com/emer/emergent">emergent on GitHub</a>.</p>`)
ss.GUI.CycleUpdateInterval = 10
nv := ss.GUI.AddNetView("NetView")
nv.Params.MaxRecs = 300
nv.SetNet(ss.Net)
ss.ViewUpdate.Config(nv, etime.Phase, etime.Phase)
ss.GUI.ViewUpdate = &ss.ViewUpdate
nv.SceneXYZ().Camera.Pose.Pos.Set(0, 1, 2.75) // more "head on" than default which is more "top down"
nv.SceneXYZ().Camera.LookAt(math32.Vec3(0, 0, 0), math32.Vec3(0, 1, 0))
ss.GUI.AddPlots(title, &ss.Logs)
ss.GUI.Body.AddAppBar(func(tb *core.Toolbar) {
ss.GUI.AddToolbarItem(tb, egui.ToolbarItem{Label: "Init", Icon: icons.Update,
Tooltip: "Initialize everything including network weights, and start over. Also applies current params.",
Active: egui.ActiveStopped,
Func: func() {
ss.Init()
ss.GUI.UpdateWindow()
},
})
ss.GUI.AddLooperCtrl(tb, ss.Loops, []etime.Modes{etime.Train, etime.Test})
////////////////////////////////////////////////
core.NewSeparator(tb)
ss.GUI.AddToolbarItem(tb, egui.ToolbarItem{Label: "Reset RunLog",
Icon: icons.Reset,
Tooltip: "Reset the accumulated log of all Runs, which are tagged with the ParamSet used",
Active: egui.ActiveAlways,
Func: func() {
ss.Logs.ResetLog(etime.Train, etime.Run)
ss.GUI.UpdatePlot(etime.Train, etime.Run)
},
})
////////////////////////////////////////////////
core.NewSeparator(tb)
ss.GUI.AddToolbarItem(tb, egui.ToolbarItem{Label: "New Seed",
Icon: icons.Add,
Tooltip: "Generate a new initial random seed to get different results. By default, Init re-establishes the same initial seed every time.",
Active: egui.ActiveAlways,
Func: func() {
ss.RandSeeds.NewSeeds()
},
})
ss.GUI.AddToolbarItem(tb, egui.ToolbarItem{Label: "README",
Icon: "file-markdown",
Tooltip: "Opens your browser on the README file that contains instructions for how to run this model.",
Active: egui.ActiveAlways,
Func: func() {
core.TheApp.OpenURL("https://github.com/emer/axon/blob/master/examples/mpi/README.md")
},
})
})
ss.GUI.FinalizeGUI(false)
if ss.Config.Run.GPU {
// vgpu.Debug = ss.Config.Debug // when debugging GPU..
ss.Net.ConfigGPUwithGUI(&ss.Context) // must happen after gui or no gui
core.TheApp.AddQuitCleanFunc(func() {
ss.Net.GPU.Destroy()
})
}
}
func (ss *Sim) RunGUI() {
ss.Init()
ss.ConfigGUI()
ss.GUI.Body.RunMainWindow()
}
func (ss *Sim) RunNoGUI() {
if ss.Config.Params.Note != "" {
mpi.Printf("Note: %s\n", ss.Config.Params.Note)
}
if ss.Config.Log.SaveWts {
mpi.Printf("Saving final weights per run\n")
}
runName := ss.Params.RunName(ss.Config.Run.Run)
ss.Stats.SetString("RunName", runName) // used for naming logs, stats, etc
netName := ss.Net.Name()
if mpi.WorldRank() == 0 {
elog.SetLogFile(&ss.Logs, ss.Config.Log.Epoch, etime.Train, etime.Epoch, "epc", netName, runName)
elog.SetLogFile(&ss.Logs, ss.Config.Log.Run, etime.Train, etime.Run, "run", netName, runName)
elog.SetLogFile(&ss.Logs, ss.Config.Log.TestEpoch, etime.Test, etime.Epoch, "tst_epc", netName, runName)
}
// Special cases for mpi per-node saving of trial data
if ss.Config.Log.Trial {
fnm := elog.LogFilename(fmt.Sprintf("trl_%d", mpi.WorldRank()), netName, runName)
ss.Logs.SetLogFile(etime.Train, etime.Trial, fnm)
}
if ss.Config.Log.TestTrial {
fnm := elog.LogFilename(fmt.Sprintf("tst_trl_%d", mpi.WorldRank()), netName, runName)
ss.Logs.SetLogFile(etime.Test, etime.Trial, fnm)
}
netdata := ss.Config.Log.NetData
if netdata {
mpi.Printf("Saving NetView data from testing\n")
ss.GUI.InitNetData(ss.Net, 200)
}
ss.Init()
mpi.Printf("Running %d Runs starting at %d\n", ss.Config.Run.NRuns, ss.Config.Run.Run)
ss.Loops.GetLoop(etime.Train, etime.Run).Counter.SetCurMaxPlusN(ss.Config.Run.Run, ss.Config.Run.NRuns)
if ss.Config.Run.GPU {
ss.Net.ConfigGPUnoGUI(&ss.Context)
}
mpi.Printf("Set NThreads to: %d\n", ss.Net.NThreads)
ss.Loops.Run(etime.Train)
ss.Logs.CloseLogFiles()
if netdata {
ss.GUI.SaveNetData(ss.Stats.String("RunName"))
}
ss.Net.GPU.Destroy() // safe even if no GPU
ss.MPIFinalize()
}
////////////////////////////////////////////////////////////////////
// MPI code
// MPIInit initializes MPI
func (ss *Sim) MPIInit() {
mpi.Init()
var err error
ss.Comm, err = mpi.NewComm(nil) // use all procs
if err != nil {
log.Println(err)
ss.Config.Run.MPI = false
} else {
mpi.Printf("MPI running on %d procs\n", mpi.WorldSize())
}
}
// MPIFinalize finalizes MPI
func (ss *Sim) MPIFinalize() {
if ss.Config.Run.MPI {
mpi.Finalize()
}
}
// MPIWtFromDWt updates weights from weight changes, using MPI to integrate
// DWt changes across parallel nodes, each of which are learning on different
// sequences of inputs.
func (ss *Sim) MPIWtFromDWt() {
ctx := &ss.Context
if ss.Config.Run.MPI {
ss.Net.CollectDWts(ctx, &ss.AllDWts)
ss.Comm.AllReduceF32(mpi.OpSum, ss.AllDWts, nil) // in place
ss.Net.SetDWts(ctx, ss.AllDWts, mpi.WorldSize())
}
ss.Net.WtFromDWt(ctx)
}