Giter Club home page Giter Club logo

safetensors.cpp's Introduction

Safetensors.cpp

Zero* Dependency Safetensors Loading and Storing with C++ and LibTorch

Arbitrary Limits

  • 2TiB overall file size (enough to store LLaMA 405b at full fp32 precision)
  • 2048 tensors
  • 8 dims per tensor
  • 2KiB max string size in metadata
  • 8KiB max overall metadata size

If you want to change these, they're in the header.

Usage Examples

Loading Safetensors

#include "safetensors.hpp"

int main() {
    try {
        // Load tensors from a file
        std::string filename = "model.safetensors";
        auto tensors = safetensors::load_safetensors(filename);

        // Print information about loaded tensors
        for (const auto& [name, tensor] : tensors) {
            std::cout << "Tensor name: " << name << std::endl;
            std::cout << "Shape: " << tensor.sizes() << std::endl;
            std::cout << "Dtype: " << tensor.dtype() << std::endl;
            std::cout << std::endl;
        }
    } catch (const safetensors::SafetensorsException& e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }

    return 0;
}

Saving Safetensors

#include "safetensors.hpp"

int main() {
    try {
        // Create some example tensors
        std::unordered_map<std::string, torch::Tensor> tensors;
        tensors["weight"] = torch::randn({3, 3});
        tensors["bias"] = torch::zeros({3});

        // Add some metadata
        std::unordered_map<std::string, std::string> metadata;
        metadata["description"] = "Example model";
        metadata["version"] = "1.0";

        // Save tensors to a file
        std::string filename = "model_output.safetensors";
        safetensors::save_safetensors(tensors, filename, metadata);

        std::cout << "Tensors saved successfully to " << filename << std::endl;
    } catch (const safetensors::SafetensorsException& e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }

    return 0;
}

Loading to a Specific Device

#include "safetensors.hpp"

int main() {
    try {
        std::string filename = "model.safetensors";
        torch::Device device(torch::kCUDA);  // or torch::kCPU for CPU
        auto tensors = safetensors::load_safetensors(filename, device);

        // Tensors are now loaded directly to the specified device
        for (const auto& [name, tensor] : tensors) {
            std::cout << "Tensor " << name << " is on " << tensor.device() << std::endl;
        }
    } catch (const safetensors::SafetensorsException& e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }

    return 0;
}

License

MIT


* LibTorch is required, but no additional dependencies are needed for safetensors functionality.

safetensors.cpp's People

Contributors

carsonpo avatar

Stargazers

Dammian Miller avatar  avatar tom zhou avatar  avatar

Watchers

 avatar Jim McShane avatar

Forkers

aiworkspace

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.