Skip to content

Commit 70edc8a

Browse files
Raahul Kalyaan Jakkafacebook-github-bot
authored andcommitted
Changing Backend Tensor initialization (#5055)
Summary: X-link: facebookresearch/FBGEMM#2065 **Context:** Currently, RocksDB stores data on row-wise format, to enable optimizer offloading for the Kernel. We will append the optimizer state to its corresponding row. During initialization, we need to randomly initialize weights while the optimizer values need to initialized to zero. When optimizer offloading is enabled, **In this diff:** We add two new arguments: 1. enable_optimizer_offloading: This flag toggles between initializing the last optimizer_D rows to zero 2. optimizer_D: The number of columns in the table that needs to be initialized to zero. This set of columns represent the optimizer values (w/wo padding). **Scenarios:** 1. Optimizer_offloading is False: max_D = Dimensions of weights only, optimizer_D = 0 2. Optimizer_offloading is True: max_D = Dimension of weights (w_D) + optimizers (o_D) optimizer_D = dimensions of optimizers (o_D) initialize o_D columns with zero Differential Revision: D85157732
1 parent f849dcd commit 70edc8a

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

fbgemm_gpu/src/ssd_split_embeddings_cache/ssd_table_batched_embeddings.h

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ class EmbeddingRocksDB : public kv_db::EmbeddingKVDB {
121121
std::optional<at::Tensor> table_dims = std::nullopt,
122122
std::optional<at::Tensor> hash_size_cumsum = std::nullopt,
123123
int64_t flushing_block_size = 2000000000 /*2GB*/,
124-
bool disable_random_init = false)
124+
bool disable_random_init = false,
125+
bool enable_optimizer_offloading = false,
126+
int64_t optimizer_D = 0)
125127
: kv_db::EmbeddingKVDB(
126128
num_shards,
127129
max_D,
@@ -266,7 +268,9 @@ class EmbeddingRocksDB : public kv_db::EmbeddingKVDB {
266268
uniform_init_lower,
267269
uniform_init_upper,
268270
row_storage_bitwidth,
269-
disable_random_init);
271+
disable_random_init,
272+
enable_optimizer_offloading,
273+
optimizer_D);
270274
executor_ = std::make_unique<folly::CPUThreadPoolExecutor>(num_shards);
271275
ro_.verify_checksums = false;
272276
ro_.async_io = true;
@@ -421,19 +425,29 @@ class EmbeddingRocksDB : public kv_db::EmbeddingKVDB {
421425
float uniform_init_lower,
422426
float uniform_init_upper,
423427
int64_t row_storage_bitwidth,
424-
bool disable_random_init) {
428+
bool disable_random_init,
429+
bool enable_optimizer_offloading = false,
430+
int64_t optimizer_D = 0) {
425431
for (auto i = 0; i < num_shards; ++i) {
426432
auto* gen = at::check_generator<at::CPUGeneratorImpl>(
427433
at::detail::getDefaultCPUGenerator());
428434
{
429435
std::lock_guard<std::mutex> lock(gen->mutex_);
430-
initializers_.push_back(
431-
std::make_unique<Initializer>(
432-
gen->random64(),
433-
max_D,
434-
uniform_init_lower,
435-
uniform_init_upper,
436-
row_storage_bitwidth));
436+
auto initializer = std::make_unique<Initializer>(
437+
gen->random64(),
438+
max_D,
439+
uniform_init_lower,
440+
uniform_init_upper,
441+
row_storage_bitwidth);
442+
443+
// When Optimizer offloading is enabled, we want to initialize the last
444+
// optimizer_D columns(optimizer values) to zero
445+
if (enable_optimizer_offloading) {
446+
auto& tensor = initializer->row_storage_;
447+
tensor.index({"...", at::indexing::Slice(max_D - optimizer_D, max_D)})
448+
.zero_();
449+
}
450+
initializers_.push_back(std::move(initializer));
437451
}
438452
}
439453
disable_random_init_ = disable_random_init;

0 commit comments

Comments
 (0)