MCPcopy Create free account
hub / github.com/SalesforceAIResearch/perfcodegen / batch_infer_local_chat_model

Function batch_infer_local_chat_model

src/inference.py:869–949  ·  view source on GitHub ↗
(model, mode, rd, input_file, output_path)

Source from the content-addressed store, hash-verified

867
868
869def batch_infer_local_chat_model(model, mode, rd, input_file, output_path):
870 if isinstance(model, str):
871 model = LocalModel(model, "0", tensor_parallel_size = 8 if "70B" in model or "Mixtral" in model or "command" in model else 4)
872
873 inputs = json.load(open(input_file, "r"))
874
875 prompts = []
876 overlong_prompts = []
877 prompt_map = {}
878
879 print("Generating prompts...")
880
881 context_length = 8192 if "llama" in model.name else 128000
882
883 for d in inputs:
884 prompt = model.tokenizer.apply_chat_template(
885 d["body"]["messages"],
886 tokenize = False,
887 add_generation_prompt=True
888 )
889 if model.get_prompt_length(prompt) < context_length and prompt not in prompt_map:
890 prompts.append(prompt)
891 elif prompt not in prompt_map:
892 overlong_prompts.append(prompt)
893 if prompt not in prompt_map:
894 prompt_map[prompt] = []
895 prompt_map[prompt].append(d["custom_id"])
896
897 print("{} overlong prompts".format(len(overlong_prompts)))
898
899 num = 0
900 for prompt in prompt_map:
901 num += len(prompt_map[prompt])
902
903 if num != len(inputs):
904 raise ValueError("The instances in the prompt map does not match the input size!")
905
906 if len(prompt_map) != len(prompts) + len(overlong_prompts):
907 raise ValueError("The number of prompt in the prompt map does not match the input prompts.")
908
909
910 try:
911 results = model.infer_many(prompts, n = inputs[0]["body"]["n"], temperature = inputs[0]["body"]["temperature"])
912 except Exception as e:
913 logger.error("Error occurred with reason: {}. INFO Dataset: {}\nModel: {}\n".format(str(e), dataset.name, model.name))
914 traceback.print_exc()
915
916 data = {}
917
918 for prompt in results:
919 for i in prompt_map[prompt]:
920 if i.count("@") == 4:
921 data[i] = {
922 "content": results[prompt][0][0],
923 "role": "assistant"
924 }
925 else:
926 data[i] = {

Callers

nothing calls this directly

Calls 3

get_prompt_lengthMethod · 0.95
infer_manyMethod · 0.95
LocalModelClass · 0.90

Tested by

no test coverage detected