This repository has been archived by the owner. It is now read-only.
Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
200 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -1,29 +1,33 @@ | ||
product_tag: 'lookalike_application' | ||
pipeline_tag: '08192021_1m' | ||
pipeline_tag: '12132021' | ||
score_generator: | ||
input: | ||
log_table : "lookalike_08172021_1000_logs" | ||
did_table: "lookalike_08172021_1000_trainready" | ||
keywords_table: "din_ad_keywords_09172020" | ||
significant_keywords_table: "lookalike_08172021_1000_keywords" | ||
din_model_tf_serving_url: "http://10.193.217.105:8506/v1/models/lookalike:predict" | ||
aid_table: "lookalike_11192021_trainready" | ||
keywords_table: "lookalike_11192021_keywords" | ||
din_model_tf_serving_url: "http://10.193.217.126:8501/v1/models/lookalike_1119:predict" | ||
din_model_length: 20 | ||
extend: 2000 | ||
alg: "euclidean" ##### currently just support "euclideand" and "dot" | ||
output: | ||
score_table: "{product_tag}_{pipeline_tag}_score" | ||
normalize: False | ||
score_vector: | ||
keywords_table: "din_ad_keywords_09172020" | ||
keywords_table: "lookalike_11192021_keywords" | ||
score_table: "{product_tag}_{pipeline_tag}_score" | ||
score_vector_table: "{product_tag}_{pipeline_tag}_score_vector" | ||
did_bucket_size: 100 | ||
did_bucket_step: 10 | ||
aid_bucket_size: 100 | ||
aid_bucket_step: 10 | ||
# Adds alpha_aid_bucket partition to allow for finer control of step size in top_n_similarity stage | ||
score_vector_rebucketing: | ||
aid_bucket_size: 100 | ||
aid_bucket_step: 10 | ||
alpha_aid_bucket_size: 10 # The number of buckets to allocate for the alpha_aid_bucket column | ||
score_vector_alpha_table: "{product_tag}_{pipeline_tag}_score_vector_alpha" | ||
top_n_similarity: | ||
did_bucket_size: 100 | ||
load_bucket_step: 20 | ||
search_bucket_step: 50 | ||
top_n: 10 | ||
index_factory_string: "IVF256,Flat" | ||
aid_bucket_size: 100 # Total number of alpha buckets to process similarity of | ||
load_bucket_step: 20 # Number of alpha buckets to load into index at a time | ||
search_bucket_step: 50 # Number of alpha buckets to process top N at a time | ||
top_n: 10 # Number of nearest neighbors to store for each aid | ||
index_factory_string: "IVF256,Flat" # See https://github.com/facebookresearch/faiss/wiki/The-index-factory for factory strings | ||
similarity_table: "{product_tag}_{pipeline_tag}_similarity" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,96 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
|
||
# http://www.apache.org/licenses/LICENSE-2.0.html | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import yaml | ||
import argparse | ||
from pyspark import SparkContext | ||
from pyspark.sql import HiveContext | ||
from pyspark.sql.functions import lit, col, udf | ||
from pyspark.sql.types import FloatType, StringType, StructType, StructField, ArrayType, MapType, IntegerType | ||
# from rest_client import predict, str_to_intlist | ||
import requests | ||
import json | ||
import argparse | ||
from pyspark.sql.functions import udf | ||
from math import sqrt | ||
import time | ||
import hashlib | ||
from util import resolve_placeholder | ||
|
||
|
||
from lookalike_model.pipeline.util import write_to_table, write_to_table_with_partition | ||
|
||
''' | ||
To run, execute the following in application folder. | ||
spark-submit --master yarn --num-executors 20 --executor-cores 5 --executor-memory 8G --driver-memory 8G --conf spark.driver.maxResultSize=5g --conf spark.hadoop.hive.exec.dynamic.partition=true --conf spark.hadoop.hive.exec.dynamic.partition.mode=nonstrict score_vector_rebucketing.py config.yml | ||
This process generates added secondary buckects ids (alpha-aid-bucket). | ||
''' | ||
|
||
|
||
def assign_new_bucket_id(df, n, new_column_name): | ||
def __hash_sha256(s): | ||
hex_value = hashlib.sha256(s.encode('utf-8')).hexdigest() | ||
return int(hex_value, 16) | ||
_udf = udf(lambda x: __hash_sha256(x) % n, IntegerType()) | ||
df = df.withColumn(new_column_name, _udf(df.aid)) | ||
return df | ||
|
||
|
||
def run(hive_context, cfg): | ||
|
||
score_vector_table = cfg['score_vector']['score_vector_table'] | ||
bucket_size = cfg['score_vector_rebucketing']['aid_bucket_size'] | ||
bucket_step = cfg['score_vector_rebucketing']['aid_bucket_step'] | ||
alpha_bucket_size = cfg['score_vector_rebucketing']['alpha_aid_bucket_size'] | ||
score_vector_alpha_table = cfg['score_vector_rebucketing']['score_vector_alpha_table'] | ||
|
||
first_round = True | ||
num_batches = (bucket_size + bucket_step - 1) / bucket_step | ||
batch_num = 1 | ||
for aid_bucket in range(0, bucket_size, bucket_step): | ||
print('Processing batch {} of {} bucket number: {}'.format(batch_num, num_batches, aid_bucket)) | ||
|
||
command = "SELECT aid, aid_bucket, score_vector, c1 FROM {} WHERE aid_bucket BETWEEN {} AND {}".format(score_vector_table, aid_bucket, min(aid_bucket+bucket_step-1, bucket_size)) | ||
|
||
df = hive_context.sql(command) | ||
df = assign_new_bucket_id(df, alpha_bucket_size, 'alpha_aid_bucket') | ||
|
||
mode = 'overwrite' if first_round else 'append' | ||
write_to_table_with_partition(df.select('aid', 'score_vector', 'c1', 'aid_bucket', 'alpha_aid_bucket'), | ||
score_vector_alpha_table, partition=('aid_bucket', 'alpha_aid_bucket'), mode=mode) | ||
first_round = False | ||
batch_num += 1 | ||
|
||
|
||
if __name__ == "__main__": | ||
start = time.time() | ||
parser = argparse.ArgumentParser(description='') | ||
parser.add_argument('config_file') | ||
args = parser.parse_args() | ||
with open(args.config_file, 'r') as yml_file: | ||
cfg = yaml.safe_load(yml_file) | ||
resolve_placeholder(cfg) | ||
sc = SparkContext.getOrCreate() | ||
sc.setLogLevel('WARN') | ||
hive_context = HiveContext(sc) | ||
|
||
run(hive_context=hive_context, cfg=cfg) | ||
sc.stop() | ||
end = time.time() | ||
print('Runtime of the program is:', (end - start)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.