url
stringlengths
31
71
targets
stringlengths
11
143
authors
stringlengths
6
190
date
stringlengths
11
18
inputs
stringlengths
140
14.8k
https://huggingface.co/blog/ethics-diffusers
Ethical guidelines for developing the Diffusers library
Giada Pistilli
March 2, 2023
We are on a journey to make our libraries more responsible, one commit at a time! As part of the Diffusers library documentation, we are proud to announce the publication of an ethical framework. Given diffusion models' real case applications in the world and potential negative impacts on society, this initiative aims to guide the technical decisions of the Diffusers library maintainers about community contributions. We wish to be transparent in how we make decisions, and above all, we aim to clarify what values guide those decisions.We see ethics as a process that leverages guiding values, concrete actions, and continuous adaptation. For this reason, we are committed to adjusting our guidelines over time, following the evolution of the Diffusers project and the valuable feedback from the community that keeps it alive.Ethical guidelinesTransparency: we are committed to being transparent in managing PRs, explaining our choices to users, and making technical decisions.Consistency: we are committed to guaranteeing our users the same level of attention in project management, keeping it technically stable and consistent.Simplicity: with a desire to make it easy to use and exploit the Diffusers library, we are committed to keeping the project’s goals lean and coherent.Accessibility: the Diffusers project helps lower the entry bar for contributors who can help run it even without technical expertise. Doing so makes research artifacts more accessible to the community.Reproducibility: we aim to be transparent about the reproducibility of upstream code, models, and datasets when made available through the Diffusers library.Responsibility: as a community and through teamwork, we hold a collective responsibility to our users by anticipating and mitigating this technology’s potential risks and dangers.Safety features and mechanismsIn addition, we provide a non-exhaustive - and hopefully continuously expanding! - list of safety features and mechanisms implemented by the Hugging Face team and the broader community.Community tab: it enables the community to discuss and better collaborate on a project.Tag feature: authors of a repository can tag their content as being “Not For All Eyes”Bias exploration and evaluation: the Hugging Face team provides a Space to demonstrate the biases in Stable Diffusion and DALL-E interactively. In this sense, we support and encourage bias explorers and evaluations.Encouraging safety in deploymentSafe Stable Diffusion: It mitigates the well-known issue that models, like Stable Diffusion, that are trained on unfiltered, web-crawled datasets tend to suffer from inappropriate degeneration. Related paper: Safe Latent Diffusion: Mitigating Inappropriate Degeneration in Diffusion Models.Staged released on the Hub: in particularly sensitive situations, access to some repositories should be restricted. This staged release is an intermediary step that allows the repository’s authors to have more control over its use.Licensing: OpenRAILs, a new type of licensing, allow us to ensure free access while having a set of restrictions that ensure more responsible use.
https://huggingface.co/blog/cloudflare-workers-ai
Bringing serverless GPU inference to Hugging Face users
Philipp Schmid, Jeff Boudier, Rita Kozlov, Nikhil Kothari
April 2, 2024
Today, we are thrilled to announce the launch of Deploy on Cloudflare Workers AI, a new integration on the Hugging Face Hub. Deploy on Cloudflare Workers AI makes using open models as a serverless API easy, powered by state-of-the-art GPUs deployed in Cloudflare edge data centers. Starting today, we are integrating some of the most popular open models on Hugging Face into Cloudflare Workers AI, powered by our production solutions, like Text Generation Inference. With Deploy on Cloudflare Workers AI, developers can build robust Generative AI applications without managing GPU infrastructure and servers and at a very low operating cost: only pay for the compute you use, not for idle capacity. Generative AI for Developers This new experience expands upon the strategic partnership we announced last year to simplify the access and deployment of open Generative AI models. One of the main problems developers and organizations face is the scarcity of GPU availability and the fixed costs of deploying servers to start building. Deploy on Cloudflare Workers AI offers an easy, low-cost solution to these challenges, providing serverless access to popular Hugging Face Models with a pay-per-request pricing model. Let's take a look at a concrete example. Imagine you develop an RAG Application that gets ~1000 requests per day, with an input of 1k tokens and an output of 100 tokens using Meta Llama 2 7B. The LLM inference production costs would amount to about $1 a day."We're excited to bring this integration to life so quickly. Putting the power of Cloudflare's global network of serverless GPUs into the hands of developers, paired with the most popular open source models on Hugging Face, will open the doors to lots of exciting innovation by our community around the world," said John Graham-Cumming, CTO, Cloudflare How it works Using Hugging Face Models on Cloudflare Workers AI is super easy. Below, you will find step-by-step instructions on how to use Hermes 2 Pro on Mistral 7B, the newest model from Nous Research.You can find all available models in this Cloudflare Collection.Note: You need access to a Cloudflare Account and API Token.You can find the Deploy on Cloudflare option on all available model pages, including models like Llama, Gemma or Mistral.Open the “Deploy” menu, and select “Cloudflare Workers AI” - this will open an interface that includes instructions on how to use this model and send requests.Note: If the model you want to use does not have a “Cloudflare Workers AI” option, it is currently not supported. We are working on extending the availability of models together with Cloudflare. You can reach out to us at api-enterprise@huggingface.co with your request.The integration can currently be used via two options: using the Workers AI REST API or directly in Workers with the Cloudflare AI SDK. Select your preferred option and copy the code into your environment. When using the REST API, you need to make sure the ACCOUNT_ID and API_TOKEN variables are defined. That’s it! Now you can start sending requests to Hugging Face Models hosted on Cloudflare Workers AI. Make sure to use the correct prompt & template expected by the model. We’re just getting started We are excited to collaborate with Cloudflare to make AI more accessible to developers. We will work with the Cloudflare team to make more models and experiences available to you!
https://huggingface.co/blog/habana-gaudi-2-benchmark
Faster Training and Inference: Habana Gaudi®-2 vs Nvidia A100 80GB
Régis Pierrard
December 14, 2022
In this article, you will learn how to use Habana® Gaudi®2 to accelerate model training and inference, and train bigger models with 🤗 Optimum Habana. Then, we present several benchmarks including BERT pre-training, Stable Diffusion inference and T5-3B fine-tuning, to assess the performance differences between first generation Gaudi, Gaudi2 and Nvidia A100 80GB. Spoiler alert - Gaudi2 is about twice faster than Nvidia A100 80GB for both training and inference!Gaudi2 is the second generation AI hardware accelerator designed by Habana Labs. A single server contains 8 accelerator devices with 96GB of memory each (versus 32GB on first generation Gaudi and 80GB on A100 80GB). The Habana SDK, SynapseAI, is common to both first-gen Gaudi and Gaudi2.That means that 🤗 Optimum Habana, which offers a very user-friendly interface between the 🤗 Transformers and 🤗 Diffusers libraries and SynapseAI, works the exact same way on Gaudi2 as on first-gen Gaudi!So if you already have ready-to-use training or inference workflows for first-gen Gaudi, we encourage you to try them on Gaudi2, as they will work without any single change.How to Get Access to Gaudi2?One of the easy, cost-efficient ways that Intel and Habana have made Gaudi2 available is on the Intel Developer Cloud. To start using Gaudi2 there, you should follow the following steps:Go to the Intel Developer Cloud landing page and sign in to your account or register if you do not have one.Go to the Intel Developer Cloud management console.Select Habana Gaudi2 Deep Learning Server featuring eight Gaudi2 HL-225H mezzanine cards and latest Intel® Xeon® Processors and click on Launch Instance in the lower right corner as shown below.You can then request an instance:Once your request is validated, re-do step 3 and click on Add OpenSSH Publickey to add a payment method (credit card or promotion code) and a SSH public key that you can generate with ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa. You may be redirected to step 3 each time you add a payment method or a SSH public key.Re-do step 3 and then click on Launch Instance. You will have to accept the proposed general conditions to actually launch the instance.Go to the Intel Developer Cloud management console and click on the tab called View Instances.You can copy the SSH command to access your Gaudi2 instance remotely!If you terminate the instance and want to use Gaudi2 again, you will have to re-do the whole process.You can find more information about this process here.BenchmarksSeveral benchmarks were performed to assess the abilities of first-gen Gaudi, Gaudi2 and A100 80GB for both training and inference, and for models of various sizes.Pre-Training BERTA few months ago, Philipp Schmid, technical lead at Hugging Face, presented how to pre-train BERT on Gaudi with 🤗 Optimum Habana. 65k training steps were performed with a batch size of 32 samples per device (so 8*32=256 in total) for a total training time of 8 hours and 53 minutes (you can see the TensorBoard logs of this run here).We re-ran the same script with the same hyperparameters on Gaudi2 and got a total training time of 2 hours and 55 minutes (see the logs here). That makes a x3.04 speedup on Gaudi2 without changing anything.Since Gaudi2 has roughly 3 times more memory per device compared to first-gen Gaudi, it is possible to leverage this greater capacity to have bigger batches. This will give HPUs more work to do and will also enable developers to try a range of hyperparameter values that was not reachable with first-gen Gaudi. With a batch size of 64 samples per device (512 in total), we got with 20k steps a similar loss convergence to the 65k steps of the previous runs. That makes a total training time of 1 hour and 33 minutes (see the logs here). The throughput is x1.16 higher with this configuration, while this new batch size strongly accelerates convergence.Overall, with Gaudi2, the total training time is reduced by a 5.75 factor and the throughput is x3.53 higher compared to first-gen Gaudi.Gaudi2 also offers a speedup over A100: 1580.2 samples/s versus 981.6 for a batch size of 32 and 1835.8 samples/s versus 1082.6 for a batch size of 64, which is consistent with the x1.8 speedup announced by Habana on the phase 1 of BERT pre-training with a batch size of 64.The following table displays the throughputs we got for first-gen Gaudi, Gaudi2 and Nvidia A100 80GB GPUs:First-gen Gaudi (BS=32)Gaudi2 (BS=32)Gaudi2 (BS=64)A100 (BS=32)A100 (BS=64)Throughput (samples/s)520.21580.21835.8981.61082.6Speedupx1.0x3.04x3.53x1.89x2.08BS is the batch size per device. The Gaudi runs were performed in mixed precision (bf16/fp32) and the A100 runs in fp16. All runs were distributed runs on 8 devices.Generating Images from Text with Stable DiffusionOne of the main new features of 🤗 Optimum Habana release 1.3 is the support for Stable Diffusion. It is now very easy to generate images from text on Gaudi. Unlike with 🤗 Diffusers on GPUs, images are generated by batches. Due to model compilation times, the first two batches will be slower than the following iterations. In this benchmark, these first two iterations were discarded to compute the throughputs for both first-gen Gaudi and Gaudi2.This script was run for a batch size of 8 samples. It uses the Habana/stable-diffusion Gaudi configuration.The results we got, which are consistent with the numbers published by Habana here, are displayed in the table below.Gaudi2 showcases latencies that are x3.51 faster than first-gen Gaudi (3.25s versus 0.925s) and x2.84 faster than Nvidia A100 (2.63s versus 0.925s). It can also support bigger batch sizes.First-gen Gaudi (BS=8)Gaudi2 (BS=8)A100 (BS=1)Latency (s/img)3.250.9252.63Speedupx1.0x3.51x1.24Update: the figures above were updated as SynapseAI 1.10 and Optimum Habana 1.6 bring an additional speedup on first-gen Gaudi and Gaudi2.BS is the batch size.The Gaudi runs were performed in bfloat16 precision and the A100 runs in fp16 precision (more information here). All runs were single-device runs.Fine-tuning T5-3BWith 96 GB of memory per device, Gaudi2 enables running much bigger models. For instance, we managed to fine-tune T5-3B (containing 3 billion parameters) with gradient checkpointing being the only applied memory optimization. This is not possible on first-gen Gaudi.Here are the logs of this run where the model was fine-tuned on the CNN DailyMail dataset for text summarization using this script.The results we achieved are presented in the table below. Gaudi2 is x2.44 faster than A100 80GB. We observe that we cannot fit a batch size larger than 1 on Gaudi2 here. This is due to the memory space taken by the graph where operations are accumulated during the first iteration of the run. Habana is working on optimizing the memory footprint in future releases of SynapseAI. We are looking forward to expanding this benchmark using newer versions of Habana's SDK and also using DeepSpeed to see if the same trend holds.First-gen GaudiGaudi2 (BS=1)A100 (BS=16)Throughput (samples/s)N/A19.78.07Speedup/x2.44x1.0BS is the batch size per device. Gaudi2 and A100 runs were performed in fp32 with gradient checkpointing enabled. All runs were distributed runs on 8 devices.ConclusionIn this article, we discuss our first experience with Gaudi2. The transition from first generation Gaudi to Gaudi2 is completely seamless since SynapseAI, Habana's SDK, is fully compatible with both. This means that new optimizations proposed by future releases will benefit both of them.You have seen that Habana Gaudi2 significantly improves performance over first generation Gaudi and delivers about twice the throughput speed as Nvidia A100 80GB for both training and inference.You also know now how to setup a Gaudi2 instance through the Intel Developer Zone. Check out the examples you can easily run on it with 🤗 Optimum Habana.If you are interested in accelerating your Machine Learning training and inference workflows using the latest AI hardware accelerators and software libraries, check out our Expert Acceleration Program. To learn more about Habana solutions, read about our partnership here and contact them. To learn more about Hugging Face efforts to make AI hardware accelerators easy to use, check out our Hardware Partner Program.Related TopicsGetting Started on Transformers with Habana GaudiAccelerate Transformer Model Training with Hugging Face and Habana LabsThanks for reading! If you have any questions, feel free to contact me, either through Github or on the forum. You can also connect with me on LinkedIn.
https://huggingface.co/blog/duckdb-nsql-7b
Text2SQL using Hugging Face Dataset Viewer API and Motherduck DuckDB-NSQL-7B
Andrea Soria, Till Döhmen, Sen Wu, Laurel Orr
April 4, 2024
Today, integrating AI-powered features, particularly leveraging Large Language Models (LLMs), has become increasingly prevalent across various tasks such as text generation, classification, image-to-text, image-to-image transformations, etc.Developers are increasingly recognizing these applications' potential benefits, particularly in enhancing core tasks such as scriptwriting, web development, and, now, interfacing with data. Historically, crafting insightful SQL queries for data analysis was primarily the domain of data analysts, SQL developers, data engineers, or professionals in related fields, all navigating the nuances of SQL dialect syntax. However, with the advent of AI-powered solutions, the landscape is evolving. These advanced models offer new avenues for interacting with data, potentially streamlining processes and uncovering insights with greater efficiency and depth.What if you could unlock fascinating insights from your dataset without diving deep into coding? To glean valuable information, one would need to craft a specialized SELECT statement, considering which columns to display, the source table, filtering conditions for selected rows, aggregation methods, and sorting preferences. This traditional approach involves a sequence of commands: SELECT, FROM, WHERE, GROUP, and ORDER.But what if you’re not a seasoned developer and still want to harness the power of your data? In such cases, seeking assistance from SQL specialists becomes necessary, highlighting a gap in accessibility and usability.This is where groundbreaking advancements in AI and LLM technology step in to bridge the divide. Imagine conversing with your data effortlessly, simply stating your information needs in plain language and having the model translate your request into a query. In recent months, significant strides have been made in this arena. MotherDuck and Numbers Station unveiled their latest innovation: DuckDB-NSQL-7B, a state-of-the-art LLM designed specifically for DuckDB SQL. What is this model’s mission? To empower users with the ability to unlock insights from their data effortlessly.Initially fine-tuned from Meta’s original Llama-2–7b model using a broad dataset covering general SQL queries, DuckDB-NSQL-7B underwent further refinement with DuckDB text-to-SQL pairs. Notably, its capabilities extend beyond crafting SELECT statements; it can generate a wide range of valid DuckDB SQL statements, including official documentation and extensions, making it a versatile tool for data exploration and analysis.In this article, we will learn how to deal with text2sql tasks using the DuckDB-NSQL-7B model, the Hugging Face dataset viewer API for parquet files and duckdb for data retrieval.text2sql flowHow to use the modelUsing Hugging Face transformers pipelinefrom transformers import pipelinepipe = pipeline("text-generation", model="motherduckdb/DuckDB-NSQL-7B-v0.1")Using transformers tokenizer and modelfrom transformers import AutoTokenizer, AutoModelForCausalLMtokenizer = AutoTokenizer.from_pretrained("motherduckdb/DuckDB-NSQL-7B-v0.1")model = AutoModelForCausalLM.from_pretrained("motherduckdb/DuckDB-NSQL-7B-v0.1")Using llama.cpp to load the model in GGUFfrom llama_cpp import Llamallama = Llama(model_path="DuckDB-NSQL-7B-v0.1-q8_0.gguf", # Path to local modeln_gpu_layers=-1,)The main goal of llama.cpp is to enable LLM inference with minimal setup and state-of-the-art performance on a wide variety of hardware - locally and in the cloud. We will use this approach.Hugging Face Dataset Viewer API for more than 120K datasetsData is a crucial component in any Machine Learning endeavor. Hugging Face is a valuable resource, offering access to over 120,000 free and open datasets spanning various formats, including CSV, Parquet, JSON, audio, and image files.Each dataset hosted by Hugging Face comes equipped with a comprehensive dataset viewer. This viewer provides users essential functionalities such as statistical insights, data size assessment, full-text search capabilities, and efficient filtering options. This feature-rich interface empowers users to easily explore and evaluate datasets, facilitating informed decision-making throughout the machine learning workflow.For this demo, we will be using the world-cities-geo dataset.Dataset viewer of world-cities-geo datasetBehind the scenes, each dataset in the Hub is processed by the Hugging Face dataset viewer API, which gets useful information and serves functionalities like:List the dataset splits, column names and data typesGet the dataset size (in number of rows or bytes)Download and view rows at any index in the datasetSearch a word in the datasetFilter rows based on a query stringGet insightful statistics about the dataAccess the dataset as parquet files to use in your favorite processing or analytics frameworkIn this demo, we will use the last functionality, auto-converted parquet files.Generate SQL queries from text instructionsFirst, download the quantized models version of DuckDB-NSQL-7B-v0.1Downloading the modelAlternatively, you can execute the following code:huggingface-cli download motherduckdb/DuckDB-NSQL-7B-v0.1-GGUF DuckDB-NSQL-7B-v0.1-q8_0.gguf --local-dir . --local-dir-use-symlinks FalseNow, lets install the needed dependencies:pip install llama-cpp-pythonpip install duckdbFor the text-to-SQL model, we will use a prompt with the following structure:### Instruction:Your task is to generate valid duckdb SQL to answer the following question.### Input:Here is the database schema that the SQL query will run on:{ddl_create}### Question:{query_input}### Response (use duckdb shorthand if possible):ddl_create will be the dataset schema as a SQL CREATE commandquery_input will be the user instructions, expressed with natural languageSo, we need to tell to the model about the schema of the Hugging Face dataset. For that, we are going to get the first parquet file for jamescalam/world-cities-geo dataset:GET https://huggingface.co/api/datasets/jamescalam/world-cities-geo/parquet{"default":{"train":["https://huggingface.co/api/datasets/jamescalam/world-cities-geo/parquet/default/train/0.parquet"]}}The parquet file is hosted in Hugging Face viewer under refs/convert/parquet revision:Parquet fileSimulate a DuckDB table creation from the first row of the parquet fileimport duckdbcon = duckdb.connect()con.execute(f"CREATE TABLE data as SELECT * FROM '{first_parquet_url}' LIMIT 1;")result = con.sql("SELECT sql FROM duckdb_tables() where table_name ='data';").df()ddl_create = result.iloc[0,0]con.close()The CREATE schema DDL is:CREATE TABLE "data"(city VARCHAR, country VARCHAR, region VARCHAR,continent VARCHAR, latitude DOUBLE, longitude DOUBLE, x DOUBLE, y DOUBLE, z DOUBLE);And, as you can see, it matches the columns in the dataset viewer:Dataset columnsNow, we can construct the prompt with the ddl_create and the query inputprompt = """### Instruction:Your task is to generate valid duckdb SQL to answer the following question.### Input:Here is the database schema that the SQL query will run on:{ddl_create}### Question:{query_input}### Response (use duckdb shorthand if possible):"""If the user wants to know the Cities from Albania country, the prompt will look like this:query = "Cities from Albania country"prompt = prompt.format(ddl_create=ddl_create, query_input=query)So the expanded prompt that will be sent to the LLM looks like this:### Instruction:Your task is to generate valid duckdb SQL to answer the following question.### Input:Here is the database schema that the SQL query will run on:CREATE TABLE "data"(city VARCHAR, country VARCHAR, region VARCHAR, continent VARCHAR, latitude DOUBLE, longitude DOUBLE, x DOUBLE, y DOUBLE, z DOUBLE);### Question:Cities from Albania country### Response (use duckdb shorthand if possible):It is time to send the prompt to the modelfrom llama_cpp import Llamallm = Llama(model_path="DuckDB-NSQL-7B-v0.1-q8_0.gguf",n_ctx=2048,n_gpu_layers=50)pred = llm(prompt, temperature=0.1, max_tokens=1000)sql_output = pred["choices"][0]["text"]The output SQL command will point to a data table, but since we don't have a real table but just a reference to the parquet file, we will replace all data occurrences by the first_parquet_url:sql_output = sql_output.replace("FROM data", f"FROM '{first_parquet_url}'")And the final output will be:SELECT city FROM 'https://huggingface.co/api/datasets/jamescalam/world-cities-geo/parquet/default/train/0.parquet' WHERE country = 'Albania'Now, it is time to finally execute our generated SQL directly in the dataset, so, lets use once again DuckDB powers:con = duckdb.connect()try:query_result = con.sql(sql_output).df()except Exception as error:print(f"❌ Could not execute SQL query {error=}")finally:con.close()And here we have the results (100 rows):Execution result (100 rows)Let's compare this result with the dataset viewer using the "search function" for Albania country, it should be the same:Search result for Albania countryYou can also get the same result calling directly to the search or filter API:Using /search APIimport requestsAPI_URL = "https://datasets-server.huggingface.co/search?dataset=jamescalam/world-cities-geo&config=default&split=train&query=Albania"def query():response = requests.get(API_URL)return response.json()data = query()Using filter APIimport requestsAPI_URL = "https://datasets-server.huggingface.co/filter?dataset=jamescalam/world-cities-geo&config=default&split=train&where=country='Albania'"def query():response = requests.get(API_URL)return response.json()data = query()Our final demo will be a Hugging Face space that looks like this:You can see the notebook with the code here.And the Hugging Face Space here
https://huggingface.co/blog/deep-rl-dqn
Deep Q-Learning with Space Invaders
Thomas Simonini
June 7, 2022
Unit 3, of the Deep Reinforcement Learning Class with Hugging Face 🤗⚠️ A new updated version of this article is available here 👉 https://huggingface.co/deep-rl-course/unit1/introductionThis article is part of the Deep Reinforcement Learning Class. A free course from beginner to expert. Check the syllabus here.⚠️ A new updated version of this article is available here 👉 https://huggingface.co/deep-rl-course/unit1/introductionThis article is part of the Deep Reinforcement Learning Class. A free course from beginner to expert. Check the syllabus here.In the last unit, we learned our first reinforcement learning algorithm: Q-Learning, implemented it from scratch, and trained it in two environments, FrozenLake-v1 ☃️ and Taxi-v3 🚕.We got excellent results with this simple algorithm. But these environments were relatively simple because the State Space was discrete and small (14 different states for FrozenLake-v1 and 500 for Taxi-v3).But as we'll see, producing and updating a Q-table can become ineffective in large state space environments.So today, we'll study our first Deep Reinforcement Learning agent: Deep Q-Learning. Instead of using a Q-table, Deep Q-Learning uses a Neural Network that takes a state and approximates Q-values for each action based on that state.And we'll train it to play Space Invaders and other Atari environments using RL-Zoo, a training framework for RL using Stable-Baselines that provides scripts for training, evaluating agents, tuning hyperparameters, plotting results, and recording videos.So let’s get started! 🚀To be able to understand this unit, you need to understand Q-Learning first.From Q-Learning to Deep Q-LearningThe Deep Q NetworkPreprocessing the input and temporal limitationThe Deep Q-Learning AlgorithmExperience Replay to make more efficient use of experiences Fixed Q-Target to stabilize the trainingDouble DQNFrom Q-Learning to Deep Q-LearningWe learned that Q-Learning is an algorithm we use to train our Q-Function, an action-value function that determines the value of being at a particular state and taking a specific action at that state.Given a state and action, our Q Function outputs a state-action value (also called Q-value) The Q comes from "the Quality" of that action at that state.Internally, our Q-function has a Q-table, a table where each cell corresponds to a state-action pair value. Think of this Q-table as the memory or cheat sheet of our Q-function.The problem is that Q-Learning is a tabular method. Aka, a problem in which the state and actions spaces are small enough to approximate value functions to be represented as arrays and tables. And this is not scalable.Q-Learning was working well with small state space environments like:FrozenLake, we had 14 states.Taxi-v3, we had 500 states.But think of what we're going to do today: we will train an agent to learn to play Space Invaders using the frames as input.As Nikita Melkozerov mentioned, Atari environments have an observation space with a shape of (210, 160, 3), containing values ranging from 0 to 255 so that gives us 256^(210x160x3) = 256^100800 (for comparison, we have approximately 10^80 atoms in the observable universe).Therefore, the state space is gigantic; hence creating and updating a Q-table for that environment would not be efficient. In this case, the best idea is to approximate the Q-values instead of a Q-table using a parametrized Q-function Qθ(s,a)Q_{\theta}(s,a)Qθ​(s,a) .This neural network will approximate, given a state, the different Q-values for each possible action at that state. And that's exactly what Deep Q-Learning does.Now that we understand Deep Q-Learning, let's dive deeper into the Deep Q-Network.The Deep Q-Network (DQN)This is the architecture of our Deep Q-Learning network:As input, we take a stack of 4 frames passed through the network as a state and output a vector of Q-values for each possible action at that state. Then, like with Q-Learning, we just need to use our epsilon-greedy policy to select which action to take.When the Neural Network is initialized, the Q-value estimation is terrible. But during training, our Deep Q-Network agent will associate a situation with appropriate action and learn to play the game well.Preprocessing the input and temporal limitationWe mentioned that we preprocess the input. It’s an essential step since we want to reduce the complexity of our state to reduce the computation time needed for training.So what we do is reduce the state space to 84x84 and grayscale it (since the colors in Atari environments don't add important information).This is an essential saving since we reduce our three color channels (RGB) to 1.We can also crop a part of the screen in some games if it does not contain important information.Then we stack four frames together.Why do we stack four frames together?We stack frames together because it helps us handle the problem of temporal limitation. Let’s take an example with the game of Pong. When you see this frame:Can you tell me where the ball is going?No, because one frame is not enough to have a sense of motion! But what if I add three more frames? Here you can see that the ball is going to the right.That’s why, to capture temporal information, we stack four frames together.Then, the stacked frames are processed by three convolutional layers. These layers allow us to capture and exploit spatial relationships in images. But also, because frames are stacked together, you can exploit some spatial properties across those frames.Finally, we have a couple of fully connected layers that output a Q-value for each possible action at that state.So, we see that Deep Q-Learning is using a neural network to approximate, given a state, the different Q-values for each possible action at that state. Let’s now study the Deep Q-Learning algorithm.The Deep Q-Learning AlgorithmWe learned that Deep Q-Learning uses a deep neural network to approximate the different Q-values for each possible action at a state (value-function estimation).The difference is that, during the training phase, instead of updating the Q-value of a state-action pair directly as we have done with Q-Learning:In Deep Q-Learning, we create a Loss function between our Q-value prediction and the Q-target and use Gradient Descent to update the weights of our Deep Q-Network to approximate our Q-values better.The Deep Q-Learning training algorithm has two phases:Sampling: we perform actions and store the observed experiences tuples in a replay memory.Training: Select the small batch of tuple randomly and learn from it using a gradient descent update step.But, this is not the only change compared with Q-Learning. Deep Q-Learning training might suffer from instability, mainly because of combining a non-linear Q-value function (Neural Network) and bootstrapping (when we update targets with existing estimates and not an actual complete return).To help us stabilize the training, we implement three different solutions:Experience Replay, to make more efficient use of experiences.Fixed Q-Target to stabilize the training.Double Deep Q-Learning, to handle the problem of the overestimation of Q-values.Experience Replay to make more efficient use of experiencesWhy do we create a replay memory?Experience Replay in Deep Q-Learning has two functions:Make more efficient use of the experiences during the training.Experience replay helps us make more efficient use of the experiences during the training. Usually, in online reinforcement learning, we interact in the environment, get experiences (state, action, reward, and next state), learn from them (update the neural network) and discard them.But with experience replay, we create a replay buffer that saves experience samples that we can reuse during the training.⇒ This allows us to learn from individual experiences multiple times.Avoid forgetting previous experiences and reduce the correlation between experiences.The problem we get if we give sequential samples of experiences to our neural network is that it tends to forget the previous experiences as it overwrites new experiences. For instance, if we are in the first level and then the second, which is different, our agent can forget how to behave and play in the first level.The solution is to create a Replay Buffer that stores experience tuples while interacting with the environment and then sample a small batch of tuples. This prevents the network from only learning about what it has immediately done.Experience replay also has other benefits. By randomly sampling the experiences, we remove correlation in the observation sequences and avoid action values from oscillating or diverging catastrophically.In the Deep Q-Learning pseudocode, we see that we initialize a replay memory buffer D from capacity N (N is an hyperparameter that you can define). We then store experiences in the memory and sample a minibatch of experiences to feed the Deep Q-Network during the training phase.Fixed Q-Target to stabilize the trainingWhen we want to calculate the TD error (aka the loss), we calculate the difference between the TD target (Q-Target) and the current Q-value (estimation of Q).But we don’t have any idea of the real TD target. We need to estimate it. Using the Bellman equation, we saw that the TD target is just the reward of taking that action at that state plus the discounted highest Q value for the next state.However, the problem is that we are using the same parameters (weights) for estimating the TD target and the Q value. Consequently, there is a significant correlation between the TD target and the parameters we are changing.Therefore, it means that at every step of training, our Q values shift but also the target value shifts. So, we’re getting closer to our target, but the target is also moving. It’s like chasing a moving target! This led to a significant oscillation in training.It’s like if you were a cowboy (the Q estimation) and you want to catch the cow (the Q-target), you must get closer (reduce the error).At each time step, you’re trying to approach the cow, which also moves at each time step (because you use the same parameters).This leads to a bizarre path of chasing (a significant oscillation in training).Instead, what we see in the pseudo-code is that we:Use a separate network with a fixed parameter for estimating the TD TargetCopy the parameters from our Deep Q-Network at every C step to update the target network.Double DQNDouble DQNs, or Double Learning, were introduced by Hado van Hasselt. This method handles the problem of the overestimation of Q-values.To understand this problem, remember how we calculate the TD Target:We face a simple problem by calculating the TD target: how are we sure that the best action for the next state is the action with the highest Q-value?We know that the accuracy of Q values depends on what action we tried and what neighboring states we explored.Consequently, we don’t have enough information about the best action to take at the beginning of the training. Therefore, taking the maximum Q value (which is noisy) as the best action to take can lead to false positives. If non-optimal actions are regularly given a higher Q value than the optimal best action, the learning will be complicated.The solution is: when we compute the Q target, we use two networks to decouple the action selection from the target Q value generation. We:Use our DQN network to select the best action to take for the next state (the action with the highest Q value).Use our Target network to calculate the target Q value of taking that action at the next state.Therefore, Double DQN helps us reduce the overestimation of q values and, as a consequence, helps us train faster and have more stable learning.Since these three improvements in Deep Q-Learning, many have been added such as Prioritized Experience Replay, Dueling Deep Q-Learning. They’re out of the scope of this course but if you’re interested, check the links we put in the reading list. 👉 https://github.com/huggingface/deep-rl-class/blob/main/unit3/README.mdNow that you've studied the theory behind Deep Q-Learning, you’re ready to train your Deep Q-Learning agent to play Atari Games. We'll start with Space Invaders, but you'll be able to use any Atari game you want 🔥 We're using the RL-Baselines-3 Zoo integration, a vanilla version of Deep Q-Learning with no extensions such as Double-DQN, Dueling-DQN, and Prioritized Experience Replay.Start the tutorial here 👉 https://colab.research.google.com/github/huggingface/deep-rl-class/blob/main/unit3/unit3.ipynbThe leaderboard to compare your results with your classmates 🏆 👉 https://huggingface.co/spaces/chrisjay/Deep-Reinforcement-Learning-LeaderboardCongrats on finishing this chapter! There was a lot of information. And congrats on finishing the tutorial. You’ve just trained your first Deep Q-Learning agent and shared it on the Hub 🥳.That’s normal if you still feel confused with all these elements. This was the same for me and for all people who studied RL.Take time to really grasp the material before continuing.Don't hesitate to train your agent in other environments (Pong, Seaquest, QBert, Ms Pac Man). The best way to learn is to try things on your own!We published additional readings in the syllabus if you want to go deeper 👉 https://github.com/huggingface/deep-rl-class/blob/main/unit3/README.mdIn the next unit, we’re going to learn about Policy Gradients methods.And don't forget to share with your friends who want to learn 🤗 !Finally, we want to improve and update the course iteratively with your feedback. If you have some, please fill this form 👉 https://forms.gle/3HgA7bEHwAmmLfwh9Keep learning, stay awesome,
https://huggingface.co/blog/bloom
🌸 Introducing The World's Largest Open Multilingual Language Model: BLOOM 🌸
BigScience Workshop
July 12, 2022
Introducing The World's Largest Open Multilingual Language Model: BLOOMHugging FaceModelsDatasetsSpacesPostsDocsSolutionsPricingLog InSign UpBack to Articles🌸 Introducing The World's Largest Open Multilingual Language Model: BLOOM 🌸
https://huggingface.co/blog/leaderboard-artificial-analysis
Bringing the Artificial Analysis LLM Performance Leaderboard to Hugging Face
Micah Hill-Smith, George Cameron, Clémentine Fourrier
May 3, 2024
Building applications with LLMs requires considering more than just quality: for many use-cases, speed and price are equally or more important. For consumer applications and chat experiences, speed and responsiveness are critical to user engagement. Users expect near-instant responses, and delays can directly lead to reduced engagement. When building more complex applications involving tool use or agentic systems, speed and cost become even more important, and can become the limiting factor on overall system capability. The time taken by sequential requests to LLMs can quickly stack up for each user request adding to the cost. This is why Artificial Analysis (@ArtificialAnlys) developed a leaderboard evaluating price, speed and quality across >100 serverless LLM API endpoints, now coming to Hugging Face.Find the leaderboard here!The LLM Performance Leaderboard The LLM Performance Leaderboard aims to provide comprehensive metrics to help AI engineers make decisions on which LLMs (both open & proprietary) and API providers to use in AI-enabled applications.When making decisions regarding which AI technologies to use, engineers need to consider quality, price and speed (latency & throughput). The LLM Performance Leaderboard brings all three together to enable decision making in one place across both proprietary & open models. Source: LLM Performance LeaderboardMetric coverage The metrics reported are:Quality: a simplified index for comparing model quality and accuracy, calculated based on metrics such as MMLU, MT-Bench, HumanEval scores, as reported by the model authors, and Chatbot Arena ranking.Context window: the maximum number of tokens an LLM can work with at any one time (including both input and output tokens).Pricing: the prices charged by a provider to query the model for inference. We report input/output per-token pricing, as well as "blended" pricing to compare hosting providers with a single metric. We blend input and output pricing at a 3:1 ratio (i.e., an assumption that the length of input is 3x longer than the output).Throughput: how fast an endpoint outputs tokens during inference, measured in tokens per second (often referred to as tokens/s or "TPS"). We report the median, P5, P25, P75 and P95 values measured over the prior 14 days.Latency: how long the endpoint takes to respond after the request has been sent, known as Time to First Token ("TTFT") and measured in seconds. We report the median, P5, P25, P75 and P95 values measured over the prior 14 days.For further definitions, see our full methodology page. Test Workloads The leaderboard allows exploration of performance under several different workloads (6 combinations in total):varying the prompt length: ~100 tokens, ~1k tokens, ~10k tokens.running parallel queries: 1 query, 10 parallel queries.Methodology We test every API endpoint on the leaderboard 8 times per day, and leaderboard figures represent the median measurement of the last 14 days. We also have percentile breakdowns within the collapsed tabs.Quality metrics are currently collected on a per-model basis and show results reports by model creators, but watch this space as we begin to share results from our independent quality evaluations across each endpoint. For further definitions, see our full methodology page.Highlights (May 2024, see the leaderboard for the latest) The language models market has exploded in complexity over the last year. Launches that have shaken up the market just within the last two months include proprietary models like Anthropic's Claude 3 series and open models such as Databricks' DBRX, Cohere's Command R Plus, Google's Gemma, Microsoft's Phi-3, Mistral's Mixtral 8x22B and Meta's Llama 3.Price and speed vary considerably between models and providers. From Claude 3 Opus to Llama 3 8B, there is a 300x pricing spread - that's more than two orders of magnitude!API providers have increased the speed of launching models. Within 48 hours, 7 providers were offering the Llama 3 models. Speaking to the demand for new, open-source models and the competitive dynamics between API providers.Key models to highlight across quality segments:High quality, typically higher price & slower: GPT-4 Turbo and Claude 3 OpusModerate quality, price & speed: Llama 3 70B, Mixtral 8x22B, Command R+, Gemini 1.5 Pro, DBRXLower quality, but with much faster speed and lower pricing available: Llama 3 8B, Claude 3 Haiku, Mixtral 8x7BOur chart of Quality vs. Throughput (tokens/s) shows the range of options with different quality and performance characteristics. Source: artificialanalysis.ai/modelsUse Case Example: Speed and Price can be as important as Quality In some cases, design patterns involving multiple requests with faster and cheaper models can result in not only lower cost but better overall system quality compared to using a single larger model. For example, consider a chatbot that needs to browse the web to find relevant information from recent news articles. One approach would be to use a large, high-quality model like GPT-4 Turbo to run a search then read and process the top handful of articles. Another would be to use a smaller, faster model like Llama 3 8B to read and extract highlights from dozens web pages in parallel, and then use GPT-4 Turbo to assess and summarize the most relevant results. The second approach will be more cost effective, even after accounting for reading 10x more content, and may result in higher quality results. Get in touch Please follow us on Twitter and LinkedIn for updates. We're available via message on either, as well as on our website and via email.
https://huggingface.co/blog/eu-ai-act-oss
AI Policy @🤗: Open ML Considerations in the EU AI Act
Yacine Jernite
July 24, 2023
AI Policy @🤗: Open ML Considerations in the EU AI ActHugging FaceModelsDatasetsSpacesPostsDocsSolutionsPricingLog InSign UpBack to ArticlesAI Policy @🤗: Open ML Considerations in the EU AI Act
https://huggingface.co/blog/os-llms
Open-Source Text Generation & LLM Ecosystem at Hugging Face
Merve Noyan
July 17, 2023
[Updated on July 24, 2023: Added Llama 2.]Text generation and conversational technologies have been around for ages. Earlier challenges in working with these technologies were controlling both the coherence and diversity of the text through inference parameters and discriminative biases. More coherent outputs were less creative and closer to the original training data and sounded less human. Recent developments overcame these challenges, and user-friendly UIs enabled everyone to try these models out. Services like ChatGPT have recently put the spotlight on powerful models like GPT-4 and caused an explosion of open-source alternatives like Llama to go mainstream. We think these technologies will be around for a long time and become more and more integrated into everyday products. This post is divided into the following sections:Brief background on text generationLicensingTools in the Hugging Face Ecosystem for LLM ServingParameter Efficient Fine Tuning (PEFT) Brief Background on Text Generation Text generation models are essentially trained with the objective of completing an incomplete text or generating text from scratch as a response to a given instruction or question. Models that complete incomplete text are called Causal Language Models, and famous examples are GPT-3 by OpenAI and Llama by Meta AI. One concept you need to know before we move on is fine-tuning. This is the process of taking a very large model and transferring the knowledge contained in this base model to another use case, which we call a downstream task. These tasks can come in the form of instructions. As the model size grows, it can generalize better to instructions that do not exist in the pre-training data, but were learned during fine-tuning.Causal language models are adapted using a process called reinforcement learning from human feedback (RLHF). This optimization is mainly made over how natural and coherent the text sounds rather than the validity of the answer. Explaining how RLHF works is outside the scope of this blog post, but you can find more information about this process here.For example, GPT-3 is a causal language base model, while the models in the backend of ChatGPT (which is the UI for GPT-series models) are fine-tuned through RLHF on prompts that can consist of conversations or instructions. It’s an important distinction to make between these models. On the Hugging Face Hub, you can find both causal language models and causal language models fine-tuned on instructions (which we’ll give links to later in this blog post). Llama is one of the first open-source LLMs to have outperformed/matched closed-source ones. A research group led by Together has created a reproduction of Llama's dataset, called Red Pajama, and trained LLMs and instruction fine-tuned models on it. You can read more about it here and find the model checkpoints on Hugging Face Hub. By the time this blog post is written, three of the largest causal language models with open-source licenses are MPT-30B by MosaicML, XGen by Salesforce and Falcon by TII UAE, available completely open on Hugging Face Hub.Recently, Meta released Llama 2, an open-access model with a license that allows commercial use. As of now, Llama 2 outperforms all of the other open-source large language models on different benchmarks. Llama 2 checkpoints on Hugging Face Hub are compatible with transformers, and the largest checkpoint is available for everyone to try at HuggingChat. You can read more about how to fine-tune, deploy and prompt with Llama 2 in this blog post.The second type of text generation model is commonly referred to as the text-to-text generation model. These models are trained on text pairs, which can be questions and answers or instructions and responses. The most popular ones are T5 and BART (which, as of now, aren’t state-of-the-art). Google has recently released the FLAN-T5 series of models. FLAN is a recent technique developed for instruction fine-tuning, and FLAN-T5 is essentially T5 fine-tuned using FLAN. As of now, the FLAN-T5 series of models are state-of-the-art and open-source, available on the Hugging Face Hub. Note that these are different from instruction-tuned causal language models, although the input-output format might seem similar. Below you can see an illustration of how these models work.Having more variation of open-source text generation models enables companies to keep their data private, to adapt models to their domains faster, and to cut costs for inference instead of relying on closed paid APIs. All open-source causal language models on Hugging Face Hub can be found here, and text-to-text generation models can be found here. Models created with love by Hugging Face with BigScience and BigCode 💗 Hugging Face has co-led two science initiatives, BigScience and BigCode. As a result of them, two large language models were created, BLOOM 🌸 and StarCoder 🌟. BLOOM is a causal language model trained on 46 languages and 13 programming languages. It is the first open-source model to have more parameters than GPT-3. You can find all the available checkpoints in the BLOOM documentation.StarCoder is a language model trained on permissive code from GitHub (with 80+ programming languages 🤯) with a Fill-in-the-Middle objective. It’s not fine-tuned on instructions, and thus, it serves more as a coding assistant to complete a given code, e.g., translate Python to C++, explain concepts (what’s recursion), or act as a terminal. You can try all of the StarCoder checkpoints in this application. It also comes with a VSCode extension.Snippets to use all models mentioned in this blog post are given in either the model repository or the documentation page of that model type in Hugging Face. Licensing Many text generation models are either closed-source or the license limits commercial use. Fortunately, open-source alternatives are starting to appear and being embraced by the community as building blocks for further development, fine-tuning, or integration with other projects. Below you can find a list of some of the large causal language models with fully open-source licenses:Falcon 40BXGenMPT-30BPythia-12BRedPajama-INCITE-7BOpenAssistant (Falcon variant)There are two code generation models, StarCoder by BigCode and Codegen by Salesforce. There are model checkpoints in different sizes and open-source or open RAIL licenses for both, except for Codegen fine-tuned on instruction.The Hugging Face Hub also hosts various models fine-tuned for instruction or chat use. They come in various styles and sizes depending on your needs.MPT-30B-Chat, by Mosaic ML, uses the CC-BY-NC-SA license, which does not allow commercial use. However, MPT-30B-Instruct uses CC-BY-SA 3.0, which can be used commercially.Falcon-40B-Instruct and Falcon-7B-Instruct both use the Apache 2.0 license, so commercial use is also permitted.Another popular family of models is OpenAssistant, some of which are built on Meta's Llama model using a custom instruction-tuning dataset. Since the original Llama model can only be used for research, the OpenAssistant checkpoints built on Llama don’t have full open-source licenses. However, there are OpenAssistant models built on open-source models like Falcon or pythia that use permissive licenses.StarChat Beta is the instruction fine-tuned version of StarCoder, and has BigCode Open RAIL-M v1 license, which allows commercial use. Instruction-tuned coding model of Salesforce, XGen model, only allows research use.If you're looking to fine-tune a model on an existing instruction dataset, you need to know how a dataset was compiled. Some of the existing instruction datasets are either crowd-sourced or use outputs of existing models (e.g., the models behind ChatGPT). ALPACA dataset created by Stanford is created through the outputs of models behind ChatGPT. Moreover, there are various crowd-sourced instruction datasets with open-source licenses, like oasst1 (created by thousands of people voluntarily!) or databricks/databricks-dolly-15k. If you'd like to create a dataset yourself, you can check out the dataset card of Dolly on how to create an instruction dataset. Models fine-tuned on these datasets can be distributed. You can find a comprehensive table of some open-source/open-access models below. ModelDatasetLicenseUseFalcon 40BFalcon RefinedWebApache-2.0Text GenerationSalesForce XGen 7BMix of C4, RedPajama and moreApache-2.0Text GenerationMPT-30BMix of C4, RedPajama and moreApache-2.0Text GenerationPythia-12BPileApache-2.0Text GenerationRedPajama INCITE 7BRedPajamaApache-2.0Text GenerationOpenAssistant Falcon 40Boasst1 and DollyApache-2.0Text GenerationStarCoderThe StackBigCode OpenRAIL-MCode GenerationSalesforce CodeGenStarcoder DataApache-2.0Code GenerationFLAN-T5-XXLgsm8k, lambada, and esnliApache-2.0Text-to-text GenerationMPT-30B ChatShareGPT-Vicuna, OpenAssistant Guanaco and moreCC-By-NC-SA-4.0ChatMPT-30B Instructduorc, competition_math, dolly_hhrlhfCC-By-SA-3.0InstructionFalcon 40B InstructbaizeApache-2.0InstructionDolly v2DollyMITText GenerationStarChat-βOpenAssistant GuanacoBigCode OpenRAIL-MCode InstructionLlama 2Undisclosed datasetCustom Meta License (Allows commercial use)Text Generation Tools in the Hugging Face Ecosystem for LLM Serving Text Generation Inference Response time and latency for concurrent users are a big challenge for serving these large models. To tackle this problem, Hugging Face has released text-generation-inference (TGI), an open-source serving solution for large language models built on Rust, Python, and gRPc. TGI is integrated into inference solutions of Hugging Face, Inference Endpoints, and Inference API, so you can directly create an endpoint with optimized inference with few clicks, or simply send a request to Hugging Face's Inference API to benefit from it, instead of integrating TGI to your platform. TGI currently powers HuggingChat, Hugging Face's open-source chat UI for LLMs. This service currently uses one of OpenAssistant's models as the backend model. You can chat as much as you want with HuggingChat and enable the Web search feature for responses that use elements from current Web pages. You can also give feedback to each response for model authors to train better models. The UI of HuggingChat is also open-sourced, and we are working on more features for HuggingChat to allow more functions, like generating images inside the chat. Recently, a Docker template for HuggingChat was released for Hugging Face Spaces. This allows anyone to deploy their instance based on a large language model with only a few clicks and customize it. You can create your large language model instance here based on various LLMs, including Llama 2. How to find the best model? Hugging Face hosts an LLM leaderboard. This leaderboard is created by evaluating community-submitted models on text generation benchmarks on Hugging Face’s clusters. If you can’t find the language or domain you’re looking for, you can filter them here.You can also check out the LLM Performance leaderboard, which aims to evaluate the latency and throughput of large language models available on Hugging Face Hub. Parameter Efficient Fine Tuning (PEFT) If you’d like to fine-tune one of the existing large models on your instruction dataset, it is nearly impossible to do so on consumer hardware and later deploy them (since the instruction models are the same size as the original checkpoints that are used for fine-tuning). PEFT is a library that allows you to do parameter-efficient fine-tuning techniques. This means that rather than training the whole model, you can train a very small number of additional parameters, enabling much faster training with very little performance degradation. With PEFT, you can do low-rank adaptation (LoRA), prefix tuning, prompt tuning, and p-tuning.You can check out further resources for more information on text generation.Further ResourcesTogether with AWS we released TGI-based LLM deployment deep learning containers called LLM Inference Containers. Read about them here.Text Generation task page to find out more about the task itself.PEFT announcement blog post.Read about how Inference Endpoints use TGI here.Read about how to fine-tune Llama 2 transformers and PEFT, and prompt here.
https://huggingface.co/blog/introducing-doi
Introducing DOI: the Digital Object Identifier to Datasets and Models
Sasha Luccioni, Sylvestre Bcht, Christopher Akiki, Alix Leroy
October 7, 2022
Our mission at Hugging Face is to democratize good machine learning. That includes best practices that make ML models and datasets more reproducible, better documented, and easier to use and share.To solve this challenge, we're excited to announce that you can now generate a DOI for your model or dataset directly from the Hub!DOIs can be generated directly from your repo settings, and anyone will then be able to cite your work by clicking "Cite this model/dataset" on your model or dataset page 🔥.DOIs in a nutshell and why do they matter?DOIs (Digital Object Identifiers) are strings uniquely identifying a digital object, anything from articles to figures, including datasets and models. DOIs are tied to object metadata, including the object's URL, version, creation date, description, etc. They are a commonly accepted reference to digital resources across research and academic communities; they are analogous to a book's ISBN.DOIs make finding information about a model or dataset easier and sharing them with the world via a permanent link that will never expire or change. As such, datasets/models with DOIs are intended to persist perpetually and may only be deleted upon filing a request with our support.How are DOIs being assigned by Hugging Face?We have partnered with DataCite to allow registered Hub users to request a DOI for their model or dataset. Once they’ve filled out the necessary metadata, they receive a shiny new DOI 🌟!If ever there’s a new version of a model or dataset, the DOI can easily be updated, and the previous version of the DOI gets outdated. This makes it easy to refer to a specific version of an object, even if it has changed.Have ideas for more improvements we can make? Many features, just like this, come directly from community feedback. Please drop us a note or tweet us at @HuggingFace to share yours or open an issue on huggingface/hub-docs 🤗Thanks DataCite team for this partnership! Thanks also Alix Leroy, Bram Vanroy, Daniel van Strien and Yoshitomo Matsubara for starting and fostering the discussion on this hub-docs GitHub issue.
https://huggingface.co/blog/gradio-blocks
Gradio 3.0 is Out!
Abubakar Abid
May 16, 2022
Machine learning demos are an increasingly vital part of releasing a model. Demos allow anyone — not just ML engineers — to try out a model in the browser, give feedback on predictions, and build trust in the model if it performs well. More than 600,000 ML demos have been built with the Gradio library since its first version in 2019, and today, we are thrilled to announce Gradio 3.0: a ground-up redesign of the Gradio library 🥳 What's New in Gradio 3.0? 🔥 A complete redesign of the frontend, based on the feedback we're hearing from Gradio users:We've switched to modern technologies (like Svelte) to build the Gradio frontend. We're seeing much smaller payloads and much faster page loads as a result! We've also embranced a much cleaner design that will allow Gradio demos to fit in visually in more settings (such as being embedded in blog posts).We've revamped our existing components, like Dataframe to be more user-friendly (try dragging-and-dropping a CSV file into a Dataframe) as well as added new components, such as the Gallery, to allow you to build the right UI for your model.We've added a TabbedInterface class which allows you to group together related demos as multiple tabs in one web appCheck out all the components you can use on our (redesigned) docs 🤗!🔥 We've created a new low-level language called Gradio Blocks that lets you build complex custom web apps, right in Python:Why did we create Blocks? Gradio demos are very easy to build, but what if you want more control over the layout of your demo, or more flexibility on how the data flows? For example, you might want to:Change the layout of your demo instead of just having all of the inputs on the left and outputs on the rightHave multi-step interfaces, in which the output of one model becomes the input to the next model, or have more flexible data flows in generalChange a component's properties (for example, the choices in a Dropdown) or its visibilty based on user inputThe low-level Blocks API allows you to do all of this, right in Python.Here's an example of a Blocks demo that creates two simple demos and uses tabs to group them together:import numpy as npimport gradio as grdef flip_text(x): return x[::-1]def flip_image(x): return np.fliplr(x)with gr.Blocks() as demo: gr.Markdown("Flip text or image files using this demo.") with gr.Tabs(): with gr.TabItem("Flip Text"): text_input = gr.Textbox() text_output = gr.Textbox() # this demo runs whenever the input textbox changes text_input.change(flip_text, inputs=text_input, outputs=text_output) with gr.TabItem("Flip Image"): with gr.Row():image_input = gr.Image()image_output = gr.Image() button = gr.Button("Flip") # this demo runs whenever the button is clicked button.click(flip_image, inputs=image_input, outputs=image_output) demo.launch()Once you run launch(), the following demo will appear:For a step-by-step introduction to Blocks, check out the dedicated Blocks Guide The Gradio Blocks Party We're very excited about Gradio Blocks -- and we'd love for you to try it out -- so we are organizing a competition, the Gradio Blocks Party (😉), to see who can build the best demos with Blocks. By building these demos, we can make state-of-the-art machine learning accessible, not just to engineers, but anyone who can use an Internet browser!Even if you've never used Gradio before, this is the perfect time to start, because the Blocks Party is running until the end of May. We'll be giving out 🤗 merch and other prizes at the end of the Party for demos built using Blocks.Learn more about Blocks Party here: https://huggingface.co/spaces/Gradio-Blocks/README
https://huggingface.co/blog/deep-rl-pg
Policy Gradient with PyTorch
Thomas Simonini
June 30, 2022
Unit 5, of the Deep Reinforcement Learning Class with Hugging Face 🤗⚠️ A new updated version of this article is available here 👉 https://huggingface.co/deep-rl-course/unit1/introductionThis article is part of the Deep Reinforcement Learning Class. A free course from beginner to expert. Check the syllabus here.⚠️ A new updated version of this article is available here 👉 https://huggingface.co/deep-rl-course/unit1/introductionThis article is part of the Deep Reinforcement Learning Class. A free course from beginner to expert. Check the syllabus here.In the last unit, we learned about Deep Q-Learning. In this value-based Deep Reinforcement Learning algorithm, we used a deep neural network to approximate the different Q-values for each possible action at a state.Indeed, since the beginning of the course, we only studied value-based methods, where we estimate a value function as an intermediate step towards finding an optimal policy.Because, in value-based, π exists only because of the action value estimates, since policy is just a function (for instance, greedy-policy) that will select the action with the highest value given a state.But, with policy-based methods, we want to optimize the policy directly without having an intermediate step of learning a value function.So today, we'll study our first Policy-Based method: Reinforce. And we'll implement it from scratch using PyTorch. Before testing its robustness using CartPole-v1, PixelCopter, and Pong.Let's get started,What are Policy-Gradient Methods?An Overview of Policy GradientsThe Advantages of Policy-Gradient MethodsThe Disadvantages of Policy-Gradient MethodsReinforce (Monte Carlo Policy Gradient)What are Policy-Gradient Methods?Policy-Gradient is a subclass of Policy-Based Methods, a category of algorithms that aims to optimize the policy directly without using a value function using different techniques. The difference with Policy-Based Methods is that Policy-Gradient methods are a series of algorithms that aim to optimize the policy directly by estimating the weights of the optimal policy using Gradient Ascent.An Overview of Policy GradientsWhy do we optimize the policy directly by estimating the weights of an optimal policy using Gradient Ascent in Policy Gradients Methods?Remember that reinforcement learning aims to find an optimal behavior strategy (policy) to maximize its expected cumulative reward.We also need to remember that a policy is a function that given a state, outputs, a distribution over actions (in our case using a stochastic policy).Our goal with Policy-Gradients is to control the probability distribution of actions by tuning the policy such that good actions (that maximize the return) are sampled more frequently in the future.Let’s take a simple example:We collect an episode by letting our policy interact with its environment.We then look at the sum of rewards of the episode (expected return). If this sum is positive, we consider that the actions taken during the episodes were good: Therefore, we want to increase the P(a|s) (probability of taking that action at that state) for each state-action pair.The Policy Gradient algorithm (simplified) looks like this:But Deep Q-Learning is excellent! Why use policy gradient methods?The Advantages of Policy-Gradient MethodsThere are multiple advantages over Deep Q-Learning methods. Let's see some of them:The simplicity of the integration: we can estimate the policy directly without storing additional data (action values).Policy gradient methods can learn a stochastic policy while value functions can't.This has two consequences:a. We don't need to implement an exploration/exploitation trade-off by hand. Since we output a probability distribution over actions, the agent explores the state space without always taking the same trajectory.b. We also get rid of the problem of perceptual aliasing. Perceptual aliasing is when two states seem (or are) the same but need different actions.Let's take an example: we have an intelligent vacuum cleaner whose goal is to suck the dust and avoid killing the hamsters.Our vacuum cleaner can only perceive where the walls are.The problem is that the two red cases are aliased states because the agent perceives an upper and lower wall for each.Under a deterministic policy, the policy will either move right when in a red state or move left. Either case will cause our agent to get stuck and never suck the dust.Under a value-based RL algorithm, we learn a quasi-deterministic policy ("greedy epsilon strategy"). Consequently, our agent can spend a lot of time before finding the dust.On the other hand, an optimal stochastic policy will randomly move left or right in grey states. Consequently, it will not be stuck and will reach the goal state with a high probability.Policy gradients are more effective in high-dimensional action spaces and continuous actions spacesIndeed, the problem with Deep Q-learning is that their predictions assign a score (maximum expected future reward) for each possible action, at each time step, given the current state.But what if we have an infinite possibility of actions?For instance, with a self-driving car, at each state, you can have a (near) infinite choice of actions (turning the wheel at 15°, 17.2°, 19,4°, honking, etc.). We'll need to output a Q-value for each possible action! And taking the max action of a continuous output is an optimization problem itself!Instead, with a policy gradient, we output a probability distribution over actions.The Disadvantages of Policy-Gradient MethodsNaturally, Policy Gradient methods have also some disadvantages:Policy gradients converge a lot of time on a local maximum instead of a global optimum.Policy gradient goes faster, step by step: it can take longer to train (inefficient).Policy gradient can have high variance (solution baseline).👉 If you want to go deeper on the why the advantages and disadvantages of Policy Gradients methods, you can check this video.Now that we have seen the big picture of Policy-Gradient and its advantages and disadvantages, let's study and implement one of them: Reinforce.Reinforce (Monte Carlo Policy Gradient)Reinforce, also called Monte-Carlo Policy Gradient, uses an estimated return from an entire episode to update the policy parameter θ\thetaθ.We have our policy π which has a parameter θ. This π, given a state, outputs a probability distribution of actions.Where πθ(at∣st)\pi_\theta(a_t|s_t)πθ​(at​∣st​) is the probability of the agent selecting action at from state st, given our policy.But how do we know if our policy is good? We need to have a way to measure it. To know that we define a score/objective function called J(θ)J(\theta)J(θ).The score function J is the expected return:Remember that policy gradient can be seen as an optimization problem. So we must find the best parameters (θ) to maximize the score function, J(θ).To do that we’re going to use the Policy Gradient Theorem. I’m not going to dive on the mathematical details but if you’re interested check this videoThe Reinforce algorithm works like this:Loop: Use the policy πθ\pi_\thetaπθ​ to collect an episode τ\tauτ Use the episode to estimate the gradient g^=∇θJ(θ)\hat{g} = abla_\theta J(\theta)g^​=∇θ​J(θ)Update the weights of the policy: θ←θ+αg^\theta \leftarrow \theta + \alpha \hat{g}θ←θ+αg^​The interpretation we can make is this one:∇θlogπθ(at∣st)abla_\theta log \pi_\theta(a_t|s_t)∇θ​logπθ​(at​∣st​) is the direction of steepest increase of the (log) probability of selecting action at from state st.=> This tells use how we should change the weights of policy if we want to increase/decrease the log probability of selecting action at at state st.R(τ)R(\tau)R(τ): is the scoring function:If the return is high, it will push up the probabilities of the (state, action) combinations.Else, if the return is low, it will push down the probabilities of the (state, action) combinations.Now that we studied the theory behind Reinforce, you’re ready to code your Reinforce agent with PyTorch. And you'll test its robustness using CartPole-v1, PixelCopter, and Pong.Start the tutorial here 👉 https://colab.research.google.com/github/huggingface/deep-rl-class/blob/main/unit5/unit5.ipynbThe leaderboard to compare your results with your classmates 🏆 👉 https://huggingface.co/spaces/chrisjay/Deep-Reinforcement-Learning-LeaderboardCongrats on finishing this chapter! There was a lot of information. And congrats on finishing the tutorial. You’ve just coded your first Deep Reinforcement Learning agent from scratch using PyTorch and shared it on the Hub 🥳.It's normal if you still feel confused with all these elements. This was the same for me and for all people who studied RL.Take time to really grasp the material before continuing.Don't hesitate to train your agent in other environments. The best way to learn is to try things on your own!We published additional readings in the syllabus if you want to go deeper 👉 https://github.com/huggingface/deep-rl-class/blob/main/unit5/README.mdIn the next unit, we’re going to learn about a combination of Policy-Based and Value-based methods called Actor Critic Methods.And don't forget to share with your friends who want to learn 🤗!Finally, we want to improve and update the course iteratively with your feedback. If you have some, please fill this form 👉 https://forms.gle/3HgA7bEHwAmmLfwh9Keep learning, stay awesome 🤗,
https://huggingface.co/blog/tapex
Efficient Table Pre-training without Real Data: An Introduction to TAPEX
Qian Liu
May 23, 2022
In recent years, language model pre-training has achieved great success via leveraging large-scale textual data. By employing pre-training tasks such as masked language modeling, these models have demonstrated surprising performance on several downstream tasks. However, the dramatic gap between the pre-training task (e.g., language modeling) and the downstream task (e.g., table question answering) makes existing pre-training not efficient enough. In practice, we often need an extremely large amount of pre-training data to obtain promising improvement, even for domain-adaptive pretraining. How might we design a pre-training task to close the gap, and thus accelerate pre-training?OverviewIn "TAPEX: Table Pre-training via Learning a Neural SQL Executor", we explore using synthetic data as a proxy for real data during pre-training, and demonstrate its powerfulness with TAPEX (Table Pre-training via Execution) as an example. In TAPEX, we show that table pre-training can be achieved by learning a neural SQL executor over a synthetic corpus.Note: [Table] is a placeholder for the user provided table in the input.As shown in the figure above, by systematically sampling executable SQL queries and their execution outputs over tables, TAPEX first synthesizes a synthetic and non-natural pre-training corpus. Then, it continues to pre-train a language model (e.g., BART) to output the execution results of SQL queries, which mimics the process of a neural SQL executor.Pre-trainingThe following figure illustrates the pre-training process. At each step, we first take a table from the web. The example table is about Olympics Games. Then we can sample an executable SQL query SELECT City WHERE Country = France ORDER BY Year ASC LIMIT 1. Through an off-the-shelf SQL executor (e.g., MySQL), we can obtain the query’s execution result Paris. Similarly, by feeding the concatenation of the SQL query and the flattened table to the model (e.g., BART encoder) as input, the execution result serves as the supervision for the model (e.g., BART decoder) as output.Why use programs such as SQL queries rather than natural language sentences as a source for pre-training? The greatest advantage is that the diversity and scale of programs can be systematically guaranteed, compared to uncontrollable natural language sentences. Therefore, we can easily synthesize a diverse, large-scale, and high-quality pre-training corpus by sampling SQL queries.You can try the trained neural SQL executor in 🤗 Transformers as below:from transformers import TapexTokenizer, BartForConditionalGenerationimport pandas as pdtokenizer = TapexTokenizer.from_pretrained("microsoft/tapex-large-sql-execution")model = BartForConditionalGeneration.from_pretrained("microsoft/tapex-large-sql-execution")data = {"year": [1896, 1900, 1904, 2004, 2008, 2012],"city": ["athens", "paris", "st. louis", "athens", "beijing", "london"]}table = pd.DataFrame.from_dict(data)# tapex accepts uncased input since it is pre-trained on the uncased corpusquery = "select year where city = beijing"encoding = tokenizer(table=table, query=query, return_tensors="pt")outputs = model.generate(**encoding)print(tokenizer.batch_decode(outputs, skip_special_tokens=True))# ['2008']Fine-tuningDuring fine-tuning, we feed the concatenation of the natural language question and the flattened table to the model as input, the answer labeled by annotators serves as the supervision for the model as output. Want to fine-tune TAPEX by yourself? You can look at the fine-tuning script here, which has been officially integrated into 🤗 Transformers 4.19.0!And by now, all available TAPEX models have interactive widgets officially supported by Huggingface! You can try to answer some questions as below.Hosted inference API Table Question Answering Examples Compute Repository Stars Contributors Programming language Transformers36542651Python Datasets451277Python Tokenizers393434Rust, Python and NodeJS Add row Add col Reset table This model can be loaded on the Inference API on-demand. JSON Output Maximize ExperimentsWe evaluate TAPEX on four benchmark datasets, including WikiSQL (Weak), WikiTableQuestions, SQA and TabFact. The first three datasets are about table question answering, while the last one is about table fact verification, both requiring joint reasoning about tables and natural language. Below are some examples from the most challenging dataset, WikiTableQuestions:QuestionAnsweraccording to the table, what is the last title that spicy horse produced?Akaneiro: Demon Hunterswhat is the difference in runners-up from coleraine academical institution and royal school dungannon?20what were the first and last movies greenstreet acted in?The Maltese Falcon, Malayain which olympic games did arasay thondike not finish in the top 20?2012which broadcaster hosted 3 titles but they had only 1 episode?Channel 4Experimental results demonstrate that TAPEX outperforms previous table pre-training approaches by a large margin and ⭐achieves new state-of-the-art results on all of them⭐. This includes the improvements on the weakly-supervised WikiSQL denotation accuracy to 89.6% (+2.3% over SOTA, +3.8% over BART), the TabFact accuracy to 84.2% (+3.2% over SOTA, +3.0% over BART), the SQA denotation accuracy to 74.5% (+3.5% over SOTA, +15.9% over BART), and the WikiTableQuestion denotation accuracy to 57.5% (+4.8% over SOTA, +19.5% over BART). To our knowledge, this is the first work to exploit pre-training via synthetic executable programs and to achieve new state-of-the-art results on various downstream tasks.Comparison to Previous Table Pre-trainingThe earliest work on table pre-training, TAPAS from Google Research - also available in 🤗 Transformers - and TaBERT from Meta AI, have revealed that collecting more domain-adaptive data can improve the downstream performance. However, these previous works mainly employ general-purpose pre-training tasks, e.g., language modeling or its variants. TAPEX explores a different path by sacrificing the naturalness of the pre-trained source in order to obtain a domain-adaptive pre-trained task, i.e. SQL execution. A graphical comparison of BERT, TAPAS/TaBERT and our TAPEX can be seen below.We believe the SQL execution task is closer to the downstream table question answering task, especially from the perspective of structural reasoning capabilities. Imagine you are faced with a SQL query SELECT City ORDER BY Year and a natural question Sort all cities by year. The reasoning paths required by the SQL query and the question are similar, except that SQL is a bit more rigid than natural language. If a language model can be pre-trained to faithfully “execute” SQL queries and produce correct results, it should have a deep understanding on natural language with similar intents.What about the efficiency? How efficient is such a pre-training method compared to the previous pre-training? The answer is given in the above figure: compared with previous table pre-training method TaBERT, TAPEX could yield 2% improvement only using 2% of the pre-training corpus, achieving a speedup of nearly 50 times! With a larger pre-training corpus (e.g., 5 million <SQL, Table, Execution Result> pairs), the performance on downstream datasets would be better.ConclusionIn this blog, we introduce TAPEX, a table pre-training approach whose corpus is automatically synthesized via sampling SQL queries and their execution results. TAPEX addresses the data scarcity challenge in table pre-training by learning a neural SQL executor on a diverse, large-scale, and high-quality synthetic corpus. Experimental results on four downstream datasets demonstrate that TAPEX outperforms previous table pre-training approaches by a large margin, with a higher pre-training efficiency.Take AwayWhat can we learn from the success of TAPEX? I suggest that, especially if you want to perform efficient continual pre-training, you may try these options:Synthesize an accurate and small corpus, instead of mining a large but noisy corpus from the Internet.Simulate domain-adaptive skills via programs, instead of general-purpose language modeling via natural language sentences.
https://huggingface.co/blog/sentence-transformers-in-the-hub
Sentence Transformers in the Hugging Face Hub
Omar Sanseviero, Nils Reimers
June 28, 2021
Over the past few weeks, we've built collaborations with many Open Source frameworks in the machine learning ecosystem. One that gets us particularly excited is Sentence Transformers.Sentence Transformers is a framework for sentence, paragraph and image embeddings. This allows to derive semantically meaningful embeddings (1) which is useful for applications such as semantic search or multi-lingual zero shot classification. As part of Sentence Transformers v2 release, there are a lot of cool new features:Sharing your models in the Hub easily.Widgets and Inference API for sentence embeddings and sentence similarity.Better sentence-embeddings models available (benchmark and models in the Hub).With over 90 pretrained Sentence Transformers models for more than 100 languages in the Hub, anyone can benefit from them and easily use them. Pre-trained models can be loaded and used directly with few lines of code:from sentence_transformers import SentenceTransformersentences = ["Hello World", "Hallo Welt"]model = SentenceTransformer('sentence-transformers/paraphrase-MiniLM-L6-v2')embeddings = model.encode(sentences)print(embeddings)But not only this. People will probably want to either demo their models or play with other models easily, so we're happy to announce the release of two new widgets in the Hub! The first one is the feature-extraction widget which shows the sentence embedding.sentence-transformers/distilbert-base-nli-max-tokens Hosted inference API Feature Extraction Compute This model is currently loaded and running on the Inference API. JSON Output Maximize But seeing a bunch of numbers might not be very useful to you (unless you're able to understand the embeddings from a quick look, which would be impressive!). We're also introducing a new widget for a common use case of Sentence Transformers: computing sentence similarity.sentence-transformers/paraphrase-MiniLM-L6-v2Hosted inference API Sentence Similarity Source Sentence Sentences to compare to Add Sentence Compute This model can be loaded on the Inference API on-demand. JSON Output Maximize Of course, on top of the widgets, we also provide API endpoints in our Inference API that you can use to programmatically call your models!import jsonimport requestsAPI_URL = "https://api-inference.huggingface.co/models/sentence-transformers/paraphrase-MiniLM-L6-v2"headers = {"Authorization": "Bearer YOUR_TOKEN"}def query(payload):response = requests.post(API_URL, headers=headers, json=payload)return response.json()data = query({"inputs": {"source_sentence": "That is a happy person","sentences": ["That is a happy dog","That is a very happy person","Today is a sunny day"]}})Unleashing the Power of SharingSo why is this powerful? In a matter of minutes, you can share your trained models with the whole community.from sentence_transformers import SentenceTransformer# Load or train a modelmodel.save_to_hub("my_new_model")Now you will have a repository in the Hub which hosts your model. A model card was automatically created. It describes the architecture by listing the layers and shows how to use the model with both Sentence Transformers and 🤗 Transformers. You can also try out the widget and use the Inference API straight away!If this was not exciting enough, your models will also be easily discoverable by filtering for all Sentence Transformers models.What's next?Moving forward, we want to make this integration even more useful. In our roadmap, we expect training and evaluation data to be included in the automatically created model card, like is the case in transformers from version v4.8.And what's next for you? We're very excited to see your contributions! If you already have a Sentence Transformer repo in the Hub, you can now enable the widget and Inference API by changing the model card metadata.---tags:- sentence-transformers- sentence-similarity # Or feature-extraction!---If you don't have any model in the Hub and want to learn more about Sentence Transformers, head to www.SBERT.net!Would you like to integrate your library to the Hub?This integration is possible thanks to the huggingface_hub library which has all our widgets and the API for all our supported libraries. If you would like to integrate your library to the Hub, we have a guide for you!ReferencesSentence-BERT: Sentence Embeddings using Siamese BERT-Networks. https://arxiv.org/abs/1908.10084
https://huggingface.co/blog/mantis-case-study
Why we’re switching to Hugging Face Inference Endpoints, and maybe you should too
Matthew Upson
February 15, 2023
Hugging Face recently launched Inference Endpoints; which as they put it: solves transformers in production. Inference Endpoints is a managed service that allows you to:Deploy (almost) any model on Hugging Face HubTo any cloud (AWS, and Azure, GCP on the way)On a range of instance types (including GPU)We’re switching some of our Machine Learning (ML) models that do inference on a CPU to this new service. This blog is about why, and why you might also want to consider it.What were we doing?The models that we have switched over to Inference Endpoints were previously managed internally and were running on AWS Elastic Container Service (ECS) backed by AWS Fargate. This gives you a serverless cluster which can run container based tasks. Our process was as follows:Train model on a GPU instance (provisioned by CML, trained with transformers)Upload to Hugging Face HubBuild API to serve model (FastAPI)Wrap API in container (Docker)Upload container to AWS Elastic Container Repository (ECR)Deploy model to ECS ClusterNow, you can reasonably argue that ECS was not the best approach to serving ML models, but it served us up until now, and also allowed ML models to sit alongside other container based services, so it reduced cognitive load.What do we do now?With Inference Endpoints, our flow looks like this:Train model on a GPU instance (provisioned by CML, trained with transformers)Upload to Hugging Face HubDeploy using Hugging Face Inference Endpoints.So this is significantly easier. We could also use another managed service such as SageMaker, Seldon, or Bento ML, etc., but since we are already uploading our model to Hugging Face hub to act as a model registry, and we’re pretty invested in Hugging Face’s other tools (like transformers, and AutoTrain) using Inference Endpoints makes a lot of sense for us.What about Latency and Stability?Before switching to Inference Endpoints we tested different CPU endpoints types using ab.For ECS we didn’t test so extensively, but we know that a large container had a latency of about ~200ms from an instance in the same region. The tests we did for Inference Endpoints we based on text classification model fine tuned on RoBERTa with the following test parameters:Requester region: eu-east-1Requester instance size: t3-mediumInference endpoint region: eu-east-1Endpoint Replicas: 1Concurrent connections: 1Requests: 1000 (1000 requests in 1–2 minutes even from a single connection would represent very heavy use for this particular application)The following table shows latency (ms ± standard deviation and time to complete test in seconds) for four Intel Ice Lake equipped CPU endpoints.size | vCPU (cores) | Memory (GB) | ECS (ms) | 🤗 (ms)----------------------------------------------------------------------small | 1 | 2 | _ | ~ 296 medium | 2 | 4 | _ | 156 ± 51 (158s) large | 4 | 8 | ~200 | 80 ± 30 (80s) xlarge | 8 | 16 | _ | 43 ± 31 (43s) What we see from these results is pretty encouraging. The application that will consume these endpoints serves requests in real time, so we need as low latency as possible. We can see that the vanilla Hugging Face container was more than twice as fast as our bespoke container run on ECS — the slowest response we received from the large Inference Endpoint was just 108ms.What about the cost?So how much does this all cost? The table below shows a price comparison for what we were doing previously (ECS + Fargate) and using Inference Endpoints.size | vCPU | Memory (GB) | ECS | 🤗 | % diff----------------------------------------------------------------------small | 1 | 2 | $ 33.18 | $ 43.80 | 0.24medium | 2 | 4 | $ 60.38 | $ 87.61 | 0.31 large | 4 | 8 | $ 114.78 | $ 175.22 | 0.34xlarge | 8 | 16 | $ 223.59 | $ 350.44 | 0.5 We can say a couple of things about this. Firstly, we want a managed solution to deployment, we don’t have a dedicated MLOPs team (yet), so we’re looking for a solution that helps us minimize the time we spend on deploying models, even if it costs a little more than handling the deployments ourselves.Inference Endpoints are more expensive that what we were doing before, there’s an increased cost of between 24% and 50%. At the scale we’re currently operating, this additional cost, a difference of ~$60 a month for a large CPU instance is nothing compared to the time and cognitive load we are saving by not having to worry about APIs, and containers. If we were deploying 100s of ML microservices we would probably want to think again, but that is probably true of many approaches to hosting.Some notes and caveats:You can find pricing for Inference Endpoints here, but a different number is displayed when you deploy a new endpoint from the GUI. I’ve used the latter, which is higher.The values that I present in the table for ECS + Fargate are an underestimate, but probably not by much. I extracted them from the fargate pricing page and it includes just the cost of hosting the instance. I’m not including the data ingress/egress (probably the biggest thing is downloading the model from Hugging Face hub), nor have I included the costs related to ECR.Other considerationsDeployment OptionsCurrently you can deploy an Inference Endpoint from the GUI or using a RESTful API. You can also make use of our command line tool hugie (which will be the subject of a future blog) to launch Inference Endpoints in one line of code by passing a configuration, it’s really this simple:hugie endpoint create example/development.jsonFor me, what’s lacking is a custom terraform provider. It’s all well and good deploying an inference endpoint from a GitHub action using hugie, as we do, but it would be better if we could use the awesome state machine that is terraform to keep track of these. I’m pretty sure that someone (if not Hugging Face) will write one soon enough — if not, we will.Hosting multiple models on a single endpointPhilipp Schmid posted a really nice blog about how to write a custom Endpoint Handler class to allow you to host multiple models on a single endpoint, potentially saving you quite a bit of money. His blog was about GPU inference, and the only real limitation is how many models you can fit into the GPU memory. I assume this will also work for CPU instances, though I’ve not tried yet.To conclude…We find Hugging Face Inference Endpoints to be a very simple and convenient way to deploy transformer (and sklearn) models into an endpoint so they can be consumed by an application. Whilst they cost a little more than the ECS approach we were using before, it’s well worth it because it saves us time on thinking about deployment, we can concentrate on the thing we want to: building NLP solutions for our clients to help solve their problems.If you’re interested in Hugging Face Inference Endpoints for your company, please contact us here - our team will contact you to discuss your requirements!This article was originally published on February 15, 2023 in Medium.
https://huggingface.co/blog/ray-rag
Retrieval Augmented Generation with Huggingface Transformers and Ray
Ray Project (Anyscale)
February 10, 2021
Huggingface Transformers recently added the Retrieval Augmented Generation (RAG) model, a new NLP architecture that leverages external documents (like Wikipedia) to augment its knowledge and achieve state of the art results on knowledge-intensive tasks. In this blog post, we introduce the integration of Ray, a library for building scalable applications, into the RAG contextual document retrieval mechanism. This speeds up retrieval calls by 2x and improves the scalability of RAG distributed fine-tuning.What is Retrieval Augmented Generation (RAG)?An overview of RAG. The model retrieves contextual documents from an external dataset as part of its execution. These contextual documents are used in conjunction with the original input to produce an output. The GIF is taken from Facebook's original blog post.Recently, Huggingface partnered with Facebook AI to introduce the RAG model as part of its Transformers library. RAG acts just like any other seq2seq model. However, RAG has an intermediate component that retrieves contextual documents from an external knowledge base (like a Wikipedia text corpus). These documents are then used in conjunction with the input sequence and passed into the underlying seq2seq generator.This information retrieval step allows RAG to make use of multiple sources of knowledge -- those that are baked into the model parameters and the information that is contained in the contextual passages, allowing it to outperform other state-of-the-art models in tasks like question answering. You can try it for yourself using this demo provided by Huggingface!Scaling up fine-tuningThis retrieval of contextual documents is crucial for RAG's state-of-the-art results but introduces an extra layer of complexity. When scaling up the training process via a data-parallel training routine, a naive implementation of the document lookup can become a bottleneck for training. Further, the document index used in the retrieval component is often quite large, making it infeasible for each training worker to load its own replicated copy of the index.The previous implementation of RAG fine-tuning leveraged the torch.distributed communication package for the document retrieval portion. However, this implementation sometimes proved to be inflexible and limited in scalability.Instead, a framework-agnostic and a more flexible implementation for ad-hoc concurrent programming is required. Ray fits the bill perfectly. Ray is a simple, yet powerful Python library for general-purpose distributed and parallel programming. Using Ray for distributed document retrieval, we achieved a 2x speedup per retrieval call compared to torch.distributed, and overall better fine-tuning scalability.Ray for Document RetrievalDocument retrieval with the torch.distributed implementationThe main drawback of the torch.distributed implementation for document retrieval was that it latched onto the same process group used for training and only the rank 0 training worker loaded the index into memory.As a result, this implementation had some limitations:Synchronization bottleneck: The rank 0 worker had to receive the inputs from all workers, perform the index query, and then send the results back to the other workers. This limited performance with multiple training workers.PyTorch specific: The document retrieval process group had to latch onto the existing process group used for training, meaning that PyTorch had to be used for training as well.Document retrieval with the Ray implementationTo overcome these limitations, we introduced a novel implementation of distributed retrieval based on Ray. With Ray’s stateful actor abstractions, multiple processes that are separate from the training processes are used to load the index and handle the retrieval queries. With multiple Ray actors, retrieval is no longer a bottleneck and PyTorch is no longer a requirement for RAG.And as you can see below, using the Ray based implementation leads to better retrieval performance for multi-GPU fine-tuning. The following results show the seconds per retrieval call and we can see that as we increase the number of GPUs that we train on, using Ray has comparatively better performance than torch.distributed. Also, if we increase the number of Ray processes that perform retrieval, we also get better performance with more training workers since a single retrieval process is no longer a bottleneck.2 GPU3 GPU4 GPUtorch.distributed2.12 sec/retrieval2.62 sec/retrieve3.438 sec/retrieveRay 2 retrieval processes1.49 sec/retrieve1.539 sec/retrieve2.029 sec/retrieveRay 4 retrieval processes1.145 sec/retrieve1.484 sec/retrieve1.66 sec/retrieveA performance comparison of different retrieval implementations. For each document retrieval implementation, we run 500 training steps with a per-GPU batch size of 8, and measure the time it takes to retrieve the contextual documents for each batch on the rank 0 training worker. As the results show, using multiple retrieval processes improves performance, especially as we scale training to multiple GPUs.How do I use it?Huggingface provides a PyTorch Lightning based fine tuning script, and we extended it to add the Ray retrieval implementation as an option. To try it out, first install the necessary requirementspip install raypip install transformerspip install -r transformers/examples/research_projects/rag/requirements.txtThen, you can specify your data paths and other configurations and run finetune-rag-ray.sh!# Sample script to finetune RAG using Ray for distributed retrieval.# Add parent directory to python path to access lightning_base.pyexport PYTHONPATH="../":"${PYTHONPATH}"# Start a single-node Ray cluster.ray start --head# A sample finetuning run, you need to specify data_dir, output_dir and model_name_or_path# run ./examples/rag/finetune_rag_ray.sh --help to see all the possible optionspython examples/rag/finetune_rag.py \--data_dir $DATA_DIR \--output_dir $OUTPUT_DIR \--model_name_or_path $MODEL_NAME_OR_PATH \--model_type rag_sequence \--fp16 \--gpus 8 \--profile \--do_train \--do_predict \--n_val -1 \--train_batch_size 8 \--eval_batch_size 1 \--max_source_length 128 \--max_target_length 25 \--val_max_target_length 25 \--test_max_target_length 25 \--label_smoothing 0.1 \--dropout 0.1 \--attention_dropout 0.1 \--weight_decay 0.001 \--adam_epsilon 1e-08 \--max_grad_norm 0.1 \--lr_scheduler polynomial \--learning_rate 3e-05 \--num_train_epochs 100 \--warmup_steps 500 \--gradient_accumulation_steps 1 \--distributed_retriever ray \--num_retrieval_workers 4# Stop the Ray cluster.ray stopWhat’s next?Using RAG with Huggingface transformers and the Ray retrieval implementation for faster distributed fine-tuning, you can leverage RAG for retrieval-based generation on your own knowledge-intensive tasks.Also, hyperparameter tuning is another aspect of transformer fine tuning and can have huge impacts on accuracy. For scalable and easy hyperparameter tuning, check out the Ray Tune library. By using Ray Tune’s integration with PyTorch Lightning, or the built-in integration with Huggingface transformers, you can run experiments to find the perfect hyperparameters for your RAG model.And lastly, stay tuned for a potential Tensorflow implementation of RAG on Huggingface!If you plan to try RAG+Ray integration out, please feel free to share your experiences on the Ray Discourse or join the Ray community Slack for further discussion -- we’d love to hear from you!Also published at https://medium.com/distributed-computing-with-ray/retrieval-augmented-generation-with-huggingface-transformers-and-ray-b09b56161b1e
https://huggingface.co/blog/diffusers-2nd-month
What's new in Diffusers? 🎨
Omar Sanseviero
September 12, 2022
A month and a half ago we released diffusers, a library that provides a modular toolbox for diffusion models across modalities. A couple of weeks later, we released support for Stable Diffusion, a high quality text-to-image model, with a free demo for anyone to try out. Apart from burning lots of GPUs, in the last three weeks the team has decided to add one or two new features to the library that we hope the community enjoys! This blog post gives a high-level overview of the new features in diffusers version 0.3! Remember to give a ⭐ to the GitHub repository.Image to Image pipelinesTextual InversionInpaintingOptimizations for Smaller GPUsRun on MacONNX ExporterNew docsCommunityGenerate videos with SD latent spaceModel ExplainabilityJapanese Stable DiffusionHigh quality fine-tuned modelCross Attention Control with Stable DiffusionReusable seedsImage to Image pipelineOne of the most requested features was to have image to image generation. This pipeline allows you to input an image and a prompt, and it will generate an image based on that!Let's see some code based on the official Colab notebook. from diffusers import StableDiffusionImg2ImgPipelinepipe = StableDiffusionImg2ImgPipeline.from_pretrained("CompVis/stable-diffusion-v1-4",revision="fp16", torch_dtype=torch.float16,use_auth_token=True)# Download an initial image# ...init_image = preprocess(init_img)prompt = "A fantasy landscape, trending on artstation"images = pipe(prompt=prompt, init_image=init_image, strength=0.75, guidance_scale=7.5, generator=generator)["sample"]Don't have time for code? No worries, we also created a Space demo where you can try it out directlyTextual InversionTextual Inversion lets you personalize a Stable Diffusion model on your own images with just 3-5 samples. With this tool, you can train a model on a concept, and then share the concept with the rest of the community! In just a couple of days, the community shared over 200 concepts! Check them out!Organization with the concepts. Navigator Colab: Browse visually and use over 150 concepts created by the community.Training Colab: Teach Stable Diffusion a new concept and share it with the rest of the community.Inference Colab: Run Stable Diffusion with the learned concepts.Experimental inpainting pipelineInpainting allows to provide an image, then select an area in the image (or provide a mask), and use Stable Diffusion to replace the mask. Here is an example:You can try out a minimal Colab notebook or check out the code below. A demo is coming soon!from diffusers import StableDiffusionInpaintPipelinepipe = StableDiffusionInpaintPipeline.from_pretrained("CompVis/stable-diffusion-v1-4",revision="fp16", torch_dtype=torch.float16,use_auth_token=True).to(device)images = pipe(prompt=["a cat sitting on a bench"] * 3,init_image=init_image,mask_image=mask_image,strength=0.75,guidance_scale=7.5,generator=None).imagesPlease note this is experimental, so there is room for improvement.Optimizations for smaller GPUsAfter some improvements, the diffusion models can take much less VRAM. 🔥 For example, Stable Diffusion only takes 3.2GB! This yields the exact same results at the expense of 10% of speed. Here is how to use these optimizationsfrom diffusers import StableDiffusionPipelinepipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", revision="fp16", torch_dtype=torch.float16,use_auth_token=True)pipe = pipe.to("cuda")pipe.enable_attention_slicing()This is super exciting as this will reduce even more the barrier to use these models!Diffusers in Mac OS🍎 That's right! Another widely requested feature was just released! Read the full instructions in the official docs (including performance comparisons, specs, and more).Using the PyTorch mps device, people with M1/M2 hardware can run inference with Stable Diffusion. 🤯 This requires minimal setup for users, try it out!from diffusers import StableDiffusionPipelinepipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=True)pipe = pipe.to("mps")prompt = "a photo of an astronaut riding a horse on mars"image = pipe(prompt).images[0]Experimental ONNX exporter and pipelineThe new experimental pipeline allows users to run Stable Diffusion on any hardware that supports ONNX. Here is an example of how to use it (note that the onnx revision is being used)from diffusers import StableDiffusionOnnxPipelinepipe = StableDiffusionOnnxPipeline.from_pretrained("CompVis/stable-diffusion-v1-4",revision="onnx",provider="CPUExecutionProvider",use_auth_token=True,)prompt = "a photo of an astronaut riding a horse on mars"image = pipe(prompt).images[0]Alternatively, you can also convert your SD checkpoints to ONNX directly with the exporter script.python scripts/convert_stable_diffusion_checkpoint_to_onnx.py --model_path="CompVis/stable-diffusion-v1-4" --output_path="./stable_diffusion_onnx"New docsAll of the previous features are very cool. As maintainers of open-source libraries, we know about the importance of high quality documentation to make it as easy as possible for anyone to try out the library.💅 Because of this, we did a Docs sprint and we're very excited to do a first release of our documentation. This is a first version, so there are many things we plan to add (and contributions are always welcome!).Some highlights of the docs:Techniques for optimizationThe training overviewA contributing guideIn-depth API docs for schedulersIn-depth API docs for pipelinesCommunityAnd while we were doing all of the above, the community did not stay idle! Here are some highlights (although not exhaustive) of what has been done out thereStable Diffusion VideosCreate 🔥 videos with Stable Diffusion by exploring the latent space and morphing between text prompts. You can:Dream different versions of the same promptMorph between different promptsThe Stable Diffusion Videos tool is pip-installable, comes with a Colab notebook and a Gradio notebook, and is super easy to use!Here is an examplefrom stable_diffusion_videos import walkvideo_path = walk(['a cat', 'a dog'], [42, 1337], num_steps=3, make_video=True)Diffusers InterpretDiffusers interpret is an explainability tool built on top of diffusers. It has cool features such as:See all the images in the diffusion processAnalyze how each token in the prompt influences the generationAnalyze within specified bounding boxes if you want to understand a part of the image(Image from the tool repository)# pass pipeline to the explainer classexplainer = StableDiffusionPipelineExplainer(pipe)# generate an image with `explainer`prompt = "Corgi with the Eiffel Tower"output = explainer(prompt, num_inference_steps=15)output.normalized_token_attributions # (token, attribution_percentage)#[('corgi', 40),# ('with', 5),# ('the', 5),# ('eiffel', 25),# ('tower', 25)]Japanese Stable DiffusionThe name says it all! The goal of JSD was to train a model that also captures information about the culture, identity and unique expressions. It was trained with 100 million images with Japanese captions. You can read more about how the model was trained in the model cardWaifu DiffusionWaifu Diffusion is a fine-tuned SD model for high-quality anime images generation.(Image from the tool repository)Cross Attention ControlCross Attention Control allows fine control of the prompts by modifying the attention maps of the diffusion models. Some cool things you can do:Replace a target in the prompt (e.g. replace cat by dog)Reduce or increase the importance of words in the prompt (e.g. if you want less attention to be given to "rocks")Easily inject stylesAnd much more! Check out the repo.Reusable SeedsOne of the most impressive early demos of Stable Diffusion was the reuse of seeds to tweak images. The idea is to use the seed of an image of interest to generate a new image, with a different prompt. This yields some cool results! Check out the Colab Thanks for reading!I hope you enjoy reading this! Remember to give a Star in our GitHub Repository and join the Hugging Face Discord Server, where we have a category of channels just for Diffusion models. Over there the latest news in the library are shared! Feel free to open issues with feature requests and bug reports! Everything that has been achieved couldn't have been done without such an amazing community.
https://huggingface.co/blog/aws-partnership
Hugging Face and AWS partner to make AI more accessible
Jeff Boudier, Philipp Schmid, Julien Simon
February 21, 2023
It’s time to make AI open and accessible to all. That’s the goal of this expanded long-term strategic partnership between Hugging Face and Amazon Web Services (AWS). Together, the two leaders aim to accelerate the availability of next-generation machine learning models by making them more accessible to the machine learning community and helping developers achieve the highest performance at the lowest cost.A new generation of open, accessible AIMachine learning is quickly becoming embedded in all applications. As its impact on every sector of the economy comes into focus, it’s more important than ever to ensure every developer can access and assess the latest models. The partnership with AWS paves the way toward this future by making it faster and easier to build, train, and deploy the latest machine learning models in the cloud using purpose-built tools. There have been significant advances in new Transformer and Diffuser machine learning models that process and generate text, audio, and images. However, most of these popular generative AI models are not publicly available, widening the gap of machine learning capabilities between the largest tech companies and everyone else. To counter this trend, AWS and Hugging Face are partnering to contribute next-generation models to the global AI community and democratize machine learning. Through the strategic partnership, Hugging Face will leverage AWS as a preferred cloud provider so developers in Hugging Face’s community can access AWS’s state-of-the-art tools (e.g., Amazon SageMaker, AWS Trainium, AWS Inferentia) to train, fine-tune, and deploy models on AWS. This will allow developers to further optimize the performance of their models for their specific use cases while lowering costs. Hugging Face will apply the latest in innovative research findings using Amazon SageMaker to build next-generation AI models. Together, Hugging Face and AWS are bridging the gap so the global AI community can benefit from the latest advancements in machine learning to accelerate the creation of generative AI applications. “The future of AI is here, but it’s not evenly distributed,” said Clement Delangue, CEO of Hugging Face. “Accessibility and transparency are the keys to sharing progress and creating tools to use these new capabilities wisely and responsibly. Amazon SageMaker and AWS-designed chips will enable our team and the larger machine learning community to convert the latest research into openly reproducible models that anyone can build on.”Collaborating to scale AI in the cloudThis expanded strategic partnership enables Hugging Face and AWS to accelerate machine learning adoption using the latest models hosted on Hugging Face with the industry-leading capabilities of Amazon SageMaker. Customers can now easily fine-tune and deploy state-of-the-art Hugging Face models in just a few clicks on Amazon SageMaker and Amazon Elastic Computing Cloud (EC2), taking advantage of purpose-built machine learning accelerators including AWS Trainium and AWS Inferentia. “Generative AI has the potential to transform entire industries, but its cost and the required expertise puts the technology out of reach for all but a select few companies,” said Adam Selipsky, CEO of AWS. “Hugging Face and AWS are making it easier for customers to access popular machine learning models to create their own generative AI applications with the highest performance and lowest costs. This partnership demonstrates how generative AI companies and AWS can work together to put this innovative technology into the hands of more customers.” Hugging Face has become the central hub for machine learning, with more than 100,000 free and accessible machine learning models downloaded more than 1 million times daily by researchers, data scientists, and machine learning engineers. AWS is by far the most popular place to run models from the Hugging Face Hub. Since the start of our collaboration, Hugging Face on Amazon SageMaker has grown exponentially. We are experiencing an exciting renaissance with generative AI, and we're just getting started. We look forward to what the future holds for Hugging Face, AWS, and the AI community.
https://huggingface.co/blog/unsloth-trl
Make LLM Fine-tuning 2x faster with Unsloth and 🤗 TRL
Daniel Han-Chen
January 10, 2024
Pulling your hair out because LLM fine-tuning is taking forever? In this post, we introduce a lightweight tool developed by the community to make LLM fine-tuning go super fast!Before diving into Unsloth, it may be helpful to read our QLoRA blog post, or be familiar with LLM fine-tuning using the 🤗 PEFT library.Unsloth - 2x faster, -40% memory usage, 0% accuracy degradationUnsloth is a lightweight library for faster LLM fine-tuning which is fully compatible with the Hugging Face ecosystem (Hub, transformers, PEFT, TRL). The library is actively developed by the Unsloth team (Daniel and Michael) and the open source community. The library supports most NVIDIA GPUs –from GTX 1070 all the way up to H100s–, and can be used with the entire trainer suite from the TRL library (SFTTrainer, DPOTrainer, PPOTrainer). At the time of writing, Unsloth supports the Llama (CodeLlama, Yi, etc) and Mistral architectures.Unsloth works by overwriting some parts of the modeling code with optimized operations. By manually deriving backpropagation steps and rewriting all Pytorch modules into Triton kernels, Unsloth can both reduce memory usage and make fine-tuning faster. Crucially, accuracy degradation is 0% with respect to normal QLoRA, because no approximations are made in the optimized code.Benchmarking1 A100 40GBDataset🤗 Hugging Face🤗 + Flash Attention 2🦥 Unsloth🦥 VRAM reductionCode Llama 34bSlim Orca1x1.01x1.94x-22.7%Llama-2 7bSlim Orca1x0.96x1.87x-39.3%Mistral 7bSlim Orca1x1.17x1.88x-65.9%Tiny Llama 1.1bAlpaca1x1.55x2.74x-57.8%DPO with ZephyrUltra Chat1x1.24x1.88x-11.6%Free Colab T4Dataset🤗 Hugging Face🤗 + Pytorch 2.1.1🦥 Unsloth🦥 VRAM reductionLlama-2 7bOASST1x1.19x1.95x-43.3%Mistral 7bAlpaca1x1.07x1.56x-13.7%Tiny Llama 1.1bAlpaca1x2.06x3.87x-73.8%DPO with ZephyrUltra Chat1x1.09x1.55x-18.6%Unsloth was benchmarked across 59 runs using 4 datasets on Tesla T4 and A100 Google Colab instances. QLoRA was applied to all linear layers (attention and MLP) with a rank of 16, and gradient checkpointing was on. By testing against the latest Transformers version (4.36), which has SDPA natively integrated if you have Pytorch 2.1.1, Unsloth is up to 2.7x faster and uses up to 74% less memory. We also tested Unsloth on a free Google Colab instance (low RAM, 1 T4 GPU, Pytorch 2.1.0 CUDA 12.1). All 59 notebooks are provided for full reproducibility, and more details are in Unsloth’s benchmarking details hereHow do I use Unsloth?Just load your model with FastLanguageModel.from_pretrained! Currently, Unsloth supports Llama and Mistral type architectures (Yi, Deepseek, TinyLlama, Llamafied Qwen). Please, open a Github issue if you want others! Also, on the latest Transformers main branch, you can now load pre-quantized 4bit models directly! This makes downloading models 4x faster, and reduces memory fragmentation by around 500MB, which allows you to fit larger batches! We have a few pre-quantized models for your convenience, including unsloth/llama-2-7b-bnb-4bit, unsloth/llama-2-13b-bnb-4bit, unsloth/mistral-7b-bnb-4bit and unsloth/codellama-34b-bnb-4bit.You will need to provide your intended maximum sequence length to from_pretrained. Unsloth internally performs RoPE Scaling, so larger maximum sequence lengths are automatically supported. Otherwise the API is pretty much the same as transformers’ from_pretrained, except that FastLanguageModel.from_pretrained also returns the model tokenizer for convenience.from unsloth import FastLanguageModelmodel, tokenizer = FastLanguageModel.from_pretrained(model_name = "unsloth/mistral-7b-bnb-4bit", # Supports Llama, Mistral - replace this!max_seq_length = 2048, # Supports RoPE Scaling internally, so choose any!load_in_4bit = True,)Once the model has been loaded, use FastLanguageModel.get_peft_model to attach adapters in order to perform QLoRA fine-tuning.# Do model patching and add fast LoRA weightsmodel = FastLanguageModel.get_peft_model(model,r = 16,target_modules = ["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],lora_alpha = 16,lora_dropout = 0, # Supports any, but = 0 is optimizedbias = "none", # Supports any, but = "none" is optimizeduse_gradient_checkpointing = True,)Once adapters are attached, you can use the model directly within any class from the HF ecosystem, such as the SFTTrainer from TRL!Unsloth + TRL integrationTo use Unsloth with the TRL library, simply pass the Unsloth model into SFTTrainer or DPOTrainer! The trained model is fully compatible with the Hugging Face ecosystem, so you can push the final model to the Hub and use transformers for inference out of the box!import torchfrom trl import SFTTrainerfrom transformers import TrainingArgumentsfrom datasets import load_datasetfrom unsloth import FastLanguageModelmax_seq_length = 2048 # Supports RoPE Scaling interally, so choose any!# Get datasetdataset = load_dataset("imdb", split="train")# Load Llama modelmodel, tokenizer = FastLanguageModel.from_pretrained(model_name = "unsloth/mistral-7b-bnb-4bit", # Supports Llama, Mistral - replace this!max_seq_length = max_seq_length,dtype = None,load_in_4bit = True,)# Do model patching and add fast LoRA weightsmodel = FastLanguageModel.get_peft_model(model,r = 16,target_modules = ["q_proj", "k_proj", "v_proj", "o_proj","gate_proj", "up_proj", "down_proj",],lora_alpha = 16,lora_dropout = 0, # Supports any, but = 0 is optimizedbias = "none", # Supports any, but = "none" is optimizeduse_gradient_checkpointing = True,random_state = 3407,max_seq_length = max_seq_length,)trainer = SFTTrainer(model = model,train_dataset = dataset,dataset_text_field = "text",max_seq_length = max_seq_length,tokenizer = tokenizer,args = TrainingArguments(per_device_train_batch_size = 2,gradient_accumulation_steps = 4,warmup_steps = 10,max_steps = 60,fp16 = not torch.cuda.is_bf16_supported(),bf16 = torch.cuda.is_bf16_supported(),logging_steps = 1,output_dir = "outputs",optim = "adamw_8bit",seed = 3407,),)trainer.train()Reproducible notebooksWe are sharing below fully reproducible notebooks for anyone that wants to try out Unsloth with SFTTrainer on a free-tier Google Colab instance.Llama 7b Free Tesla T4 colab example hereMistral 7b Free Tesla T4 colab example hereCodeLlama 34b A100 colab example hereZephyr DPO replication T4 colab example here
https://huggingface.co/blog/community-datasets
Data is better together: Enabling communities to collectively build better datasets together using Argilla and Hugging Face Spaces
Daniel van Strien, Daniel Vila
March 4, 2024
Recently, Argilla and Hugging Face launched Data is Better Together, an experiment to collectively build a preference dataset of prompt rankings. In a few days, we had:350 community contributors labeling data Over 11,000 prompt ratingsSee the progress dashboard for the latest stats!This resulted in the release of 10k_prompts_ranked, a dataset consisting of 10,000 prompts with user ratings for the quality of the prompt. We want to enable many more projects like this!In this post, we’ll discuss why we think it’s essential for the community to collaborate on building datasets and share an invitation to join the first cohort of communities Argilla and Hugging Face will support to develop better datasets together! Data remains essential for better models Data continues to be essential for better models: We see continued evidence from published research, open-source experiments, and from the open-source community that better data can lead to better models. The question.A frequent answer. Why build datasets collectively? Data is vital for machine learning, but many languages, domains, and tasks still lack high-quality datasets for training, evaluating, and benchmarking — the community already shares thousands of models, datasets, and demos daily via the Hugging Face Hub. As a result of collaboration, the open-access AI community has created amazing things. Enabling the community to build datasets collectively will unlock unique opportunities for building the next generation of datasets to build the next generation of models. Empowering the community to build and improve datasets collectively will allow people to:Contribute to the development of Open Source ML with no ML or programming skills required.Create chat datasets for a particular language.Develop benchmark datasets for a specific domain. Create preference datasets from a diverse range of participants.Build datasets for a particular task.Build completely new types of datasets collectively as a community.Importantly we believe that building datasets collectively will allow the community to build better datasets abd allow people who don't know how to code to contribute to the development of AI. Making it easy for people to contribute One of the challenges to many previous efforts to build AI datasets collectively was setting up an efficient annotation task. Argilla is an open-source tool that can help create datasets for LLMs and smaller specialised task-specific models. Hugging Face Spaces is a platform for building and hosting machine learning demos and applications. Recently, Argilla added support for authentication via a Hugging Face account for Argilla instances hosted on Spaces. This means it now takes seconds for users to start contributing to an annotation task. Now that we have stress-tested this new workflow when creating the 10k_prompts_ranked, dataset, we want to support the community in launching new collective dataset efforts. Join our first cohort of communities who want to build better datasets together! We’re very excited about the possibilities unlocked by this new, simple flow for hosting annotation tasks. To support the community in building better datasets, Hugging Face and Argilla invite interested people and communities to join our initial cohort of community dataset builders. People joining this cohort will:Be supported in creating an Argilla Space with Hugging Face authentication. Hugging Face will grant free persistent storage and improved CPU spaces for participants. Have their comms and promotion advertising the initiative amplified by Argilla and Hugging Face.Be invited to join a cohort community channelOur goal is to support the community in building better datasets together. We are open to many ideas and want to support the community as far as possible in building better datasets together. What types of projects are we looking for? We are open to supporting many types of projects, especially those of existing open-source communities. We are particularly interested in projects focusing on building datasets for languages, domains, and tasks that are currently underrepresented in the open-source community. Our only current limitation is that we're primarily focused on text-based datasets. If you have a very cool idea for multimodal datasets, we'd love to hear from you, but we may not be able to support you in this first cohort. Tasks can either be fully open or open to members of a particular Hugging Face Hub organization. If you want to be part of the first cohort, please join us in the #data-is-better-together channel in the Hugging Face Discord and let us know what you want to build together! We are looking forward to building better datasets together with you!
https://huggingface.co/blog/regions
Introducing Storage Regions on the Hub
Eliott Coyac, Remy TROMPIER, Adrien, Michelle Habonneau, Violette Lepercq, Julien Chaumond
November 3, 2023
As part of our Enterprise Hub plan, we recently released support for Storage Regions.Regions let you decide where your org's models and datasets will be stored. This has two main benefits, which we'll briefly go over in this blog post:Regulatory and legal compliance, and more generally, better digital sovereigntyPerformance (improved download and upload speeds and latency)Currently we support the following regions:US 🇺🇸EU 🇪🇺coming soon: Asia-Pacific 🌏But first, let's see how to setup this feature in your organization's settings 🔥Org settingsIf your organization is not an Enterprise Hub org yet, you will see the following screen:As soon as you subscribe, you will be able to see the Regions settings page:On that page you can see:an audit of where your orgs' repos are currently locateddropdowns to select where your repos will be createdRepository TagAny repo (model or dataset) stored in a non-default location will display its Region directly as a tag. That way your organization's members can see at a glance where repos are located.Regulatory and legal complianceIn many regulated industries, you may have a requirement to store your data in a specific area.For companies in the EU, that means you can use the Hub to build ML in a GDPR compliant way: with datasets, models and inference endpoints all stored within EU data centers.If you are an Enterprise Hub customer and have further questions about this, please get in touch!PerformanceStoring your models or your datasets closer to your team and infrastructure also means significantly improved performance, for both uploads and downloads.This makes a big difference considering model weights and dataset files are usually very large.As an example, if you are located in Europe and store your repositories in the EU region, you can expect to see ~4-5x faster upload and download speeds vs. if they were stored in the US.
https://huggingface.co/blog/carbon-emissions-on-the-hub
CO2 Emissions and the 🤗 Hub: Leading the Charge
Sasha Luccioni, Zachary Mueller, Nate Raw
April 22, 2022
What are CO2 Emissions and why are they important?Climate change is one of the greatest challenges that we are facing and reducing emissions of greenhouse gases such as carbon dioxide (CO2) is an important part of tackling this problem. Training and deploying machine learning models will emit CO2 due to the energy usage of the computing infrastructures that are used: from GPUs to storage, it all needs energy to function and emits CO2 in the process.Pictured: Recent Transformer models and their carbon footprintsThe amount of CO2 emitted depends on different factors such as runtime, hardware used, and carbon intensity of the energy source. Using the tools described below will help you both track and report your own emissions (which is important to improve the transparency of our field as a whole!) and choose models based on their carbon footprint. How to calculate your own CO2 Emissions automatically with TransformersBefore we begin, if you do not have the latest version of the huggingface_hub library on your system, please run the following:pip install huggingface_hub -UHow to find low-emission models using the Hugging Face HubWith the model now uploaded to the Hub, how can you search for models on the Hub while trying to be eco-friendly? Well, the huggingface_hub library has a new special parameter to perform this search: emissions_threshold. All you need to do is specify a minimum or maximum number of grams, and all models that fall within that range. For example, we can search for all models that took a maximum of 100 grams to make:from huggingface_hub import HfApiapi = HfApi()models = api.list_models(emissions_thresholds=(None, 100), cardData=True)len(models)>>> 191There were quite a few! This also helps to find smaller models, given they typically did not release as much carbon during training.We can look at one up close to see it does fit our threshold:model = models[0]print(f'Model Name: {model.modelId}CO2 Emitted during training: {model.cardData["co2_eq_emissions"]}')>>> Model Name: esiebomajeremiah/autonlp-email-classification-657119381CO2 Emitted during training: 3.516233232503715Similarly, we can search for a minimum value to find very large models that emitted a lot of CO2 during training:models = api.list_models(emissions_thresholds=(500, None), cardData=True)len(models)>>> 10Now let's see exactly how much CO2 one of these emitted:model = models[0]print(f'Model Name: {model.modelId}CO2 Emitted during training: {model.cardData["co2_eq_emissions"]}')>>> Model Name: Maltehb/aelaectra-danish-electra-small-casedCO2 Emitted during training: 4009.5That's a lot of CO2!As you can see, in just a few lines of code we can quickly vet models we may want to use to make sure we're being environmentally cognizant! How to Report Your Carbon Emissions with transformersIf you're using transformers, you can automatically track and report carbon emissions thanks to the codecarbon integration. If you've installed codecarbon on your machine, the Trainer object will automatically add the CodeCarbonCallback while training, which will store carbon emissions data for you as you train.So, if you run something like this...from datasets import load_datasetfrom transformers import AutoModelForSequenceClassification, AutoTokenizer, Trainer, TrainingArguments​ds = load_dataset("imdb")model = AutoModelForSequenceClassification.from_pretrained("bert-base-cased", num_labels=2)tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")​def tokenize_function(examples):return tokenizer(examples["text"], padding="max_length", truncation=True)​​small_train_dataset = ds["train"].shuffle(seed=42).select(range(1000)).map(tokenize_function, batched=True)small_eval_dataset = ds["test"].shuffle(seed=42).select(range(1000)).map(tokenize_function, batched=True)​​training_args = TrainingArguments("codecarbon-text-classification",num_train_epochs=4,push_to_hub=True)​trainer = Trainer(model=model,args=training_args,train_dataset=small_train_dataset,eval_dataset=small_eval_dataset,)​trainer.train()...you'll be left with a file within the codecarbon-text-classification directory called emissions.csv. This file will keep track of the carbon emissions across different training runs. Then, when you're ready, you can take the emissions from the run you used to train your final model and include that in its model card. 📝An example of this data being included at the top of the model card is shown below:For more references on the metadata format for co2_eq_emissions see the hub docs.Further readingsRolnick et al. (2019) - Tackling Climate Change with Machine Learning Strubell et al. (2019) - Energy and Policy Considerations for Deep Learning in NLPSchwartz et al. (2020) - Green AI
https://huggingface.co/blog/decision-transformers
Introducing Decision Transformers on Hugging Face 🤗
Edward Beeching, Thomas Simonini
March 28, 2022
At Hugging Face, we are contributing to the ecosystem for Deep Reinforcement Learning researchers and enthusiasts. Recently, we have integrated Deep RL frameworks such as Stable-Baselines3. And today we are happy to announce that we integrated the Decision Transformer, an Offline Reinforcement Learning method, into the 🤗 transformers library and the Hugging Face Hub. We have some exciting plans for improving accessibility in the field of Deep RL and we are looking forward to sharing them with you over the coming weeks and months.What is Offline Reinforcement Learning?Introducing Decision TransformersUsing the Decision Transformer in 🤗 TransformersConclusionWhat's next?ReferencesWhat is Offline Reinforcement Learning?Deep Reinforcement Learning (RL) is a framework to build decision-making agents. These agents aim to learn optimal behavior (policy) by interacting with the environment through trial and error and receiving rewards as unique feedback.The agent’s goal is to maximize its cumulative reward, called return. Because RL is based on the reward hypothesis: all goals can be described as the maximization of the expected cumulative reward.Deep Reinforcement Learning agents learn with batches of experience. The question is, how do they collect it?:A comparison between Reinforcement Learning in an Online and Offline setting, figure taken from this postIn online reinforcement learning, the agent gathers data directly: it collects a batch of experience by interacting with the environment. Then, it uses this experience immediately (or via some replay buffer) to learn from it (update its policy).But this implies that either you train your agent directly in the real world or have a simulator. If you don’t have one, you need to build it, which can be very complex (how to reflect the complex reality of the real world in an environment?), expensive, and insecure since if the simulator has flaws, the agent will exploit them if they provide a competitive advantage. On the other hand, in offline reinforcement learning, the agent only uses data collected from other agents or human demonstrations. It does not interact with the environment.The process is as follows:Create a dataset using one or more policies and/or human interactions.Run offline RL on this dataset to learn a policyThis method has one drawback: the counterfactual queries problem. What do we do if our agent decides to do something for which we don’t have the data? For instance, turning right on an intersection but we don’t have this trajectory. There’s already exists some solutions on this topic, but if you want to know more about offline reinforcement learning you can watch this videoIntroducing Decision TransformersThe Decision Transformer model was introduced by “Decision Transformer: Reinforcement Learning via Sequence Modeling” by Chen L. et al. It abstracts Reinforcement Learning as a conditional-sequence modeling problem.The main idea is that instead of training a policy using RL methods, such as fitting a value function, that will tell us what action to take to maximize the return (cumulative reward), we use a sequence modeling algorithm (Transformer) that, given a desired return, past states, and actions, will generate future actions to achieve this desired return. It’s an autoregressive model conditioned on the desired return, past states, and actions to generate future actions that achieve the desired return.This is a complete shift in the Reinforcement Learning paradigm since we use generative trajectory modeling (modeling the joint distribution of the sequence of states, actions, and rewards) to replace conventional RL algorithms. It means that in Decision Transformers, we don’t maximize the return but rather generate a series of future actions that achieve the desired return.The process goes this way:We feed the last K timesteps into the Decision Transformer with 3 inputs:Return-to-goStateActionThe tokens are embedded either with a linear layer if the state is a vector or CNN encoder if it’s frames.The inputs are processed by a GPT-2 model which predicts future actions via autoregressive modeling.Decision Transformer architecture. States, actions, and returns are fed into modality specific linear embeddings and a positional episodic timestep encoding is added. Tokens are fed into a GPT architecture which predicts actions autoregressively using a causal self-attention mask. Figure from [1].Using the Decision Transformer in 🤗 TransformersThe Decision Transformer model is now available as part of the 🤗 transformers library. In addition, we share nine pre-trained model checkpoints for continuous control tasks in the Gym environment.An “expert” Decision Transformers model, learned using offline RL in the Gym Walker2d environment.Install the packagepip install git+https://github.com/huggingface/transformersLoading the modelUsing the Decision Transformer is relatively easy, but as it is an autoregressive model, some care has to be taken in order to prepare the model’s inputs at each time-step. We have prepared both a Python script and a Colab notebook that demonstrates how to use this model.Loading a pretrained Decision Transformer is simple in the 🤗 transformers library:from transformers import DecisionTransformerModelmodel_name = "edbeeching/decision-transformer-gym-hopper-expert"model = DecisionTransformerModel.from_pretrained(model_name)Creating the environmentWe provide pretrained checkpoints for the Gym Hopper, Walker2D and Halfcheetah. Checkpoints for Atari environments will soon be available.import gymenv = gym.make("Hopper-v3")state_dim = env.observation_space.shape[0] # state sizeact_dim = env.action_space.shape[0] # action sizeAutoregressive prediction functionThe model performs an autoregressive prediction; that is to say that predictions made at the current time-step t are sequentially conditioned on the outputs from previous time-steps. This function is quite meaty, so we will aim to explain it in the comments.# Function that gets an action from the model using autoregressive prediction # with a window of the previous 20 timesteps.def get_action(model, states, actions, rewards, returns_to_go, timesteps):# This implementation does not condition on past rewardsstates = states.reshape(1, -1, model.config.state_dim)actions = actions.reshape(1, -1, model.config.act_dim)returns_to_go = returns_to_go.reshape(1, -1, 1)timesteps = timesteps.reshape(1, -1)# The prediction is conditioned on up to 20 previous time-stepsstates = states[:, -model.config.max_length :]actions = actions[:, -model.config.max_length :]returns_to_go = returns_to_go[:, -model.config.max_length :]timesteps = timesteps[:, -model.config.max_length :]# pad all tokens to sequence length, this is required if we process batchespadding = model.config.max_length - states.shape[1]attention_mask = torch.cat([torch.zeros(padding), torch.ones(states.shape[1])])attention_mask = attention_mask.to(dtype=torch.long).reshape(1, -1)states = torch.cat([torch.zeros((1, padding, state_dim)), states], dim=1).float()actions = torch.cat([torch.zeros((1, padding, act_dim)), actions], dim=1).float()returns_to_go = torch.cat([torch.zeros((1, padding, 1)), returns_to_go], dim=1).float()timesteps = torch.cat([torch.zeros((1, padding), dtype=torch.long), timesteps], dim=1)# perform the predictionstate_preds, action_preds, return_preds = model(states=states,actions=actions,rewards=rewards,returns_to_go=returns_to_go,timesteps=timesteps,attention_mask=attention_mask,return_dict=False,)return action_preds[0, -1]Evaluating the modelIn order to evaluate the model, we need some additional information; the mean and standard deviation of the states that were used during training. Fortunately, these are available for each of the checkpoint’s model card on the Hugging Face Hub! We also need a target return for the model. This is the power of return conditioned Offline Reinforcement Learning: we can use the target return to control the performance of the policy. This could be really powerful in a multiplayer setting, where we would like to adjust the performance of an opponent bot to be at a suitable difficulty for the player. The authors show a great plot of this in their paper!Sampled (evaluation) returns accumulated by Decision Transformer when conditioned onthe specified target (desired) returns. Top: Atari. Bottom: D4RL medium-replay datasets. Figure from [1].TARGET_RETURN = 3.6 # This was normalized during trainingMAX_EPISODE_LENGTH = 1000 state_mean = np.array([1.3490015, -0.11208222, -0.5506444, -0.13188992, -0.00378754, 2.6071432,0.02322114, -0.01626922, -0.06840388, -0.05183131, 0.04272673,])state_std = np.array([0.15980862, 0.0446214, 0.14307782, 0.17629202, 0.5912333, 0.5899924,1.5405099, 0.8152689, 2.0173461, 2.4107876, 5.8440027,])state_mean = torch.from_numpy(state_mean)state_std = torch.from_numpy(state_std)state = env.reset()target_return = torch.tensor(TARGET_RETURN).float().reshape(1, 1)states = torch.from_numpy(state).reshape(1, state_dim).float()actions = torch.zeros((0, act_dim)).float()rewards = torch.zeros(0).float()timesteps = torch.tensor(0).reshape(1, 1).long()# take steps in the environmentfor t in range(max_ep_len):# add zeros for actions as input for the current time-stepactions = torch.cat([actions, torch.zeros((1, act_dim))], dim=0)rewards = torch.cat([rewards, torch.zeros(1)])# predicting the action to takeaction = get_action(model,(states - state_mean) / state_std,actions,rewards,target_return,timesteps)actions[-1] = actionaction = action.detach().numpy()# interact with the environment based on this actionstate, reward, done, _ = env.step(action)cur_state = torch.from_numpy(state).reshape(1, state_dim)states = torch.cat([states, cur_state], dim=0)rewards[-1] = rewardpred_return = target_return[0, -1] - (reward / scale)target_return = torch.cat([target_return, pred_return.reshape(1, 1)], dim=1)timesteps = torch.cat([timesteps, torch.ones((1, 1)).long() * (t + 1)], dim=1)if done:breakYou will find a more detailed example, with the creation of videos of the agent in our Colab notebook.ConclusionIn addition to Decision Transformers, we want to support more use cases and tools from the Deep Reinforcement Learning community. Therefore, it would be great to hear your feedback on the Decision Transformer model, and more generally anything we can build with you that would be useful for RL. Feel free to reach out to us. What’s next?In the coming weeks and months, we plan on supporting other tools from the ecosystem:Integrating RL-baselines3-zooUploading RL-trained-agents models into the Hub: a big collection of pre-trained Reinforcement Learning agents using stable-baselines3Integrating other Deep Reinforcement Learning librariesImplementing Convolutional Decision Transformers For AtariAnd more to come 🥳The best way to keep in touch is to join our discord server to exchange with us and with the community.References[1] Chen, Lili, et al. "Decision transformer: Reinforcement learning via sequence modeling." Advances in neural information processing systems 34 (2021).[2] Agarwal, Rishabh, Dale Schuurmans, and Mohammad Norouzi. "An optimistic perspective on offline reinforcement learning." International Conference on Machine Learning. PMLR, 2020.AcknowledgementsWe would like to thank the paper’s first authors, Kevin Lu and Lili Chen, for their constructive conversations.
https://huggingface.co/blog/model-cards
Model Cards
Ezi Ozoani, Marissa Gerchick, Margaret Mitchell
December 20, 2022
Introduction Model cards are an important documentation framework for understanding, sharing, and improving machine learning models. When done well, a model card can serve as a boundary object, a single artefact that is accessible to people with different backgrounds and goals in understanding models - including developers, students, policymakers, ethicists, and those impacted by machine learning models.Today, we launch a model card creation tool and a model card Guide Book, which details how to fill out model cards, user studies, and state of the art in ML documentation. This work, building from many other people and organizations, focuses on the inclusion of people with different backgrounds and roles. We hope it serves as a stepping stone in the path toward improved ML documentation.In sum, today we announce the release of:A Model Card Creator Tool, to ease card creation without needing to program, and to help teams share the work of different sections.An updated model card template, released in the huggingface_hub library, drawing together model card work in academia and throughout the industry.An Annotated Model Card Template, which details how to fill the card out.A User Study on model card usage at Hugging Face.A Landscape Analysis and Literature Review of the state of the art in model documentation. Model Cards To-Date Since Model Cards were proposed by Mitchell et al. (2018), inspired by the major documentation framework efforts of Data Statements for Natural Language Processing (Bender & Friedman, 2018) and Datasheets for Datasets (Gebru et al., 2018), the landscape of machine learning documentation has expanded and evolved. A plethora of documentation tools and templates for data, models, and ML systems have been proposed and developed - reflecting the incredible work of hundreds of researchers, impacted community members, advocates, and other stakeholders. Important discussions about the relationship between ML documentation and theories of change in responsible AI have also shaped these developments in the ML documentation ecosystem. Work to-date on documentation within ML has provided for different audiences. We bring many of these ideas together in the work we share today. Our Work Our work presents a view of where model cards stand right now and where they could go in the future. We conducted a broad analysis of the growing landscape of ML documentation tools and conducted user interviews within Hugging Face to supplement our understanding of the diverse opinions about model cards. We also created or updated dozens of model cards for ML models on the Hugging Face Hub, and informed by all of these experiences, we propose a new template for model cards. Standardising Model Card Structure Through our background research and user studies, which are discussed further in the Guide Book, we aimed to establish a new standard of "model cards" as understood by the general public. Informed by these findings, we created a new model card template that not only standardized the structure and content of HF model cards but also provided default prompt text. This text aimed to aide with writing model card sections, with a particular focus on the Bias, Risks and Limitations section. Accessibility and Inclusion In order to lower barriers to entry for creating model cards, we designed the model card writing tool, a tool with a graphical user interface (GUI) to enable people and teams with different skill sets and roles to easily collaborate and create model cards, without needing to code or use markdown. The writing tool encourages those who have yet to write model cards to create them more easily. For those who have previously written model cards, this approach invites them to add to the prompted information -- while centering the ethical components of model documentation.As ML continues to be more intertwined with different domains, collaborative and open-source ML processes that center accessibility, ethics and inclusion are a critical part of the machine learning lifecycle and a stepping stone in ML documentation. Today's release sits within a larger ecosystem of ML documentation work: Data and model documentation have been taken up by many tech companies, including Hugging Face 🤗. We've prioritized "Repository Cards" for both dataset cards and model cards, focusing on multidisciplinarity. Continuing in this line of work, the model card creation UI tool focuses on inclusivity, providing guidance on formatting and prompting to aid card creation for people with different backgrounds. Call to action Let's look aheadThis work is a "snapshot" of the current state of model cards, informed by a landscape analysis of the many ways ML documentation artefacts have been instantiated. The model book and these findings represent one perspective amongst multiple about both the current state and more aspirational visions of model cards.The Hugging Face ecosystem will continue to advance methods that streamline Model Card creation through code and user interfaces, including building more features directly into the repos and product. As we further develop model tools such as Evaluate on the Hub, we will integrate their usage within the model card development workflow. For example, as automatically evaluating model performance across disaggregated factors becomes easier, these results will be possible to import into the model card.There is further study to be done to advance the pairing of research models and model cards, such as building out a research paper → to model documentation pipeline, making it make it trivial to go from paper to model card creation. This would allow for greater cross-domain reach and further standardisation of model documentation.We continue to learn more about how model cards are created and used, and the effect of cards on model usage. Based on these learnings, we will further update the model card template, instructions, and Hub integrations. As we strive to incorporate more voices and stakeholders' use cases for model cards, bookmark our model cards writing tool and give it a try!We are excited to know your thoughts on model cards, our model card writing GUI, and how AI documentation can empower your domain.🤗 Acknowledgements This release would not have been possible without the extensive contributions of Omar Sanseviero, Lucain Pouget, Julien Chaumond, Nazneen Rajani, and Nate Raw.
https://huggingface.co/blog/snowball-fight
Introducing Snowball Fight ☃️, our First ML-Agents Environment
Thomas Simonini
December 2, 2021
We're excited to share our first custom Deep Reinforcement Learning environment: Snowball Fight 1vs1 🎉.Snowball Fight is a game made with Unity ML-Agents, where you shoot snowballs against a Deep Reinforcement Learning agent. The game is hosted on Hugging Face Spaces. 👉 You can play it online hereIn this post, we'll cover the ecosystem we are working on for Deep Reinforcement Learning researchers and enthusiasts that use Unity ML-Agents.Unity ML-Agents at Hugging FaceThe Unity Machine Learning Agents Toolkit is an open source library that allows you to build games and simulations with Unity game engine to serve as environments for training intelligent agents.With this first step, our goal is to build an ecosystem on Hugging Face for Deep Reinforcement Learning researchers and enthusiasts that uses ML-Agents, with three features.Building and sharing custom environments. We are developing and sharing exciting environments to experiment with new problems: snowball fights, racing, puzzles... All of them will be open source and hosted on the Hugging Face's Hub.Allowing you to easily host your environments, save models and share them on the Hugging Face Hub. We have already published the Snowball Fight training environment here, but there will be more to come!You can now easily host your demos on Spaces and showcase your results quickly with the rest of the ecosystem.Be part of the conversation: join our discord server!If you're using ML-Agents or interested in Deep Reinforcement Learning and want to be part of the conversation, you can join our discord server. We just added two channels (and we'll add more in the future):Deep Reinforcement LearningML-AgentsOur discord is the place where you can exchange about Hugging Face, NLP, Deep RL, and more! It's also in this discord that we'll announce all our new environments and features in the future.What's next?In the coming weeks and months, we will be extending the ecosystem by:Writing some technical tutorials on ML-Agents.Working on a Snowball Fight 2vs2 version, where the agents will collaborate in teams using MA-POCA, a new Deep Reinforcement Learning algorithm that trains cooperative behaviors in a team.And we're building new custom environments that will be hosted in Hugging Face.ConclusionWe're excited to see what you're working on with ML-Agents and how we can build features and tools that help you to empower your work.Don't forget to join our discord server to be alerted of the new features.
https://huggingface.co/blog/ambassadors
Student Ambassador Program’s call for applications is open!
Violette Lepercq
May 13, 2022
Student Ambassador Program’s call for applications is open!Hugging FaceModelsDatasetsSpacesPostsDocsSolutionsPricingLog InSign UpBack to ArticlesStudent Ambassador Program’s call for applications is open!
https://huggingface.co/blog/peft
🤗 PEFT: Parameter-Efficient Fine-Tuning of Billion-Scale Models on Low-Resource Hardware
Sourab Mangrulkar, Sayak Paul
February 10, 2023
Motivation Large Language Models (LLMs) based on the transformer architecture, like GPT, T5, and BERT have achieved state-of-the-art results in various Natural Language Processing (NLP) tasks. They have also started foraying into other domains, such as Computer Vision (CV) (VIT, Stable Diffusion, LayoutLM) and Audio (Whisper, XLS-R). The conventional paradigm is large-scale pretraining on generic web-scale data, followed by fine-tuning to downstream tasks. Fine-tuning these pretrained LLMs on downstream datasets results in huge performance gains when compared to using the pretrained LLMs out-of-the-box (zero-shot inference, for example).However, as models get larger and larger, full fine-tuning becomes infeasible to train on consumer hardware. In addition, storing and deploying fine-tuned models independently for each downstream task becomes very expensive, because fine-tuned models are the same size as the original pretrained model. Parameter-Efficient Fine-tuning (PEFT) approaches are meant to address both problems!PEFT approaches only fine-tune a small number of (extra) model parameters while freezing most parameters of the pretrained LLMs, thereby greatly decreasing the computational and storage costs. This also overcomes the issues of catastrophic forgetting, a behaviour observed during the full finetuning of LLMs. PEFT approaches have also shown to be better than fine-tuning in the low-data regimes and generalize better to out-of-domain scenarios. It can be applied to various modalities, e.g., image classification and stable diffusion dreambooth.It also helps in portability wherein users can tune models using PEFT methods to get tiny checkpoints worth a few MBs compared to the large checkpoints of full fine-tuning, e.g., bigscience/mt0-xxl takes up 40GB of storage and full fine-tuning will lead to 40GB checkpoints for each downstream dataset whereas using PEFT methods it would be just a few MBs for each downstream dataset all the while achieving comparable performance to full fine-tuning. The small trained weights from PEFT approaches are added on top of the pretrained LLM. So the same LLM can be used for multiple tasks by adding small weights without having to replace the entire model.In short, PEFT approaches enable you to get performance comparable to full fine-tuning while only having a small number of trainable parameters.Today, we are excited to introduce the 🤗 PEFT library, which provides the latest Parameter-Efficient Fine-tuning techniques seamlessly integrated with 🤗 Transformers and 🤗 Accelerate. This enables using the most popular and performant models from Transformers coupled with the simplicity and scalability of Accelerate. Below are the currently supported PEFT methods, with more coming soon:LoRA: LORA: LOW-RANK ADAPTATION OF LARGE LANGUAGE MODELSPrefix Tuning: P-Tuning v2: Prompt Tuning Can Be Comparable to Fine-tuning Universally Across Scales and TasksPrompt Tuning: The Power of Scale for Parameter-Efficient Prompt Tuning P-Tuning: GPT Understands, Too Use Cases We explore many interesting use cases here. These are a few of the most interesting ones: Using 🤗 PEFT LoRA for tuning bigscience/T0_3B model (3 Billion parameters) on consumer hardware with 11GB of RAM, such as Nvidia GeForce RTX 2080 Ti, Nvidia GeForce RTX 3080, etc using 🤗 Accelerate's DeepSpeed integration: peft_lora_seq2seq_accelerate_ds_zero3_offload.py. This means you can tune such large LLMs in Google Colab.Taking the previous example a notch up by enabling INT8 tuning of the OPT-6.7b model (6.7 Billion parameters) in Google Colab using 🤗 PEFT LoRA and bitsandbytes: Stable Diffusion Dreambooth training using 🤗 PEFT on consumer hardware with 11GB of RAM, such as Nvidia GeForce RTX 2080 Ti, Nvidia GeForce RTX 3080, etc. Try out the Space demo, which should run seamlessly on a T4 instance (16GB GPU): smangrul/peft-lora-sd-dreambooth.PEFT LoRA Dreambooth Gradio Space Training your model using 🤗 PEFT Let's consider the case of fine-tuning bigscience/mt0-large using LoRA. Let's get the necessary imports from transformers import AutoModelForSeq2SeqLM+ from peft import get_peft_model, LoraConfig, TaskType model_name_or_path = "bigscience/mt0-large" tokenizer_name_or_path = "bigscience/mt0-large"Creating config corresponding to the PEFT methodpeft_config = LoraConfig( task_type=TaskType.SEQ_2_SEQ_LM, inference_mode=False, r=8, lora_alpha=32, lora_dropout=0.1)Wrapping base 🤗 Transformers model by calling get_peft_model model = AutoModelForSeq2SeqLM.from_pretrained(model_name_or_path)+ model = get_peft_model(model, peft_config)+ model.print_trainable_parameters()# output: trainable params: 2359296 || all params: 1231940608 || trainable%: 0.19151053100118282That's it! The rest of the training loop remains the same. Please refer example peft_lora_seq2seq.ipynb for an end-to-end example.When you are ready to save the model for inference, just do the following.model.save_pretrained("output_dir") # model.push_to_hub("my_awesome_peft_model") also worksThis will only save the incremental PEFT weights that were trained. For example, you can find the bigscience/T0_3B tuned using LoRA on the twitter_complaints raft dataset here: smangrul/twitter_complaints_bigscience_T0_3B_LORA_SEQ_2_SEQ_LM. Notice that it only contains 2 files: adapter_config.json and adapter_model.bin with the latter being just 19MB.To load it for inference, follow the snippet below: from transformers import AutoModelForSeq2SeqLM+ from peft import PeftModel, PeftConfig peft_model_id = "smangrul/twitter_complaints_bigscience_T0_3B_LORA_SEQ_2_SEQ_LM" config = PeftConfig.from_pretrained(peft_model_id) model = AutoModelForSeq2SeqLM.from_pretrained(config.base_model_name_or_path)+ model = PeftModel.from_pretrained(model, peft_model_id) tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path) model = model.to(device) model.eval() inputs = tokenizer("Tweet text : @HondaCustSvc Your customer service has been horrible during the recall process. I will never purchase a Honda again. Label :", return_tensors="pt") with torch.no_grad(): outputs = model.generate(input_ids=inputs["input_ids"].to("cuda"), max_new_tokens=10) print(tokenizer.batch_decode(outputs.detach().cpu().numpy(), skip_special_tokens=True)[0])# 'complaint' Next steps We've released PEFT as an efficient way of tuning large LLMs on downstream tasks and domains, saving a lot of compute and storage while achieving comparable performance to full finetuning. In the coming months, we'll be exploring more PEFT methods, such as (IA)3 and bottleneck adapters. Also, we'll focus on new use cases such as INT8 training of whisper-large model in Google Colab and tuning of RLHF components such as policy and ranker using PEFT approaches.In the meantime, we're excited to see how industry practitioners apply PEFT to their use cases - if you have any questions or feedback, open an issue on our GitHub repo 🤗.Happy Parameter-Efficient Fine-Tuning!
https://huggingface.co/blog/clipseg-zero-shot
Zero-shot image segmentation with CLIPSeg
Tobias Cornille, Niels Rogge
December 21, 2022
This guide shows how you can use CLIPSeg, a zero-shot image segmentation model, using 🤗 transformers. CLIPSeg creates rough segmentation masks that can be used for robot perception, image inpainting, and many other tasks. If you need more precise segmentation masks, we’ll show how you can refine the results of CLIPSeg on Segments.ai.Image segmentation is a well-known task within the field of computer vision. It allows a computer to not only know what is in an image (classification), where objects are in the image (detection), but also what the outlines of those objects are. Knowing the outlines of objects is essential in fields such as robotics and autonomous driving. For example, a robot has to know the shape of an object to grab it correctly. Segmentation can also be combined with image inpainting to allow users to describe which part of the image they want to replace.One limitation of most image segmentation models is that they only work with a fixed list of categories. For example, you cannot simply use a segmentation model trained on oranges to segment apples. To teach the segmentation model an additional category, you have to label data of the new category and train a new model, which can be costly and time-consuming. But what if there was a model that can already segment almost any kind of object, without any further training? That’s exactly what CLIPSeg, a zero-shot segmentation model, achieves.Currently, CLIPSeg still has its limitations. For example, the model uses images of 352 x 352 pixels, so the output is quite low-resolution. This means we cannot expect pixel-perfect results when we work with images from modern cameras. If we want more precise segmentations, we can fine-tune a state-of-the-art segmentation model, as shown in our previous blog post. In that case, we can still use CLIPSeg to generate some rough labels, and then refine them in a labeling tool such as Segments.ai. Before we describe how to do that, let’s first take a look at how CLIPSeg works.CLIP: the magic model behind CLIPSegCLIP, which stands for Contrastive Language–Image Pre-training, is a model developed by OpenAI in 2021. You can give CLIP an image or a piece of text, and CLIP will output an abstract representation of your input. This abstract representation, also called an embedding, is really just a vector (a list of numbers). You can think of this vector as a point in high-dimensional space. CLIP is trained so that the representations of similar pictures and texts are similar as well. This means that if we input an image and a text description that fits that image, the representations of the image and the text will be similar (i.e., the high-dimensional points will be close together).At first, this might not seem very useful, but it is actually very powerful. As an example, let’s take a quick look at how CLIP can be used to classify images without ever having been trained on that task. To classify an image, we input the image and the different categories we want to choose from to CLIP (e.g. we input an image and the words “apple”, “orange”, …). CLIP then gives us back an embedding of the image and of each category. Now, we simply have to check which category embedding is closest to the embedding of the image, et voilà! Feels like magic, doesn’t it? Example of image classification using CLIP (source).What’s more, CLIP is not only useful for classification, but it can also be used for image search (can you see how this is similar to classification?), text-to-image models (DALL-E 2 is powered by CLIP), object detection (OWL-ViT), and most importantly for us: image segmentation. Now you see why CLIP was truly a breakthrough in machine learning.The reason why CLIP works so well is that the model was trained on a huge dataset of images with text captions. The dataset contained a whopping 400 million image-text pairs taken from the internet. These images contain a wide variety of objects and concepts, and CLIP is great at creating a representation for each of them.CLIPSeg: image segmentation with CLIPCLIPSeg is a model that uses CLIP representations to create image segmentation masks. It was published by Timo Lüddecke and Alexander Ecker. They achieved zero-shot image segmentation by training a Transformer-based decoder on top of the CLIP model, which is kept frozen. The decoder takes in the CLIP representation of an image, and the CLIP representation of the thing you want to segment. Using these two inputs, the CLIPSeg decoder creates a binary segmentation mask. To be more precise, the decoder doesn’t only use the final CLIP representation of the image we want to segment, but it also uses the outputs of some of the layers of CLIP. SourceThe decoder is trained on the PhraseCut dataset, which contains over 340,000 phrases with corresponding image segmentation masks. The authors also experimented with various augmentations to expand the size of the dataset. The goal here is not only to be able to segment the categories that are present in the dataset, but also to segment unseen categories. Experiments indeed show that the decoder can generalize to unseen categories. One interesting feature of CLIPSeg is that both the query (the image we want to segment) and the prompt (the thing we want to segment in the image) are input as CLIP embeddings. The CLIP embedding for the prompt can either come from a piece of text (the category name), or from another image. This means you can segment oranges in a photo by giving CLIPSeg an example image of an orange.This technique, which is called "visual prompting", is really helpful when the thing you want to segment is hard to describe. For example, if you want to segment a logo in a picture of a t-shirt, it's not easy to describe the shape of the logo, but CLIPSeg allows you to simply use the image of the logo as the prompt.The CLIPSeg paper contains some tips on improving the effectiveness of visual prompting. They find that cropping the query image (so that it only contains the object you want to segment) helps a lot. Blurring and darkening the background of the query image also helps a little bit. In the next section, we'll show how you can try out visual prompting yourself using 🤗 transformers.Using CLIPSeg with Hugging Face TransformersUsing Hugging Face Transformers, you can easily download and run apre-trained CLIPSeg model on your images. Let's start by installingtransformers.!pip install -q transformersTo download the model, simply instantiate it.from transformers import CLIPSegProcessor, CLIPSegForImageSegmentationprocessor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined")Now we can load an image to try out the segmentation. We'll choose apicture of a delicious breakfast taken by CalumLewis.from PIL import Imageimport requestsurl = "https://unsplash.com/photos/8Nc_oQsc2qQ/download?ixid=MnwxMjA3fDB8MXxhbGx8fHx8fHx8fHwxNjcxMjAwNzI0&force=true&w=640"image = Image.open(requests.get(url, stream=True).raw)imageText promptingLet's start by defining some text categories we want to segment.prompts = ["cutlery", "pancakes", "blueberries", "orange juice"]Now that we have our inputs, we can process them and input them to themodel.import torchinputs = processor(text=prompts, images=[image] * len(prompts), padding="max_length", return_tensors="pt")# predictwith torch.no_grad():outputs = model(**inputs)preds = outputs.logits.unsqueeze(1)Finally, let's visualize the output.import matplotlib.pyplot as plt_, ax = plt.subplots(1, len(prompts) + 1, figsize=(3*(len(prompts) + 1), 4))[a.axis('off') for a in ax.flatten()]ax[0].imshow(image)[ax[i+1].imshow(torch.sigmoid(preds[i][0])) for i in range(len(prompts))];[ax[i+1].text(0, -15, prompt) for i, prompt in enumerate(prompts)];Visual promptingAs mentioned before, we can also use images as the input prompts (i.e.in place of the category names). This can be especially useful if it'snot easy to describe the thing you want to segment. For this example,we'll use a picture of a coffee cup taken by DanielHooper.url = "https://unsplash.com/photos/Ki7sAc8gOGE/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8MTJ8fGNvZmZlJTIwdG8lMjBnb3xlbnwwfHx8fDE2NzExOTgzNDQ&force=true&w=640"prompt = Image.open(requests.get(url, stream=True).raw)promptWe can now process the input image and prompt image and input them tothe model.encoded_image = processor(images=[image], return_tensors="pt")encoded_prompt = processor(images=[prompt], return_tensors="pt")# predictwith torch.no_grad():outputs = model(**encoded_image, conditional_pixel_values=encoded_prompt.pixel_values)preds = outputs.logits.unsqueeze(1)preds = torch.transpose(preds, 0, 1)Then, we can visualize the results as before._, ax = plt.subplots(1, 2, figsize=(6, 4))[a.axis('off') for a in ax.flatten()]ax[0].imshow(image)ax[1].imshow(torch.sigmoid(preds[0]))Let's try one last time by using the visual prompting tips described inthe paper, i.e. cropping the image and darkening the background.url = "https://i.imgur.com/mRSORqz.jpg"alternative_prompt = Image.open(requests.get(url, stream=True).raw)alternative_promptencoded_alternative_prompt = processor(images=[alternative_prompt], return_tensors="pt")# predictwith torch.no_grad():outputs = model(**encoded_image, conditional_pixel_values=encoded_alternative_prompt.pixel_values)preds = outputs.logits.unsqueeze(1)preds = torch.transpose(preds, 0, 1)_, ax = plt.subplots(1, 2, figsize=(6, 4))[a.axis('off') for a in ax.flatten()]ax[0].imshow(image)ax[1].imshow(torch.sigmoid(preds[0]))In this case, the result is pretty much the same. This is probablybecause the coffee cup was already separated well from the background inthe original image.Using CLIPSeg to pre-label images on Segments.aiAs you can see, the results from CLIPSeg are a little fuzzy and verylow-res. If we want to obtain better results, you can fine-tune astate-of-the-art segmentation model, as explained in our previousblogpost. To finetunethe model, we'll need labeled data. In this section, we'll show youhow you can use CLIPSeg to create some rough segmentation masks and thenrefine them onSegments.ai,a labeling platform with smart labeling tools for image segmentation.First, create an account athttps://segments.ai/joinand install the Segments Python SDK. Then you can initialize theSegments.ai Python client using an API key. This key can be found onthe account page.!pip install -q segments-aifrom segments import SegmentsClientfrom getpass import getpassapi_key = getpass('Enter your API key: ')segments_client = SegmentsClient(api_key)Next, let's load an image from a dataset using the Segments client.We'll use the a2d2 self-drivingdataset. You can also create yourown dataset by following theseinstructions.samples = segments_client.get_samples("admin-tobias/clipseg")# Use the last image as an examplesample = samples[1]image = Image.open(requests.get(sample.attributes.image.url, stream=True).raw)imageWe also need to get the category names from the dataset attributes.dataset = segments_client.get_dataset("admin-tobias/clipseg")category_names = [category.name for category in dataset.task_attributes.categories]Now we can use CLIPSeg on the image as before. This time, we'll alsoscale up the outputs so that they match the input image's size.from torch import nninputs = processor(text=category_names, images=[image] * len(category_names), padding="max_length", return_tensors="pt")# predictwith torch.no_grad():outputs = model(**inputs)# resize the outputspreds = nn.functional.interpolate(outputs.logits.unsqueeze(1),size=(image.size[1], image.size[0]),mode="bilinear")And we can visualize the results again.len_cats = len(category_names)_, ax = plt.subplots(1, len_cats + 1, figsize=(3*(len_cats + 1), 4))[a.axis('off') for a in ax.flatten()]ax[0].imshow(image)[ax[i+1].imshow(torch.sigmoid(preds[i][0])) for i in range(len_cats)];[ax[i+1].text(0, -15, category_name) for i, category_name in enumerate(category_names)];Now we have to combine the predictions to a single segmented image.We'll simply do this by taking the category with the greatest sigmoidvalue for each patch. We'll also make sure that all the values under acertain threshold do not count.threshold = 0.1flat_preds = torch.sigmoid(preds.squeeze()).reshape((preds.shape[0], -1))# Initialize a dummy "unlabeled" mask with the thresholdflat_preds_with_treshold = torch.full((preds.shape[0] + 1, flat_preds.shape[-1]), threshold)flat_preds_with_treshold[1:preds.shape[0]+1,:] = flat_preds# Get the top mask index for each pixelinds = torch.topk(flat_preds_with_treshold, 1, dim=0).indices.reshape((preds.shape[-2], preds.shape[-1]))Let's quickly visualize the result.plt.imshow(inds)Lastly, we can upload the prediction to Segments.ai. To do that, we'llfirst convert the bitmap to a png file, then we'll upload this file tothe Segments, and finally we'll add the label to the sample.from segments.utils import bitmap2fileimport numpy as npinds_np = inds.numpy().astype(np.uint32)unique_inds = np.unique(inds_np).tolist()f = bitmap2file(inds_np, is_segmentation_bitmap=True)asset = segments_client.upload_asset(f, "clipseg_prediction.png")attributes = {'format_version': '0.1','annotations': [{"id": i, "category_id": i} for i in unique_inds if i != 0],'segmentation_bitmap': { 'url': asset.url },}segments_client.add_label(sample.uuid, 'ground-truth', attributes)If you take a look at the uploaded prediction onSegments.ai,you can see that it's not perfect. However, you can manually correctthe biggest mistakes, and then you can use the corrected dataset totrain a better model than CLIPSeg.ConclusionCLIPSeg is a zero-shot segmentation model that works with both text and image prompts. The model adds a decoder to CLIP and can segment almost anything. However, the output segmentation masks are still very low-res for now, so you’ll probably still want to fine-tune a different segmentation model if accuracy is important. Note that there's more research on zero-shot segmentation currently being conducted, so you can expect more models to be added in the near future. One example is GroupViT, which is already available in 🤗 Transformers. To stay up to date with the latest news in segmentation research, you can follow us on Twitter: @TobiasCornille, @NielsRogge, and @huggingface.If you’re interested in learning how to fine-tune a state-of-the-art segmentation model, check out our previous blog post: https://huggingface.co/blog/fine-tune-segformer.
https://huggingface.co/blog/infinity-cpu-performance
Case Study: Millisecond Latency using Hugging Face Infinity and modern CPUs
Philipp Schmid, Jeff Boudier, Morgan Funtowicz
January 13, 2022
Inference Endpoints to easily deploy models on dedicated infrastructure managed by Hugging Face.Our open-source optimization libraries, 🤗 Optimum Intel and 🤗 Optimum ONNX Runtime, to get the highest efficiency out of training and running models for inference.Hugging Face Expert Acceleration Program, a commercial service for Hugging Face experts to work directly with your team to accelerate your Machine Learning roadmap and models.IntroductionTransfer learning has changed Machine Learning by reaching new levels of accuracy from Natural Language Processing (NLP) to Audio and Computer Vision tasks. At Hugging Face, we work hard to make these new complex models and large checkpoints as easily accessible and usable as possible. But while researchers and data scientists have converted to the new world of Transformers, few companies have been able to deploy these large, complex models in production at scale.The main bottleneck is the latency of predictions which can make large deployments expensive to run and real-time use cases impractical. Solving this is a difficult engineering challenge for any Machine Learning Engineering team and requires the use of advanced techniques to optimize models all the way down to the hardware.With Hugging Face Infinity, we offer a containerized solution that makes it easy to deploy low-latency, high-throughput, hardware-accelerated inference pipelines for the most popular Transformer models. Companies can get both the accuracy of Transformers and the efficiency necessary for large volume deployments, all in a simple to use package. In this blog post, we want to share detailed performance results for Infinity running on the latest generation of Intel Xeon CPU, to achieve optimal cost, efficiency, and latency for your Transformer deployments.What is Hugging Face InfinityHugging Face Infinity is a containerized solution for customers to deploy end-to-end optimized inference pipelines for State-of-the-Art Transformer models, on any infrastructure.Hugging Face Infinity consists of 2 main services:The Infinity Container is a hardware-optimized inference solution delivered as a Docker container.Infinity Multiverse is a Model Optimization Service through which a Hugging Face Transformer model is optimized for the Target Hardware. Infinity Multiverse is compatible with Infinity Container.The Infinity Container is built specifically to run on a Target Hardware architecture and exposes an HTTP /predict endpoint to run inference.Figure 1. Infinity OverviewAn Infinity Container is designed to serve 1 Model and 1 Task. A Task corresponds to machine learning tasks as defined in the Transformers Pipelines documentation. As of the writing of this blog post, supported tasks include feature extraction/document embedding, ranking, sequence classification, and token classification.You can find more information about Hugging Face Infinity at hf.co/infinity, and if you are interested in testing it for yourself, you can sign up for a free trial at hf.co/infinity-trial.BenchmarkInference performance benchmarks often only measure the execution of the model. In this blog post, and when discussing the performance of Infinity, we always measure the end-to-end pipeline including pre-processing, prediction, post-processing. Please keep this in mind when comparing these results with other latency measurements. Figure 2. Infinity End-to-End PipelineEnvironmentAs a benchmark environment, we are going to use the Amazon EC2 C6i instances, which are compute-optimized instances powered by the 3rd generation of Intel Xeon Scalable processors. These new Intel-based instances are using the ice-lake Process Technology and support Intel AVX-512, Intel Turbo Boost, and Intel Deep Learning Boost.In addition to superior performance for machine learning workloads, the Intel Ice Lake C6i instances offer great cost-performance and are our recommendation to deploy Infinity on Amazon Web Services. To learn more, visit the EC2 C6i instance page. MethodologiesWhen it comes to benchmarking BERT-like models, two metrics are most adopted:Latency: Time it takes for a single prediction of the model (pre-process, prediction, post-process)Throughput: Number of executions performed in a fixed amount of time for one benchmark configuration, respecting Physical CPU cores, Sequence Length, and Batch SizeThese two metrics will be used to benchmark Hugging Face Infinity across different setups to understand the benefits and tradeoffs in this blog post.ResultsTo run the benchmark, we created an infinity container for the EC2 C6i instance (Ice-lake) and optimized a DistilBERT model for sequence classification using Infinity Multiverse. This ice-lake optimized Infinity Container can achieve up to 34% better latency & throughput compared to existing cascade-lake-based instances, and up to 800% better latency & throughput compared to vanilla transformers running on ice-lake.The Benchmark we created consists of 192 different experiments and configurations. We ran experiments for: Physical CPU cores: 1, 2, 4, 8Sequence length: 8, 16, 32, 64, 128, 256, 384, 512Batch_size: 1, 2, 4, 8, 16, 32In each experiment, we collect numbers for:Throughput (requests per second)Latency (min, max, avg, p90, p95, p99)You can find the full data of the benchmark in this google spreadsheet: 🤗 Infinity: CPU Ice-Lake Benchmark.In this blog post, we will highlight a few results of the benchmark including the best latency and throughput configurations.In addition to this, we deployed the DistilBERT model we used for the benchmark as an API endpoint on 2 physical cores. You can test it and get a feeling for the performance of Infinity. Below you will find a curl command on how to send a request to the hosted endpoint. The API returns a x-compute-time HTTP Header, which contains the duration of the end-to-end pipeline.curl --request POST `-i` \--url https://infinity.huggingface.co/cpu/distilbert-base-uncased-emotion \--header 'Content-Type: application/json' \--data '{"inputs":"I like you. I love you"}'ThroughputBelow you can find the throughput comparison for running infinity on 2 physical cores with batch size 1, compared with vanilla transformers.Figure 3. Throughput: Infinity vs TransformersSequence LengthInfinityTransformersimprovement8248 req/sec49 req/sec+506%16212 req/sec50 req/sec+424%32150 req/sec40 req/sec+375%6497 req/sec28 req/sec+346%12855 req/sec18 req/sec+305%25627 req/sec9 req/sec+300%38417 req/sec5 req/sec+340%51212 req/sec4 req/sec+300%LatencyBelow, you can find the latency results for an experiment running Hugging Face Infinity on 2 Physical Cores with Batch Size 1. It is remarkable to see how robust and constant Infinity is, with minimal deviation for p95, p99, or p100 (max latency). This result is confirmed for other experiments as well in the benchmark. Figure 4. Latency (Batch=1, Physical Cores=2)ConclusionIn this post, we showed how Hugging Face Infinity performs on the new Intel Ice Lake Xeon CPU. We created a detailed benchmark with over 190 different configurations sharing the results you can expect when using Hugging Face Infinity on CPU, what would be the best configuration to optimize your Infinity Container for latency, and what would be the best configuration to maximize throughput.Hugging Face Infinity can deliver up to 800% higher throughput compared to vanilla transformers, and down to 1-4ms latency for sequence lengths up to 64 tokens.The flexibility to optimize transformer models for throughput, latency, or both enables businesses to either reduce the amount of infrastructure cost for the same workload or to enable real-time use cases that were not possible before. If you are interested in trying out Hugging Face Infinity sign up for your trial at hf.co/infinity-trial ResourcesHugging Face InfinityHugging Face Infinity TrialAmazon EC2 C6i instances DistilBERTDistilBERT paperDistilBERT model🤗 Infinity: CPU Ice-Lake Benchmark
https://huggingface.co/blog/ort-accelerating-hf-models
Accelerating over 130,000 Hugging Face models with ONNX Runtime
Sophie Schoenmeyer, Morgan Funtowicz
October 4, 2023
What is ONNX Runtime?ONNX Runtime is a cross-platform machine learning tool that can be used to accelerate a wide variety of models, particularly those with ONNX support.Hugging Face ONNX Runtime SupportThere are over 130,000 ONNX-supported models on Hugging Face, an open source community that allows users to build, train, and deploy hundreds of thousands of publicly available machine learning models.These ONNX-supported models, which include many increasingly popular large language models (LLMs) and cloud models, can leverage ONNX Runtime to improve performance, along with other benefits.For example, using ONNX Runtime to accelerate the whisper-tiny model can improve average latency per inference, with an up to 74.30% gain over PyTorch.ONNX Runtime works closely with Hugging Face to ensure that the most popular models on the site are supported.In total, over 90 Hugging Face model architectures are supported by ONNX Runtime, including the 11 most popular architectures (where popularity is determined by the corresponding number of models uploaded to the Hugging Face Hub):Model ArchitectureApproximate No. of ModelsBERT28180GPT214060DistilBERT11540RoBERTa10800T510450Wav2Vec26560Stable-Diffusion5880XLM-RoBERTa5100Whisper4400BART3590Marian2840Learn MoreTo learn more about accelerating Hugging Face models with ONNX Runtime, check out our recent post on the Microsoft Open Source Blog.
https://huggingface.co/blog/inference-endpoints-llm
Deploy LLMs with Hugging Face Inference Endpoints
Philipp Schmid
July 4, 2023
Open-source LLMs like Falcon, (Open-)LLaMA, X-Gen, StarCoder or RedPajama, have come a long way in recent months and can compete with closed-source models like ChatGPT or GPT4 for certain use cases. However, deploying these models in an efficient and optimized way still presents a challenge.In this blog post, we will show you how to deploy open-source LLMs to Hugging Face Inference Endpoints, our managed SaaS solution that makes it easy to deploy models. Additionally, we will teach you how to stream responses and test the performance of our endpoints. So let's get started!How to deploy Falcon 40B instructTest the LLM endpointStream responses in Javascript and PythonBefore we start, let's refresh our knowledge about Inference Endpoints. What is Hugging Face Inference EndpointsHugging Face Inference Endpoints offers an easy and secure way to deploy Machine Learning models for use in production. Inference Endpoints empower developers and data scientists alike to create AI applications without managing infrastructure: simplifying the deployment process to a few clicks, including handling large volumes of requests with autoscaling, reducing infrastructure costs with scale-to-zero, and offering advanced security. Here are some of the most important features for LLM deployment:Easy Deployment: Deploy models as production-ready APIs with just a few clicks, eliminating the need to handle infrastructure or MLOps.Cost Efficiency: Benefit from automatic scale to zero capability, reducing costs by scaling down the infrastructure when the endpoint is not in use, while paying based on the uptime of the endpoint, ensuring cost-effectiveness.Enterprise Security: Deploy models in secure offline endpoints accessible only through direct VPC connections, backed by SOC2 Type 2 certification, and offering BAA and GDPR data processing agreements for enhanced data security and compliance.LLM Optimization: Optimized for LLMs, enabling high throughput with Paged Attention and low latency through custom transformers code and Flash Attention power by Text Generation InferenceComprehensive Task Support: Out of the box support for 🤗 Transformers, Sentence-Transformers, and Diffusers tasks and models, and easy customization to enable advanced tasks like speaker diarization or any Machine Learning task and library.You can get started with Inference Endpoints at: https://ui.endpoints.huggingface.co/1. How to deploy Falcon 40B instructTo get started, you need to be logged in with a User or Organization account with a payment method on file (you can add one here), then access Inference Endpoints at https://ui.endpoints.huggingface.coThen, click on “New endpoint”. Select the repository, the cloud, and the region, adjust the instance and security settings, and deploy in our case tiiuae/falcon-40b-instruct.Inference Endpoints suggest an instance type based on the model size, which should be big enough to run the model. Here 4x NVIDIA T4 GPUs. To get the best performance for the LLM, change the instance to GPU [xlarge] · 1x Nvidia A100.Note: If the instance type cannot be selected, you need to contact us and request an instance quota.You can then deploy your model with a click on “Create Endpoint”. After 10 minutes, the Endpoint should be online and available to serve requests. 2. Test the LLM endpointThe Endpoint overview provides access to the Inference Widget, which can be used to manually send requests. This allows you to quickly test your Endpoint with different inputs and share it with team members. Those Widgets do not support parameters - in this case this results to a “short” generation. The widget also generates a cURL command you can use. Just add your hf_xxx and test. curl https://j4xhm53fxl9ussm8.us-east-1.aws.endpoints.huggingface.cloud \-X POST \-d '{"inputs":"Once upon a time,"}' \-H "Authorization: Bearer <hf_token>" \-H "Content-Type: application/json"You can use different parameters to control the generation, defining them in the parameters attribute of the payload. As of today, the following parameters are supported:temperature: Controls randomness in the model. Lower values will make the model more deterministic and higher values will make the model more random. Default value is 1.0.max_new_tokens: The maximum number of tokens to generate. Default value is 20, max value is 512.repetition_penalty: Controls the likelihood of repetition. Default is null.seed: The seed to use for random generation. Default is null.stop: A list of tokens to stop the generation. The generation will stop when one of the tokens is generated.top_k: The number of highest probability vocabulary tokens to keep for top-k-filtering. Default value is null, which disables top-k-filtering.top_p: The cumulative probability of parameter highest probability vocabulary tokens to keep for nucleus sampling, default to nulldo_sample: Whether or not to use sampling; use greedy decoding otherwise. Default value is false.best_of: Generate best_of sequences and return the one if the highest token logprobs, default to null.details: Whether or not to return details about the generation. Default value is false.return_full_text: Whether or not to return the full text or only the generated part. Default value is false.truncate: Whether or not to truncate the input to the maximum length of the model. Default value is true.typical_p: The typical probability of a token. Default value is null.watermark: The watermark to use for the generation. Default value is false.3. Stream responses in Javascript and PythonRequesting and generating text with LLMs can be a time-consuming and iterative process. A great way to improve the user experience is streaming tokens to the user as they are generated. Below are two examples of how to stream tokens using Python and JavaScript. For Python, we are going to use the client from Text Generation Inference, and for JavaScript, the HuggingFace.js libraryStreaming requests with PythonFirst, you need to install the huggingface_hub library:pip install -U huggingface_hubWe can create a InferenceClient providing our endpoint URL and credential alongside the hyperparameters we want to usefrom huggingface_hub import InferenceClient# HF Inference Endpoints parameterendpoint_url = "https://YOUR_ENDPOINT.endpoints.huggingface.cloud"hf_token = "hf_YOUR_TOKEN"# Streaming Clientclient = InferenceClient(endpoint_url, token=hf_token)# generation parametergen_kwargs = dict(max_new_tokens=512,top_k=30,top_p=0.9,temperature=0.2,repetition_penalty=1.02,stop_sequences=["User:", "<|endoftext|>", "</s>"],)# promptprompt = "What can you do in Nuremberg, Germany? Give me 3 Tips"stream = client.text_generation(prompt, stream=True, details=True, **gen_kwargs)# yield each generated tokenfor r in stream:# skip special tokensif r.token.special:continue# stop if we encounter a stop sequenceif r.token.text in gen_kwargs["stop_sequences"]:break# yield the generated tokenprint(r.token.text, end = "")# yield r.token.textReplace the print command with the yield or with a function you want to stream the tokens to. Streaming requests with JavaScriptFirst, you need to install the @huggingface/inference library.npm install @huggingface/inferenceWe can create a HfInferenceEndpoint providing our endpoint URL and credential alongside the hyperparameter we want to use.import { HfInferenceEndpoint } from '@huggingface/inference'const hf = new HfInferenceEndpoint('https://YOUR_ENDPOINT.endpoints.huggingface.cloud', 'hf_YOUR_TOKEN')//generation parameterconst gen_kwargs = {max_new_tokens: 512,top_k: 30,top_p: 0.9,temperature: 0.2,repetition_penalty: 1.02,stop_sequences: ['User:', '<|endoftext|>', '</s>'],}// promptconst prompt = 'What can you do in Nuremberg, Germany? Give me 3 Tips'const stream = hf.textGenerationStream({ inputs: prompt, parameters: gen_kwargs })for await (const r of stream) {// # skip special tokensif (r.token.special) {continue}// stop if we encounter a stop sequenceif (gen_kwargs['stop_sequences'].includes(r.token.text)) {break}// yield the generated tokenprocess.stdout.write(r.token.text)}Replace the process.stdout call with the yield or with a function you want to stream the tokens to. ConclusionIn this blog post, we showed you how to deploy open-source LLMs using Hugging Face Inference Endpoints, how to control the text generation with advanced parameters, and how to stream responses to a Python or JavaScript client to improve the user experience. By using Hugging Face Inference Endpoints you can deploy models as production-ready APIs with just a few clicks, reduce your costs with automatic scale to zero, and deploy models into secure offline endpoints backed by SOC2 Type 2 certification.Thanks for reading! If you have any questions, feel free to contact me on Twitter or LinkedIn.
https://huggingface.co/blog/sc2-instruct
StarCoder2-Instruct: Fully Transparent and Permissive Self-Alignment for Code Generation
Yuxiang Wei, Federico Cassano, Jiawei Liu, Yifeng Ding, Naman Jain, Harm de Vries, Leandro von Werra, Arjun Guha, Lingming Zhang
April 29, 2024
Instruction tuning is an approach of fine-tuning that gives large language models (LLMs) the capability to follow natural and human-written instructions. However, for programming tasks, most models are tuned on either human-written instructions (which are very expensive) or instructions generated by huge and proprietary LLMs (which may not be permitted). We introduce StarCoder2-15B-Instruct-v0.1, the very first entirely self-aligned code LLM trained with a fully permissive and transparent pipeline. Our open-source pipeline uses StarCoder2-15B to generate thousands of instruction-response pairs, which are then used to fine-tune StarCoder-15B itself without any human annotations or distilled data from huge and proprietary LLMs.StarCoder2-15B-Instruct achieves a 72.6 HumanEval score, even surpassing the 72.0 score of CodeLlama-70B-Instruct! Further evaluation on LiveCodeBench shows that the self-aligned model is even better than the same model trained on data distilled from GPT-4, implying that an LLM could learn more effectively from data within its own distribution than a shifted distribution from a teacher LLM.Method Our data generation pipeline mainly consists of three steps:Extract high-quality and diverse seed functions from The Stack v1, a huge corpus of permissively licensed source code.Create diverse and realistic code instructions that incorporate different code concepts present in the seed functions (e.g., data deserialization, list concatenation, and recursion).For each instruction, generate a high-quality response through execution-guided self-validation.In the following sections, we will explore each of these aspects in detail.Collecting seed code snippets To fully unlock the instruction-following capabilities of a code model, it should be exposed to a diverse set of instructions encompassing a wide range of programming principles and practices. Motivated by OSS-Instruct, we further promote such diversity by mining code concepts from open-source code snippets that are, specifically, well-formed seed Python functions from The Stack V1.For our seed dataset, we carefully extract all Python functions with docstrings in The Stack V1, infer dependencies required using autoimport, and apply the following filtering rules on all functions:Type checking: We apply the Pyright heuristic type-checker to remove all functions that produce static errors, signaling a possibly incorrect item.Decontamination: We detect and remove all benchmark items on which we evaluate. We use exact string match on both the solutions and prompts.Docstring Quality Filtering: We utilize StarCoder2-15B as a judge to remove functions with poor documentation. We prompt the base model with 7 few-shot examples, requiring it to respond with either "Yes" or "No" for retaining the item.Near-Deduplication: We utilize MinHash and locality-sensitive hashing with a Jaccard similarity threshold of 0.5 to filter duplicate seed functions in our dataset. This is the same process applied to StarCoder’s training data.This filtering pipeline results in a dataset of 250k Python functions filtered from 5M functions with docstrings. This process is highly inspired by the data collection pipeline used in MultiPL-T.Self-OSS-Instruct After collecting the seed functions, we use Self-OSS-Instruct to generate diverse instructions. In detail, we employ in-context learning to let the base StarCoder2-15B self-generate instructions from the given seed code snippets. This process utilizes 16 carefully designed few-shot examples, each formatted as (snippet, concepts, instruction). The instruction generation procedure is divided into two steps:Concepts extraction: For each seed function, StarCoder2-15B is prompted to produce a list of code concepts present within the function. Code concepts refer to the foundational principles and techniques used in programming, such as pattern matching and data type conversion, which are crucial for developers to master.Instruction generation: StarCoder2-15B is then prompted to self-generate a coding task that incorporates the identified code concepts.Eventually, 238k instructions are generated from this process.Response self-validation Given the instructions generated from Self-OSS-Instruct, our next step is to match each instruction with a high-quality response. Prior practices commonly rely on distilling responses from stronger teacher models, such as GPT-4, which hopefully exhibit higher quality. However, distilling proprietary models leads to non-permissive licensing and a stronger teacher model might not always be available. More importantly, teacher models can be wrong as well, and the distribution gap between teacher and student can be detrimental.We propose to self-align StarCoder2-15B by explicitly instructing the model to generate tests for self-validation after it produces a response interleaved with natural language. This process is similar to how developers test their code implementations. Specifically, for each instruction, StarCoder2-15B generates 10 samples of the format (NL Response, Test) and we filter out those falsified by the test execution under a sandbox environment. We then randomly select one passing response per instruction to the final SFT dataset. In total, we generated 2.4M (10 x 238k) responses for the 238k instructions with temperature 0.7, where 500k passed the execution test. After deduplication, we are left with 50k instructions, each paired with a random passing response, which we finally use as our SFT dataset.Evaluation On the popular and rigorous EvalPlus benchmark, StarCoder2-15B-Instruct stands out as the top-performing permissive LLM at its scale, outperforming the much larger Grok-1 Command-R+, DBRX, while closely matching Snowflake Arctic 480B and Mixtral-8x22B-Instruct. To our knowledge, StarCoder2-15B-Instruct is the first code LLM with a fully transparent and permissive pipeline reaching a 70+ HumanEval score. It drastically outperforms OctoCoder, which is the previous state-of-the-art permissive code LLM with a transparent pipeline.Even compared to powerful LLMs with restrictive licenses, StarCoder2-15B-Instruct remains competitive, surpassing Gemini Pro and Mistral Large and comparable to CodeLlama-70B-Instruct. Additionally, StarCoder2-15B-Instruct, trained purely on self-generated data, closely rivals OpenCodeInterpreter-SC2-15B, which finetunes StarCoder2-15B on distilled data from GPT-3.5/4.Besides EvalPlus, we also evaluated state-of-the-art open-source models with similar or smaller sizes on LiveCodeBench, which includes fresh coding problems created after 2023-09-01, as well as DS-1000 that targets data science programs. On LiveCodeBench, StarCoder2-15B-Instruct achieves the best results among the models evaluated and consistently outperforms OpenCodeInterpreter-SC2-15B which distills GPT-4 data. On DS-1000, the StarCoder2-15B-Instruct is still competitive despite being trained on very limited data science problems.Conclusion StarCoder2-15B-Instruct-v0.1 showcases for the first time that we can create powerful instruction-tuned code models without relying on stronger teacher models like GPT-4. This model demonstrates that self-alignment, where a model uses its own generated content to learn, is also effective for code. It is fully transparent and allows for distillation, setting it apart from other larger permissive but non-transparent models such as Snowflake-Arctic, Grok-1, Mixtral-8x22B, DBRX, and CommandR+. We have made our datasets and the entire pipeline, including data curation and training, fully open-source. We hope this seminal work can inspire more future research and development in this field.Resources StarCoder2-15B-Instruct-v0.1: the instruction-tuned modelstarcoder2-self-align: the self-alignment pipelineStarCoder2-Self-OSS-Instruct: the self-generated, instruction-tuning dataset
https://huggingface.co/blog/leaderboard-nphardeval
NPHardEval Leaderboard: Unveiling the Reasoning Abilities of Large Language Models through Complexity Classes and Dynamic Updates
Lizhou Fan, Wenyue Hua, Haoyang Ling, Clémentine Fourrier
February 2, 2024
We're happy to introduce the NPHardEval leaderboard, using NPHardEval, a cutting-edge benchmark developed by researchers from the University of Michigan and Rutgers University. NPHardEval introduces a dynamic, complexity-based framework for assessing Large Language Models' (LLMs) reasoning abilities. It poses 900 algorithmic questions spanning the NP-Hard complexity class and lower, designed to rigorously test LLMs, and is updated on a monthly basis to prevent overfitting! A Unique Approach to LLM Evaluation NPHardEval stands apart by employing computational complexity classes, offering a quantifiable and robust measure of LLM reasoning skills. The benchmark's tasks mirror real-world decision-making challenges, enhancing its relevance and applicability. Regular monthly updates of the benchmark data points mitigate the risk of model overfitting, ensuring a reliable evaluation. The major contributions of NPHardEval are new using new benchmarking strategies (proposing an automatic and dynamic benchmark), and introducing a new way to evaluate LLM reasoning.Regarding benchmarking strategies, NPHardEval uses an automated mechanism, both to generate and check questions in the benchmark. Since they are based on algorithmically computable problems, human intervention is not required to determine the correctness of the responses from LLMs. This also allows NPHardEval to be a dynamic benchmark: since questions can be automatically generated, the benchmark can be updated on a monthly basis. This monthly-refreshed benchmark helps prevent model overfitting as we can always generate novel questions with varying difficulty levels for evaluation. The questions themselves use a new system to evaluate LLM Reasoning. The questions in the benchmark are grounded in the computational complexity hierarchy, a well-established concept extensively studied in theoretical computer science. This foundation enables us to leverage existing research to rigorously and quantitatively measure an LLM's logical reasoning extent, by defining reasoning via complexity classes. The benchmark also deliberatley excludes numerical computation from the questions, since it is a notoriously difficult task for LLMs. Focusing on logical questions allows for a more accurate evaluation of an LLM's pure logical reasoning ability, as numerical questions can obscure this assessment. Data Synthesis NPHardEval uses 100 questions for each of 9 different algorithms, with 10 difficulty levels, resulting in 900 questions across complexity and difficulty. The 9 algorithms, including 3 P, 3 NP-complete, and 3 NP-hard questions, are characterized according to the computing theory. The 900 questions are all synthesized and updated monthly.More background and insights are available in these slides. Evaluation Metrics We use two metrics to evaluate the reasoning ability of LLMs: Weighted Accuracy and Failure Rate. Weighted Accuracy (WA) Weighted Accuracy (WA) is used to evaluate problem-solving accuracy. This method is applied to each problem, either through comparison with a correct answer or via step-by-step result checking for problems without a singular answer. To reflect comparative accuracy more effectively, we assign weights to different difficulty levels. Each level's weight corresponds to its relative importance or challenge, with higher difficulty levels receiving more weight in a linear progression (for instance, level 1 has weight 1, level 2 has weight 2, and so on).The formula for Weighted Accuracy is as follows: WA=∑i=110(wi×Ai)∑i=110wi WA = \frac{\sum\limits_{i=1}^{10} (w_i \times A_i)}{\sum\limits_{i=1}^{10} w_i} WA=i=1∑10​wi​i=1∑10​(wi​×Ai​)​In this equation, wiw_iwi​ represents the weight assigned to difficulty level iii (ranging from 1 to 10), and AiA_iAi​ is the accuracy at that level. Failure Rate (FR) Another critical metric we consider is the Failure Rate (FR). This measure helps assess the frequency of unsuccessful outcomes across different problems and difficulty levels. It's particularly useful for identifying instances where an LLM's result does not match the expected output format.The Failure Rate is calculated by considering the proportion of failed attempts relative to the total number of attempts for each difficulty level. An attempt is counted as failed if the model generates results that cannot be successfully parsed in all endpoint calls. We set the maximum number of tries as 10. For each problem, the Failure Rate is then aggregated across all difficulty levels, considering the total of 10 attempts at each level.The formal definition of Failure Rate is: FR=∑i=110Fi100 FR = \frac{\sum\limits_{i=1}^{10} F_i}{100} FR=100i=1∑10​Fi​​Here, Fi F_i Fi​ denotes the number of failed attempts at difficulty level i i i. Experimentation and Insights The benchmark includes comprehensive experiments to analyze LLMs across various complexity classes and difficulty levels. It delves into the nuances of LLM performance, providing valuable insights into their reasoning strengths and limitations. In general:Closed-source models generally perform better than open-source models, with GPT 4 Turbo performing the best overall.Models generally perform better on less-complex questions, i.e. easier complexity classes, while not always linearly decrease on complexity levels. Models such as Claude 2 perform the best on NP-complete (middle-complexity) questions.Some open-source models can outperform close-source models on specific questions. Leading open-source models include Yi-34b, Qwen-14b, Phi-2, and Mistral-7b. Reproducing NPHardEval Benchmark results on your machine To set up the NPHardEval Benchmark, you need to follow a few steps:Environment setup: after cloning the repository to your local machine, install the required python library with conda. conda create --name llm_reason python==3.10conda activate llm_reasongit clone https://github.com/casmlab/NPHardEval.gitpip install -r requirements.txtSet-up API keys: fetch API keys and change the corresponding entries in secrets.txt.Example Commands: evaluate your model with the NPHardEval benchmark!For example, to use the GPT 4 Turbo model (GPT-4-1106-preview) and the edit distance problem (EDP) for evaluation: For its zeroshot experiment, we can use: cd Close/run python run_p_EDP.py gpt-4-1106-previewFor its fewshot experiment, cd Close/run python run_p_EDP_few.py gpt-4-1106-preview selfWe currently support fewshot examples from the same question (self), and may support examples from other questions (other) in the future. Join the Conversation The NPHardEval leaderboard, dataset and code are available on Github and Hugging Face for community access and contributions.We'll love to see community contributions and interest on the NPHardEval GitHub Repository and Hugging Face Leaderboard.
https://huggingface.co/blog/fetch-eap-case-study
Fetch Consolidates AI Tools and Saves 30% Development Time with Hugging Face on AWS
Violette Lepercq
February 23, 2023
If you need support in using Hugging Face and AWS, please get in touch with us here - our team will contact you to discuss your requirements! Executive Summary Fetch, a consumer rewards company, developed about 15 different AI tools to help it receive, route, read, process, analyze, and store receipts uploaded by users. The company has more than 18 million active monthly users for its shopping rewards app. Fetch wanted to rebuild its AI-powered platform and, using Amazon Web Services (AWS) and with the support of AWS Partner Hugging Face, moved from using third-party applications to developing its own tools to gain better insights about customers. Consumers scan receipts —or forward electronic receipts— to receive rewards points for their purchases. Businesses can offer special rewards to users, such as extra points for purchasing a particular product. The company can now process more than 11 million receipts per day faster and gets better data. Fetch Needed a Scalable Way to Train AI Faster Fetch—formerly Fetch Rewards—has grown since its founding to serve 18 million active users every month who scan 11 million receipts every day to earn reward points. Users simply take a picture of their receipt and upload it using the company’s app. Users can also upload electronic receipts. Receipts earn points; if the receipt is from a brand partner of Fetch, it may qualify for promotions that award additional points. Those points can be redeemed for gift cards from a number of partners. But scanning is just the beginning. Once Fetch receives the receipts, it must process them, extracting data and analytics and filing the data and the receipts. It has been using artificial intelligence (AI) tools running on AWS to do that.The company was using an AI solution from a third party to process receipts, but found it wasn’t getting the data insights it needed. Fetch’s business partners wanted information about how customers were engaging with their promotions, and Fetch didn’t have the granularity it needed to extract and process data from millions of receipts daily. “Fetch was using a third-party provider for its brain, which is scanning receipts, but scanning is not enough,” says Boris Kogan, computer vision scientist at Fetch. “That solution was a black box and we had no control or insight into what it did. We just got results we had to accept. We couldn’t give our business partners the information they wanted.”Kogan joined Fetch tasked with the job of building thorough machine learning (ML) and AI expertise into the company and giving it full access to all aspects of the data it was receiving. To do this, he hired a team of engineers to bring his vision to life. “All of our infrastructure runs on AWS, we also rely on the AWS products to train our models,” says Kogan. “When the team started working on creating a brain of our own, of course, we first had to train our models and we did that on AWS. We allocated 12 months for the project and completed it in 8 month because we always had the resources we needed.” Hugging Face Opens Up the Black Box The Fetch team engaged with AWS Partner Hugging Face through the Hugging Face Expert Acceleration Program on the AWS Marketplace to help Fetch unlock new tools to power processes after the scans had been uploaded. Hugging Face is a leader in open-source AI and provides guidance to enterprises on using AI. Many enterprises, including Fetch, use transformers from Hugging Face, which allow users to train and deploy open-source ML models in minutes. “Easy access to Transformers models is something that started with Hugging Face, and they're great at that,” says Kogan. The Fetch and Hugging Face teams worked to identify and train state-of-the-art document AI models, improving entity resolution and semantic search.In this relationship, Hugging Face acted in an advisory capacity, transferring knowledge to help the Fetch engineers use its resources more effectively. “Fetch had a great team in place,” says Yifeng Yin, machine learning engineer at Hugging Face. “They didn't need us to come in and run the project or build it. They wanted to learn how to use Hugging Face to train the models they were building. We showed them how to use the resources, and they ran with it.” With Yifeng’s guidance, Fetch was able to cut its development time by 30 percent.Because it was building its own AI and ML models to take over from the third-party ‘brain’, it needed to ensure a robust system that produced good results before switching over. Fetch required doing this without interrupting the flow of millions of receipts every day. “Before we rolled anything out, we built a shadow pipeline,” says Sam Corzine, lead machine learning engineer at Fetch. “We took all the things and reprocessed them in our new ML pipeline. We could do audits of everything. It was running full volume, reprocessing all of those 11 million receipts and doing analytics on them for quite a while before anything made it into the main data fields. The black box was still running the show and we were checking our results against it.” The solution uses Amazon SageMaker—which lets businesses build, train, and deploy ML models for any use case with fully managed infrastructure, tools, and workflows. It also uses AWS Inferentia accelerators to deliver high performance at the lowest cost for deep learning (DL) inference applications. Fetch Grows AI Expertise, Cuts Latency by 50%, and Saves Costs Fetch’s commitment to developing in-house ML and AI capabilities has resulted in several benefits, including some cost savings, but more important is the development of a service that better serves the needs of the customers. “With any app you have to give the customer a reason to keep coming back,” says Corzine. “We’ve improved responsiveness for customers with faster processing of uploads, cutting processing latency by 50 percent. If you keep customers waiting too long, they’ll disengage. And the more customers use Fetch, the better understanding we and our partners get about what’s important to them. By building our own models, we get details we never had before.”The company can now train a model in hours instead of the days or weeks it used to take. Development time has also been reduced by about 30 percent. And while it may not be possible to put a number to it, another major benefit has been creating a more stable foundation for Fetch. “Relying on a third-party black box presented considerable business risk to us,” says Corzine. “Because Hugging Face existed and its community existed, we were able to use that tooling and work with that community. At the end of the day, we now control our destiny.”Fetch is continuing to improve the service to customers and gain a better understanding of customer behavior now that it is an AI-first company, rather than a company that uses a third-party AI ‘brain’. “Hugging Face and AWS gave us the infrastructure and the resources to do what we need,” says Kogan. “Hugging Face has democratized transformer models, models that were nearly impossible to train, and made them available to anyone. We couldn’t have done this without them.”This article is a cross-post from an originally published post on February 2024 on AWS's website.
https://huggingface.co/blog/game-jam-first-edition-results
Results of the Open Source AI Game Jam
Thomas Simonini, Dylan Ebert, Omar Sanseviero
July 21, 2023
From July 7th to July 11th, we hosted our first Open Source AI Game Jam, an exciting event that challenged game developers to create innovative games within a tight 48-hour window using AI.The primary objective was to create games that incorporate at least one Open Source AI Tool. Although proprietary AI tools were allowed, we encouraged participants to integrate open-source tools into their game or workflow.The response to our initiative was beyond our expectations, with over 1300 signups and the submission of 88 amazing games.You can try them here 👉 https://itch.io/jam/open-source-ai-game-jam/entriesThe Theme: ExpandingTo inspire creativity, we decided on the theme of "EXPANDING." We left it open to interpretation, allowing developers to explore and experiment with their ideas, leading to a diverse range of games.The games were evaluated by their peers and contributors based on three key criteria: fun, creativity, and adherence to the theme.The top 10 games were then presented to three judges (Dylan Ebert, Thomas Simonini and Omar Sanseviero), who selected the best game.The Winner 🏆🥇After careful deliberation, the judges crowned one outstanding game as the Winner of the Open Source AI Game Jam.It's Snip It by ohmlet 👏👏👏.Code: Ruben GresAI assets: Philippe SaadeMusic / SFX: Matthieu DeloffreIn this AI-generated game, you visit a museum where the paintings come to life. Snip the objects in the paintings to uncover their hidden secrets.You can play it here 👉 https://ohmlet.itch.io/snip-itParticipants Selection: Top 10 🥈🥉🏅Out of the 88 fantastic submissions, these impressive games emerged as the Top 11 finalists.#1: Snip ItIn addition to be the winner of the Game Jam, Snip it has been selected as the top participant selection.🤖 Open Source Model Used: Stable Diffusion to generate the assets.🎮👉 https://ohmlet.itch.io/snip-it#2: Yabbit AttackIn Yabbit Attack, your goal is to beat the constantly adapting neural network behind the Yabbits.🤖 Used genetic algorithms in the context of natural selection and evolution.🤖 Backgrounds visuals were generated using Stable Diffusion🎮👉 https://visionistx.itch.io/yabbit-attack#3: Fish Dang Bot Rolling LandIn this game, you take control of a fish-shaped robot named Fein, who is abandoned in a garbage dump with mechanical legs. Unexpectedly, it develops self-awareness, and upon awakening, it sees a dung beetle pushing a dung ball. Naturally, Fein assumes himself to be a dung beetle and harbours a dream of pushing the largest dung ball. With this dream in mind, it decides to embark on its own adventure.🤖 Used Text To Speech model to generate the voices.🎮👉 https://zeenaz.itch.io/fish-dang-rolling-laud#4: Everchanging QuestIn this game, you are the village's last hope. Arm yourself before embarking on your adventure, and don't hesitate to ask the locals for guidance. The world beyond the portal will never be the same, so be prepared. Defeat your enemies to collect points and find your way to the end.🤖 Used GPT-4 to place the tiles and objects (proprietary) but also Starcoder to code (open source).🎮👉 https://jofthomas.itch.io/everchanging-quest#5: Word ConquestIn this game, you need to write as many unrelated words as you can to conquer the map. The more unrelated, the farther away and the more score you get.🤖 Used embeddings from all-MiniLM-L6-v2 model and GloVe to generate the map.🎮👉 https://danielquelali.itch.io/wordconquest#6: Expanding UniverseIn this sandbox gravity game, you create an expanding universe and try to complete the challenges.🤖 Used Dream Textures Blender (Stable Diffusion) add-on to create textures for all of the planets and stars and an LLM model to generate descriptions of the stars and planets.🎮👉 https://carsonkatri.itch.io/expanding-universe#7: Hexagon Tactics: The Expanding ArenaIn this game, you are dropped into an arena battle. Defeat your opponents, then upgrade your deck and the arena expands.🤖 Stable Diffusion 1.5 to generate your own character (executable version of the game).🎮👉 https://dgeisert.itch.io/hextactics#8: Galactic DominationIn this game, you embark on an interstellar journey as a spaceship captain, pitted against formidable spaceships in a battle for dominance. Your goal is to be the first to construct a powerful space station that will expand your influence and secure your supremacy in the vast expanse of the cosmos. As you navigate the treacherous battlefield, you must gather essential resources to fuel the growth of your space station. It's a construction race!🤖 Unity ML-Agents (bot-AI works with reinforcement learning)🤖 Charmed - Texture Generator🤖 Soundful - Music generator🤖 Elevenlabs - Voice generator🤖 Scenario - Image generator🎮👉 https://blastergames.itch.io/galactic-domination#9: Apocalypse ExpansionIn this game, you'll step into the decaying shoes of a zombie, driven by an insatiable hunger for human flesh. Your objective? To build the largest horde of zombies ever seen, while evading the relentless pursuit of the determined police force.🤖 Used Stable Diffusion to generate the images🤖 Used MusicGen (melody 1.5B) for the music🎮👉 https://mad25.itch.io/apocalypse-expansion#10: Galactic Bride: Bullet BalletIn this game, you dive into an exhilarating bullet-hell journey to become the Star Prince's bride and fulfill your wishes.🎮👉 https://n30hrtgdv.itch.io/galactic-bride-bullet-ballet#10: SingularityThis demo is a conceptual demonstration of what could soon be the generation of experiences/games in the near future.🤖 Used Stable Diffusion🎮👉 https://ilumine-ai.itch.io/dreamlike-hugging-face-open-source-ai-game-jamIn addition to this top 10, don't hesitate to check the other amazing games (Ghost In Smoke, Outopolis, Dungeons and Decoders...). You can find the whole list here 👉 https://itch.io/jam/open-source-ai-game-jam/entriesThe first-ever Open Source AI Game Jam proved to be an astounding success, exceeding our expectations in terms of community engagement and the quality of games produced. The overwhelming response has reinforced our belief in the potential of open-source AI tools to revolutionize the gaming industry.We are eager to continue this initiative and plan to host more sessions in the future, providing game developers with an opportunity to showcase their skills and explore the power of AI in game development.For those interested in AI for games, we have compiled a list of valuable resources, including AI tools for game development and tutorials on integrating AI into game engines like Unity:Compilation of AI tools for Game DevHow to install the Unity Hugging Face API: https://huggingface.co/blog/unity-apiAI Speech Recognition in Unity: https://huggingface.co/blog/unity-asrMaking ML-powered web games with Transformers.js: https://huggingface.co/blog/ml-web-gamesBuilding a smart Robot AI using Hugging Face 🤗 and Unity: https://thomassimonini.substack.com/p/building-a-smart-robot-ai-using-huggingTo stay connected and stay updated on future events, feel free to drop by our Discord server, where you can find channels dedicated to exchanging ideas about AI for games.Join our Discord Server 👉 https://hf.co/join/discordThank you to all the participants, contributors, and supporters who made this event a memorable success!
https://huggingface.co/blog/transformers-design-philosophy
Don't Repeat Yourself*
Patrick von Platen
April 5, 2022
🤗 Transformers Design Philosophy"Don't repeat yourself", or DRY, is a well-known principle of software development. The principle originates from "The pragmatic programmer", one of the most read books on code design.The principle's simple message makes obvious sense: Don't rewrite a logic that already exists somewhere else. This ensures the code remains in sync, making it easier to maintain and more robust. Any change to this logical pattern will uniformly affect all of its dependencies.At first glance, the design of Hugging Face's Transformers library couldn't be more contrary to the DRY principle. Code for the attention mechanism is more or less copied over 50 times into different model files. Sometimes code of the whole BERT model is copied into other model files. We often force new model contributions identical to existing models - besides a small logical tweak - to copy all of the existing code. Why do we do this? Are we just too lazy or overwhelmed to centralize all logical pieces into one place?No, we are not lazy - it's a very conscious decision not to apply the DRY design principle to the Transformers library. Instead, we decided to adopt a different design principle which we like to call the single model file policy. The single model file policy states that all code necessary for the forward pass of a model is in one and only one file - called the model file. If a reader wants to understand how BERT works for inference, she should only have to look into BERT's modeling_bert.py file. We usually reject any attempt to abstract identical sub-components of different models into a new centralized place. We don't want to have a attention_layer.py that includes all possible attention mechanisms. Again why do we do this?In short the reasons are:1. Transformers is built by and for the open-source community.2. Our product are models and our customers are users reading or tweaking model code.3. The field of machine learning evolves extremely fast.4. Machine Learning models are static.1. Built by and for the open-source communityTransformers is built to actively incentivize external contributions. A contribution is often either a bug fix or a new model contribution. If a bug is found in one of the model files, we want to make it as easy as possible for the finder to fix it. There is little that is more demotivating than fixing a bug only to see that it caused 100 failures of other models. Because model code is independent from all other models, it's fairly easy for someone that only understands the one model she is working with to fix it. Similarly, it's easier to add new modeling code and review the corresponding PR if only a single new model file is added. The contributor does not have to figure out how to add new functionality to a centralized attention mechanism without breaking existing models. The reviewer can easily verify that none of the existing models are broken.2. Modeling code is our productWe assume that a significant amount of users of the Transformers library not only read the documentation, but also look into the actual modeling code and potentially modify it. This hypothesis is backed by the Transformers library being forked over 10,000 times and the Transformers paper being cited over a thousand times.Therefore it is of utmost importance that someone reading Transformers modeling code for the first time can easily understand and potentially adapt it. Providing all the necessary logical components in order in a single modeling file helps a lot to achieve improved readability and adaptability. Additionally, we care a great deal about sensible variable/method naming and prefer expressive/readable code over character-efficient code. 3. Machine Learning is evolving at a neck-breaking speedResearch in the field of machine learning, and especially neural networks, evolves extremely fast. A model that was state-of-the-art a year ago might be outdated today. We don't know which attention mechanism, position embedding, or architecture will be the best in a year. Therefore, we cannot define standard logical patterns that apply to all models. As an example, two years ago, one might have defined BERT's self attention layer as the standard attention layer used by all Transformers models. Logically, a "standard" attention function could have been moved into a central attention.py file. But then came attention layers that added relative positional embeddings in each attention layer (T5), multiple different forms of chunked attention (Reformer, Longformer, BigBird), and separate attention mechanism for position and word embeddings (DeBERTa), etc... Every time we would have to have asked ourselves whether the "standard" attention function should be adapted or whether it would have been better to add a new attention function to attention.py. But then how do we name it? attention_with_positional_embd, reformer_attention, deberta_attention? It's dangerous to give logical components of machine learning models general names because the perception of what this component stands for might change or become outdated very quickly. E.g., does chunked attention corresponds to GPTNeo's, Reformer's, or BigBird's chunked attention? Is the attention layer a self-attention layer, a cross-attentional layer, or does it include both? However, if we name attention layers by their model's name, we should directly put the attention function in the corresponding modeling file.4. Machine Learning models are staticThe Transformers library is a unified and polished collection of machine learning models that different research teams have created. Every machine learning model is usually accompanied by a paper and its official GitHub repository. Once a machine learning model is published, it is rarely adapted or changed afterward.Instead, research teams tend to publish a new model built upon previous models but rarely make significant changes to already published code. This is an important realization when deciding on the design principles of the Transformers library.It means that once a model architecture has been added to Transformers, the fundamental components of the model don't change anymore. Bugs are often found and fixed, methods and variables might be renamed, and the output or input format of the model might be slightly changed, but the model's core components don't change anymore. Consequently, the need to apply global changes to all models in Transformers is significantly reduced, making it less important that every logical pattern only exists once since it's rarely changed.A second realization is that models do not depend on each other in a bidirectional way. More recent published models might depend on existing models, but it's quite obvious that an existing model cannot logically depend on its successor. E.g. T5 is partly built upon BERT and therefore T5's modeling code might logically depend on BERT's modeling code, but BERT cannot logically depend in any way on T5. Thus, it would not be logically sound to refactor BERT's attention function to also work with T5's attention function - someone reading through BERT's attention layer should not have to know anything about T5. Again, this advocates against centralizing components such as the attention layer into modules that all models can access.On the other hand, the modeling code of successor models can very well logically depend on its predecessor model. E.g., DeBERTa-v2 modeling code does logically depend to some extent on DeBERTa's modeling code. Maintainability is significantly improved by ensuring the modeling code of DeBERTa-v2 stays in sync with DeBERTa's. Fixing a bug in DeBERTa should ideally also fix the same bug in DeBERTa-v2. How can we maintain the single model file policy while ensuring that successor models stay in sync with their predecessor model? Now, we explain why we put the asterisk * {}^{\textbf{*}} * after "Repeat Yourself". We don't blindly copy-paste all existing modeling code even if it looks this way. One of Transformers' core maintainers, Sylvain Gugger, found a great mechanism that respects both the single file policy and keeps maintainability cost in bounds. This mechanism, loosely called "the copying mechanism", allows us to mark logical components, such as an attention layer function, with a # Copied from <predecessor_model>.<function> statement, which enforces the marked code to be identical to the <function> of the <predecessor_model>. E.g., this line of over DeBERTa-v2's class enforces the whole class to be identical to DeBERTa's class except for the prefix DeBERTav2.This way, the copying mechanism keeps modeling code very easy to understand while significantly reducing maintenance. If some code is changed in a function of a predecessor model that is referred to by a function of its successor model, there are tools in place that automatically correct the successor model's function.DrawbacksClearly, there are also drawbacks to the single file policy two of which we quickly want to mention here.A major goal of Transformers is to provide a unified API for both inference and training for all models so that a user can quickly switch between different models in her setup. However, ensuring a unified API across models is much more difficult if modeling files are not allowed to use abstracted logical patterns. We solvethis problem by running a lot of tests (ca. 20,000 tests are run daily at the time of writing this blog post) to ensure that models follow a consistent API. In this case, the single file policy requires us to be very rigorous when reviewing model and test additions.Second, there is a lot of research on just a single component of a Machine Learning model. E.g., researchteams investigate new forms of an attention mechanism that would apply to all existing pre-trained models as has been done in the Rethinking Attention with Performers. How should we incorporate such research into the Transformers library? It is indeed problematic. Should we change all existing models? This would go against points 3. and 4. as written above. Should we add 100+ new modeling files each prefixed with Performer...? This seems absurd. In such a case there is sadly no good solutionand we opt for not integrating the paper into Transformers in this case. If the paper would have gotten much more traction and included strong pre-trained checkpoints, we would have probably added new modeling files of the most important models such as modeling_performer_bert.pyavailable.ConclusionAll in all, at 🤗 Hugging Face we are convinced that the single file policy is the right coding philosophy for Transformers.What do you think? If you read until here, we would be more than interested in hearing your opinion!If you would like to leave a comment, please visit the corresponding forum post here.
https://huggingface.co/blog/streamlit-spaces
Hosting your Models and Datasets on Hugging Face Spaces using Streamlit
Merve Noyan
October 5, 2021
Showcase your Datasets and Models using Streamlit on Hugging Face SpacesStreamlit allows you to visualize datasets and build demos of Machine Learning models in a neat way. In this blog post we will walk you through hosting models and datasets and serving your Streamlit applications in Hugging Face Spaces. Building demos for your modelsYou can load any Hugging Face model and build cool UIs using Streamlit. In this particular example we will recreate "Write with Transformer" together. It's an application that lets you write anything using transformers like GPT-2 and XLNet. We will not dive deep into how the inference works. You only need to know that you need to specify some hyperparameter values for this particular application. Streamlit provides many components for you to easily implement custom applications. We will use some of them to receive necessary hyperparameters inside the inference code.The .text_area component creates a nice area to input sentences to be completed.The Streamlit .sidebar method enables you to accept variables in a sidebar. The slider is used to take continuous values. Don't forget to give slider a step, otherwise it will treat the values as integers. You can let the end-user input integer vaues with number_input .import streamlit as st# adding the text that will show in the text box as defaultdefault_value = "See how a modern neural network auto-completes your text 🤗 This site, built by the Hugging Face team, lets you write a whole document directly from your browser, and you can trigger the Transformer anywhere using the Tab key. Its like having a smart machine that completes your thoughts 😀 Get started by typing a custom snippet, check out the repository, or try one of the examples. Have fun!"sent = st.text_area("Text", default_value, height = 275)max_length = st.sidebar.slider("Max Length", min_value = 10, max_value=30)temperature = st.sidebar.slider("Temperature", value = 1.0, min_value = 0.0, max_value=1.0, step=0.05)top_k = st.sidebar.slider("Top-k", min_value = 0, max_value=5, value = 0)top_p = st.sidebar.slider("Top-p", min_value = 0.0, max_value=1.0, step = 0.05, value = 0.9)num_return_sequences = st.sidebar.number_input('Number of Return Sequences', min_value=1, max_value=5, value=1, step=1)The inference code returns the generated output, you can print the output using simple st.write.st.write(generated_sequences[-1])Here's what our replicated version looks like.You can checkout the full code here.Showcase your Datasets and Data VisualizationsStreamlit provides many components to help you visualize datasets. It works seamlessly with 🤗 Datasets, pandas, and visualization libraries such as matplotlib, seaborn and bokeh. Let's start by loading a dataset. A new feature in Datasets, called streaming, allows you to work immediately with very large datasets, eliminating the need to download all of the examples and load them into memory.from datasets import load_datasetimport streamlit as stdataset = load_dataset("merve/poetry", streaming=True)df = pd.DataFrame.from_dict(dataset["train"])If you have structured data like mine, you can simply use st.dataframe(df) to show your dataset. There are many Streamlit components to plot data interactively. One such component is st.barchart() , which I used to visualize the most used words in the poem contents. st.write("Most appearing words including stopwords")st.bar_chart(words[0:50])If you'd like to use libraries like matplotlib, seaborn or bokeh, all you have to do is to put st.pyplot() at the end of your plotting script.st.write("Number of poems for each author")sns.catplot(x="author", data=df, kind="count", aspect = 4)plt.xticks(rotation=90)st.pyplot()You can see the interactive bar chart, dataframe component and hosted matplotlib and seaborn visualizations below. You can check out the code here.Hosting your Projects in Hugging Face SpacesYou can simply drag and drop your files as shown below. Note that you need to include your additional dependencies in the requirements.txt. Also note that the version of Streamlit you have on your local is the same. For seamless usage, refer to Spaces API reference. There are so many components and packages you can use to demonstrate your models, datasets, and visualizations. You can get started here.
https://huggingface.co/blog/asr-chunking
Making automatic speech recognition work on large files with Wav2Vec2 in 🤗 Transformers
Nicolas Patry
February 1, 2022
Wav2Vec2 is a popular pre-trained model for speech recognition.Released in September 2020by Meta AI Research, the novel architecture catalyzed progress inself-supervised pretraining for speech recognition, e.g. G. Ng etal., 2021, Chen et al,2021, Hsu et al.,2021 and Babu et al.,2021. On the Hugging Face Hub,Wav2Vec2's most popular pre-trained checkpoint currently amounts toover 250,000 monthlydownloads.Wav2Vec2 is at its core a transformers models and one caveatof transformers is that it usually has a finite amount of sequencelength it can handle. Either because it uses position encodings (notthe case here) or simply because the cost of attention in transformersis actually O(n²) in sequence_length, meaning that using very largesequence_length explodes in complexity/memory. So you cannot run with finite hardware(even a very large GPU like A100), simply run Wav2Vec2 on an hour longfile. Your program will crash. Let's try it !pip install transformersfrom transformers import pipeline# This will work on any of the thousands of models at# https://huggingface.co/models?pipeline_tag=automatic-speech-recognitionpipe = pipeline(model="facebook/wav2vec2-base-960h")# The Public Domain LibriVox file used for the test#!wget https://ia902600.us.archive.org/8/items/thecantervilleghostversion_2_1501_librivox/thecantervilleghostversion2_01_wilde_128kb.mp3 -o very_long_file.mp3pipe("very_long_file.mp3")# Crash out of memory !pipe("very_long_file.mp3", chunk_length_s=10)# This works and prints a very long string ! # This whole blogpost will explain how to make things workSimple ChunkingThe simplest way to achieve inference on very long files would be to simply chunkthe initial audio into shorter samples, let's say 10 seconds each, run inference on those, and end upwith a final reconstruction. This is efficient computationally but usually leadsto subpar results, the reason being that in order to do good inference, the modelneeds some context, so around the chunking border, inference tends to be of poorquality.Look at the following diagram:There are ways to try and work around the problem in a general fashion, butthey are never entirely robust. You can try to chunk only when you encountersilence but you may have a non silent audio for a long time (a song, or noisycafé audio). You can also try to cut only when there's no voice but it requiresanother model and this is not an entirely solved problem. You could also havea continous voice for a very long time.As it turns out, CTC structure, which is used by Wav2Vec2, can be exploitedin order to achieve very robust speech recognition even on very long fileswithout falling into those pitfalls.Chunking with strideWav2Vec2 uses the CTC algorithm, which means that every frame of audio is mappedto a single letter prediction (logit).That's the main feature we're going to use in order to add a stride.This link explains it in the image context, but it's the same concept for audio.Because of this property, we can:Start doing inference on overlapping chunksso that the model actually has proper context in the center.Drop the inferenced logits on the side. Chain the logits without their dropped sides to recover something extremely close to what the model would havepredicted on the full length audio.This is not technically 100% the same thing as running the model on the wholefile so it is not enabled by default, but as you saw in the earlier example youneed only to add chunk_length_s to your pipeline for it to work.In practice, we observed that most of the bad inference is kept withinthe strides, which get dropped before inference, leading to a properinference of the full text.Let's note that you can choose every argument of this technique:from transformers import pipelinepipe = pipeline(model="facebook/wav2vec2-base-960h")# stride_length_s is a tuple of the left and right stride length.# With only 1 number, both sides get the same stride, by default# the stride_length on one side is 1/6th of the chunk_length_soutput = pipe("very_long_file.mp3", chunk_length_s=10, stride_length_s=(4, 2))Chunking with stride on LM augmented modelsIn transformers, we alsoadded support for adding LM to Wav2Vec2 in order to boost the WER performanceof the models without even finetuning. See this excellent blogpost explaininghow it works.It turns out, that the LM works directly on the logits themselves, so wecan actually apply the exact same technique as before without any modification !So chunking large files on these LM boosted models still works out of the box.Live inferenceA very nice perk of using a CTC model like Wav2vec2, is that it is a singlepass model, so it is very fast. Especially on GPU. We can exploit that in orderto do live inference.The principle is exactly the same as regular striding, but this time we canfeed the pipeline data as it is coming in and simply use striding onfull chunks of length 10s for instance with 1s striding to get proper context.That requires running much more inference steps than simple file chunking, but it can make the live experience much better because the model can print things as you are speaking, without having to wait for X seconds before seeing something displayed.
https://huggingface.co/blog/sd_distillation
Open-sourcing Knowledge Distillation Code and Weights of SD-Small and SD-Tiny
Yatharth Gupta
August 1, 2023
In recent times, the AI community has witnessed a remarkable surge in the development of larger and more performant language models, such as Falcon 40B, LLaMa-2 70B, Falcon 40B, MPT 30B, and in the imaging domain with models like SD2.1 and SDXL. These advancements have undoubtedly pushed the boundaries of what AI can achieve, enabling highly versatile and state-of-the-art image generation and language understanding capabilities. However, as we marvel at the power and complexity of these models, it is essential to recognize a growing need to make AI models smaller, efficient, and more accessible, particularly by open-sourcing them.At Segmind, we have been working on how to make generative AI models faster and cheaper. Last year, we have open-sourced our accelerated SD-WebUI library called voltaML, which is a AITemplate/TensorRT based inference acceleration library that has delivered between 4-6X increase in the inference speed. To continue towards the goal of making generative models faster, smaller and cheaper, we are open-sourcing the weights and training code of our compressed SD models; SD-Small and SD-Tiny. The pretrained checkpoints are available on Huggingface 🤗Knowledge DistillationOur new compressed models have been trained on Knowledge-Distillation (KD) techniques and the work has been largely based on this paper. The authors describe a Block-removal Knowledge-Distillation method where some of the UNet layers are removed and the student model weights are trained. Using the KD methods described in the paper, we were able to train two compressed models using the 🧨 diffusers library; Small and Tiny, that have 35% and 55% fewer parameters, respectively than the base model while achieving comparable image fidelity as the base model. We have open-sourced our distillation code in this repo and pretrained checkpoints on Huggingface 🤗.Knowledge-Distillation training a neural network is similar to a teacher guiding a student step-by-step. A large teacher model is pre-trained on a large amount of data and then a smaller model is trained on a smaller dataset, to imitate the outputs of the larger model along with classical training on the dataset.In this particular type of knowledge distillation, the student model is trained to do the normal diffusion task of recovering an image from pure noise, but at the same time, the model is made to match the output of the larger teacher model. The matching of outputs happens at every block of the U-nets, hence the model quality is mostly preserved. So, using the previous analogy, we can say that during this kind of distillation, the student will not only try to learn from the Questions and Answers but also from the Teacher’s answers, as well as the step by step method of getting to the answer. We have 3 components in the loss function to achieve this, firstly the traditional loss between latents of the target image and latents of the generated image. Secondly, the loss between latents of the image generated by the teacher and latents of image generated by the student. And lastly, and the most important component, is the feature level loss, which is the loss between the outputs of each of the blocks of the teacher and the student.Combining all of this makes up the Knowledge-Distillation training. Below is an architecture of the Block Removed UNet used in the KD as described in the paper.Image taken from the paper “On Architectural Compression of Text-to-Image Diffusion Models” by Shinkook. et. alWe have taken Realistic-Vision 4.0 as our base teacher model and have trained on the LAION Art Aesthetic dataset with image scores above 7.5, because of their high quality image descriptions. Unlike the paper, we have chosen to train the two models on 1M images for 100K steps for the Small and 125K steps for the Tiny mode respectively. The code for the distillation training can be found here.Model UsageThe Model can be used using the DiffusionPipeline from 🧨 diffusersfrom diffusers import DiffusionPipelineimport torchpipeline = DiffusionPipeline.from_pretrained("segmind/small-sd", torch_dtype=torch.float16)prompt = "Portrait of a pretty girl"negative_prompt = "(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon, drawing, anime:1.4), text, close up, cropped, out of frame, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck"image = pipeline(prompt, negative_prompt = negative_prompt).images[0]image.save("my_image.png")Speed in terms of inference latencyWe have observed that distilled models are up to 100% faster than the original base models. The Benchmarking code can be found here.Potential LimitationsThe distilled models are in early phase and the outputs may not be at a production quality yet.These models may not be the best general models. They are best used as fine-tuned or LoRA trained on specific concepts/styles.Distilled models are not very good at composibility or multiconcepts yet.Fine-tuning SD-tiny model on portrait datasetWe have fine-tuned our sd-tiny model on portrait images generated with the Realistic Vision v4.0 model. Below are the fine tuning parameters used.Steps: 131000Learning rate: 1e-4Batch size: 32Gradient accumulation steps: 4Image resolution: 768Dataset size - 7k imagesMixed-precision: fp16We were able to produce image quality close to the images produced by the original model, with almost 40% fewer parameters and the sample results below speak for themselves:The code for fine-tuning the base models can be found here.LoRA TrainingOne of the advantages of LoRA training on a distilled model is faster training. Below are some of the images of the first LoRA we trained on the distilled model on some abstract concepts. The code for the LoRA training can be found here.ConclusionWe invite the open-source community to help us improve and achieve wider adoption of these distilled SD models. Users can join our Discord server, where we will be announcing the latest updates to these models, releasing more checkpoints and some exciting new LoRAs. And if you like our work, please give us a star on our Github.
https://huggingface.co/blog/gcp-partnership
Hugging Face and Google partner for open AI collaboration
Jeff Boudier, Philipp Schmid
January 25, 2024
At Hugging Face, we want to enable all companies to build their own AI, leveraging open models and open source technologies. Our goal is to build an open platform, making it easy for data scientists, machine learning engineers and developers to access the latest models from the community, and use them within the platform of their choice.Today, we are thrilled to announce our strategic partnership with Google Cloud to democratize good machine learning. We will collaborate with Google across open science, open source, cloud, and hardware to enable companies to build their own AI with the latest open models from Hugging Face and the latest cloud and hardware features from Google Cloud.A collaboration for open scienceFrom the original Transformer to the Vision Transformer, Google has published some of the most important contributions to open AI research and prompted the AI community to change the World one model at a time, with now over 1 million models, datasets and AI applications based on transformer models hosted on Hugging Face.Our strategic partnership will help amplify efforts led by Google and Hugging Face to make the latest AI research more accessible to the community. A collaboration for open sourceFrom Tensorflow to JAX, Google has contributed some of the most important open source tools, enabling researchers and data scientists to build their own AI models and create a virtuous cycle of model performance improvement through rapid iteration.Our strategic partnership will accelerate our collaboration to make the latest AI innovations easily accessible through Hugging Face open-source libraries, whichever framework you use.A collaboration for Google Cloud customersToday, hundreds of thousands of Hugging Face users are active on Google Cloud every month, downloading models to create Generative AI applications. Our strategic partnership will enable new experiences for Google Cloud customers to easily train and deploy Hugging Face models within Google Kubernetes Engine (GKE) and Vertex AI. Customers will benefit from the unique hardware capabilities available in Google Cloud, like TPU instances, A3 VMs, powered by NVIDIA H100 Tensor Core GPUs, and C3 VMs, powered by Intel Sapphire Rapid CPUs.A collaboration for Hugging Face Hub usersMillions of researchers, data scientists, developers and AI hobbyists rely on the Hugging Face Hub every month to easily create and experience the most recent AI models and applications. The fruits of our collaboration with Google in open science, open source and Google Cloud will be made available to Hugging Face Hub users and enable new experiences throughout 2024. Models will be easily deployed for production on Google Cloud with Inference Endpoints. AI builders will be able to accelerate their applications with TPU on Hugging Face Spaces. Organizations will be able to leverage their Google Cloud account to easily manage the usage and billing of their Enterprise Hub subscription.What’s nextWe can’t wait to make these new experiences available to you. Stay tuned for announcements starting this quarter! For now, we leave you with a word from our CEOs: “Google Cloud and Hugging Face share a vision for making generative AI more accessible and impactful for developers. This partnership ensures that developers on Hugging Face will have access to Google Cloud’s purpose-built, AI platform, Vertex AI, along with our secure infrastructure, which can accelerate the next generation of AI services and applications,” says Thomas Kurian, CEO of Google Cloud.“With this new partnership, we will make it easy for Hugging Face users and Google Cloud customers to leverage the latest open models together with leading optimized AI infrastructure and tools from Google Cloud including Vertex AI and TPUs to meaningfully advance developers ability to build their own AI models,” says Clement Delangue CEO of Hugging Face.
https://huggingface.co/blog/accelerate-deepspeed
Accelerate Large Model Training using DeepSpeed
Sourab Mangrulkar, Sylvain Gugger
June 28, 2022
In this post we will look at how we can leverage the Accelerate library for training large models which enables users to leverage the ZeRO features of DeeSpeed. Motivation 🤗 Tired of Out of Memory (OOM) errors while trying to train large models? We've got you covered. Large models are very performant [1] but difficult to train with the available hardware. To get the most of the available hardware for training large models one can leverage Data Parallelism using ZeRO - Zero Redundancy Optimizer [2]. Below is a short description of Data Parallelism using ZeRO with diagram from this blog post(Source: link) a. Stage 1 : Shards optimizer states across data parallel workers/GPUs b. Stage 2 : Shards optimizer states + gradients across data parallel workers/GPUs c. Stage 3: Shards optimizer states + gradients + model parameters across data parallel workers/GPUs d. Optimizer Offload: Offloads the gradients + optimizer states to CPU/Disk building on top of ZERO Stage 2 e. Param Offload: Offloads the model parameters to CPU/Disk building on top of ZERO Stage 3In this blogpost we will look at how to leverage Data Parallelism using ZeRO using Accelerate. DeepSpeed, FairScale and PyTorch FullyShardedDataParallel (FSDP) have implemented the core ideas of the ZERO paper. These have already been integrated in 🤗 transformers Trainer and 🤗 accelerate accompanied by great blogs Fit More and Train Faster With ZeRO via DeepSpeed and FairScale [4] and Accelerate Large Model Training using PyTorch Fully Sharded Data Parallel [5]. We defer the explanation of what goes behind the scenes to those blogs and mainly focus on leveraging DeepSpeed ZeRO using Accelerate. Accelerate 🚀: Leverage DeepSpeed ZeRO without any code changes Hardware setup: 2X24GB NVIDIA Titan RTX GPUs. 60GB RAM.We will look at the task of finetuning encoder-only model for text-classification. We will use pretrained microsoft/deberta-v2-xlarge-mnli (900M params) for finetuning on MRPC GLUE dataset.The code is available here run_cls_no_trainer.py. It is similar to the official text-classification example here with the addition of logic to measure train and eval time. Let's compare performance between Distributed Data Parallel (DDP) and DeepSpeed ZeRO Stage-2 in a Multi-GPU Setup.To enable DeepSpeed ZeRO Stage-2 without any code changes, please run accelerate config and leverage the Accelerate DeepSpeed Plugin. ZeRO Stage-2 DeepSpeed Plugin Examplecompute_environment: LOCAL_MACHINEdeepspeed_config: gradient_accumulation_steps: 1 gradient_clipping: 1.0 offload_optimizer_device: none offload_param_device: none zero3_init_flag: false zero_stage: 2distributed_type: DEEPSPEEDfsdp_config: {}machine_rank: 0main_process_ip: nullmain_process_port: nullmain_training_function: mainmixed_precision: fp16num_machines: 1num_processes: 2use_cpu: falseNow, run below command for training:accelerate launch run_cls_no_trainer.py \ --model_name_or_path "microsoft/deberta-v2-xlarge-mnli" \ --task_name "mrpc" \ --ignore_mismatched_sizes \ --max_length 128 \ --per_device_train_batch_size 40 \ --learning_rate 2e-5 \ --num_train_epochs 3 \ --output_dir "/tmp/mrpc/deepspeed_stage2/" \ --with_tracking \ --report_to "wandb" \In our Single-Node Multi-GPU setup, the maximum batch size that DDP supports without OOM error is 8. In contrast, DeepSpeed Zero-Stage 2 enables batch size of 40 without running into OOM errors. Therefore, DeepSpeed enables to fit 5X more data per GPU when compared to DDP. Below is the snapshot of the plots from wandb run along with benchmarking table comparing DDP vs DeepSpeed. MethodBatch Size MaxTrain time per epoch (seconds)Eval time per epoch (seconds)F1 scoreAccuracyDDP (Distributed Data Parallel)8103.572.040.9310.904DeepSpeed ZeRO Stage 24028.981.790.9360.912Table 1: Benchmarking DeepSpeed ZeRO Stage-2 on DeBERTa-XL (900M) modelWith this bigger batch size, we observe ~3.5X speed up in total training time without any drop in perforamnce metrics, all this without changing any code. Yay! 🤗. To be able to tweak more options, you will need to use a DeepSpeed config file and minimal code changes. Let's see how to do this. Accelerate 🚀: Leverage a DeepSpeed Config file to tweak more options First, We will look at the task of finetuning a sequence-to-sequence model for training our own Chatbot. Specifically, we will finetune facebook/blenderbot-400M-distill on the smangrul/MuDoConv (Multi-Domain Conversation) dataset. The dataset contains conversations from 10 different data sources covering personas, grounding in specific emotional contexts, goal-oriented (e.g., restaurant reservation) and general wikipedia topics (e.g, Cricket).The code is available here run_seq2seq_no_trainer.py. Current pratice to effectively measure the Engagingness and Humanness of Chatbots is via Human evlauations which are expensive [6]. As such for this example, the metric being tracked is BLEU score (which isn't ideal but is the conventional metric for such tasks). One can adapt the code to train larger T5 models if you have access to GPUs that support bfloat16 precision else you will run into NaN loss values. We will run a quick benchmark on 10000 train samples and 1000 eval samples as we are interested in DeepSpeed vs DDP.We will leverage the DeepSpeed Zero Stage-2 config zero2_config_accelerate.json (given below) For training. for detailed information on the various config features, please refer DeeSpeed documentation.{ "fp16": { "enabled": "true", "loss_scale": 0, "loss_scale_window": 1000, "initial_scale_power": 15, "hysteresis": 2, "min_loss_scale": 1 }, "optimizer": { "type": "AdamW", "params": { "lr": "auto", "weight_decay": "auto", "torch_adam": true, "adam_w_mode": true } }, "scheduler": { "type": "WarmupDecayLR", "params": { "warmup_min_lr": "auto", "warmup_max_lr": "auto", "warmup_num_steps": "auto", "total_num_steps": "auto" } }, "zero_optimization": { "stage": 2, "allgather_partitions": true, "allgather_bucket_size": 2e8, "overlap_comm": true, "reduce_scatter": true, "reduce_bucket_size": 2e8, "contiguous_gradients": true }, "gradient_accumulation_steps": 1, "gradient_clipping": "auto", "steps_per_print": 2000, "train_batch_size": "auto", "train_micro_batch_size_per_gpu": "auto", "wall_clock_breakdown": false}To enable DeepSpeed ZeRO Stage-2 with above config, please run accelerate config and provide the config file path when asked. For more details, refer the 🤗 accelerate official documentation for DeepSpeed Config File.ZeRO Stage-2 DeepSpeed Config File Examplecompute_environment: LOCAL_MACHINEdeepspeed_config: deepspeed_config_file: /path/to/zero2_config_accelerate.json zero3_init_flag: falsedistributed_type: DEEPSPEEDfsdp_config: {}machine_rank: 0main_process_ip: nullmain_process_port: nullmain_training_function: mainmixed_precision: fp16num_machines: 1num_processes: 2use_cpu: falseNow, run below command for training:accelerate launch run_seq2seq_no_trainer.py \ --dataset_name "smangrul/MuDoConv" \ --max_source_length 128 \ --source_prefix "chatbot: " \ --max_target_length 64 \ --val_max_target_length 64 \ --val_min_target_length 20 \ --n_val_batch_generations 5 \ --n_train 10000 \ --n_val 1000 \ --pad_to_max_length \ --num_beams 10 \ --model_name_or_path "facebook/blenderbot-400M-distill" \ --per_device_train_batch_size 200 \ --per_device_eval_batch_size 100 \ --learning_rate 1e-6 \ --weight_decay 0.0 \ --num_train_epochs 1 \ --gradient_accumulation_steps 1 \ --num_warmup_steps 100 \ --output_dir "/tmp/deepspeed_zero_stage2_accelerate_test" \ --seed 25 \ --logging_steps 100 \ --with_tracking \ --report_to "wandb" \ --report_name "blenderbot_400M_finetuning"When using DeepSpeed config, if user has specified optimizer and scheduler in config, the user will have to use accelerate.utils.DummyOptim and accelerate.utils.DummyScheduler. Those are the only minor changes that the user has to do. Below we show an example of the minimal changes required when using DeepSpeed config:- optimizer = torch.optim.Adam(optimizer_grouped_parameters, lr=args.learning_rate)+ optimizer = accelerate.utils.DummyOptim(optimizer_grouped_parameters, lr=args.learning_rate)- lr_scheduler = get_scheduler(- name=args.lr_scheduler_type,- optimizer=optimizer,- num_warmup_steps=args.num_warmup_steps,- num_training_steps=args.max_train_steps,- )+ lr_scheduler = accelerate.utils.DummyScheduler(+ optimizer, total_num_steps=args.max_train_steps, warmup_num_steps=args.num_warmup_steps+ )MethodBatch Size MaxEval Size MaxTrain time per epoch (seconds)Eval time per epoch (seconds)DDP (Distributed Data Parallel)1005027.3648.41DeepSpeed ZeRO Stage 220010019.0639.27Table 2: Benchmarking DeepSpeed ZeRO Stage-2 on BlenderBot (400M) modelIn our Single-Node Multi-GPU setup, the maximum batch size that DDP supports without OOM error is 100. In contrast, DeepSpeed Zero-Stage 2 enables batch size of 200 without running into OOM errors. Therefore, DeepSpeed enables to fit 2X more data per GPU when compared to DDP. We observe ~1.44X speedup in training and ~1.23X speedup in evaluation as we are able to fit more data on the same available hardware. As this model is of medium size, the speedup isn't that exciting but this will improve with bigger models. You can chat with the Chatbot trained using the entire data at 🤗 Space smangrul/Chat-E. You can give bot a persona, ground conversation to a particular emotion, use to in goal-oriented tasks or in a free flow manner. Below is a fun conversation with the chatbot 💬. You can find snapshots of more conversations using different contexts here. CPU/Disk Offloading to enable training humongous models that won’t fit the GPU memory On a single 24GB NVIDIA Titan RTX GPU, one cannot train GPT-XL Model (1.5B parameters) even with a batch size of 1. We will look at how we can use DeepSpeed ZeRO Stage-3 with CPU offloading of optimizer states, gradients and parameters to train GPT-XL Model. We will leverage the DeepSpeed Zero Stage-3 CPU offload config zero3_offload_config_accelerate.json (given below) for training. The rest of the process of using the config with 🤗 accelerate is similar to the above experiment.{ "fp16": { "enabled": true, "loss_scale": 0, "loss_scale_window": 1000, "initial_scale_power": 16, "hysteresis": 2, "min_loss_scale": 1 }, "optimizer": { "type": "AdamW", "params": { "lr": "auto", "weight_decay": "auto" } }, "scheduler": { "type": "WarmupDecayLR", "params": { "warmup_min_lr": "auto", "warmup_max_lr": "auto", "warmup_num_steps": "auto", "total_num_steps": "auto" } }, "zero_optimization": { "stage": 3, "offload_optimizer": { "device": "cpu", "pin_memory": true }, "offload_param": { "device": "cpu", "pin_memory": true }, "overlap_comm": true, "contiguous_gradients": true, "reduce_bucket_size": "auto", "stage3_prefetch_bucket_size": "auto", "stage3_param_persistence_threshold": "auto", "sub_group_size": 1e9, "stage3_max_live_parameters": 1e9, "stage3_max_reuse_distance": 1e9, "stage3_gather_16bit_weights_on_model_save": true }, "gradient_accumulation_steps": 1, "gradient_clipping": "auto", "steps_per_print": 2000, "train_batch_size": "auto", "train_micro_batch_size_per_gpu": "auto", "wall_clock_breakdown": false}ZeRO Stage-3 CPU Offload DeepSpeed Config File Examplecompute_environment: LOCAL_MACHINEdeepspeed_config: deepspeed_config_file: /path/to/zero3_offload_config_accelerate.json zero3_init_flag: truedistributed_type: DEEPSPEEDfsdp_config: {}machine_rank: 0main_process_ip: nullmain_process_port: nullmain_training_function: mainmixed_precision: fp16num_machines: 1num_processes: 2use_cpu: falseNow, run below command for training:accelerate launch run_clm_no_trainer.py \--config_name "gpt2-xl" \--tokenizer_name "gpt2-xl" \--dataset_name "wikitext" \--dataset_config_name "wikitext-2-raw-v1" \--block_size 128 \--output_dir "/tmp/clm_deepspeed_stage3_offload__accelerate" \--learning_rate 5e-4 \--per_device_train_batch_size 16 \--per_device_eval_batch_size 1 \--num_train_epochs 1 \--with_tracking \--report_to "wandb"\MethodBatch Size MaxTrain time per epoch (seconds)NotesDDP (Distributed Data Parallel)--OOM ErrorDeepSpeed ZeRO Stage 3166608.35Table 3: Benchmarking DeepSpeed ZeRO Stage-3 CPU Offload on GPT-XL (1.5B) modelDDP will result in OOM error even with batch size 1. On the other hand, with DeepSpeed ZeRO Stage-3 CPU offload, we can train with a batch size of 16.Finally, please, remember that, 🤗 Accelerate only integrates DeepSpeed, therefore if youhave any problems or questions with regards to DeepSpeed usage, please, file an issue with DeepSpeed GitHub. References [1] Train Large, Then Compress: Rethinking Model Size for Efficient Training and Inference of Transformers[2] ZeRO: Memory Optimizations Toward Training Trillion Parameter Models[3] DeepSpeed: Extreme-scale model training for everyone - Microsoft Research[4] Fit More and Train Faster With ZeRO via DeepSpeed and FairScale[5] Accelerate Large Model Training using PyTorch Fully Sharded Data Parallel[6] Recipes for building an open-domain chatbot
https://huggingface.co/blog/ryght-case-study
Ryght’s Journey to Empower Healthcare and Life Sciences with Expert Support from Hugging Face
Andrew Reed, Johnny Crupi
April 16, 2024
This is a guest blog post by the Ryght team. Who is Ryght? Ryght is building an enterprise-grade generative AI platform tailored for the healthcare and life sciences sectors. Today is their official launch of Ryght Preview, now publicly available for all.Life science companies are amassing a wealth of data from diverse sources (lab data, EMR, genomics, claims, pharmacy, clinical, etc.), but analysis of that data is archaic, requiring large teams for everything from simple queries to developing useful ML models. There is huge demand for actionable knowledge to drive drug development, clinical trials, and commercial activity, and the rise of precision medicine is only accelerating this demand.Ryght’s goal is to empower life science professionals to get the insights they need swiftly and securely. To do so, they’re building a SaaS platform that offers industry-specific AI copilots and custom built solutions for professionals and organizations to accelerate their research, analysis, and documentation across a variety of complex data sources.Recognizing how fast paced and ever changing the AI landscape is, Ryght sought out Hugging Face as a technical advisory partner early in their journey via the Expert Support Program. Overcoming challenges, together Our partnership with Hugging Face's expert support has played a crucial role in expediting the development of our generative AI platform. The rapidly evolving landscape of AI has the potential to revolutionize our industry, and Hugging Face’s highly performant and enterprise-ready Text Generation Inference (TGI) and Text Embeddings Inference (TEI) services are game changers in their own right. - Johnny Crupi, CTO at RyghtRyght faced several challenges as they set out to build their generative AI platform. 1. The need to quickly upskill a team and stay informed in a highly dynamic environment With AI and ML technologies advancing so quickly, ensuring that the team remains abreast of the latest techniques, tools, and best practices is critical. This continuous learning curve is steep and requires a concerted effort to stay informed.Having access to Hugging Face’s team of experts who operate at the center of the AI ecosystem helps Ryght keep up with the latest developments and models that are relevant to their domain. This is achieved through open, asynchronous channels of communication, regular advisory meetings, and dedicated technical workshops. 2. Identifying the most [cost] effective ML approaches amidst the noisy sea of options The AI field is bustling with innovation, leading to an abundance of tools, libraries, models, and methodologies. For a startup like Ryght, it's imperative to cut through this noise and identify which ML strategies are most applicable to their unique use cases in the life sciences sector. This involves not just understanding the current state of the art, but also looking ahead to which technologies will remain relevant and scalable for the future.Hugging Face serves as a partner to Ryght’s technical team – assisting in solution design, proof-of-concept development, and production workload optimization. This includes tailored recommendations on libraries, frameworks, and models best fit for Ryght’s specific needs, along with demonstrable examples of how to use them. This guidance ultimately streamlines the decision-making process and reduces the time to development. 3. Requirement to develop performant solutions that emphasize security, privacy, and flexibility Given the focus on enterprise-level solutions, Ryght prioritizes security, privacy, and governance. This necessitates a flexible architecture capable of interfacing with various large language models (LLMs) in real-time, a crucial feature for their life science-specific content generation and query handling.Understanding the rapid innovation within the open-source community, especially regarding medical LLMs, they embraced an architectural approach that supports "pluggable" LLMs. This design choice allows them to seamlessly evaluate and integrate new or specialized medical LLMs as they emerge.In Ryght’s platform, each LLM is registered and linked to one or more, customer-specific inference endpoints. This setup not only secures the connections, but also provides the ability to switch between different LLMs, offering unparalleled flexibility – a design choice that is made possible by the adoption of Hugging Face’s Text Generation Inference (TGI) and Inference Endpoints.In addition to TGI, Ryght has also integrated Text Embeddings Inference (TEI) into their ML platform. Serving open-source embedding models with TEI marks a significant improvement over relying solely on proprietary embeddings – enabling Ryght to benefit from faster inference speeds, the elimination of rate limit worries, and the flexibility to serve their own fine-tuned models, tailored to the unique requirements of the life sciences domain.Catering to multiple customers simultaneously, their system is designed to handle high volumes of concurrent requests while maintaining low latency. Their embedding and inference services go beyond simple model invocation and encompass a suite of services adept at batching, queuing, and distributing model processing across GPUs. This infrastructure is critical to avoiding performance bottlenecks and ensuring users do not experience delays, thereby maintaining an optimal system response time. Conclusion Ryght's strategic partnership with and integration of Hugging Face's ML services underscores their commitment to delivering cutting-edge solutions in healthcare and life sciences. By embracing a flexible, secure, and scalable architecture, they ensure that their platform remains at the forefront of innovation, offering their clients unparalleled service and expertise in navigating the complexities of modern medical domains. Sign up for Ryght Preview, now publicly available to life sciences knowledge workers as a free, secure platform with frictionless onboarding. Ryght’s copilot library consists of a diverse collection of tools to accelerate information retrieval, synthesis and structuring of complex unstructured data, and document builders, taking what might have taken weeks to complete down to days or hours. To inquire about custom building and collaborations, contact their team of AI experts to discuss Ryght for Enterprise.If you’re interested to know more about Hugging Face Expert Support, please contact us here - our team will reach out to discuss your requirements!
https://huggingface.co/blog/1b-sentence-embeddings
Train a Sentence Embedding Model with 1 Billion Training Pairs
Antoine SIMOULIN
October 25, 2021
Sentence embedding is a method that maps sentences to vectors of real numbers. Ideally, these vectors would capture the semantic of a sentence and be highly generic. Such representations could then be used for many downstream applications such as clustering, text mining, or question answering.We developed state-of-the-art sentence embedding models as part of the project "Train the Best Sentence Embedding Model Ever with 1B Training Pairs". This project took place during the Community week using JAX/Flax for NLP & CV, organized by Hugging Face. We benefited from efficient hardware infrastructure to run the project: 7 TPUs v3-8, as well as guidance from Google’s Flax, JAX, and Cloud team members about efficient deep learning frameworks!Training methodologyModelUnlike words, we can not define a finite set of sentences. Sentence embedding methods, therefore, compose inner words to compute the final representation. For example, SentenceBert model (Reimers and Gurevych, 2019) uses Transformer, the cornerstone of many NLP applications, followed by a pooling operation over the contextualized word vectors. (c.f. Figure below.)Multiple Negative Ranking LossThe parameters from the composition module are usually learned using a self-supervised objective. For the project, we used a contrastive training method illustrated in the figure below. We constitute a dataset with sentence pairs (ai,pi) (a_i, p_i) (ai​,pi​) such that sentences from the pair have a close meaning. For example, we consider pairs such as (query, answer-passage), (question, duplicate_question),(paper title, cited paper title). Our model is then trained to map pairs (ai,pi) (a_i , p_i) (ai​,pi​) to close vectors while assigning unmatched pairs (ai,pj),i≠j (a_i , p_j), i eq j (ai​,pj​),i=j to distant vectors in the embedding space. This training method is also called training with in-batch negatives, InfoNCE or NTXentLoss.Formally, given a batch of training samples, the model optimises the following loss function:−1n∑i=1nexp(sim(ai,pi))∑jexp(sim(ai,pj))-\frac{1}{n}\sum_{i=1}^n\frac{exp(sim(a_i, p_i))}{\sum_j exp(sim(a_i, p_j))}−n1​i=1∑n​∑j​exp(sim(ai​,pj​))exp(sim(ai​,pi​))​An illustrative example can be seen below. The model first embeds each sentence from every pair in the batch. Then, we compute a similarity matrix between every possible pair (ai,pj) (a_i, p_j) (ai​,pj​). We then compare the similarity matrix with the ground truth, which indicates the original pairs. Finally, we perform the comparison using the cross entropy loss.Intuitively, the model should assign high similarity to the sentences « How many people live in Berlin? » and « Around 3.5 million people live in Berlin » and low similarity to other negative answers such as « The capital of France is Paris » as detailed in the Figure below.In the loss equation, sim indicates a similarity function between (a,p) (a, p) (a,p). The similarity function could be either the Cosine-Similarity or the Dot-Product operator. Both methods have their pros and cons summarized below (Thakur et al., 2021, Bachrach et al., 2014):Cosine-similarityDot-productVector has highest similarity to itself since cos(a,a)=1 cos(a, a)=1 cos(a,a)=1.Other vectors can have higher dot-products dot(a,a)<dot(a,b) dot(a, a) < dot (a, b) dot(a,a)<dot(a,b).With normalised vectors it is equal to the dot product. The max vector length is equals 1.It might be slower with certain approximate nearest neighbour methods since the max vector not known.With normalised vectors, it is proportional to euclidian distance. It works with k-means clustering.It does not work with k-means clustering.In practice, we used a scaled similarity because score differences tends to be too small and apply a scaling factor C C C such that simscaled(a,b)=C∗sim(a,b) sim_{scaled}(a, b) = C * sim(a, b) simscaled​(a,b)=C∗sim(a,b) with typically C=20 C = 20 C=20 (Henderson and al., 2020, Radford and al., 2021).Improving Quality with Better BatchesIn our method, we build batches of sample pairs (ai,pi) (a_i , p_i) (ai​,pi​). We consider all other samples from the batch, (ai,pj),i≠j (a_i , p_j), i eq j (ai​,pj​),i=j, as negatives sample pairs. The batch composition is therefore a key training aspect. Given the literature in the domain, we mainly focused on three main aspects of the batch.1. Size mattersIn contrastive learning, a larger batch size is synonymous with better performance. As shown in the Figure extracted from Qu and al., (2021), a larger batch size increases the results.2. Hard NegativesIn the same figure, we observe that including hard negatives also improves performance. Hard negatives are sample pj p_j pj​ which are hard to distinguish from pi p_i pi​. In our example, it could be the pairs « What is the capital of France? » and « What is the capital of the US? » which have a close semantic content and requires precisely understanding the full sentence to be answered correctly. On the contrary, the samples « What is the capital of France? » and «How many Star Wars movies is there?» are less difficult to distinguish since they do not refer to the same topic.3. Cross dataset batchesWe concatenated multiple datasets to train our models. We built a large batch and gathered samples from the same batch dataset to limit the topic distribution and favor hard negatives. However, we also mix at least two datasets in the batch to learn a global structure between topics and not only a local structure within a topic.Training infrastructure and dataAs mentioned earlier, the quantity of data and the batch size directly impact the model performances. As part of the project, we benefited from efficient hardware infrastructure. We trained our models on TPUs which are compute units developed by Google and super efficient for matrix multiplications. TPUs have some hardware specificities which might require some specific code implementation.Additionally, we trained models on a large corpus as we concatenated multiple datasets up to 1 billion sentence pairs! All datasets used are detailed for each model in the model card.ConclusionYou can find all models and datasets we created during the challenge in our HuggingFace repository. We trained 20 general-purpose Sentence Transformers models such as Mini-LM (Wang and al., 2020), RoBERTa (liu and al., 2019), DistilBERT (Sanh and al., 2020) and MPNet (Song and al., 2020). Our models achieve SOTA on multiple general-purpose Sentence Similarity evaluation tasks. We also shared 8 datasets specialized for Question Answering, Sentence-Similarity, and Gender Evaluation. General sentence embeddings might be used for many applications. We built a Spaces demo to showcase several applications:The sentence similarity module compares the similarity of the main text with other texts of your choice. In the background, the demo extracts the embedding for each text and computes the similarity between the source sentence and the other using cosine similarity.Asymmetric QA compares the answer likeliness of a given query with answer candidates of your choice.Search / Cluster returns nearby answers from a query. For example, if you input « python », it will retrieve closest sentences using dot-product distance.Gender Bias Evaluation report inherent gender bias in training set via random sampling of the sentences. Given an anchor text without mentioning gender for target occupation and 2 propositions with gendered pronouns, we compare if models assign a higher similarity to a given proposition and therefore evaluate their proportion to favor a specific gender.The Community week using JAX/Flax for NLP & CV has been an intense and highly rewarding experience! The quality of Google’s Flax, JAX, and Cloud and Hugging Face team members' guidance and their presence helped us all learn a lot. We hope all projects had as much fun as we did in ours. Whenever you have questions or suggestions, don’t hesitate to contact us!
https://huggingface.co/blog/amused
Welcome aMUSEd: Efficient Text-to-Image Generation
Isamu Isozaki, Suraj Patil, Will Berman, Sayak Paul
January 4, 2024
We’re excited to present an efficient non-diffusion text-to-image model named aMUSEd. It’s called so because it’s a open reproduction of Google's MUSE. aMUSEd’s generation quality is not the best and we’re releasing a research preview with a permissive license. In contrast to the commonly used latent diffusion approach (Rombach et al. (2022)), aMUSEd employs a Masked Image Model (MIM) methodology. This not only requires fewer inference steps, as noted by Chang et al. (2023), but also enhances the model's interpretability.Just as MUSE, aMUSEd demonstrates an exceptional ability for style transfer using a single image, a feature explored in depth by Sohn et al. (2023). This aspect could potentially open new avenues in personalized and style-specific image generation.In this blog post, we will give you some internals of aMUSEd, show how you can use it for different tasks, including text-to-image, and show how to fine-tune it. Along the way, we will provide all the important resources related to aMUSEd, including its training code. Let’s get started 🚀Table of contentsHow does it work?Using in diffusersFine-tuning aMUSEdLimitationsResourcesWe have built a demo for readers to play with aMUSEd. You can try it out in this Space or in the playground embedded below:How does it work?aMUSEd is based on Masked Image Modeling. It makes for a compelling use case for the community to explore components that are known to work in language modeling in the context of image generation. The figure below presents a pictorial overview of how aMUSEd works. During training:input images are tokenized using a VQGAN to obtain image tokensthe image tokens are then masked according to a cosine masking schedule.the masked tokens (conditioned on the prompt embeddings computed using a CLIP-L/14 text encoder are passed to a U-ViT model that predicts the masked patchesDuring inference:input prompt is embedded using the CLIP-L/14 text encoder.iterate till N steps are reached:start with randomly masked tokens and pass them to the U-ViT model along with the prompt embeddingspredict the masked tokens and only keep a certain percentage of the most confident predictions based on the N and mask schedule. Mask the remaining ones and pass them off to the U-ViT modelpass the final output to the VQGAN decoder to obtain the final imageAs mentioned at the beginning, aMUSEd borrows a lot of similarities from MUSE. However, there are some notable differences:aMUSEd doesn’t follow a two-stage approach for predicting the final masked patches.Instead of using T5 for text conditioning, CLIP L/14 is used for computing the text embeddings.Following Stable Diffusion XL (SDXL), additional conditioning, such as image size and cropping, is passed to the U-ViT. This is referred to as “micro-conditioning”.To learn more about aMUSEd, we recommend reading the technical report here. Using aMUSEd in 🧨 diffusersaMUSEd comes fully integrated into 🧨 diffusers. To use it, we first need to install the libraries: pip install -U diffusers accelerate transformers -qLet’s start with text-to-image generation:import torchfrom diffusers import AmusedPipelinepipe = AmusedPipeline.from_pretrained("amused/amused-512", variant="fp16", torch_dtype=torch.float16)pipe = pipe.to("cuda")prompt = "A mecha robot in a favela in expressionist style"negative_prompt = "low quality, ugly"image = pipe(prompt, negative_prompt=negative_prompt, generator=torch.manual_seed(0)).images[0]imageWe can study how num_inference_steps affects the quality of the images under a fixed seed:from diffusers.utils import make_image_grid images = []for step in [5, 10, 15]:image = pipe(prompt, negative_prompt=negative_prompt, num_inference_steps=step, generator=torch.manual_seed(0)).images[0]images.append(image)grid = make_image_grid(images, rows=1, cols=3)gridCrucially, because of its small size (only ~800M parameters, including the text encoder and VQ-GAN), aMUSEd is very fast. The figure below provides a comparative study of the inference latencies of different models, including aMUSEd:Tuples, besides the model names, have the following format: (timesteps, resolution). Benchmark conducted on A100. More details are in the technical report.As a direct byproduct of its pre-training objective, aMUSEd can do image inpainting zero-shot, unlike other models such as SDXL. import torchfrom diffusers import AmusedInpaintPipelinefrom diffusers.utils import load_imagefrom PIL import Imagepipe = AmusedInpaintPipeline.from_pretrained("amused/amused-512", variant="fp16", torch_dtype=torch.float16)pipe = pipe.to("cuda")prompt = "a man with glasses"input_image = (load_image("https://huggingface.co/amused/amused-512/resolve/main/assets/inpainting_256_orig.png").resize((512, 512)).convert("RGB"))mask = (load_image("https://huggingface.co/amused/amused-512/resolve/main/assets/inpainting_256_mask.png").resize((512, 512)).convert("L")) image = pipe(prompt, input_image, mask, generator=torch.manual_seed(3)).images[0]aMUSEd is the first non-diffusion system within diffusers. Its iterative scheduling approach for predicting the masked patches made it a good candidate for diffusers. We are excited to see how the community leverages it. We encourage you to check out the technical report to learn about all the tasks we explored with aMUSEd. Fine-tuning aMUSEdWe provide a simple training script for fine-tuning aMUSEd on custom datasets. With the 8-bit Adam optimizer and float16 precision, it's possible to fine-tune aMUSEd with just under 11GBs of GPU VRAM. With LoRA, the memory requirements get further reduced to just 7GBs. a pixel art character with square red glassesaMUSEd comes with an OpenRAIL license, and hence, it’s commercially friendly to adapt. Refer to this directory for more details on fine-tuning. LimitationsaMUSEd is not a state-of-the-art image generation regarding image quality. We released aMUSEd to encourage the community to explore non-diffusion frameworks such as MIM for image generation. We believe MIM’s potential is underexplored, given its benefits:Inference efficiencySmaller size, enabling on-device applicationsTask transfer without requiring expensive fine-tuningAdvantages of well-established components from the language modeling world(Note that the original work on MUSE is close-sourced)For a detailed description of the quantitative evaluation of aMUSEd, refer to the technical report. We hope that the community will find the resources useful and feel motivated to improve the state of MIM for image generation. ResourcesPapers:Muse: Text-To-Image Generation via Masked Generative TransformersaMUSEd: An Open MUSE ReproductionExploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer (T5)Learning Transferable Visual Models From Natural Language Supervision (CLIP)SDXL: Improving Latent Diffusion Models for High-Resolution Image SynthesisSimple diffusion: End-to-end diffusion for high resolution images (U-ViT)LoRA: Low-Rank Adaptation of Large Language ModelsCode + misc:aMUSEd training codeaMUSEd documentationaMUSEd fine-tuning codeaMUSEd modelsAcknowledgementsSuraj led training. William led data and supported training. Patrick von Platen supported both training and data and provided general guidance. Robin Rombach did the VQGAN training and provided general guidance. Isamu Isozaki helped with insightful discussions and made code contributions.Thanks to Patrick von Platen and Pedro Cuenca for their reviews on the blog post draft.
https://huggingface.co/blog/safecoder
Introducing SafeCoder
Jeff Boudier, Philipp Schmid
August 22, 2023
Today we are excited to announce SafeCoder - a code assistant solution built for the enterprise.The goal of SafeCoder is to unlock software development productivity for the enterprise, with a fully compliant and self-hosted pair programmer. In marketing speak: “your own on-prem GitHub copilot”.Before we dive deeper, here’s what you need to know:SafeCoder is not a model, but a complete end-to-end commercial solutionSafeCoder is built with security and privacy as core principles - code never leaves the VPC during training or inferenceSafeCoder is designed for self-hosting by the customer on their own infrastructureSafeCoder is designed for customers to own their own Code Large Language ModelWhy SafeCoder?Code assistant solutions built upon LLMs, such as GitHub Copilot, are delivering strong productivity boosts. For the enterprise, the ability to tune Code LLMs on the company code base to create proprietary Code LLMs improves reliability and relevance of completions to create another level of productivity boost. For instance, Google internal LLM code assistant reports a completion acceptance rate of 25-34% by being trained on an internal code base.However, relying on closed-source Code LLMs to create internal code assistants exposes companies to compliance and security issues. First during training, as fine-tuning a closed-source Code LLM on an internal codebase requires exposing this codebase to a third party. And then during inference, as fine-tuned Code LLMs are likely to “leak” code from their training dataset during inference. To meet compliance requirements, enterprises need to deploy fine-tuned Code LLMs within their own infrastructure - which is not possible with closed source LLMs.With SafeCoder, Hugging Face will help customers build their own Code LLMs, fine-tuned on their proprietary codebase, using state of the art open models and libraries, without sharing their code with Hugging Face or any other third party. With SafeCoder, Hugging Face delivers a containerized, hardware-accelerated Code LLM inference solution, to be deployed by the customer directly within the Customer secure infrastructure, without code inputs and completions leaving their secure IT environment.From StarCoder to SafeCoderAt the core of the SafeCoder solution is the StarCoder family of Code LLMs, created by the BigCode project, a collaboration between Hugging Face, ServiceNow and the open source community.The StarCoder models offer unique characteristics ideally suited to enterprise self-hosted solution:State of the art code completion results - see benchmarks in the paper and multilingual code evaluation leaderboardDesigned for inference performance: a 15B parameters model with code optimizations, Multi-Query Attention for reduced memory footprint, and Flash Attention to scale to 8,192 tokens context.Trained on the Stack, an ethically sourced, open source code dataset containing only commercially permissible licensed code, with a developer opt-out mechanism from the get-go, refined through intensive PII removal and deduplication efforts.Note: While StarCoder is the inspiration and model powering the initial version of SafeCoder, an important benefit of building a LLM solution upon open source models is that it can adapt to the latest and greatest open source models available. In the future, SafeCoder may offer other similarly commercially permissible open source models built upon ethically sourced and transparent datasets as the base LLM available for fine-tuning.Privacy and Security as a Core PrincipleFor any company, the internal codebase is some of its most important and valuable intellectual property. A core principle of SafeCoder is that the customer internal codebase will never be accessible to any third party (including Hugging Face) during training or inference.In the initial set up phase of SafeCoder, the Hugging Face team provides containers, scripts and examples to work hand in hand with the customer to select, extract, prepare, duplicate, deidentify internal codebase data into a training dataset to be used in a Hugging Face provided training container configured to the hardware infrastructure available to the customer.In the deployment phase of SafeCoder, the customer deploys containers provided by Hugging Face on their own infrastructure to expose internal private endpoints within their VPC. These containers are configured to the exact hardware configuration available to the customer, including NVIDIA GPUs, AMD Instinct GPUs, Intel Xeon CPUs, AWS Inferentia2 or Habana Gaudi accelerators.Compliance as a Core PrincipleAs the regulation framework around machine learning models and datasets is still being written across the world, global companies need to make sure the solutions they use minimize legal risks.Data sources, data governance, management of copyrighted data are just a few of the most important compliance areas to consider. BigScience, the older cousin and inspiration for BigCode, addressed these areas in working groups before they were broadly recognized by the draft AI EU Act, and as a result was graded as most compliant among Foundational Model Providers in a Stanford CRFM study.BigCode expanded upon this work by implementing novel techniques for the code domain and building The Stack with compliance as a core principle, such as commercially permissible license filtering, consent mechanisms (developers can easily find out if their code is present and request to be opted out of the dataset), and extensive documentation and tools to inspect the source data, and dataset improvements (such as deduplication and PII removal).All these efforts translate into legal risk minimization for users of the StarCoder models, and customers of SafeCoder. And for SafeCoder users, these efforts translate into compliance features: when software developers get code completions these suggestions are checked against The Stack, so users know if the suggested code matches existing code in the source dataset, and what the license is. Customers can specify which licenses are preferred and surface those preferences to their users.How does it work?SafeCoder is a complete commercial solution, including service, software and support.Training your own SafeCoder modelStarCoder was trained in more than 80 programming languages and offers state of the art performance on multiple benchmarks. To offer better code suggestions specifically for a SafeCoder customer, we start the engagement with an optional training phase, where the Hugging Face team works directly with the customer team to guide them through the steps to prepare and build a training code dataset, and to create their own code generation model through fine-tuning, without ever exposing their codebase to third parties or the internet.The end result is a model that is adapted to the code languages, standards and practices of the customer. Through this process, SafeCoder customers learn the process and build a pipeline for creating and updating their own models, ensuring no vendor lock-in, and keeping control of their AI capabilities.Deploying SafeCoderDuring the setup phase, SafeCoder customers and Hugging Face design and provision the optimal infrastructure to support the required concurrency to offer a great developer experience. Hugging Face then builds SafeCoder inference containers that are hardware-accelerated and optimized for throughput, to be deployed by the customer on their own infrastructure.SafeCoder inference supports various hardware to give customers a wide range of options: NVIDIA Ampere GPUs, AMD Instinct GPUs, Habana Gaudi2, AWS Inferentia 2, Intel Xeon Sapphire Rapids CPUs and more.Using SafeCoderOnce SafeCoder is deployed and its endpoints are live within the customer VPC, developers can install compatible SafeCoder IDE plugins to get code suggestions as they work. Today, SafeCoder supports popular IDEs, including VSCode, IntelliJ and with more plugins coming from our partners.How can I get SafeCoder?Today, we are announcing SafeCoder in collaboration with VMware at the VMware Explore conference and making SafeCoder available to VMware enterprise customers. Working with VMware helps ensure the deployment of SafeCoder on customers’ VMware Cloud infrastructure is successful – whichever cloud, on-premises or hybrid infrastructure scenario is preferred by the customer. In addition to utilizing SafeCoder, VMware has published a reference architecture with code samples to enable the fastest possible time-to-value when deploying and operating SafeCoder on VMware infrastructure. VMware’s Private AI Reference Architecture makes it easy for organizations to quickly leverage popular open source projects such as ray and kubeflow to deploy AI services adjacent to their private datasets, while working with Hugging Face to ensure that organizations maintain the flexibility to take advantage of the latest and greatest in open-source models. This is all without tradeoffs in total cost of ownership or performance.“Our collaboration with Hugging Face around SafeCoder fully aligns to VMware’s goal of enabling customer choice of solutions while maintaining privacy and control of their business data. In fact, we have been running SafeCoder internally for months and have seen excellent results. Best of all, our collaboration with Hugging Face is just getting started, and I’m excited to take our solution to our hundreds of thousands of customers worldwide,” says Chris Wolf, Vice President of VMware AI Labs. Learn more about private AI and VMware’s differentiation in this emerging space here.If you’re interested in SafeCoder for your company, please contact us here - our team will contact you to discuss your requirements!
https://huggingface.co/blog/game-jam
Announcing the Open Source AI Game Jam 🎮
Thomas Simonini
June 1, 2023
Announcing the Open Source AI Game Jam 🎮Hugging Face Models Datasets Spaces Posts Docs Solutions Pricing Log In Sign Up Back to Articles Announcing the Open Source AI Game Jam 🎮
https://huggingface.co/blog/huggingface-and-ibm
Hugging Face and IBM partner on watsonx.ai, the next-generation enterprise studio for AI builders
Julien Simon
May 23, 2023
Hugging Face and IBM partner on watsonx.ai, the next-generation enterprise studio for AI buildersHugging FaceModelsDatasetsSpacesPostsDocsSolutionsPricingLog InSign UpBack to ArticlesHugging Face and IBM partner on watsonx.ai, the next-generation enterprise studio for AI builders
https://huggingface.co/blog/fellowship
Announcing the Hugging Face Fellowship Program
Merve Noyan, Omar Espejel
May 17, 2022
The Fellowship is a network of exceptional people from different backgrounds who contribute to the Machine Learning open-source ecosystem 🚀. The goal of the program is to empower key contributors to enable them to scale their impact while inspiring others to contribute as well. How the Fellowship works 🙌🏻 This is Hugging Face supporting the amazing work of contributors! Being a Fellow works differently for everyone. The key question here is:❓ What would contributors need to have more impact? How can Hugging Face support them so they can do that project they have always wanted to do?Fellows of all backgrounds are welcome! The progress of Machine Learning depends on grassroots contributions. Each person has a unique set of skills and knowledge that can be used to democratize the field in a variety of ways. Each Fellow achieves impact differently and that is perfect 🌈. Hugging Face supports them to continue creating and sharing the way that fits their needs the best. What are the benefits of being part of the Fellowship? 🤩 The benefits will be based on the interests of each individual. Some examples of how Hugging Face supports Fellows:💾 Computing and resources🎁 Merchandise and assets.✨ Official recognition from Hugging Face. How to become a Fellow Fellows are currently nominated by members of the Hugging Face team or by another Fellow. How can prospects get noticed? The main criterion is that they have contributed to the democratization of open-source Machine Learning.How? In the ways that they prefer. Here are some examples of the first Fellows:María Grandury - Created the largest Spanish-speaking NLP community and organized a Hackathon that achieved 23 Spaces, 23 datasets, and 33 models that advanced the SOTA for Spanish (see the Organization in the Hub). 👩🏼‍🎤Manuel Romero - Contributed over 300 models to the Hugging Face Hub. He has trained multiple SOTA models in Spanish. 🤴🏻Aritra Roy Gosthipathy: Contributed new architectures for TensorFlow to the Transformers library, improved Keras tooling, and helped create the Keras working group (for example, see his Vision Transformers tutorial). 🦹🏻 Vaibhav Srivastav - Advocacy in the field of speech. He has led the ML4Audio working group (see the recordings) and paper discussion sessions. 🦹🏻Bram Vanroy - Helped many contributors and the Hugging Face team from the beginning. He has reported several issues and merged pull requests in the Transformers library since September 2019. 🦸🏼 Christopher Akiki - Contributed to sprints, workshops, Big Science, and cool demos! Check out some of his recent projects like his TF-coder and the income stats explorer. 🦹🏻‍♀️Ceyda Çınarel - Contributed to many successful Hugging Face and Spaces models in various sprints. Check out her ButterflyGAN Space or search for reaction GIFs with CLIP. 👸🏻Additionally, there are strategic areas where Hugging Face is looking for open-source contributions. These areas will be added and updated frequently on the Fellowship Doc with specific projects. Prospects should not hesitate to write in the #looking-for-collaborators channel in the Hugging Face Discord if they want to undertake a project in these areas, support or be considered as a Fellow. Additionally, refer to the Where and how can I contribute? question below.If you are currently a student, consider applying to the Student Ambassador Program. The application deadline is June 13, 2022.Hugging Face is actively working to build a culture that values ​​diversity, equity, and inclusion. Hugging Face intentionally creates a community where people feel respected and supported, regardless of who they are or where they come from. This is critical to building the future of open Machine Learning. The Fellowship will not discriminate based on race, religion, color, national origin, gender, sexual orientation, age, marital status, veteran status, or disability status. Frequently Asked Questions I am just starting to contribute. Can I be a fellow?Fellows are nominated based on their open-source and community contributions. If you want to participate in the Fellowship, the best way to start is to begin contributing! If you are a student, the Student Ambassador Program might be more suitable for you (the application deadline is June 13, 2022).Where and how can I contribute?It depends on your interests. Here are some ideas of areas where you can contribute, but you should work on things that get you excited!Share exciting models with the community through the Hub. These can be for Computer Vision, Reinforcement Learning, and any other ML domain!Create tutorials and projects using different open-source libraries—for example, Stable-Baselines 3, fastai, or Keras.Organize local sprints to promote open source Machine Learning in different languages or niches. For example, the Somos NLP Hackathon focused on Spanish speakers. The HugGAN sprint focused on generative models.Translate the Hugging Face Course, the Transformers documentation or the Educational Toolkit.Doc with specific projects where contributions would be valuable. The Hugging Face team will frequently update the doc with new projects.Please share in the #looking-for-contributors channel on the Hugging Face Discord if you want to work on a particular project.Will I be an employee of Hugging Face?No, the Fellowship does not mean you are an employee of Hugging Face. However, feel free to mention in any forum, including LinkedIn, that you are a Hugging Face Fellow. Hugging Face is growing and this could be a good path for a bigger relationship in the future 😎. Check the Hugging Face job board for updated opportunities. Will I receive benefits during the Fellowship?Yes, the benefits will depend on the particular needs and projects that each Fellow wants to undertake.Is there a deadline?No. Admission to the program is ongoing and contingent on the nomination of a current Fellow or member of the Hugging Face team. Please note that being nominated may not be enough to be admitted as a Fellow.
https://huggingface.co/blog/us-national-ai-research-resource
AI Policy @🤗: Comments on U.S. National AI Research Resource Interim Report
Irene Solaiman
August 1, 2022
Comments on U.S. National AI Research Resource Interim ReportHugging FaceModelsDatasetsSpacesPostsDocsSolutionsPricingLog InSign UpBack to ArticlesAI Policy @🤗: Comments on U.S. National AI Research Resource Interim Report
https://huggingface.co/blog/quanto-introduction
Quanto: a pytorch quantization toolkit
David Corvoysier, Younes Belkada, Marc Sun
March 18, 2024
Quantization is a technique to reduce the computational and memory costs of evaluating Deep Learning Models by representing their weights and activations with low-precision data types like 8-bit integer (int8) instead of the usual 32-bit floating point (float32).Reducing the number of bits means the resulting model requires less memory storage, which is crucial for deploying Large Language Models on consumer devices.It also enables specific optimizations for lower bitwidth datatypes, such as int8 or float8 matrix multiplications on CUDA devices.Many open-source libraries are available to quantize pytorch Deep Learning Models, each providing very powerful features, yet often restricted to specific model configurations and devices.Also, although they are based on the same design principles, they are unfortunately often incompatible with one another.Today, we are excited to introduce quanto, a versatile pytorch quantization toolkit, that provides several unique features:available in eager mode (works with non-traceable models)quantized models can be placed on any device (including CUDA and MPS),automatically inserts quantization and dequantization stubs,automatically inserts quantized functional operations,automatically inserts quantized modules (see below the list of supported modules),provides a seamless workflow for a float model, going from a dynamic to a static quantized model,supports quantized model serialization as a state_dict,supports not only int8 weights, but also int2 and int4,supports not only int8 activations, but also float8.Recent quantization methods appear to be focused on quantizing Large Language Models (LLMs), whereas quanto intends to provide extremely simple quantization primitives for simple quantization schemes (linear quantization, per-group quantization) that are adaptable across any modality.The goal of quanto is not to replace other quantization libraries, but to foster innovation by lowering the barto implement and combine quantization features.Make no mistake, quantization is hard, and integrating it seamlessly in existing models requires a deep understanding of pytorch internals.But don't worry, quanto's goal is to do most of the heavy-lifting for you, so that you can focuson what matters most, exploring low-bitwidth machine learning and finding solutions for the GPU poor.Quantization workflowQuanto is available as a pip package.pip install quantoquanto does not make a clear distinction between dynamic and static quantization. Models are dynamically quantized first,but their weights can be "frozen" later to static values.A typical quantization workflow consists of the following steps:1. QuantizeThe first step converts a standard float model into a dynamically quantized model.quantize(model, weights=quanto.qint8, activations=quanto.qint8)At this stage, the model's float weights are dynamically quantized only for inference.2. Calibrate (optional if activations are not quantized)Quanto supports a calibration mode that records the activation ranges while passing representative samples through the quantized model.with calibration(momentum=0.9):model(samples)This automatically activates the quantization of the activations in the quantized modules.3. Tune, aka Quantization-Aware-Training (optional)If the performance of the model degrades too much, one can tune it for a few epochs to try to recover the float model performance.model.train()for batch_idx, (data, target) in enumerate(train_loader):data, target = data.to(device), target.to(device)optimizer.zero_grad()output = model(data).dequantize()loss = torch.nn.functional.nll_loss(output, target)loss.backward()optimizer.step()4. Freeze integer weightsWhen freezing a model, its float weights are replaced by quantized integer weights.freeze(model)Please refer to the examples for instantiations of the quantization workflow.You can also check this notebook where we show you how to quantize a BLOOM model with quanto!PerformanceThese are some very preliminary results, as we are constantly improving both the accuracy and efficiency of quantized models, but it already looks very promising.Below are two graphs evaluating the accuracy of different quantized configurations for mistralai/Mistral-7B-v0.1.Note: the first bar in each group always corresponds to the non-quantized model.These results are obtained without applying any Post-Training-Optimization algorithm like hqq or AWQ.The graph below gives the latency per-token measured on an NVIDIA A100 GPU.These results don't include any optimized matrix multiplication kernels.You can see that the quantization adds a significant overhead for lower bitwidth.Stay tuned for updated results as we are constantly improving quanto and will soon add optimizers and optimized kernels.Please refer to the quanto benchmarks for detailed results for different model architectures and configurations.Integration in transformersQuanto is seamlessly integrated in the Hugging Face transformers library. You can quantize any model by passing a QuantoConfig to from_pretrained!Currently, you need to use the latest version of accelerate to make sure the integration is fully compatible.from transformers import AutoModelForCausalLM, AutoTokenizer, QuantoConfigmodel_id = "facebook/opt-125m"tokenizer = AutoTokenizer.from_pretrained(model_id)quantization_config = QuantoConfig(weights="int8")quantized_model = AutoModelForCausalLM.from_pretrained(model_id,quantization_config= quantization_config)You can quantize the weights and/or activations in int8, float8, int4, or int2 by simply passing the correct argument in QuantoConfig. The activations can be either in int8 or float8. For float8, you need to have hardware that is compatible with float8 precision, otherwise quanto will silently upcast the weights and activations to torch.float32 or torch.float16 (depending on the original data type of the model) when we perform the matmul (only when the weight is quantized). If you try to use float8 using MPS devices, torch will currently raise an error.Quanto is device agnostic, meaning you can quantize and run your model regardless if you are on CPU/GPU/ MPS (Apple Silicon).Quanto is also torch.compile friendly. You can quantize a model with quanto and call torch.compile to the model to compile it for faster generation. This feature might not work out of the box if dynamic quantization is involved (i.e., Quantization Aware Training or quantized activations enabled). Make sure to keep activations=None when creating your QuantoConfig in case you use the transformers integration.It is also possible to quantize any model, regardless of the modality using quanto! We demonstrate how to quantize openai/whisper-large-v3 model in int8 using quanto.from transformers import AutoModelForSpeechSeq2Seqmodel_id = "openai/whisper-large-v3"quanto_config = QuantoConfig(weights="int8")model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id,torch_dtype=torch.float16,device_map="cuda",quantization_config=quanto_config)Check out this notebook for a complete tutorial on how to properly use quanto with the transformers integration!Implementation detailsQuantized tensorsAt the heart of quanto are Tensor subclasses that corresponds to:the projection using a scale of a source Tensor into the optimal range for a given quantization type,the mapping of projected values to the destination type.For floating-point destination types, the mapping is done by the native pytorch cast (i.e. Tensor.to()).For integer destination types, the mapping is a simple rounding operation (i.e. torch.round()).The goal of the projection is to increase the accuracy of the conversion by minimizing the number of:saturated values (i.e. mapped to the destination type min/max),zeroed values (because they are below the smallest number that can be represented by the destination type)For efficiency, the projection is symmetric for 8-bit quantization types, i.e. it is centered around zero.Symmetric quantized Tensors are usually compatible with many standard operations.For lower bitwidth quantization types, such as int2 or int4, the projection is affine, i.e. it uses a zeropoint to shift theprojected values, which allows a better coverage of the quantization range. Affine quantized Tensors are typically harder to work withand require custom operations.Quantized modulesQuanto provides a generic mechanism to replace torch modules (torch.nn.Module) by quanto modules that are able to process quanto tensors.Quanto modules dynamically convert their weight parameter until a model is frozen, which slows inference down a bit but isrequired if the model needs to be tuned (a.k.a Quantization Aware Training).Module bias parameters are not quantized because they are much smaller than weights and quantized addition is hard to accelerate.Activations are dynamically quantized using static scales (defaults to the range [-1, 1]). The model needs to be calibrated to evaluatethe best activation scales (using momentum).The following modules can be quantized:Linear (QLinear).Weights are always quantized, and biases are not quantized. Inputs and outputs can be quantized.Conv2d (QConv2D).Weights are always quantized, and biases are not quantized. Inputs and outputs can be quantized.LayerNorm,Weights and biases are not quantized. Outputs can be quantized.Custom operationsThanks to the awesome pytorch dispatch mechanism, quanto provides implementations forthe most common functions used in transformers or diffusers models, enabling quantized Tensors without modifying the modeling code too much.Most of these "dispatched" functions can be performed using combinations of standard pytorch operations.Complex functions however require the definition of custom operations under the torch.ops.quanto namespace.Examples of such operations are fused matrix multiplications involving lower bitwidth terms.Post-training quantization optimizersPost-training quantization optimizers are not available yet in quanto, but the library is versatile enoughto be compatible with most PTQ optimization algorithms like hqq or AWQ.Moving forward, the plan is to integrate the most popular algorithms in the most seamless possible way.Contributing to quantoContributions to quanto are very much welcomed, especially in the following areas:optimized kernels for quanto operations targeting specific devices,PTQ optimizers,new dispatched operations for quantized Tensors.
https://huggingface.co/blog/how-to-deploy-a-pipeline-to-google-clouds
My Journey to a serverless transformers pipeline on Google Cloud
Dominici
March 18, 2021
This article will discuss my journey to deploy the transformers sentiment-analysis pipeline on Google Cloud. We will start with a quick introduction to transformers and then move to the technical part of the implementation. Finally, we'll summarize this implementation and review what we have achieved.The GoalI wanted to create a micro-service that automatically detects whether a customer review left in Discord is positive or negative. This would allow me to treat the comment accordingly and improve the customer experience. For instance, if the review was negative, I could create a feature which would contact the customer, apologize for the poor quality of service, and inform him/her that our support team will contact him/her as soon as possible to assist him and hopefully fix the problem. Since I don't plan to get more than 2,000 requests per month, I didn't impose any performance constraints regarding the time and the scalability.The Transformers libraryI was a bit confused at the beginning when I downloaded the .h5 file. I thought it would be compatible with tensorflow.keras.models.load_model, but this wasn't the case. After a few minutes of research I was able to figure out that the file was a weights checkpoint rather than a Keras model.After that, I tried out the API that Hugging Face offers and read a bit more about the pipeline feature they offer. Since the results of the API & the pipeline were great, I decided that I could serve the model through the pipeline on my own server.Below is the official example from the Transformers GitHub page.from transformers import pipeline# Allocate a pipeline for sentiment-analysisclassifier = pipeline('sentiment-analysis')classifier('We are very happy to include pipeline into the transformers repository.')[{'label': 'POSITIVE', 'score': 0.9978193640708923}]Deploy transformers to Google CloudGCP is chosen as it is the cloud environment I am using in my personal organization.Step 1 - ResearchI already knew that I could use an API-Service like flask to serve a transformers model. I searched in the Google Cloud AI documentation and found a service to host Tensorflow models named AI-Platform Prediction. I also found App Engine and Cloud Run there, but I was concerned about the memory usage for App Engine and was not very familiar with Docker.Step 2 - Test on AI-Platform PredictionAs the model is not a "pure TensorFlow" saved model but a checkpoint, and I couldn't turn it into a "pure TensorFlow model", I figured out that the example on this page wouldn't work.From there I saw that I could write some custom code, allowing me to load the pipeline instead of having to handle the model, which seemed is easier. I also learned that I could define a pre-prediction & post-prediction action, which could be useful in the future for pre- or post-processing the data for customers' needs.I followed Google's guide but encountered an issue as the service is still in beta and everything is not stable. This issue is detailed here.Step 3 - Test on App EngineI moved to Google's App Engine as it's a service that I am familiar with, but encountered an installation issue with TensorFlow due to a missing system dependency file. I then tried with PyTorch which worked with an F4_1G instance, but it couldn't handle more than 2 requests on the same instance, which isn't really great performance-wise.Step 4 - Test on Cloud RunLastly, I moved to Cloud Run with a docker image. I followed this guide to get an idea of how it works. In Cloud Run, I could configure a higher memory and more vCPUs to perform the prediction with PyTorch. I ditched Tensorflow as PyTorch seems to load the model faster.Implementation of the serverless pipelineThe final solution consists of four different components: main.py handling the request to the pipelineDockerfile used to create the image that will be deployed on Cloud Run.Model folder having the pytorch_model.bin, config.json and vocab.txt.Model : DistilBERT base uncased finetuned SST-2To download the model folder, follow the instructions in the button. You don't need to keep the rust_model.ot or the tf_model.h5 as we will use PyTorch.requirement.txt for installing the dependenciesThe content on the main.py is really simple. The idea is to receive a GET request containing two fields. First the review that needs to be analysed, second the API key to "protect" the service. The second parameter is optional, I used it to avoid setting up the oAuth2 of Cloud Run. After these arguments are provided, we load the pipeline which is built based on the model distilbert-base-uncased-finetuned-sst-2-english (provided above). In the end, the best match is returned to the client.import osfrom flask import Flask, jsonify, requestfrom transformers import pipelineapp = Flask(__name__)model_path = "./model"@app.route('/')def classify_review():review = request.args.get('review')api_key = request.args.get('api_key')if review is None or api_key != "MyCustomerApiKey":return jsonify(code=403, message="bad request")classify = pipeline("sentiment-analysis", model=model_path, tokenizer=model_path)return classify("that was great")[0]if __name__ == '__main__':# This is used when running locally only. When deploying to Google Cloud# Run, a webserver process such as Gunicorn will serve the app.app.run(debug=False, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))Then the DockerFile which will be used to create a docker image of the service. We specify that our service runs with python:3.7, plus that we need to install our requirements. Then we use gunicorn to handle our process on the port 5000.# Use Python37FROM python:3.7# Allow statements and log messages to immediately appear in the Knative logsENV PYTHONUNBUFFERED True# Copy requirements.txt to the docker image and install packagesCOPY requirements.txt /RUN pip install -r requirements.txt# Set the WORKDIR to be the folderCOPY . /app# Expose port 5000EXPOSE 5000ENV PORT 5000WORKDIR /app# Use gunicorn as the entrypointCMD exec gunicorn --bind :$PORT main:app --workers 1 --threads 1 --timeout 0It is important to note the arguments --workers 1 --threads 1 which means that I want to execute my app on only one worker (= 1 process) with a single thread. This is because I don't want to have 2 instances up at once because it might increase the billing. One of the downsides is that it will take more time to process if the service receives two requests at once. After that, I put the limit to one thread due to the memory usage needed for loading the model into the pipeline. If I were using 4 threads, I might have 4 Gb / 4 = 1 Gb only to perform the full process, which is not enough and would lead to a memory error.Finally, the requirement.txt fileFlask==1.1.2torch===1.7.1transformers~=4.2.0gunicorn>=20.0.0Deployment instructionsFirst, you will need to meet some requirements such as having a project on Google Cloud, enabling the billing and installing the gcloud cli. You can find more details about it in the Google's guide - Before you begin, Second, we need to build the docker image and deploy it to cloud run by selecting the correct project (replace PROJECT-ID) and set the name of the instance such as ai-customer-review. You can find more information about the deployment on Google's guide - Deploying to.gcloud builds submit --tag gcr.io/PROJECT-ID/ai-customer-reviewgcloud run deploy --image gcr.io/PROJECT-ID/ai-customer-review --platform managedAfter a few minutes, you will also need to upgrade the memory allocated to your Cloud Run instance from 256 MB to 4 Gb. To do so, head over to the Cloud Run Console of your project.There you should find your instance, click on it.After that you will have a blue button labelled "edit and deploy new revision" on top of the screen, click on it and you'll be prompt many configuration fields. At the bottom you should find a "Capacity" section where you can specify the memory.PerformancesHandling a request takes less than five seconds from the moment you send the request including loading the model into the pipeline, and prediction. The cold start might take up an additional 10 seconds more or less.We can improve the request handling performance by warming the model, it means loading it on start-up instead on each request (global variable for example), by doing so, we win time and memory usage.CostsI simulated the cost based on the Cloud Run instance configuration with Google pricing simulatorFor my micro-service, I am planning to near 1,000 requests per month, optimistically. 500 may more likely for my usage. That's why I considered 2,000 requests as an upper bound when designing my microservice.Due to that low number of requests, I didn't bother so much regarding the scalability but might come back into it if my billing increases.Nevertheless, it's important to stress that you will pay the storage for each Gigabyte of your build image. It's roughly €0.10 per Gb per month, which is fine if you don't keep all your versions on the cloud since my version is slightly above 1 Gb (Pytorch for 700 Mb & the model for 250 Mb).ConclusionBy using Transformers' sentiment analysis pipeline, I saved a non-negligible amount of time. Instead of training/fine-tuning a model, I could find one ready to be used in production and start the deployment in my system. I might fine-tune it in the future, but as shown on my test, the accuracy is already amazing!I would have liked a "pure TensorFlow" model, or at least a way to load it in TensorFlow without Transformers dependencies to use the AI platform. It would also be great to have a lite version.
https://huggingface.co/blog/writer-case-study
Leveraging Hugging Face for complex generative AI use casess
Jeff Boudier, Waseem AlShikh
July 1, 2023
Leveraging Hugging Face for complex generative AI use casesHugging FaceModelsDatasetsSpacesPostsDocsSolutionsPricingLog InSign UpBack to ArticlesLeveraging Hugging Face for complex generative AI use casess
https://huggingface.co/blog/matryoshka
🪆 Introduction to Matryoshka Embedding Models
Tom Aarsen, Joshua, Omar Sanseviero
February 23, 2024
In this blogpost, we will introduce you to the concept of Matryoshka Embeddings and explain why they are useful. We will discuss how these models are theoretically trained and how you can train them using Sentence Transformers.Additionally, we will provide practical guidance on how to use Matryoshka Embedding models and share a comparison between a Matryoshka embedding model and a regular embedding model. Finally, we invite you to check out our interactive demo that showcases the power of these models.Table of ContentsUnderstanding Embeddings🪆 Matryoshka Embeddings🪆 Matryoshka DollsWhy would you use 🪆 Matryoshka Embedding models?How are 🪆 Matryoshka Embedding models trained?TheoreticallyIn Sentence TransformersHow do I use 🪆 Matryoshka Embedding models?TheoreticallyIn Sentence TransformersResultsDemoReferencesUnderstanding EmbeddingsEmbeddings are one of the most versatile tools in natural language processing, enabling practitioners to solve a large variety of tasks. In essence, an embedding is a numerical representation of a more complex object, like text, images, audio, etc. The embedding model will always produce embeddings of the same fixed size. You can then compute the similarity of complex objects by computing the similarity of the respective embeddings!This has an enormous amount of use cases, and serves as the backbone for recommendation systems, retrieval, one-shot or few-shot learning, outlier detection, similarity search, paraphrase detection, clustering, classification, and much more!🪆 Matryoshka EmbeddingsAs research progressed, new state-of-the-art (text) embedding models started producing embeddings with increasingly higher output dimensions, i.e., every input text is represented using more values. Although this improves performance, it comes at the cost of efficiency of downstream tasks such as search or classification.Consequently, Kusupati et al. (2022) were inspired to create embedding models whose embeddings could reasonably be shrunk without suffering too much on performance.These Matryoshka embedding models are trained such that these small truncated embeddings would still be useful. In short, Matryoshka embedding models can produce useful embeddings of various dimensions.🪆 Matryoshka DollsFor those unfamiliar, "Matryoshka dolls", also known as "Russian nesting dolls", are a set of wooden dolls of decreasing size that are placed inside one another. In a similar way, Matryoshka embedding models aim to store more important information in earlier dimensions, and less important information in later dimensions. This characteristic of Matryoshka embedding models allows us to truncate the original (large) embedding produced by the model, while still retaining enough of the information to perform well on downstream tasks.Why would you use 🪆 Matryoshka Embedding models?Such variable-size embedding models can be quite valuable to practitioners, for example:Shortlisting and reranking: Rather than performing your downstream task (e.g., nearest neighbor search) on the full embeddings, you can shrink the embeddings to a smaller size and very efficiently "shortlist" your embeddings. Afterwards, you can process the remaining embeddings using their full dimensionality.Trade-offs: Matryoshka models will allow you to scale your embedding solutions to your desired storage cost, processing speed, and performance.How are 🪆 Matryoshka Embedding models trained?TheoreticallyThe Matryoshka Representation Learning (MRL) approach can be adopted for almost all embedding model training frameworks. Normally, a training step for an embedding model involves producing embeddings for your training batch (of texts, for example) and then using some loss function to create a loss value that represents the quality of the produced embeddings. The optimizer will adjust the model weights throughout training to reduce the loss value.For Matryoshka Embedding models, a training step also involves producing embeddings for your training batch, but then you use some loss function to determine not just the quality of your full-size embeddings, but also the quality of your embeddings at various different dimensionalities. For example, output dimensionalities are 768, 512, 256, 128, and 64. The loss values for each dimensionality are added together, resulting in a final loss value. The optimizer will then try and adjust the model weights to lower this loss value.In practice, this incentivizes the model to frontload the most important information at the start of an embedding, such that it will be retained if the embedding is truncated. In Sentence TransformersSentence Tranformers is a commonly used framework to train embedding models, and it recently implemented support for Matryoshka models. Training a Matryoshka embedding model using Sentence Transformers is quite elementary: rather than applying some loss function on only the full-size embeddings, we also apply that same loss function on truncated portions of the embeddings.For example, if a model has an original embedding dimension of 768, it can now be trained on 768, 512, 256, 128 and 64. Each of these losses will be added together, optionally with some weight:from sentence_transformers import SentenceTransformerfrom sentence_transformers.losses import CoSENTLoss, MatryoshkaLossmodel = SentenceTransformer("microsoft/mpnet-base")base_loss = CoSENTLoss(model=model)loss = MatryoshkaLoss(model=model,loss=base_loss,matryoshka_dims=[768, 512, 256, 128, 64],matryoshka_weight=[1, 1, 1, 1, 1],)model.fit(train_objectives=[(train_dataset, loss)],...,)Training with MatryoshkaLoss does not incur a notable overhead in training time.References:MatryoshkaLossCoSENTLossSentenceTransformerSentenceTransformer.fitMatryoshka Embeddings - TrainingSee the following complete scripts as examples of how to apply the MatryoshkaLoss in practice:matryoshka_nli.py: This example uses the MultipleNegativesRankingLoss with MatryoshkaLoss to train a strong embedding model using Natural Language Inference (NLI) data. It is an adaptation of the NLI documentation.matryoshka_nli_reduced_dim.py: This example uses the MultipleNegativesRankingLoss with MatryoshkaLoss to train a strong embedding model with a small maximum output dimension of 256. It trains using Natural Language Inference (NLI) data, and is an adaptation of the NLI documentation.matryoshka_sts.py: This example uses the CoSENTLoss with MatryoshkaLoss to train an embedding model on the training set of the STSBenchmark dataset. It is an adaptation of the STS documentation.How do I use 🪆 Matryoshka Embedding models?TheoreticallyIn practice, getting embeddings from a Matryoshka embedding model works the same way as with a normal embedding model. The only difference is that, after receiving the embeddings, we can optionally truncate them to a smaller dimensionality. Do note that if the embeddings were normalized, then after truncating they will no longer be, so you may want to re-normalize.After truncating, you can either directly apply them for your use cases, or store them such that they can be used later. After all, smaller embeddings in your vector database should result in considerable speedups!Keep in mind that although processing smaller embeddings for downstream tasks (retrieval, clustering, etc.) will be faster, getting the smaller embeddings from the model is just as fast as getting the larger ones.In Sentence TransformersIn Sentence Transformers, you can load a Matryoshka Embedding model just like any other model, but you can specify the desired embedding size using the truncate_dim argument. After that, you can perform inference using the SentenceTransformers.encode function, and the embeddings will be automatically truncated to the specified size.Let's try to use a model that I trained using matryoshka_nli.py with microsoft/mpnet-base:from sentence_transformers import SentenceTransformerfrom sentence_transformers.util import cos_simmatryoshka_dim = 64model = SentenceTransformer("tomaarsen/mpnet-base-nli-matryoshka", truncate_dim=matryoshka_dim)embeddings = model.encode(["The weather is so nice!","It's so sunny outside!","He drove to the stadium.",])print(embeddings.shape)# => (3, 64)# Similarity of the first sentence to the other two:similarities = cos_sim(embeddings[0], embeddings[1:])print(similarities)# => tensor([[0.8910, 0.1337]])Link to the model: tomaarsen/mpnet-base-nli-matryoshkaFeel free to experiment with using different values for matryoshka_dim and observe how that affects the similarities. You can do so either by running this code locally, on the cloud such as with Google Colab, or by checking out the demo.References:SentenceTransformerSentenceTransformer.encodeutil.cos_simMatryoshka Embeddings - InferenceClick here to see how to use the Nomic v1.5 Matryoshka ModelNote: Nomic specifically requires an F.layer_norm before the embedding truncation. As a result, the following snippet uses manual truncation to the desired dimension. For all other models, you can use the truncate_dim option in the constructor, as shown in the previous example.from sentence_transformers import SentenceTransformerfrom sentence_transformers.util import cos_simimport torch.nn.functional as Fmodel = SentenceTransformer("nomic-ai/nomic-embed-text-v1.5", trust_remote_code=True)matryoshka_dim = 64embeddings = model.encode(["search_query: What is TSNE?","search_document: t-distributed stochastic neighbor embedding (t-SNE) is a statistical method for visualizing high-dimensional data by giving each datapoint a location in a two or three-dimensional map.","search_document: Amelia Mary Earhart was an American aviation pioneer and writer.",],convert_to_tensor=True,)# The Nomic team uses a custom architecture, making them recommend Layer Normalization before truncationembeddings = F.layer_norm(embeddings, normalized_shape=(embeddings.shape[1],))embeddings[..., :matryoshka_dim] # Shrink the embedding dimensionssimilarities = cos_sim(embeddings[0], embeddings[1:])# => tensor([[0.7154, 0.4468]])Link to the model: nomic-ai/nomic-embed-text-v1.5ResultsNow that Matryoshka models have been introduced, let's look at the actual performance that we may be able to expect from a Matryoshka embedding model versus a regular embedding model. For this experiment, I have trained two models:tomaarsen/mpnet-base-nli-matryoshka: Trained by running matryoshka_nli.py with microsoft/mpnet-base.tomaarsen/mpnet-base-nli: Trained by running a modified version of matryoshka_nli.py where the training loss is only MultipleNegativesRankingLoss rather than MatryoshkaLoss on top of MultipleNegativesRankingLoss. I also use microsoft/mpnet-base as the base model.Both of these models were trained on the AllNLI dataset, which is a concatenation of the SNLI and MultiNLI datasets. I have evaluated these models on the STSBenchmark test set using multiple different embedding dimensions. The results are plotted in the following figure:In the top figure, you can see that the Matryoshka model reaches a higher Spearman similarity than the standard model at all dimensionalities, indicative that the Matryoshka model is superior in this task.Furthermore, the performance of the Matryoshka model falls off much less quickly than the standard model. This is shown clearly in the second figure, which shows the performance at the embedding dimension relative to the maximum performance. Even at 8.3% of the embedding size, the Matryoshka model preserves 98.37% of the performance, much higher than the 96.46% by the standard model.These findings are indicative that truncating embeddings by a Matryoshka model could: 1) significantly speed up downstream tasks such as retrieval and 2) significantly save on storage space, all without a notable hit in performance.DemoIn this demo, you can dynamically shrink the output dimensions of the nomic-ai/nomic-embed-text-v1.5 Matryoshka embedding model and observe how it affects the retrieval performance. All of the embeddings are computed in the browser using 🤗 Transformers.js.ReferencesKusupati, A., Bhatt, G., Rege, A., Wallingford, M., Sinha, A., Ramanujan, V., ... & Farhadi, A. (2022). Matryoshka representation learning. Advances in Neural Information Processing Systems, 35, 30233-30249. https://arxiv.org/abs/2205.13147Matryoshka Embeddings — Sentence-Transformers documentation. (n.d.). https://sbert.net/examples/training/matryoshka/README.htmlUKPLab. (n.d.). GitHub. https://github.com/UKPLab/sentence-transformersUnboxing Nomic Embed v1.5: Resizable Production Embeddings with Matryoshka Representation Learning. (n.d.). https://blog.nomic.ai/posts/nomic-embed-matryoshka
https://huggingface.co/blog/train-your-controlnet
Train your ControlNet with diffusers 🧨
Apolinário from multimodal AI art, Pedro Cuenca
March 24, 2023
IntroductionControlNet is a neural network structure that allows fine-grained control of diffusion models by adding extra conditions. The technique debuted with the paper Adding Conditional Control to Text-to-Image Diffusion Models, and quickly took over the open-source diffusion community author's release of 8 different conditions to control Stable Diffusion v1-5, including pose estimations, depth maps, canny edges, sketches, and more.In this blog post we will go over each step in detail on how we trained the Uncanny Faces model - a model on face poses based on 3D synthetic faces (the uncanny faces was an unintended consequence actually, stay tuned to see how it came through).Getting started with training your ControlNet for Stable DiffusionTraining your own ControlNet requires 3 steps: Planning your condition: ControlNet is flexible enough to tame Stable Diffusion towards many tasks. The pre-trained models showcase a wide-range of conditions, and the community has built others, such as conditioning on pixelated color palettes.Building your dataset: Once a condition is decided, it is time to build your dataset. For that, you can either construct a dataset from scratch, or use a sub-set of an existing dataset. You need three columns on your dataset to train the model: a ground truth image, a conditioning_image and a prompt. Training the model: Once your dataset is ready, it is time to train the model. This is the easiest part thanks to the diffusers training script. You'll need a GPU with at least 8GB of VRAM.1. Planning your conditionTo plan your condition, it is useful to think of two questions: What kind of conditioning do I want to use?Is there an already existing model that can convert 'regular' images into my condition?For our example, we thought about using a facial landmarks conditioning. Our reasoning was: 1. the general landmarks conditioned ControlNet works well. 2. Facial landmarks are a widespread enough technique, and there are multiple models that calculate facial landmarks on regular pictures 3. Could be fun to tame Stable Diffusion to follow a certain facial landmark or imitate your own facial expression.2. Building your datasetOkay! So we decided to do a facial landmarks Stable Diffusion conditioning. So, to prepare the dataset we need: The ground truth image: in this case, images of facesThe conditioning_image: in this case, images where the facial landmarks are visualisedThe caption: a caption that describes the images being usedFor this project, we decided to go with the FaceSynthetics dataset by Microsoft: it is a dataset that contains 100K synthetic faces. Other face research datasets with real faces such as Celeb-A HQ, FFHQ - but we decided to go with synthetic faces for this project.The FaceSynthetics dataset sounded like a great start: it contains ground truth images of faces, and facial landmarks annotated in the iBUG 68-facial landmarks format, and a segmented image of the face. Perfect. Right? Unfortunately, not really. Remember the second question in the "planning your condition" step - that we should have models that convert regular images to the conditioning? Turns out there was is no known model that can turn faces into the annotated landmark format of this dataset.So we decided to follow another path:Use the ground truths image of faces of the FaceSynthetics dataseUse a known model that can convert any image of a face into the 68-facial landmarks format of iBUG (in our case we used the SOTA model SPIGA)Use custom code that converts the facial landmarks into a nice illustrated mask to be used as the conditioning_imageSave that as a Hugging Face DatasetHere you can find the code used to convert the ground truth images from the FaceSynthetics dataset into the illustrated mask and save it as a Hugging Face Dataset.Now, with the ground truth image and the conditioning_image on the dataset, we are missing one step: a caption for each image. This step is highly recommended, but you can experiment with empty prompts and report back on your results. As we did not have captions for the FaceSynthetics dataset, we ran it through a BLIP captioning. You can check the code used for captioning all images hereWith that, we arrived to our final dataset! The Face Synthetics SPIGA with captions contains a ground truth image, segmentation and a caption for the 100K images of the FaceSynthetics dataset. We are ready to train the model!3. Training the modelWith our dataset ready, it is time to train the model! Even though this was supposed to be the hardest part of the process, with the diffusers training script, it turned out to be the easiest. We used a single A100 rented for US$1.10/h on LambdaLabs. Our training experienceWe trained the model for 3 epochs (this means that the batch of 100K images were shown to the model 3 times) and a batch size of 4 (each step shows 4 images to the model). This turned out to be excessive and overfit (so it forgot concepts that diverge a bit of a real face, so for example "shrek" or "a cat" in the prompt would not make a shrek or a cat but rather a person, and also started to ignore styles). With just 1 epoch (so after the model "saw" 100K images), it already converged to following the poses and not overfit. So it worked, but... as we used the face synthetics dataset, the model ended up learning uncanny 3D-looking faces, instead of realistic faces. This makes sense given that we used a synthetic face dataset as opposed to real ones, and can be used for fun/memetic purposes. Here is the uncannyfaces_25K model. In this interactive table you can play with the dial below to go over how many training steps the model went through and how it affects the training process. At around 15K steps, it already started learning the poses. And it matured around 25K steps. Here How did we do the trainingAll we had to do was, install the dependencies:pip install git+https://github.com/huggingface/diffusers.git transformers accelerate xformers==0.0.16 wandbhuggingface-cli loginwandb login And then run the train_controlnet.py code!accelerate launch train_controlnet.py \--pretrained_model_name_or_path="stabilityai/stable-diffusion-2-1-base" \--output_dir="model_out" \--dataset_name=multimodalart/facesyntheticsspigacaptioned \--conditioning_image_column=spiga_seg \--image_column=image \--caption_column=image_caption \--resolution=512 \--learning_rate=1e-5 \--validation_image "./face_landmarks1.jpeg" "./face_landmarks2.jpeg" "./face_landmarks3.jpeg" \--validation_prompt "High-quality close-up dslr photo of man wearing a hat with trees in the background" "Girl smiling, professional dslr photograph, dark background, studio lights, high quality" "Portrait of a clown face, oil on canvas, bittersweet expression" \--train_batch_size=4 \--num_train_epochs=3 \--tracker_project_name="controlnet" \--enable_xformers_memory_efficient_attention \--checkpointing_steps=5000 \--validation_steps=5000 \--report_to wandb \--push_to_hubLet's break down some of the settings, and also let's go over some optimisation tips for going as low as 8GB of VRAM for training.pretrained_model_name_or_path: The Stable Diffusion base model you would like to use (we chose v2-1 here as it can render faces better)output_dir: The directory you would like your model to be saveddataset_name: The dataset that will be used for training. In our case Face Synthetics SPIGA with captionsconditioning_image_column: The name of the column in your dataset that contains the conditioning image (in our case spiga_seg)image_column: The name of the colunn in your dataset that contains the ground truth image (in our case image)caption_column: The name of the column in your dataset that contains the caption of tha image (in our case image_caption)resolution: The resolution of both the conditioning and ground truth images (in our case 512x512)learning_rate: The learing rate. We found out that 1e-5 worked well for these examples, but you may experiment with different values ranging between 1e-4 and 2e-6, for example.validation_image: This is for you to take a sneak peak during training! The validation images will be ran for every amount of validation_steps so you can see how your training is going. Insert here a local path to an arbitrary number of conditioning imagesvalidation_prompt: A prompt to be ran togehter with your validation image. Can be anything that can test if your model is training welltrain_batch_size: This is the size of the training batch to fit the GPU. We can afford 4 due to having an A100, but if you have a GPU with lower VRAM we recommend bringing this value down to 1.num_train_epochs: Each epoch corresponds to how many times the images in the training set will be "seen" by the model. We experimented with 3 epochs, but turns out the best results required just a bit more than 1 epoch, with 3 epochs our model overfit.checkpointing_steps: Save an intermediary checkpoint every x steps (in our case 5000). Every 5000 steps, an intermediary checkpoint was saved.validation_steps: Every x steps the validaton_prompt and the validation_image are ran. report_to: where to report your training to. Here we used Weights and Biases, which gave us this nice report.But reducing the train_batch_size from 4 to 1 may not be enough for the training to fit a small GPU, here are some additional parameters to add for each GPU VRAM size: push_to_hub: a parameter to push the final trained model to the Hugging Face Hub.Fitting on a 16GB VRAM GPUpip install bitsandbytes--train_batch_size=1 \--gradient_accumulation_steps=4 \--gradient_checkpointing \--use_8bit_adamThe combination of a batch size of 1 with 4 gradient accumulation steps is equivalent to using the original batch size of 4 we used in our example. In addition, we enabled gradient checkpointing and 8-bit Adam for additional memory savings.Fitting on a 12GB VRAM GPU--gradient_accumulation_steps=4 \--gradient_checkpointing \--use_8bit_adam--set_grads_to_noneFitting on a 8GB VRAM GPUPlease follow our guide here4. Conclusion!This experience of training a ControlNet was a lot of fun. We succesfully trained a model that can follow real face poses - however it learned to make uncanny 3D faces instead of real 3D faces because this was the dataset it was trained on, which has its own charm and flare. Try out our Hugging Face Space: As for next steps for us - in order to create realistically looking faces, while still not using a real face dataset, one idea is running the entire FaceSynthetics dataset through Stable Diffusion Image2Imaage, converting the 3D-looking faces into realistically looking ones, and then trainign another ControlNet.And stay tuned, as we will have a ControlNet Training event soon! Follow Hugging Face on Twitter or join our Discord to stay up to date on that.