Holds a tensor or tensor reference. For tensor references, we need a mutex to prevent concurrent access to the tensor.
| 536 | // Holds a tensor or tensor reference. For tensor references, we need |
| 537 | // a mutex to prevent concurrent access to the tensor. |
| 538 | struct TensorValue { |
| 539 | TensorValue() : mutex_if_ref(nullptr), tensor(nullptr) {} |
| 540 | explicit TensorValue(Tensor* t) : mutex_if_ref(nullptr), tensor(t) {} |
| 541 | TensorValue(mutex* mu, Tensor* t) : mutex_if_ref(mu), tensor(t) {} |
| 542 | Tensor* operator->() const { return tensor; } |
| 543 | bool is_ref() const { return mutex_if_ref != nullptr; } |
| 544 | |
| 545 | // Return the dtype of the Tensor. For references, return the underlying type. |
| 546 | DataType dtype() const { |
| 547 | if (is_ref()) { |
| 548 | return MakeRefType(tensor->dtype()); |
| 549 | } else { |
| 550 | return tensor->dtype(); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | // Return the dtype of the Tensor. For references, return the underlying type. |
| 555 | // This variation on the dtype() acquires the lock for references. |
| 556 | // |
| 557 | // TODO(b/133843385): Disallow dtype modifications |
| 558 | DataType dtype_safe() const { |
| 559 | if (is_ref()) { |
| 560 | tf_shared_lock ml(*mutex_if_ref); |
| 561 | return MakeRefType(tensor->dtype()); |
| 562 | } else { |
| 563 | return tensor->dtype(); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | mutex* mutex_if_ref; // nullptr if not a ref, != nullptr if a ref |
| 568 | Tensor* tensor; |
| 569 | }; |
| 570 | |
| 571 | // Used to store partitioned graphs from function-calling ops. |
| 572 | struct GraphCollector { |
no outgoing calls