Skip to content

Commit 5347bce

Browse files
committed
resotore mul3
1 parent cc14b6e commit 5347bce

File tree

4 files changed

+31
-125
lines changed

4 files changed

+31
-125
lines changed

examples/qualcomm/custom_op/custom_ops_1.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,16 @@ def annotate_custom(gm: torch.fx.GraphModule) -> None:
6969
This function is specific for custom op.
7070
The source_fn of the rewritten nn module turns out to be "my_ops.mul3.default"
7171
"""
72-
from executorch.backends.qualcomm.quantizer.annotators import _is_annotated
72+
from executorch.backends.qualcomm.quantizer.annotators import (
73+
_is_annotated,
74+
QUANT_ANNOTATION_KEY,
75+
)
76+
7377
from executorch.backends.qualcomm.quantizer.qconfig import (
7478
get_ptq_per_channel_quant_config,
7579
)
7680
from torch.fx import Node
7781
from torchao.quantization.pt2e.quantizer import QuantizationAnnotation
78-
from torchao.quantization.pt2e.quantizer.quantizer import Q_ANNOTATION_KEY
7982

8083
quantization_config = get_ptq_per_channel_quant_config()
8184
for node in gm.graph.nodes:
@@ -92,7 +95,7 @@ def annotate_custom(gm: torch.fx.GraphModule) -> None:
9295
input_spec = quantization_config.input_activation
9396
input_qspec_map[input_act] = input_spec
9497

95-
node.meta[Q_ANNOTATION_KEY] = QuantizationAnnotation(
98+
node.meta[QUANT_ANNOTATION_KEY] = QuantizationAnnotation(
9699
input_qspec_map=input_qspec_map,
97100
output_qspec=quantization_config.output_activation,
98101
_annotated=True,
@@ -177,8 +180,7 @@ def main(args):
177180
# ensure the working directory exist.
178181
os.makedirs(args.artifact, exist_ok=True)
179182

180-
# quant_dtype: Literal[QuantDtype.use_16a16w] = QuantDtype.use_16a16w
181-
quant_dtype: Literal[QuantDtype.use_16a16w] = QuantDtype.use_8a8w
183+
quant_dtype = QuantDtype.use_8a8w
182184
if args.use_fp16:
183185
quant_dtype = None
184186

@@ -195,11 +197,9 @@ def main(args):
195197
soc_info.htp_info.htp_arch,
196198
args.build_op_package,
197199
)
198-
quantizer = None
199-
if not args.use_fp16:
200-
quantizer = make_quantizer(
201-
quant_dtype=quant_dtype, custom_annotations=(annotate_custom,)
202-
)
200+
quantizer = make_quantizer(
201+
quant_dtype=quant_dtype, custom_annotations=(annotate_custom,)
202+
)
203203

204204
build_executorch_binary(
205205
instance,
@@ -228,14 +228,13 @@ def main(args):
228228

229229
runner_cmd = " ".join(
230230
[
231-
f"export QNN_FARF_LEVEL=4 && export LD_LIBRARY_PATH={qnn_sdk}/lib/{target}/:{args.build_folder}/lib &&",
231+
f"export LD_LIBRARY_PATH={qnn_sdk}/lib/{target}/:{args.build_folder}/lib &&",
232232
f"./{args.build_folder}/examples/qualcomm/executor_runner/qnn_executor_runner",
233233
f"--model_path {args.artifact}/{pte_filename}.pte",
234234
f"--input_list_path {args.artifact}/{input_list_filename}",
235235
f"--output_folder_path {output_data_folder}",
236236
]
237237
)
238-
239238
subprocess.run(
240239
runner_cmd,
241240
# stdout=subprocess.PIPE,
@@ -259,7 +258,6 @@ def main(args):
259258
device_id=args.device,
260259
host_id=args.host,
261260
soc_model=args.model,
262-
shared_buffer=args.shared_buffer,
263261
)
264262
adb.push(inputs=sample_input, files=op_package_paths)
265263
adb.execute()

examples/qualcomm/custom_op/example_op_package_htp/ExampleOpPackage/Makefile

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,19 @@ $(info "HEXAGON_SDK_ROOT is [${HEXAGON_SDK_ROOT}]")
4444
HEXAGON_SDK_ROOT_V68 := $(HEXAGON_SDK_BASE)/hexagon-sdk-4.2.0
4545
HEXAGON_SDK_ROOT_V69 := $(HEXAGON_SDK_BASE)/hexagon-sdk-4.3.0
4646
HEXAGON_SDK_ROOT_V73 := $(HEXAGON_SDK_BASE)/hexagon-sdk-5.4.0
47-
# HEXAGON_SDK_ROOT_V75 := $(HEXAGON_SDK_BASE)/hexagon-sdk-5.4.0
48-
HEXAGON_SDK_ROOT_V75 := $(HEXAGON_SDK_BASE)
49-
# HEXAGON_SDK_ROOT_V79 := $(HEXAGON_SDK_BASE)/hexagon-sdk-6.0.0
50-
HEXAGON_SDK_ROOT_V79 := $(HEXAGON_SDK_BASE)
47+
HEXAGON_SDK_ROOT_V75 := $(HEXAGON_SDK_BASE)/hexagon-sdk-5.4.0
48+
HEXAGON_SDK_ROOT_V79 := $(HEXAGON_SDK_BASE)/hexagon-sdk-6.0.0
5149

5250
#Updated to point to latest sdk to match with libQnnHtp.so
53-
HEXAGON_SDK_ROOT_X86 := $(HEXAGON_SDK_BASE)
51+
HEXAGON_SDK_ROOT_X86 := $(HEXAGON_SDK_BASE)/hexagon-sdk-6.0.0
5452
HEXAGON_TOOLS_VERSION_V68 := 8.4.09
5553
HEXAGON_TOOLS_VERSION_V69 := 8.5.03
5654
HEXAGON_TOOLS_VERSION_V73 := 8.6.02
5755
HEXAGON_TOOLS_VERSION_V75 := 8.7.03
58-
# HEXAGON_TOOLS_VERSION_V79 := 8.8.02
59-
HEXAGON_TOOLS_VERSION_V79 := 8.8.06
56+
HEXAGON_TOOLS_VERSION_V79 := 8.8.02
6057

6158
#Updated to point to latest sdk to match with libQnnHtp.so
62-
# HEXAGON_TOOLS_VERSION_X86 := 8.8.02
63-
HEXAGON_TOOLS_VERSION_X86 := 8.8.06
59+
HEXAGON_TOOLS_VERSION_X86 := 8.8.02
6460

6561
ifndef ANDROID_NDK_ROOT
6662
ifeq ($(MAKECMDGOALS),htp_aarch64)
@@ -91,8 +87,6 @@ COMMON_CXX_FLAGS += -DQNN_API="__attribute__((visibility(\"default\")))" -D__QA
9187

9288
X86_LIBNATIVE_RELEASE_DIR := $(HEXAGON_SDK_ROOT_X86)/tools/HEXAGON_Tools/$(HEXAGON_TOOLS_VERSION_X86)/Tools
9389

94-
$(info "HEXAGON_SDK_ROOT_X86 is [${HEXAGON_SDK_ROOT_X86}]")
95-
9690
# Ensure hexagon sdk tool version can be retrieved
9791
ifeq ($(wildcard $(X86_LIBNATIVE_RELEASE_DIR)/.),)
9892
$(error "Cannot retrieve hexagon tools from: $(X86_LIBNATIVE_RELEASE_DIR). \
@@ -125,8 +119,6 @@ $(error "ERROR: HEXAGON_SDK_ROOT_V75 is set incorrectly. Cannot retrieve $(HEXAG
125119
endif
126120
endif
127121

128-
$(info "HEXAGON_SDK_ROOT_V79 is [${HEXAGON_SDK_ROOT_V79}]")
129-
130122
#Check tools for hexagon_v79 are present.
131123
ifeq ($(MAKECMDGOALS),htp_v79)
132124
ifeq ($(wildcard $(HEXAGON_SDK_ROOT_V79)),)
@@ -173,7 +165,6 @@ ifeq ($(shell $(X86_CXX) -v 2>&1 | grep -c "clang version"), 0)
173165
X86_CXX := clang++
174166
endif
175167
X86_LDFLAGS:= -Wl,--whole-archive -L$(X86_LIBNATIVE_RELEASE_DIR)/libnative/lib -lnative -Wl,--no-whole-archive -lpthread
176-
177168
X86_C_FLAGS := -D__HVXDBL__ -I$(X86_LIBNATIVE_RELEASE_DIR)/libnative/include -ffast-math -DUSE_OS_LINUX
178169
X86_CXX_FLAGS = $(COMMON_CXX_FLAGS) $(X86_C_FLAGS) -fomit-frame-pointer -Wno-invalid-offsetof
179170
linux_objs =
@@ -187,55 +178,12 @@ HEXAGON_CXX_FLAGS_V73 := $(HEXAGON_CXX_FLAGS) -mv73 -I$(HEXAGON_SDK_ROOT_V73)/rt
187178
HEXAGON_CXX_FLAGS_V75 := $(HEXAGON_CXX_FLAGS) -mv75 -I$(HEXAGON_SDK_ROOT_V75)/rtos/qurt/computev75/include/qurt -I$(HEXAGON_SDK_ROOT_V75)/rtos/qurt/computev75/include/posix -I$(HEXAGON_SDK_ROOT_V75)/incs -I$(HEXAGON_SDK_ROOT_V75)/incs/stddef
188179
HEXAGON_CXX_FLAGS_V79 := $(HEXAGON_CXX_FLAGS) -mv79 -I$(HEXAGON_SDK_ROOT_V79)/rtos/qurt/computev79/include/qurt -I$(HEXAGON_SDK_ROOT_V79)/rtos/qurt/computev79/include/posix -I$(HEXAGON_SDK_ROOT_V79)/incs -I$(HEXAGON_SDK_ROOT_V79)/incs/stddef
189180

190-
QHL_HVX_DIR := $(HEXAGON_SDK_ROOT)/libs/qhl_hvx
191-
QHL_HVX_INC_DIRS := \
192-
$(QHL_HVX_DIR)/inc/internal \
193-
$(QHL_HVX_DIR)/inc/qhdsp_hvx \
194-
$(QHL_HVX_DIR)/inc/qhblas_hvx \
195-
$(QHL_HVX_DIR)/inc/qhmath_hvx
196-
197-
HEXAGON_CXX_FLAGS_V79 += $(addprefix -I,$(QHL_HVX_INC_DIRS))
198-
199-
QHL_DIR := $(HEXAGON_SDK_ROOT)/libs/qhl
200-
QHL_INC_DIRS := \
201-
$(QHL_DIR)/inc/qhmath \
202-
$(QHL_DIR)/inc/qhcomplex \
203-
$(QHL_DIR)/inc/qhdsp \
204-
$(QHL_DIR)/inc/qhblas
205-
206-
QHL_LIBS := \
207-
$(HEXAGON_SDK_ROOT)/libs/qhl/prebuilt/hexagon_toolv88_v79/libqhblas.a \
208-
$(HEXAGON_SDK_ROOT)/libs/qhl/prebuilt/hexagon_toolv88_v79/libqhdsp.a \
209-
$(HEXAGON_SDK_ROOT)/libs/qhl/prebuilt/hexagon_toolv88_v79/libqhmath.a \
210-
$(HEXAGON_SDK_ROOT)/libs/qhl/prebuilt/hexagon_toolv88_v79/libqhcomplex.a \
211-
$(HEXAGON_SDK_ROOT)/libs/qhl_hvx/prebuilt/hexagon_toolv88_v79/libqhdsp_hvx.a \
212-
$(HEXAGON_SDK_ROOT)/libs/qhl_hvx/prebuilt/hexagon_toolv88_v79/libqhblas_hvx.a
213-
214-
HEXAGON_CXX_FLAGS_V79 += $(addprefix -I,$(QHL_INC_DIRS))
215-
216-
$(info "HEXAGON_TOOLS_VERSION_V68 is [${HEXAGON_TOOLS_VERSION_V68}]")
217-
$(info "HEXAGON_TOOLS_VERSION_V69 is [${HEXAGON_TOOLS_VERSION_V69}]")
218-
$(info "HEXAGON_TOOLS_VERSION_V73 is [${HEXAGON_TOOLS_VERSION_V73}]")
219-
$(info "HEXAGON_TOOLS_VERSION_V75 is [${HEXAGON_TOOLS_VERSION_V75}]")
220-
$(info "HEXAGON_TOOLS_VERSION_V79 is [${HEXAGON_TOOLS_VERSION_V79}]")
221-
222-
$(info "HEXAGON_SDK_ROOT_V68 is [${HEXAGON_SDK_ROOT_V68}]")
223-
$(info "HEXAGON_SDK_ROOT_V69 is [${HEXAGON_SDK_ROOT_V69}]")
224-
$(info "HEXAGON_SDK_ROOT_V73 is [${HEXAGON_SDK_ROOT_V73}]")
225-
$(info "HEXAGON_SDK_ROOT_V75 is [${HEXAGON_SDK_ROOT_V75}]")
226-
$(info "HEXAGON_SDK_ROOT_V79 is [${HEXAGON_SDK_ROOT_V79}]")
227-
228181
HEXAGON_CXX_V68 := $(HEXAGON_SDK_ROOT_V68)/tools/HEXAGON_Tools/$(HEXAGON_TOOLS_VERSION_V68)/Tools/bin/hexagon-clang++
229182
HEXAGON_CXX_V69 := $(HEXAGON_SDK_ROOT_V69)/tools/HEXAGON_Tools/$(HEXAGON_TOOLS_VERSION_V69)/Tools/bin/hexagon-clang++
230183
HEXAGON_CXX_V73 := $(HEXAGON_SDK_ROOT_V73)/tools/HEXAGON_Tools/$(HEXAGON_TOOLS_VERSION_V73)/Tools/bin/hexagon-clang++
231184
HEXAGON_CXX_V75 := $(HEXAGON_SDK_ROOT_V75)/tools/HEXAGON_Tools/$(HEXAGON_TOOLS_VERSION_V75)/Tools/bin/hexagon-clang++
232185
HEXAGON_CXX_V79 := $(HEXAGON_SDK_ROOT_V79)/tools/HEXAGON_Tools/$(HEXAGON_TOOLS_VERSION_V79)/Tools/bin/hexagon-clang++
233186

234-
$(info "HEXAGON_CXX_V68[2] is [${HEXAGON_CXX_V68}]")
235-
$(info "HEXAGON_CXX_V69[2] is [${HEXAGON_CXX_V69}]")
236-
$(info "HEXAGON_CXX_V73[2] is [${HEXAGON_CXX_V73}]")
237-
$(info "HEXAGON_CXX_V75[2] is [${HEXAGON_CXX_V75}]")
238-
$(info "HEXAGON_CXX_V79[2] is [${HEXAGON_CXX_V79}]")
239187

240188
HEX_LDFLAGS =
241189
hexagon_objs =
@@ -279,6 +227,7 @@ HEXAGON_BUILD_V75: $(WORK)/hexagon-v75/$(LIBRARY_NAME)
279227
HEXAGON_BUILD_V79: $(WORK)/hexagon-v79/$(LIBRARY_NAME)
280228

281229

230+
282231
X86_BUILD: $(WORK)/x86_64-linux-clang/$(LIBRARY_NAME)
283232

284233

@@ -391,11 +340,8 @@ $(WORK)/hexagon-v79/ops/%.o: $(OP_SRC_DIR)/%.cpp | $(WORK)/hexagon-v79
391340
$(WORK)/hexagon-v79/ops/%.o: $(OP_SRC_DIR)/v79_asm/%.S | $(WORK)/hexagon-v79
392341
$(HEXAGON_CXX_V79) $(HEXAGON_CXX_FLAGS_V79) -DTHIS_PKG_NAME=$(PACKAGE_NAME) -MMD -c $< -o $@
393342

394-
# $(WORK)/hexagon-v79/$(LIBRARY_NAME): $(hexagon-v79_objs) | $(HFILES)
395-
# $(HEXAGON_CXX_V79) -fPIC -std=c++17 -g -shared -o $@ $^ $(HEX_LDFLAGS)
396-
397-
$(WORK)/hexagon-v79/$(LIBRARY_NAME): $(hexagon-v79_objs)
398-
$(HEXAGON_CXX_V79) -fPIC -std=c++17 -g -shared -o $@ $^ $(QHL_LIBS) $(HEX_LDFLAGS)
343+
$(WORK)/hexagon-v79/$(LIBRARY_NAME): $(hexagon-v79_objs) | $(HFILES)
344+
$(HEXAGON_CXX_V79) -fPIC -std=c++17 -g -shared -o $@ $^ $(HEX_LDFLAGS)
399345

400346

401347

examples/qualcomm/custom_op/example_op_package_htp/ExampleOpPackage/src/ExampleOpPackageInterface.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@
1212
#include "QnnOpPackage.h"
1313
#include "QnnSdkBuildId.h"
1414

15-
#ifdef __hexagon__
16-
#include "qhblas_hvx.h" // may re-export symbols in qhblas
17-
#include "qhcomplex.h"
18-
#include "qhdsp_hvx.h" // still present under qhl/inc/qhdsp
19-
#endif
20-
2115
DEFINE_UNIQ_TY()
2216
BEGIN_PKG_OPS_OPTS_LIST()
2317

examples/qualcomm/custom_op/example_op_package_htp/ExampleOpPackage/src/ops/ExampleCustomOp.cpp

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@
33
//==============================================================================
44

55
#include "HTP/core/constraints.h"
6-
#include "HTP/core/intrinsics.h"
76
#include "HTP/core/op_package_feature_support.h"
87
#include "HTP/core/op_register_ext.h"
98
#include "HTP/core/optimize.h"
109
#include "HTP/core/simple_reg.h"
1110
#include "QnnOpPackage.h"
12-
#include "hexagon_protos.h"
13-
#include "hexagon_types.h"
14-
#include "hvx_hexagon_protos.h"
15-
1611
#ifdef __hexagon__
1712
#include "HAP_farf.h"
1813
#else /* __hexagon__ */
@@ -165,48 +160,21 @@ GraphStatus examplecustomopImpl(TensorType& out_0, const TensorType& in_0)
165160
if (input_intfc.dtype == DType::Float32) {
166161
const float* p_input = static_cast<const float*>(in_0.raw_data_const());
167162
float* p_output = static_cast<float*>(out_0.raw_data());
168-
const size_t N = in_0.total_storage_elements();
169-
170-
// Allocate temporary FP16 buffers on stack or heap
171-
std::vector<Float16> tmp_in(N);
172-
std::vector<Float16> tmp_out(N);
173-
174-
// 1. Convert FP32 -> FP16
175-
for (size_t i = 0; i < N; ++i) {
176-
tmp_in[i] = static_cast<Float16>(p_input[i]);
177-
}
178-
179-
#ifdef __hexagon__
180-
// 2. Run HVX multiply (FP16 domain)
181-
union {
182-
Float16 f16;
183-
uint16_t bits;
184-
} f3 = {static_cast<Float16>(3.0f)};
185-
HVX_Vector v_mul = Q6_Vh_vsplat_R(f3.bits);
186-
187-
const int vector_bytes = 128;
188-
const int elems_per_vec = vector_bytes / sizeof(Float16);
189-
190-
for (size_t i = 0; i < N; i += elems_per_vec) {
191-
HVX_Vector vin = q6op_V_vldu_A(&tmp_in[i]);
192-
HVX_Vector vout = Q6_Vhf_vmpy_VhfVhf(vin, v_mul);
193-
q6op_vstu_AV(&tmp_out[i], vout);
194-
}
195-
#else
196-
// 2. Fallback scalar multiply
197-
for (size_t i = 0; i < N; ++i) {
198-
tmp_out[i] = static_cast<Float16>(tmp_in[i] * static_cast<Float16>(3.0f));
199-
}
200-
#endif
163+
const int multiplier = 3;
164+
for (size_t i = 0; i < input_num_elements; ++i) {
165+
p_output[i] = multiplier * p_input[i];
201166

202-
// 3. Convert FP16 -> FP32
203-
for (size_t i = 0; i < N; ++i) {
204-
p_output[i] = static_cast<float>(tmp_out[i]);
167+
FARF(
168+
ALWAYS,
169+
"[QNN ExecuTorch Op Package test]"
170+
"input0[%zu]=%f, multiplier=%d, output[%zu]=%f",
171+
i,
172+
p_input[i],
173+
multiplier,
174+
i,
175+
p_output[i]);
205176
}
206-
207-
return GraphStatus::Success;
208177
} else if (input_intfc.dtype == DType::QUInt8) {
209-
// printf("[QNN ExecuTorch Op Package test] input is QUInt8\n");
210178
const uint8_t* p_input = static_cast<const uint8_t*>(in_0.raw_data_const());
211179
uint8_t* p_output = static_cast<uint8_t*>(out_0.raw_data());
212180
const int multiplier = 3 * input_intfc.scale / out_intfc.scale;

0 commit comments

Comments
 (0)