1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
//! # Context
extern crate alloc;
use crate::{
graph::Graph,
node_id::NodeId,
shape::Shape,
tensor::{IntoTensor, Tensor},
OutOfMemoryError,
};
use core::cell::RefCell;
use rclite::Rc;
/// # Context
///
/// Context stores all data associated with tensors.
/// It stores actual values, that is device buffers,
/// directed acyclic graph of all operations executed on all tensors
/// and devices that are used to execute those operations.
#[derive(Debug)]
pub struct Context {
graph: Rc<RefCell<Graph>>,
}
impl Default for Context {
fn default() -> Self {
Self::new()
}
}
impl From<Rc<RefCell<Graph>>> for Context {
fn from(graph: Rc<RefCell<Graph>>) -> Self {
Self { graph }
}
}
impl Context {
/// Returns Vec of strings of all nodes in this context.
/// When you just want to print all nodes in realized graphs,
/// consider using feature debug1.
#[must_use]
pub fn debug_nodes(&self) -> alloc::vec::Vec<alloc::string::String> {
self.graph.borrow_mut().debug_nodes()
}
/// Create new context. This context will use slow rust backend by default.
/// # Example
/// ```
/// use zyx::context::Context;
/// let ctx = Context::new();
/// ```
#[must_use]
pub fn new() -> Context {
Self {
graph: Rc::new(RefCell::new(Graph::default())),
}
}
/// Create new context that uses `OpenCL` backend.
/// # Errors
/// Returns `ClError` if there was problem initializing `OpenCL`.
#[cfg(feature = "opencl")]
pub fn opencl() -> Result<Self, cl3::error_codes::ClError> {
let graph = Rc::new(RefCell::new(Graph::default()));
let device = crate::device::Device::opencl()?;
graph.borrow_mut().devices.push(device);
graph.borrow_mut().default_device = 1;
Ok(Self { graph })
}
/// Create new context that uses `Torch` backend.
#[cfg(feature = "torch")]
#[must_use]
pub fn torch() -> Self {
let graph = Rc::new(RefCell::new(Graph::default()));
let device = crate::device::Device::torch();
graph.borrow_mut().devices.push(device);
graph.borrow_mut().default_device = 1;
Self { graph }
}
// TODO this can be perhaps interesting for multi platform execution,
// but there needs to be way to convert tensors between devices and that is slow.
/* /// Adds new device to context and makes it default.
/// This function should be used with caution, because executing graph
/// containing tensors that were already realized by different device will result in panic.
pub fn on(&mut self, device: crate::device::Device) -> Self {
let mut graph = self.graph.borrow_mut();
graph.default_device = graph.devices.len();
graph.devices.push(device.device);
Self {
rng: self.rng.clone(),
graph: self.graph.clone(),
}
}*/
/// Create new f32 tensor filled with value.
/// # Example
/// ```
/// # use zyx::context::Context;
/// let ctx = Context::new();
/// let mut x = ctx.full((2, 4), 2.);
/// x.realize().unwrap();
/// assert_eq!(x, [[2., 2., 2., 2.], [2., 2., 2., 2.]]);
/// ```
#[must_use]
pub fn full(&self, shape: impl Into<Shape>, value: f32) -> Tensor {
let shape = shape.into();
let mut graph = self.graph.borrow_mut();
let temp = graph.push(crate::graph::Node::StoreF32(
alloc::boxed::Box::new([value]),
1.into(),
));
let data = graph.push(crate::graph::Node::Expand(temp, shape));
graph.release(temp);
Tensor {
data,
grad: None,
graph: self.graph.clone(),
}
}
/// Create new i32 tensor filled with value.
/// # Example
/// ```
/// # use zyx::context::Context;
/// let ctx = Context::new();
/// let mut x = ctx.full_i32((2, 4), 2);
/// x.realize().unwrap();
/// assert_eq!(x, [[2, 2, 2, 2], [2, 2, 2, 2]]);
/// ```
#[must_use]
pub fn full_i32(&self, shape: impl Into<Shape>, value: i32) -> Tensor {
let shape = shape.into();
let mut graph = self.graph.borrow_mut();
let temp = graph.push(crate::graph::Node::StoreI32(
alloc::boxed::Box::new([value]),
1.into(),
));
let data = graph.push(crate::graph::Node::Expand(temp, shape));
graph.release(temp);
Tensor {
data,
grad: None,
graph: self.graph.clone(),
}
}
/// Dot language string of graph
#[must_use]
pub fn dot_graph(&self) -> alloc::string::String {
self.graph.borrow().show_graph()
}
/// Number of tensors stored in this context
/// # Example
/// ```
/// # use zyx::context::Context;
/// let ctx = Context::new();
/// let x = ctx.randn((2, 3));
/// let y = ctx.randn((4, 3));
/// assert_eq!(ctx.num_tensors(), 2);
/// ```
#[must_use]
pub fn num_tensors(&self) -> usize {
self.graph.borrow().num_nodes()
}
/// Create new ones tensor.
/// # Example
/// ```
/// # use zyx::context::Context;
/// let ctx = Context::new();
/// let mut x = ctx.ones((2, 4));
/// x.realize().unwrap();
/// assert_eq!(x, [[1., 1., 1., 1.], [1., 1., 1., 1.]]);
/// ```
#[must_use]
pub fn ones(&self, shape: impl Into<Shape>) -> Tensor {
self.full(shape, 1.)
}
/// Create new ones i32 tensor.
/// # Example
/// ```
/// # use zyx::context::Context;
/// let ctx = Context::new();
/// let mut x = ctx.ones_i32((2, 4));
/// x.realize().unwrap();
/// assert_eq!(x, [[1, 1, 1, 1], [1, 1, 1, 1]]);
/// ```
#[must_use]
pub fn ones_i32(&self, shape: impl Into<Shape>) -> Tensor {
self.full_i32(shape, 1)
}
/// Create new tensor filled with values sampled from standard distribution.
/// # Example
/// ```
/// # use zyx::context::Context;
/// let ctx = Context::new();
/// let mut x = ctx.randn((2, 4));
/// ```
#[must_use]
pub fn randn(&self, shape: impl Into<Shape>) -> Tensor {
Tensor {
data: self.graph.borrow_mut().randn_f32(shape.into()),
grad: None,
graph: self.graph.clone(),
}
}
/// Create new i32 tensor filled with values sampled from standard distribution.
/// # Example
/// ```
/// # use zyx::context::Context;
/// let ctx = Context::new();
/// let mut x = ctx.randn_i32((2, 4));
/// ```
#[must_use]
pub fn randn_i32(&self, shape: impl Into<Shape>) -> Tensor {
Tensor {
data: self.graph.borrow_mut().randn_i32(shape.into()),
grad: None,
graph: self.graph.clone(),
}
}
pub(crate) fn realize(&self, nodes: &[NodeId]) -> Result<(), OutOfMemoryError> {
self.graph.borrow_mut().realize(nodes)
}
/// Create new tensor from data.
/// # Example
/// ```
/// # use zyx::context::Context;
/// let ctx = Context::new();
/// let mut x = ctx.tensor([[2, 3, 4], [5, 3, 4]]);
/// ```
#[must_use]
pub fn tensor(&self, data: impl IntoTensor) -> Tensor {
data.into_tensor(self)
}
/// Create new tensor from iterator
#[must_use]
pub fn tensor_from_iter_f32(
&self,
shape: impl Into<Shape>,
iter: impl IntoIterator<Item = f32>,
) -> Tensor {
let shape = shape.into();
let n = shape.numel();
Tensor {
data: self
.graph
.borrow_mut()
.iter_f32(shape, iter.into_iter().take(n)),
grad: None,
graph: self.graph.clone(),
}
}
/// Create new i32 tensor from iterator
#[must_use]
pub fn tensor_from_iter_i32(
&self,
shape: impl Into<Shape>,
iter: impl IntoIterator<Item = i32>,
) -> Tensor {
let shape = shape.into();
let n = shape.numel();
Tensor {
data: self
.graph
.borrow_mut()
.iter_i32(shape, iter.into_iter().take(n)),
grad: None,
graph: self.graph.clone(),
}
}
/// Create new i32 tensor filled with values sampled from uniform distribution.
/// # Example
/// ```
/// # use zyx::context::Context;
/// let ctx = Context::new();
/// let mut x = ctx.uniform((2, 4), -2.0..5.0);
/// ```
#[must_use]
pub fn uniform(&self, shape: impl Into<Shape>, range: core::ops::Range<f32>) -> Tensor {
Tensor {
data: self.graph.borrow_mut().uniform_f32(shape.into(), range),
grad: None,
graph: self.graph.clone(),
}
}
/// Create new i32 tensor filled with values sampled from uniform distribution.
/// # Example
/// ```
/// # use zyx::context::Context;
/// let ctx = Context::new();
/// let mut x = ctx.uniform_i32((2, 4), -2..5);
/// ```
#[must_use]
pub fn uniform_i32(&self, shape: impl Into<Shape>, range: core::ops::Range<i32>) -> Tensor {
Tensor {
data: self.graph.borrow_mut().uniform_i32(shape.into(), range),
grad: None,
graph: self.graph.clone(),
}
}
/// Create new f32 tensor filled with zeros
/// # Example
/// ```
/// # use zyx::context::Context;
/// let ctx = Context::new();
/// let mut x = ctx.zeros((2, 4));
/// x.realize().unwrap();
/// assert_eq!(x, [[0., 0., 0., 0.], [0., 0., 0., 0.]]);
/// ```
#[must_use]
pub fn zeros(&self, shape: impl Into<Shape>) -> Tensor {
self.full(shape, 0.)
}
/// Create new i32 tensor filled with zeros
/// # Example
/// ```
/// # use zyx::context::Context;
/// let ctx = Context::new();
/// let mut x = ctx.zeros_i32((2, 4));
/// x.realize().unwrap();
/// assert_eq!(x, [[0, 0, 0, 0], [0, 0, 0, 0]]);
/// ```
#[must_use]
pub fn zeros_i32(&self, shape: impl Into<Shape>) -> Tensor {
self.full_i32(shape, 0)
}
}
impl IntoTensor for Tensor {
fn into_tensor(self, _: &crate::context::Context) -> Tensor {
self
}
}
impl IntoTensor for &Tensor {
fn into_tensor(self, _: &crate::context::Context) -> Tensor {
self.clone()
}
}
impl IntoTensor for f32 {
fn into_tensor(self, ctx: &Context) -> Tensor {
ctx.tensor_from_iter_f32(1, [self])
}
}
impl<const L: usize> IntoTensor for [f32; L] {
fn into_tensor(self, ctx: &Context) -> Tensor {
ctx.tensor_from_iter_f32(L, self)
}
}
impl<const L: usize, const M: usize> IntoTensor for [[f32; L]; M] {
fn into_tensor(self, ctx: &Context) -> Tensor {
ctx.tensor_from_iter_f32((M, L), self.into_iter().flatten())
}
}
impl<const L: usize, const M: usize, const N: usize> IntoTensor for [[[f32; L]; M]; N] {
fn into_tensor(self, ctx: &Context) -> Tensor {
ctx.tensor_from_iter_f32((N, M, L), self.into_iter().flatten().flatten())
}
}
impl IntoTensor for i32 {
fn into_tensor(self, ctx: &Context) -> Tensor {
ctx.tensor_from_iter_i32(1, [self])
}
}
impl<const L: usize> IntoTensor for [i32; L] {
fn into_tensor(self, ctx: &Context) -> Tensor {
ctx.tensor_from_iter_i32(L, self)
}
}
impl<const L: usize, const M: usize> IntoTensor for [[i32; L]; M] {
fn into_tensor(self, ctx: &Context) -> Tensor {
ctx.tensor_from_iter_i32((M, L), self.into_iter().flatten())
}
}
impl<const L: usize, const M: usize, const N: usize> IntoTensor for [[[i32; L]; M]; N] {
fn into_tensor(self, ctx: &Context) -> Tensor {
ctx.tensor_from_iter_i32((N, M, L), self.into_iter().flatten().flatten())
}
}