From 7b0902e7b6132a2e2981dab874f15d1bd70f2888 Mon Sep 17 00:00:00 2001 From: "Wang, Jian4" <61138589+hzjane@users.noreply.github.com> Date: Wed, 29 Nov 2023 10:56:17 +0800 Subject: [PATCH] LLM: Update for cpu qlora mpirun (#9548) --- .../alpaca-qlora/alpaca_qlora_finetuning_cpu.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/python/llm/example/CPU/QLoRA-FineTuning/alpaca-qlora/alpaca_qlora_finetuning_cpu.py b/python/llm/example/CPU/QLoRA-FineTuning/alpaca-qlora/alpaca_qlora_finetuning_cpu.py index 5ffae73eaf8..7cfedaf535b 100644 --- a/python/llm/example/CPU/QLoRA-FineTuning/alpaca-qlora/alpaca_qlora_finetuning_cpu.py +++ b/python/llm/example/CPU/QLoRA-FineTuning/alpaca-qlora/alpaca_qlora_finetuning_cpu.py @@ -52,6 +52,21 @@ # import them from bigdl.llm.transformers.qlora to get a BigDL-LLM compatible Peft model from bigdl.llm.transformers.qlora import get_peft_model, prepare_model_for_kbit_training +def get_int_from_env(env_keys, default): + """Returns the first positive env value found in the `env_keys` list or the default.""" + for e in env_keys: + val = int(os.environ.get(e, -1)) + if val >= 0: + return val + return default + +local_rank = get_int_from_env(["LOCAL_RANK","MPI_LOCALRANKID"], "0") +world_size = get_int_from_env(["WORLD_SIZE","PMI_SIZE"], "1") +port = get_int_from_env(["MASTER_PORT"], 29500) +os.environ["LOCAL_RANK"] = str(local_rank) +os.environ["WORLD_SIZE"] = str(world_size) +os.environ["RANK"] = str(local_rank) +os.environ["MASTER_PORT"] = str(port) def train( # model/data params