-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
180 lines (144 loc) · 6.93 KB
/
inference.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
# Copyright (c) 2023 Graphcore Ltd. All rights reserved.
"""
This file defines the PopXL inference session that will be executed. This
example makes use of phased execution to avoid loading the entire 12 billion
parameter model into memory at one time, loading one transformer block at a time
instead. The main function `inference` is intended to be imported and used in
some front-end such as another inference script or a Jupyter notebook. Running
this file directly is for testing purposes only.
"""
import logging
import time
import numpy as np
import popdist
import popxl
from popxl import ops
from math import ceil
import popxl_addons as addons
from popxl_addons.named_tensors import NamedTensors
from popxl_addons.patterns import apply_pre_alias_patterns
from popxl_addons.remote import named_variable_buffers, load_remote_graph
from popxl_addons.utils import timer
from popxl_addons.task_session import TaskSession
from config import CONFIG_DIR, DollyConfig
from modelling.embedding import DollyEmbeddingsTP
from modelling.decoder import DollyDecoderBlockTP
from modelling.dolly_lm import DollyLMHeadTP, gather_logits_tp
from utils.setup import dolly_config_setup
__all__ = ["inference"]
def inference(config: DollyConfig) -> TaskSession:
assert config.execution.data_parallel == 1, "You can't use data-parallelism for inference"
replicas = config.execution.tensor_parallel
ir = popxl.Ir(replication="popdist" if popdist.isPopdistEnvSet() else replicas)
assert ir.replication_factor == replicas
# Options
opts = ir._pb_ir.getSessionOptions()
opts.partialsTypeMatMuls = "half"
opts.engineOptions["target.syncReplicasIndependently"] = "true"
with timer("PopXL IR construction"):
with ir.main_graph:
# ----- Define input and output streams -----
shard_size = ceil(config.model.embedding.vocab_size / config.execution.tensor_parallel)
input_shape = (
config.execution.micro_batch_size,
config.model.sequence_length,
)
input_streams = addons.InputStreams(
words=(input_shape, popxl.int32), last_token_indices=((config.execution.micro_batch_size,), popxl.int32)
)
output_streams = addons.OutputStreams(
next_token_logits=(
(config.execution.micro_batch_size, config.model.embedding.vocab_size),
config.model.dtype,
)
)
# ----- Build compute graphs -----
embeddings_facts, embeddings_graph = DollyEmbeddingsTP(config).create_graph(input_streams.words.spec)
layer_facts, layer_graph = DollyDecoderBlockTP(config).create_graph(*embeddings_graph.graph.outputs)
lm_facts, lm_graph = DollyLMHeadTP(config).create_graph(layer_graph.graph.outputs[0])
# ---- Transform graphs ----
addons.set_available_memory_proportion_by_ipu(ir, config.execution.available_memory_proportion)
# ----- Create Variables -----
# Create RemoteBuffers for each variable
embeddings_buffers = named_variable_buffers(embeddings_facts, shard_over_dict=False)
layer_buffers = named_variable_buffers(layer_facts, entries=config.model.layers, shard_over_dict=False)
lm_buffers = named_variable_buffers(lm_facts, shard_over_dict=False)
variables = NamedTensors()
transformer = NamedTensors()
transformer.insert(
"embeddings", embeddings_facts.init_remote(embeddings_buffers, 0, "embeddings", empty=True)
)
transformer.insert(
"decoder",
NamedTensors.from_dict(
{
n: layer_facts.init_remote(layer_buffers, n, f"decoder.{n}", empty=True)
for n in range(config.model.layers)
}
),
)
variables.insert("transformer", transformer)
variables.insert("lm_head", lm_facts.init_remote(lm_buffers, 0, "lm_head", empty=True))
# ---- Execute ----
with popxl.in_sequence():
word = ops.host_load(input_streams.words)
last_token_indices = ops.host_load(input_streams.last_token_indices)
# Embeddings
load_graph, names = load_remote_graph(embeddings_buffers)
embedding_vars = NamedTensors.pack(names, load_graph.call(0))
(x,) = embeddings_graph.bind(embedding_vars).call(word)
# Decoder
load_graph, names = load_remote_graph(layer_buffers)
def layer(x, n):
load_graph, names = load_remote_graph(layer_buffers)
layer_vars = NamedTensors.pack(names, load_graph.call(n))
(x,) = layer_graph.bind(layer_vars).call(x)
return x, n + 1
i = popxl.constant(0, name="layer_index")
layers_graph = ir.create_graph(layer, x, i)
x, _ = ops.repeat(layers_graph, config.model.layers, x, i)
# LM head
load_graph, names = load_remote_graph(lm_buffers)
squad_vars = NamedTensors.pack(names, load_graph.call(0))
(logits,) = lm_graph.bind(squad_vars).call(x)
logits = gather_logits_tp(config, logits, last_token_indices)
ops.host_store(
output_streams.next_token_logits, logits.reshape_(output_streams.next_token_logits.shape)
)
# Run `OpToIdentityPattern` among others part of `PreAliasPatterns`
apply_pre_alias_patterns(ir, level="default")
logging.info("PopXL IR construction complete")
ir.num_host_transfers = config.execution.device_iterations
session = TaskSession(
inputs=input_streams,
outputs=output_streams,
state=NamedTensors(fwd=variables),
ir=ir,
device_desc="ipu_hw",
weights_to_host_on_exit=False,
)
logging.info("PopXL compilation complete")
return session
def main():
from modelling.hf_mapping import hf_mapping_lm_tp
"""Run a benchmark configuration"""
config, _, _ = dolly_config_setup(CONFIG_DIR / "inference.yml", "release", "tiny", hf_model_setup=False)
session = inference(config)
inputs = {
stream: np.ones(session._full_input_shape(stream.shape), stream.dtype.as_numpy())
for stream in session.expected_inputs()
}
with session:
# Skip one result
session.run(inputs)
durations = []
for _ in range(5):
start = time.time()
session.run(inputs)
durations.append(time.time() - start)
duration = np.mean(durations)
samples_per_step = config.execution.micro_batch_size
result_str = f"Duration: {duration} s " f"Throughput: {samples_per_step/duration:6.1f} samples/s "
logging.info(result_str)
if __name__ == "__main__":
main()