| 262 | } |
| 263 | |
| 264 | Status InferenceContext::AllocateMemory(const GraphFloat32& graph, |
| 265 | const CLDevice& device, |
| 266 | CLContext* context) { |
| 267 | std::map<ValueId, int2> usages; |
| 268 | for (int op_index = 0; op_index < nodes_.size(); ++op_index) { |
| 269 | auto tensors = GetCLNodeTensors(nodes_[op_index]); |
| 270 | for (auto& tensor : tensors) { |
| 271 | AddUsage(tensor.first, op_index, &usages); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | std::vector<TensorUsageRecord<BHWC>> usage_records; |
| 276 | std::map<ValueId, ValueId> remap_from_graph_ids; |
| 277 | for (auto& usage : usages) { |
| 278 | const auto& shape = graph.GetValue(usage.first)->tensor.shape; |
| 279 | remap_from_graph_ids[usage.first] = usage_records.size(); |
| 280 | usage_records.push_back({shape, static_cast<TaskId>(usage.second.x), |
| 281 | static_cast<TaskId>(usage.second.y)}); |
| 282 | } |
| 283 | |
| 284 | ObjectsAssignment<BHWC> assignment; |
| 285 | RETURN_IF_ERROR(AssignObjectsToTensors( |
| 286 | usage_records, MemoryStrategy::EQUALITY, &assignment)); |
| 287 | |
| 288 | for (auto& node : nodes_) { |
| 289 | for (auto& id : node.inputs) { |
| 290 | ValueId new_id = assignment.object_ids[remap_from_graph_ids[id]]; |
| 291 | remap_from_graph_ids_to_shared_[id] = new_id; |
| 292 | id = new_id; |
| 293 | } |
| 294 | for (auto& id : node.outputs) { |
| 295 | ValueId new_id = assignment.object_ids[remap_from_graph_ids[id]]; |
| 296 | remap_from_graph_ids_to_shared_[id] = new_id; |
| 297 | id = new_id; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | for (auto& node : nodes_) { |
| 302 | auto tensors = GetCLNodeTensors(node); |
| 303 | for (auto& tensor : tensors) { |
| 304 | const auto& it = tensors_.find(tensor.first); |
| 305 | if (it == tensors_.end()) { |
| 306 | const auto& shape = assignment.object_sizes[tensor.first]; |
| 307 | Tensor* t = &tensors_[tensor.first]; |
| 308 | RETURN_IF_ERROR(CreateTensor(*context, device, shape.w, shape.h, |
| 309 | shape.c, tensor.second.data_type, |
| 310 | tensor.second.storage_type, t)); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | return OkStatus(); |
| 315 | } |
| 316 | |
| 317 | void InferenceContext::BindMemoryToOperations() { |
| 318 | for (auto& node : nodes_) { |
nothing calls this directly
no test coverage detected