bool VerifyLoomMetadata()

in tensorflow_fold/loom/weaver.cc [105:182]


bool VerifyLoomMetadata(const LoomMetadata &metadata, string *error_string) {
  if (metadata.max_depth() < -1) {
    *error_string = StrCat(
        "metadata.max_depth must be -1 or greater.  Got: ",
        metadata.max_depth());
    return false;
  }

  tensor_idx_t num_type_shapes = metadata.type_shape_metadata_size();
  tensor_idx_t num_ops = metadata.op_metadata_size();

  for (tensor_idx_t op_idx = 0; op_idx < num_ops; ++op_idx) {
    const auto& op = metadata.op_metadata(op_idx);
    if (op.input_ts_idx_size() <= 0) {
      *error_string = StrCat(
          "metadata.op_metadata[", op_idx,
          "] should have at least one input.");
      return false;
    }
    for (tensor_idx_t ts_idx : op.input_ts_idx()) {
      if (!FastBoundsCheck(ts_idx, num_type_shapes)) {
        *error_string = StrCat(
            "metadata.op_metadata[", op_idx,
            "] has an invalid input typeshape: ", ts_idx);
        return false;
      }
    }

    if (op.output_ts_idx_size() <= 0) {
      *error_string = StrCat(
          "metadata.op_metadata[", op_idx,
          "] should have at least one output.");
      return false;
    }
    for (tensor_idx_t ts_idx : op.output_ts_idx()) {
      if (!FastBoundsCheck(ts_idx, num_type_shapes)) {
        *error_string = StrCat(
            "metadata.op_metadata[", op_idx,
            "] has an invalid output typeshape: ", ts_idx);
        return false;
      }
    }
  }

  if (num_type_shapes > num_ops) {
    *error_string = StrCat(
        "metadata specifies num_type_shapes (", num_type_shapes,
        ") which ought to be at most num_ops (", num_ops, ")");
    return false;
  }

  for (tensor_idx_t ts_idx = 0; ts_idx < num_type_shapes; ++ts_idx) {
    const auto& op = metadata.op_metadata(ts_idx);
    if (op.input_ts_idx_size() != 1) {
      *error_string = StrCat(
        "PassThrough Op ", ts_idx, " ought to have a single input.");
      return false;
    }
    if (op.output_ts_idx_size() != 1) {
      *error_string = StrCat(
        "PassThrough Op ", ts_idx, " ought to have a single output.");
      return false;
    }
    if (op.input_ts_idx(0) != ts_idx) {
      *error_string = StrCat(
        "PassThrough Op ", ts_idx, " input has the wrong TypeShape:",
        op.input_ts_idx(0));
      return false;
    }
    if (op.output_ts_idx(0) != ts_idx) {
      *error_string = StrCat(
        "PassThrough Op ", ts_idx, " output has the wrong TypeShape:",
        op.output_ts_idx(0));
      return false;
    }
  }
  return true;
}