Skip to content

Commit

Permalink
fix for mobilent ptq
Browse files Browse the repository at this point in the history
  • Loading branch information
kali committed Aug 11, 2023
1 parent 119a611 commit 0ee4781
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
5 changes: 3 additions & 2 deletions core/src/ops/cnn/conv/unary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,9 @@ impl ConvUnary {
let mut new = self.clone();
new.pool_spec.padding = padding;
let mut patch = TypedModelPatch::default();
let wire = patch.tap_model(model, prec.inputs[0])?;
let wire = patch.wire_node(&node.name, new, &[wire])?;
let mut wire = patch.taps(model, &node.inputs)?;
wire[0] = patch.tap_model(model, prec.inputs[0])?;
let wire = patch.wire_node(&node.name, new, &wire)?;
patch.shunt_outside(model, node.id.into(), wire[0])?;
Ok(Some(patch))
}
Expand Down
4 changes: 2 additions & 2 deletions tflite/src/ops/cnn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn de_conv2d(op: &mut DeserOp) -> TractResult<TVec<OutletId>> {
output_channel_override: Some(*co),
};
let mut inputs = tvec!(op.inputs[0]);
let q_params = super::linearops_quantization_suport(op, &input, &mut inputs)?;
let q_params = super::linearops_quantization_suport(op, &input, &mut inputs, true)?;
let bias_dt = bias.datum_type().unquantized();
let bias = bias.into_tensor().cast_to_dt(bias_dt)?.into_owned().into_arc_tensor();
let conv = core::cnn::ConvUnary {
Expand Down Expand Up @@ -213,7 +213,7 @@ fn de_dw_conv2d(op: &mut DeserOp) -> TractResult<TVec<OutletId>> {
output_channel_override: Some(co),
};
let mut inputs = tvec!(op.inputs[0]);
let q_params = super::linearops_quantization_suport(op, &input, &mut inputs)?;
let q_params = super::linearops_quantization_suport(op, &input, &mut inputs, true)?;
let conv = core::cnn::ConvUnary {
pool_spec,
kernel_fmt: KernelFormat::OHWI,
Expand Down
9 changes: 7 additions & 2 deletions tflite/src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ fn linearops_quantization_suport(
op: &mut DeserOp,
input: &TypedFact,
inputs: &mut TVec<OutletId>,
kscale_is_per_axis: bool,
) -> TractResult<Option<DatumType>> {
if op.output_facts[0].datum_type.is_quantized() {
let p = &op.prefix;
Expand All @@ -63,11 +64,15 @@ fn linearops_quantization_suport(
let k_input = op.flat.inputs().unwrap().get(1);
let k_tensor = op.ctx.subgraph.tensors().unwrap().get(k_input as usize);
let k_qp = k_tensor.quantization().unwrap();
let kscale = k_qp.scale().unwrap().iter().collect_vec();
let k_scale = if kscale_is_per_axis {
rctensor1(&k_qp.scale().unwrap().iter().collect_vec())
} else {
rctensor0(k_qp.scale().unwrap().get(0))
};
let k_zp = k_qp.zero_point().unwrap().iter().map(|i| i as i32).collect_vec();
ensure!(k_zp.iter().all(|x| *x == 0));
inputs.push(op.ctx.target.add_const(format!("{p}.k0"), rctensor0(0i8))?);
inputs.push(op.ctx.target.add_const(format!("{p}.kscale"), rctensor1(&kscale))?);
inputs.push(op.ctx.target.add_const(format!("{p}.kscale"), k_scale)?);
inputs.push(op.ctx.target.add_const(format!("{p}.i0"), rctensor0(iqp.zp_scale().0 as i8))?);
inputs.push(op.ctx.target.add_const(format!("{p}.iscale"), rctensor0(iqp.zp_scale().1))?);
inputs.push(op.ctx.target.add_const(format!("{p}.c0"), rctensor0(oqp.zp_scale().0 as i8))?);
Expand Down
1 change: 1 addition & 0 deletions tflite/src/ops/nn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fn de_fully_connected(op: &mut DeserOp) -> TractResult<TVec<OutletId>> {
op,
&input,
&mut inputs,
false,
)?;
let operating_dt =
if input.datum_type.is_float() { input.datum_type } else { i32::datum_type() };
Expand Down

0 comments on commit 0ee4781

Please sign in to comment.