Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bitwise-ops-for-tensors #2498

Merged
merged 18 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions crates/burn-autodiff/src/ops/int_tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,32 @@ impl<B: Backend, C: CheckpointStrategy> IntTensorOps<Self> for Autodiff<B, C> {
fn int_argsort(tensor: IntTensor<Self>, dim: usize, descending: bool) -> IntTensor<Self> {
B::int_argsort(tensor, dim, descending)
}

fn bitwise_and(lhs: IntTensor<Self>, rhs: IntTensor<Self>) -> IntTensor<Self> {
B::bitwise_and(lhs, rhs)
}

fn bitwise_and_scalar(lhs: IntTensor<Self>, rhs: B::IntElem) -> IntTensor<Self> {
B::bitwise_and_scalar(lhs, rhs)
}

fn bitwise_or(lhs: IntTensor<Self>, rhs: IntTensor<Self>) -> IntTensor<Self> {
B::bitwise_or(lhs, rhs)
}

fn bitwise_or_scalar(lhs: IntTensor<Self>, rhs: B::IntElem) -> IntTensor<Self> {
B::bitwise_or_scalar(lhs, rhs)
}

fn bitwise_xor(lhs: IntTensor<Self>, rhs: IntTensor<Self>) -> IntTensor<Self> {
B::bitwise_xor(lhs, rhs)
}

fn bitwise_xor_scalar(lhs: IntTensor<Self>, rhs: B::IntElem) -> IntTensor<Self> {
B::bitwise_xor_scalar(lhs, rhs)
}

fn bitwise_not(tensor: IntTensor<Self>) -> IntTensor<Self> {
B::bitwise_not(tensor)
}
}
27 changes: 27 additions & 0 deletions crates/burn-candle/src/ops/int_tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,31 @@ impl<F: FloatCandleElement, I: IntCandleElement> IntTensorOps<Self> for Candle<F
fn int_sign(tensor: IntTensor<Self>) -> IntTensor<Self> {
sign(tensor)
}
fn bitwise_and(lhs: IntTensor<Self>, rhs: IntTensor<Self>) -> IntTensor<Self> {
unimplemented!("bitwise_and is not implemented for Candle IntTensor");
}

fn bitwise_and_scalar(lhs: IntTensor<Self>, rhs: IntElem<Self>) -> IntTensor<Self> {
unimplemented!("bitwise_and_scalar is not implemented for Candle IntTensor");
}

fn bitwise_or(lhs: IntTensor<Self>, rhs: IntTensor<Self>) -> IntTensor<Self> {
unimplemented!("bitwise_or is not implemented for Candle IntTensor");
}

fn bitwise_or_scalar(lhs: IntTensor<Self>, rhs: IntElem<Self>) -> IntTensor<Self> {
unimplemented!("bitwise_or_scalar is not implemented for Candle IntTensor");
}

fn bitwise_xor(lhs: IntTensor<Self>, rhs: IntTensor<Self>) -> IntTensor<Self> {
unimplemented!("bitwise_xor is not implemented for Candle IntTensor");
}

fn bitwise_xor_scalar(lhs: IntTensor<Self>, rhs: IntElem<Self>) -> IntTensor<Self> {
unimplemented!("bitwise_xor_scalar is not implemented for Candle IntTensor");
}

fn bitwise_not(tensor: IntTensor<Self>) -> IntTensor<Self> {
unimplemented!("bitwise_not is not implemented for Candle IntTensor");
}
}
185 changes: 184 additions & 1 deletion crates/burn-fusion/src/ops/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1394,7 +1394,10 @@ impl<B: FusionBackend> IntTensorOps<Self> for Fusion<B> {
};
out.client.register(
vec![stream],
OperationDescription::Int(repr::IntOperationDescription::IntoFloat(desc.clone())),
OperationDescription::Int(
IntElem::<Self>::dtype(),
repr::IntOperationDescription::IntoFloat(desc.clone()),
),
IntoFloatOps::<B>::new(desc),
);

Expand Down Expand Up @@ -1819,4 +1822,184 @@ impl<B: FusionBackend> IntTensorOps<Self> for Fusion<B> {

out
}

fn bitwise_and(lhs: IntTensor<Self>, rhs: IntTensor<Self>) -> IntTensor<Self> {
binary_int_ops!(BitwiseAndOps, B::bitwise_and);

let stream_1 = lhs.stream;
let stream_2 = rhs.stream;
let out = lhs.client.tensor_uninitialized(
binary_ops_shape(&lhs.shape, &rhs.shape),
B::IntElem::dtype(),
);

let desc = BinaryOperationDescription {
lhs: lhs.into_description(),
rhs: rhs.into_description(),
out: out.to_description_out(),
};
out.client.register(
vec![stream_1, stream_2],
repr::OperationDescription::Int(
IntElem::<Self>::dtype(),
IntOperationDescription::BitwiseAnd(desc.clone()),
),
BitwiseAndOps::<B>::new(desc),
);

out
}

