forked from HabanaAI/Model-References
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhpu_multicard.py
229 lines (202 loc) · 7.32 KB
/
hpu_multicard.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
###############################################################################
# Copyright (C) 2023 Habana Labs, Ltd. an Intel Company
###############################################################################
# Changes:
# - Added to support for multi-card inference
# - Added support for higher batch size inference
# - Added quatinzation support
# - Updated warmup implementation
import argparse
from flask import Flask, request, jsonify, make_response
import dataset
import coco
import torch
import os
import numpy as np
import habana_frameworks.torch.gpu_migration
import habana_frameworks.torch.hpu as torch_hpu
import habana_frameworks.torch.core as htcore
import flask.cli
flask.cli.show_server_banner = lambda *args: None
import logging
logging.getLogger("werkzeug").disabled = True
import tools.generate_fp32_weights as gw
app = Flask(__name__)
# Initialize global vars
node = ""
model = None
ds = None
rank = 0
port = 0
warmup_samples = None
SUPPORTED_DATASETS = {
"coco-1024": (
coco.Coco,
dataset.preprocess,
coco.PostProcessCoco(),
{"image_size": [3, 1024, 1024]},
)
}
PORT = [3000, 3001, 3002, 3003, 3004, 3005, 3006, 3007]
@app.route('/predict/', methods=['POST'])
def predict():
"""Receives a query (e.g., a text) runs inference, and returns a prediction."""
query_id = request.get_json(force=True)['id']
data, _ = ds.get_samples(query_id)
result = model.predict(data)
processed_results = post_proc(result, query_id)
result = np.stack(processed_results)
response = make_response(result.tobytes())
response.headers['output_shape'] = list(result.shape)
return response
@app.route('/warmup/', methods=['POST'])
def warmup():
"""Receives a query (e.g., a text) runs inference, used for warmup only."""
_ = model.predict(warmup_samples)
return jsonify(response="Success")
@app.route('/getname/', methods=['POST', 'GET'])
def getname():
"""Returns the name of the SUT."""
return jsonify(name=f'Demo SUT (Network SUT) node' + (' ' + node) if node else '')
def get_backend(backend, **kwargs):
if backend == "pytorch":
from backend_pytorch import BackendPytorch
backend = BackendPytorch(**kwargs)
elif backend == "debug":
from backend_debug import BackendDebug
backend = BackendDebug()
else:
raise ValueError("unknown backend: " + backend)
return backend
if __name__ == '__main__':
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ("yes", "true", "t", "y", "1"):
return True
elif v.lower() in ("no", "false", "f", "n", "0"):
return False
else:
raise argparse.ArgumentTypeError("Boolean value expected.")
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", choices=SUPPORTED_DATASETS.keys(), help="dataset")
parser.add_argument("--dataset-path", required=True, help="path to the dataset")
parser.add_argument("--backend", help="Name of the backend")
parser.add_argument("--model-path", default=None, help="Path to model weights")
parser.add_argument(
"--dtype",
default="fp32",
choices=["fp32", "fp16", "bf16"],
help="dtype of the model",
)
parser.add_argument(
"--device",
default="cuda",
choices=["cuda", "cpu"],
help="device to run the benchmark",
)
parser.add_argument(
"--latent-framework",
default="torch",
choices=["torch", "numpy"],
help="framework to load the latents",
)
parser.add_argument(
"--max-batchsize",
type=int,
default=1,
help="max batch size in a single inference",
)
parser.add_argument('--hpu-graph',
const=True,
default=True,
type=str2bool,
nargs="?"
)
parser.add_argument(
"--quantize",
type=bool,
default=False,
help="enable quantization"
)
args = parser.parse_args()
node = 1
if args.dtype == "fp16":
dtype = torch.float16
elif args.dtype == "bf16":
dtype = torch.bfloat16
else:
dtype = torch.float32
# find backend
backend = get_backend(
args.backend,
precision=args.dtype,
device=args.device,
model_path=args.model_path,
batch_size=args.max_batchsize
)
htcore.hpu_set_env()
model = backend.load()
setattr(model.pipe, 'quantized', args.quantize)
if args.quantize:
# import quantization package and load quantization configuration
import habana_quantization_toolkit
# additional unet for last 2 steps
import copy
unet_bf16 = copy.deepcopy(backend.pipe.unet)
if args.hpu_graph and torch_hpu.is_available():
unet_bf16 = torch_hpu.wrap_in_hpu_graph(unet_bf16)
setattr(backend.pipe, 'unet_bf16', unet_bf16)
# replace bf16 weights to be quantized with fp32 weights
temp_dict = gw.get_unet_weights(args.model_path)
for name, module in backend.pipe.unet.named_modules():
if name in temp_dict.keys():
del module.weight
setattr(module,'weight',torch.nn.Parameter(temp_dict[name].clone().to(args.device)))
temp_dict.clear()
quant_config_full_fp8 = os.getenv('QUANT_CONFIG')
quant_config_partial_fp8 = os.getenv('QUANT_CONFIG_2')
habana_quantization_toolkit.prep_model(backend.pipe.unet, config_path=quant_config_full_fp8)
htcore.hpu_initialize(backend.pipe.unet)
habana_quantization_toolkit.prep_model(backend.pipe.unet_bf16, config_path=quant_config_partial_fp8)
htcore.hpu_initialize(backend.pipe.unet_bf16)
if args.hpu_graph and torch_hpu.is_available():
backend.pipe.unet = torch_hpu.wrap_in_hpu_graph(backend.pipe.unet)
# dataset to use
dataset_class, pre_proc, post_proc, kwargs = SUPPORTED_DATASETS[args.dataset]
ds = dataset_class(
data_path=args.dataset_path,
name=args.dataset,
pre_process=pre_proc,
pipe_tokenizer=model.pipe.tokenizer,
pipe_tokenizer_2=model.pipe.tokenizer_2,
latent_dtype=dtype,
latent_device=args.device,
latent_framework=args.latent_framework,
**kwargs,
)
# Create synthetic samples for warmup
syntetic_str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
latents_pt= torch.rand(ds.latents.shape, dtype=dtype).to(args.device)
warmup_samples = [
{
"input_tokens": ds.preprocess(syntetic_str, model.pipe.tokenizer),
"input_tokens_2": ds.preprocess(syntetic_str, model.pipe.tokenizer_2),
"latents":latents_pt,
}
for _ in range(args.max_batchsize)
]
ds.load_query_samples(list(range(0, ds.get_item_count())))
world_size = 1
# Check for CUDA availability
if torch.cuda.is_available():
# Check for distributed environment variables
local_rank = int(os.environ.get("LOCAL_RANK", 0))
world_size = int(os.environ.get("WORLD_SIZE", 1))
if world_size > 1:
# Initialize distributed process group
torch.distributed.init_process_group(backend='nccl', init_method='env://')
torch.cuda.set_device(local_rank)
rank = torch.distributed.get_rank()
port = PORT[rank]
app.run(debug=False, port=port)