Skip to content

Commit 81602bc

Browse files
EddyLXJfacebook-github-bot
authored andcommitted
Add Python enum configs and KJT builder for enrichment (#5464)
Summary: X-link: meta-pytorch/torchrec#3850 X-link: facebookresearch/FBGEMM#2439 CONTEXT: The enrichment configuration in EnrichmentPolicy uses raw strings for enrichment_type and response_format, which is error-prone and lacks type safety. Additionally, there is no utility to extract unhashed IDs from KJT features for enrichment queries. WHAT: Strengthen enrichment configuration with Python enums and add a KJT builder utility. - Added EnrichmentType enum (IGR_LASER_EMBEDDING, IGR_LASER_SID) and EnrichmentResponseFormat enum (JSON, THRIFT_FLOAT, THRIFT_INT64) in split_table_batched_embeddings_ops_common.py - Updated EnrichmentPolicy to use enum types instead of strings - Added enrichment_policy field to KVZCHTBEConfig for config propagation - Convert enum values to int when passing to C++ TorchScript layer in training.py - Added build_embedding_cache_write_kjt() in kvzch_utils.py to extract hashed/unhashed feature pairs from KJT and encode unhashed IDs as float32 weights for enrichment queries - Wired enrichment_policy through batched_embedding_kernel.py to KVZCHParams Differential Revision: D95883280
1 parent b099cf8 commit 81602bc

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

fbgemm_gpu/fbgemm_gpu/split_table_batched_embeddings_ops_common.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,17 +233,28 @@ def validate(self) -> None:
233233
)
234234

235235

236+
class EnrichmentType(enum.IntEnum):
237+
IGR_LASER_EMBEDDING = 0
238+
IGR_LASER_SID = 1
239+
240+
241+
class EnrichmentResponseFormat(enum.IntEnum):
242+
JSON = 0
243+
THRIFT_FLOAT = 1
244+
THRIFT_INT64 = 2
245+
246+
236247
class EnrichmentPolicy(NamedTuple):
237-
# Model and method identifier, e.g. "igr_laser_embedding", "igr_laser_sid"
238-
enrichment_type: str = ""
248+
# Model and method identifier
249+
enrichment_type: EnrichmentType = EnrichmentType.IGR_LASER_EMBEDDING
239250
# External provider name (e.g. Laser provider name)
240251
provider_name: str = ""
241252
# Client identifier for the external service
242253
client_id: str = ""
243254
# Dimension of data returned by the source
244255
enrichment_dim: int = 0
245-
# Deserialization format: "json", "thrift_float", "thrift_int64"
246-
response_format: str = "json"
256+
# Deserialization format
257+
response_format: EnrichmentResponseFormat = EnrichmentResponseFormat.JSON
247258

248259

249260
class KVZCHParams(NamedTuple):
@@ -294,6 +305,8 @@ class KVZCHTBEConfig(NamedTuple):
294305
optimizer_type_for_st: Optional[str] = None
295306
# [DO NOT USE] This is for st publish only, do not set it in your config
296307
optimizer_state_dtypes_for_st: Optional[FrozenSet[Tuple[str, int]]] = None
308+
# Enrichment policy for embedding cache enrichment from external sources (e.g. Laser)
309+
enrichment_policy: Optional[EnrichmentPolicy] = None
297310

298311

299312
class BackendType(enum.IntEnum):

fbgemm_gpu/fbgemm_gpu/tbe/ssd/training.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -773,15 +773,15 @@ def __init__(
773773
if (
774774
self.kv_zch_params
775775
and self.kv_zch_params.enrichment_policy is not None
776-
and self.kv_zch_params.enrichment_policy.enrichment_type
776+
and self.kv_zch_params.enrichment_policy.enrichment_type is not None
777777
):
778778
ep = self.kv_zch_params.enrichment_policy
779779
enrichment_config = torch.classes.fbgemm.EnrichmentConfig(
780-
ep.enrichment_type,
780+
ep.enrichment_type.value,
781781
ep.provider_name,
782782
ep.client_id,
783783
ep.enrichment_dim,
784-
ep.response_format,
784+
ep.response_format.value,
785785
)
786786
self._ssd_db = torch.classes.fbgemm.DramKVEmbeddingCacheWrapper(
787787
self.cache_row_dim,

0 commit comments

Comments
 (0)