burn_fusion/ops/
qtensor.rs

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
use std::{marker::PhantomData, ops::Range};

use burn_tensor::{
    ops::{FloatElem, FloatTensor, IntTensor, QTensorOps, QuantizedTensor},
    quantization::{QuantizationParametersPrimitive, QuantizationScheme, QuantizationStrategy},
    repr::{
        DequantizeOperationDescription, FloatOperationDescription, HandleContainer,
        OperationDescription, QuantizationParametersDescription, QuantizeOperationDescription,
        QuantizedKind,
    },
    DType, Device, Element, Shape, TensorData,
};

use crate::{
    client::FusionClient,
    get_client,
    stream::{execution::Operation, StreamId},
    Fusion, FusionBackend, FusionQuantizationParameters, QFusionTensor,
};

impl<B: FusionBackend> QTensorOps<Self> for Fusion<B> {
    fn q_from_data(data: TensorData, device: &Device<Self>) -> QuantizedTensor<Self> {
        match data.dtype {
            DType::QFloat(strategy) => {
                let client = get_client::<B>(device);
                let tensor = B::q_from_data(data, device);
                let shape = B::q_shape(&tensor);

                let handles = B::quantized_tensor_handle(tensor);
                let qparams = match strategy {
                    QuantizationStrategy::PerTensorAffineInt8(_) => {
                        let offset = if let Some(offset) = handles.offset {
                            offset
                        } else {
                            panic!("Expected offset for quantized tensor.");
                        };
                        FusionQuantizationParameters {
                            scale: client.register_tensor(
                                handles.scale,
                                vec![1],
                                StreamId::current(),
                                B::FloatElem::dtype(),
                            ),
                            offset: Some(client.register_tensor(
                                offset,
                                vec![1],
                                StreamId::current(),
                                B::IntElem::dtype(),
                            )),
                        }
                    }
                    QuantizationStrategy::PerTensorSymmetricInt8(_) => {
                        assert!(
                            handles.offset.is_none(),
                            "Offset should not be provided for symmetric quantization."
                        );
                        FusionQuantizationParameters {
                            scale: client.register_tensor(
                                handles.scale,
                                vec![1],
                                StreamId::current(),
                                B::FloatElem::dtype(),
                            ),
                            offset: None,
                        }
                    }
                };
                let qtensor = client.register_tensor(
                    handles.tensor,
                    shape.dims,
                    StreamId::current(),
                    B::QuantizedEncoding::dtype(),
                );
                QFusionTensor {
                    qtensor,
                    qparams,
                    scheme: strategy.scheme(),
                }
            }
            _ => panic!(
                "Invalid dtype (expected DType::QFloat, got {:?})",
                data.dtype
            ),
        }
    }

    fn quantize(
        tensor: FloatTensor<Self>,
        scheme: &QuantizationScheme,
        qparams: QuantizationParametersPrimitive<Self>,
    ) -> QuantizedTensor<Self> {
        #[derive(new)]
        struct QuantizeOp<B: FusionBackend> {
            desc: QuantizeOperationDescription,
            _b: PhantomData<B>,
        }

        impl<B: FusionBackend> Operation<B::FusionRuntime> for QuantizeOp<B> {
            fn execute(self: Box<Self>, handles: &mut HandleContainer<B::Handle>) {
                let tensor = handles.get_float_tensor::<B>(&self.desc.tensor);
                let scale = handles.get_float_tensor::<B>(&self.desc.qparams.scale);
                let offset = self
                    .desc
                    .qparams
                    .offset
                    .as_ref()
                    .map(|x| handles.get_int_tensor::<B>(x));

                let qparams = QuantizationParametersPrimitive { scale, offset };
                let output = B::quantize(tensor, &self.desc.scheme, qparams);
                let q_ids = if let Some(offset) = &self.desc.qparams.offset {
                    QuantizedKind {
                        tensor: self.desc.out.id,
                        scale: self.desc.qparams.scale.id,
                        offset: Some(offset.id),
                    }
                } else {
                    QuantizedKind {
                        tensor: self.desc.out.id,
                        scale: self.desc.qparams.scale.id,
                        offset: None,
                    }
                };
                handles.register_quantized_tensor::<B>(&q_ids, output);
            }
        }

        let shape: Vec<usize> = tensor.shape.clone();
        let out = tensor
            .client
            .tensor_uninitialized(shape, B::QuantizedEncoding::dtype());

        let streams = if let Some(offset) = &qparams.offset {
            vec![tensor.stream, qparams.scale.stream, offset.stream]
        } else {
            vec![tensor.stream, qparams.scale.stream]
        };

        let desc = QuantizeOperationDescription {
            tensor: tensor.into_description(),
            qparams: QuantizationParametersDescription {
                scale: qparams.scale.clone().into_description(),
                offset: qparams.offset.clone().map(|x| x.into_description()),
            },
            scheme: scheme.clone(),
            out: out.to_description_out(),
        };

        out.client.register(
            streams,
            OperationDescription::Float(
                FloatElem::<Self>::dtype(),
                FloatOperationDescription::Quantize(desc.clone()),
            ),
            QuantizeOp::<B>::new(desc),
        );

        QFusionTensor {
            qtensor: out,
            scheme: scheme.clone(),
            qparams: qparams.into(),
        }
    }

