tensorflow/compiler/tf2xla/rearrange_function_argument.cc
// Given original input types, check if we need to rewrite the function (by
// checking if all DT_RESOURCE inputs are in the end). If the function needs to
// be rewritten, `resource_input_count` will be set to number of DT_RESOURCE
// inputs, and `index_mapping` will hold a mapping for original input index to
// rearranged input index.
Given original input types, check if we need to rewrite the function (by
checking if all DT_RESOURCE inputs are in the end). If the function needs to
be rewritten, `resource_input_count` will be set to number of DT_RESOURCE
inputs, and `index_mapping` will hold a mapping for original input index to
rearranged input index.
Given original input types check if we need to rewrite the function ( by checking if all DT_RESOURCE inputs are in the end ) . If the function needs to be rewritten ` resource_input_count ` will be set to number of DT_RESOURCE inputs and ` index_mapping ` will hold a mapping for original input index to rearranged input index .
Status InputTypesNeedsRearrange(const std::vector<DataType>& in_types,
                                bool* need_rewrite, int* resource_input_count,
                                std::vector<int>* index_mapping) {
  int first_resource_index = -1;
  for (int i = 0, end = in_types.size(); i < end; i++) {
    DataType type = in_types[i];
    if (type == DT_RESOURCE) {
      first_resource_index = i;
      break;
    }
  }
  if (first_resource_index == -1) {
    *need_rewrite = false;
    return Status::OK();
  }
  *need_rewrite = false;
  for (int i = first_resource_index + 1, end = in_types.size(); i < end; i++) {
    if (in_types[i] != DT_RESOURCE) {
      *need_rewrite = true;
      break;
    }
  }
  if (!*need_rewrite) {
    return Status::OK();
  }
  *resource_input_count = 0;
  for (int i = 0, end = in_types.size(); i < end; i++) {
    DataType type = in_types[i];
    if (type == DT_RESOURCE) {
      ++(*resource_input_count);
    }
  }
  int non_resource_index = 0,
      resource_index = in_types.size() - *resource_input_count;
  index_mapping->resize(in_types.size());
  for (int i = 0, end = in_types.size(); i < end; i++) {
    if (in_types[i] != DT_RESOURCE) {
      (*index_mapping)[i] = non_resource_index;
      non_resource_index++;
    } else {
      (*index_mapping)[i] = resource_index;
      resource_index++;
    }
  }
  return Status::OK();
}
Status InputTypesNeedsRearrange ( const std :: vector < DataType > & in_types , bool * need_rewrite , int * resource_input_count , std :: vector < int > * index_mapping ) { int first_resource_index = -1 ; for ( int i = 0 , end = in_types . size ( ) ; i < end ; i ++ ) { DataType type = in_types [ i ] ; if ( type == DT_RESOURCE ) { first_resource_index = i ; break ; } } if ( first_resource_index == -1 ) { * need_rewrite = false ; return Status :: OK ( ) ; } * need_rewrite = false ; for ( int i = first_resource_index + 1 , end = in_types . size ( ) ; i < end ; i ++ ) { if ( in_types [ i ] != DT_RESOURCE ) { * need_rewrite = true ; break ; } } if ( ! * need_rewrite ) { return Status :: OK ( ) ; } * resource_input_count = 0 ; for ( int i = 0 , end = in_types . size ( ) ; i < end ; i ++ ) { DataType type = in_types [ i ] ; if ( type == DT_RESOURCE ) { ++ ( * resource_input_count ) ; } } int non_resource_index = 0 , resource_index = in_types . size ( ) - * resource_input_count ; index_mapping -> resize ( in_types . size ( ) ) ; for ( int i = 0 , end = in_types . size ( ) ; i < end ; i ++ ) { if ( in_types [ i ] != DT_RESOURCE ) { ( * index_mapping ) [ i ] = non_resource_index ; non_resource_index ++ ; } else { ( * index_mapping ) [ i ] = resource_index ; resource_index ++ ; } } return Status :: OK ( ) ; }
Given original input types, check if we need to rewrite the function (by
checking if all DT_RESOURCE inputs are in the end).
Given original input types check if we need to rewrite the function ( by checking if all DT_RESOURCE inputs are in the end ) .
// No resource input. No need to rewrite.