Skip to content

Commit

Permalink
20241118 docs (#2629)
Browse files Browse the repository at this point in the history
* module docs

* varbuilder gguf docs

* add a link to gguf files

* small additonal mod doc titles

* safetensor docs

* more core docs

* more module docs in canlde_core

* 2 more link fixes
  • Loading branch information
zachcp authored Nov 19, 2024
1 parent 1a0f9cc commit 3159f91
Show file tree
Hide file tree
Showing 27 changed files with 72 additions and 12 deletions.
2 changes: 2 additions & 0 deletions candle-core/src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Traits to Define Backend Behavior
//!
use crate::op::{BinaryOpT, CmpOp, ReduceOp, UnaryOpT};
use crate::{CpuStorage, DType, Layout, Result, Shape};

Expand Down
2 changes: 1 addition & 1 deletion candle-core/src/backprop.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Methods for backpropagation of gradients.
//! Methods for backpropagation of gradients.
use crate::op::{BinaryOp, Op, ReduceOp, UnaryOp};
use crate::{Error, Result, Tensor, TensorId};
use std::collections::HashMap;
Expand Down
2 changes: 2 additions & 0 deletions candle-core/src/conv.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! 1D and 2D Convolutions
//!
use crate::{op::BackpropOp, op::Op, Error, Result, Tensor};

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down
2 changes: 2 additions & 0 deletions candle-core/src/cpu/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Traits and methods for CPU-backed Tensors
pub mod erf;
pub mod kernels;

Expand Down
1 change: 1 addition & 0 deletions candle-core/src/cpu_backend/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Implementation of Backend Fns for CPU
use crate::backend::{BackendDevice, BackendStorage};
use crate::op::{BinaryOpT, CmpOp, ReduceOp, UnaryOpT};
use crate::{DType, Error, IntDType, Layout, Result, Shape, WithDType};
Expand Down
2 changes: 2 additions & 0 deletions candle-core/src/cuda_backend/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Implementation of Backend traits for CUDA device
//!
use crate::backend::{BackendDevice, BackendStorage};
use crate::op::{BinaryOpT, CmpOp, ReduceOp, UnaryOpT};
use crate::{CpuStorage, DType, Layout, Result, Shape, WithDType};
Expand Down
1 change: 1 addition & 0 deletions candle-core/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub enum DeviceLocation {
Metal { gpu_id: usize },
}

/// Cpu, Cuda, or Metal
#[derive(Debug, Clone)]
pub enum Device {
Cpu,
Expand Down
7 changes: 4 additions & 3 deletions candle-core/src/display.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// Pretty printing of tensors
/// This implementation should be in line with the PyTorch version.
/// https://github.com/pytorch/pytorch/blob/7b419e8513a024e172eae767e24ec1b849976b13/torch/_tensor_str.py
//! Pretty printing of tensors
//!
//! This implementation should be in line with the [PyTorch version](https://github.com/pytorch/pytorch/blob/7b419e8513a024e172eae767e24ec1b849976b13/torch/_tensor_str.py).
//!
use crate::{DType, Result, Tensor, WithDType};
use half::{bf16, f16};

Expand Down
2 changes: 2 additions & 0 deletions candle-core/src/dummy_cuda_backend.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Implementation of the Cuda backend when Cuda support has not been compiled in.
//!
#![allow(dead_code)]
use crate::op::{BinaryOpT, CmpOp, ReduceOp, UnaryOpT};
use crate::{CpuStorage, DType, Error, Layout, Result, Shape};
Expand Down
1 change: 1 addition & 0 deletions candle-core/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Candle-specific Error and Result
use crate::{DType, DeviceLocation, Layout, MetalError, Shape};

#[derive(Debug, Clone)]
Expand Down
1 change: 1 addition & 0 deletions candle-core/src/layout.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Tensor Layouts including contiguous or sparse strides
use crate::{Error, Result, Shape};

#[derive(Debug, PartialEq, Eq, Clone)]
Expand Down
8 changes: 4 additions & 4 deletions candle-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//!
//! let a = Tensor::arange(0f32, 6f32, &Device::Cpu)?.reshape((2, 3))?;
//! let b = Tensor::arange(0f32, 12f32, &Device::Cpu)?.reshape((3, 4))?;
//!
//! let c = a.matmul(&b)?;
//!
//! # Ok(())}
//! ```
//!
Expand Down Expand Up @@ -140,7 +140,7 @@ impl ToUsize2 for (usize, usize) {
}
}

// A simple trait defining a module with forward method using a single argument.
/// Defining a module with forward method using a single argument.
pub trait Module {
fn forward(&self, xs: &Tensor) -> Result<Tensor>;
}
Expand All @@ -160,8 +160,8 @@ impl<M: Module> Module for Option<&M> {
}
}