    fn dequantize(tensor: QuantizedTensor<Self>) -> FloatTensor<Self> {
        #[derive(new)]
        struct DequantizeOp<B: FusionBackend> {
            desc: DequantizeOperationDescription,
            _b: PhantomData<B>,
        }

        impl<B: FusionBackend> Operation<B::FusionRuntime> for DequantizeOp<B> {
            fn execute(self: Box<Self>, handles: &mut HandleContainer<B::Handle>) {
                let tensor = handles.get_quantized_tensor::<B>(&self.desc.qtensor);

                let output = B::dequantize(tensor);
                handles.register_float_tensor::<B>(&self.desc.out.id, output);
            }
        }

        let shape: Vec<usize> = tensor.qtensor.shape.clone();
        let out = tensor
            .qtensor
            .client
            .tensor_uninitialized(shape, B::FloatElem::dtype());

        let streams = if let Some(offset) = &tensor.qparams.offset {
            vec![
                tensor.qtensor.stream,
                tensor.qparams.scale.stream,
                offset.stream,
            ]
        } else {
            vec![tensor.qtensor.stream, tensor.qparams.scale.stream]
        };

        let desc = DequantizeOperationDescription {
            qtensor: tensor.into_description(),
            out: out.to_description_out(),
        };

        out.client.register(
            streams,
            OperationDescription::Float(
                FloatElem::<Self>::dtype(),
                FloatOperationDescription::Dequantize(desc.clone()),
            ),
            DequantizeOp::<B>::new(desc),
        );

        out
    }

    fn q_shape(tensor: &QuantizedTensor<Self>) -> Shape {
        tensor.qtensor.shape()
    }

    fn q_device(tensor: &QuantizedTensor<Self>) -> Device<Self> {
        tensor.qtensor.client.device().clone()
    }

    fn q_to_device(tensor: QuantizedTensor<Self>, device: &Device<Self>) -> QuantizedTensor<Self> {
        // Quantization parameters are on the same device as the qtensor
        let device_original: &B::Device = tensor.qtensor.client.device();
        let device_target: B::Device = device.clone();

        if device_original == &device_target {
            return tensor;
        }
        println!("q_to_device {:?} {:?}", device_original, device_target);

        let client_target = get_client::<B>(&device_target);
        let client_original = tensor.qtensor.client.clone();

        let ids = if let Some(offset) = &tensor.qparams.offset {
            vec![
                tensor.qtensor.stream,
                tensor.qparams.scale.stream,
                offset.stream,
            ]
        } else {
            vec![tensor.qtensor.stream, tensor.qparams.scale.stream]
        };

        client_original.change_client_quantized::<B>(tensor.into_description(), client_target, ids)
    }

    fn q_reshape(_tensor: QuantizedTensor<Self>, _shape: Shape) -> QuantizedTensor<Self> {
        unimplemented!()
    }

    async fn q_into_data(tensor: QuantizedTensor<Self>) -> TensorData {
        tensor.into_data::<B>().await
    }

    fn q_swap_dims(
        _tensor: QuantizedTensor<Self>,
        _dim1: usize,
        _dim2: usize,
    ) -> QuantizedTensor<Self> {
        unimplemented!()
    }

    fn q_permute(_tensor: QuantizedTensor<Self>, _axes: &[usize]) -> QuantizedTensor<Self> {
        unimplemented!()
    }

    fn q_flip(_tensor: QuantizedTensor<Self>, _axes: &[usize]) -> QuantizedTensor<Self> {
        unimplemented!()
    }

    fn q_gather(
        _dim: usize,
        _tensor: QuantizedTensor<Self>,
        _indices: IntTensor<Self>,
    ) -> QuantizedTensor<Self> {
        unimplemented!()
    }

    fn q_select(
        _tensor: QuantizedTensor<Self>,
        _dim: usize,
        _indices: IntTensor<Self>,
    ) -> QuantizedTensor<Self> {
        unimplemented!()
    }

    fn q_slice(_tensor: QuantizedTensor<Self>, _ranges: &[Range<usize>]) -> QuantizedTensor<Self> {
        unimplemented!()
    }

    fn q_expand(_tensor: QuantizedTensor<Self>, _shape: Shape) -> QuantizedTensor<Self> {
        unimplemented!()
    }
}