| 52 | } |
| 53 | |
| 54 | static Status ReadOpHistory(Env* env, const string& file, |
| 55 | const string& directory, |
| 56 | OpCompatibilityLib::OpHistory* out) { |
| 57 | // Read op history form `directory` if it exists there. |
| 58 | std::vector<string> matching_files; |
| 59 | Status status = env->GetMatchingPaths(io::JoinPath(directory, "*.pbtxt"), |
| 60 | &matching_files); |
| 61 | if (status.ok() && !matching_files.empty()) { |
| 62 | printf("Reading op history from %s/*.pbtxt...\n", directory.c_str()); |
| 63 | std::sort(matching_files.begin(), matching_files.end()); |
| 64 | for (const string& full_file : matching_files) { |
| 65 | string op_history_str; |
| 66 | TF_RETURN_IF_ERROR(ReadFileToString(env, full_file, &op_history_str)); |
| 67 | OpList in_op_history; |
| 68 | protobuf::TextFormat::ParseFromString(op_history_str, &in_op_history); |
| 69 | const string file_tail = FileNameFromOpName(in_op_history.op(0).name()); |
| 70 | const string expected = io::JoinPath(directory, file_tail); |
| 71 | if (full_file != expected) { |
| 72 | return errors::Internal("Expected file paths to match but '", full_file, |
| 73 | "' != '", expected, "'"); |
| 74 | } |
| 75 | out->emplace_back(file_tail, in_op_history); |
| 76 | } |
| 77 | } else { // Otherwise, fall back to reading op history from `file`. |
| 78 | printf("Reading op history from %s...\n", file.c_str()); |
| 79 | string op_history_str; |
| 80 | TF_RETURN_IF_ERROR(ReadFileToString(env, file, &op_history_str)); |
| 81 | OpList in_op_history; |
| 82 | protobuf::TextFormat::ParseFromString(op_history_str, &in_op_history); |
| 83 | // Convert from a linear OpList to OpHistory format with one OpList per |
| 84 | // unique op name. |
| 85 | int start = 0; |
| 86 | while (start < in_op_history.op_size()) { |
| 87 | int end = start + 1; |
| 88 | while (end < in_op_history.op_size() && |
| 89 | in_op_history.op(start).name() == in_op_history.op(end).name()) { |
| 90 | ++end; |
| 91 | } |
| 92 | AddNewOpToHistory(in_op_history.op(start), out); |
| 93 | for (++start; start < end; ++start) { |
| 94 | *out->back().second.add_op() = in_op_history.op(start); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | return Status::OK(); |
| 99 | } |
| 100 | |
| 101 | OpCompatibilityLib::OpCompatibilityLib(const string& ops_prefix, |
| 102 | const string& history_version, |
no test coverage detected