class_docstr: Handler for TorchRec DLRM example
class_docstr_tok: ["Handler","for","TorchRec","DLRM","example"]
docstr: The input values for the DLRM model are twofold. There is a dense part and a sparse part.
  The sparse part consists of a list of ids where each entry can consist of zero, one or multiple ids.
  Due to the inconsistency in elements, the sparse part is represented by the KeyJaggedTensor class provided by TorchRec.
  
  Args:
      data (str): The input data is in the form of a string
  
  Returns:
      Tuple of:
          (Tensor): Dense features
          (KeyJaggedTensor): Sparse features
docstr_tok: ["The","input","values","for","the","DLRM","model","are","twofold",".","There","is","a","dense","part","and","a","sparse","part",".","The","sparse","part","consists","of","a","list","of","ids","where","each","entry","can","consist","of","zero",",","one","or","multiple","ids",".","Due","to","the","inconsistency","in","elements",",","the","sparse","part","is","represented","by","the","KeyJaggedTensor","class","provided","by","TorchRec",".","Args",":","data","(","str",")",":","The","input","data","is","in","the","form","of","a","string","Returns",":","Tuple","of",":","(","Tensor",")",":","Dense","features","(","KeyJaggedTensor",")",":","Sparse","features"]
code: def preprocess(self, data):
      """
      The input values for the DLRM model are twofold. There is a dense part and a sparse part.
      The sparse part consists of a list of ids where each entry can consist of zero, one or multiple ids.
      Due to the inconsistency in elements, the sparse part is represented by the KeyJaggedTensor class provided by TorchRec.
  
      Args:
          data (str): The input data is in the form of a string
  
      Returns:
          Tuple of:
              (Tensor): Dense features
              (KeyJaggedTensor): Sparse features
      """
  
      (
          float_features,
          id_list_features_lengths,
          id_list_features_values,
      ) = ([], [], [])
  
      for row in data:
          input = row.get("data") or row.get("body")
  
          if not isinstance(input, dict):
              input = json.loads(input)
  
          # This is the dense feature part
          assert "float_features" in input
          # The sparse input consists of a length vector and the values.
          # The length vector contains the number of elements which are part fo the same entry in the linear list provided as input.
          assert "id_list_features.lengths" in input
          assert "id_list_features.values" in input
  
          float_features.append(input["float_features"])
          id_list_features_lengths.extend(
              input["id_list_features.lengths"]
          )
          id_list_features_values.append(
              input["id_list_features.values"]
          )
  
      # Reformat the values input for KeyedJaggedTensor
      id_list_features_values = torch.FloatTensor(
          id_list_features_values
      )
      id_list_features_values = torch.transpose(
          id_list_features_values, 0, 1
      )
      id_list_features_values = [
          value for value in id_list_features_values
      ]
  
      # Dense and Sparse Features for DLRM model
      dense_features = torch.FloatTensor(float_features)
      sparse_features = KeyedJaggedTensor(
          keys=DEFAULT_CAT_NAMES,
          lengths=torch.LongTensor(id_list_features_lengths),
          values=torch.cat(id_list_features_values),
      )
  
      return dense_features, sparse_features
  
file_path: serve/examples/torchrec_dlrm/dlrm_handler.py
imports: import json
  import logging
  import os
  from abc import ABC
  import torch
  from torchrec.datasets.criteo import DEFAULT_CAT_NAMES
  from torchrec.sparse.jagged_tensor import KeyedJaggedTensor
  from ts.torch_handler.base_handler import BaseHandler
label_desc: Use image node_id 2 for calling the TorchRecDLRMHandler obj's underlying member method code with example usage: obj.preprocess(data) and returns: dense_features,  sparse_features