Skip to content

Commit 59e942d

Browse files
committed
rebase to pacify GitHub again
[ghstack-poisoned]
2 parents 7b97a9f + 3270089 commit 59e942d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1675
-228
lines changed

backends/apple/coreml/runtime/delegate/multiarray.mm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ bool init_bnns_descriptor(BNNSNDArrayDescriptor& bnns_descriptor, const MultiArr
124124

125125
bool copy_using_bnns(const MultiArray& src, MultiArray& dst) {
126126
if (src.layout().dataType() != dst.layout().dataType()) {
127-
return false;
127+
// Copying from FP16 to FP32 is supported and this is a common use case
128+
if (!(src.layout().dataType() == MultiArray::DataType::Float16 && dst.layout().dataType() == MultiArray::DataType::Float32)) {
129+
return false;
130+
}
128131
}
129132
if (dst.layout().num_bytes() < src.layout().num_bytes()) {
130133
return false;

backends/arm/quantizer/arm_quantizer.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,20 @@ def get_symmetric_quantization_config(
101101
weight_observer_or_fake_quant_ctr: ObserverOrFakeQuantizeConstructor = (
102102
MinMaxObserver
103103
)
104+
104105
# Determine the right observer/fake-quant constructor
105106
if is_qat:
106-
# Set plain fake-quant with true min/max
107-
weight_observer_or_fake_quant_ctr = FakeQuantize
107+
if is_per_channel:
108+
weight_observer_or_fake_quant_ctr = PerChannelMinMaxObserver
109+
else:
110+
# Set plain fake-quant with true min/max
111+
weight_observer_or_fake_quant_ctr = FakeQuantize
108112
else:
109113
# PTQ: set min/max observer
110114
weight_observer_or_fake_quant_ctr = (
111115
PerChannelMinMaxObserver if is_per_channel else MinMaxObserver
112116
)
113117

114-
extra_args = {"eps": 2**-12}
115-
116118
weight_quantization_spec = QuantizationSpec(
117119
dtype=torch.int8,
118120
quant_min=weight_qmin,

backends/arm/test/misc/test_bn_relu_folding_qat.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@ def forward(self, x: torch.Tensor):
4040

4141

4242
models = {
43-
"conv_bn_relu": ConvModule(batch_norm=True),
44-
"conv_relu": ConvModule(batch_norm=False),
43+
# name : (model, is_per_channel)
44+
"conv_bn_relu_per_channel": (ConvModule(batch_norm=True), True),
45+
"conv_relu_per_channel": (ConvModule(batch_norm=False), True),
46+
"conv_bn_relu_per_tensor": (ConvModule(batch_norm=True), False),
47+
"conv_relu_per_tensor": (ConvModule(batch_norm=False), False),
4548
}
4649

4750

48-
@common.parametrize("model", models)
49-
def test_qat_tosa_INT(model: torch.nn.Module):
51+
@common.parametrize("test_data", models)
52+
def test_qat_tosa_INT(test_data):
53+
model, per_channel = test_data
5054
pipeline = TosaPipelineINT[input_t1](model, model.test_data, [], [], qtol=1)
5155
tosa_version = conftest.get_option("tosa_version")
5256
tosa_profiles = {
@@ -59,7 +63,7 @@ def test_qat_tosa_INT(model: torch.nn.Module):
5963
Quantize(
6064
quantizer=quantizer,
6165
quantization_config=get_symmetric_quantization_config(
62-
is_qat=True, is_per_channel=False
66+
is_qat=True, is_per_channel=per_channel
6367
),
6468
is_qat=True,
6569
),

backends/arm/tosa/dialect/TARGETS

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
11
load("@fbcode_macros//build_defs:python_library.bzl", "python_library")
22

3+
python_library(
4+
name = "core",
5+
srcs = [
6+
"lib.py",
7+
"ops_registration.py",
8+
],
9+
deps = [
10+
"//caffe2:torch",
11+
"//executorch/backends/arm:tosa_specification",
12+
"//executorch/exir/dialects:lib",
13+
],
14+
)
15+
16+
python_library(
17+
name = "ops",
18+
srcs = glob(["ops/*.py"]),
19+
deps = [
20+
":core",
21+
"//caffe2:torch",
22+
"//executorch/backends/arm:tosa_specification",
23+
],
24+
)
25+
326
python_library(
427
name = "lib",
5-
srcs = glob(["*.py"]),
28+
srcs = ["__init__.py"],
29+
deps = [
30+
":core",
31+
":ops",
32+
"//caffe2:torch",
33+
"//executorch/backends/arm:tosa_specification",
34+
"//executorch/exir/dialects:lib",
35+
],
636
)

backends/vulkan/runtime/VulkanBackend.cpp

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <executorch/runtime/core/event_tracer_hooks_delegate.h>
2323
#endif // ET_EVENT_TRACER_ENABLED
2424
#include <executorch/runtime/core/exec_aten/util/tensor_util.h>
25+
#include <executorch/runtime/core/named_data_map.h>
2526
#include <executorch/runtime/platform/compiler.h>
2627
#include <executorch/runtime/platform/profiler.h>
2728

@@ -47,6 +48,7 @@ using executorch::runtime::Error;
4748
using executorch::runtime::EValue;
4849
using executorch::runtime::FreeableBuffer;
4950
using executorch::runtime::kTensorDimensionLimit;
51+
using executorch::runtime::NamedDataMap;
5052
using executorch::runtime::Result;
5153
using executorch::runtime::Span;
5254

@@ -66,14 +68,6 @@ using BytesVector =
6668
const flatbuffers::Vector<flatbuffers::Offset<vkgraph::VkBytes>>*;
6769
using UIntVector = const flatbuffers::Vector<uint32_t>*;
6870

69-
const uint8_t* get_constant_data_ptr(
70-
VkGraphPtr flatbuffer_graph,
71-
const int32_t buffer_idx,
72-
const uint8_t* constant_data) {
73-
VkBytesPtr constant_bytes = flatbuffer_graph->constants()->Get(buffer_idx);
74-
return constant_data + constant_bytes->offset();
75-
}
76-
7771
vkapi::ScalarType get_scalar_type(const vkgraph::VkDataType& vk_datatype) {
7872
switch (vk_datatype) {
7973
case vkgraph::VkDataType::BOOL:
@@ -166,17 +160,22 @@ class GraphBuilder {
166160
ComputeGraph* compute_graph_;
167161
VkGraphPtr flatbuffer_;
168162
const uint8_t* constant_data_;
163+
const NamedDataMap* named_data_map_;
164+
std::vector<FreeableBuffer> loaded_buffers_from_map_;
169165

170166
std::vector<ValueRef> ref_mapping_;
171167

172168
public:
173169
explicit GraphBuilder(
174170
ComputeGraph* compute_graph,
175171
VkGraphPtr flatbuffer,
176-
const uint8_t* constant_data)
172+
const uint8_t* constant_data,
173+
const NamedDataMap* named_data_map)
177174
: compute_graph_(compute_graph),
178175
flatbuffer_(flatbuffer),
179176
constant_data_(constant_data),
177+
named_data_map_(named_data_map),
178+
loaded_buffers_from_map_(),
180179
ref_mapping_() {}
181180

182181
void resize(uint32_t size) {
@@ -212,10 +211,27 @@ class GraphBuilder {
212211

213212
ValueRef ref;
214213
if (tensor_fb->constant_id() >= 0) {
215-
const uint8_t* tensor_data = get_constant_data_ptr(
216-
flatbuffer_, tensor_fb->constant_id(), constant_data_);
214+
VkBytesPtr constant_bytes =
215+
flatbuffer_->constants()->Get(tensor_fb->constant_id());
217216

218-
ref = compute_graph_->add_tensorref(dims_vector, dtype, tensor_data);
217+
if (constant_bytes->named_key() != nullptr &&
218+
constant_bytes->offset() == UINT64_MAX &&
219+
named_data_map_ != nullptr) {
220+
const std::string& data_name = constant_bytes->named_key()->str();
221+
Result<FreeableBuffer> buffer =
222+
named_data_map_->get_data(data_name.c_str());
223+
224+
VK_CHECK_COND(
225+
buffer.ok(),
226+
"Failed to get constant data for key %s from named_data_map. Error code: %u",
227+
data_name.c_str(),
228+
static_cast<uint32_t>(buffer.error()));
229+
ref = compute_graph_->add_tensorref(
230+
dims_vector, dtype, std::move(buffer.get()));
231+
} else {
232+
const uint8_t* tensor_data = constant_data_ + constant_bytes->offset();
233+
ref = compute_graph_->add_tensorref(dims_vector, dtype, tensor_data);
234+
}
219235
} else {
220236
ref = compute_graph_->add_tensor(
221237
dims_vector,
@@ -479,8 +495,10 @@ class VulkanBackend final : public ::executorch::runtime::BackendInterface {
479495
return true;
480496
}
481497

482-
ET_NODISCARD Error
483-
compileModel(const void* buffer_pointer, ComputeGraph* compute_graph) const {
498+
ET_NODISCARD Error compileModel(
499+
const void* buffer_pointer,
500+
ComputeGraph* compute_graph,
501+
const NamedDataMap* named_data_map) const {
484502
Result<VulkanDelegateHeader> header =
485503
VulkanDelegateHeader::parse(buffer_pointer);
486504

@@ -506,7 +524,8 @@ class VulkanBackend final : public ::executorch::runtime::BackendInterface {
506524

507525
VkGraphPtr flatbuffer_graph = vkgraph::GetVkGraph(flatbuffer_data);
508526

509-
GraphBuilder builder(compute_graph, flatbuffer_graph, constant_data);
527+
GraphBuilder builder(
528+
compute_graph, flatbuffer_graph, constant_data, named_data_map);
510529

511530
builder.build_graph();
512531

@@ -532,7 +551,8 @@ class VulkanBackend final : public ::executorch::runtime::BackendInterface {
532551
graph_config.external_adapter = vkapi::set_and_get_external_adapter();
533552
new (compute_graph) ComputeGraph(graph_config);
534553

535-
Error err = compileModel(processed->data(), compute_graph);
554+
const NamedDataMap* named_data_map = context.get_named_data_map();
555+
Error err = compileModel(processed->data(), compute_graph, named_data_map);
536556

537557
// This backend does not need its processed data after compiling the
538558
// model.

backends/vulkan/runtime/api/containers/Tensor.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,16 @@ VkMemoryRequirements vTensor::get_memory_requirements() const {
897897
return {};
898898
}
899899

900+
bool vTensor::memory_is_bound() const {
901+
switch (storage_type()) {
902+
case utils::kBuffer:
903+
return storage_->buffer_.has_memory();
904+
case utils::kTexture2D:
905+
case utils::kTexture3D:
906+
return storage_->image_.has_memory();
907+
}
908+
}
909+
900910
void vTensor::bind_allocation(const vkapi::Allocation& allocation) {
901911
switch (storage_type()) {
902912
case utils::kBuffer:
@@ -909,6 +919,18 @@ void vTensor::bind_allocation(const vkapi::Allocation& allocation) {
909919
}
910920
}
911921

922+
void vTensor::acquire_allocation(vkapi::Allocation&& allocation) {
923+
switch (storage_type()) {
924+
case utils::kBuffer:
925+
storage_->buffer_.acquire_allocation(std::move(allocation));
926+
break;
927+
case utils::kTexture2D:
928+
case utils::kTexture3D:
929+
storage_->image_.acquire_allocation(std::move(allocation));
930+
break;
931+
}
932+
}
933+
912934
void vTensor::update_metadata() {
913935
numel_ = utils::multiply_integers(sizes_);
914936
strides_ = calculate_strides(sizes_, dim_order_);

backends/vulkan/runtime/api/containers/Tensor.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,12 @@ class vTensor final {
560560
*/
561561
VmaAllocationCreateInfo get_allocation_create_info() const;
562562

563+
/*
564+
* Checks if the tensor's underlying buffer or image resource is bound to a
565+
* memory allocation.
566+
*/
567+
bool memory_is_bound() const;
568+
563569
/*
564570
* Return the VkMemoryRequirements of the underlying resource
565571
*/
@@ -570,6 +576,11 @@ class vTensor final {
570576
*/
571577
void bind_allocation(const vkapi::Allocation& allocation);
572578

579+
/*
580+
* Binds and acquires a rvalue memory allocation
581+
*/
582+
void acquire_allocation(vkapi::Allocation&& allocation);
583+
573584
private:
574585
/*
575586
* Assuming sizes, dim order, or axis mapping was modified, recompute all

0 commit comments

Comments
 (0)