framework/pytorch/op_base.cc
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "framework/pytorch/op_base.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 | 32 | torch::Tensor emb_read_torch(const torch::Tensor& keys, int64_t embedding_dim) { | |
| 12 | 32 | return std::get<0>(CachedReadTorch(keys, embedding_dim)); | |
| 13 | } | ||
| 14 | |||
| 15 | ✗ | void emb_update_torch(const torch::Tensor& keys, const torch::Tensor& grads) { | |
| 16 | ✗ | throw std::runtime_error( | |
| 17 | "emb_update_torch is deprecated. Use the Python-based sparse " | ||
| 18 | ✗ | "optimizer."); | |
| 19 | } | ||
| 20 | |||
| 21 | 2 | void emb_update_table_torch(const std::string& table_name, | |
| 22 | const torch::Tensor& keys, | ||
| 23 | const torch::Tensor& grads) { | ||
| 24 | 2 | TORCH_CHECK(!table_name.empty(), "table_name must be non-empty"); | |
| 25 | 2 | CheckRows(keys, grads); | |
| 26 | |||
| 27 | 2 | if (keys.size(0) == 0) { | |
| 28 | ✗ | return; | |
| 29 | } | ||
| 30 | |||
| 31 | 2 | torch::Tensor cpu_keys = ToCpuTensor(keys); | |
| 32 | 2 | torch::Tensor cpu_grads = ToCpuTensor(grads); | |
| 33 | |||
| 34 | 2 | base::RecTensor rec_keys = ToRecTensor(cpu_keys, base::DataType::UINT64); | |
| 35 | 2 | base::RecTensor rec_grads = ToRecTensor(cpu_grads, base::DataType::FLOAT32); | |
| 36 | |||
| 37 | 4 | GetConcreteKVClientOp()->EmbUpdateCached( | |
| 38 | 4 | table_name, rec_keys, rec_grads, CacheActions(keys, grads.size(1))); | |
| 39 | 2 | } | |
| 40 | |||
| 41 | ✗ | int64_t emb_update_async_torch(const std::string& table_name, | |
| 42 | const torch::Tensor& keys, | ||
| 43 | const torch::Tensor& grads) { | ||
| 44 | ✗ | TORCH_CHECK(!table_name.empty(), "table_name must be non-empty"); | |
| 45 | ✗ | CheckRows(keys, grads); | |
| 46 | |||
| 47 | ✗ | torch::Tensor cpu_keys = ToCpuTensor(keys); | |
| 48 | ✗ | torch::Tensor cpu_grads = ToCpuTensor(grads); | |
| 49 | ✗ | auto kv_op = GetConcreteKVClientOp(); | |
| 50 | ✗ | const uint64_t update_id = kv_op->EmbUpdateAsync( | |
| 51 | table_name, | ||
| 52 | ✗ | ToRecTensor(cpu_keys, base::DataType::UINT64), | |
| 53 | ✗ | ToRecTensor(cpu_grads, base::DataType::FLOAT32), | |
| 54 | ✗ | CacheActions(keys, grads.size(1))); | |
| 55 | ✗ | return static_cast<int64_t>(update_id); | |
| 56 | ✗ | } | |
| 57 | |||
| 58 | ✗ | void emb_update_wait_torch(int64_t update_id) { | |
| 59 | ✗ | TORCH_CHECK(update_id > 0, "update_id must be positive"); | |
| 60 | ✗ | GetConcreteKVClientOp()->WaitForEmbUpdate(static_cast<uint64_t>(update_id)); | |
| 61 | ✗ | } | |
| 62 | |||
| 63 | 10 | int64_t init_embedding_table_torch( | |
| 64 | const std::string& table_name, | ||
| 65 | int64_t num_embeddings, | ||
| 66 | int64_t embedding_dim, | ||
| 67 | int64_t table_id = 0) { | ||
| 68 | 10 | TORCH_CHECK(!table_name.empty(), "table_name must be non-empty"); | |
| 69 | 10 | TORCH_CHECK(num_embeddings > 0, "num_embeddings must be positive"); | |
| 70 | 10 | TORCH_CHECK(embedding_dim > 0, "embedding_dim must be positive"); | |
| 71 | 10 | TORCH_CHECK(table_id >= 0, "table_id must be non-negative"); | |
| 72 | |||
| 73 | 10 | EmbeddingTableConfig cfg{}; | |
| 74 | 10 | cfg.num_embeddings = static_cast<uint64_t>(num_embeddings); | |
| 75 | 10 | cfg.embedding_dim = static_cast<uint64_t>(embedding_dim); | |
| 76 | 10 | cfg.table_id = static_cast<uint64_t>(table_id); | |
| 77 | |||
| 78 | 30 | return GetConcreteKVClientOp()->InitEmbeddingTableCached( | |
| 79 | 30 | table_name, cfg, CacheActions()); | |
| 80 | } | ||
| 81 | |||
| 82 | ✗ | bool save_checkpoint_torch(const std::string& path, | |
| 83 | const std::string& metadata) { | ||
| 84 | ✗ | TORCH_CHECK(!path.empty(), "checkpoint path must be non-empty"); | |
| 85 | ✗ | TORCH_CHECK(!metadata.empty(), "checkpoint metadata must be non-empty"); | |
| 86 | ✗ | return GetKVClientOp()->SaveCheckpoint(path, metadata); | |
| 87 | } | ||
| 88 | |||
| 89 | ✗ | bool load_checkpoint_torch(const std::string& path, | |
| 90 | const std::string& metadata) { | ||
| 91 | ✗ | TORCH_CHECK(!path.empty(), "checkpoint path must be non-empty"); | |
| 92 | ✗ | TORCH_CHECK(!metadata.empty(), "checkpoint metadata must be non-empty"); | |
| 93 | ✗ | return GetConcreteKVClientOp()->LoadCheckpointCached( | |
| 94 | ✗ | path, metadata, CacheActions()); | |
| 95 | } | ||
| 96 | |||
| 97 | 18 | void emb_write_torch(const torch::Tensor& keys, const torch::Tensor& values) { | |
| 98 | 18 | CheckRows(keys, values); | |
| 99 | |||
| 100 | 18 | if (keys.size(0) == 0) { | |
| 101 | ✗ | return; | |
| 102 | } | ||
| 103 | |||
| 104 | 18 | torch::Tensor cpu_keys = ToCpuTensor(keys); | |
| 105 | 18 | torch::Tensor cpu_values = ToCpuTensor(values); | |
| 106 | |||
| 107 | 18 | base::RecTensor rec_keys = ToRecTensor(cpu_keys, base::DataType::UINT64); | |
| 108 | 18 | base::RecTensor rec_values = ToRecTensor(cpu_values, base::DataType::FLOAT32); | |
| 109 | |||
| 110 | 36 | GetConcreteKVClientOp()->EmbWriteCached( | |
| 111 | 36 | rec_keys, rec_values, CacheActions(keys, values.size(1))); | |
| 112 | 18 | } | |
| 113 | |||
| 114 | ✗ | void set_ps_config_torch(const std::string& host, int64_t port) { | |
| 115 | ✗ | auto kv_op = GetConcreteKVClientOp(); | |
| 116 | ✗ | kv_op->SetPSConfig(host, static_cast<int>(port)); | |
| 117 | ✗ | } | |
| 118 | |||
| 119 | ✗ | void set_ps_backend_torch(const std::string& backend) { | |
| 120 | ✗ | auto kv_op = GetConcreteKVClientOp(); | |
| 121 | ✗ | kv_op->SetPSBackend(backend); | |
| 122 | ✗ | } | |
| 123 | |||
| 124 | ✗ | std::string current_ps_backend_torch() { | |
| 125 | ✗ | auto kv_op = GetConcreteKVClientOp(); | |
| 126 | ✗ | return kv_op->CurrentPSBackend(); | |
| 127 | ✗ | } | |
| 128 | |||
| 129 | } // namespace | ||
| 130 | |||
| 131 | 16 | void RegisterBaseOps(torch::Library& m) { | |
| 132 | 16 | m.def("emb_read", emb_read_torch); | |
| 133 | 16 | m.def("emb_update", emb_update_torch); | |
| 134 | 16 | m.def("emb_update_table", emb_update_table_torch); | |
| 135 | 16 | m.def("emb_update_async", emb_update_async_torch); | |
| 136 | 16 | m.def("emb_update_wait", emb_update_wait_torch); | |
| 137 | 16 | m.def("init_embedding_table", init_embedding_table_torch); | |
| 138 | 16 | m.def("save_checkpoint", save_checkpoint_torch); | |
| 139 | 16 | m.def("load_checkpoint", load_checkpoint_torch); | |
| 140 | 16 | m.def("emb_write", emb_write_torch); | |
| 141 | 16 | m.def("set_ps_config", set_ps_config_torch); | |
| 142 | 16 | m.def("set_ps_backend", set_ps_backend_torch); | |
| 143 | 16 | m.def("current_ps_backend", current_ps_backend_torch); | |
| 144 | 16 | } | |
| 145 | |||
| 146 | } // namespace framework | ||
| 147 | } // namespace recstore | ||
| 148 |