Skip to content

Commit

Permalink
fix Eigen index type errors that strict compilers/some versions of Ei…
Browse files Browse the repository at this point in the history
…gen cause
  • Loading branch information
CHRIS DYER authored and CHRIS DYER committed Sep 15, 2018
1 parent b066cec commit 4948b5c
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 133 deletions.
6 changes: 3 additions & 3 deletions dynet/nodes-affinetransform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void AffineTransform::forward_dev_impl(const MyDevice & dev, const vector<const
tvec(fx).device(*dev.edevice) = tvec(*xs[0]);
} else {
#ifdef __CUDACC__
Eigen::array<int, 3> bcast; bcast[0] = 1; bcast[1] = fx.d[1]/xs[0]->d[1]; bcast[2] = fx.d.bd/xs[0]->d.bd;
Eigen::array<ptrdiff_t, 3> bcast = {1, fx.d[1]/xs[0]->d[1], fx.d.bd/xs[0]->d.bd};
tb<2>(fx).device(*dev.edevice) = tb<2>(*xs[0]).broadcast(bcast);
#else
DYNET_ARG_CHECK(xs[0]->d.bd == 1, "In AffineTransform, broadcasting over columns with mini-batched inputs is not implemented yet");
Expand Down Expand Up @@ -137,10 +137,10 @@ void AffineTransform::backward_dev_impl(const MyDevice & dev,
DYNET_ARG_CHECK(dEdxi.d.bd == 1, "In AffineTransform, broadcasting over columns with mini-batched inputs is not implemented yet");
#ifdef __CUDACC__
if(dEdxi.d[1] == dEdf.d[1]) {
Eigen::array<int, 1> red_axis; red_axis[0] = 2;
Eigen::array<ptrdiff_t, 1> red_axis = { 2 };
t<2>(dEdxi).device(*dev.edevice) += tb<2>(dEdf).sum(red_axis);
} else {
Eigen::array<int, 2> red_axis; red_axis[0] = 1; red_axis[1] = 2;
Eigen::array<ptrdiff_t, 2> red_axis = {1, 2};
t<1>(dEdxi).device(*dev.edevice) += tb<2>(dEdf).sum(red_axis);
}
#else
Expand Down
32 changes: 16 additions & 16 deletions dynet/nodes-arith-cwise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void CwiseSum::forward_dev_impl(const MyDevice & dev, const vector<const Tensor*
} else {
int greater = xs[0]->d.bd > xs[1]->d.bd ? 0 : 1;
#ifdef __CUDACC__
Eigen::array<int, 2> bcast = {1,(int)xs[greater]->d.bd};
Eigen::array<ptrdiff_t, 2> bcast = {1, xs[greater]->d.bd};
tbvec(fx).device(*dev.edevice) = tbvec(*xs[1-greater]).broadcast(bcast) + tbvec(*xs[greater]);
#else
for(size_t b = 0; b < fx.d.bd; ++b)
Expand All @@ -68,7 +68,7 @@ void CwiseSum::forward_dev_impl(const MyDevice & dev, const vector<const Tensor*
}
// Broadcasting over dims as well
} else {
Eigen::array<int, 5> bcast_left = {1,1,1,1,1}, bcast_right = {1,1,1,1,1};
Eigen::array<ptrdiff_t, 5> bcast_left = {1,1,1,1,1}, bcast_right = {1,1,1,1,1};
bool has_left = false, has_right = false;
for(; i < fx.d.nd; ++i){
if(xs[0]->d[i] > xs[1]->d[i]) {
Expand Down Expand Up @@ -114,7 +114,7 @@ void CwiseSum::backward_dev_impl(const MyDevice & dev,
tvec(dEdxi).device(*dev.edevice) += tvec(dEdf);
} else {
#ifdef __CUDACC__
Eigen::array<int, 1> red_axis = {1};
Eigen::array<ptrdiff_t, 1> red_axis = {1};
tvec(dEdxi).device(*dev.edevice) += tbvec(dEdf).sum(red_axis);
#else
for(size_t b = 0; b < dEdf.d.bd; ++b)
Expand All @@ -140,10 +140,10 @@ void CwiseSum::backward_helper(const MyDevice & dev,
const Tensor& dEdf,
unsigned i,
Tensor& dEdxi) const {
Eigen::array<int, ReductionOrder> red_axis;
Eigen::array<ptrdiff_t, ReductionOrder> red_axis;
if(ReductionOrder>0) red_axis[ReductionOrder-1] = 4;
int curr_red_axis = 0;
Eigen::array<int, 5> morph = {1,1,1,1,(int)xs[i]->d.bd};
Eigen::array<ptrdiff_t, 5> morph = {1,1,1,1,(int)xs[i]->d.bd};
for(unsigned int di = 0; di < fx.d.nd; di++) {
if((di >= xs[i]->d.nd && fx.d[di]>1) || xs[i]->d[di] != fx.d[di]) {
red_axis[curr_red_axis] = di;
Expand Down Expand Up @@ -205,7 +205,7 @@ void CwiseMultiply::forward_dev_impl(const MyDevice & dev, const vector<const Te
} else {
int greater = xs[0]->d.bd > xs[1]->d.bd ? 0 : 1;
#ifdef __CUDACC__
Eigen::array<int, 2> bcast = {1,(int)xs[greater]->d.bd};
Eigen::array<ptrdiff_t, 2> bcast = {1,(int)xs[greater]->d.bd};
tbvec(fx).device(*dev.edevice) = tbvec(*xs[1-greater]).broadcast(bcast) * tbvec(*xs[greater]);
#else
for(size_t b = 0; b < fx.d.bd; ++b)
Expand All @@ -214,7 +214,7 @@ void CwiseMultiply::forward_dev_impl(const MyDevice & dev, const vector<const Te
}
// Broadcasting over dims as well
} else {
Eigen::array<int, 5> bcast_left = {1,1,1,1,1}, bcast_right = {1,1,1,1,1};
Eigen::array<ptrdiff_t, 5> bcast_left = {1,1,1,1,1}, bcast_right = {1,1,1,1,1};
bool has_left = false, has_right = false;
for(; i < fx.d.nd; ++i){
if(xs[0]->d[i] > xs[1]->d[i]) {
Expand Down Expand Up @@ -263,10 +263,10 @@ void CwiseMultiply::backward_dev_impl(const MyDevice & dev,
tvec(dEdxi).device(*dev.edevice) += tvec(dEdf) * tvec(*xs[1-i]);
} else if(xs[1-i]->d.bd == 1) {
// TODO: Make alternative code path for CPU?
Eigen::array<int, 2> bcast; bcast[0] = 1; bcast[1] = fx.d.bd;
Eigen::array<ptrdiff_t, 2> bcast = { 1, fx.d.bd };
tbvec(dEdxi).device(*dev.edevice) += tbvec(dEdf) * tbvec(*xs[1-i]).broadcast(bcast);
} else {
Eigen::array<int, 1> red_axis; red_axis[0] = 1;
Eigen::array<ptrdiff_t, 1> red_axis = {1};
tvec(dEdxi).device(*dev.edevice) += (tbvec(dEdf) * tbvec(*xs[1-i])).sum(red_axis);
}
// Otherwise work with broadcasting, etc.
Expand All @@ -288,8 +288,8 @@ void CwiseMultiply::backward_helper(const MyDevice & dev,
const Tensor& dEdf,
unsigned i,
Tensor& dEdxi) const {
Eigen::array<int, ReductionOrder> red_axis;
Eigen::array<int, 5> morph = {1,1,1,1,(int)xs[i]->d.bd}, bcast_other = {1,1,1,1,1};
Eigen::array<ptrdiff_t, ReductionOrder> red_axis;
Eigen::array<ptrdiff_t, 5> morph = {1,1,1,1,(int)xs[i]->d.bd}, bcast_other = {1,1,1,1,1};
if(ReductionOrder>0) red_axis[ReductionOrder-1] = 4;
int curr_red_axis = 0;
for(unsigned int di = 0; di < fx.d.nd; di++){
Expand Down Expand Up @@ -341,7 +341,7 @@ void CwiseQuotient::forward_dev_impl(const MyDevice & dev, const vector<const Te
if(xs[0]->d.size() == xs[1]->d.size()){
tb<4>(fx).device(*dev.edevice) = tb<4>(*xs[0]) / tb<4>(*xs[1]);
} else {
Eigen::array<int, 5> bcast = {1,1,1,1,1};
Eigen::array<ptrdiff_t, 5> bcast = {1,1,1,1,1};
for(unsigned int di = 0; di<xs[0]->d.nd; di++){
if(xs[1]->d[di]==1) bcast[di] = xs[0]->d[di];
}
Expand All @@ -362,7 +362,7 @@ void CwiseQuotient::backward_dev_impl(const MyDevice & dev,
if(xs[0]->d.size() == xs[1]->d.size()){
tb<4>(dEdxi).device(*dev.edevice) += tb<4>(dEdf) / tb<4>(*xs[1]);
} else {
Eigen::array<int, 5> bcast = {1,1,1,1,1};
Eigen::array<ptrdiff_t, 5> bcast = {1,1,1,1,1};
for(unsigned int di = 0; di<xs[0]->d.nd; di++){
if(xs[0]->d[di]!=xs[1]->d[di]) bcast[di] = xs[0]->d[di];
}
Expand Down Expand Up @@ -393,7 +393,7 @@ void CwiseQuotient::backward_helper(const MyDevice & dev,
const Tensor& dEdf,
unsigned i,
Tensor& dEdxi) const {
Eigen::array<int, ReductionOrder> red_axis;
Eigen::array<ptrdiff_t, ReductionOrder> red_axis;
if(ReductionOrder>0) red_axis[ReductionOrder-1] = 4;
int curr_red_axis = 0;
for(unsigned int di = 0; di < xs[0]->d.nd; di++){
Expand All @@ -402,12 +402,12 @@ void CwiseQuotient::backward_helper(const MyDevice & dev,
curr_red_axis++;
}
}
Eigen::array<int, 5> morph = {1,1,1,1,1};
Eigen::array<ptrdiff_t, 5> morph = {1,1,1,1,1};
for(unsigned int di = 0; di < xs[0]->d.nd; di++){
morph[di] = xs[i]->d[di];
}
morph[4] = xs[i]->d.bd;
Eigen::array<int, 5> bcast = {1,1,1,1,1};
Eigen::array<ptrdiff_t, 5> bcast = {1,1,1,1,1};
for(unsigned int di = 0; di < xs[0]->d.nd; di++){
if(xs[0]->d[di]!=xs[1]->d[di]) bcast[di] = xs[0]->d[di];
}
Expand Down
48 changes: 24 additions & 24 deletions dynet/nodes-arith-sum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void Sum::forward_dev_impl(const MyDevice & dev, const vector<const Tensor*>& xs
// Not all the same batch size, so need to broadcast in the cases where they differ
TensorTools::zero(fx);
#ifdef __CUDACC__
Eigen::array<int, 2> bcast({ 1, (int)fx.d.bd });
Eigen::array<ptrdiff_t, 2> bcast({ 1, fx.d.bd });
#endif
for (unsigned i = 0; i < num_args; ++i) {
if (xs[i]->d.bd == fx.d.bd) {
Expand Down Expand Up @@ -123,7 +123,7 @@ void Sum::backward_dev_impl(const MyDevice & dev,
if(dEdxi.d.bd == fx.d.bd) {
tvec(dEdxi).device(*dev.edevice) += tvec(dEdf);
} else {
Eigen::array<int, 1> red_axis = {1};
Eigen::array<ptrdiff_t, 1> red_axis = {1};
tvec(dEdxi).device(*dev.edevice) += tbvec(dEdf).sum(red_axis);
}
}
Expand All @@ -149,7 +149,7 @@ Dim SumElements::dim_forward(const vector<Dim>& xs) const {
template<class MyDevice>
void SumElements::forward_dev_impl(const MyDevice & dev, const vector<const Tensor*>& xs, Tensor& fx) const {
DYNET_ARG_CHECK(xs.size() == 1, "Failed dimension check in SumElements::forward");
Eigen::array<int, 1> red_axis; red_axis[0] = 0;
Eigen::array<ptrdiff_t, 1> red_axis = {0};
tb<0>(fx).device(*dev.edevice) = tbvec(*xs[0]).sum(red_axis);
}

Expand All @@ -161,7 +161,7 @@ void SumElements::backward_dev_impl(const MyDevice & dev,
unsigned i,
Tensor& dEdxi) const {
DYNET_ARG_CHECK(i == 0, "Failed dimension check in SumElements::backward");
Eigen::array<int, 2> bcast = {(int)xs[0]->d.batch_size(), 1};
Eigen::array<ptrdiff_t, 2> bcast = {xs[0]->d.batch_size(), 1};
tbvec(dEdxi).device(*dev.edevice) += tbvec(dEdf).broadcast(bcast);
}
DYNET_NODE_INST_DEV_IMPL(SumElements)
Expand Down Expand Up @@ -199,19 +199,19 @@ void SumDimension::forward_dev_impl(const MyDevice & dev, const vector<const Ten
DYNET_ASSERT(xs.size() == 1, "Failed input count check in SumDimension");

if(dims.size()==0 && include_batch_dim){
Eigen::array<int, 1> reduction_axis = {1};
Eigen::array<ptrdiff_t, 1> reduction_axis = {1};
tvec(fx).device(*dev.edevice) = tbvec(*xs[0]).sum(reduction_axis);
} else if(dims.size()==1 && !include_batch_dim){
Eigen::array<int, 1> reduction_axis = {(int)dims[0]};
Eigen::array<ptrdiff_t, 1> reduction_axis = {dims[0]};
tb<2>(fx).device(*dev.edevice) = tb<3>(*xs[0]).sum(reduction_axis);
} else if(dims.size()==1 && include_batch_dim){
Eigen::array<int, 2> reduction_axis = {(int)dims[0], 3};
Eigen::array<ptrdiff_t, 2> reduction_axis = {dims[0], 3};
t<2>(fx).device(*dev.edevice) = tb<3>(*xs[0]).sum(reduction_axis);
} else if(dims.size()==2 && !include_batch_dim){
Eigen::array<int, 2> reduction_axis = {(int)dims[0], (int)dims[1]};
Eigen::array<ptrdiff_t, 2> reduction_axis = {dims[0], dims[1]};
tb<1>(fx).device(*dev.edevice) = tb<3>(*xs[0]).sum(reduction_axis);
} else if(dims.size()==2 && include_batch_dim){
Eigen::array<int, 3> reduction_axis = {(int)dims[0], (int)dims[1], 3};
Eigen::array<ptrdiff_t, 3> reduction_axis = {dims[0], dims[1], 3};
t<1>(fx).device(*dev.edevice) = tb<3>(*xs[0]).sum(reduction_axis);
}
}
Expand All @@ -226,23 +226,23 @@ void SumDimension::backward_dev_impl(const MyDevice & dev,
DYNET_ARG_CHECK(i == 0, "Failed dimension check in SumDimension::backward");

if(dims.size()==0 && include_batch_dim){
Eigen::array<int, 2> bcast = {1, (int)xs[0]->d.bd};
Eigen::array<ptrdiff_t, 2> bcast = {1, xs[0]->d.bd};
tbvec(dEdxi).device(*dev.edevice) += tbvec(dEdf).broadcast(bcast);
} else if(dims.size()==1 && !include_batch_dim){
Eigen::array<int, 4> bcast = {1,1,1,1}; bcast[dims[0]] = xs[0]->d[dims[0]];
Eigen::array<int, 4> morph = {(int)xs[0]->d[0],(int)xs[0]->d[1],(int)xs[0]->d[2],(int)xs[0]->d.bd}; morph[dims[0]] = 1;
Eigen::array<ptrdiff_t, 4> bcast = {1,1,1,1}; bcast[dims[0]] = xs[0]->d[dims[0]];
Eigen::array<ptrdiff_t, 4> morph = {xs[0]->d[0],xs[0]->d[1],xs[0]->d[2],xs[0]->d.bd}; morph[dims[0]] = 1;
tb<3>(dEdxi).device(*dev.edevice) += tb<2>(dEdf).reshape(morph).broadcast(bcast);
} else if(dims.size()==1 && include_batch_dim){
Eigen::array<int, 4> bcast = {1,1,1,1}; bcast[dims[0]] = xs[0]->d[dims[0]]; bcast[3] = xs[0]->d.bd;
Eigen::array<int, 4> morph = {(int)xs[0]->d[0],(int)xs[0]->d[1],(int)xs[0]->d[2],(int)1}; morph[dims[0]] = 1;
Eigen::array<ptrdiff_t, 4> bcast = {1,1,1,1}; bcast[dims[0]] = xs[0]->d[dims[0]]; bcast[3] = xs[0]->d.bd;
Eigen::array<ptrdiff_t, 4> morph = {xs[0]->d[0],xs[0]->d[1],xs[0]->d[2],1}; morph[dims[0]] = 1;
tb<3>(dEdxi).device(*dev.edevice) += t<2>(dEdf).reshape(morph).broadcast(bcast);
} else if(dims.size()==2 && !include_batch_dim){
Eigen::array<int, 4> bcast = {1,1,1,1}; bcast[dims[0]] = xs[0]->d[dims[0]]; bcast[dims[1]] = xs[0]->d[dims[1]];
Eigen::array<int, 4> morph = {(int)xs[0]->d[0],(int)xs[0]->d[1],(int)xs[0]->d[2],(int)xs[0]->d.bd}; morph[dims[0]] = 1; morph[dims[1]] = 1;
Eigen::array<ptrdiff_t, 4> bcast = {1,1,1,1}; bcast[dims[0]] = xs[0]->d[dims[0]]; bcast[dims[1]] = xs[0]->d[dims[1]];
Eigen::array<ptrdiff_t, 4> morph = {xs[0]->d[0],xs[0]->d[1],xs[0]->d[2],xs[0]->d.bd}; morph[dims[0]] = 1; morph[dims[1]] = 1;
tb<3>(dEdxi).device(*dev.edevice) += tb<1>(dEdf).reshape(morph).broadcast(bcast);
} else if(dims.size()==2 && include_batch_dim){
Eigen::array<int, 4> bcast = {1,1,1,1}; bcast[dims[0]] = xs[0]->d[dims[0]]; bcast[dims[1]] = xs[0]->d[dims[1]]; bcast[3] = xs[0]->d.bd;
Eigen::array<int, 4> morph = {(int)xs[0]->d[0],(int)xs[0]->d[1],(int)xs[0]->d[2],(int)1}; morph[dims[0]] = 1; morph[dims[1]] = 1;
Eigen::array<ptrdiff_t, 4> bcast = {1,1,1,1}; bcast[dims[0]] = xs[0]->d[dims[0]]; bcast[dims[1]] = xs[0]->d[dims[1]]; bcast[3] = xs[0]->d.bd;
Eigen::array<ptrdiff_t, 4> morph = {xs[0]->d[0],xs[0]->d[1],xs[0]->d[2],1}; morph[dims[0]] = 1; morph[dims[1]] = 1;
tb<3>(dEdxi).device(*dev.edevice) += t<1>(dEdf).reshape(morph).broadcast(bcast);
}
}
Expand Down Expand Up @@ -274,13 +274,13 @@ void AddVectorToAllColumns::forward_dev_impl(const MyDevice & dev, const vector<
// Broadcasting is slow on CPU, so split codepaths
#ifdef __CUDACC__
if(xs[0]->d.bd >= xs[1]->d.bd) {
Eigen::array<int, 3> bcasts = {1, (int)xs[0]->d[1], (int)(xs[0]->d.bd/xs[1]->d.bd)};
Eigen::array<ptrdiff_t, 3> bcasts = {1, xs[0]->d[1], xs[0]->d.bd/xs[1]->d.bd};
tb<2>(fx).device(*dev.edevice) = tb<2>(*xs[0]) + tb<2>(*xs[1]).broadcast(bcasts);
} else {
DYNET_ASSERT(xs[0]->d.bd == 1,
"Bad dimensions in AddVectorToAllColumns::forward: " << xs[0]->d << ", " << xs[1]->d);
Eigen::array<int, 3> bcasts0 = {1, 1, (int)xs[1]->d.bd};
Eigen::array<int, 3> bcasts1 = {1, (int)xs[0]->d[1], 1};
Eigen::array<ptrdiff_t, 3> bcasts0 = {1, 1, xs[1]->d.bd};
Eigen::array<ptrdiff_t, 3> bcasts1 = {1, xs[0]->d[1], 1};
tb<2>(fx).device(*dev.edevice) = tb<2>(*xs[0]).broadcast(bcasts0) + tb<2>(*xs[1]).broadcast(bcasts1);
}
#else
Expand Down Expand Up @@ -315,17 +315,17 @@ void AddVectorToAllColumns::backward_dev_impl(const MyDevice & dev,
if(dEdf.d.bd == dEdxi.d.bd) {
tvec(dEdxi).device(*dev.edevice) += tvec(dEdf);
} else {
Eigen::array<int, 1> red_axis = {2};
Eigen::array<ptrdiff_t, 1> red_axis = {2};
t<2>(dEdxi).device(*dev.edevice) += tb<2>(dEdf).sum(red_axis);
}
} else { // bias
if(dEdf.d.bd == dEdxi.d.bd) {
Eigen::array<int, 1> red_axis = {1};
Eigen::array<ptrdiff_t, 1> red_axis = {1};
tb<1>(dEdxi).device(*dev.edevice) += tb<2>(dEdf).sum(red_axis);
} else {
DYNET_ASSERT(dEdxi.d.bd == 1,
"Bad dimensions in AddVectorToAllColumns::backward: " << xs[0]->d << ", " << xs[1]->d);
Eigen::array<int, 2> red_axis = {1,2};
Eigen::array<ptrdiff_t, 2> red_axis = {1,2};
t<1>(dEdxi).device(*dev.edevice) += tb<2>(dEdf).sum(red_axis);
}
}
Expand Down
2 changes: 1 addition & 1 deletion dynet/nodes-concat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void Concatenate::backward_dev_impl(const MyDevice & dev,
if(dEdxi.d.bd == dEdf.d.bd) {
tb<4>(dEdxi).device(*dev.edevice) += tb<4>(dEdf).slice(indices, sizes);
} else {
Eigen::array<int, 1> red_axis; red_axis[0] = 4;
Eigen::array<ptrdiff_t, 1> red_axis = {4};
t<4>(dEdxi).device(*dev.edevice) += tb<4>(dEdf).slice(indices, sizes).sum(red_axis);
}
}
Expand Down
12 changes: 6 additions & 6 deletions dynet/nodes-contract.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void InnerProduct3D_1D::forward_dev_impl(const MyDevice & dev, const vector<cons
// Handle hypothetical bias
if (xs.size() == 3) {
auto C = tb<2>(*xs[2]);
Eigen::array<int, 3> bcast_C = {1, 1, (int)(xs[2]->d.bd == 1 ? fx.d.bd : 1)};
Eigen::array<ptrdiff_t, 3> bcast_C = {1, 1, (xs[2]->d.bd == 1 ? fx.d.bd : 1)};
tb<2>(fx).device(*dev.edevice) = C.broadcast(bcast_C);
}
#if defined(__CUDACC__) && !defined(DYNET_SKIP_CUDA_CONTRACTIONS)
Expand All @@ -81,7 +81,7 @@ void InnerProduct3D_1D::forward_dev_impl(const MyDevice & dev, const vector<cons
// TODO : maybe on CPU broadcast is not as affective as looping?
if (xs[0]->d.bd == 1) {
// A is a 3 tensor
Eigen::array<int, 2> bcast_b = {1, (int)(xs[1]->d.bd == 1 ? fx.d.bd : 1)};
Eigen::array<ptrdiff_t, 2> bcast_b = {1, (xs[1]->d.bd == 1 ? fx.d.bd : 1)};
auto b = tb<1>(*xs[1]);
auto A = t<3>(*xs[0]);
tb<2>(fx).device(*dev.edevice) += A.contract(b.broadcast(bcast_b), dims);
Expand Down Expand Up @@ -149,14 +149,14 @@ void InnerProduct3D_1D::backward_dev_impl(const MyDevice & dev,
if (xs[0]->d.bd == 1) { // A is a 3 tensor
// tensor product
auto b = tb<1>(*xs[1]);
Eigen::array<int, 2> bcast_b = {1, (int)(xs[1]->d.bd == 1 ? fx.d.bd : 1)};
Eigen::array<ptrdiff_t, 2> bcast_b = {1, (xs[1]->d.bd == 1 ? fx.d.bd : 1)};
Eigen::array<DimPair, 1> dims({{DimPair(2, 1)}});
t<3>(dEdxi).device(*dev.edevice) += tdEdf.contract(b.broadcast(bcast_b), dims);
} else {
// For now if A is batched the CUDA version is not implemented
if (xs[1]->d.bd == 1) {
// auto b = t<1>(*xs[1]);
// Eigen::array<int, 4> morph {dEdf.d[0], dEdf.d[1], xs[1]->d[0], dEdf.d.bd};
// Eigen::array<ptrdiff_t, 4> morph {dEdf.d[0], dEdf.d[1], xs[1]->d[0], dEdf.d.bd};
// tb<3>(dEdxi).device(*dev.edevice) += tdEdf.contract(b, Eigen::array<DimPair, 0> {{}}).reshape(morph);
auto b = t<1>(*xs[1]);
for (unsigned i = 0; i < fx.d.bd; ++i) {
Expand Down Expand Up @@ -211,7 +211,7 @@ void InnerProduct3D_1D::backward_dev_impl(const MyDevice & dev,
if (xs[1]->d.bd == 1) { // b is a 1 tensor
if (xs[0]->d.bd == 1) {
auto A = t<3>(*xs[0]); // A is 3 tensor
Eigen::array<int, 1> red_axis; red_axis[0] = 0;
Eigen::array<ptrdiff_t, 1> red_axis = {0};
Eigen::array<DimPair, 2> dims({{DimPair(0, 0), DimPair(1, 1)}});
t<1>(dEdxi).device(*dev.edevice) += tdEdf.contract(A, dims).sum(red_axis);
} else {
Expand All @@ -235,7 +235,7 @@ void InnerProduct3D_1D::backward_dev_impl(const MyDevice & dev,
#endif
} else if (i == 2) { // dEdC
if (xs[2]->d.bd == 1) {
Eigen::array<int, 1> red_axis; red_axis[0] = 2;
Eigen::array<ptrdiff_t, 1> red_axis = {2};
t<2>(dEdxi).device(*dev.edevice) += tdEdf.sum(red_axis);
} else {
tb<2>(dEdxi).device(*dev.edevice) += tdEdf;
Expand Down
2 changes: 1 addition & 1 deletion dynet/nodes-conv2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ void Conv2D::backward_dev_impl(const MyDevice & dev,
t<4>(HWCN_dEdxi).device(*dev.edevice) = t<4>(NCHW_dEdxi).shuffle(shuffles);
t<4>(dEdxi).device(*dev.edevice) += t<4>(HWCN_dEdxi);
} else { //backward w.r.t the bias
Eigen::array<int, 3> red_axis = {0, 1, 3};
Eigen::array<ptrdiff_t, 3> red_axis = {0, 1, 3};
t<1>(dEdxi).device(*dev.edevice) += tb<3>(dEdf).sum(red_axis);
}
#endif
Expand Down
Loading

0 comments on commit 4948b5c

Please sign in to comment.