Skip to content

Commit

Permalink
2024-07-26 nightly release (dbf87b0)
Browse files Browse the repository at this point in the history
  • Loading branch information
pytorchbot committed Jul 26, 2024
1 parent 9eb78a7 commit 319e381
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "set -e\n\nif ! command -v cmake &> /dev/null\nthen\n echo \"cmake not found, please install cmake.\"\n exit 1\nfi\n\nCMAKE_DIR=\"$TEMP_DIR/cmake\"\nrm -rf \"$CMAKE_DIR\"\n\nPLATFORM=\"SIMULATORARM64\"\nDEPLOYMENT_TARGET=\"17.0\"\n\nif [[ \"$PLATFORM_NAME\" == *\"iphoneos\"* ]]; then\n PLATFORM=\"OS64\"\nelif [[ \"$PLATFORM_NAME\" == *\"macos\"* ]]; then\n PLATFORM=\"MAC_ARM64\"\n DEPLOYMENT_TARGET=\"10.15\"\nfi\n\ncmake_build() {\n local src_dir=$1\n shift\n local extra_args=(\"$@\")\n local build_dir=\"$CMAKE_DIR/build/$(basename \"$src_dir\")\"\n\n mkdir -p \"$build_dir\" && cd \"$build_dir\"\n cmake -G Xcode \\\n -DCMAKE_BUILD_TYPE=\"Release\" \\\n -DCMAKE_TOOLCHAIN_FILE=\"$SRCROOT/../../../../third-party/ios-cmake/ios.toolchain.cmake\" \\\n -DCMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD=\"c++17\" \\\n -DCMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY=\"libc++\" \\\n -DPLATFORM=\"$PLATFORM\" \\\n -DDEPLOYMENT_TARGET=\"$DEPLOYMENT_TARGET\" \\\n \"${extra_args[@]}\" \\\n \"$src_dir\"\n cmake --build . --config \"Release\"\n cmake --install . --prefix \"$CMAKE_DIR\"\n}\n\ncmake_build \"$SRCROOT/../../../../extension/llm/third-party/abseil-cpp\" \\\n -DABSL_PROPAGATE_CXX_STD=ON\n\ncmake_build \"$SRCROOT/../../../../extension/llm/third-party/re2\" \\\n -DCMAKE_PREFIX_PATH=\"$CMAKE_DIR/lib/cmake/absl\"\n\necho \"$(find $CMAKE_DIR/lib -name \"*.a\" | sed -E 's|^.*/lib([^/]+)\\.a|-l\\1|g' | tr '\\n' ' ')\" > \"$CMAKE_DIR/linker_flags\"\n";
shellScript = "set -e\n\nif ! command -v cmake &> /dev/null\nthen\n echo \"cmake not found, please install cmake.\"\n exit 1\nfi\n\nCMAKE_DIR=\"$TEMP_DIR/cmake\"\nrm -rf \"$CMAKE_DIR\"\n\nPLATFORM=\"SIMULATORARM64\"\nDEPLOYMENT_TARGET=\"17.0\"\n\nif [[ \"$PLATFORM_NAME\" == *\"iphoneos\"* ]]; then\n PLATFORM=\"OS64\"\nelif [[ \"$PLATFORM_NAME\" == *\"macos\"* ]]; then\n PLATFORM=\"MAC_ARM64\"\n DEPLOYMENT_TARGET=\"10.15\"\nfi\n\ncmake_build() {\n local src_dir=$1\n shift\n local extra_args=(\"$@\")\n local build_dir=\"$CMAKE_DIR/build/$(basename \"$src_dir\")\"\n\n mkdir -p \"$build_dir\" && cd \"$build_dir\"\n cmake -G Xcode \\\n -DCMAKE_BUILD_TYPE=\"Release\" \\\n -DCMAKE_CXX_STANDARD=17 \\\n -DCMAKE_TOOLCHAIN_FILE=\"$SRCROOT/../../../../third-party/ios-cmake/ios.toolchain.cmake\" \\\n -DCMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD=\"c++17\" \\\n -DCMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY=\"libc++\" \\\n -DPLATFORM=\"$PLATFORM\" \\\n -DDEPLOYMENT_TARGET=\"$DEPLOYMENT_TARGET\" \\\n \"${extra_args[@]}\" \\\n \"$src_dir\"\n cmake --build . --config \"Release\"\n cmake --install . --prefix \"$CMAKE_DIR\"\n}\n\ncmake_build \"$SRCROOT/../../../../extension/llm/third-party/abseil-cpp\" \\\n -DABSL_PROPAGATE_CXX_STD=ON\n\ncmake_build \"$SRCROOT/../../../../extension/llm/third-party/re2\" \\\n -DCMAKE_PREFIX_PATH=\"$CMAKE_DIR/lib/cmake/absl\"\n \ncmake_build \"$SRCROOT/../../../../extension/llm/third-party/sentencepiece\" \\\n -DSPM_ENABLE_SHARED=OFF\n\necho \"$(find $CMAKE_DIR/lib -name \"*.a\" | sed -E 's|^.*/lib([^/]+)\\.a|-l\\1|g' | tr '\\n' ' ')\" > \"$CMAKE_DIR/linker_flags\"\n";
};
/* End PBXShellScriptBuildPhase section */

