Verify feature effectiveness

Updated at:

Background

During model iteration and deployment, you often need to confirm that a new or modified feature works as expected. Sometimes, the model's final output score remains unchanged even when the feature has been updated online, making it difficult to determine the feature's effectiveness. This document provides a systematic method to verify that the model correctly receives and processes features.

Prerequisites

  • You have deployed a recall or sorting model service on PAI-EAS.

  • You have deployed a PAI-Rec recommendation engine service. In the engine or A/B test configuration, you have configured this service to call the model service and use the target feature.

Verify feature effectiveness

The core strategy is to capture a live request, save its feature data locally, modify a specific feature value, and then resend the request. By observing how the feature data changes before it enters the model, you can determine if the feature is processed correctly.

Step 1: Capture live feature data

  1. Send a debug request to the recommendation engine service. Log in to the PAI-Rec console. In the left-side navigation pane, click Troubleshooting Tools > Recommendation Diagnosis. Select the environment, enable debug mode, and click Diagnose to send the request.

    Ensure the request is routed to the model service you want to verify. For example, in a recall or fine-grained sorting experiment, you can add the user ID of the test request to a whitelist. Alternatively, you can configure the experiment to receive 100% of the traffic in its layer. When the model service receives a debug request, it automatically saves the feature data from the engine as a Protocol Buffers (Protobuf) file to the service's mounted directory.

  2. Locate and download the feature file. Go to the PAI-EAS console and find the corresponding model service instance.

    1. View the request in the PAI-EAS console. Open your model service and go to the Logs page. Search for the debug request by its request_id to view the corresponding feature names.

    2. Download the feature file. If searching by request_id does not provide enough information, you can download the feature file in Protocol Buffers (Protobuf) format from the model's OSS path.

      For TorchEasyRec processor versions earlier than 1.12:

      For TorchEasyRec processor versions 1.12 and later:

      For EasyRec:

Step 2: Local analysis and controlled experiment

