Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# --------------------------------------------------------------------------
 
"""Measure CUDA InferenceSession creation for a model with external data.
 
Example:
  python benchmark_cuda_model_loading.py \
      --model /path/to/model.onnx \
      --external-data /path/to/model.onnx.data \
      --evict-file-cache
 
Run this script in a fresh process for every sample. Cache eviction is applied
only to the paths passed on the command line and remains an operating-system
hint rather than a guarantee.
"""
 
import argparse
import json
import os
import time
 
import onnxruntime
 
 
def reading_thread_count(value):
    count = int(value)
    if not 0 <= count <= 64:
        raise argparse.ArgumentTypeError("must be between 0 and 64")
    return count
 
 
def evict_file_cache(path):
    if not hasattr(os, "posix_fadvise"):
        raise RuntimeError("file-cache eviction requires os.posix_fadvise")
 
    with open(path, "rb") as file:
        os.posix_fadvise(file.fileno(), 0, 0, os.POSIX_FADV_DONTNEED)
 
 
def main():
    parser = argparse.ArgumentParser(description="Benchmark CUDA model loading")
    parser.add_argument("--model", required=True, help="Path to the ONNX model")
    parser.add_argument(
        "--external-data",
        action="append",
        default=[],
        help="External-data file to evict with --evict-file-cache; may be specified more than once",
    )
    parser.add_argument("--device-id", type=int, default=0, help="CUDA device ID")
    parser.add_argument("--threads", type=int, default=96, help="Intra-op thread count")
    parser.add_argument(
        "--reading-threads",
        type=reading_thread_count,
        help="Override parallel reads per CUDA pinned staging buffer; 0 disables the loader (runtime default: 4)",
    )
    parser.add_argument(
        "--evict-file-cache",
        action="store_true",
        help="Advise the OS to evict the model and external-data files before creating the session",
    )
    args = parser.parse_args()
 
    paths = [args.model, *args.external_data]
    for path in paths:
        if not os.path.isfile(path):
            raise FileNotFoundError(path)
 
    if args.evict_file_cache:
        for path in paths:
            evict_file_cache(path)
 
    options = onnxruntime.SessionOptions()
    options.intra_op_num_threads = args.threads
    options.add_session_config_entry("session.intra_op.allow_spinning", "0")
    cuda_provider_options = {"device_id": args.device_id}
    if args.reading_threads is not None:
        cuda_provider_options["external_data_loader_reading_threads"] = args.reading_threads
 
    start = time.perf_counter()
    session = onnxruntime.InferenceSession(
        args.model,
        sess_options=options,
        providers=[
            ("CUDAExecutionProvider", cuda_provider_options),
            "CPUExecutionProvider",
        ],
    )
    elapsed = time.perf_counter() - start
    if "CUDAExecutionProvider" not in session.get_providers():
        raise RuntimeError("CUDAExecutionProvider was requested but is not active")
 
    print(
        json.dumps(
            {
                "active_providers": session.get_providers(),
                "device_id": args.device_id,
                "evict_file_cache": args.evict_file_cache,
                "model": os.path.abspath(args.model),
                "reading_threads": args.reading_threads if args.reading_threads is not None else 4,
                "seconds": elapsed,
                "threads": args.threads,
            },
            sort_keys=True,
        )
    )
 
 
if __name__ == "__main__":
    main()