iree/tools/iree-e2e-matmul-test.c
static iree_status_t iree_check_matmul_failure(
    FILE* file, iree_vm_value_t actual_value, iree_vm_value_t expected_value,
    iree_hal_dim_t row, iree_hal_dim_t col, iree_hal_buffer_view_t* lhs,
    iree_hal_buffer_view_t* rhs, iree_hal_buffer_view_t* acc,
    iree_hal_buffer_view_t* actual_result,
    iree_hal_buffer_view_t* expected_result) {
  fprintf(file,
          "\n\nerror: the actual and expected result matrices disagree "
          "at row %d, column %d.\n\n",
          row, col);
  char actual_value_buf[32];
  char expected_value_buf[32];
  snprintf_value(actual_value_buf, sizeof actual_value_buf, actual_value,
                 PRECISION_HIGH);
  snprintf_value(expected_value_buf, sizeof expected_value_buf, expected_value,
                 PRECISION_HIGH);
  fprintf(file, "actual value: %s\n", actual_value_buf);
  fprintf(file, "expected value: %s\n", expected_value_buf);

  iree_hal_dim_t m_size, k_size, n_size;
  IREE_RETURN_IF_ERROR(get_matmul_sizes(lhs, rhs, acc, actual_result, &m_size,
                                        &k_size, &n_size));
  iree_hal_dim_t context = 8;
  const char* context_env = getenv("IREE_MATMUL_TEST_SHOW_CONTEXT");
  if (context_env) {
    if (1 != sscanf(context_env, "%d", &context)) {
      return iree_make_status(IREE_STATUS_INVALID_ARGUMENT,
                              "Failed to parse IREE_MATMUL_TEST_SHOW_CONTEXT "
                              "as \"%%d\". Got \"%s\"",
                              context_env);
    }
  }
  int m_start = iree_max(0, row - context);
  int m_end = iree_min(m_size, row + context);
  int n_start = iree_max(0, col - context);
  int n_end = iree_min(n_size, col + context);
  // We have a lot more freedom to pick k_start, k_end, since these parameters
  // only affect which regions of the input lhs and rhs matrices are printed.
  // If we were only testing random lhs and rhs, we would just pick
  // k_start = 0 and any reasonable k_end value. Since we are often using
  // identity matrices for lhs and rhs, and we expect the majority of
  // test failures to occur with such identity matrices, we try to pick
  // k_start and k_end so that nontrivial regions of identity matrices will be
  // printed. That means that we try to have [k_start, k_end) intervals
  // overlap [m_start, m_end) and [n_start, n_end).
  int k_start = iree_max(0, iree_min(m_start, n_start));
  int k_end = iree_min(k_size, iree_max(m_end, n_end));
  // [k_start, k_end) could be arbitrarily long at this point. Constrain it a
  // bit to avoid huge output.
  k_end = iree_min(k_end, k_start + 4 * context);

  fprintf(file, "\n");
  print_matrix(file, "left-hand side", PRECISION_LOW, m_start, m_end, k_start,
               k_end, lhs, NULL);
  fprintf(file, "\n");
  print_matrix(file, "right-hand side", PRECISION_LOW, k_start, k_end, n_start,
               n_end, rhs, NULL);
  fprintf(file, "\n");
  print_matrix(file, "input accumulator", PRECISION_LOW, m_start, m_end,
               n_start, n_end, acc, NULL);
  fprintf(file, "\n");
  print_matrix(file, "expected result", PRECISION_LOW, m_start, m_end, n_start,
               n_end, expected_result, actual_result);
  fprintf(file, "\n");
  print_matrix(file, "actual result", PRECISION_LOW, m_start, m_end, n_start,
               n_end, actual_result, expected_result);
  fprintf(file, "\n");
  return iree_make_status(IREE_STATUS_ABORTED,
                          "matmul test failure, details logged above");
}
// Helper for iree_check_matmul: handler for the failure case.
Helper for iree_check_matmul: handler for the failure case.
Helper for iree_check_matmul : handler for the failure case .
static iree_status_t iree_check_matmul_failure(
    FILE* file, iree_vm_value_t actual_value, iree_vm_value_t expected_value,
    iree_hal_dim_t row, iree_hal_dim_t col, iree_hal_buffer_view_t* lhs,
    iree_hal_buffer_view_t* rhs, iree_hal_buffer_view_t* acc,
    iree_hal_buffer_view_t* actual_result,
    iree_hal_buffer_view_t* expected_result) {
  fprintf(file,
          "\n\nerror: the actual and expected result matrices disagree "
          "at row %d, column %d.\n\n",
          row, col);
  char actual_value_buf[32];
  char expected_value_buf[32];
  snprintf_value(actual_value_buf, sizeof actual_value_buf, actual_value,
                 PRECISION_HIGH);
  snprintf_value(expected_value_buf, sizeof expected_value_buf, expected_value,
                 PRECISION_HIGH);
  fprintf(file, "actual value: %s\n", actual_value_buf);
  fprintf(file, "expected value: %s\n", expected_value_buf);
  iree_hal_dim_t m_size, k_size, n_size;
  IREE_RETURN_IF_ERROR(get_matmul_sizes(lhs, rhs, acc, actual_result, &m_size,
                                        &k_size, &n_size));
  iree_hal_dim_t context = 8;
  const char* context_env = getenv("IREE_MATMUL_TEST_SHOW_CONTEXT");
  if (context_env) {
    if (1 != sscanf(context_env, "%d", &context)) {
      return iree_make_status(IREE_STATUS_INVALID_ARGUMENT,
                              "Failed to parse IREE_MATMUL_TEST_SHOW_CONTEXT "
                              "as \"%%d\". Got \"%s\"",
                              context_env);
    }
  }
  int m_start = iree_max(0, row - context);
  int m_end = iree_min(m_size, row + context);
  int n_start = iree_max(0, col - context);
  int n_end = iree_min(n_size, col + context);
  int k_start = iree_max(0, iree_min(m_start, n_start));
  int k_end = iree_min(k_size, iree_max(m_end, n_end));
  k_end = iree_min(k_end, k_start + 4 * context);
  fprintf(file, "\n");
  print_matrix(file, "left-hand side", PRECISION_LOW, m_start, m_end, k_start,
               k_end, lhs, NULL);
  fprintf(file, "\n");
  print_matrix(file, "right-hand side", PRECISION_LOW, k_start, k_end, n_start,
               n_end, rhs, NULL);
  fprintf(file, "\n");
  print_matrix(file, "input accumulator", PRECISION_LOW, m_start, m_end,
               n_start, n_end, acc, NULL);
  fprintf(file, "\n");
  print_matrix(file, "expected result", PRECISION_LOW, m_start, m_end, n_start,
               n_end, expected_result, actual_result);
  fprintf(file, "\n");
  print_matrix(file, "actual result", PRECISION_LOW, m_start, m_end, n_start,
               n_end, actual_result, expected_result);
  fprintf(file, "\n");
  return iree_make_status(IREE_STATUS_ABORTED,
                          "matmul test failure, details logged above");
}
static iree_status_t iree_check_matmul_failure ( FILE * file , iree_vm_value_t actual_value , iree_vm_value_t expected_value , iree_hal_dim_t row , iree_hal_dim_t col , iree_hal_buffer_view_t * lhs , iree_hal_buffer_view_t * rhs , iree_hal_buffer_view_t * acc , iree_hal_buffer_view_t * actual_result , iree_hal_buffer_view_t * expected_result ) { fprintf ( file , " \n \n " " \n \n " , row , col ) ; char actual_value_buf [ 32 ] ; char expected_value_buf [ 32 ] ; snprintf_value ( actual_value_buf , sizeof actual_value_buf , actual_value , PRECISION_HIGH ) ; snprintf_value ( expected_value_buf , sizeof expected_value_buf , expected_value , PRECISION_HIGH ) ; fprintf ( file , " \n " , actual_value_buf ) ; fprintf ( file , " \n " , expected_value_buf ) ; iree_hal_dim_t m_size , k_size , n_size ; IREE_RETURN_IF_ERROR ( get_matmul_sizes ( lhs , rhs , acc , actual_result , & m_size , & k_size , & n_size ) ) ; iree_hal_dim_t context = 8 ; const char * context_env = getenv ( " " ) ; if ( context_env ) { if ( 1 != sscanf ( context_env , " " , & context ) ) { return iree_make_status ( IREE_STATUS_INVALID_ARGUMENT , " " " \" \" \" \" " , context_env ) ; } } int m_start = iree_max ( 0 , row - context ) ; int m_end = iree_min ( m_size , row + context ) ; int n_start = iree_max ( 0 , col - context ) ; int n_end = iree_min ( n_size , col + context ) ; int k_start = iree_max ( 0 , iree_min ( m_start , n_start ) ) ; int k_end = iree_min ( k_size , iree_max ( m_end , n_end ) ) ; k_end = iree_min ( k_end , k_start + 4 * context ) ; fprintf ( file , " \n " ) ; print_matrix ( file , " " , PRECISION_LOW , m_start , m_end , k_start , k_end , lhs , NULL ) ; fprintf ( file , " \n " ) ; print_matrix ( file , " " , PRECISION_LOW , k_start , k_end , n_start , n_end , rhs , NULL ) ; fprintf ( file , " \n " ) ; print_matrix ( file , " " , PRECISION_LOW , m_start , m_end , n_start , n_end , acc , NULL ) ; fprintf ( file , " \n " ) ; print_matrix ( file , " " , PRECISION_LOW , m_start , m_end , n_start , n_end , expected_result , actual_result ) ; fprintf ( file , " \n " ) ; print_matrix ( file , " " , PRECISION_LOW , m_start , m_end , n_start , n_end , actual_result , expected_result ) ; fprintf ( file , " \n " ) ; return iree_make_status ( IREE_STATUS_ABORTED , " " ) ; }
Helper for iree_check_matmul: handler for the failure case.
Helper for iree_check_matmul : handler for the failure case .
// We have a lot more freedom to pick k_start, k_end, since these parameters // only affect which regions of the input lhs and rhs matrices are printed. // If we were only testing random lhs and rhs, we would just pick // k_start = 0 and any reasonable k_end value. Since we are often using // identity matrices for lhs and rhs, and we expect the majority of // test failures to occur with such identity matrices, we try to pick // k_start and k_end so that nontrivial regions of identity matrices will be // printed. That means that we try to have [k_start, k_end) intervals // overlap [m_start, m_end) and [n_start, n_end). // [k_start, k_end) could be arbitrarily long at this point. Constrain it a // bit to avoid huge output.