| 1173 | } |
| 1174 | |
| 1175 | void MutableGraphView::ApplyNodeUpdates() { |
| 1176 | for (auto& diff : mutation_.updated_nodes_) { |
| 1177 | if (internal::IsEmpty(&diff)) { |
| 1178 | continue; |
| 1179 | } |
| 1180 | MutableNodeView& node_view = nodes_[diff.node_index]; |
| 1181 | diff.node_index = internal::kMissingIndex; |
| 1182 | // Clean up node view. |
| 1183 | node_view.update_index_ = internal::kMissingIndex; |
| 1184 | |
| 1185 | NodeDef* node_def = node_view.node(); |
| 1186 | |
| 1187 | // Set updated fields and attributes of node. |
| 1188 | if (diff.update_op) { |
| 1189 | node_def->set_op(diff.op); |
| 1190 | } |
| 1191 | if (diff.update_device) { |
| 1192 | node_def->set_device(diff.device); |
| 1193 | } |
| 1194 | node_def->mutable_attr()->swap(diff.processed_attrs); |
| 1195 | |
| 1196 | // Updated fanins. Only one of `regular_inputs_to_remove_` or |
| 1197 | // `regular_inputs_to_add_` can be set. |
| 1198 | if (diff.num_regular_inputs_to_remove > 0) { |
| 1199 | // Truncate trailing regular fanins. |
| 1200 | const int first_index = |
| 1201 | node_view.NumRegularFanins() - diff.num_regular_inputs_to_remove; |
| 1202 | for (int i = first_index; i < node_view.NumRegularFanins(); ++i) { |
| 1203 | RemoveRegularFaninFanoutInternal(&node_view, i); |
| 1204 | } |
| 1205 | node_view.regular_fanins_.resize(first_index); |
| 1206 | node_def->mutable_input()->DeleteSubrange( |
| 1207 | node_view.NumRegularFanins(), diff.num_regular_inputs_to_remove); |
| 1208 | } else if (diff.num_regular_inputs_to_add > 0) { |
| 1209 | // Append regular fanins. |
| 1210 | node_def->mutable_input()->Reserve(node_def->mutable_input()->size() + |
| 1211 | diff.num_regular_inputs_to_add); |
| 1212 | int curr_index = node_view.NumRegularFanins(); |
| 1213 | int curr_control_start = curr_index; |
| 1214 | for (const SafeTensorId& fanin : diff.regular_inputs_to_add) { |
| 1215 | AddRegularFaninInternal(&node_view, fanin); |
| 1216 | node_def->add_input(SafeTensorIdToString(fanin)); |
| 1217 | node_def->mutable_input()->SwapElements(curr_index, |
| 1218 | node_def->input_size() - 1); |
| 1219 | if (curr_control_start == curr_index) { |
| 1220 | curr_control_start = node_def->input_size() - 1; |
| 1221 | } |
| 1222 | ++curr_index; |
| 1223 | } |
| 1224 | // Rotate shifted controlling fanins to match up with |
| 1225 | // `node_view.controlling_fanins_` as `num_regular_inputs_to_add_` may not |
| 1226 | // be a multiple of `num_regular_inputs_to_add_`. This is to prevent |
| 1227 | // rehashing controlling fanins in `node_view.controlling_fanins_index_`. |
| 1228 | if (node_view.NumControllingFanins() > 1 && |
| 1229 | curr_control_start != node_view.NumRegularFanins()) { |
| 1230 | std::rotate( |
| 1231 | node_def->mutable_input()->begin() + node_view.NumRegularFanins(), |
| 1232 | node_def->mutable_input()->begin() + curr_control_start, |
nothing calls this directly
no test coverage detected