|
| 1 | +#include <iostream> |
| 2 | +#include "serving/processor/serving/processor.h" |
| 3 | +#include "serving/processor/serving/predict.pb.h" |
| 4 | + |
| 5 | +static const char* model_config = "{ \ |
| 6 | + \"omp_num_threads\": 4, \ |
| 7 | + \"kmp_blocktime\": 0, \ |
| 8 | + \"feature_store_type\": \"memory\", \ |
| 9 | + \"serialize_protocol\": \"protobuf\", \ |
| 10 | + \"inter_op_parallelism_threads\": 10, \ |
| 11 | + \"intra_op_parallelism_threads\": 10, \ |
| 12 | + \"init_timeout_minutes\": 1, \ |
| 13 | + \"signature_name\": \"serving_default\", \ |
| 14 | + \"read_thread_num\": 3, \ |
| 15 | + \"update_thread_num\": 2, \ |
| 16 | + \"model_store_type\": \"local\", \ |
| 17 | + \"checkpoint_dir\": \"/root/deeprec/DeepRec/modelzoo/BST/result/\", \ |
| 18 | + \"savedmodel_dir\": \"/root/deeprec/DeepRec/modelzoo/BST/savedmodels/1657183908.2336085/\" \ |
| 19 | + } "; |
| 20 | + |
| 21 | +INPUT_FEATURES = [ |
| 22 | + 'pid', 'adgroup_id', 'cate_id', 'campaign_id', 'customer', 'brand', |
| 23 | + 'user_id', 'cms_segid', 'cms_group_id', 'final_gender_code', 'age_level', |
| 24 | + 'pvalue_level', 'shopping_level', 'occupation', 'new_user_class_level', |
| 25 | + 'tag_category_list', 'tag_brand_list', 'price' |
| 26 | +] |
| 27 | + |
| 28 | +struct input_format{ |
| 29 | + string pid; |
| 30 | + string adgroup_id; |
| 31 | + string cate_id; |
| 32 | + string campaign_id; |
| 33 | + string customer; |
| 34 | + string brand; |
| 35 | + string user_id; |
| 36 | + string cms_segid; |
| 37 | + string cms_group_id; |
| 38 | + string final_gender_code; |
| 39 | + string age_level; |
| 40 | + string pvalue_level; |
| 41 | + string shopping_level; |
| 42 | + string occupation; |
| 43 | + string new_user_class_level; |
| 44 | + string tag_category_list; |
| 45 | + string tag_brand_list; |
| 46 | + string price; |
| 47 | + |
| 48 | +}; |
| 49 | + |
| 50 | +::tensorflow::eas::ArrayProto get_proto(char* char_input,int dim,::tensorflow::eas::ArrayDataType type){ |
| 51 | + ::tensorflow::eas::ArrayShape array_shape; |
| 52 | + array_shape.add_dim(1); |
| 53 | + array_shape.add_dim(dim); |
| 54 | + // input array |
| 55 | + ::tensorflow::eas::ArrayProto input; |
| 56 | + input.add_string_val(char_input); |
| 57 | + input.set_dtype(type); |
| 58 | + *(input.mutable_array_shape()) = array_shape; |
| 59 | + |
| 60 | + return input; |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +int main(int argc, char** argv) { |
| 65 | + int state; |
| 66 | + void* model = initialize("", model_config, &state); |
| 67 | + if (state == -1) { |
| 68 | + std::cerr << "initialize error\n"; |
| 69 | + } |
| 70 | + |
| 71 | + // input format |
| 72 | + input_format inputs = {"430548_1007","669310","1665","360359","167792","247789","841908","81","10","1","4","2","3","0","3","8153|8153|8153|8154|8154|8154|1673|1673|1673|6115|6115|6115|1665|1665|1665|1665|1665|1665|8188|8188|8188|8188|8188|8188|8188|8188|8188|1665|1665|1665|8188|8188|8188|8188|8188|8188|8188|8188|8188|10747|10747|10747|10747|10747|10747|10747|10747|10747|10747|10747","197848|197848|197848|237004|237004|237004|330898|330898|330898|337445|337445|337445|258262|258262|258262|247789|247789|247789|339517|339517|339517|339517|339517|339517|339517|339517|339517|278878|278878|278878|339517|339517|339517|339517|339517|339517|339517|339517|339517|339517|339517|339517|339517|339517|339517|339517|339517|339517|339517|339517","6"} |
| 73 | + |
| 74 | + // input type: float |
| 75 | + ::tensorflow::eas::ArrayDataType dtype = |
| 76 | + ::tensorflow::eas::ArrayDataType::DT_STRING; |
| 77 | + |
| 78 | + |
| 79 | +// ------------------------------------------------------------------------input setting------------------------------------------------------------------------------ |
| 80 | + |
| 81 | + ::tensorflow::eas::ArrayProto input0 = get_proto(inputs.pid,strlen(inputs.pid),dtype); |
| 82 | + ::tensorflow::eas::ArrayProto input1 = get_proto(inputs.adgroup_id,strlen(inputs.adgroup_id),dtype); |
| 83 | + ::tensorflow::eas::ArrayProto input2 = get_proto(inputs.cate_id,strlen(inputs.cate_id),dtype); |
| 84 | + ::tensorflow::eas::ArrayProto input3 = get_proto(inputs.campaign_id,strlen(inputs.campaign_id),dtype); |
| 85 | + ::tensorflow::eas::ArrayProto input4 = get_proto(inputs.customer,strlen(inputs.customer),dtype); |
| 86 | + ::tensorflow::eas::ArrayProto input5 = get_proto(inputs.brand,strlen(inputs.brand),dtype); |
| 87 | + ::tensorflow::eas::ArrayProto input6 = get_proto(inputs.user_id,strlen(inputs.user_id),dtype); |
| 88 | + ::tensorflow::eas::ArrayProto input7 = get_proto(inputs.cms_segid,strlen(inputs.cms_segid),dtype); |
| 89 | + ::tensorflow::eas::ArrayProto input8 = get_proto(inputs.cms_group_id,strlen(inputs.cms_group_id),dtype); |
| 90 | + ::tensorflow::eas::ArrayProto input9 = get_proto(inputs.final_gender_code,strlen(inputs.final_gender_code),dtype); |
| 91 | + ::tensorflow::eas::ArrayProto input10 = get_proto(inputs.age_level,strlen(inputs.age_level),dtype); |
| 92 | + ::tensorflow::eas::ArrayProto input11 = get_proto(inputs.pvalue_level,strlen(inputs.pvalue_level),dtype); |
| 93 | + ::tensorflow::eas::ArrayProto input12 = get_proto(inputs.shopping_level,strlen(inputs.shopping_level),dtype); |
| 94 | + ::tensorflow::eas::ArrayProto input13 = get_proto(inputs.occupation,strlen(inputs.occupation),dtype); |
| 95 | + ::tensorflow::eas::ArrayProto input14 = get_proto(inputs.new_user_class_level,strlen(inputs.new_user_class_level),dtype); |
| 96 | + ::tensorflow::eas::ArrayProto input15 = get_proto(inputs.tag_category_list,strlen(inputs.tag_category_list),dtype); |
| 97 | + ::tensorflow::eas::ArrayProto input16 = get_proto(inputs.tag_brand_list,strlen(inputs.tag_brand_list),dtype); |
| 98 | + ::tensorflow::eas::ArrayProto input17 = get_proto(inputs.price,strlen(inputs.price),dtype); |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + // PredictRequest |
| 103 | + ::tensorflow::eas::PredictRequest req; |
| 104 | + req.set_signature_name("serving_default"); |
| 105 | + req.add_output_filter("output:0"); |
| 106 | + |
| 107 | + (*req.mutable_inputs())["pid:0"] = input0; |
| 108 | + (*req.mutable_inputs())["adgroup_id:0"] = input1; |
| 109 | + (*req.mutable_inputs())["cate_id:0"] = input2; |
| 110 | + (*req.mutable_inputs())["campaign_id:0"] = input3; |
| 111 | + (*req.mutable_inputs())["customer:0"] = input4; |
| 112 | + (*req.mutable_inputs())["brand:0"] = input5; |
| 113 | + (*req.mutable_inputs())["user_id:0"] = input6; |
| 114 | + (*req.mutable_inputs())["cms_segid:0"] = input7; |
| 115 | + (*req.mutable_inputs())["cms_group_id:0"] = input8; |
| 116 | + (*req.mutable_inputs())["final_gender_code:0"] = input9; |
| 117 | + (*req.mutable_inputs())["age_level:0"] = input10; |
| 118 | + (*req.mutable_inputs())["pvalue_level:0"] = input11; |
| 119 | + (*req.mutable_inputs())["shopping_level:0"] = input12; |
| 120 | + (*req.mutable_inputs())["occupation:0"] = input13; |
| 121 | + (*req.mutable_inputs())["new_user_class_level:0"] = input14; |
| 122 | + (*req.mutable_inputs())["tag_category_list:0"] = input15; |
| 123 | + (*req.mutable_inputs())["tag_brand_list:0"] = input16; |
| 124 | + (*req.mutable_inputs())["price:0"] = input17; |
| 125 | + |
| 126 | + size_t size = req.ByteSizeLong(); |
| 127 | + void *buffer = malloc(size); |
| 128 | + req.SerializeToArray(buffer, size); |
| 129 | + |
| 130 | + // do process |
| 131 | + void* output = nullptr; |
| 132 | + int output_size = 0; |
| 133 | + state = process(model, buffer, size, &output, &output_size); |
| 134 | + |
| 135 | + // parse response |
| 136 | + std::string output_string((char*)output, output_size); |
| 137 | + ::tensorflow::eas::PredictResponse resp; |
| 138 | + resp.ParseFromString(output_string); |
| 139 | + std::cout << "process returned state: " << state << ", response: " << resp.DebugString(); |
| 140 | + |
| 141 | + return 0; |
| 142 | +} |
| 143 | + |
0 commit comments