Expand Down
122 changes: 122 additions & 0 deletions examples/models/phi-3-mini/eager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.


# Script to run phi-3-mini model in eager mode.

import argparse
import time

import torch

from transformers import AutoTokenizer, Phi3ForCausalLM

end_of_text_token = 32000


def _generate_token(args, model, prompt_tokens):
current_token = 0
generated_tokens = []

print("Generating tokens:", end="", flush=True)

while current_token != end_of_text_token and len(generated_tokens) < args.seq_len:
outputs = model.forward(input_ids=prompt_tokens)
current_token = torch.argmax(outputs.logits[:, -1, :], dim=-1).item()
print(f" {current_token}", end="", flush=True)
generated_tokens.append(current_token)
prompt_tokens = torch.cat(
[prompt_tokens, torch.tensor([[current_token]], dtype=torch.long)], dim=-1
)

print("", flush=True)

return generated_tokens


def _generate_token_with_kv_cache(args, model, prompt_tokens):
print("Generating tokens:", end="", flush=True)

result = model.forward(input_ids=prompt_tokens, use_cache=True, return_dict=True)

current_token = torch.argmax(result.logits[:, -1, :], dim=-1).item()
current_key_value = result.past_key_values

print(f" {current_token}", end="", flush=True)

generated_tokens = [current_token]

while current_token != end_of_text_token and len(generated_tokens) < args.seq_len:
result = model.forward(
input_ids=torch.tensor([[current_token]], dtype=torch.long),
use_cache=True,
return_dict=True,
past_key_values=current_key_value,
)
current_token = torch.argmax(result.logits[:, -1, :], dim=-1).item()
current_key_value = result.past_key_values
print(f" {current_token}", end="", flush=True)
generated_tokens.append(current_token)

print("", flush=True)

return generated_tokens


def main(args):
seed = 42
torch.manual_seed(seed)
model_name = "microsoft/Phi-3-mini-4k-instruct"
model = Phi3ForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

tokens = tokenizer.encode(args.prompt, return_tensors="pt")

start = time.time()
generated_tokens = (
_generate_token_with_kv_cache(args, model, tokens)
if args.use_kv_cache
else _generate_token(args, model, tokens)
)
end = time.time()

print(
"Generated response: \n {}".format(
tokenizer.decode(
generated_tokens,
skip_special_tokens=True,
clean_up_tokenization_spaces=False,
)
),
flush=True,
)
print(f"Time spent: {end - start}", flush=True)


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-s",
"--seq_len",
type=int,
default=128,
help="Maximum number of tokens to generate",
)
parser.add_argument(
"-kv",
"--use_kv_cache",
default=False,
action="store_true",
help="Whether or not to use KV cache",
)
parser.add_argument(
"-p",
"--prompt",
type=str,
default="Tell me a story",
help="Prompt as input for the model",
)
main(parser.parse_args())
6 changes: 3 additions & 3 deletions exir/program/_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ def _gen_edge_manager_for_partitioners(
# check on which ops need to be preserved and which ops need to be decomposed
# Those which are truly preserved will be replaced with transformed ops
ops_set_to_not_decompose_by_program[name] = (
_replace_aten_ops_with_transformed_ops(name, program, partitioner)
_replace_aten_ops_with_transformed_ops(name, program, partitioner) or []
)
program = program.run_decompositions(_default_decomposition_table())

Expand Down Expand Up @@ -982,8 +982,8 @@ def _to_edge_transform_and_lower(

if not isinstance(partitioner, dict) and partitioner is not None:
partitioner = {"forward": partitioner}
else:
partitioner = {}
elif partitioner is None:
partitioner = {"forward": []}

edge_manager = _gen_edge_manager_for_partitioners(
partitioner, aten_programs, config, constant_methods
Expand Down

0 comments on commit 319e381

Please sign in to comment.