devinamatthews/tblis

Wrong contraction results only on certain choices of labels

manopapad opened this issue · 1 comments

The following program executes the contraction ab,ab->a, which for two 5x6 inputs of all 1s should produce the vector [6,6,6,6,6,6]. However, this program outputs result = 36 36 36 36 3.29985e+08.

Note that the error only happens for specific choices of integers for the labels:

a = 0 b = 1
a = 128 b = 129
a = 129 b = 130
...
a = 254 b = 255
a = 255 b = 0

For example, this doesn't happen if we use the characters 'A' (decimal 65) and 'B' (decimal 66) for the labels.

#include <iostream>
#include "tblis.h"

using namespace tblis;

int main() {
  len_type lens_A[2];
  lens_A[0] = 5;
  lens_A[1] = 6;
  len_type size_A = 5 * 6;
  stride_type strides_A[2];
  strides_A[0] = 6;
  strides_A[1] = 1;
  double* data_A = new double[size_A];
  for (int i = 0; i < size_A; ++i) {
    data_A[i] = 1.0;
  }
  tblis_tensor A;
  tblis_init_tensor_d(&A, 2, lens_A, data_A, strides_A);

  len_type lens_C[1];
  lens_C[0] = 5;
  len_type size_C = 5;
  stride_type strides_C[1];
  strides_C[0] = 1;
  double* data_C = new double[size_C];
  for (int i = 0; i < size_C; ++i) {
    data_C[i] = 0.0;
  }
  tblis_tensor C;
  tblis_init_tensor_d(&C, 1, lens_C, data_C, strides_C);

  label_type A_modes[2];
  A_modes[0] = 0;
  A_modes[1] = 1;
  label_type C_modes[1];
  C_modes[0] = 0;
  tblis_tensor_mult(tblis_single, NULL, &A, A_modes, &A, A_modes, &C, C_modes);

  std::cout << "result =";
  for (int i = 0; i < 5; ++i) {
    std::cout << " " << data_C[i];
  }
  std::cout << std::endl;
}

@manopapad thanks for pointing this out. Turns out it was a problem with a set union algorithm!