| 206 | |
| 207 | namespace { |
| 208 | void RunCallableHelper(tensorflow::Session* session, int64_t handle, |
| 209 | PyObject* feed_values, TF_Status* out_status, |
| 210 | PyObjectVector* out_values, TF_Buffer* run_metadata) { |
| 211 | // Convert feed values to a vector of tensorflow::Tensor objects. |
| 212 | std::vector<Tensor> input_tensors; |
| 213 | Status s; |
| 214 | { |
| 215 | feed_values = |
| 216 | PySequence_Fast(feed_values, "feed_values must be a sequence"); |
| 217 | if (feed_values == nullptr) return; |
| 218 | Safe_PyObjectPtr feed_values_holder(make_safe(feed_values)); |
| 219 | Py_ssize_t len = PySequence_Fast_GET_SIZE(feed_values); |
| 220 | input_tensors.reserve(len); |
| 221 | for (Py_ssize_t i = 0; i < len; ++i) { |
| 222 | PyObject* elem = PySequence_Fast_GET_ITEM(feed_values, i); |
| 223 | if (!elem) { |
| 224 | Set_TF_Status_from_Status( |
| 225 | out_status, errors::Internal("Could not get feed value ", i)); |
| 226 | return; |
| 227 | } |
| 228 | Tensor t; |
| 229 | s = NdarrayToTensor(elem, &t); |
| 230 | if (!s.ok()) { |
| 231 | Set_TF_Status_from_Status(out_status, s); |
| 232 | return; |
| 233 | } |
| 234 | input_tensors.push_back(std::move(t)); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | // Allocate a RunMetadata protobuf object to receive the metadata, |
| 239 | // if the caller is expecting any. |
| 240 | std::unique_ptr<RunMetadata> run_metadata_proto; |
| 241 | if (run_metadata != nullptr) { |
| 242 | run_metadata_proto.reset(new RunMetadata); |
| 243 | } |
| 244 | |
| 245 | // Run the callable. |
| 246 | std::vector<Tensor> output_tensors; |
| 247 | Py_BEGIN_ALLOW_THREADS; |
| 248 | s = session->RunCallable(handle, input_tensors, &output_tensors, |
| 249 | run_metadata_proto.get()); |
| 250 | Py_END_ALLOW_THREADS; |
| 251 | |
| 252 | if (!s.ok()) { |
| 253 | Set_TF_Status_from_Status(out_status, s); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | // If requested, serialize the RunMetadata to pass it back to the caller. |
| 258 | if (run_metadata != nullptr) { |
| 259 | s = MessageToBuffer(*run_metadata_proto, run_metadata); |
| 260 | if (!s.ok()) { |
| 261 | Set_TF_Status_from_Status(out_status, s); |
| 262 | return; |
| 263 | } |
| 264 | } |
| 265 |
no test coverage detected