| 192 | } |
| 193 | |
| 194 | dtype::pstring* StringTensor::mutable_data(const Place& place, |
| 195 | size_t requested_size) { |
| 196 | PADDLE_ENFORCE_GE( |
| 197 | numel(), |
| 198 | 0, |
| 199 | common::errors::PreconditionNotMet( |
| 200 | "The Tensor's element number must be equal or greater than zero. " |
| 201 | "The Tensor's shape is [", |
| 202 | dims(), |
| 203 | "] now")); |
| 204 | size_t size = numel() * SizeOf(dtype()); |
| 205 | if (requested_size && (requested_size > size)) { |
| 206 | size = requested_size; |
| 207 | } |
| 208 | |
| 209 | /* some versions of paddle::variant don't have operator!= */ |
| 210 | if (holder_ == nullptr || !(holder_->place() == place) || |
| 211 | holder_->size() < size + meta_.offset) { |
| 212 | holder_.reset(); |
| 213 | holder_ = memory_utils::AllocShared(place, size); |
| 214 | // Initialize the allocated bytes |
| 215 | init_holder(); |
| 216 | meta_.offset = 0; |
| 217 | } |
| 218 | uintptr_t ptr = reinterpret_cast<uintptr_t>(holder_->ptr()) + meta_.offset; |
| 219 | return reinterpret_cast<dtype::pstring*>(ptr); |
| 220 | } |
| 221 | |
| 222 | } // namespace phi |