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
use crate::internal::*; use ndarray::*; #[derive(Debug, Clone, new, Hash)] pub struct ScatterNd; impl_dyn_hash!(ScatterNd); impl Op for ScatterNd { fn name(&self) -> Cow<str> { "ScatterNd".into() } op_core_mir!(); op_as_typed_op!(); } impl ScatterNd { unsafe fn eval_t<T: Datum>( &self, data: Arc<Tensor>, indices: &ArrayViewD<i64>, updates: Arc<Tensor>, ) -> TractResult<Arc<Tensor>> { let mut data = data.into_tensor().into_array_unchecked::<T>(); let updates_view = updates.to_array_view_unchecked::<T>(); for coords in tract_ndarray::indices(&indices.shape()[..indices.ndim() - 1]) { let mut indices_into_data = indices.view(); let mut updates = updates_view.view(); for x in coords.slice() { indices_into_data.index_axis_inplace(Axis(0), *x); updates.index_axis_inplace(Axis(0), *x); } let mut data = data.view_mut(); for x in indices_into_data { data.index_axis_inplace(Axis(0), *x as usize); } data.assign(&updates) } let mut tensor = data.into_tensor(); tensor.set_datum_type(updates.datum_type()); Ok(tensor.into_arc_tensor()) } } impl TypedOp for ScatterNd { as_op!(); fn output_facts(&self, inputs: &[&TypedFact]) -> TractResult<TVec<TypedFact>> { Ok(tvec!(TypedFact::dt_shape(inputs[0].datum_type, inputs[0].shape.to_tvec()))) } } impl EvalOp for ScatterNd { fn is_stateless(&self) -> bool { true } fn eval(&self, mut inputs: TVec<Arc<Tensor>>) -> TractResult<TVec<Arc<Tensor>>> { let (data, indices, updates) = args_3!(inputs); let indices = indices.cast_to::<i64>()?; let indices = indices.to_array_view::<i64>()?; if data.datum_type() != updates.datum_type() { bail!( "Data and update must be of the same type, got {:?} and {:?}", data.datum_type(), updates.datum_type() ); } unsafe { Ok(tvec!(dispatch_datum_by_size!(Self::eval_t(data.datum_type())( &self, data, &indices, updates ))?)) } } }