// A trait defining a module with forward method using a single tensor argument and a flag to
// separate the training and evaluation behaviors.
/// A single forward method using a single single tensor argument and a flag to
/// separate the training and evaluation behaviors.
pub trait ModuleT {
fn forward_t(&self, xs: &Tensor, train: bool) -> Result<Tensor>;
}
Expand Down
2 changes: 2 additions & 0 deletions candle-core/src/metal_backend/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Implementation of Backend traits for Metal
//!
use crate::backend::{BackendDevice, BackendStorage};
use crate::conv::{ParamsConv1D, ParamsConv2D, ParamsConvTranspose1D, ParamsConvTranspose2D};
use crate::op::{BinaryOpT, CmpOp, ReduceOp, UnaryOpT};
Expand Down
2 changes: 2 additions & 0 deletions candle-core/src/op.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Tensor Opertion Enums and Traits
//!
#![allow(clippy::redundant_closure_call)]
use crate::Tensor;
use half::{bf16, f16};
Expand Down
2 changes: 1 addition & 1 deletion candle-core/src/pickle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Just enough pickle support to be able to read PyTorch checkpoints.
//! Just enough pickle support to be able to read PyTorch checkpoints.
// This hardcodes objects that are required for tensor reading, we may want to make this a bit more
// composable/tensor agnostic at some point.
use crate::{DType, Error as E, Layout, Result, Tensor};
Expand Down
2 changes: 1 addition & 1 deletion candle-core/src/quantized/ggml_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fn from_raw_data<T: super::GgmlType + Send + Sync + 'static>(
super::QTensor::new(data, dims)
}

/// Creates a [Tensor] from a raw GGML tensor.
/// Creates a Tensor from a raw GGML tensor.
pub fn qtensor_from_ggml(
ggml_dtype: GgmlDType,
raw_data: &[u8],
Expand Down
3 changes: 1 addition & 2 deletions candle-core/src/quantized/gguf_file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Support for the GGUF file format.
//! Support for the [GGUF file format](https://github.com/philpax/ggml/blob/gguf-spec/docs/gguf.md).
//!
//! Spec: https://github.com/philpax/ggml/blob/gguf-spec/docs/gguf.md
use super::{GgmlDType, QTensor};
use crate::{Device, Result};
Expand Down
1 change: 1 addition & 0 deletions candle-core/src/quantized/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Code for GGML and GGUF files
use crate::{CpuStorage, DType, Device, Result, Shape, Storage, Tensor};
use k_quants::*;
use std::borrow::Cow;
Expand Down
11 changes: 11 additions & 0 deletions candle-core/src/safetensors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
//! Module to load `safetensor` files into CPU/GPU memory.
//!
//! There are multiple ways to load tensors from safetensor files:
//! - `load` function for loading directly into memory and returning a HashMap of tensors
//! - `MmapedSafetensors` for memory mapping files and avoiding full allocation
//! - `SliceSafetensors` for working with in-memory buffers
//! - `BufferedSafetensors` for owning a buffer of data
//!
//! Tensors can also be serialized to safetensor format using the `save` function or
//! `Tensor::save_safetensors` method.
//!
use crate::{DType, Device, Error, Result, Tensor, WithDType};
use safetensors::tensor as st;
use safetensors::tensor::SafeTensors;
Expand Down
2 changes: 2 additions & 0 deletions candle-core/src/scalar.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! TensorScalar Enum and Trait
//!
use crate::{Result, Tensor, WithDType};

pub enum TensorScalar {
Expand Down
2 changes: 2 additions & 0 deletions candle-core/src/streaming.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! StreamTensror useful for streaming ops.
//!
use crate::{Result, Shape, Tensor};

pub trait Dim: crate::shape::Dim + Copy {}
Expand Down
1 change: 1 addition & 0 deletions candle-core/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Useful functions for checking features.
use std::str::FromStr;

pub fn get_num_threads() -> usize {
Expand Down
5 changes: 5 additions & 0 deletions candle-transformers/src/generation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
//! Logit Processing and Sampling
//!
//! Functionality for modeling sampling strategies and logits processing in text generation
//! with support for temperature-based sampling, top-k filtering, nucleus sampling (top-p),
//! and combinations thereof.
use candle::{DType, Error, Result, Tensor};
use rand::{distributions::Distribution, SeedableRng};

Expand Down
6 changes: 6 additions & 0 deletions candle-transformers/src/object_detection.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
//! Bounding Boxes and Intersection
//!
//! This module provides functionality for handling bounding boxes and their manipulation,
//! particularly in the context of object detection. It includes tools for calculating
//! intersection over union (IoU) and non-maximum suppression (NMS).
/// A bounding box around an object.
#[derive(Debug, Clone)]
pub struct Bbox<D> {
Expand Down
6 changes: 6 additions & 0 deletions candle-transformers/src/quantized_nn.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
//! Utilities for quanitized network layers
//!
//! This module contains various implementations of standard neural network layers, modules and
//! utilities including embedding, linear layers, and various normalization techniques.
//! Most implementations provide quantized weights support.
use crate::models::with_tracing::QMatMul;
use crate::quantized_var_builder::VarBuilder;
use candle::quantized::QTensor;
Expand Down
6 changes: 6 additions & 0 deletions candle-transformers/src/quantized_var_builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
//! Varbuilder for Loading gguf files
//!
//! VarBuilder is a utility to store quantized tensors from a [GGUF model file](https://huggingface.co/docs/hub/gguf).
//! These tensors can be loaded from disk using `from_gguf` or from an in-memory
//! buffer using `from_gguf_buffer`.
use candle::quantized::QTensor;
use candle::{Device, Result, Shape};
use std::sync::Arc;
Expand Down
2 changes: 2 additions & 0 deletions candle-transformers/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Apply penalty and repeat_kv
use candle::{Result, Tensor};

pub fn apply_repeat_penalty(logits: &Tensor, penalty: f32, context: &[u32]) -> Result<Tensor> {
Expand Down

0 comments on commit 3159f91

Please sign in to comment.