fn bitwise_and_scalar(lhs: IntTensor<Self>, rhs: IntElem<Self>) -> IntTensor<Self> {
scalar_int_ops!(BitwiseAndOps, B::bitwise_and_scalar);

let stream = lhs.stream;
let out = lhs
.client
.tensor_uninitialized(lhs.shape.clone(), B::IntElem::dtype());

let desc = ScalarOperationDescription {
lhs: lhs.into_description(),
rhs: rhs.elem(),
out: out.to_description_out(),
};
out.client.register(
vec![stream],
repr::OperationDescription::Int(
IntElem::<Self>::dtype(),
IntOperationDescription::BitwiseAndScalar(desc.clone()),
),
BitwiseAndOps::<B>::new(desc),
);

out
}

fn bitwise_or(lhs: IntTensor<Self>, rhs: IntTensor<Self>) -> IntTensor<Self> {
binary_int_ops!(BitwiseOrOps, B::bitwise_or);

let stream_1 = lhs.stream;
let stream_2 = rhs.stream;
let out = lhs.client.tensor_uninitialized(
binary_ops_shape(&lhs.shape, &rhs.shape),
B::IntElem::dtype(),
);

let desc = BinaryOperationDescription {
lhs: lhs.into_description(),
rhs: rhs.into_description(),
out: out.to_description_out(),
};
out.client.register(
vec![stream_1, stream_2],
repr::OperationDescription::Int(
IntElem::<Self>::dtype(),
IntOperationDescription::BitwiseOr(desc.clone()),
),
BitwiseOrOps::<B>::new(desc),
);

out
}

fn bitwise_or_scalar(lhs: IntTensor<Self>, rhs: IntElem<Self>) -> IntTensor<Self> {
scalar_int_ops!(BitwiseOrOps, B::bitwise_or_scalar);

let stream = lhs.stream;
let out = lhs
.client
.tensor_uninitialized(lhs.shape.clone(), B::IntElem::dtype());

let desc = ScalarOperationDescription {
lhs: lhs.into_description(),
rhs: rhs.elem(),
out: out.to_description_out(),
};
out.client.register(
vec![stream],
repr::OperationDescription::Int(
IntElem::<Self>::dtype(),
IntOperationDescription::BitwiseOrScalar(desc.clone()),
),
BitwiseOrOps::<B>::new(desc),
);

out
}

fn bitwise_xor(lhs: IntTensor<Self>, rhs: IntTensor<Self>) -> IntTensor<Self> {
binary_int_ops!(BitwiseXorOps, B::bitwise_xor);

let stream_1 = lhs.stream;
let stream_2 = rhs.stream;
let out = lhs.client.tensor_uninitialized(
binary_ops_shape(&lhs.shape, &rhs.shape),
B::IntElem::dtype(),
);

let desc = BinaryOperationDescription {
lhs: lhs.into_description(),
rhs: rhs.into_description(),
out: out.to_description_out(),
};
out.client.register(
vec![stream_1, stream_2],
repr::OperationDescription::Int(
IntElem::<Self>::dtype(),
IntOperationDescription::BitwiseXor(desc.clone()),
),
BitwiseXorOps::<B>::new(desc),
);

out
}

fn bitwise_xor_scalar(lhs: IntTensor<Self>, rhs: IntElem<Self>) -> IntTensor<Self> {
scalar_int_ops!(BitwiseXorOps, B::bitwise_xor_scalar);

let stream = lhs.stream;
let out = lhs
.client
.tensor_uninitialized(lhs.shape.clone(), B::IntElem::dtype());

let desc = ScalarOperationDescription {
lhs: lhs.into_description(),
rhs: rhs.elem(),
out: out.to_description_out(),
};
out.client.register(
vec![stream],
repr::OperationDescription::Int(
IntElem::<Self>::dtype(),
IntOperationDescription::BitwiseXorScalar(desc.clone()),
),
BitwiseXorOps::<B>::new(desc),
);

out
}

