   THCudaCheck(THCudaFree(state, info_gpu));
 
   THCudaCheck(THCudaFree(state, d_matrices1));
-  THCudaCheck(THCudaFree(state, d_matrices1_const));
   THCudaCheck(THCudaFree(state, d_matrices2));
 
-  THCTensor_(freeCopyTo)(state, output, input);
+  THCTensor_(free)(state, input);
 #endif
 }
 

  magma_free_pinned(ipiv);
  THCTensor_(freeCopyTo)(state, input, ra_);
#else
  THArgCheck(a->nDimension == 2, 2, "A should be 2 dimensional");
  THArgCheck(a->size[0] == a->size[1], 2, "A should be square");

  int n = a->size[0];

  // input
  THCTensor *input = THCTensor_(newColumnMajor)(state, ra_, a);
  // output
  THCTensor *output = THCTensor_(newColumnMajor)(state, ra_, a);

  size_t matrices_size = sizeof(real*);

  real **matrices1 = (real **)THAlloc(matrices_size);
  const real **matrices1_const = (const real **)THAlloc(matrices_size);
  real **matrices2 = (real **)THAlloc(matrices_size);
  matrices1[0] = THCTensor_(data)(state, input);
  matrices1_const[0] = THCTensor_(data)(state, input);
  matrices2[0] = THCTensor_(data)(state, output);

  // Copy pointers to device.
  real **d_matrices1, **d_matrices2;
  const real **d_matrices1_const;
  THCudaCheck(THCudaMalloc(state, (void**)&d_matrices1, matrices_size));
  THCudaCheck(THCudaMalloc(state, (void**)&d_matrices1_const, matrices_size));
  THCudaCheck(THCudaMalloc(state, (void**)&d_matrices2, matrices_size));

  THCudaCheck(cudaMemcpyAsync(d_matrices1, matrices1, matrices_size,
                              cudaMemcpyHostToDevice, THCState_getCurrentStream(state)));
  THCudaCheck(cudaMemcpyAsync(d_matrices1_const, matrices1_const, matrices_size,
                              cudaMemcpyHostToDevice, THCState_getCurrentStream(state)));
  THCudaCheck(cudaMemcpyAsync(d_matrices2, matrices2, matrices_size,
                              cudaMemcpyHostToDevice, THCState_getCurrentStream(state)));
  int info;
  int *info_gpu;


THC_API void THCTensor_(getri)(THCState *state, THCTensor *ra_, THCTensor *a)
{
  THArgCheck(a->nDimension == 2, 2, "A should be 2 dimensional");
  THArgCheck(a->size[0] == a->size[1], 2, "A should be square");

#ifdef USE_MAGMA
  int info;
  int n = a->size[0];
  int lwork = n * magma_get_sgetri_nb(n);
