stan-dev / stan-dev/math

Inconsistent `arena_matrix`/`varmat` interaction between `=` and `+=`

Open
#3,407 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
839
Forks
220
Avg merge
2d 4h
Merged PRs (30d)
14

Description

Opening this one as an issue since I'm not sure what the correct/intentional behaviour should be!

The arena_matrix <-> var_value<Eigen::MatrixXd> interaction currently handles inplace writes differently to assignment.

Where modifying an arena_matrix after creating a var_value will also update the value of the var_value if an inplace write is used, but not if an assignment is used:

#include <stan/math/rev.hpp>
#include <iostream>

int main() {
  Eigen::MatrixXd m = Eigen::MatrixXd::Constant(1, 1, 1);

  stan::arena_t<Eigen::MatrixXd> a(m);
  stan::math::var_value<Eigen::MatrixXd> v(a);  // shares a's memory
  std::cout << v.val() << "\n";

  a += m;  // writes in place, v sees the new values
  std::cout << v.val() << "\n";

  a = a + m;  // rebinds a, v keeps the old memory
  std::cout << v.val() << std::endl;
}

Returns:

$ ./arena 
1
2
2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided reproducer and inspect the interaction between arena_tEigen::MatrixXd and var_valueEigen::MatrixXd. Determine the intended aliasing behavior for += versus =, then define a consistent expected result and tests for both operations.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.