Return allocator from platform info if non-null, or populate and return a pointer to the allocator adapter with allocator from context. This is necessary because for XLA devices the underlying TF allocator returns dummy tensors.
| 181 | // This is necessary because for XLA devices the underlying TF allocator returns |
| 182 | // dummy tensors. |
| 183 | static std::shared_ptr<se::DeviceMemoryAllocator> GetAllocator( |
| 184 | OpKernelContext* ctx, const XlaPlatformInfo& platform_info) { |
| 185 | if (platform_info.custom_allocator()) { |
| 186 | return platform_info.custom_allocator(); |
| 187 | } |
| 188 | auto *alloc = ctx->device()->GetAllocator({}); |
| 189 | auto dc = ctx->op_device_context(); |
| 190 | if (!dc) { |
| 191 | // Stream is not set for the host platform. |
| 192 | se::Platform* platform = |
| 193 | se::MultiPlatformManager::PlatformWithId(platform_info.platform_id()) |
| 194 | .ValueOrDie(); |
| 195 | return std::make_shared<se::TfAllocatorAdapter>(alloc, platform); |
| 196 | } else { |
| 197 | return std::make_shared<se::TfAllocatorAdapter>(alloc, dc->stream()); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | } // namespace |
| 202 |
no test coverage detected