Skip to content

Commit c589d6c

Browse files
[executorch][native] Let quantized weights feed ops directly (pytorch#23336)
This PR was created by the merge bot to help merge the original PR into the main branch. ghstack PR number: pytorch#23231 by @digantdesai ^ Please use this as the source of truth for the PR details, comments, and reviews ghstack PR base: https://github.com/pytorch/executorch/tree/gh/digantdesai/111/base ghstack PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/111/head Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/digantdesai/110/orig Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/111/orig Differential Revision: [D121704320](https://our.internmc.facebook.com/intern/diff/D121704320/) @diff-train-skip-merge Co-authored-by: Digant Desai <digantdesai@meta.com>
1 parent 5b1be39 commit c589d6c

4 files changed

Lines changed: 20 additions & 4 deletions

File tree

‎backends/native/runtime/graph/TensorMeta.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace ptn {
1919

20+
// See native_graph.fbs AffineGroup. As a direct op operand, a tensor carrying
21+
// this scheme denotes its decoded value, with dtype scale_dtype.
2022
struct AffineGroupQuant {
2123
std::string scale_data_key;
2224
ScalarType scale_dtype = ScalarType::Float;

‎backends/native/runtime/graph/Value.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
namespace ptn {
1212

13+
TensorMeta& Value::tensor_meta() {
14+
TensorMeta* m = std::get_if<TensorMeta>(&value_);
15+
if (m == nullptr) {
16+
throw std::runtime_error("Value is not a tensor");
17+
}
18+
return *m;
19+
}
20+
1321
const TensorMeta& Value::tensor_meta() const {
1422
const TensorMeta* m = std::get_if<TensorMeta>(&value_);
1523
if (m == nullptr) {

‎backends/native/runtime/graph/Value.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class Value {
111111
}
112112

113113
// Typed payload accessors: throw std::runtime_error unless the kind matches.
114+
TensorMeta& tensor_meta();
114115
const TensorMeta& tensor_meta() const;
115116
const Scalar& scalar() const;
116117
const std::vector<ValueId>& content_ids() const;

‎backends/native/serialization/native_graph.fbs‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,17 @@ enum OutputValueKind : byte {
116116
// axis). Scales (and, if asymmetric, zero-points) are shipped out-of-line via the
117117
// external constant file, keyed by data_key (empty zero_point_data_key means symmetric).
118118
// Their shapes derive from the weight shape and group_size, but their dtypes do not,
119-
// so they are recorded here. The dequant result dtype is the op output value's
120-
// TensorMeta.dtype, not part of this scheme. The qdata itself (the enclosing
121-
// TensorMeta) uses dtype=BYTE with logical sizes; its packed byte length follows from
122-
// the logical shape and bitwidth, not prod(sizes). The scale and zero-point keys must
119+
// so they are recorded here. The qdata itself (the enclosing TensorMeta) uses
120+
// dtype=BYTE with logical sizes. Sub-byte packing is flat over the elements in
121+
// memory order with no per-row padding, so the packed byte length is
122+
// ceil(numel * bitwidth / 8), not prod(sizes). The scale and zero-point keys must
123123
// be read-only stored constants of every method that uses the quantized tensor,
124124
// whether that tensor is itself a constant or a graph value such as a user input.
125+
//
126+
// A quantized tensor may feed an explicit dequant op, whose output TensorMeta.dtype
127+
// gives the result dtype, or it may be a direct op operand. A direct operand denotes
128+
// its decoded value with dtype scale_dtype; an engine must lower the consuming op,
129+
// materialize the decode, or reject the program.
125130
table AffineGroup {
126131
scale_data_key: string (required);
127132
scale_dtype: ScalarType; // FLOAT, HALF, or BFLOAT16; not derivable

0 commit comments

Comments
 (0)