Skip to content

Commit

Permalink
add doc and reimplement Dim::truncate (clab#970)
Browse files Browse the repository at this point in the history
* remote use GPU:0 by default when use --dynet-gpu

* fix error

* Comment DeviceMempool enum

* clab#945
1. add doc for Dim::truncate
2. reimplement Dim::truncate
3. add tests for dim
  • Loading branch information
chunyang-wen authored and neubig committed Oct 13, 2017
1 parent 29536c4 commit 0c6c6de
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
12 changes: 5 additions & 7 deletions dynet/dim.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,14 @@ struct Dim {
return p;
}
/**
* \brief [TODO]
* \details [long description]
* \return [description]
* \brief remove trailing dimensions of 1
* \details iterate all the dimensions of Dim, stop at last dimension of 1
* \return truncated dimension
*/
inline Dim truncate() const {
Dim r = *this;
unsigned int m = 1;
unsigned int s = size();
for (unsigned int i = 1; i < s; ++i)
if (size(i) > 1) m = i + 1;
unsigned int m = nd;
while (m > 1 && size(m-1) == 1) --m;
r.resize(m);
return r;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if (DEFINED ENV{DYNET_TEST_DEVICES}) # use env variable as preprocessor macro
add_definitions(-DDYNET_TEST_DEVICES=$ENV{DYNET_TEST_DEVICES})
endif()

foreach(TESTNAME dynet exec io mem nodes params tensor trainers rnn softmax)
foreach(TESTNAME dim dynet exec io mem nodes params tensor trainers rnn softmax)
add_executable(test-${TESTNAME} test-${TESTNAME}.cc)
if (NOT MSVC)
target_link_libraries(test-${TESTNAME} ${Boost_LIBRARIES} ${CMAKE_DL_LIBS})
Expand Down
40 changes: 40 additions & 0 deletions tests/test-dim.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#define BOOST_TEST_MODULE TEST_DIM

#include <dynet/dim.h>

#include "test.h"

using namespace dynet;

struct DimTest {

};

BOOST_FIXTURE_TEST_SUITE(dim_test, DimTest);

BOOST_AUTO_TEST_CASE( test_dim_truncate_no_trailing_one ) {
Dim d1({1,3,4});
Dim t1 = d1.truncate();
BOOST_CHECK_EQUAL(t1.nd, 3);
}

BOOST_AUTO_TEST_CASE( test_dim_truncate_all_one ) {
Dim d1({1,1,1});
Dim t1 = d1.truncate();
BOOST_CHECK_EQUAL(t1.nd, 1);
}

BOOST_AUTO_TEST_CASE( test_dim_truncate_trailing_one ) {
Dim d1({4,3,1});
Dim t1 = d1.truncate();
BOOST_CHECK_EQUAL(t1.nd, 2);
}

BOOST_AUTO_TEST_CASE( test_dim_truncate_multiple_one ) {
Dim d1({4,1,1});
Dim t1 = d1.truncate();
BOOST_CHECK_EQUAL(t1.nd, 1);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 0c6c6de

Please sign in to comment.