framework/pytorch/op_flat.cc
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "framework/pytorch/op_flat.h" | ||
| 2 | |||
| 3 | #include <torch/extension.h> | ||
| 4 | |||
| 5 | #include "framework/pytorch/op_internal.h" | ||
| 6 | |||
| 7 | namespace recstore { | ||
| 8 | namespace framework { | ||
| 9 | namespace { | ||
| 10 | |||
| 11 | torch::Tensor | ||
| 12 | ✗ | local_lookup_flat_torch(const torch::Tensor& keys, int64_t embedding_dim) { | |
| 13 | ✗ | return std::get<0>(CachedReadTorch(keys, embedding_dim, true)); | |
| 14 | } | ||
| 15 | |||
| 16 | ✗ | void local_update_flat_torch(const std::string& table_name, | |
| 17 | const torch::Tensor& keys, | ||
| 18 | const torch::Tensor& grads) { | ||
| 19 | ✗ | ResetLocalUpdateFlatProfile(); | |
| 20 | #ifdef RECSTORE_ENABLE_GPU_CACHE | ||
| 21 | gpu::ResetLastGpuCacheProfile(); | ||
| 22 | #endif | ||
| 23 | ✗ | const auto total_start = SteadyNow(); | |
| 24 | ✗ | TORCH_CHECK(!table_name.empty(), "table_name must be non-empty"); | |
| 25 | ✗ | TORCH_CHECK(keys.dim() == 1, "Keys tensor must be 1-dimensional"); | |
| 26 | ✗ | TORCH_CHECK(keys.scalar_type() == torch::kInt64, | |
| 27 | "Keys tensor must have dtype int64"); | ||
| 28 | ✗ | TORCH_CHECK(keys.is_contiguous(), "Keys tensor must be contiguous"); | |
| 29 | |||
| 30 | ✗ | TORCH_CHECK(grads.dim() == 2, "Grads tensor must be 2-dimensional"); | |
| 31 | ✗ | TORCH_CHECK(grads.scalar_type() == torch::kFloat32, | |
| 32 | "Grads tensor must have dtype float32"); | ||
| 33 | ✗ | TORCH_CHECK(grads.is_contiguous(), "Grads tensor must be contiguous"); | |
| 34 | ✗ | TORCH_CHECK(keys.size(0) == grads.size(0), | |
| 35 | "Keys and grads tensors must have the same number of entries"); | ||
| 36 | |||
| 37 | ✗ | auto kv_op = GetConcreteKVClientOp(); | |
| 38 | ✗ | kv_op->RequireLocalBackend(); | |
| 39 | |||
| 40 | ✗ | if (keys.size(0) == 0) { | |
| 41 | ✗ | g_last_local_update_flat_profile[kUpdateTotalMs] = ElapsedMs(total_start); | |
| 42 | ✗ | return; | |
| 43 | } | ||
| 44 | |||
| 45 | ✗ | torch::Tensor cpu_keys = keys; | |
| 46 | const bool can_async_stage_cuda = | ||
| 47 | ✗ | (keys.is_cuda() || grads.is_cuda()) && | |
| 48 | ✗ | (!keys.is_cuda() || !grads.is_cuda() || keys.device() == grads.device()); | |
| 49 | ✗ | bool staged_cuda_async = false; | |
| 50 | ✗ | if (keys.is_cuda()) { | |
| 51 | ✗ | const auto keys_stage_start = SteadyNow(); | |
| 52 | ✗ | if (can_async_stage_cuda) { | |
| 53 | ✗ | cpu_keys = StageCudaTensorToPinnedCpuAsyncNoCast(keys); | |
| 54 | ✗ | staged_cuda_async = true; | |
| 55 | } else { | ||
| 56 | ✗ | cpu_keys = StageCudaTensorToPinnedCpu(keys, torch::kInt64); | |
| 57 | } | ||
| 58 | ✗ | g_last_local_update_flat_profile[kUpdateKeysStageMs] = | |
| 59 | ✗ | ElapsedMs(keys_stage_start); | |
| 60 | } | ||
| 61 | ✗ | torch::Tensor cpu_grads = grads; | |
| 62 | ✗ | if (grads.is_cuda()) { | |
| 63 | ✗ | const auto grads_stage_start = SteadyNow(); | |
| 64 | ✗ | if (can_async_stage_cuda) { | |
| 65 | ✗ | cpu_grads = StageCudaTensorToPinnedCpuAsyncNoCast(grads); | |
| 66 | ✗ | staged_cuda_async = true; | |
| 67 | } else { | ||
| 68 | ✗ | cpu_grads = StageCudaTensorToPinnedCpu(grads, torch::kFloat32); | |
| 69 | } | ||
| 70 | ✗ | g_last_local_update_flat_profile[kUpdateGradsStageMs] = | |
| 71 | ✗ | ElapsedMs(grads_stage_start); | |
| 72 | } | ||
| 73 | ✗ | if (staged_cuda_async) { | |
| 74 | ✗ | const auto stage_wait_start = SteadyNow(); | |
| 75 | ✗ | SynchronizeCurrentCudaStreamForTensor(keys.is_cuda() ? keys : grads); | |
| 76 | ✗ | g_last_local_update_flat_profile[kUpdateStageWaitMs] = | |
| 77 | ✗ | ElapsedMs(stage_wait_start); | |
| 78 | } | ||
| 79 | |||
| 80 | ✗ | base::RecTensor rec_keys = ToRecTensor(cpu_keys, base::DataType::UINT64); | |
| 81 | ✗ | base::RecTensor rec_grads = ToRecTensor(cpu_grads, base::DataType::FLOAT32); | |
| 82 | |||
| 83 | ✗ | const auto shm_call_start = SteadyNow(); | |
| 84 | ✗ | kv_op->EmbUpdateCached( | |
| 85 | ✗ | table_name, rec_keys, rec_grads, CacheActions(keys, grads.size(1)), true); | |
| 86 | ✗ | g_last_local_update_flat_profile[kUpdateShmCallMs] = | |
| 87 | ✗ | ElapsedMs(shm_call_start); | |
| 88 | |||
| 89 | // Client-side split of the same round trip. The server half | ||
| 90 | // (kUpdateServerBackendMs) comes back in the slot header, so the remainder | ||
| 91 | // is slot acquire + copy + wake latency. | ||
| 92 | ✗ | const auto last_request = GetLastLocalShmRequestProfile(); | |
| 93 | ✗ | if (last_request.opcode == static_cast<uint32_t>(LocalOpcode::kUpdateFlat)) { | |
| 94 | ✗ | g_last_local_update_flat_profile[kUpdateSlotAcquireMs] = | |
| 95 | ✗ | last_request.acquire_slot_us / 1000.0; | |
| 96 | ✗ | g_last_local_update_flat_profile[kUpdateCopyEnqueueMs] = | |
| 97 | ✗ | last_request.enqueue_us / 1000.0; | |
| 98 | ✗ | g_last_local_update_flat_profile[kUpdateWaitMs] = | |
| 99 | ✗ | last_request.wait_us / 1000.0; | |
| 100 | ✗ | g_last_local_update_flat_profile[kUpdateServerQueueWaitMs] = | |
| 101 | ✗ | last_request.server_queue_wait_us / 1000.0; | |
| 102 | ✗ | g_last_local_update_flat_profile[kUpdateServerBackendMs] = | |
| 103 | ✗ | last_request.server_backend_us / 1000.0; | |
| 104 | } | ||
| 105 | |||
| 106 | ✗ | g_last_local_update_flat_profile[kUpdateTotalMs] = ElapsedMs(total_start); | |
| 107 | ✗ | } | |
| 108 | |||
| 109 | ✗ | bool warmup_local_lookup_flat_cuda_region_torch() { | |
| 110 | ✗ | auto kv_op = GetConcreteKVClientOp(); | |
| 111 | ✗ | const void* payload_base = nullptr; | |
| 112 | ✗ | std::size_t payload_bytes = 0; | |
| 113 | ✗ | if (!kv_op->GetLocalLookupFlatPayloadRegion(&payload_base, &payload_bytes)) { | |
| 114 | ✗ | return false; | |
| 115 | } | ||
| 116 | ✗ | return EnsurePinnedLocalShmPayload(payload_base, payload_bytes); | |
| 117 | ✗ | } | |
| 118 | |||
| 119 | } // namespace | ||
| 120 | |||
| 121 | 16 | void RegisterFlatOps(torch::Library& m) { | |
| 122 | 16 | m.def("local_lookup_flat", local_lookup_flat_torch); | |
| 123 | 16 | m.def("local_update_flat", local_update_flat_torch); | |
| 124 | 16 | m.def("warmup_local_lookup_flat_cuda_region", | |
| 125 | warmup_local_lookup_flat_cuda_region_torch); | ||
| 126 | 16 | } | |
| 127 | |||
| 128 | } // namespace framework | ||
| 129 | } // namespace recstore | ||
| 130 |