summaryrefslogtreecommitdiff
path: root/candle-core/src/tensor.rs
diff options
context:
space:
mode:
authorLaurent Mazare <laurent.mazare@gmail.com>2023-08-17 11:12:05 +0100
committerGitHub <noreply@github.com>2023-08-17 11:12:05 +0100
commit03be33eea482accbcf4c547728c2db7e24b7ebb0 (patch)
treeda5680d6d705d9346edbac9f2ce4a05779b86343 /candle-core/src/tensor.rs
parentd32e8199cd6c8381aa309528675d6d6a88c0f850 (diff)
downloadcandle-03be33eea482accbcf4c547728c2db7e24b7ebb0.tar.gz
candle-03be33eea482accbcf4c547728c2db7e24b7ebb0.tar.bz2
candle-03be33eea482accbcf4c547728c2db7e24b7ebb0.zip
Relax the requirements on CustomOp. (#486)
* Relax the requirements on CustomOp. * Simplify the custom-ops when no backward is required.
Diffstat (limited to 'candle-core/src/tensor.rs')
-rw-r--r--candle-core/src/tensor.rs65
1 files changed, 53 insertions, 12 deletions
diff --git a/candle-core/src/tensor.rs b/candle-core/src/tensor.rs
index c14a4e39..c71ea5ec 100644
--- a/candle-core/src/tensor.rs
+++ b/candle-core/src/tensor.rs
@@ -1870,22 +1870,53 @@ impl Tensor {
std::ptr::eq(lhs, rhs)
}
+ /// Applies a unary custom op without backward support
+ pub fn apply_op1_no_bwd<C: CustomOp1>(&self, c: &C) -> Result<Self> {
+ let (storage, shape) = self.storage().apply_op1(self.layout(), c)?;
+ Ok(from_storage(storage, shape, BackpropOp::none(), false))
+ }
+
+ /// Applies a binary custom op without backward support
+ pub fn apply_op2_no_bwd<C: CustomOp2>(&self, rhs: &Self, c: &C) -> Result<Self> {
+ let (storage, shape) =
+ self.storage()
+ .apply_op2(self.layout(), &rhs.storage(), rhs.layout(), c)?;
+ Ok(from_storage(storage, shape, BackpropOp::none(), false))
+ }
+
+ /// Applies a ternary custom op without backward support
+ pub fn apply_op3_no_bwd<C: CustomOp3>(&self, t2: &Self, t3: &Self, c: &C) -> Result<Self> {
+ let (storage, shape) = self.storage().apply_op3(
+ self.layout(),
+ &t2.storage(),
+ t2.layout(),
+ &t3.storage(),
+ t3.layout(),
+ c,
+ )?;
+ Ok(from_storage(storage, shape, BackpropOp::none(), false))
+ }
+
/// Applies a unary custom op.
- pub fn custom_op1_arc(&self, c: Arc<Box<dyn CustomOp1>>) -> Result<Self> {
+ pub fn apply_op1_arc(&self, c: Arc<Box<dyn CustomOp1 + Send + Sync>>) -> Result<Self> {
let (storage, shape) = self
.storage()
- .custom_op1(self.layout(), c.as_ref().as_ref())?;
+ .apply_op1(self.layout(), c.as_ref().as_ref())?;
let op = BackpropOp::new1(self, |s| Op::CustomOp1(s, c.clone()));
Ok(from_storage(storage, shape, op, false))
}
- pub fn custom_op1<C: 'static + CustomOp1>(&self, c: C) -> Result<Self> {
- self.custom_op1_arc(Arc::new(Box::new(c)))
+ pub fn apply_op1<C: 'static + CustomOp1 + Send + Sync>(&self, c: C) -> Result<Self> {
+ self.apply_op1_arc(Arc::new(Box::new(c)))
}
/// Applies a binary custom op.
- pub fn custom_op2_arc(&self, rhs: &Self, c: Arc<Box<dyn CustomOp2>>) -> Result<Self> {
- let (storage, shape) = self.storage().custom_op2(
+ pub fn apply_op2_arc(
+ &self,
+ rhs: &Self,
+ c: Arc<Box<dyn CustomOp2 + Send + Sync>>,
+ ) -> Result<Self> {
+ let (storage, shape) = self.storage().apply_op2(
self.layout(),
&rhs.storage(),
rhs.layout(),
@@ -1895,13 +1926,18 @@ impl Tensor {
Ok(from_storage(storage, shape, op, false))
}
- pub fn custom_op2<C: 'static + CustomOp2>(&self, r: &Self, c: C) -> Result<Self> {
- self.custom_op2_arc(r, Arc::new(Box::new(c)))
+ pub fn apply_op2<C: 'static + CustomOp2 + Send + Sync>(&self, r: &Self, c: C) -> Result<Self> {
+ self.apply_op2_arc(r, Arc::new(Box::new(c)))
}
/// Applies a ternary custom op.
- pub fn custom_op3_arc(&self, t2: &Self, t3: &Self, c: Arc<Box<dyn CustomOp3>>) -> Result<Self> {
- let (storage, shape) = self.storage().custom_op3(
+ pub fn apply_op3_arc(
+ &self,
+ t2: &Self,
+ t3: &Self,
+ c: Arc<Box<dyn CustomOp3 + Send + Sync>>,
+ ) -> Result<Self> {
+ let (storage, shape) = self.storage().apply_op3(
self.layout(),
&t2.storage(),
t2.layout(),
@@ -1915,8 +1951,13 @@ impl Tensor {
Ok(from_storage(storage, shape, op, false))
}
- pub fn custom_op3<C: 'static + CustomOp3>(&self, t2: &Self, t3: &Self, c: C) -> Result<Self> {
- self.custom_op3_arc(t2, t3, Arc::new(Box::new(c)))
+ pub fn apply_op3<C: 'static + CustomOp3 + Send + Sync>(
+ &self,
+ t2: &Self,
+ t3: &Self,
+ c: C,
+ ) -> Result<Self> {
+ self.apply_op3_arc(t2, t3, Arc::new(Box::new(c)))
}
}