Giter VIP home page Giter VIP logo

Comments (5)

andrjohns avatar andrjohns commented on June 19, 2024 2

Great! I've just about finished up a PR with these changes, will open that later today

from math.

andrjohns avatar andrjohns commented on June 19, 2024 1

A couple of other accumulator thoughts.

At the moment, Eigen types are summed and then appended to the buffer, but std::vector types are simply elementwise appended - ostensibly for the code to also be compatible with std::vector<container> types:

  template <typename S, require_matrix_t<S>* = nullptr>
  inline void add(const S& m) {
    buf_.push_back(stan::math::sum(m));
  }

  template <typename S>
  inline void add(const std::vector<S>& xs) {
    for (size_t i = 0; i < xs.size(); ++i) {
      this->add(xs[i]);
    }
  }

If we change the templates such that the Eigen overload also takes non-nested std:vectors and the std::vector overload is only for nested containers, then this should dramatically improve performance with larger std::vector inputs. It would also reduce memory usage, since the non-nested std::vectors are no longer just being appended to the buffer.

One last thought, would there be any benefit from std::move()-ing the result?:

  template <typename S, require_matrix_t<S>* = nullptr>
  inline void add(const S& m) {
    buf_.push_back(std::move(stan::math::sum(m)));
  }

from math.

SteveBronder avatar SteveBronder commented on June 19, 2024 1

We can't use log_sum_exp here since we're after the sum of the logs, not the log of the sum of the exps.

Ooops yes totally forgot, forget this idea.

But it did make me have a look at the accumulator() and sum() code, and I think we should change the std::vector overload to use Eigen::Map<> and then sum, rather than std::accumulate: https://godbolt.org/z/zhe3eMWv7

Especially since accumulator sums its buffer using the std::vector<> method, which doesn't look to get the vectorised benefits that Eigen does

Yes I like this idea!

If we change the templates such that the Eigen overload also takes non-nested std:vectors and the std::vector overload is only for nested containers, then this should dramatically improve performance with larger std::vector inputs. It would also reduce memory usage, since the non-nested std::vectors are no longer just being appended to the buffer.

Yes I agree that would be a good change!

One last thought, would there be any benefit from std::move()-ing the result?:

Not here, we only need to move if the container we are moving has a good amount of dynamically allocated memory. In the code above sum() returns a scalar so it would not need to be moved.

You can think of moves like it's giving ownership of deleting dynamically allocated memory to a new object from an old object.

struct foo {
  double* data_;//
  std::size_t n_;
  // Copy makes a copy of the memory
  foo(foo& x) : data_(copy(x.data_, n)), n_(x.n_) {} 
  foo(foo&& x) : data_(x.data_), n_(x.n_) {
    // Moves takes the pointer and then nulls out the input's pointer
    // This means the new object is now in charge of calling delete
    x.data_ = nullptr; 
  }
}

from math.

andrjohns avatar andrjohns commented on June 19, 2024

We can't use log_sum_exp here since we're after the sum of the logs, not the log of the sum of the exps.

But it did make me have a look at the accumulator() and sum() code, and I think we should change the std::vector<T> overload to use Eigen::Map<> and then sum, rather than std::accumulate: https://godbolt.org/z/zhe3eMWv7

Especially since accumulator sums its buffer using the std::vector<> method, which doesn't look to get the vectorised benefits that Eigen does

from math.

SteveBronder avatar SteveBronder commented on June 19, 2024

Also changed the name of the issue to reflect topic changed

from math.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.