forked from google-coral/libedgetpu
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpackage_registry.h
806 lines (640 loc) · 29.4 KB
/
package_registry.h
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef DARWINN_DRIVER_PACKAGE_REGISTRY_H_
#define DARWINN_DRIVER_PACKAGE_REGISTRY_H_
#include <memory>
#include <mutex> // NOLINT
#include <string>
#include <unordered_map>
#include <vector>
#include "api/buffer.h"
#include "api/chip.h"
#include "api/driver_options_generated.h"
#include "api/execution_context_interface.h"
#include "api/layer_information.h"
#include "api/package_reference.h"
#include "driver/aligned_allocator.h"
#include "driver/device_buffer_mapper.h"
#include "driver/instruction_buffers.h"
#include "driver/memory/dram_allocator.h"
#include "driver/package_verifier.h"
#include "executable/executable_generated.h"
#include "port/status_macros.h"
#include "port/statusor.h"
#include "port/std_mutex_lock.h"
#include "port/thread_annotations.h"
namespace platforms {
namespace darwinn {
namespace driver {
class PackageReference;
// Holds the input and output layer info from an executable.
class ExecutableLayersInfo {
public:
ExecutableLayersInfo(const Executable* executable);
~ExecutableLayersInfo() = default;
// This class is neither copyable nor movable.
ExecutableLayersInfo(const ExecutableLayersInfo&) = delete;
ExecutableLayersInfo& operator=(const ExecutableLayersInfo&) = delete;
// Returns the index of input layer with given name.
StatusOr<int> InputIndex(const std::string& name) const;
// Returns the index of output layer with given name.
StatusOr<int> OutputIndex(const std::string& name) const;
// Returns number of input layers.
int NumInputLayers() const { return inputs_.size(); }
// Returns number of output layers.
int NumOutputLayers() const { return outputs_.size(); }
// Returns list of input layer names.
const std::vector<std::string>& InputLayerNames() const {
return input_layer_names_;
}
// Returns list of output layer names.
const std::vector<std::string>& OutputLayerNames() const {
return output_layer_names_;
}
// Returns information on given input layer. Returns nullptr if index is out
// of bounds.
const api::InputLayerInformation* InputLayer(int index) const;
// Returns information on given output layer. Returns nullptr if index is out
// of bounds.
const api::OutputLayerInformation* OutputLayer(int index) const;
// Returns information on given input layer.
StatusOr<const api::InputLayerInformation*> InputLayer(
const std::string& layer_name) const;
// Returns information on given output layer.
StatusOr<const api::OutputLayerInformation*> OutputLayer(
const std::string& layer_name) const;
// Returns the expected byte size of activations for given input layer index.
int InputLayerSizeBytes(int index) const {
CHECK(InputLayer(index) != nullptr);
return InputLayer(index)->ActualSizeBytes();
}
// Returns the expected byte size of activations for given input layer index.
// This is post-padding, if any.
// TODO Remove this method.
int InputLayerPaddedSizeBytes(int index) const {
CHECK(InputLayer(index) != nullptr);
return InputLayer(index)->PaddedSizeBytes();
}
// Returns the expected byte size of activations for given output layer index.
int OutputLayerSizeBytes(int index) const {
CHECK(OutputLayer(index) != nullptr);
return OutputLayer(index)->ActualSizeBytes();
}
// Returns the expected size (in value count) of activations for given input
// layer index. This is pre-padding, if any.
int InputLayerSize(int index) const {
auto layer = InputLayer(index);
CHECK(layer != nullptr);
return layer->y_dim() * layer->x_dim() * layer->z_dim() *
layer->execution_count_per_inference();
}
// Returns the expected size (in value count) of activations for given input
// layer index. This is pre-padding, if any.
int OutputLayerSize(int index) const {
auto layer = OutputLayer(index);
CHECK(layer != nullptr);
return layer->y_dim() * layer->x_dim() * layer->z_dim() *
layer->execution_count_per_inference();
}
// Returns the expected size of activations for given input layer.
// Prefer index based APIs for performance.
StatusOr<int> InputLayerSizeBytes(const std::string& name) const;
// Returns the expected size of activations for given input layer including
// padding bytes (if any).
// Prefer index based APIs for performance.
// TODO Remove this method.
StatusOr<int> InputLayerPaddedSizeBytes(const std::string& name) const;
// Returns the expected size of activations for given output layer.
// Prefer index based APIs for performance.
StatusOr<int> OutputLayerSizeBytes(const std::string& name) const;
// Returns name for given input layer index.
std::string InputLayerName(int index) const {
CHECK(InputLayer(index) != nullptr);
return InputLayer(index)->name();
}
// Returns name for given output layer index.
std::string OutputLayerName(int index) const {
CHECK(OutputLayer(index) != nullptr);
return OutputLayer(index)->name();
}
// Returns if on-chip DRAM is needed in either input or output layers.
bool NeedsDramInLayers() const { return needs_dram_in_layers_; }
private:
// Vector with list of input layer names.
std::vector<std::string> input_layer_names_;
// Vector with list of output layer names.
std::vector<std::string> output_layer_names_;
// Vector with detailed input layer information.
std::vector<api::InputLayerInformation> inputs_;
// Vector with detailed outpu layer information.
std::vector<api::OutputLayerInformation> outputs_;
// Maps input layer names to indices.
std::unordered_map<std::string, int> input_map_;
// Maps output layer names to indices.
std::unordered_map<std::string, int> output_map_;
// Specifies if this executable needs on-chip DRAM for input or output layers.
bool needs_dram_in_layers_ = false;
};
// Reference to a single executable.
class ExecutableReference {
public:
// This class is neither copyable nor movable.
ExecutableReference(const ExecutableReference&) = delete;
ExecutableReference& operator=(const ExecutableReference&) = delete;
// Returns the index of input layer with given name.
StatusOr<int> InputIndex(const std::string& name) const {
return executable_layers_info_->InputIndex(name);
}
// Returns the index of output layer with given name.
StatusOr<int> OutputIndex(const std::string& name) const {
return executable_layers_info_->OutputIndex(name);
}
// Returns number of input layers.
int NumInputLayers() const {
return executable_layers_info_->NumInputLayers();
}
// Returns number of output layers.
int NumOutputLayers() const {
return executable_layers_info_->NumOutputLayers();
}
// Returns list of input layer names.
const std::vector<std::string>& InputLayerNames() const {
return executable_layers_info_->InputLayerNames();
}
// Returns list of output layer names.
const std::vector<std::string>& OutputLayerNames() const {
return executable_layers_info_->OutputLayerNames();
}
// Returns information on given input layer. Returns nullptr if index is out
// of bounds.
const api::InputLayerInformation* InputLayer(int index) const {
return executable_layers_info_->InputLayer(index);
}
// Returns information on given output layer. Returns nullptr if index is out
// of bounds.
const api::OutputLayerInformation* OutputLayer(int index) const {
return executable_layers_info_->OutputLayer(index);
}
// Returns information on given input layer.
StatusOr<const api::InputLayerInformation*> InputLayer(
const std::string& layer_name) const {
return executable_layers_info_->InputLayer(layer_name);
}
// Returns information on given output layer.
StatusOr<const api::OutputLayerInformation*> OutputLayer(
const std::string& layer_name) const {
return executable_layers_info_->OutputLayer(layer_name);
}
// Returns the expected byte size of activations for given input layer index.
int InputLayerSizeBytes(int index) const {
return executable_layers_info_->InputLayerSizeBytes(index);
}
// Returns the expected byte size of activations for given input layer index.
// This is post-padding, if any.
// TODO Remove this method.
int InputLayerPaddedSizeBytes(int index) const {
return executable_layers_info_->InputLayerPaddedSizeBytes(index);
}
// Returns the expected byte size of activations for given output layer index.
int OutputLayerSizeBytes(int index) const {
return executable_layers_info_->OutputLayerSizeBytes(index);
}
// Returns the expected size (in value count) of activations for given input
// layer index. This is pre-padding, if any.
int InputLayerSize(int index) const {
return executable_layers_info_->InputLayerSize(index);
}
// Returns the expected size (in value count) of activations for given input
// layer index. This is pre-padding, if any.
int OutputLayerSize(int index) const {
return executable_layers_info_->OutputLayerSize(index);
}
// Returns the expected size of activations for given input layer.
// Prefer index based APIs for performance.
StatusOr<int> InputLayerSizeBytes(const std::string& name) const {
return executable_layers_info_->InputLayerSizeBytes(name);
}
// Returns the expected size of activations for given input layer including
// padding bytes (if any).
// Prefer index based APIs for performance.
// TODO Remove this method.
StatusOr<int> InputLayerPaddedSizeBytes(const std::string& name) const {
return executable_layers_info_->InputLayerPaddedSizeBytes(name);
}
// Returns the expected size of activations for given output layer.
// Prefer index based APIs for performance.
StatusOr<int> OutputLayerSizeBytes(const std::string& name) const {
return executable_layers_info_->OutputLayerSizeBytes(name);
}
// Returns name for given input layer index.
std::string InputLayerName(int index) const {
return executable_layers_info_->InputLayerName(index);
}
// Returns name for given output layer index.
std::string OutputLayerName(int index) const {
return executable_layers_info_->OutputLayerName(index);
}
// Returns batch size.
int BatchSize() const { return executable_->batch_size(); }
// Executable
const darwinn::Executable& executable() const { return *executable_; }
// Memory aligned copy of the parameters.
const Buffer& parameters() const { return parameters_; }
// Sets mapped parameters.
// Move-only. The given mapped_parameter will be unmapped during destruction
// time, so we cannot allow copy-construction, to avoid doubly unmapping a
// Device Buffer.
Status SetMappedParameters(MappedDeviceBuffer&& mapped_parameters);
// Unmaps the parameters buffer from the device.
Status UnmapParameters();
// Returns true if the parameters buffer is already mapped to the device.
bool ParametersMapped() const { return parameters_mapped_; }
// Returns the device mapped buffer for the parameters in this executable.
const DeviceBuffer& GetParameterDeviceBuffer() const {
return mapped_parameters_.device_buffer();
}
// Scratch buffer, if the executable requires scratch space. If not, then the
// buffer will be invalid.
Buffer scratch() const { return scratch_; }
// Validates that the given input buffer is compatible with the executable.
Status ValidateInput(const std::string& input_name,
const Buffer& input) const;
// Validates that the given output buffer is compatible with the executable.
Status ValidateOutput(const std::string& output_name,
const Buffer& output) const;
// Returns the parameter-caching token which is unique across models that are
// compiled together and can cache their parameters on TPU SRAM at the same
// time. If 0, it means this executable's parameters cannot safely co-exist
// with those of others. Please note that tokens are not limited to parameter
// cached models. We could have a stand-alone compiled model that still has
// a token to ensure us it will not overwite cached parameters of other
// models.
uint64 ParameterCachingToken() const {
return executable().parameter_caching_token();
}
// Returns the estimated runtime in cycles for this executable.
int64 EstimatedCycles() const {
return executable().estimated_cycles_64bit();
}
// Reuses or creates instruction buffers.
std::unique_ptr<InstructionBuffers> GetInstructionBuffers(
Allocator* allocator);
// Returns instruction buffers back to executable reference.
// TODO: Add pool size limit. This currently doesn't have size
// limit, and if there are many requests happened at the same time, we might
// increase the total memory footprint. Notice that this won't increase
// the memory peak size but will hold them longer. If this becomes an issue
// we should investigate what's the correct size limit.
void ReturnInstructionBuffers(
std::unique_ptr<InstructionBuffers> instruction_buffers);
// Makes sure parameters are present in host or TPU DRAM to be used in an
// inference. This method is not thread-safe.
Status PrepareParameters();
// Resets any assumptions about parameters being loaded on TPU DRAM. This
// method is not thread-safe.
void ResetParametersLoaded();
// Specifies if this executable needs on-chip DRAM to execute.
bool NeedsDram() const { return needs_dram_; }
// Returns the amount of narrow memory (in bytes) used by each tile in this
// executable.
int64 UsedNarrowMemoryBytesPerTile() const {
return executable_->used_narrow_memory_bytes_per_tile();
}
const PackageReference& GetPackageReference() const {
return *package_reference_;
}
private:
friend class PackageReference;
// Allow constructions through the friend classes only.
ExecutableReference(const Executable* executable, Allocator* allocator,
DramAllocator* dram_allocator, PackageReference* pkg_ref);
// Memory aligned copy of parameters.
Buffer parameters_;
// Mapped parameters.
MappedDeviceBuffer mapped_parameters_;
// Scratch buffer, if the executable requires scratch space. If not, then the
// buffer will be invalid.
Buffer scratch_;
// Holds the backing executable.
const Executable* executable_;
// Holds the information on input and output layers.
std::unique_ptr<ExecutableLayersInfo> executable_layers_info_;
mutable std::mutex instruction_buffers_vector_mutex_;
std::vector<std::unique_ptr<InstructionBuffers>> instruction_buffers_vector_
GUARDED_BY(instruction_buffers_vector_mutex_);
// Specifies if parameters of this executable are mapped to the device.
bool parameters_mapped_ = false;
// Specifies if parameters are already loaded to on-chip DRAM.
bool parameters_loaded_ = false;
// Specifies if this executable needs on-chip DRAM to execute.
// The DRAM might be needed in input and output layers, parameters, or scratch
// memory.
bool needs_dram_ = false;
// Pointer to package reference that contains this object. This object does
// not own package_reference_.
PackageReference *package_reference_;
};
// Contains an executable package.
class PackageReference : public api::PackageReference {
public:
// This class is neither copyable nor movable.
PackageReference(const PackageReference&) = delete;
PackageReference& operator=(const PackageReference&) = delete;
// Verifies the digital signature in the package.
Status VerifySignature() const override {
return verifier_->VerifySignature(package_buffer_.ptr());
}
// The following methods just call their counterpart in
// MainExecutableReference().
StatusOr<int> InputIndex(const std::string& name) const override {
return MainExecutableReference()->InputIndex(name);
}
StatusOr<int> OutputIndex(const std::string& name) const override {
return MainExecutableReference()->OutputIndex(name);
}
int NumInputLayers() const override {
return MainExecutableReference()->NumInputLayers();
}
int NumOutputLayers() const override {
return MainExecutableReference()->NumOutputLayers();
}
const std::vector<std::string>& InputLayerNames() const override {
return MainExecutableReference()->InputLayerNames();
}
const std::vector<std::string>& OutputLayerNames() const override {
return MainExecutableReference()->OutputLayerNames();
}
const api::InputLayerInformation* InputLayer(int index) const override {
return MainExecutableReference()->InputLayer(index);
}
const api::OutputLayerInformation* OutputLayer(int index) const override {
return MainExecutableReference()->OutputLayer(index);
}
StatusOr<const api::InputLayerInformation*> InputLayer(
const std::string& layer_name) const override {
return MainExecutableReference()->InputLayer(layer_name);
}
StatusOr<const api::OutputLayerInformation*> OutputLayer(
const std::string& layer_name) const override {
return MainExecutableReference()->OutputLayer(layer_name);
}
int InputLayerSizeBytes(int index) const override {
return MainExecutableReference()->InputLayerSizeBytes(index);
}
// TODO Remove this method.
int InputLayerPaddedSizeBytes(int index) const override {
return MainExecutableReference()->InputLayerPaddedSizeBytes(index);
}
int OutputLayerSizeBytes(int index) const override {
return MainExecutableReference()->OutputLayerSizeBytes(index);
}
int InputLayerSize(int index) const override {
return MainExecutableReference()->InputLayerSize(index);
}
int OutputLayerSize(int index) const override {
return MainExecutableReference()->OutputLayerSize(index);
}
StatusOr<int> InputLayerSizeBytes(const std::string& name) const override {
return MainExecutableReference()->InputLayerSizeBytes(name);
}
// TODO Remove this method.
StatusOr<int> InputLayerPaddedSizeBytes(
const std::string& name) const override {
return MainExecutableReference()->InputLayerPaddedSizeBytes(name);
}
StatusOr<int> OutputLayerSizeBytes(const std::string& name) const override {
return MainExecutableReference()->OutputLayerSizeBytes(name);
}
std::string InputLayerName(int index) const override {
return MainExecutableReference()->InputLayerName(index);
}
std::string OutputLayerName(int index) const override {
return MainExecutableReference()->OutputLayerName(index);
}
int BatchSize() const override {
return MainExecutableReference()->BatchSize();
}
Status SetLatencyTolerance(int64 latency_tolerance_ms) override;
// Returns a vector of all executable references in this package.
std::vector<driver::ExecutableReference*> AllExecutableReferences() const;
// Returns the main executable reference to refer to for read-only information
// (e.g. number of layers).
const driver::ExecutableReference* MainExecutableReference() const {
return standalone_reference_ ? standalone_reference_.get()
: inference_reference_.get();
}
// Returns true if this package is parameter-caching-enabled.
bool ParameterCachingEnabled() const {
return parameter_caching_reference_ != nullptr;
}
// Returns the inference executable reference in this package. You can make
// sure such reference exists by calling ParameterCachingEnabled method.
const driver::ExecutableReference* InferenceExecutableReference() const {
return inference_reference_.get();
}
// Returns the parameter-caching executable reference in this package. You can
// make sure such reference exists by calling ParameterCachingEnabled method.
const driver::ExecutableReference* ParameterCachingExecutableReference()
const {
return parameter_caching_reference_.get();
}
// Returns true if parameters of this package are mapped to the device.
StatusOr<bool> ParametersMapped() const;
// Specifies if this package needs on-chip DRAM to execute.
bool NeedsDram() const;
// Sets the execution context interface. This class owns the execution
// context.
void SetExecutionContextInterface(
std::unique_ptr<api::ExecutionContextInterface>
execution_context_interface) override {
execution_context_interface_ = std::move(execution_context_interface);
}
std::string ModelIdentifier() const override {
return flatbuffers::GetString(package_->model_identifier());
}
// Returns the stored execution context interface. This class still owns the
// object.
api::ExecutionContextInterface* GetExecutionContextInterface() const {
return execution_context_interface_.get();
}
// Returns the amount of time in milliseconds that this package can tolerate
// for an inference to run (including parameter-caching). If batched, then for
// all batch elements to complete.
int64 LatencyToleranceMs() const { return latency_tolerance_ms_; }
private:
friend class PackageRegistry;
// Allow constructions through the ExecutableRegistry class only.
//
// The current implementation allows either a stand-alone executable or
// parameter-caching + inference.
// Constructor for stand-alone executable.
PackageReference(const Buffer& package_buffer,
const Executable* standalone_executable,
Allocator* allocator, DramAllocator* dram_allocator,
PackageVerifier* verifier);
// Constructor for a parameter cached package.
PackageReference(const Buffer& package_buffer,
const Executable* parameter_caching_executable,
const Executable* inference_executable, Allocator* allocator,
DramAllocator* dram_allocator, PackageVerifier* verifier);
// Unmaps parameters of all executables in this package.
Status UnmapParameters();
// Buffer backing the package buffer.
Buffer package_buffer_;
// The flatbuffer representation of the package we are wrapping.
const Package* package_;
// A ExecutableVerifier for checking digital signatures on executable
// packages.
const PackageVerifier* const verifier_;
// The ExecutableReference for the parameter-caching executable.
std::unique_ptr<driver::ExecutableReference> parameter_caching_reference_;
// The Executable reference for the inference executable.
std::unique_ptr<driver::ExecutableReference> inference_reference_;
// The Executable reference for the stand-alone executable.
std::unique_ptr<driver::ExecutableReference> standalone_reference_;
// The execution context for the package reference.
std::unique_ptr<api::ExecutionContextInterface> execution_context_interface_;
// Maximum number of milliseconds this package can tolerate for an inference
// request to run.
int64 latency_tolerance_ms_ = -1;
};
// A registry for executable files. Most methods do not have built-in thread-
// safety and rely on base driver class to ensure that. Some methods require
// thread-safety with respect to their call-sites in base driver and that is
// implemented in this class.
class PackageRegistry {
public:
PackageRegistry(api::Chip chip,
std::unique_ptr<PackageVerifier> executable_verifier,
DramAllocator* dram_allocator);
// Constructs an ExecutableRegistry that will check to make sure all
// registered executables are for the correct chip. However, it does not
// support DRAM nor signature verification. Please prefer the first
// constructor.
explicit PackageRegistry(api::Chip chip);
// Constructs an ExecutableRegistry that does not check if the executables
// being registered are for the correct device. Please prefer the first
// constructor.
PackageRegistry();
~PackageRegistry() = default;
// This class is neither copyable nor movable.
PackageRegistry(const PackageRegistry&) = delete;
PackageRegistry& operator=(const PackageRegistry&) = delete;
// Returns the main executable layer info from the given executable binary.
static StatusOr<std::unique_ptr<ExecutableLayersInfo>>
GetMainExecutableLayersInfoFromBinary(const char* executable_content,
size_t length);
// Registers a serialized executable binary. Once the executable is
// registered, driver has its own copy of it so there would be no need to keep
// the executable_content in memory.
StatusOr<const api::PackageReference*> RegisterSerialized(
const std::string& executable_content);
StatusOr<const api::PackageReference*> RegisterSerialized(
const char* executable_content, size_t length);
// Same as above, but the executable is read from the given file.
StatusOr<const api::PackageReference*> RegisterFile(
const std::string& executable_filename);
// Unregisters an executable. Invokes the callback to unmap the parameter.
Status Unregister(const api::PackageReference* package_reference);
// Unregisteres all registered executables.
Status UnregisterAll() LOCKS_EXCLUDED(registrations_mutex_);
// Unmaps all parameters in all registered packages.
Status UnmapAllParameters() LOCKS_EXCLUDED(registrations_mutex_);
// Returns the number of registered executables.
int GetRegistrySize() const LOCKS_EXCLUDED(registrations_mutex_) {
StdMutexLock registration_lock(®istrations_mutex_);
return registrations_.size();
}
// Returns all the package references registered here.
std::vector<api::PackageReference*> GetAllRegistrations() const
LOCKS_EXCLUDED(registrations_mutex_);
// Resets any assumptions about parameters of any current registrations being
// loaded on TPU DRAM.
void ResetParametersLoaded() LOCKS_EXCLUDED(registrations_mutex_);
private:
// Returns the main executable from the executable map.
// Returns error if failed to find main executable or had unexpected
// executable combinations.
static StatusOr<const Executable*> GetMainExecutableFromExecutableMap(
std::unordered_map<ExecutableType, const Executable*>);
// Returns the parameter caching executable from the executable map.
// Returns nullptr if no parameter caching executable could be found in the
// map.
// Returns error if had unexpected executable combinations.
static StatusOr<const Executable*> GetPCExecutableFromExecutableMap(
std::unordered_map<ExecutableType, const Executable*>);
// Registers an executable package binary.
StatusOr<const api::PackageReference*> RegisterPackage(
const Buffer& package_buffer);
// Takes in a multi-executable and returns a map of each executable type to
// its executable pointer. It will return an error in any illegal combination.
// Legitimate combinations are:
//
// 1. A single executable (no matter what type).
// 2. 1 parameter-caching and 1 inference executable.
// 3. 1 parameter-caching, 1 inference and 1 stand-alone executable.
static StatusOr<std::unordered_map<ExecutableType, const Executable*>>
ExtractExecutables(const MultiExecutable& multi_executable);
// Takes in executable binary content and returns a map of each executable
// type to its executable pointer.
// The inputs are pointers to the executable binary, the length of the binary,
// and the targeted chip to run this executables on.
static StatusOr<std::unordered_map<ExecutableType, const Executable*>>
GetExecutablesFromBinary(const char* executable_content, size_t length);
// Fetches an Executable from its serialized version and performs some
// verification checks (does not include signature verification).
static StatusOr<const Executable*> FetchAndVerifyExecutable(
const char* executable_serialized, size_t length);
// Checks if the chip config specified in the executable binary matches the
// one registered to this package registry.
Status VerifyExecutableMatchesChip(const Executable*) const;
const api::PackageReference* SetRegistrations(
std::unique_ptr<api::PackageReference> api_package_ref)
LOCKS_EXCLUDED(registrations_mutex_);
// Allocator.
AlignedAllocator allocator_;
// A pointer to the entity responsible for allocating on-chip DRAM buffers.
DramAllocator* dram_allocator_;
// A mutex to synchronize access to registrations_.
mutable std::mutex registrations_mutex_;
// Tracks registrations.
// Uses a map instead of a set, since looking up an std::set of unique_ptr is
// tricky.
std::unordered_map<const api::PackageReference*,
std::unique_ptr<api::PackageReference>>
registrations_ GUARDED_BY(registrations_mutex_);
// Executables will be checked to ensure they were compiled for this chip.
// Can be kUnknown to disable checking.
const api::Chip chip_;
// A verifier for checking digital signatures on executable packages.
std::unique_ptr<PackageVerifier> verifier_;
};
} // namespace driver
} // namespace darwinn
} // namespace platforms
#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ < 6
namespace std {
template<>
struct hash<::platforms::darwinn::ExecutableType> {
typedef ::platforms::darwinn::ExecutableType argument_type;
typedef std::underlying_type<argument_type>::type underlying_type;
typedef std::hash<underlying_type>::result_type result_type;
result_type operator()(const argument_type& arg) const {
std::hash<underlying_type> hasher;
return hasher(static_cast<underlying_type>(arg));
}
};
}
#endif
#endif // DARWINN_DRIVER_PACKAGE_REGISTRY_H_