Use the following Python scripts to parse the downloaded Protocol Buffers (Protobuf) file, modify features, and resend the request to the model to observe internal data changes.

  1. For TorchEasyRec Processor versions earlier than 1.12, run the following analysis script.

    ## Install the eas-prediction package: pip install -U eas-prediction --user
    from eas_prediction import PredictClient
    from eas_prediction.torchrec_request import TorchRecRequest
    from eas_prediction.torchrec_predict_pb2 import PBRequest
    from google.protobuf import text_format
    
    
    def read_pb_and_request_and_save(path, str_path, client, req):
        with open(path, 'rb') as f:
            pb_data = f.read()
        pb_req = PBRequest()
        pb_req.ParseFromString(pb_data)
        req.request_data = pb_req
        resp = client.predict(req)
        print(resp)
        with open(str_path, 'w', encoding='utf-8') as f:
            f.write(str(pb_req))
    
    
    def predict_new_pb(str_path, debug_feature_path, client, req):
        with open(str_path, 'r', encoding='utf-8') as f:
            pb_data = f.read()
        pb_req = PBRequest()
        text_format.Merge(pb_data, pb_req)
        req.request_data = pb_req
        req.set_debug_level(2)
        resp = client.predict(req)
        with open(debug_feature_path, 'w', encoding='utf-8') as f:
            f.write(str(resp))
    
    
    if __name__ == '__main__':
        path = 'torch_req/on_line.pb'  # Local path to the downloaded Protobuf file
        str_path = 'torch_req/on_line.txt'  # Path to save the parsed Protobuf file as a readable text file
        debug_feature_path = 'torch_req/on_score.txt'  # Path to save raw_features (all features before FG) and generate_features (all features after FG)
        # Configure the model URL
        client = PredictClient('173xxxx.cn-beijing.pai-eas.aliyuncs.com', '<model_name>')
        # Set the model token
        client.set_token('<your_token>')
        client.init()
        req = TorchRecRequest()
        # 1. Call read_pb_and_request_and_save to send a request and save the Protobuf data as a string.
        # 2. Modify the target feature in the text file.
        # 3. Comment out read_pb_and_request_and_save, then run predict_new_pb.
        read_pb_and_request_and_save(path, str_path, client, req)
        # predict_new_pb(str_path,debug_feature_path,client,req)
    
    1. Generate a readable feature file named on_line.txt. Configure the path and str_path variables, and then call the read_pb_and_request_and_save function. This creates an on_line.txt file that contains all request features in a readable format.

    2. Modify the feature and run a controlled experiment. Open the on_line.txt file, find the feature you want to test, and manually change its value. For example, change a numerical feature from 0.1 to 0.9, or replace an ID feature with a different value. If the recall set contains many items, you can reduce the number of items and modify the features for only a few of them.

    3. Get the debug output. Save the modified on_line.txt file, and then run the predict_new_pb function in the script. Remember to comment out the call to read_pb_and_request_and_save. This function resends the request to the model with your modified features and saves detailed debug information, including raw_features and generate_features, to the on_score.txt file.

  2. For TorchEasyRec processor versions 1.12 and later, run the following analysis script.

    ## Install the eas-prediction package: pip install -U eas-prediction --user
    from eas_prediction import PredictClient
    from eas_prediction.torchrec_request import TorchRecRequest
    from eas_prediction.torchrec_predict_pb2 import PBRequest, PBLogData
    from google.protobuf import text_format
    
    
    def read_pb_response(log_path, req_path):
        with open(log_path, 'rb') as f:
            pb_data = f.read()
        pb_log = PBLogData()
        pb_log.ParseFromString(pb_data)
        with open(req_path, 'w', encoding='utf-8') as f:
            f.write(str(pb_log.request))
        print(pb_log.request)
        print(pb_log.raw_features['200025658'])  # Print a sample raw_feature
        print(pb_log.generate_features['200025658'])  # Print a sample generate_feature
    
    
    def predict_new_pb(req_path, debug_feature_path, client):
        with open(req_path, 'r', encoding='utf-8') as f:
            pb_data = f.read()
        pb_req = PBRequest()
        text_format.Merge(pb_data, pb_req)
        req = TorchRecRequest()
        req.request_data = pb_req
        req.set_debug_level(2)
        resp = client.predict(req)
        with open(debug_feature_path, 'w', encoding='utf-8') as f:
            f.write(str(resp))
    
    if __name__ == '__main__':
        log_path = 'torch_req/log.pb'  # Local path to the downloaded log Protobuf file
        req_path = 'torch_req/log_request.pb'  # Path to save the request parsed from the local Protobuf file
        debug_feature_path = 'torch_req/on_score.txt'
        # Configure the model URL
        client = PredictClient('173xxxx.cn-beijing.pai-eas.aliyuncs.com', '<model_name>')
        # Set the model token
        client.set_token('<your_token>')
        client.init()
        req = TorchRecRequest()
        # 1. Call read_pb_response to get the request body and the features before and after FG.
        # 2. Modify the target feature.
        # 3. Call predict_new_pb to get the new scores and features after your modification.
        read_pb_response(log_path, req_path)
        predict_new_pb(req_path, debug_feature_path, client)
        
  3. For EasyRec models, use the following analysis script.

    ## Install the eas-prediction package: pip install -U eas-prediction --user
    from eas_prediction import PredictClient
    from eas_prediction.easyrec_request import EasyRecRequest
    from eas_prediction.easyrec_predict_pb2 import PBRequest
    from google.protobuf import text_format
    
    def read_pb_and_request_and_save(path, str_path, client, req):
        with open(path, 'rb') as f:
            pb_data = f.read()
        pb_req = PBRequest()
        pb_req.ParseFromString(pb_data)
        req.add_feed(pb_req)
        resp = client.predict(req)
        print(resp)
        with open(str_path, 'w', encoding='utf-8') as f:
            f.write(str(pb_req))
    
    
    def predict_new_pb(str_path, debug_feature_path, client, req):
        with open(str_path, 'r', encoding='utf-8') as f:
            pb_data = f.read()
        pb_req = PBRequest()
        text_format.Merge(pb_data, pb_req)
        req.add_feed(pb_req, debug_level=2)
        resp = client.predict(req)
        with open(debug_feature_path, 'w', encoding='utf-8') as f:
            f.write(str(resp))
    
    
    if __name__ == '__main__':
        path = './easyrec_req/on_line.pb'
        str_path = 'easyrec_req/on_line.txt'
        debug_feature_path = 'easyrec_req/on_score.txt'
    
        client = PredictClient('173xxxx.cn-beijing.pai-eas.aliyuncs.com', '<model_name>')
        # Set the model token
        client.set_token('<your_token>')
        client.init()
        req = EasyRecRequest()
        # 1. Call read_pb_and_request_and_save to send a request and save the Protobuf data as a string.
        # 2. Modify the target feature in the text file.
        # 3. Comment out read_pb_and_request_and_save, then run predict_new_pb.
        read_pb_and_request_and_save(path, str_path, client, req)
        # predict_new_pb(str_path,debug_feature_path,client,req)
    
    
  4. Verify the results. Open the on_score.txt file. It contains two key sets of information:

    • raw_features: The raw features received by the model service after preprocessing, such as table lookups and concatenation, but before being encoded by the Feature Generation (FG) operator.

    • generate_features: Features encoded by the FG operator and input into the model for scoring.

    Use the following checks to confirm that the feature is effective:

    • Check raw_features: Confirm that your modified feature and its new value appear correctly in raw_features. This proves the feature was passed to the model service.

    • Check generate_features: Observe how the feature appears in generate_features after being encoded by the FG operator. If your modification (for example, from 0.1 to 0.9) causes a change in the corresponding embedding ID or numerical value in generate_features, it proves that the online feature processing logic is working correctly.

Troubleshooting: Unchanged score after feature change

If you confirm that generate_features changes in response to your modifications but the final model score remains the same, it usually means that the feature's weight in the model is close to zero. This is a model performance issue, not a pipeline problem.

However, if you find that generate_features remains unchanged after you modify raw_features, even after multiple attempts, the problem may be that the online FG operator's logic is not working as expected. In this case, perform an offline validation:

  1. Find the FG configuration file: In your model project, locate the FG encoder feature configuration file used for fine-grained sorting.

  2. Simulate offline execution: Use a tool like pyfg to perform the FG transformation locally. Use the feature values observed in raw_features as input.

  3. Compare the results: Compare the output of the offline pyfg execution with the online generate_features values.

    • If they match: This indicates that the online FG logic correctly executed your configuration.

    • If they do not match: This suggests a discrepancy between your online and offline environments that requires further investigation.