-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathgraph.clj
More file actions
892 lines (718 loc) · 29.6 KB
/
Copy pathgraph.clj
File metadata and controls
892 lines (718 loc) · 29.6 KB
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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
(ns cortex.graph
"Several algorithms in cortex are simplified by using a simple directed graph structure. There
are at this point two different general classes of nodes and these are differentiated by
understanding which pass they take part in. All node's have a type and this type links
to a metadata multimethod which gives further information on the node. All nodes are functions
taking a map of arguments. Layers are functions which also have implicit input and output
arguments which correspond to the edges of the graph the layers attach to."
(:require [cortex.util :as util]
[clojure.set :as c-set]
[cortex.keyword-fn :as keyword-fn]
[cortex.buffer-initialization :as buf-init]
[clojure.core.matrix :as m]
[cortex.argument :as arg]))
(defn- edges
[graph]
(get graph :edges))
(defn- edges->map
[graph key-fn val-fn]
(->> (edges graph)
(group-by key-fn)
(map (fn [[k v]]
[k (map val-fn v)]))
(into {})))
(defn parent->child-map
[graph]
(edges->map graph first second))
(defn child->parent-map
[graph]
(edges->map graph second first))
(defmulti get-node-metadata
"Given that any node has a type member, return metadata on the node which
must contain at least an :arguments member listing the arguments to the node."
:type)
(defmethod get-node-metadata :default [node] {})
(defn get-node-argument-keys
[node]
(->> (get-node-metadata node)
:arguments
keys))
(defn get-node-metadata-arguments
[node]
(->> (get-node-metadata node)
:arguments
(map (fn [[k v]]
(assoc v :key k)))))
(defn get-node-argument
[node arg-key]
(let [learn-atten (get node :learning-attenuation 1.0)
non-trainable? (get node :non-trainable? false)
node-defaults (:arguments (get-node-metadata node))
default-arg (get node-defaults arg-key)
default-arg (assoc default-arg :key arg-key)
arg (util/deep-merge default-arg (get node arg-key))]
(when-not default-arg
(throw (ex-info (str "Invalid node argument: " arg-key)
{:node node
:arg arg-key
:accepted-args (get (get-node-metadata node) :arguments)})))
(if (or (zero? (get arg :learning-attenuation learn-atten))
non-trainable?)
(assoc arg :gradients? false)
arg)))
(defn get-node-arguments
"Get the node arguments 'before' being merged with the node
buffers."
[node]
(->> (get-node-argument-keys node)
(map #(get-node-argument node %))))
(defn empty-graph
"Create an empty graph, which is stored as a map of:
{:edges [] adjacency list of [id id]
:id->node-map {} each node has an id and a type
:buffers {} parameter buffers, map of id->{:buffer data :gradient gradient}
:streams {} stream-name -> shape-descriptor. Streams act as roots of the graph.
}"
[]
{:nodes {}
:edges []
:buffers {}
:streams {}})
(defn get-node
[graph node-id]
(let [retval (get-in graph [:nodes node-id])]
(when-not retval
(throw (ex-info "Failed to find node:"
{:node-id node-id
:nodes (keys (get graph :nodes))})))
retval))
(defn- get-or-create-node-id
"Generate an id for this node."
[graph node]
(if-let [existing-id (get node :id)]
(do
(when-let [existing-node (get-in graph [:nodes existing-id])]
(throw (ex-info "Duplicate id detected in graph:"
{:new-node node
:existing-node existing-node})))
node)
(assoc node :id (util/generate-id (name (get node :type))
(set (keys (get graph :nodes)))))))
(defn add-node
"Add a node to the graph with a list of predecessors. If the node has no id one will
be generated; if it does and it is not unique and exception will be thrown.
If any of the predecessors does not exist an error will be thrown. Returns a pair
of [graph node-id]"
[graph node predecessor-id-seq]
(when-not (contains? graph :nodes)
(throw (ex-info "nil graph in add-node"
{:graph graph
:node node})))
(when-not (every? (get graph :nodes) predecessor-id-seq)
(throw (ex-info "Failed to find all predecessor id's in graph"
{:id-seq predecessor-id-seq
:missing-ids (remove (get graph :nodes) predecessor-id-seq)
:existing-ids (vec (keys (get graph :nodes)))})))
(let [node (get-or-create-node-id graph node)]
[(-> graph
(assoc-in [:nodes (get node :id)] node)
(update :edges #(concat %
(map vector
predecessor-id-seq
(repeat (get node :id))))))
(get node :id)]))
(defn- recur-remove-node
[p->c-map graph node-id]
(let [graph (reduce (partial recur-remove-node p->c-map)
graph
(get p->c-map node-id))
buffer-ids (->> (get-node-arguments (get-node graph node-id))
(filter #(= :parameter (get % :type)))
(map :buffer-id))]
(-> graph
(update :edges #(remove (fn [[p c]]
(or (= p node-id)
(= c node-id)))
%))
(update :buffers #(apply dissoc % buffer-ids))
(update :nodes dissoc node-id))))
(defn remove-node
"Remove a node, its buffers and all children from the graph."
[graph node-id]
(recur-remove-node (parent->child-map graph) graph node-id))
(defn remove-children
"Remove all children of a node (and there children, recursively)."
[graph parent-node-id]
(let [p->c-map (parent->child-map graph)]
(reduce (partial recur-remove-node p->c-map)
graph
(get p->c-map parent-node-id))))
(defn any-trainable-arguments?
[node]
(->> (get-node-arguments node)
(filter :gradients?)
seq))
(defmulti build-node
"Callback called when the node is added to the graph. Note that the node at this point
is not located in the graph. Also note that any parameter arguments are generated
in a separate step. This is simply a translation from node->node called during
the add-node step. Predessors have been built, successors have not been built."
(fn [graph node predecessor-ids successor-ids]
(get node :type)))
(declare node->output-dimensions)
(defn clear-dimension-identifiers
"Remove identifiers on dimensions. This is used as a general tool when copying input
dimensions to output dimensions. The identifiers on the input dimensions should be removed."
[dims]
(dissoc dims :id :stream))
(defn ensure-single-output-dimensions
"Ensure a node has one output and get it's output dimensions."
[previous node]
(let [output-dims (node->output-dimensions previous)]
(when-not (or (= 1 (count output-dims))
(= 1 (count (filter #(= (get node :id)
(get % :id))
output-dims))))
(throw (ex-info "Previous node has multiple output dimensions pertaining to this node."
{:previous previous
:node node
:output-dimensions output-dims})))
(clear-dimension-identifiers (first output-dims))))
(defn carry-input-dims-forward
[previous item]
(assoc item :input-dimensions [(assoc
(ensure-single-output-dimensions previous item)
:id (get previous :id))]))
(defn carry-io-dims-forward
[previous item]
(let [input-dims (ensure-single-output-dimensions previous item)]
;;For single input nodes it is still important to note the parent this input came from.
(assoc item :input-dimensions [(assoc input-dims :id (get previous :id))]
;;But for single outputs the source is clear.
:output-dimensions [input-dims])))
(defn ensure-single-parent
[graph node previous-id-seq]
(when-not (= 1 (count previous-id-seq))
(throw (ex-info "Node only takes a single node of input."
{:node node
:previous previous-id-seq})))
(get-node graph (first previous-id-seq)))
(defn- default-build-fn
[graph node predecessor-ids successor-ids]
(let [previous (ensure-single-parent graph node predecessor-ids)]
(carry-io-dims-forward previous node)))
;;lots of nodes do not need to be built.
(defmethod build-node :default
[& args]
(apply default-build-fn args))
(defn stream-descriptor
"Shape descriptors are used to describe streams. Currently there are two types
of streams, a multi-channeled input like an image and a single channel input like
a vector of floats."
([channels height width]
{:channels channels
:height height
:width width})
([width]
(stream-descriptor 1 1 width)))
(defn shape->stream-descriptor
[shape]
(condp = (count shape)
1 (stream-descriptor (first shape))
3 (let [[channels height width] shape]
(stream-descriptor channels height width))))
(defn stream-descriptor->size
^long [shape-desc]
(long (apply * (vals shape-desc))))
(defn add-stream
[graph stream-name shape-descriptor]
(assoc-in graph [:streams stream-name] shape-descriptor))
(defn stream->descriptor
[graph stream-name]
(if-let [stream-shape (get-in graph [:streams stream-name])]
stream-shape
(throw (ex-info "Failed to find stream in graph"
{:stream stream-name
:available-streams (keys (get graph :streams))}))))
(defn stream->size
[graph stream-name]
(stream-descriptor->size (stream->descriptor graph stream-name)))
(defn get-node
[graph node-id]
(if-let [node (get-in graph [:nodes node-id])]
node
(throw (ex-info (str "Failed to find node: " node-id (nil? node-id))
{:node-id node-id
:nodes (keys (:nodes graph))}))))
(defn- get-or-create-node-id
"Generate an id for this node."
[graph node]
(if-let [existing-id (get node :id)]
(do
(when-let [existing-node (get-in graph [:nodes existing-id])]
(throw (ex-info "Duplicate id detected in graph:"
{:new-node node
:existing-node existing-node})))
node)
(assoc node :id (util/generate-id (name (get node :type))
(set (keys (get graph :nodes)))))))
(defn- edges
[graph]
(get graph :edges))
(defn- parent-seq
[graph]
(map first (edges graph)))
(defn- child-seq
[graph]
(map second (edges graph)))
(defn- parent-set
[graph]
(-> (parent-seq graph)
set))
(defn- child-set
[graph]
(-> (child-seq graph)
set))
(defn- set->ordered-vec
[item-set item-seq]
(->> (filter item-set item-seq)
distinct
vec))
(defn roots
[graph]
(-> (c-set/difference (parent-set graph) (child-set graph))
(set->ordered-vec (parent-seq graph))))
(defn leaves
[graph]
(-> (c-set/difference (child-set graph) (parent-set graph))
(set->ordered-vec (child-seq graph))))
(defn dfs-seq
"Get a sequence of ids in dfs order."
[graph]
(let [p->c-map (-> (parent->child-map graph)
(assoc :roots (roots graph)))]
(->> (tree-seq #(contains? p->c-map %)
#(get p->c-map %)
:roots)
(drop 1)
;;Account for cases where the graph has multiple roots. by taking the last occurance
;;of a multiply-occuring node. this ensures that a child will not get visited until
;;after every parent has been visited.
reverse
distinct
reverse)))
(defn relative-dfs-seq
[graph node-id]
(let [p->c-map (parent->child-map graph)]
(tree-seq #(contains? p->c-map %)
#(get p->c-map %)
node-id)))
(defn leaf->root-seq
[graph node-id]
(let [c->p-map (-> (child->parent-map graph)
(assoc :roots [node-id]))]
(->> (tree-seq #(contains? c->p-map %)
#(get c->p-map %)
:roots)
(drop 1)
(reverse)
distinct
(reverse))))
(defn create-node-dimensions
"Create a node dimension map. Dimensions are a map of
:channels :height :width with the last item (width) being the most
rapidly changing index and channels being the least rapidly changing index."
([channels height width]
{:channels channels
:height height
:width width})
([width] (create-node-dimensions 1 1 width)))
(defn- node-inline-data->dims
[node key-stem]
(let [retval
(if-let [width (get node (keyword (str key-stem "-width")))]
[{:channels (get node (keyword (str key-stem "-channels")))
:height (get node (keyword (str key-stem "-height")))
:width width}]
[{:channels 1
:height 1
:width (get node (keyword (str key-stem "-size")))}])]
(when-not (every? number? (vals (first retval)))
(throw (ex-info "Failed to convert node's built information into dimensions"
{:node node
:result retval
})))
retval))
(defn node->input-dimensions
"Return a list of dimensions in order, one for every input of the node."
[node]
(or (get node :input-dimensions)
(node-inline-data->dims node "input")))
(defn node->output-dimensions
"Return a list of dimensions in order, one for every input of the node."
[node]
(or (get node :output-dimensions)
(node-inline-data->dims node "output")))
(defn dimensions->dims-with-ids
"When there are only 1 of dimensions and ids then the id of the first dimension
is unambiguous and set into the dimension entry. When there are more ensure
that each id has an unambiguous mapping to a dimension."
[dims-seq id-seq]
(when-not (= (count dims-seq) (count id-seq))
(throw (ex-info "Dimensions vector and id sequence mismatch"
{:dims-sequence dims-seq
:id-sequence id-seq})))
(if (= 1 (count dims-seq))
(update (vec dims-seq) 0
assoc :id (first id-seq))
;;Error check the dimensions that each id in the id seq appears in them
;;and that id's are not repeated.
(do
(when-not (every? #(contains? % :id)
dims-seq)
(throw (ex-info "Every dimension entry must contain a mapping id"
{:dimensions dims-seq})))
(let [dims-ids (set (map :id dims-seq))
id-set (set id-seq)]
(when-not (= (count id-seq)
(count id-set))
(throw (ex-info "Duplicate ids detected"
{:id-set id-set
:id-seq id-seq
:dims-seq dims-seq})))
(when-not (= id-set dims-ids)
(throw (ex-info "id set differs from dimension id set"
{:id-seq id-seq
:dimension-seq dims-seq})))
(vec dims-seq)))))
(defn node->output-dimension
[node]
(let [output-dims (node->output-dimensions node)]
(when-not (= 1 (count output-dims))
(throw (ex-info "Node has multiple outputs thus canonical dimension is ambiguous."
{:node node
:output-dims output-dims})))
(first output-dims)))
(defn node->input-dimension
[node]
(let [input-dims (node->input-dimensions node)]
(when-not (= 1 (count input-dims))
(throw (ex-info "Node has multiple inputs thus canonical dimension is ambiguous"
{:node node
:input-dims input-dims})))
(first input-dims)))
(defn dimensions->size
^long [dims]
(apply * (vals (clear-dimension-identifiers dims))))
(defn dimensions->shape
"Return a vector of integers with the highest indexes changing more rapidly than the
lower indexes...In other words the dimenion tuple is in big-endian order."
[dims]
(mapv dims [:channels :height :width]))
(defn- dims-vec->size
^long [node dims-vec direction]
(when-not (= 1 (count dims-vec))
(throw (ex-info "Cannot convert to size, node has multiple or zero dimensions"
{:node node
:direction direction})))
(dimensions->size (first dims-vec)))
(defn node->input-size
"Given a node, ensure it has 1 input dimension and call dimension->size for that dim."
^long [node]
(dims-vec->size node (node->input-dimensions node) :input))
(defn node->output-size
"Given a node, ensure it has 1 input dimension and call dimension->size for that dim."
^long [node]
(dims-vec->size node (node->output-dimensions node) :output))
(defn- do-build-graph
[c->p-map p->c-map graph node-id]
(let [node (build-node graph (get-node graph node-id)
(get c->p-map node-id) (get p->c-map node-id))]
(update graph :nodes assoc node-id node)))
(defn build-graph
"Propagate size information (input/output sizes) through the graph in dfs order."
[graph]
(let [c->p-map (child->parent-map graph)
p->c-map (parent->child-map graph)]
(reduce (partial do-build-graph c->p-map p->c-map)
graph
(dfs-seq graph))))
(defn update-node
[graph node-id update-fn]
(when-not (contains? (get graph :nodes) node-id)
(throw (ex-info "Update failed to find node"
{:node-id node-id})))
(update-in graph [:nodes node-id] update-fn))
(defmulti get-argument-shape
"Get the expected shape of an argument"
(fn [graph node argument]
(get argument :type)))
(defmethod get-argument-shape :stream
[graph node argument]
(if-let [retval (stream->size graph (get argument :stream))]
[(long retval)]
(throw (ex-info "Failed to find stream size for argument"
{:stream (get argument :stream)
:streams (keys stream->size)}))))
(defmethod get-argument-shape :node-output
[graph node argument]
(let [target-node (get-node graph (get argument :node-id))]
(if-let [retval (node->output-size target-node)]
[(long retval)]
(throw (ex-info "Failed to find node output size"
{:argument argument
:nodes (keys (get graph :nodes))})))))
(defmethod get-argument-shape :node-argument
[graph node argument]
(let [target-node (get-node graph (get argument :node-id))
target-arg (get-node-argument target-node (get argument :argument))]
(when-not (and target-node target-arg)
(throw (ex-info "Failed to find node or node argument"
{:node target-node
:argument target-arg
:src-argument argument})))
(get-argument-shape graph target-node target-arg)))
(defmethod get-argument-shape :stream-augmentation
[graph node argument]
(throw (ex-info "Cannot get shape of stream augments without actually augmenting stream"
{:argument argument})))
(defmethod get-argument-shape :parameter
[graph node argument]
(try
(keyword-fn/call-keyword-fn (get argument :shape-fn)
graph node argument)
(catch Throwable e
(throw (ex-info "Failed to resolve and call shape function"
{:node-id (get node :id)
:argument argument
:error e})))))
(defmulti initialize-graph-parameter-buffer
"Initialize a graph parameter buffer"
(fn
[graph node argument shape initialization]
(get initialization :type)))
(defmethod initialize-graph-parameter-buffer :default
[graph node argument shape initialization]
(buf-init/initialize-buffer (assoc initialization :shape shape)))
(defn- smart-shape-compare
[shape1 shape2]
(= (drop-while #(= 1 %) shape1)
(drop-while #(= 1 %) shape2)))
(defn generate-parameter-argument-buffer
"Given a parameter argument generate it's buffer."
[node-id graph argument]
(let [node (get-node graph node-id)
expected-shape (get-argument-shape graph node argument)]
(if-let [existing-buffer (get-in graph [:buffers (get argument :buffer-id) :buffer])]
(do
(when-not (smart-shape-compare expected-shape (m/shape existing-buffer))
(throw (ex-info "Existing buffer does not match expected shape"
{:node-id node-id
:existing-shape (m/shape existing-buffer)
:expected-shape expected-shape
:node node})))
graph)
(let [param-buffer-id (util/generate-id (str (name (get node :id))
"-"
(name (get argument :key)))
(set (keys (get graph :buffers))))
param-buffer
(if-let [user-supplied-buffer (get argument :buffer)]
(let [user-shape (m/shape user-supplied-buffer)]
(when-not (= user-shape expected-shape)
(throw (ex-info "User supplied buffer is incorrect shape"
{:user-buffer-shape user-shape
:expected-shape expected-shape})))
user-supplied-buffer)
(initialize-graph-parameter-buffer graph node argument
expected-shape
(get argument :initialization)))]
(-> graph
(assoc-in [:buffers param-buffer-id :buffer]
param-buffer)
(update-in [:nodes node-id (get argument :key)]
dissoc :buffer)
(update-in [:nodes node-id (get argument :key)]
assoc :buffer-id param-buffer-id))))))
(defn generate-node-parameter-buffers
[graph node]
(->> node
get-node-arguments
(filter #(= :parameter (get % :type)))
(reduce (partial generate-parameter-argument-buffer (get node :id))
graph)))
(defn generate-parameters
"Go through all the nodes in the graph and generate any parameter buffers
that do not already exist. Returns a new graph."
[graph]
(reduce
(fn [graph id]
(generate-node-parameter-buffers graph (get-node graph id)))
graph
(dfs-seq graph)))
(defn get-stream-augmentation-arguments
[graph]
(->> (dfs-seq graph)
(map #(get-node graph %))
(mapcat get-node-arguments)
(filter #(= :stream-augmentation (get % :type)))))
(defn perform-stream-augmentations
[stream-aug-args stream-map]
(->> stream-aug-args
(map (fn [{:keys [stream augmentation] :as arg}]
(when-not (contains? stream-map stream)
(throw (ex-info "Failed to find stream for augmentation"
{:argument arg
:streams (vec (keys stream-map))})))
(try
(let [augment-result (keyword-fn/call-keyword-fn augmentation
(get stream-map stream))
augment-data (if (get arg :datatype)
{:datatype (get arg :datatype)
:data augment-result}
augment-result)]
[(arg/augmented-stream-arg->id arg) augment-data])
(catch Throwable e
(throw (ex-info "Failed to augment stream"
{:argument arg
:error e}))))))
(into {})
(merge stream-map)))
(defn augment-streams
"Augment the streams in the map and return a new map of data."
[graph stream-map]
(-> (get-stream-augmentation-arguments graph)
(perform-stream-augmentations stream-map)))
(defmulti resolve-argument
"Resolve a particular argument returning a map containing
at least :buffer if not both :buffer and :gradient."
(fn [graph node argument stream-map node-id->output-map]
(get argument :type)))
(defmethod resolve-argument :stream
[graph node argument stream-map node-id->output-map]
(if-let [buffer (get stream-map (get argument :stream))]
buffer
(throw (ex-info "Failed to resolve argument"
{:streams (keys stream-map)
:argument argument}))))
(defmethod resolve-argument :parameter
[graph node argument stream-map node-id->output-map]
;;Rather than get-in here we use a function style lookup because
;;the buffers in the graph 'may' actually be a function instead
;;of a map to enable runtime systems to provide a minimal set of
;;parameters.
(if-let [buffer ((get graph :buffers) (get argument :buffer-id))]
buffer
(throw (ex-info "Failed to resolve argument"
{:argument argument
:buffers (keys (get graph :buffers))}))))
(defmethod resolve-argument :node-output
[graph node argument stream-map node-id->output-map]
(if-let [buffer (get node-id->output-map (get argument :node-id))]
buffer
(throw (ex-info "Failed to resolve argument"
{:argument argument
:node-outputs (keys node-id->output-map)}))))
(defmethod resolve-argument :node-argument
[graph node {:keys [node-id] :as argument} stream-map node-id->output-map]
(let [target-node (get-node graph node-id)
target-arg (get-node-argument target-node (get argument :argument))]
(resolve-argument graph target-node target-arg stream-map node-id->output-map)))
(defmethod resolve-argument :stream-augmentation
[graph node argument stream-map node-id->output-map]
(if-let [buffer (get stream-map (arg/augmented-stream-arg->id argument))]
buffer
(throw (ex-info "Failed to resolve argument"
{:argument argument
:streams (keys stream-map)}))))
(defn resolve-arguments
"Resolve the arguments to a particular node. It is expected the stream map contains the
augmented data if necessary. Note that for uniformity the values are returned without
modification. This means the the format of the stream map and the node->output-map must be
entries of the form of at least {:buffer data} instead of linking key directly to data. This
allows a uniform system both when doing auto-differentiation and when simply doing execution
because when doing back propagation the entries must link to both {:buffer
:gradient}."
[graph node stream-map node-id->output-map]
(->> (get-node-arguments node)
(mapv (fn [{:keys [key type] :as argument}]
[key (resolve-argument graph node argument
stream-map node-id->output-map)]))
(into {})))
(defn parameter-count
"Return the number of trainable and non-trainable parameters."
[graph]
(->> (get graph :buffers)
vals
(map (comp #(apply * %) m/shape :buffer))
(reduce +)))
(defn graph->nodes
"Return a list of all nodes in the graph in dfs order."
[graph]
(->> (dfs-seq graph)
(map #(get-node graph %))))
(defn get-parameter-buffer
[graph buffer-id]
(if-let [retval (get-in graph [:buffers buffer-id])]
retval
(throw (ex-info "Failed to find buffer for buffer id"
{:buffer-id buffer-id
:buffers (keys (get graph :buffers))}))))
(defmulti generate-stream-definitions
"A stream definition is a pair of [stream shape]. Some nodes can generate this assuming
they are built (normal loss terms for example) but most nodes cannot say anything useful
for this step."
(fn [graph node]
(get node :type)))
(defmethod generate-stream-definitions :default
[graph node]
[])
(defn generate-leaf-streams
"Given the graph datastructure, generate streams for stream bindings. Node's generate streams
based on their output size and possibly based on some internal state defined in the node such
as a combination of the stream they are bound to and another node's output size."
[graph]
(->> (leaves graph)
(map #(get-node graph %))
(mapcat (partial generate-stream-definitions graph))
(reduce (fn [graph [stream shape]]
(add-stream graph stream (shape->stream-descriptor shape)))
graph)))
(defn filter-graph
"Given a graph and a predicate produce a new graph with only functions that match
the predicate."
[graph pred]
(->> (get graph :nodes)
vals
(remove pred)
(map :id)
(reduce remove-node graph)))
(defn graph->required-streams
"Run through nodes of graph and keep track of streams encountered. Return the set of stream
names."
[graph]
(->> (get graph :nodes)
vals
(mapcat #(get-node-arguments %))
(filter #(= :stream (get % :type)))
(map :stream)
set))
(defn graph->output-node-ids
"Run through nodes of graph and identify nodes are either leaves or that are bound in
arguments. Only nodes that return truthy for predicate will be returned."
[graph pred]
(let [passing-leaves (->> (leaves graph)
(map #(get-node graph %))
(filter pred)
(map :id))
;;There is an inherent issue here in that losses (not loss gradients) are always
;;calculated on the cpu. This means we have to jump through hoops in order to get them
;;evaulated during training (to measure loss). This would be aleviated if loss terms
;;are made part of the graph.
passing-args (->> (get graph :nodes)
vals
(mapcat #(get-node-arguments %))
(filter #(= :node-output (get % :type)))
(map :node-id)
(filter (comp pred #(get-node graph %))))]
(set (concat passing-leaves passing-args))))