tensorflow/lite/toco/graph_transformations/merge_reshape_into_preceding_transpose.cc
// Returns whether the reshape could be a transpose.
Returns whether the reshape could be a transpose.
Returns whether the reshape could be a transpose .
std::vector<int32> ReshapeToTranspose(const Model& model,
                                      const TensorFlowReshapeOperator* op) {
  CHECK(!op->shape.empty());
  CHECK(model.HasArray(op->inputs[0]));
  CHECK(model.HasArray(op->outputs[0]));
  const auto& input_array = model.GetArray(op->inputs[0]);
  const auto& output_array = model.GetArray(op->outputs[0]);
  CHECK(input_array.has_shape());
  CHECK(output_array.has_shape());
  std::vector<int> in_shape = input_array.shape().dims();
  std::vector<int> out_shape = output_array.shape().dims();
  std::vector<int> one_indices;
  std::vector<int> not_one_indices;
  for (size_t i = 0; i < in_shape.size(); i++) {
    if (in_shape[i] == 1) {
      one_indices.push_back(i);
    } else {
      not_one_indices.push_back(i);
    }
  }
  std::vector<int> perm;
  perm.reserve(in_shape.size());
  int one_index = 0;
  int not_one_index = 0;
  for (const auto val : out_shape) {
    if (val == 1) {
      perm.push_back(one_indices[one_index]);
      one_index++;
    } else {
      perm.push_back(not_one_indices[not_one_index]);
      not_one_index++;
    }
  }
  return perm;
}
std :: vector < int32 > ReshapeToTranspose ( const Model & model , const TensorFlowReshapeOperator * op ) { CHECK ( ! op -> shape . empty ( ) ) ; CHECK ( model . HasArray ( op -> inputs [ 0 ] ) ) ; CHECK ( model . HasArray ( op -> outputs [ 0 ] ) ) ; const auto & input_array = model . GetArray ( op -> inputs [ 0 ] ) ; const auto & output_array = model . GetArray ( op -> outputs [ 0 ] ) ; CHECK ( input_array . has_shape ( ) ) ; CHECK ( output_array . has_shape ( ) ) ; std :: vector < int > in_shape = input_array . shape ( ) . dims ( ) ; std :: vector < int > out_shape = output_array . shape ( ) . dims ( ) ; std :: vector < int > one_indices ; std :: vector < int > not_one_indices ; for ( size_t i = 0 ; i < in_shape . size ( ) ; i ++ ) { if ( in_shape [ i ] == 1 ) { one_indices . push_back ( i ) ; } else { not_one_indices . push_back ( i ) ; } } std :: vector < int > perm ; perm . reserve ( in_shape . size ( ) ) ; int one_index = 0 ; int not_one_index = 0 ; for ( const auto val : out_shape ) { if ( val == 1 ) { perm . push_back ( one_indices [ one_index ] ) ; one_index ++ ; } else { perm . push_back ( not_one_indices [ not_one_index ] ) ; not_one_index ++ ; } } return perm ; }
Returns whether the reshape could be a transpose.
Returns whether the reshape could be a transpose .
// Separate into one indices and not one indices. // Reorder the vertices.