use super::prelude::*;
impl NodeCodegen for onnx_ir::node::bitwiseand::BitwiseAndNode {
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_and(#rhs_bc) }
}
(lhs_ty, ArgType::ScalarNative(_)) if lhs_ty.is_on_device() => {
quote! { #lhs_value.bitwise_and_scalar((#rhs_value as i64).elem()) }
}
(ArgType::ScalarNative(_), rhs_ty) if rhs_ty.is_on_device() => {
quote! { #rhs_value.bitwise_and_scalar((#lhs_value as i64).elem()) }
}
(ArgType::ScalarNative(_), ArgType::ScalarNative(_)) => {
quote! { #lhs_value & #rhs_value }
}
_ => panic!("BitwiseAnd 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::bitwiseand::BitwiseAndNodeBuilder;
#[test]
fn test_bitwiseand_tensor() {
let node = BitwiseAndNodeBuilder::new("and1")
.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_and(rhs);
output
}
");
}
#[test]
fn test_bitwiseand_scalar() {
let node = BitwiseAndNodeBuilder::new("and2")
.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_and_scalar((rhs as i64).elem());
output
}
");
}
#[test]
fn test_bitwiseand_scalar_tensor() {
let node = BitwiseAndNodeBuilder::new("and3")
.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_and_scalar((lhs as i64).elem());
output
}
");
}
#[test]
fn test_bitwiseand_scalar_scalar() {
let node = BitwiseAndNodeBuilder::new("and4")
.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_bitwiseand_broadcast_lhs_higher() {
let node = BitwiseAndNodeBuilder::new("and5")
.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_and((rhs).unsqueeze_dims(&[0isize]));
output
}
");
}
#[test]
fn test_bitwiseand_broadcast_rhs_higher() {
let node = BitwiseAndNodeBuilder::new("and6")
.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_and(rhs);
output
}
");
}
}