Skip to content

Commit 1241cfc

Browse files
authored
Merge pull request #679 from clab/nodes-separate-final
Separate nodes into different files Former-commit-id: 9606e2a
2 parents 693c5e4 + 786d3a2 commit 1241cfc

File tree

92 files changed

+5270
-4430
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+5270
-4430
lines changed

doc/source/code_style.rst

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,29 @@ Coding Tips
88
One of the most common things that one will want to do to modify DyNet is to add a new operation
99
to calculate a new function.
1010
You can find more information on how to do so at the end of the tutorial slides
11-
`here <http://phontron.com/slides/emnlp2016-dynet-tutorial-part1.pdf>`_.
11+
`here <http://phontron.com/slides/emnlp2016-dynet-tutorial-part1.pdf>`_ (note that some file
12+
names are old).
13+
14+
Taking a look at the existing operations in the ``nodes-XXX.h`` and ``nodes-XXX.cc`` files
15+
will be the best guide in creating new operations. Here are some fine-grained tips for
16+
those that want to dive into the process.
17+
18+
1. ``fx`` is a pointer to the (preallocated) location for the result
19+
of forward to be stored
20+
2. ``fx`` is not initialized, so after calling forward ``fx`` must contain the correct answer
21+
3. dEdxi MUST **ACCUMULATE** a result since multiple calls to forward may depend on
22+
the same ``x_i``. Even, e.g., Identity must be implemented as ``dEdx1 += dEdf``.
23+
4. scalars results of forward are placed in ``fx.v[0]``
24+
5. DyNet manages its own memory, not Eigen, and it is configured with the
25+
EIGEN_NO_MALLOC option. If you get an error about Eigen attempting to allocate
26+
memory, it is (probably) because of an implicit creation of a temporary variable.
27+
If you really do need a temporary variable, its capacity must be requested by
28+
Node::aux_storage_size
29+
30+
And here are some notes on debugging problems with new operations
31+
32+
1. fx is uninitialized when forward is called- are you relying on it being 0?
33+
2. dEdxi must accumulate (see point 3 above!)
1234

1335
Coding Practices
1436
----------------

dynet/CMakeLists.txt

