|
| 1 | +from pathlib import PosixPath |
| 2 | +import os |
| 3 | +import tempfile |
| 4 | + |
| 5 | +import whowhatbench |
| 6 | +from whowhatbench.wwb import load_dataset |
| 7 | +from optimum.intel.openvino import OVModelForCausalLM |
| 8 | + |
| 9 | +from openvino_genai import ContinuousBatchingPipeline, SchedulerConfig, GenerationConfig, CacheEvictionConfig, AggregationMode |
| 10 | + |
| 11 | +from openvino_tokenizers import convert_tokenizer |
| 12 | +from openvino import serialize |
| 13 | +from transformers import AutoTokenizer |
| 14 | + |
| 15 | +model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" |
| 16 | +MAX_NEW_TOKENS = 128 |
| 17 | +SEQS_PER_REQUEST = 5 |
| 18 | +MAX_SEQUENCES = 100 |
| 19 | + |
| 20 | + |
| 21 | +model = OVModelForCausalLM.from_pretrained(model_id, export=True, trust_remote_code=True) |
| 22 | +tokenizer = AutoTokenizer.from_pretrained(model_id) |
| 23 | +model_path = PosixPath(tempfile.gettempdir()) / model_id |
| 24 | +model.save_pretrained(model_path) |
| 25 | + |
| 26 | +ov_tokenizer, ov_detokenizer = convert_tokenizer(tokenizer, with_detokenizer=True, skip_special_tokens=True) |
| 27 | +serialize(ov_tokenizer, model_path / "openvino_tokenizer.xml") |
| 28 | +serialize(ov_detokenizer, model_path / "openvino_detokenizer.xml") |
| 29 | + |
| 30 | +scheduler_config_noopt = SchedulerConfig() |
| 31 | +scheduler_config_noopt.num_kv_blocks = 300 |
| 32 | +scheduler_config_noopt.dynamic_split_fuse = True |
| 33 | +scheduler_config_noopt.max_num_batched_tokens = 256 |
| 34 | +scheduler_config_noopt.max_num_seqs = 256 |
| 35 | +scheduler_config_noopt.enable_prefix_caching = False |
| 36 | + |
| 37 | +scheduler_config_opt = SchedulerConfig() |
| 38 | +scheduler_config_opt.num_kv_blocks = 300 |
| 39 | +scheduler_config_opt.dynamic_split_fuse = True |
| 40 | +scheduler_config_opt.max_num_batched_tokens = 256 |
| 41 | +scheduler_config_opt.max_num_seqs = 256 |
| 42 | +scheduler_config_opt.use_cache_eviction = True |
| 43 | +scheduler_config_opt.enable_prefix_caching = False |
| 44 | +eviction_config = CacheEvictionConfig(32, 32, 128, AggregationMode.NORM_SUM) |
| 45 | +scheduler_config_opt.cache_eviction_config = eviction_config |
| 46 | + |
| 47 | +generation_config = GenerationConfig() |
| 48 | +generation_config.num_return_sequences = 1 |
| 49 | +generation_config.max_new_tokens = MAX_NEW_TOKENS |
| 50 | + |
| 51 | +data = load_dataset(path='squad', name=None, split='validation')["context"] |
| 52 | +data_dict = {"questions": list(dict({k: None for k in data}).keys())[:MAX_SEQUENCES]} |
| 53 | + |
| 54 | +model_cb_noopt = ContinuousBatchingPipeline(model_path.absolute().as_posix(), scheduler_config_noopt, "CPU", {}) |
| 55 | +model_cb_opt = ContinuousBatchingPipeline(model_path.absolute().as_posix(), scheduler_config_opt, "CPU", {}) |
| 56 | + |
| 57 | + |
| 58 | +GT_DATA_FILE = 'gt_data.csv' |
| 59 | + |
| 60 | +if os.path.exists(GT_DATA_FILE): |
| 61 | + evaluator = whowhatbench.Evaluator(base_model=model_cb_noopt, gt_data=GT_DATA_FILE, tokenizer=tokenizer, |
| 62 | + test_data=data_dict, generation_config=generation_config, |
| 63 | + max_new_tokens=MAX_NEW_TOKENS, seqs_per_request=3) |
| 64 | +else: |
| 65 | + evaluator = whowhatbench.Evaluator(base_model=model_cb_noopt, tokenizer=tokenizer, test_data=data_dict, |
| 66 | + generation_config=generation_config, max_new_tokens=MAX_NEW_TOKENS, |
| 67 | + seqs_per_request=3) |
| 68 | + evaluator.dump_gt('gt_data.csv') |
| 69 | + |
| 70 | + |
| 71 | +all_metrics_per_question, all_metrics = evaluator.score(model_cb_opt) |
| 72 | + |
| 73 | + |
| 74 | +print(all_metrics_per_question) |
| 75 | +print(all_metrics) |
| 76 | + |
| 77 | +metrics = ["similarity", "SDT norm"] |
| 78 | + |
| 79 | +for metric in metrics: |
| 80 | + worst_examples = evaluator.worst_examples(top_k=5, metric=metric) |
| 81 | + print("Metric: ", metric) |
| 82 | + for e in worst_examples: |
| 83 | + print("\t=========================") |
| 84 | + print(f"\t{metric}: ", e[metric]) |
| 85 | + print("\tPrompt: ", e["prompt"]) |
| 86 | + print("\tSource Model:\n ", "\t" + e["source_model"]) |
| 87 | + print("\tOptimized Model:\n ", "\t" + e["optimized_model"]) |
| 88 | + |
| 89 | +pipeline_opt_metrics = model_cb_opt.get_metrics() |
| 90 | +pipeline_noopt_metrics = model_cb_noopt.get_metrics() |
| 91 | + |
| 92 | +print(f"No-opt cache usage: max {pipeline_noopt_metrics.max_cache_usage:.3f}, avg {pipeline_noopt_metrics.avg_cache_usage:.3f}") |
| 93 | +print(f"Opt cache usage: max {pipeline_opt_metrics.max_cache_usage:.3f}, avg {pipeline_opt_metrics.avg_cache_usage:.3f}") |
| 94 | +max_optimization_ratio = (pipeline_noopt_metrics.max_cache_usage / pipeline_opt_metrics.max_cache_usage) |
| 95 | +avg_optimization_ratio = (pipeline_noopt_metrics.avg_cache_usage / pipeline_opt_metrics.avg_cache_usage) |
| 96 | +print(f"Optimization ratios: max {max_optimization_ratio:.3f}x, avg {avg_optimization_ratio:.3f}x") |
0 commit comments