burn-onnx 0.21.0-pre.3

Library for importing ONNX models into the Burn framework
Documentation
use super::prelude::*;

impl NodeCodegen for onnx_ir::node::bitwiseor::BitwiseOrNode {
    fn inputs(&self) -> &[Argument] {
        &self.inputs
    }

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

    fn forward(&self, scope: &mut ScopeAtPosition<'_>) -> TokenStream {
        let lhs = self.inputs.first().unwrap();
        let rhs = self.inputs.get(1).unwrap();
        let output = arg_to_ident(self.outputs.first().unwrap());

        let lhs_value = scope.arg(lhs);

        let rhs_value = scope.arg(rhs);

        let function = match (&lhs.ty, &rhs.ty) {
            (lhs_ty, rhs_ty) if lhs_ty.is_on_device() && rhs_ty.is_on_device() => {
                let lhs_rank = lhs_ty.rank();
                let rhs_rank = rhs_ty.rank();
                let lhs_bc =
                    broadcast_helpers::leading_broadcast(quote! { #lhs_value }, lhs_rank, rhs_rank);
                let rhs_bc =
                    broadcast_helpers::leading_broadcast(quote! { #rhs_value }, rhs_rank, lhs_rank);
                quote! { #lhs_bc.bitwise_or(#rhs_bc) }
            }
            (lhs_ty, ArgType::ScalarNative(_)) if lhs_ty.is_on_device() => {
                quote! { #lhs_value.bitwise_or_scalar((#rhs_value as i64).elem()) }
            }
            (ArgType::ScalarNative(_), rhs_ty) if rhs_ty.is_on_device() => {
                quote! { #rhs_value.bitwise_or_scalar((#lhs_value as i64).elem()) }
            }
            (ArgType::ScalarNative(_), ArgType::ScalarNative(_)) => {
                quote! { #lhs_value | #rhs_value }
            }
            _ => panic!("BitwiseOr operation requires tensor or scalar inputs"),
        };

        quote! {
            let #output = #function;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::super::test_helpers::*;
    use burn::tensor::DType;
    use insta::assert_snapshot;
    use onnx_ir::node::bitwiseor::BitwiseOrNodeBuilder;

    #[test]
    fn test_bitwiseor_tensor() {
        let node = BitwiseOrNodeBuilder::new("or1")
            .input_tensor("lhs", 2, DType::I32)
            .input_tensor("rhs", 2, DType::I32)
            .output_tensor("output", 2, DType::I32)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(
            &self,
            lhs: Tensor<B, 2, Int>,
            rhs: Tensor<B, 2, Int>,
        ) -> Tensor<B, 2, Int> {
            let output = lhs.bitwise_or(rhs);
            output
        }
        ");
    }

    #[test]
    fn test_bitwiseor_scalar() {
        let node = BitwiseOrNodeBuilder::new("or2")
            .input_tensor("lhs", 2, DType::I32)
            .input_scalar("rhs", DType::I32)
            .output_tensor("output", 2, DType::I32)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(&self, lhs: Tensor<B, 2, Int>, rhs: i32) -> Tensor<B, 2, Int> {
            let output = lhs.bitwise_or_scalar((rhs as i64).elem());
            output
        }
        ");
    }

    #[test]
    fn test_bitwiseor_scalar_tensor() {
        let node = BitwiseOrNodeBuilder::new("or3")
            .input_scalar("lhs", DType::I32)
            .input_tensor("rhs", 2, DType::I32)
            .output_tensor("output", 2, DType::I32)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(&self, lhs: i32, rhs: Tensor<B, 2, Int>) -> Tensor<B, 2, Int> {
            let output = rhs.bitwise_or_scalar((lhs as i64).elem());
            output
        }
        ");
    }

    #[test]
    fn test_bitwiseor_scalar_scalar() {
        let node = BitwiseOrNodeBuilder::new("or4")
            .input_scalar("lhs", DType::I32)
            .input_scalar("rhs", DType::I32)
            .output_scalar("output", DType::I32)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(&self, lhs: i32, rhs: i32) -> i32 {
            let output = lhs | rhs;
            output
        }
        ");
    }

    #[test]
    fn test_bitwiseor_broadcast_lhs_higher() {
        let node = BitwiseOrNodeBuilder::new("or5")
            .input_tensor("lhs", 3, DType::I32)
            .input_tensor("rhs", 2, DType::I32)
            .output_tensor("output", 3, DType::I32)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(
            &self,
            lhs: Tensor<B, 3, Int>,
            rhs: Tensor<B, 2, Int>,
        ) -> Tensor<B, 3, Int> {
            let output = lhs.bitwise_or((rhs).unsqueeze_dims(&[0isize]));
            output
        }
        ");
    }

    #[test]
    fn test_bitwiseor_broadcast_rhs_higher() {
        let node = BitwiseOrNodeBuilder::new("or6")
            .input_tensor("lhs", 2, DType::I32)
            .input_tensor("rhs", 3, DType::I32)
            .output_tensor("output", 3, DType::I32)
            .build();
        let code = codegen_forward_default(&node);
        assert_snapshot!(code, @r"
        pub fn forward(
            &self,
            lhs: Tensor<B, 2, Int>,
            rhs: Tensor<B, 3, Int>,
        ) -> Tensor<B, 3, Int> {
            let output = (lhs).unsqueeze_dims(&[0isize]).bitwise_or(rhs);
            output
        }
        ");
    }
}