use super::prelude::*;

impl NodeCodegen for onnx_ir::transpose::TransposeNode {
    fn inputs(&self) -> &[Argument] {
        &self.inputs
    }

    fn outputs(&self) -> &[Argument] {
        &self.outputs
    }

    fn forward(&self, scope: &mut ScopeAtPosition<'_>) -> TokenStream {
        let input = scope.arg(self.inputs.first().unwrap());
        let output = arg_to_ident(self.outputs.first().unwrap());
        let perm = self.config.perm.to_tokens();

        quote! {
            let #output = #input.permute(#perm);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::super::test_helpers::*;
    use burn::tensor::DType;
    use insta::assert_snapshot;
    use onnx_ir::transpose::{TransposeConfig, TransposeNodeBuilder};

    #[test]
    fn test_transpose_forward() {
        let config = TransposeConfig::new(vec![0, 2, 3, 1]);
        let node = TransposeNodeBuilder::new("transpose1")
            .input_tensor("input", 4, DType::F32)
            .output_tensor("output", 4, DType::F32)
            .config(config)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(&self, input: Tensor<B, 4>) -> Tensor<B, 4> {
            let output = input.permute([0, 2, 3, 1]);
            output
        }
        ");
    }
}
