I have a program that I've been modifying to extract some data.
Recently I did another data extraction and used a work-around for lack of time.
Now, I am trying to find out what the problem was, because I have some spare time.
Below, there is a function Act() which is a member function of class Mat, and it forms a new matrix with altered data values. The Mat has a pointer to a buffer containing actual data. I found that after the Act() function which returns a Matrix, the 'operator =' function is called. (there was no 'operator =' function so I added it. maybe default '= operator' was being used. With or without my added 'operator =' function, it does not produce the final right output) Here is the portion of the '=' operator which is returning the data(type Mat).(it is a c++ code but I just used printf because it works anyway)
template<typename T>
Mat<T> Mat<T>::operator = (const Mat<T>& rhs)
{
Mat<T> result(rhs.row_, rhs.col_);
int num_bytes = sizeof(T) * rhs.row_ * rhs.col_;
printf("ope = , res val = %x, src val = %x, row = %d, col = %d\n", result.val_, rhs.val_, rhs.row_, rhs.col_);
for(int i=0;i<10;i++){
printf("%04x ",((half_float::half *)rhs.val_)[i].data_);
}
printf("\n");
memcpy(result.val_, rhs.val_, num_bytes);
for(int i=0;i<10;i++){
printf("%04x ",((half_float::half *)result.val_)[i].data_);
}
printf("\n");
return result;
}
ope = , res val = 12199b0, src val = c07680, row = 128, col = 128
3be0 bbfc bbf5 3bf8 3af0 bbf6 bbef b29f bbaf 3bfd
3be0 bbfc bbf5 3bf8 3af0 bbf6 bbef b29f bbaf 3bfd
This '3be0 bbfc bbf5 ...' is the right pattern.
The calling part looks like this.
printf("calling activation..\n");
d0_out = d0_out.Act(NN_CELL::AT_TANH);
std::cout << "MLP Layer 0 executed." << std::endl;
for(int i=0;i<10;i++){
printf("%04x ",((half_float::half *)d0_out.val_)[i].data_);
}
printf("\n");
But after the assignment, seen from the main function, the data is weird(inside the = operator, the copy seems to have been made correctly, but outside, it's doesn't).
MLP Layer 0 executed.
40de c2e2 c1eb 425e 3d4a c21b c187 b2b8 bfce 4358
As you can see, the data is wrong.
The only way I can produce right output is using a new object and assigning the result of Act() function to it.
NN_CELL::Mat<T> d0_out1 = d0_out.Act(NN_CELL::AT_TANH); // tmp work-around
Then, d0_out1 (new object) contains the right data(3be0 bbfc bbf5 ..), instead of original
d0_out = d0_out.Act(NN_CELL::AT_TANH);
What is wrong with the original code?