Assign memory blob to py-torch output tensor (C++ API) - pytorch

I am training a linear model using py-torch and I am saving it to a file with the "save" function call. I have another code that loads the model in C++ and performs inference.
I would like to instruct the Torch CPP Library to use a specific memory blob at the final output tensor. Is this even possible? If yes, how? Below you can see a small example of what I am trying to achieve.
#include <iostream>
#include <memory>
#include <torch/script.h>
int main(int argc, const char* argv[]) {
if (argc != 3) {
std::cerr << "usage: example-app <path-to-exported-script-module>\n";
return -1;
}
long numElements = (1024*1024)/sizeof(float) * atoi(argv[2]);
float *a = new float[numElements];
float *b = new float[numElements];
float *c = new float[numElements*4];
for (int i = 0; i < numElements; i++){
a[i] = i;
b[i] = -i;
}
//auto options = torch::TensorOptions().dtype(torch::kFloat64);
at::Tensor a_t = torch::from_blob((float*) a, {numElements,1});
at::Tensor b_t = torch::from_blob((float*) b, {numElements,1});
at::Tensor out = torch::from_blob((float*) c, {numElements,4});
at::Tensor c_t = at::cat({a_t,b_t}, 1);
at::Tensor d_t = at::reshape(c_t, {numElements,2});
torch::jit::script::Module module;
try {
module = torch::jit::load(argv[1]);
}
catch (const c10::Error& e) {
return -1;
}
out = module.forward({d_t}).toTensor();
std::cout<< out.sizes() << "\n";
delete [] a;
delete [] b;
delete [] c;
return 0;
}
So, I am allocating memory into "c" and then I am casting creating a tensor out of this memory. I store this memory into a tensor named "out". I load the model when I call the forward method. I observe that the resulted data are copied/moved into the "out" tensor. However, I would like to instruct Torch to directly store into "out" memory. Is this possible?

Somewhere in libtorch source code (I don' remember where, I'll try to find the file), there is an operator which is something like below (notice the last &&)
torch::tensor& operator=(torch::Tensor rhs) &&;
and which does what you need if I remember correctly. Basically torch assumes that if you allocate a tensor rhs to an rvalue reference tensor, then you actually mean to copy rhs into the underlying storage.
So in your case, that would be
std::move(out) = module.forward({d_t}).toTensor();
or
torch::from_blob((float*) c, {numElements,4}) = module.forward({d_t}).toTensor();

Related

RcppArrayFire passing a matrix row as af::array input

