Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dim.h: truncate() should be documented #945

Closed
chunyang-wen opened this issue Sep 29, 2017 · 4 comments
Closed

dim.h: truncate() should be documented #945

chunyang-wen opened this issue Sep 29, 2017 · 4 comments
Labels
minor bug Bugs that aren't too bad, only concern documentation, or have easy work-arounds

Comments

@chunyang-wen
Copy link
Contributor

  /**
   * \brief [TODO]
   * \details [long description]
   * \return [description]
   */
  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;
    r.resize(m);
    return r;
  }

size(i) definitions can not be found. And I want to know the purpose of truncate which means description. And it is related to find the problem of cdiv (#892)

Thanks in advance.

@neubig
Copy link
Contributor

neubig commented Sep 29, 2017

This removes all dimensions of size 1 from the end of the dim object. A PR documenting this would be welcome.

@neubig neubig added the minor bug Bugs that aren't too bad, only concern documentation, or have easy work-arounds label Sep 29, 2017
@neubig neubig changed the title Question in dim.h: truncate() dim.h: truncate() should be documented Sep 29, 2017
@chunyang-wen
Copy link
Contributor Author

chunyang-wen commented Sep 30, 2017

If so, I think the implementation is not that efficient.

/**
   * \brief [TODO]
   * \details [long description]
   * \return [description]
   */
  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;
    r.resize(m);
    return r;
  }

  inline unsigned int size() const {
    return batch_size() * bd;
  }

  inline unsigned int batch_size() const {
    unsigned int p = 1;
    for (unsigned int i = 0; i < nd; ++i) p *= d[i];
    return p;
  }

  inline void resize(unsigned int i) {
    // if i < nd, it will remove size of dimension 1.
    while(nd < i)
      d[nd++] = 1;
    nd = i;
  }

inline unsigned int size(unsigned int i) const { return (*this)[i]; }

inline unsigned int operator[](unsigned int i) const { return i < nd ? d[i] : 1; }

The size() function in truncate calculate number of elements. We will iterate through all the elements.

For example:

Dim = { 2, 3, 4, 1}, nd=4, bd=1
truncate will iterate through 2*3*4*1 times to remove trailing dimensions of 1.
Actually only 4 times is required. If iterate backward, only 2 times is required.

truncate() is called in following files:

  • nodes-arith-cwise.cc
  • nodes-arith-scalar.cc
  • nodes-arith-sum.cc
  • nodes-logsumexp.cc

I will try to figure out why dimensions of 1 should be removed in dim_forward.

@msperber
Copy link
Collaborator

It might be worth considering to remove the dimension truncation for better consistency? That's what already happened for some operations touched by #776.

chunyang-wen added a commit to chunyang-wen/dynet that referenced this issue Oct 9, 2017
1. add doc for Dim::truncate
2. reimplement Dim::truncate
3. add tests for dim
neubig pushed a commit that referenced this issue Oct 13, 2017
* remote use GPU:0 by default when use --dynet-gpu

* fix error

* Comment DeviceMempool enum

* #945
1. add doc for Dim::truncate
2. reimplement Dim::truncate
3. add tests for dim
@pmichel31415
Copy link
Collaborator

fixed by #970 I think

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
minor bug Bugs that aren't too bad, only concern documentation, or have easy work-arounds
Projects
None yet
Development

No branches or pull requests

4 participants