Lines changed: 84 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
set(dynet_library_SRCS
44
aligned-mem-pool.cc
55
cfsm-builder.cc
6-
dynet.cc
76
deep-lstm.cc
87
devices.cc
98
dict.cc
109
dim.cc
10+
dynet.cc
1111
exec.cc
1212
expr.cc
1313
fast-lstm.cc
@@ -17,34 +17,51 @@ set(dynet_library_SRCS
1717
gru.cc
1818
hsm-builder.cc
1919
init.cc
20+
io.cc
2021
lstm.cc
2122
mem.cc
2223
model.cc
23-
nodes.cc
24-
nodes-common.cc
24+
nodes-activations.cc
25+
nodes-affinetransform.cc
26+
nodes-arith-const.cc
27+
nodes-arith-cwise.cc
28+
nodes-arith-scalar.cc
29+
nodes-arith-sum.cc
30+
nodes-arith-unary.cc
31+
nodes-concat.cc
32+
nodes-const.cc
2533
nodes-contract.cc
2634
nodes-conv.cc
2735
nodes-conv2d.cc
36+
nodes-dropout.cc
37+
nodes-flow.cc
38+
nodes-hinge.cc
39+
nodes-linalg.cc
40+
nodes-logsumexp.cc
41+
nodes-losses.cc
42+
nodes-matrixmultiply.cc
2843
nodes-maxpooling2d.cc
44+
nodes-minmax.cc
45+
nodes-moments.cc
46+
nodes-normalization.cc
47+
nodes-norms.cc
2948
nodes-pickneglogsoftmax.cc
30-
nodes-matrixmultiply.cc
31-
nodes-hinge.cc
32-
nodes-affinetransform.cc
49+
nodes-random.cc
50+
nodes-select.cc
3351
nodes-similarities.cc
34-
nodes-norms.cc
35-
nodes-unary-arith.cc
52+
nodes-softmaxes.cc
53+
nodes-trig.cc
3654
param-init.cc
3755
param-nodes.cc
3856
pretrain.cc
39-
rnn.cc
4057
rnn-state-machine.cc
58+
rnn.cc
4159
saxe-init.cc
4260
shadow-params.cc
4361
tensor.cc
4462
training.cc
4563
treelstm.cc
4664
weight-decay.cc
47-
io.cc
4865
)
4966
if(ENABLE_BOOST)
5067
list(APPEND dynet_library_SRCS mp.cc)
@@ -53,51 +70,99 @@ endif()
5370
# Headers:
5471
set(dynet_library_HDRS
5572
aligned-mem-pool.h
56-
cfsm-builder.h
57-
cudnn-ops.h
5873
c2w.h
59-
dynet.h
74+
cfsm-builder.h
75+
cuda-matrix-multiply.h
6076
cuda.h
77+
cudnn-ops.h
78+
deep-lstm.h
6179
devices.h
6280
dict.h
6381
dim.h
82+
dynet-helper.h
83+
dynet.h
84+
except.h
6485
exec.h
6586
expr.h
6687
fast-lstm.h
6788
functors.h
6889
globals.h
6990
gpu-kernels.h
7091
gpu-ops.h
92+
grad-check.h
7193
graph.h
7294
gru.h
7395
hsm-builder.h
7496
init.h
97+
io.h
7598
lstm.h
7699
mem.h
77100
model.h
78-
nodes.h
79101
nodes-contract.h
80102
nodes-conv.h
103+
nodes-macros.h
104+
nodes.h
81105
op-helper.h
106+
param-init.h
82107
param-nodes.h
108+
pretrain.h
83109
rnn-state-machine.h
84110
rnn.h
85111
saxe-init.h
86112
shadow-params.h
113+
sig.h
87114
simd-functors.h
115+
str-util.h
88116
tensor.h
89117
timing.h
90118
training.h
91119
treelstm.h
92-
except.h
93-
nodes-macros.h
94120
weight-decay.h
95-
io.h
96121
)
97122
if(ENABLE_BOOST)
98123
list(APPEND dynet_library_HDRS mp.h)
99124
endif()
100125

126+
set(dynet_gpu_SRCS
127+
cuda.cc
128+
cudnn-ops.cu
129+
gpu-ops.cu
130+
gpu-nodes-activations.cu
131+
gpu-nodes-affinetransform.cu
132+
gpu-nodes-arith-const.cu
133+
gpu-nodes-arith-cwise.cu
134+
gpu-nodes-arith-scalar.cu
135+
gpu-nodes-arith-sum.cu
136+
gpu-nodes-arith-unary.cu
137+
gpu-nodes-concat.cu
138+
gpu-nodes-const.cu
139+
gpu-nodes-contract.cu
140+
gpu-nodes-conv2d.cu
141+
gpu-nodes-conv.cu
142+
gpu-nodes-dropout.cu
143+
gpu-nodes-flow.cu
144+
gpu-nodes-hinge.cu
145+
gpu-nodes-linalg.cu
146+
gpu-nodes-logsumexp.cu
147+
gpu-nodes-losses.cu
148+
gpu-nodes-matrixmultiply.cu
149+
gpu-nodes-maxpooling2d.cu
150+
gpu-nodes-minmax.cu
151+
gpu-nodes-moments.cu
152+
gpu-nodes-normalization.cu
153+
gpu-nodes-norms.cu
154+
gpu-nodes-pickneglogsoftmax.cu
155+
gpu-nodes-random.cu
156+
gpu-nodes-select.cu
157+
gpu-nodes-similarities.cu
158+
gpu-nodes-softmaxes.cu
159+
gpu-nodes-trig.cu
160+
gpu-param-nodes.cu
161+
gpu-tensor.cu
162+
gpu-training.cu
163+
gpu-model.cu
164+
)
165+
101166
file(GLOB TEST_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} tests/*.cc)
102167
if (NOT MSVC)
103168
set(BUILD_SHARED_LIBS ON)
@@ -150,10 +215,10 @@ if(WITH_CUDA_BACKEND)
150215
list(APPEND CUDA_NVCC_FLAGS_DEBUG "--compiler-options \"/MDd\"")
151216
list(APPEND CUDA_NVCC_FLAGS_RELEASE "--compiler-options \"/MD\"")
152217
SET(CUDA_PROPAGATE_HOST_FLAGS OFF)
153-
cuda_add_library(gdynet ${dynet_library_SRCS} ${dynet_library_HDRS} cuda.cc cudnn-ops.cu gpu-ops.cu gpu-nodes.cu gpu-nodes-contract.cu gpu-nodes-conv.cu gpu-nodes-conv2d.cu gpu-nodes-maxpooling2d.cu gpu-param-nodes.cu gpu-tensor.cu gpu-training.cu gpu-model.cu gpu-nodes-pickneglogsoftmax.cu gpu-nodes-matrixmultiply.cu gpu-nodes-hinge.cu gpu-nodes-affinetransform.cu gpu-nodes-similarities.cu gpu-nodes-norms.cu gpu-nodes-unary-arith.cu)
218+
cuda_add_library(gdynet ${dynet_library_SRCS} ${dynet_library_HDRS} ${dynet_gpu_SRCS})
154219
else()
155220
SET(CUDA_PROPAGATE_HOST_FLAGS OFF)
156-
cuda_add_library(gdynet ${dynet_library_SRCS} ${dynet_library_HDRS} cuda.cc cudnn-ops.cu gpu-ops.cu gpu-nodes.cu gpu-nodes-contract.cu gpu-nodes-conv.cu gpu-nodes-conv2d.cu gpu-nodes-maxpooling2d.cu gpu-param-nodes.cu gpu-tensor.cu gpu-training.cu gpu-model.cu gpu-nodes-pickneglogsoftmax.cu gpu-nodes-matrixmultiply.cu gpu-nodes-hinge.cu gpu-nodes-affinetransform.cu gpu-nodes-similarities.cu gpu-nodes-norms.cu gpu-nodes-unary-arith.cu OPTIONS --compiler-options "-fPIC")
221+
cuda_add_library(gdynet ${dynet_library_SRCS} ${dynet_library_HDRS} ${dynet_gpu_SRCS} OPTIONS --compiler-options "-fPIC")
157222
endif()
158223
set_target_properties(gdynet PROPERTIES
159224
COMPILE_DEFINITIONS HAVE_CUDA)
@@ -172,4 +237,3 @@ if(WITH_CUDA_BACKEND)
172237
endif(WITH_CUDA_BACKEND)
173238

174239
# target_compile_features(dynet PRIVATE cxx_range_for)
175-

dynet/dynet.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "dynet/dynet.h"
22

33
#include "dynet/exec.h"
4-
#include "dynet/nodes.h"
54
#include "dynet/param-nodes.h"
65
#include "dynet/aligned-mem-pool.h"
76
#include "dynet/dynet-helper.h"

dynet/gpu-nodes-activations.cu

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This is a dummy file that contains the same content as nodes.cc but compiled
2+
// on CUDA
3+
#include "nodes-activations.cc"

dynet/gpu-nodes-arith-const.cu

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This is a dummy file that contains the same content as nodes.cc but compiled
2+
// on CUDA
3+
#include "nodes-arith-const.cc"

dynet/gpu-nodes-arith-cwise.cu

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This is a dummy file that contains the same content as nodes.cc but compiled
2+
// on CUDA
3+
#include "nodes-arith-cwise.cc"

dynet/gpu-nodes-arith-scalar.cu

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This is a dummy file that contains the same content as nodes.cc but compiled
2+
// on CUDA
3+
#include "nodes-arith-scalar.cc"

dynet/gpu-nodes-arith-sum.cu

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This is a dummy file that contains the same content as nodes.cc but compiled
2+
// on CUDA
3+
#include "nodes-arith-sum.cc"

dynet/gpu-nodes-arith-unary.cu

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This is a dummy file that contains the same content as nodes.cc but compiled
2+
// on CUDA
3+
#include "nodes-arith-unary.cc"

dynet/gpu-nodes-concat.cu

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This is a dummy file that contains the same content as nodes.cc but compiled
2+
// on CUDA
3+
#include "nodes-concat.cc"

0 commit comments

Comments
 (0)