In this simple example I would like to subset a matrix by row and pass it to another cpp function; the example demonstrates this works by passing an input array to the other function first.
#include "RcppArrayFire.h"
using namespace Rcpp;
af::array theta_check_cpp( af::array theta){
if(*theta(1).host<double>() >= 1){
theta(1) = 0;
}
return theta;
}
// [[Rcpp::export]]
af::array theta_check(RcppArrayFire::typed_array<f64> theta){
const int theta_size = theta.dims()[0];
af::array X(2, theta_size);
X(0, af::seq(theta_size)) = theta_check_cpp( theta );
X(1, af::seq(theta_size)) = theta;
// return X;
Rcpp::Rcout << " works till here";
return theta_check_cpp( X.row(1) );
}
/*** R
theta <- c( 2, 2, 2)
theta_check(theta)
*/
The constructor you are using to create X has an argument ty for the data type, which defaults to f32. Therefore X uses 32 bit floats and you cannot extract a 64 bit host pointer from that. Either use
af::array X(2, theta_size, f64);
to create an array using 64 bit doubles, or extract a 32 bit host pointer via
if(*theta(1).host<float>() >= 1){
...

random shuffle in RcppArrayFire

I am experiencing trouble getting this example to run correctly. Currently it produces the same random sample for every iteration and seed input, despite the seed changing as shown by af::getSeed().
#include "RcppArrayFire.h"
#include <random>
using namespace Rcpp;
using namespace RcppArrayFire;
// [[Rcpp::export]]
af::array random_test(RcppArrayFire::typed_array<f64> theta, int counts, int seed){
const int theta_size = theta.dims()[0];
af::array out(counts, theta_size, f64);
for(int f = 0; f < counts; f++){
af::randomEngine engine;
af_set_seed(seed + f);
//Rcpp::Rcout << af::getSeed();
af::array out_temp = af::randu(theta_size, u8, engine);
out(f, af::span) = out_temp;
// out(f, af::span) = theta(out_temp);
}
return out;
}
/*** R
theta <- 1:10
random_test(theta, 5, 1)
random_test(theta, 5, 2)
*/
The immediate problem is that you are creating a random engine within each iteration of the loop but set the seed of the global random engine. Either you set the seed of the local engine via engine.setSeed(seed), or you get rid of the local engine all together, letting af::randu default to using the global engine.
However, it would still be "unusual" to change the seed during each step of the loop. Normally one sets the seed only once, e.g.:
// [[Rcpp::depends(RcppArrayFire)]]
#include "RcppArrayFire.h"
// [[Rcpp::export]]
af::array random_test(RcppArrayFire::typed_array<f64> theta, int counts, int seed){
const int theta_size = theta.dims()[0];
af::array out(counts, theta_size, f64);
af::setSeed(seed);
for(int f = 0; f < counts; f++){
af::array out_temp = af::randu(theta_size, u8);
out(f, af::span) = out_temp;
}
return out;
}
BTW, it makes sense to parallelize this as long as your device has enough memory. For example, you could generate a random counts x theta_size matrix in one go using af::randu(counts, theta_size, u8).

Most efficient way to translate C++ classes/objects into Rcpp?

I am new to Rccp and came across a problem by translating C++ code into the Rcpp environment – and I could not find a solution so far (this is an edited version of my original post that I think was unclear):
Background: I have multiple parameters and large matrices/arrays that needs to be transferred to the C++ level. In C++, I have several functions that need to access these parameters and matrices and in some cases, change values etc. In C++ I would create classes that combine all parameters and matrices as well as the functions that need to access them. By doing so, I dod not need to pass them (each time) to the function.
Issue: I could not figure out how that may work with Rcpp. In the example below (the function is stupid, but hopefully an easy way to illustrate my issue), I create a matrix in R that is then used in C++. However, I then hand the entire matrix over to a sub-function in order to use the matrix within this function. This seems a very bad idea and I would rater like to have the matrix M in the namespace memory and access it in the sub function without cloning it.
#include <RcppArmadillo.h>
//[[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
double fnc1 (int t, int s, arma::mat M) // I would prefer not to have M in the arguments but rather available in the namespace
{
double out = M(t,s) - M(t,s);
return out;
}
// [[Rcpp::export]]
arma::mat Rout (arma::mat M)
{
int ncol = M.n_cols;
int nrow = M.n_rows;
for(int c = 0; c<ncol; ++c)
{
for(int r = 0; r<nrow; ++r)
{
M(r,c) = fnc1(r, c, M);
}
}
return M;
}
/*** R
m <- matrix(runif(50), ncol = 10, nrow = 5)
Rout(m)
*/
Okay, let's talk about R to C++. At somepoint, you have to have a function exported to R that will receive an R object and pass it back to C++. Once inside C++, the sky's the limit as to how you want to structure the interaction with that object. The thought process of:
However, I then hand the entire matrix over to a sub-function in order to use the matrix within this function. This seems a very bad idea and I would rater like to have the matrix M in the namespace memory and access it in the sub function without cloning it.
is slightly problematic as you have now just introduced a global variable called M to handle your data. If M is not initialized, then the routine will falter. If you inadvertently modify M, then the data will change for all routines. So, I'm not sure going the global variable approach is the solution you desire.
The main issue you seem to have is the emphasized portion regarding a "clone". When working with C++, the default pass by construct is to copy the object. However, unlike R, it is very easy to pass by reference by prefixing object names with & and, thus, negate a copy entirely. This localizes the process.
Pass-by-Reference Demo
#include <RcppArmadillo.h>
//[[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
double fnc1 (int t, int s, const arma::mat& M) {
double out = M(t,s) - M(t,s);
return out;
}
// [[Rcpp::export]]
arma::mat Rout (arma::mat& M) {
int ncol = M.n_cols;
int nrow = M.n_rows;
for(int c = 0; c<ncol; ++c) {
for(int r = 0; r<nrow; ++r) {
M(r,c) = fnc1(r, c, M);
}
}
return M;
}
/*** R
m <- matrix(runif(50), ncol = 10, nrow = 5)
Rout(m)
*/
Global Variable Demo
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// Create a namespace to store M
namespace toad {
arma::mat M;
}
double fnc1 (int t, int s)
{
double out = toad::M(t,s) - toad::M(t,s);
return out;
}
// [[Rcpp::export]]
void Rin (arma::mat M)
{
toad::M = M;
}
// [[Rcpp::export]]
void Rmanipulate()
{
int ncol = toad::M.n_cols;
int nrow = toad::M.n_rows;
for(int c = 0; c<ncol; ++c)
{
for(int r = 0; r<nrow; ++r)
{
toad::M(r,c) = fnc1(r, c);
}
}
}
// [[Rcpp::export]]
arma::mat Rout (){
return toad::M;
}
/*** R
m <- matrix(runif(50), ncol = 10, nrow = 5)
Rin(m)
Rmanipulate()
Rout()
*/

This function gives different result if compiled with Visual-C++ or gnu g++

The following function (extracted from a .cpp file) gives two different results (that is, the output buffer int_image is different) if executed on a PC with Visual Studio (cpu Intel i7 running Windows 7) or on my Android phone (P880). The two input buffers im1 and im2, of type int8 (a synonymous of char), are exactly the same (checked), as well as the parameters w and h. I can't understand why this is happening:
void Compute(int8* im1,
int8* im2,
int w,
int h,
int* int_image)
{
int index = 0;
int sum;
for(int i = 0; i<h; i++)
{
// reset this column sum
sum = 0;
for(int j = 0; j<w; j++)
{
int pn;
int8 v1, v2;
v1 = im1[index];
v2 = im2[index];
pn = v1*v2;
//pn = ((int)im1[index]) * ((int)im2[index]);
sum += pn;
if (i==0)
int_image[index] = sum;
else
int_image[index] = int_image[index - w] + sum;
index++;
}
}
}
Note.
The size of char images im1 and im2 can be such that an integer overflow can happen (but i think that this kind of situation is handled equally by the two compilers, but at this point i am not so sure).
Found the source of error. I defined int8 as char, believing that by default it was a signed char. Instead, it is unsigned on gcc, while signed on Visual C++. I do not know what the standard says, but i suggest to all programmers out there to use explicitly signed char and unsigned char when defining your own macro for int8 and uint8.
Interesting discussion about this on stackoverflow.

MPI-IO deadlock using MPI_File_write_all

My MPI code deadlocks when I run this simple code on 512 processes on a cluster. I am far from the memory limit. If I increase the number of procesess to 2048, which is far too many for this problem, the code runs again. The deadlock occurs in the line containing the MPI_File_write_all.
Any suggestions?
int count = imax*jmax*kmax;
// CREATE THE SUBARRAY
MPI_Datatype subarray;
int totsize [3] = {kmax, jtot, itot};
int subsize [3] = {kmax, jmax, imax};
int substart[3] = {0, mpicoordy*jmax, mpicoordx*imax};
MPI_Type_create_subarray(3, totsize, subsize, substart, MPI_ORDER_C, MPI_DOUBLE, &subarray);
MPI_Type_commit(&subarray);
// SET THE VALUE OF THE GRID EQUAL TO THE PROCESS ID FOR CHECKING
if(mpiid == 0) std::printf("Setting the value of the array\n");
for(int i=0; i<count; i++)
u[i] = (double)mpiid;
// WRITE THE FULL GRID USING MPI-IO
if(mpiid == 0) std::printf("Write the full array to disk\n");
char filename[] = "u.dump";
MPI_File fh;
if(MPI_File_open(commxy, filename, MPI_MODE_CREATE | MPI_MODE_WRONLY | MPI_MODE_EXCL, MPI_INFO_NULL, &fh))
return 1;
// select noncontiguous part of 3d array to store the selected data
MPI_Offset fileoff = 0; // the offset within the file (header size)
char name[] = "native";
if(MPI_File_set_view(fh, fileoff, MPI_DOUBLE, subarray, name, MPI_INFO_NULL))
return 1;
if(MPI_File_write_all(fh, u, count, MPI_DOUBLE, MPI_STATUS_IGNORE))
return 1;
if(MPI_File_close(&fh))
return 1;
Your code looks right upon quick inspection. I would suggest that you let your MPI-IO library help tell you what's wrong: instead of returning from error, why don't you at least display the error? Here's some code that might help:
static void handle_error(int errcode, char *str)
{
char msg[MPI_MAX_ERROR_STRING];
int resultlen;
MPI_Error_string(errcode, msg, &resultlen);
fprintf(stderr, "%s: %s\n", str, msg);
MPI_Abort(MPI_COMM_WORLD, 1);
}
Is MPI_SUCCESS guaranteed to be 0? I'd rather see
errcode = MPI_File_routine();
if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_open(1)");
Put that in and if you are doing something tricky like setting a file view with offsets that are not monotonically non-decreasing, the error string might suggest what's wrong.

Resources