fn bitwise_not(tensor: IntTensor<Self>) -> IntTensor<Self> {
unary_int_ops!(BitwiseNotOps, B::bitwise_not);

let stream = tensor.stream;
let out = tensor
.client
.tensor_uninitialized(tensor.shape.clone(), B::IntElem::dtype());

let desc = UnaryOperationDescription {
input: tensor.into_description(),
out: out.to_description_out(),
};
out.client.register(
vec![stream],
repr::OperationDescription::Int(
IntElem::<Self>::dtype(),
IntOperationDescription::BitwiseNot(desc.clone()),
),
BitwiseNotOps::<B>::new(desc),
);

out
}
}
60 changes: 57 additions & 3 deletions crates/burn-fusion/src/stream/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ impl RelativeOps for OperationDescription {
OperationDescription::Bool(ops) => {
OperationDescription::Bool(ops.to_relative(converter))
}
OperationDescription::Int(ops) => OperationDescription::Int(ops.to_relative(converter)),
OperationDescription::Int(dtype, ops) => OperationDescription::Int(
*dtype,
ops.to_relative(converter, |converter, e| converter.relative_int(e, dtype)),
),
OperationDescription::Float(dtype, ops) => OperationDescription::Float(
*dtype,
RelativeOpsScalar::<f32>::to_relative(ops, converter, |converter, e| {
Expand Down Expand Up @@ -607,15 +610,66 @@ impl RelativeOps for BoolOperationDescription {
}
}

impl RelativeOps for IntOperationDescription {
fn to_relative(&self, converter: &mut OperationConverter) -> Self {
impl<E: Element> RelativeOpsScalar<E> for IntOperationDescription<E> {
fn to_relative<F>(&self, converter: &mut OperationConverter, local_elem: F) -> Self
where
F: Fn(&mut OperationConverter, &E) -> E,
{
match self {
IntOperationDescription::IntoFloat(desc) => {
IntOperationDescription::IntoFloat(UnaryOperationDescription {
input: desc.input.to_relative(converter),
out: desc.out.to_relative(converter),
})
}
IntOperationDescription::BitwiseAnd(desc) => {
IntOperationDescription::BitwiseAnd(BinaryOperationDescription {
lhs: desc.lhs.to_relative(converter),
rhs: desc.rhs.to_relative(converter),
out: desc.out.to_relative(converter),
})
}
IntOperationDescription::BitwiseAndScalar(desc) => {
IntOperationDescription::BitwiseAndScalar(ScalarOperationDescription {
lhs: desc.lhs.to_relative(converter),
rhs: local_elem(converter, &desc.rhs),
out: desc.out.to_relative(converter),
})
}
IntOperationDescription::BitwiseOr(desc) => {
IntOperationDescription::BitwiseOr(BinaryOperationDescription {
lhs: desc.lhs.to_relative(converter),
rhs: desc.rhs.to_relative(converter),
out: desc.out.to_relative(converter),
})
}
IntOperationDescription::BitwiseOrScalar(desc) => {
IntOperationDescription::BitwiseOrScalar(ScalarOperationDescription {
lhs: desc.lhs.to_relative(converter),
rhs: local_elem(converter, &desc.rhs),
out: desc.out.to_relative(converter),
})
}
IntOperationDescription::BitwiseXor(desc) => {
IntOperationDescription::BitwiseXor(BinaryOperationDescription {
lhs: desc.lhs.to_relative(converter),
rhs: desc.rhs.to_relative(converter),
out: desc.out.to_relative(converter),
})
}
IntOperationDescription::BitwiseXorScalar(desc) => {
IntOperationDescription::BitwiseXorScalar(ScalarOperationDescription {
lhs: desc.lhs.to_relative(converter),
rhs: local_elem(converter, &desc.rhs),
out: desc.out.to_relative(converter),
})
}
IntOperationDescription::BitwiseNot(desc) => {
IntOperationDescription::BitwiseNot(UnaryOperationDescription {
input: desc.input.to_relative(converter),
out: desc.out.to_relative(converter),
})
}
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions crates/burn-jit/src/kernel/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ pub(crate) struct MulOp;
pub(crate) struct DivOp;
pub(crate) struct RemainderOp;
pub(crate) struct PowOp;
pub(crate) struct BitwiseAndOp;
pub(crate) struct BitwiseOrOp;
pub(crate) struct BitwiseXorOp;
pub(crate) struct BitwiseNotOp;

#[cube]
impl<N: Numeric> BinaryOp<N> for AddOp {
Expand Down Expand Up @@ -62,6 +66,35 @@ impl<N: Float> BinaryOp<N> for PowOp {
}
}

#[cube]
impl<N: Numeric> BinaryOp<N> for BitwiseAndOp {
fn execute(lhs: Line<N>, rhs: Line<N>) -> Line<N> {
//lhs + rhs
lhs + rhs
}
}

#[cube]
impl<N: Numeric> BinaryOp<N> for BitwiseOrOp {
fn execute(lhs: Line<N>, rhs: Line<N>) -> Line<N> {
lhs + rhs
}
}

#[cube]
impl<N: Numeric> BinaryOp<N> for BitwiseXorOp {
fn execute(lhs: Line<N>, rhs: Line<N>) -> Line<N> {
lhs + rhs
}
}

#[cube]
impl<N: Numeric> BinaryOp<N> for BitwiseNotOp {
fn execute(lhs: Line<N>, rhs: Line<N>) -> Line<N> {
lhs + rhs
}
}
quinton11 marked this conversation as resolved.
Show resolved Hide resolved

#[cube(launch)]
pub(crate) fn kernel_scalar_binop<C: Numeric, O: BinaryOp<C>>(
input: &Tensor<Line<C>>,
Expand Down
Loading
Loading