| 148 | } |
| 149 | |
| 150 | void* StringTensor::AllocateFrom(Allocator* allocator, |
| 151 | DataType dtype, |
| 152 | size_t requested_size, |
| 153 | bool fake_alloc) { |
| 154 | PADDLE_ENFORCE_NOT_NULL( |
| 155 | allocator, |
| 156 | errors::InvalidArgument( |
| 157 | "Required allocator shall not be nullptr, but received nullptr.")); |
| 158 | |
| 159 | size_t bytes = numel() * SizeOf(this->dtype()); |
| 160 | if (fake_alloc) { |
| 161 | bytes = 0; |
| 162 | } else { |
| 163 | PADDLE_ENFORCE_EQ( |
| 164 | valid(), |
| 165 | true, |
| 166 | errors::PreconditionNotMet("The meta data must be valid when call the " |
| 167 | "mutable data function.")); |
| 168 | if (requested_size) { |
| 169 | PADDLE_ENFORCE_GE(requested_size, |
| 170 | bytes, |
| 171 | errors::InvalidArgument( |
| 172 | "The reserved size %d should be enough to meet the " |
| 173 | "volume required by metadata %d.", |
| 174 | requested_size, |
| 175 | bytes)); |
| 176 | |
| 177 | bytes = requested_size; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if (!holder_ || holder_->size() < bytes + meta_.offset) { |
| 182 | meta_.offset = 0; |
| 183 | VLOG(10) << "Allocate string data with bytes: " << bytes; |
| 184 | holder_.reset(); |
| 185 | holder_ = allocator->Allocate(bytes); |
| 186 | // Initialize the allocated bytes |
| 187 | init_holder(); |
| 188 | meta_.offset = 0; |
| 189 | } |
| 190 | uintptr_t ptr = reinterpret_cast<uintptr_t>(holder_->ptr()) + meta_.offset; |
| 191 | return reinterpret_cast<void*>(ptr); |
| 192 | } |
| 193 | |
| 194 | dtype::pstring* StringTensor::mutable_data(const Place& place, |
| 195 | size_t requested_size) { |