text
stringlengths
96
319k
id
stringlengths
14
178
metadata
dict
# coding=utf-8 # Copyright 2024 HuggingFace Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import types import unittest from textwrap import dedent import numpy as np import pytest from smolagents.default_tools import BASE_PYTHON_TOOLS from smolagents.local_python_executor import ( InterpreterError, PrintContainer, evaluate_python_code, fix_final_answer_code, get_safe_module, ) # Fake function we will use as tool def add_two(x): return x + 2 class PythonInterpreterTester(unittest.TestCase): def assertDictEqualNoPrint(self, dict1, dict2): return self.assertDictEqual( {k: v for k, v in dict1.items() if k != "_print_outputs"}, {k: v for k, v in dict2.items() if k != "_print_outputs"}, ) def test_evaluate_assign(self): code = "x = 3" state = {} result, _ = evaluate_python_code(code, {}, state=state) assert result == 3 self.assertDictEqualNoPrint(state, {"x": 3, "_operations_count": 2}) code = "x = y" state = {"y": 5} result, _ = evaluate_python_code(code, {}, state=state) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqualNoPrint(state, {"x": 5, "y": 5, "_operations_count": 2}) code = "a=1;b=None" result, _ = evaluate_python_code(code, {}, state={}) # evaluate returns the value of the last assignment. assert result is None def test_assignment_cannot_overwrite_tool(self): code = "print = '3'" with pytest.raises(InterpreterError) as e: evaluate_python_code(code, {"print": print}, state={}) assert "Cannot assign to name 'print': doing this would erase the existing tool!" in str(e) def test_subscript_call(self): code = """def foo(x,y):return x*y\n\ndef boo(y):\n\treturn y**3\nfun = [foo, boo]\nresult_foo = fun[0](4,2)\nresult_boo = fun[1](4)""" state = {} result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state=state) assert result == 64 assert state["result_foo"] == 8 assert state["result_boo"] == 64 def test_evaluate_call(self): code = "y = add_two(x)" state = {"x": 3} result, _ = evaluate_python_code(code, {"add_two": add_two}, state=state) assert result == 5 self.assertDictEqualNoPrint(state, {"x": 3, "y": 5, "_operations_count": 3}) # Should not work without the tool with pytest.raises(InterpreterError) as e: evaluate_python_code(code, {}, state=state) assert "tried to execute add_two" in str(e.value) def test_evaluate_constant(self): code = "x = 3" state = {} result, _ = evaluate_python_code(code, {}, state=state) assert result == 3 self.assertDictEqualNoPrint(state, {"x": 3, "_operations_count": 2}) def test_evaluate_dict(self): code = "test_dict = {'x': x, 'y': add_two(x)}" state = {"x": 3} result, _ = evaluate_python_code(code, {"add_two": add_two}, state=state) self.assertDictEqual(result, {"x": 3, "y": 5}) self.assertDictEqualNoPrint(state, {"x": 3, "test_dict": {"x": 3, "y": 5}, "_operations_count": 7}) def test_evaluate_expression(self): code = "x = 3\ny = 5" state = {} result, _ = evaluate_python_code(code, {}, state=state) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqualNoPrint(state, {"x": 3, "y": 5, "_operations_count": 4}) def test_evaluate_f_string(self): code = "text = f'This is x: {x}.'" state = {"x": 3} result, _ = evaluate_python_code(code, {}, state=state) # evaluate returns the value of the last assignment. assert result == "This is x: 3." self.assertDictEqualNoPrint(state, {"x": 3, "text": "This is x: 3.", "_operations_count": 6}) def test_evaluate_if(self): code = "if x <= 3:\n y = 2\nelse:\n y = 5" state = {"x": 3} result, _ = evaluate_python_code(code, {}, state=state) # evaluate returns the value of the last assignment. assert result == 2 self.assertDictEqualNoPrint(state, {"x": 3, "y": 2, "_operations_count": 6}) state = {"x": 8} result, _ = evaluate_python_code(code, {}, state=state) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqualNoPrint(state, {"x": 8, "y": 5, "_operations_count": 6}) def test_evaluate_list(self): code = "test_list = [x, add_two(x)]" state = {"x": 3} result, _ = evaluate_python_code(code, {"add_two": add_two}, state=state) self.assertListEqual(result, [3, 5]) self.assertDictEqualNoPrint(state, {"x": 3, "test_list": [3, 5], "_operations_count": 5}) def test_evaluate_name(self): code = "y = x" state = {"x": 3} result, _ = evaluate_python_code(code, {}, state=state) assert result == 3 self.assertDictEqualNoPrint(state, {"x": 3, "y": 3, "_operations_count": 2}) def test_evaluate_subscript(self): code = "test_list = [x, add_two(x)]\ntest_list[1]" state = {"x": 3} result, _ = evaluate_python_code(code, {"add_two": add_two}, state=state) assert result == 5 self.assertDictEqualNoPrint(state, {"x": 3, "test_list": [3, 5], "_operations_count": 9}) code = "test_dict = {'x': x, 'y': add_two(x)}\ntest_dict['y']" state = {"x": 3} result, _ = evaluate_python_code(code, {"add_two": add_two}, state=state) assert result == 5 self.assertDictEqualNoPrint(state, {"x": 3, "test_dict": {"x": 3, "y": 5}, "_operations_count": 11}) code = "vendor = {'revenue': 31000, 'rent': 50312}; vendor['ratio'] = round(vendor['revenue'] / vendor['rent'], 2)" state = {} evaluate_python_code(code, {"min": min, "print": print, "round": round}, state=state) assert state["vendor"] == {"revenue": 31000, "rent": 50312, "ratio": 0.62} def test_subscript_string_with_string_index_raises_appropriate_error(self): code = """ search_results = "[{'title': 'Paris, Ville de Paris, France Weather Forecast | AccuWeather', 'href': 'https://www.accuweather.com/en/fr/paris/623/weather-forecast/623', 'body': 'Get the latest weather forecast for Paris, Ville de Paris, France , including hourly, daily, and 10-day outlooks. AccuWeather provides you with reliable and accurate information on temperature ...'}]" for result in search_results: if 'current' in result['title'].lower() or 'temperature' in result['title'].lower(): current_weather_url = result['href'] print(current_weather_url) break""" with pytest.raises(InterpreterError) as e: evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert "You're trying to subscript a string with a string index" in e def test_evaluate_for(self): code = "x = 0\nfor i in range(3):\n x = i" state = {} result, _ = evaluate_python_code(code, {"range": range}, state=state) assert result == 2 self.assertDictEqualNoPrint(state, {"x": 2, "i": 2, "_operations_count": 11}) def test_evaluate_binop(self): code = "y + x" state = {"x": 3, "y": 6} result, _ = evaluate_python_code(code, {}, state=state) assert result == 9 self.assertDictEqualNoPrint(state, {"x": 3, "y": 6, "_operations_count": 4}) def test_recursive_function(self): code = """ def recur_fibo(n): if n <= 1: return n else: return(recur_fibo(n-1) + recur_fibo(n-2)) recur_fibo(6)""" result, _ = evaluate_python_code(code, {}, state={}) assert result == 8 def test_evaluate_string_methods(self): code = "'hello'.replace('h', 'o').split('e')" result, _ = evaluate_python_code(code, {}, state={}) assert result == ["o", "llo"] def test_evaluate_slicing(self): code = "'hello'[1:3][::-1]" result, _ = evaluate_python_code(code, {}, state={}) assert result == "le" def test_access_attributes(self): code = "integer = 1\nobj_class = integer.__class__\nobj_class" result, _ = evaluate_python_code(code, {}, state={}) assert result is int def test_list_comprehension(self): code = "sentence = 'THESEAGULL43'\nmeaningful_sentence = '-'.join([char.lower() for char in sentence if char.isalpha()])" result, _ = evaluate_python_code(code, {}, state={}) assert result == "t-h-e-s-e-a-g-u-l-l" def test_string_indexing(self): code = """text_block = [ "THESE", "AGULL" ] sentence = "" for block in text_block: for col in range(len(text_block[0])): sentence += block[col] """ result, _ = evaluate_python_code(code, {"len": len, "range": range}, state={}) assert result == "THESEAGULL" def test_tuples(self): code = "x = (1, 2, 3)\nx[1]" result, _ = evaluate_python_code(code, {}, state={}) assert result == 2 code = """ digits, i = [1, 2, 3], 1 digits[i], digits[i + 1] = digits[i + 1], digits[i]""" evaluate_python_code(code, {"range": range, "print": print, "int": int}, {}) code = """ def calculate_isbn_10_check_digit(number): total = sum((10 - i) * int(digit) for i, digit in enumerate(number)) remainder = total % 11 check_digit = 11 - remainder if check_digit == 10: return 'X' elif check_digit == 11: return '0' else: return str(check_digit) # Given 9-digit numbers numbers = [ "478225952", "643485613", "739394228", "291726859", "875262394", "542617795", "031810713", "957007669", "871467426" ] # Calculate check digits for each number check_digits = [calculate_isbn_10_check_digit(number) for number in numbers] print(check_digits) """ state = {} evaluate_python_code( code, { "range": range, "print": print, "sum": sum, "enumerate": enumerate, "int": int, "str": str, }, state, ) def test_listcomp(self): code = "x = [i for i in range(3)]" result, _ = evaluate_python_code(code, {"range": range}, state={}) assert result == [0, 1, 2] def test_break_continue(self): code = "for i in range(10):\n if i == 5:\n break\ni" result, _ = evaluate_python_code(code, {"range": range}, state={}) assert result == 5 code = "for i in range(10):\n if i == 5:\n continue\ni" result, _ = evaluate_python_code(code, {"range": range}, state={}) assert result == 9 def test_call_int(self): code = "import math\nstr(math.ceil(149))" result, _ = evaluate_python_code(code, {"str": lambda x: str(x)}, state={}) assert result == "149" def test_lambda(self): code = "f = lambda x: x + 2\nf(3)" result, _ = evaluate_python_code(code, {}, state={}) assert result == 5 def test_dictcomp(self): code = "x = {i: i**2 for i in range(3)}" result, _ = evaluate_python_code(code, {"range": range}, state={}) assert result == {0: 0, 1: 1, 2: 4} code = "{num: name for num, name in {101: 'a', 102: 'b'}.items() if name not in ['a']}" result, _ = evaluate_python_code(code, {"print": print}, state={}, authorized_imports=["pandas"]) assert result == {102: "b"} code = """ shifts = {'A': ('6:45', '8:00'), 'B': ('10:00', '11:45')} shift_minutes = {worker: ('a', 'b') for worker, (start, end) in shifts.items()} """ result, _ = evaluate_python_code(code, {}, state={}) assert result == {"A": ("a", "b"), "B": ("a", "b")} def test_tuple_assignment(self): code = "a, b = 0, 1\nb" result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert result == 1 def test_while(self): code = "i = 0\nwhile i < 3:\n i += 1\ni" result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert result == 3 # test infinite loop code = "i = 0\nwhile i < 3:\n i -= 1\ni" with pytest.raises(InterpreterError) as e: evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert "iterations in While loop exceeded" in str(e) # test lazy evaluation code = """ house_positions = [0, 7, 10, 15, 18, 22, 22] i, n, loc = 0, 7, 30 while i < n and house_positions[i] <= loc: i += 1 """ state = {} evaluate_python_code(code, BASE_PYTHON_TOOLS, state=state) def test_generator(self): code = "a = [1, 2, 3, 4, 5]; b = (i**2 for i in a); list(b)" result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert result == [1, 4, 9, 16, 25] def test_boolops(self): code = """if (not (a > b and a > c)) or d > e: best_city = "Brooklyn" else: best_city = "Manhattan" best_city """ result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}) assert result == "Brooklyn" code = """if d > e and a < b: best_city = "Brooklyn" elif d < e and a < b: best_city = "Sacramento" else: best_city = "Manhattan" best_city """ result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}) assert result == "Sacramento" def test_if_conditions(self): code = """char='a' if char.isalpha(): print('2')""" state = {} evaluate_python_code(code, BASE_PYTHON_TOOLS, state=state) assert state["_print_outputs"].value == "2\n" def test_imports(self): code = "import math\nmath.sqrt(4)" result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert result == 2.0 code = "from random import choice, seed\nseed(12)\nchoice(['win', 'lose', 'draw'])" result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert result == "lose" code = "import time, re\ntime.sleep(0.1)" result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert result is None code = "from queue import Queue\nq = Queue()\nq.put(1)\nq.get()" result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert result == 1 code = "import itertools\nlist(itertools.islice(range(10), 3))" result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert result == [0, 1, 2] code = "import re\nre.search('a', 'abc').group()" result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert result == "a" code = "import stat\nstat.S_ISREG(0o100644)" result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert result code = "import statistics\nstatistics.mean([1, 2, 3, 4, 4])" result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert result == 2.8 code = "import unicodedata\nunicodedata.name('A')" result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert result == "LATIN CAPITAL LETTER A" # Test submodules are handled properly, thus not raising error code = "import numpy.random as rd\nrng = rd.default_rng(12345)\nrng.random()" result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}, authorized_imports=["numpy"]) code = "from numpy.random import default_rng as d_rng\nrng = d_rng(12345)\nrng.random()" result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}, authorized_imports=["numpy"]) def test_additional_imports(self): code = "import numpy as np" evaluate_python_code(code, authorized_imports=["numpy"], state={}) code = "import numpy.random as rd" evaluate_python_code(code, authorized_imports=["numpy.random"], state={}) evaluate_python_code(code, authorized_imports=["numpy"], state={}) evaluate_python_code(code, authorized_imports=["*"], state={}) with pytest.raises(InterpreterError): evaluate_python_code(code, authorized_imports=["random"], state={}) def test_multiple_comparators(self): code = "0 <= -1 < 4 and 0 <= -5 < 4" result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert not result code = "0 <= 1 < 4 and 0 <= -5 < 4" result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert not result code = "0 <= 4 < 4 and 0 <= 3 < 4" result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert not result code = "0 <= 3 < 4 and 0 <= 3 < 4" result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert result def test_print_output(self): code = "print('Hello world!')\nprint('Ok no one cares')" state = {} result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state=state) assert result is None assert state["_print_outputs"].value == "Hello world!\nOk no one cares\n" # Test print in function (state copy) code = """ print("1") def function(): print("2") function()""" state = {} evaluate_python_code(code, {"print": print}, state=state) assert state["_print_outputs"].value == "1\n2\n" # Test print in list comprehension (state copy) code = """ print("1") def function(): print("2") [function() for i in range(10)]""" state = {} evaluate_python_code(code, {"print": print, "range": range}, state=state) assert state["_print_outputs"].value == "1\n2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n" def test_tuple_target_in_iterator(self): code = "for a, b in [('Ralf Weikert', 'Austria'), ('Samuel Seungwon Lee', 'South Korea')]:res = a.split()[0]" result, _ = evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert result == "Samuel" def test_classes(self): code = """ class Animal: species = "Generic Animal" def __init__(self, name, age): self.name = name self.age = age def sound(self): return "The animal makes a sound." def __str__(self): return f"{self.name}, {self.age} years old" class Dog(Animal): species = "Canine" def __init__(self, name, age, breed): super().__init__(name, age) self.breed = breed def sound(self): return "The dog barks." def __str__(self): return f"{self.name}, {self.age} years old, {self.breed}" class Cat(Animal): def sound(self): return "The cat meows." def __str__(self): return f"{self.name}, {self.age} years old, {self.species}" # Testing multiple instances dog1 = Dog("Fido", 3, "Labrador") dog2 = Dog("Buddy", 5, "Golden Retriever") # Testing method with built-in function animals = [dog1, dog2, Cat("Whiskers", 2)] num_animals = len(animals) # Testing exceptions in methods class ExceptionTest: def method_that_raises(self): raise ValueError("An error occurred") try: exc_test = ExceptionTest() exc_test.method_that_raises() except ValueError as e: exception_message = str(e) # Collecting results dog1_sound = dog1.sound() dog1_str = str(dog1) dog2_sound = dog2.sound() dog2_str = str(dog2) cat = Cat("Whiskers", 2) cat_sound = cat.sound() cat_str = str(cat) """ state = {} evaluate_python_code( code, {"print": print, "len": len, "super": super, "str": str, "sum": sum}, state=state, ) # Assert results assert state["dog1_sound"] == "The dog barks." assert state["dog1_str"] == "Fido, 3 years old, Labrador" assert state["dog2_sound"] == "The dog barks." assert state["dog2_str"] == "Buddy, 5 years old, Golden Retriever" assert state["cat_sound"] == "The cat meows." assert state["cat_str"] == "Whiskers, 2 years old, Generic Animal" assert state["num_animals"] == 3 assert state["exception_message"] == "An error occurred" def test_variable_args(self): code = """ def var_args_method(self, *args, **kwargs): return sum(args) + sum(kwargs.values()) var_args_method(1, 2, 3, x=4, y=5) """ state = {} result, _ = evaluate_python_code(code, {"sum": sum}, state=state) assert result == 15 def test_exceptions(self): code = """ def method_that_raises(self): raise ValueError("An error occurred") try: method_that_raises() except ValueError as e: exception_message = str(e) """ state = {} evaluate_python_code( code, {"print": print, "len": len, "super": super, "str": str, "sum": sum}, state=state, ) assert state["exception_message"] == "An error occurred" def test_print(self): code = "print(min([1, 2, 3]))" state = {} evaluate_python_code(code, {"min": min, "print": print}, state=state) assert state["_print_outputs"].value == "1\n" def test_types_as_objects(self): code = "type_a = float(2); type_b = str; type_c = int" state = {} result, is_final_answer = evaluate_python_code(code, {"float": float, "str": str, "int": int}, state=state) assert result is int def test_tuple_id(self): code = """ food_items = {"apple": 2, "banana": 3, "orange": 1, "pear": 1} unique_food_items = [item for item, count in food_item_counts.items() if count == 1] """ state = {} result, is_final_answer = evaluate_python_code(code, {}, state=state) assert result == ["orange", "pear"] def test_nonsimple_augassign(self): code = """ counts_dict = {'a': 0} counts_dict['a'] += 1 counts_list = [1, 2, 3] counts_list += [4, 5, 6] class Counter: self.count = 0 a = Counter() a.count += 1 """ state = {} evaluate_python_code(code, {}, state=state) assert state["counts_dict"] == {"a": 1} assert state["counts_list"] == [1, 2, 3, 4, 5, 6] assert state["a"].count == 1 def test_adding_int_to_list_raises_error(self): code = """ counts = [1, 2, 3] counts += 1""" with pytest.raises(InterpreterError) as e: evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert "Cannot add non-list value 1 to a list." in str(e) def test_error_highlights_correct_line_of_code(self): code = """a = 1 b = 2 counts = [1, 2, 3] counts += 1 b += 1""" with pytest.raises(InterpreterError) as e: evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert "Code execution failed at line 'counts += 1" in str(e) def test_error_type_returned_in_function_call(self): code = """def error_function(): raise ValueError("error") error_function()""" with pytest.raises(InterpreterError) as e: evaluate_python_code(code) assert "error" in str(e) assert "ValueError" in str(e) def test_assert(self): code = """ assert 1 == 1 assert 1 == 2 """ with pytest.raises(InterpreterError) as e: evaluate_python_code(code, BASE_PYTHON_TOOLS, state={}) assert "1 == 2" in str(e) and "1 == 1" not in str(e) def test_with_context_manager(self): code = """ class SimpleLock: def __init__(self): self.locked = False def __enter__(self): self.locked = True return self def __exit__(self, exc_type, exc_value, traceback): self.locked = False lock = SimpleLock() with lock as l: assert l.locked == True assert lock.locked == False """ state = {} tools = {} evaluate_python_code(code, tools, state=state) def test_default_arg_in_function(self): code = """ def f(a, b=333, n=1000): return b + n n = f(1, n=667) """ res, is_final_answer = evaluate_python_code(code, {}, {}) assert res == 1000 assert not is_final_answer def test_set(self): code = """ S1 = {'a', 'b', 'c'} S2 = {'b', 'c', 'd'} S3 = S1.difference(S2) S4 = S1.intersection(S2) """ state = {} evaluate_python_code(code, {}, state=state) assert state["S3"] == {"a"} assert state["S4"] == {"b", "c"} def test_break(self): code = """ i = 0 while True: i+= 1 if i==3: break i""" result, is_final_answer = evaluate_python_code(code, {"print": print, "round": round}, state={}) assert result == 3 assert not is_final_answer def test_return(self): # test early returns code = """ def add_one(n, shift): if True: return n + shift return n add_one(1, 1) """ state = {} result, is_final_answer = evaluate_python_code( code, {"print": print, "range": range, "ord": ord, "chr": chr}, state=state ) assert result == 2 # test returning None code = """ def returns_none(a): return returns_none(1) """ state = {} result, is_final_answer = evaluate_python_code( code, {"print": print, "range": range, "ord": ord, "chr": chr}, state=state ) assert result is None def test_nested_for_loop(self): code = """ all_res = [] for i in range(10): subres = [] for j in range(i): subres.append(j) all_res.append(subres) out = [i for sublist in all_res for i in sublist] out[:10] """ state = {} result, is_final_answer = evaluate_python_code(code, {"print": print, "range": range}, state=state) assert result == [0, 0, 1, 0, 1, 2, 0, 1, 2, 3] def test_pandas(self): code = """ import pandas as pd df = pd.DataFrame.from_dict({'SetCount': ['5', '4', '5'], 'Quantity': [1, 0, -1]}) df['SetCount'] = pd.to_numeric(df['SetCount'], errors='coerce') parts_with_5_set_count = df[df['SetCount'] == 5.0] parts_with_5_set_count[['Quantity', 'SetCount']].values[1] """ state = {} result, _ = evaluate_python_code(code, {}, state=state, authorized_imports=["pandas"]) assert np.array_equal(result, [-1, 5]) code = """ import pandas as pd df = pd.DataFrame.from_dict({"AtomicNumber": [111, 104, 105], "ok": [0, 1, 2]}) # Filter the DataFrame to get only the rows with outdated atomic numbers filtered_df = df.loc[df['AtomicNumber'].isin([104])] """ result, _ = evaluate_python_code(code, {"print": print}, state={}, authorized_imports=["pandas"]) assert np.array_equal(result.values[0], [104, 1]) # Test groupby code = """import pandas as pd data = pd.DataFrame.from_dict([ {"Pclass": 1, "Survived": 1}, {"Pclass": 2, "Survived": 0}, {"Pclass": 2, "Survived": 1} ]) survival_rate_by_class = data.groupby('Pclass')['Survived'].mean() """ result, _ = evaluate_python_code(code, {}, state={}, authorized_imports=["pandas"]) assert result.values[1] == 0.5 # Test loc and iloc code = """import pandas as pd data = pd.DataFrame.from_dict([ {"Pclass": 1, "Survived": 1}, {"Pclass": 2, "Survived": 0}, {"Pclass": 2, "Survived": 1} ]) survival_rate_biased = data.loc[data['Survived']==1]['Survived'].mean() survival_rate_biased = data.loc[data['Survived']==1]['Survived'].mean() survival_rate_sorted = data.sort_values(by='Survived', ascending=False).iloc[0] """ result, _ = evaluate_python_code(code, {}, state={}, authorized_imports=["pandas"]) def test_starred(self): code = """ from math import radians, sin, cos, sqrt, atan2 def haversine(lat1, lon1, lat2, lon2): R = 6371000 # Radius of the Earth in meters lat1, lon1, lat2, lon2 = map(radians, [lat1, lon1, lat2, lon2]) dlat = lat2 - lat1 dlon = lon2 - lon1 a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2 c = 2 * atan2(sqrt(a), sqrt(1 - a)) distance = R * c return distance coords_geneva = (46.1978, 6.1342) coords_barcelona = (41.3869, 2.1660) distance_geneva_barcelona = haversine(*coords_geneva, *coords_barcelona) """ result, _ = evaluate_python_code(code, {"print": print, "map": map}, state={}, authorized_imports=["math"]) assert round(result, 1) == 622395.4 def test_for(self): code = """ shifts = { "Worker A": ("6:45 pm", "8:00 pm"), "Worker B": ("10:00 am", "11:45 am") } shift_intervals = {} for worker, (start, end) in shifts.items(): shift_intervals[worker] = end shift_intervals """ result, _ = evaluate_python_code(code, {"print": print, "map": map}, state={}) assert result == {"Worker A": "8:00 pm", "Worker B": "11:45 am"} def test_syntax_error_points_error(self): code = "a = ;" with pytest.raises(InterpreterError) as e: evaluate_python_code(code) assert "SyntaxError" in str(e) assert " ^" in str(e) def test_fix_final_answer_code(self): test_cases = [ ( "final_answer = 3.21\nfinal_answer(final_answer)", "final_answer_variable = 3.21\nfinal_answer(final_answer_variable)", ), ( "x = final_answer(5)\nfinal_answer = x + 1\nfinal_answer(final_answer)", "x = final_answer(5)\nfinal_answer_variable = x + 1\nfinal_answer(final_answer_variable)", ), ( "def func():\n final_answer = 42\n return final_answer(final_answer)", "def func():\n final_answer_variable = 42\n return final_answer(final_answer_variable)", ), ( "final_answer(5) # Should not change function calls", "final_answer(5) # Should not change function calls", ), ( "obj.final_answer = 5 # Should not change object attributes", "obj.final_answer = 5 # Should not change object attributes", ), ( "final_answer=3.21;final_answer(final_answer)", "final_answer_variable=3.21;final_answer(final_answer_variable)", ), ] for i, (input_code, expected) in enumerate(test_cases, 1): result = fix_final_answer_code(input_code) assert result == expected, f""" Test case {i} failed: Input: {input_code} Expected: {expected} Got: {result} """ def test_dangerous_subpackage_access_blocked(self): # Direct imports with dangerous patterns should fail code = "import random._os" with pytest.raises(InterpreterError): evaluate_python_code(code) # Import of whitelisted modules should succeed but dangerous submodules should not exist code = "import random;random._os.system('echo bad command passed')" with pytest.raises(InterpreterError) as e: evaluate_python_code(code) assert "AttributeError:module 'random' has no attribute '_os'" in str(e) code = "import doctest;doctest.inspect.os.system('echo bad command passed')" with pytest.raises(InterpreterError): evaluate_python_code(code, authorized_imports=["doctest"]) def test_close_matches_subscript(self): code = 'capitals = {"Czech Republic": "Prague", "Monaco": "Monaco", "Bhutan": "Thimphu"};capitals["Butan"]' with pytest.raises(Exception) as e: evaluate_python_code(code) assert "Maybe you meant one of these indexes instead" in str(e) and "['Bhutan']" in str(e).replace("\\", "") def test_dangerous_builtins_calls_are_blocked(self): unsafe_code = "import os" dangerous_code = f""" exec = callable.__self__.exec compile = callable.__self__.compile exec(compile('{unsafe_code}', 'no filename', 'exec')) """ with pytest.raises(InterpreterError): evaluate_python_code(unsafe_code, static_tools=BASE_PYTHON_TOOLS) with pytest.raises(InterpreterError): evaluate_python_code(dangerous_code, static_tools=BASE_PYTHON_TOOLS) def test_dangerous_builtins_are_callable_if_explicitly_added(self): dangerous_code = """ compile = callable.__self__.compile eval = callable.__self__.eval exec = callable.__self__.exec eval("1 + 1") exec(compile("1 + 1", "no filename", "exec")) teval("1 + 1") texec(tcompile("1 + 1", "no filename", "exec")) """ evaluate_python_code( dangerous_code, static_tools={"tcompile": compile, "teval": eval, "texec": exec} | BASE_PYTHON_TOOLS ) def test_can_import_os_if_explicitly_authorized(self): dangerous_code = "import os; os.listdir('./')" evaluate_python_code(dangerous_code, authorized_imports=["os"]) @pytest.mark.parametrize( "code, expected_result", [ ( dedent("""\ x = 1 x += 2 """), 3, ), ( dedent("""\ x = "a" x += "b" """), "ab", ), ( dedent("""\ class Custom: def __init__(self, value): self.value = value def __iadd__(self, other): self.value += other * 10 return self x = Custom(1) x += 2 x.value """), 21, ), ], ) def test_evaluate_augassign(code, expected_result): state = {} result, _ = evaluate_python_code(code, {}, state=state) assert result == expected_result @pytest.mark.parametrize( "operator, expected_result", [ ("+=", 7), ("-=", 3), ("*=", 10), ("/=", 2.5), ("//=", 2), ("%=", 1), ("**=", 25), ("&=", 0), ("|=", 7), ("^=", 7), (">>=", 1), ("<<=", 20), ], ) def test_evaluate_augassign_number(operator, expected_result): code = dedent("""\ x = 5 x {operator} 2 """).format(operator=operator) state = {} result, _ = evaluate_python_code(code, {}, state=state) assert result == expected_result @pytest.mark.parametrize( "operator, expected_result", [ ("+=", 7), ("-=", 3), ("*=", 10), ("/=", 2.5), ("//=", 2), ("%=", 1), ("**=", 25), ("&=", 0), ("|=", 7), ("^=", 7), (">>=", 1), ("<<=", 20), ], ) def test_evaluate_augassign_custom(operator, expected_result): operator_names = { "+=": "iadd", "-=": "isub", "*=": "imul", "/=": "itruediv", "//=": "ifloordiv", "%=": "imod", "**=": "ipow", "&=": "iand", "|=": "ior", "^=": "ixor", ">>=": "irshift", "<<=": "ilshift", } code = dedent("""\ class Custom: def __init__(self, value): self.value = value def __{operator_name}__(self, other): self.value {operator} other return self x = Custom(5) x {operator} 2 x.value """).format(operator=operator, operator_name=operator_names[operator]) state = {} result, _ = evaluate_python_code(code, {}, state=state) assert result == expected_result def test_get_safe_module_handle_lazy_imports(): class FakeModule(types.ModuleType): def __init__(self, name): super().__init__(name) self.non_lazy_attribute = "ok" def __getattr__(self, name): if name == "lazy_attribute": raise ImportError("lazy import failure") return super().__getattr__(name) def __dir__(self): return super().__dir__() + ["lazy_attribute"] fake_module = FakeModule("fake_module") safe_module = get_safe_module(fake_module, dangerous_patterns=[], authorized_imports=set()) assert not hasattr(safe_module, "lazy_attribute") assert getattr(safe_module, "non_lazy_attribute") == "ok" class TestPrintContainer: def test_initial_value(self): pc = PrintContainer() assert pc.value == "" def test_append(self): pc = PrintContainer() pc.append("Hello") assert pc.value == "Hello" def test_iadd(self): pc = PrintContainer() pc += "World" assert pc.value == "World" def test_str(self): pc = PrintContainer() pc.append("Hello") assert str(pc) == "Hello" def test_repr(self): pc = PrintContainer() pc.append("Hello") assert repr(pc) == "PrintContainer(Hello)" def test_len(self): pc = PrintContainer() pc.append("Hello") assert len(pc) == 5
smolagents/tests/test_local_python_executor.py/0
{ "file_path": "smolagents/tests/test_local_python_executor.py", "repo_id": "smolagents", "token_count": 17042 }
# Build the image and get out the docker file: # # docker build -t tgi-nix-builder -f Dockerfile.nix # docker run --log-driver=none tgi-nix-builder | docker load FROM nixos/nix:2.18.8 AS builder RUN echo "experimental-features = nix-command flakes" >> /etc/nix/nix.conf RUN nix profile install nixpkgs#cachix RUN cachix use text-generation-inference WORKDIR /root ADD . . RUN nix build . RUN mkdir /tmp/nix-store-closure RUN cp -R $(nix-store -qR result/) /tmp/nix-store-closure FROM ubuntu:24.04 WORKDIR /app # Copy /nix/store COPY --from=builder /tmp/nix-store-closure /nix/store COPY --from=builder /root/result /app RUN ldconfig CMD ["ldconfig", "/app/bin/text-generation-launcher"]
text-generation-inference/Dockerfile.nix/0
{ "file_path": "text-generation-inference/Dockerfile.nix", "repo_id": "text-generation-inference", "token_count": 268 }
/// Multi shard Client use crate::{v2, Health, ShardInfo}; use crate::{ClientError, Result}; use crate::v2::InfoResponse; use async_trait::async_trait; use futures::future::join_all; use tonic::transport::Uri; use tracing::instrument; use v2::client::{DecodeTimings, PrefillTimings}; use v2::{ Batch, CachedBatch, Client, Generation, GrammarType, HealthResponse, NextTokenChooserParameters, Request, StoppingCriteriaParameters, }; #[derive(Debug, Clone)] /// Text Generation Inference gRPC multi client pub struct ShardedClient { clients: Vec<Client>, } impl ShardedClient { fn new(clients: Vec<Client>) -> Self { Self { clients } } /// Create a new ShardedClient from a master client. The master client will communicate with /// the other shards and returns all uris/unix sockets with the `service_discovery` gRPC method. async fn from_master_client(mut master_client: Client) -> Result<Self> { // Get all uris/unix sockets from the master client let uris = master_client.service_discovery().await?; let futures = uris.into_iter().map(Client::connect_uds); let clients: Result<Vec<Client>> = join_all(futures).await.into_iter().collect(); Ok(Self::new(clients?)) } /// Returns a client connected to the given uri pub async fn connect(uri: Uri) -> Result<Self> { let master_client = Client::connect(uri).await?; Self::from_master_client(master_client).await } /// Returns a client connected to the given unix socket pub async fn connect_uds(path: String) -> Result<Self> { let master_client = Client::connect_uds(path).await?; Self::from_master_client(master_client).await } /// Get the model info #[instrument(skip(self))] pub async fn info(&mut self) -> Result<ShardInfo> { let futures: Vec<_> = self .clients .iter_mut() .map(|client| client.info()) .collect(); join_all(futures).await.pop().unwrap().map(ShardInfo::from) } /// GRPC health check #[instrument(skip(self))] pub async fn health(&mut self) -> Result<HealthResponse> { let futures: Vec<_> = self .clients .iter_mut() .map(|client| client.health()) .collect(); join_all(futures).await.pop().unwrap() } /// Clear the past generations cache #[instrument(skip(self))] pub async fn clear_cache(&mut self, batch_id: Option<u64>) -> Result<()> { let futures: Vec<_> = self .clients .iter_mut() .map(|client| client.clear_cache(batch_id)) .collect(); join_all(futures).await.into_iter().collect() } /// Filter a cached batch #[instrument(skip(self))] pub async fn filter_batch( &mut self, batch_id: u64, request_ids: Vec<u64>, ) -> Result<Option<CachedBatch>> { let futures: Vec<_> = self .clients .iter_mut() .map(|client| Box::pin(client.filter_batch(batch_id, request_ids.clone()))) .collect(); // all shards return the same message join_all(futures).await.pop().unwrap() } /// Warmup on a max size batch /// /// Returns the maximum amount of tokens supported by the hardware #[instrument(skip(self))] pub async fn warmup( &mut self, max_input_length: u32, max_prefill_tokens: u32, max_total_tokens: u32, max_batch_size: Option<usize>, ) -> Result<Option<u32>> { let futures: Vec<_> = self .clients .iter_mut() .map(|client| { Box::pin(client.warmup( max_input_length, max_prefill_tokens, max_total_tokens, max_batch_size, )) }) .collect(); // Take the minimum value let results = join_all(futures) .await .into_iter() .collect::<Result<Vec<Option<u32>>>>()?; Ok(results.into_iter().flatten().min()) } /// Generate one token for each request in the given batch /// /// Returns Generation for each request in batch /// and the next cached batch #[instrument(skip_all, fields(id = & batch.id, size = & batch.size))] pub async fn prefill( &mut self, batch: Batch, ) -> Result<(Vec<Generation>, Option<CachedBatch>, PrefillTimings)> { let futures: Vec<_> = self .clients .iter_mut() .map(|client| Box::pin(client.prefill(batch.clone()))) .collect(); #[allow(clippy::type_complexity)] let results: Result<Vec<(Vec<Generation>, Option<CachedBatch>, PrefillTimings)>> = join_all(futures).await.into_iter().collect(); let mut results = results?; let (mut generations, next_batch, mut timings) = results.pop().ok_or(ClientError::EmptyResults)?; // Merge generations from different model shards for (mut shard_generations, _, shard_timings) in results.into_iter() { generations.append(&mut shard_generations); // Return the timings of the slowest shard if shard_timings.total > timings.total { timings = shard_timings; } } Ok((generations, next_batch, timings)) } /// Generate one token for each request in the given cached batches /// /// Returns Generation for each request in batches /// and the next cached batch #[instrument(skip_all, fields(size = batches.iter().map(| batch | {batch.size}).sum::< u32 > ()))] pub async fn decode( &mut self, batches: Vec<CachedBatch>, ) -> Result<(Vec<Generation>, Option<CachedBatch>, DecodeTimings)> { let futures: Vec<_> = self .clients .iter_mut() .map(|client| Box::pin(client.decode(batches.clone()))) .collect(); #[allow(clippy::type_complexity)] let results: Result<Vec<(Vec<Generation>, Option<CachedBatch>, DecodeTimings)>> = join_all(futures).await.into_iter().collect(); let mut results = results?; let (mut generations, next_batch, mut timings) = results.pop().ok_or(ClientError::EmptyResults)?; // Merge generations from different model shards for (mut shard_generations, _, shard_timings) in results.into_iter() { generations.append(&mut shard_generations); // Return the timings of the slowest shard if shard_timings.total > timings.total { timings = shard_timings; } } Ok((generations, next_batch, timings)) } } impl From<InfoResponse> for ShardInfo { fn from(value: InfoResponse) -> Self { Self { requires_padding: value.requires_padding, dtype: value.dtype, device_type: value.device_type, window_size: value.window_size, speculate: value.speculate, } } } #[async_trait] impl Health for ShardedClient { async fn device_health(&self) -> Result<()> { self.clone().health().await?; Ok(()) } async fn model_health(&self) -> Result<()> { // Dummy batch of 1 token and 1 generated token let liveness_request = Request { id: u64::MAX, inputs: "liveness".to_string(), truncate: 10, prefill_logprobs: false, parameters: Some(NextTokenChooserParameters { temperature: 1.0, top_k: 0, top_p: 1.0, typical_p: 1.0, do_sample: false, seed: 0, repetition_penalty: 1.0, frequency_penalty: 0.0, watermark: false, grammar: String::new(), grammar_type: GrammarType::None as i32, }), stopping_parameters: Some(StoppingCriteriaParameters { max_new_tokens: 1, stop_sequences: vec![], ignore_eos_token: false, }), top_n_tokens: 0, }; let batch = Batch { id: u64::MAX, requests: vec![liveness_request], size: 1, max_tokens: 2, }; self.clone().prefill(batch).await?; Ok(()) } }
text-generation-inference/backends/client/src/v2/sharded_client.rs/0
{ "file_path": "text-generation-inference/backends/client/src/v2/sharded_client.rs", "repo_id": "text-generation-inference", "token_count": 3969 }
#ifndef TGI_BACKEND_TRTLLM_FFI #define TGI_BACKEND_TRTLLM_FFI #include <memory> #include <thread> #include <nvml.h> #include <tensorrt_llm/common/tllmException.h> #include <tensorrt_llm/plugins/api/tllmPlugin.h> #include <spdlog/spdlog.h> #include <backend.hpp> #include <hardware.hpp> namespace rust::behavior { template<typename Try, typename Fail> static void trycatch(Try &&func, Fail &&fail) noexcept try { func(); } catch (tensorrt_llm::common::TllmException &e) { fail(e.what()); } } namespace huggingface::tgi::backends::trtllm { class tensorrt_llm_backend_t; } #include "backends/trtllm/src/lib.rs.h" namespace huggingface::tgi::backends::trtllm { std::once_flag backend_initialized_flag; constexpr finish_reason_t as_finish_reason_t(const tle::FinishReason reason) noexcept { switch (reason) { case tle::FinishReason::kNOT_FINISHED: return finish_reason_t::kNOT_FINISHED; case tle::FinishReason::kSTOP_WORDS: return finish_reason_t::kSTOP_WORDS; case tle::FinishReason::kEND_ID: return finish_reason_t::kEND_ID; case tle::FinishReason::kLENGTH: return finish_reason_t::kLENGTH; default: std::unreachable(); } } static auto as_generation_step = [](const tle::Response &r) { const auto reqId = r.getRequestId(); if (!r.hasError()) [[likely]] { const auto result = r.getResult(); const auto logits = result.logProbs.value()[0]; return generation_step_t{ reqId, static_cast<uint32_t>(result.outputTokenIds[0][0]), logits.back(), result.isFinal, as_finish_reason_t(result.finishReasons[0]), false, std::string() }; } else { return generation_step_t{ reqId, 0, 0.0, true, finish_reason_t::kNOT_FINISHED, true, std::move(r.getErrorMsg()) }; } }; class tensorrt_llm_backend_t { private: backend_t inner_; public: tensorrt_llm_backend_t(std::filesystem::path &&engine_folder, std::filesystem::path &&executor_worker_path) : inner_(engine_folder, executor_worker_path) {} size_t num_tokens_ready() const noexcept { return inner_.num_tokens_ready(); } request_id_t submit( rust::Slice<const uint32_t> tokens, uint32_t max_new_tokens, uint32_t top_k, float_t top_p, float_t temperature, float_t repetition_penalty, float_t frequency_penalty, uint64_t seed ) { // This is enabled only if using add_compile_definitions(SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE) SPDLOG_TRACE(FMT_STRING("[FFI] Submitting {:d} prompt tokens to the executor")); // Submit the request to the executor and get back a potential request_id used to track request status const auto signed_tokens = std::vector<int32_t>(tokens.begin(), tokens.end()); const auto maybe_request_id = inner_.submit( signed_tokens, {max_new_tokens}, {top_k, top_p, repetition_penalty, frequency_penalty, temperature, seed} ); // If we do have a value, let's return the request_id if (maybe_request_id.has_value()) [[likely]] { return *maybe_request_id; } else { SPDLOG_WARN("[FFI] Failed to submit request to the executor"); return maybe_request_id.error(); } } std::unique_ptr<std::vector<generation_step_t>> pull_tokens() noexcept { if (num_tokens_ready() > 0) [[likely]] { const auto responses = inner_.pull_tokens(); SPDLOG_TRACE("[FFI] Successfully pulled out {:d} responses from executor", responses.size()); // Transform tle::Response to generation_step_t #ifdef __cpp_lib_ranges_to_container auto steps = responses | std::views::transform(as_generation_step) | std::ranges::to<std::vector>(); #else auto steps = std::vector<generation_step_t>(); steps.reserve(responses.size()); std::transform(responses.begin(), responses.end(), std::back_inserter(steps), as_generation_step); #endif return std::make_unique<std::vector<generation_step_t>>(steps); } else { return std::make_unique<std::vector<generation_step_t>>(); } } void cancel(request_id_t request_id) noexcept { SPDLOG_DEBUG("[FFI] cancelling request {:d}", request_id); inner_.cancel(request_id); } }; void initialize_logging() { #ifndef TGI_TRTLLM_BACKEND_DEBUG if (const auto TRTLLM_LOG_LEVEL_CSTR = std::getenv("TRTLLM_LOG_LEVEL")) { std::string log_level(TRTLLM_LOG_LEVEL_CSTR); std::transform(log_level.begin(), log_level.end(), log_level.begin(), [](unsigned char c) { return std::tolower(c); }); if (log_level == "debug") spdlog::set_level(spdlog::level::debug); else spdlog::set_level(spdlog::level::info); } #else spdlog::set_level(spdlog::level::debug); #endif } void initialize_tensorrt_llm_backend() { SPDLOG_INFO("Initializing TGI - TensoRT-LLM Backend (v{})", tle::version()); // Initialize everyone initialize_logging(); nvmlInit_v2(); initTrtLlmPlugins(); const auto numGpus = huggingface::tgi::hardware::cuda::get_device_count(); if (numGpus.has_value()) { SPDLOG_INFO("[FFI] Detected {:d} Nvidia GPU(s)", *numGpus); } else { SPDLOG_WARN("[FFI] Failed to detected Nvidia GPU(s) on the system"); // todo: throw } } std::unique_ptr<tensorrt_llm_backend_t> create_backend_from_engine_folder(const rust::Str engines_folder, const rust::Str executor_worker_path) { std::call_once(backend_initialized_flag, initialize_tensorrt_llm_backend); return std::make_unique<tensorrt_llm_backend_t>( std::filesystem::path(std::string_view(engines_folder.begin(), engines_folder.end()), std::filesystem::path::format::auto_format), std::filesystem::path(std::string_view(executor_worker_path.begin(), executor_worker_path.end()), std::filesystem::path::format::auto_format) ); } } #endif
text-generation-inference/backends/trtllm/csrc/ffi.hpp/0
{ "file_path": "text-generation-inference/backends/trtllm/csrc/ffi.hpp", "repo_id": "text-generation-inference", "token_count": 3501 }
[package] name = "text-generation-benchmark" description = "Text Generation Benchmarking tool" version.workspace = true edition.workspace = true authors.workspace = true homepage.workspace = true [lib] path = "src/lib.rs" [[bin]] name = "text-generation-benchmark" path = "src/main.rs" [dependencies] average = "0.14" clap = { version = "4.4.5", features = ["derive", "env"] } float-ord = "0.3.2" serde = {version = "1.0.188", features = ["derive"]} serde_json = "1.0" tabled = "0.14.0" text-generation-client = { path = "../backends/client" } thiserror = "1.0.48" tokenizers = { workspace = true } tokio = { version = "1.32.0", features = ["rt", "rt-multi-thread", "parking_lot", "signal", "sync", "macros"] } ratatui = "0.28.1" tracing = "0.1.37" tracing-subscriber = { version = "0.3.17", features = ["json", "env-filter"] } hf-hub = { workspace = true }
text-generation-inference/benchmark/Cargo.toml/0
{ "file_path": "text-generation-inference/benchmark/Cargo.toml", "repo_id": "text-generation-inference", "token_count": 335 }
from text_generation.errors import ( parse_error, GenerationError, IncompleteGenerationError, OverloadedError, ValidationError, BadRequestError, ShardNotReadyError, ShardTimeoutError, NotFoundError, RateLimitExceededError, UnknownError, ) def test_generation_error(): payload = {"error_type": "generation", "error": "test"} assert isinstance(parse_error(400, payload), GenerationError) def test_incomplete_generation_error(): payload = {"error_type": "incomplete_generation", "error": "test"} assert isinstance(parse_error(400, payload), IncompleteGenerationError) def test_overloaded_error(): payload = {"error_type": "overloaded", "error": "test"} assert isinstance(parse_error(400, payload), OverloadedError) def test_validation_error(): payload = {"error_type": "validation", "error": "test"} assert isinstance(parse_error(400, payload), ValidationError) def test_bad_request_error(): payload = {"error": "test"} assert isinstance(parse_error(400, payload), BadRequestError) def test_shard_not_ready_error(): payload = {"error": "test"} assert isinstance(parse_error(403, payload), ShardNotReadyError) assert isinstance(parse_error(424, payload), ShardNotReadyError) def test_shard_timeout_error(): payload = {"error": "test"} assert isinstance(parse_error(504, payload), ShardTimeoutError) def test_not_found_error(): payload = {"error": "test"} assert isinstance(parse_error(404, payload), NotFoundError) def test_rate_limit_exceeded_error(): payload = {"error": "test"} assert isinstance(parse_error(429, payload), RateLimitExceededError) def test_unknown_error(): payload = {"error": "test"} assert isinstance(parse_error(500, payload), UnknownError)
text-generation-inference/clients/python/tests/test_errors.py/0
{ "file_path": "text-generation-inference/clients/python/tests/test_errors.py", "repo_id": "text-generation-inference", "token_count": 598 }
# Serving Private & Gated Models If the model you wish to serve is behind gated access or the model repository on Hugging Face Hub is private, and you have access to the model, you can provide your Hugging Face Hub access token. You can generate and copy a read token from [Hugging Face Hub tokens page](https://huggingface.co/settings/tokens) If you're using the CLI, set the `HF_TOKEN` environment variable. For example: ``` export HF_TOKEN=<YOUR READ TOKEN> ``` If you would like to do it through Docker, you can provide your token by specifying `HF_TOKEN` as shown below. ```bash model=meta-llama/Llama-2-7b-chat-hf volume=$PWD/data token=<your READ token> docker run --gpus all \ --shm-size 1g \ -e HF_TOKEN=$token \ -p 8080:80 \ -v $volume:/data ghcr.io/huggingface/text-generation-inference:3.1.0 \ --model-id $model ```
text-generation-inference/docs/source/basic_tutorials/gated_model_access.md/0
{ "file_path": "text-generation-inference/docs/source/basic_tutorials/gated_model_access.md", "repo_id": "text-generation-inference", "token_count": 290 }
[ { "choices": [ { "finish_reason": "", "index": 0, "logprobs": null, "text": " A" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 1, "logprobs": null, "text": " This" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 2, "logprobs": null, "text": " Paris" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 3, "logprobs": null, "text": "us" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 0, "logprobs": null, "text": " Beginner" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 1, "logprobs": null, "text": " is" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 2, "logprobs": null, "text": "\n" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 3, "logprobs": null, "text": "cul" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 0, "logprobs": null, "text": "’s" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 1, "logprobs": null, "text": " a" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 2, "logprobs": null, "text": "What" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 3, "logprobs": null, "text": "as" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 0, "logprobs": null, "text": " Guide" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 1, "logprobs": null, "text": " question" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 2, "logprobs": null, "text": " is" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 3, "logprobs": null, "text": "_minus" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 0, "logprobs": null, "text": "\n" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 1, "logprobs": null, "text": " that" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 2, "logprobs": null, "text": " the" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 3, "logprobs": null, "text": "cul" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 0, "logprobs": null, "text": "Deep" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 1, "logprobs": null, "text": " has" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 2, "logprobs": null, "text": " capital" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 3, "logprobs": null, "text": "as" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 0, "logprobs": null, "text": " learning" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 1, "logprobs": null, "text": " puzzled" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 2, "logprobs": null, "text": " of" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 3, "logprobs": null, "text": "(s" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 0, "logprobs": null, "text": " is" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 1, "logprobs": null, "text": " many" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 2, "logprobs": null, "text": " France" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 3, "logprobs": null, "text": "):\n" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 0, "logprobs": null, "text": " a" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 1, "logprobs": null, "text": " people" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 2, "logprobs": null, "text": "?\n" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "", "index": 3, "logprobs": null, "text": " " } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "length", "index": 0, "logprobs": null, "text": " subset" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "length", "index": 1, "logprobs": null, "text": " for" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "length", "index": 2, "logprobs": null, "text": "The" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" }, { "choices": [ { "finish_reason": "length", "index": 3, "logprobs": null, "text": " \"\"\"\n" } ], "created": 1725883643, "id": "", "model": "meta-llama/Meta-Llama-3.1-8B-Instruct", "object": "text_completion", "system_fingerprint": "2.2.1-dev0-native" } ]
text-generation-inference/integration-tests/models/__snapshots__/test_completion_prompts/test_flash_llama_completion_many_prompts_stream.json/0
{ "file_path": "text-generation-inference/integration-tests/models/__snapshots__/test_completion_prompts/test_flash_llama_completion_many_prompts_stream.json", "repo_id": "text-generation-inference", "token_count": 7100 }
{ "details": { "best_of_sequences": null, "finish_reason": "length", "generated_tokens": 10, "prefill": [], "seed": 0, "tokens": [ { "id": 5380, "logprob": 0.0, "special": false, "text": "?\n" }, { "id": 34564, "logprob": 0.0, "special": false, "text": "Deep" }, { "id": 6975, "logprob": 0.0, "special": false, "text": " learning" }, { "id": 320, "logprob": -0.19580078, "special": false, "text": " (" }, { "id": 16931, "logprob": -1.7783203, "special": false, "text": "DL" }, { "id": 8, "logprob": 0.0, "special": false, "text": ")" }, { "id": 374, "logprob": -1.4287109, "special": false, "text": " is" }, { "id": 264, "logprob": 0.0, "special": false, "text": " a" }, { "id": 27084, "logprob": 0.0, "special": false, "text": " subset" }, { "id": 315, "logprob": 0.0, "special": false, "text": " of" } ], "top_tokens": null }, "generated_text": "What is deep learning?\nDeep learning (DL) is a subset of" }
text-generation-inference/integration-tests/models/__snapshots__/test_compressed_tensors_wna16_int_24/test_compressed_tensors_wna16_int_24_all_params.json/0
{ "file_path": "text-generation-inference/integration-tests/models/__snapshots__/test_compressed_tensors_wna16_int_24/test_compressed_tensors_wna16_int_24_all_params.json", "repo_id": "text-generation-inference", "token_count": 849 }
[ { "details": { "best_of_sequences": null, "finish_reason": "length", "generated_tokens": 10, "prefill": [], "seed": null, "tokens": [ { "id": 1736, "logprob": -2.09375, "special": false, "text": " form" }, { "id": 109, "logprob": -1.9140625, "special": false, "text": "\n\n" }, { "id": 651, "logprob": -2.453125, "special": false, "text": "The" }, { "id": 2121, "logprob": -1.8984375, "special": false, "text": " test" }, { "id": 3853, "logprob": -0.23535156, "special": false, "text": " request" }, { "id": 1736, "logprob": -0.091308594, "special": false, "text": " form" }, { "id": 603, "logprob": -0.96875, "special": false, "text": " is" }, { "id": 1671, "logprob": -1.6484375, "special": false, "text": " used" }, { "id": 577, "logprob": -0.43164062, "special": false, "text": " to" }, { "id": 3853, "logprob": -1.2421875, "special": false, "text": " request" } ], "top_tokens": null }, "generated_text": " form\n\nThe test request form is used to request" }, { "details": { "best_of_sequences": null, "finish_reason": "length", "generated_tokens": 10, "prefill": [], "seed": null, "tokens": [ { "id": 1736, "logprob": -2.09375, "special": false, "text": " form" }, { "id": 109, "logprob": -1.9140625, "special": false, "text": "\n\n" }, { "id": 651, "logprob": -2.453125, "special": false, "text": "The" }, { "id": 2121, "logprob": -1.8984375, "special": false, "text": " test" }, { "id": 3853, "logprob": -0.23535156, "special": false, "text": " request" }, { "id": 1736, "logprob": -0.091308594, "special": false, "text": " form" }, { "id": 603, "logprob": -0.96875, "special": false, "text": " is" }, { "id": 1671, "logprob": -1.6484375, "special": false, "text": " used" }, { "id": 577, "logprob": -0.43164062, "special": false, "text": " to" }, { "id": 3853, "logprob": -1.2421875, "special": false, "text": " request" } ], "top_tokens": null }, "generated_text": " form\n\nThe test request form is used to request" }, { "details": { "best_of_sequences": null, "finish_reason": "length", "generated_tokens": 10, "prefill": [], "seed": null, "tokens": [ { "id": 1736, "logprob": -2.09375, "special": false, "text": " form" }, { "id": 109, "logprob": -1.9140625, "special": false, "text": "\n\n" }, { "id": 651, "logprob": -2.453125, "special": false, "text": "The" }, { "id": 2121, "logprob": -1.8984375, "special": false, "text": " test" }, { "id": 3853, "logprob": -0.23535156, "special": false, "text": " request" }, { "id": 1736, "logprob": -0.091308594, "special": false, "text": " form" }, { "id": 603, "logprob": -0.96875, "special": false, "text": " is" }, { "id": 1671, "logprob": -1.6484375, "special": false, "text": " used" }, { "id": 577, "logprob": -0.43164062, "special": false, "text": " to" }, { "id": 3853, "logprob": -1.2421875, "special": false, "text": " request" } ], "top_tokens": null }, "generated_text": " form\n\nThe test request form is used to request" }, { "details": { "best_of_sequences": null, "finish_reason": "length", "generated_tokens": 10, "prefill": [], "seed": null, "tokens": [ { "id": 1736, "logprob": -2.09375, "special": false, "text": " form" }, { "id": 109, "logprob": -1.9140625, "special": false, "text": "\n\n" }, { "id": 651, "logprob": -2.453125, "special": false, "text": "The" }, { "id": 2121, "logprob": -1.8984375, "special": false, "text": " test" }, { "id": 3853, "logprob": -0.23535156, "special": false, "text": " request" }, { "id": 1736, "logprob": -0.091308594, "special": false, "text": " form" }, { "id": 603, "logprob": -0.96875, "special": false, "text": " is" }, { "id": 1671, "logprob": -1.6484375, "special": false, "text": " used" }, { "id": 577, "logprob": -0.43164062, "special": false, "text": " to" }, { "id": 3853, "logprob": -1.2421875, "special": false, "text": " request" } ], "top_tokens": null }, "generated_text": " form\n\nThe test request form is used to request" } ]
text-generation-inference/integration-tests/models/__snapshots__/test_flash_gemma/test_flash_gemma_load.json/0
{ "file_path": "text-generation-inference/integration-tests/models/__snapshots__/test_flash_gemma/test_flash_gemma_load.json", "repo_id": "text-generation-inference", "token_count": 4072 }
{ "details": { "best_of_sequences": null, "finish_reason": "length", "generated_tokens": 10, "prefill": [], "seed": null, "tokens": [ { "id": 5229, "logprob": -2.7988281, "special": false, "text": " failed" }, { "id": 29901, "logprob": -0.91259766, "special": false, "text": ":" }, { "id": 853, "logprob": -2.8496094, "special": false, "text": " Un" }, { "id": 23765, "logprob": -1.1894531, "special": false, "text": "supported" }, { "id": 4714, "logprob": -1.5917969, "special": false, "text": " browser" }, { "id": 29892, "logprob": -0.34765625, "special": false, "text": "," }, { "id": 1873, "logprob": -1.2695312, "special": false, "text": " version" }, { "id": 470, "logprob": -0.25170898, "special": false, "text": " or" }, { "id": 7481, "logprob": -0.21411133, "special": false, "text": " platform" }, { "id": 13, "logprob": -1.1162109, "special": false, "text": "\n" } ], "top_tokens": null }, "generated_text": " failed: Unsupported browser, version or platform\n" }
text-generation-inference/integration-tests/models/__snapshots__/test_flash_llama_marlin_24/test_flash_llama_marlin.json/0
{ "file_path": "text-generation-inference/integration-tests/models/__snapshots__/test_flash_llama_marlin_24/test_flash_llama_marlin.json", "repo_id": "text-generation-inference", "token_count": 869 }
{ "details": { "best_of_sequences": null, "finish_reason": "eos_token", "generated_tokens": 9, "prefill": [], "seed": null, "tokens": [ { "id": 2684, "logprob": -0.24902344, "special": false, "text": " There" }, { "id": 374, "logprob": -0.0703125, "special": false, "text": " is" }, { "id": 264, "logprob": -0.23535156, "special": false, "text": " a" }, { "id": 35372, "logprob": -0.125, "special": false, "text": " statue" }, { "id": 304, "logprob": -0.30273438, "special": false, "text": " in" }, { "id": 279, "logprob": -0.20507812, "special": false, "text": " the" }, { "id": 2217, "logprob": -0.076171875, "special": false, "text": " image" }, { "id": 13, "logprob": -0.053710938, "special": false, "text": "." }, { "id": 128258, "logprob": -0.011352539, "special": true, "text": "<end_of_utterance>" } ], "top_tokens": null }, "generated_text": " There is a statue in the image." }
text-generation-inference/integration-tests/models/__snapshots__/test_idefics3/test_flash_idefics3_next_simple_url.json/0
{ "file_path": "text-generation-inference/integration-tests/models/__snapshots__/test_idefics3/test_flash_idefics3_next_simple_url.json", "repo_id": "text-generation-inference", "token_count": 796 }
{ "details": { "best_of_sequences": null, "finish_reason": "length", "generated_tokens": 10, "prefill": [ { "id": 0, "logprob": null, "text": "<pad>" } ], "seed": 0, "tokens": [ { "id": 16017, "logprob": 0.0, "special": false, "text": " blue" }, { "id": 20495, "logprob": 0.0, "special": false, "text": " sky" }, { "id": 259, "logprob": -0.47070312, "special": false, "text": " " }, { "id": 261, "logprob": -0.15307617, "special": false, "text": "," }, { "id": 35622, "logprob": -0.796875, "special": false, "text": " cloud" }, { "id": 263, "logprob": -1.2958984, "special": false, "text": "s" }, { "id": 305, "logprob": 0.0, "special": false, "text": " and" }, { "id": 35622, "logprob": -1.2998047, "special": false, "text": " cloud" }, { "id": 263, "logprob": 0.0, "special": false, "text": "s" }, { "id": 1, "logprob": 0.0, "special": true, "text": "</s>" } ], "top_tokens": null }, "generated_text": "Why is the sky blue?blue sky , clouds and clouds" }
text-generation-inference/integration-tests/models/__snapshots__/test_mt0_base/test_mt0_base_all_params.json/0
{ "file_path": "text-generation-inference/integration-tests/models/__snapshots__/test_mt0_base/test_mt0_base_all_params.json", "repo_id": "text-generation-inference", "token_count": 910 }
{ "choices": [ { "delta": { "content": " assistant", "role": "assistant", "tool_calls": null }, "finish_reason": null, "index": 0, "logprobs": null } ], "created": 1728497531, "id": "", "model": "meta-llama/Llama-3.1-8B-Instruct", "object": "chat.completion.chunk", "system_fingerprint": "2.4.2-dev0-native", "usage": null }
text-generation-inference/integration-tests/models/__snapshots__/test_tools_llama/test_flash_llama_grammar_tools_insufficient_information_stream.json/0
{ "file_path": "text-generation-inference/integration-tests/models/__snapshots__/test_tools_llama/test_flash_llama_grammar_tools_insufficient_information_stream.json", "repo_id": "text-generation-inference", "token_count": 205 }
import pytest @pytest.fixture(scope="module") def flash_llama_awq_handle(launcher): with launcher( "abhinavkulkarni/codellama-CodeLlama-7b-Python-hf-w4-g128-awq", num_shard=1, quantize="awq", ) as handle: yield handle @pytest.fixture(scope="module") async def flash_llama_awq(flash_llama_awq_handle): await flash_llama_awq_handle.health(300) return flash_llama_awq_handle.client @pytest.mark.release @pytest.mark.asyncio async def test_flash_llama_awq(flash_llama_awq, response_snapshot): response = await flash_llama_awq.generate( "What is Deep Learning?", max_new_tokens=10, decoder_input_details=True ) assert response.details.generated_tokens == 10 assert ( response.generated_text == "\nWhat is the difference between Deep Learning and Machine" ) assert response == response_snapshot @pytest.mark.release @pytest.mark.asyncio async def test_flash_llama_awq_all_params(flash_llama_awq, response_snapshot): response = await flash_llama_awq.generate( "What is Deep Learning?", max_new_tokens=10, repetition_penalty=1.2, return_full_text=True, temperature=0.5, top_p=0.9, top_k=10, truncate=5, typical_p=0.9, watermark=True, decoder_input_details=True, seed=0, ) assert response.details.generated_tokens == 10 assert response == response_snapshot @pytest.mark.release @pytest.mark.asyncio async def test_flash_llama_awq_load(flash_llama_awq, generate_load, response_snapshot): responses = await generate_load( flash_llama_awq, "What is Deep Learning?", max_new_tokens=10, n=4 ) assert len(responses) == 4 assert all( [ r.generated_text == "\nWhat is the difference between Deep Learning and Machine" for r in responses ] ) assert responses == response_snapshot
text-generation-inference/integration-tests/models/test_flash_awq.py/0
{ "file_path": "text-generation-inference/integration-tests/models/test_flash_awq.py", "repo_id": "text-generation-inference", "token_count": 866 }
import pytest @pytest.fixture(scope="module") def flash_starcoder_handle(launcher): with launcher("bigcode/starcoder", num_shard=2) as handle: yield handle @pytest.fixture(scope="module") async def flash_starcoder(flash_starcoder_handle): await flash_starcoder_handle.health(300) return flash_starcoder_handle.client @pytest.mark.release @pytest.mark.asyncio @pytest.mark.private async def test_flash_starcoder(flash_starcoder, response_snapshot): response = await flash_starcoder.generate( "def print_hello", max_new_tokens=10, decoder_input_details=True ) assert response.details.generated_tokens == 10 assert response == response_snapshot @pytest.mark.release @pytest.mark.asyncio @pytest.mark.private async def test_flash_starcoder_default_params(flash_starcoder, response_snapshot): response = await flash_starcoder.generate( "def print_hello", max_new_tokens=60, temperature=0.2, top_p=0.95, decoder_input_details=True, seed=0, ) assert response.details.generated_tokens == 60 assert response == response_snapshot @pytest.mark.release @pytest.mark.asyncio @pytest.mark.private async def test_flash_starcoder_load(flash_starcoder, generate_load, response_snapshot): responses = await generate_load( flash_starcoder, "def print_hello", max_new_tokens=10, n=4 ) assert len(responses) == 4 assert all([r.generated_text == responses[0].generated_text for r in responses]) assert responses == response_snapshot
text-generation-inference/integration-tests/models/test_flash_starcoder.py/0
{ "file_path": "text-generation-inference/integration-tests/models/test_flash_starcoder.py", "repo_id": "text-generation-inference", "token_count": 602 }
import pytest @pytest.fixture(scope="module") def neox_sharded_handle(launcher): with launcher( "OpenAssistant/oasst-sft-1-pythia-12b", num_shard=2, use_flash_attention=False ) as handle: yield handle @pytest.fixture(scope="module") async def neox_sharded(neox_sharded_handle): await neox_sharded_handle.health(300) return neox_sharded_handle.client @pytest.mark.release @pytest.mark.skip @pytest.mark.asyncio async def test_neox(neox_sharded, response_snapshot): response = await neox_sharded.generate( "<|prompter|>What is a meme, and what's the history behind this word?<|endoftext|><|assistant|>", max_new_tokens=10, decoder_input_details=True, ) assert response.details.generated_tokens == 10 assert response == response_snapshot @pytest.mark.release @pytest.mark.skip @pytest.mark.asyncio async def test_neox_load(neox_sharded, generate_load, response_snapshot): responses = await generate_load( neox_sharded, "<|prompter|>What is a meme, and what's the history behind this word?<|endoftext|><|assistant|>", max_new_tokens=10, n=4, ) assert len(responses) == 4 assert all([r.generated_text == responses[0].generated_text for r in responses]) assert responses == response_snapshot
text-generation-inference/integration-tests/models/test_neox_sharded.py/0
{ "file_path": "text-generation-inference/integration-tests/models/test_neox_sharded.py", "repo_id": "text-generation-inference", "token_count": 523 }
import { check } from 'k6'; import { scenario } from 'k6/execution'; import http from 'k6/http'; import { Trend, Counter } from 'k6/metrics'; const host = __ENV.HOST; const model_id = __ENV.MODEL_ID; const timePerToken = new Trend('time_per_token', true); const tokens = new Counter('tokens'); const new_tokens = new Counter('new_tokens'); const input_tokens = new Counter('input_tokens'); const max_new_tokens = 50; // const shareGPT = JSON.parse(open("ShareGPT_V3_unfiltered_cleaned_split.json")) const shareGPT = JSON.parse(open("small.json")) export function get_options() { return { thresholds: { http_req_failed: ['rate==0'], // time_per_token: [{ // threshold: `p(50)<${5 * reference_latency_ms}`, // abortOnFail: true, // delayAbortEval: '10s' // }], }, scenarios: { // single_user: { // executor: 'constant-arrival-rate', // duration: '60s', // preAllocatedVUs: 1, // rate: 20, // timeUnit: '1s', // }, // load_test: { // executor: 'constant-arrival-rate', // duration: '60s', // preAllocatedVUs: 100, // rate: 1, // timeUnit: '1s', // }, // breakpoint: { // executor: 'ramping-arrival-rate', //Assure load increase if the system slows // preAllocatedVUs: 300, // stages: [ // { duration: '60s', target: 100 }, // just slowly ramp-up to a HUGE load // ], // }, throughput: { executor: 'shared-iterations', vus: 100, iterations: 200, maxDuration: '40s', }, }, }; } function generate_payload(gpt, max_new_tokens) { const input = gpt["conversations"][0]["value"]; return { "messages": [{ "role": "user", "content": input }], "temperature": 0, "model": `${model_id}`, "max_tokens": max_new_tokens } } export const options = get_options(); export default function run() { const headers = { 'Content-Type': 'application/json' }; const query = shareGPT[scenario.iterationInTest % shareGPT.length]; const payload = JSON.stringify(generate_payload(query, max_new_tokens)); const res = http.post(`http://${host}/v1/chat/completions`, payload, { headers, }); if (res.status >= 400 && res.status < 500) { return; } check(res, { 'Post status is 200': (res) => res.status === 200, }); const duration = res.timings.duration; if (res.status === 200) { const body = res.json(); const completion_tokens = body.usage.completion_tokens; const latency_ms_per_token = duration / completion_tokens; timePerToken.add(latency_ms_per_token); const prompt_tokens = body.usage.prompt_tokens; input_tokens.add(prompt_tokens); new_tokens.add(completion_tokens); tokens.add(completion_tokens + prompt_tokens); } }
text-generation-inference/load_tests/common.js/0
{ "file_path": "text-generation-inference/load_tests/common.js", "repo_id": "text-generation-inference", "token_count": 1530 }
[package] name = "text-generation-router" description = "Text Generation Webserver" build = "build.rs" version.workspace = true edition.workspace = true authors.workspace = true homepage.workspace = true [dependencies] anyhow = "1" async-trait = "0.1.74" async-stream = "0.3.5" axum = { version = "0.7", features = ["json"] } axum-tracing-opentelemetry = "0.16" clap = { version = "4.4.5", features = ["derive", "env"] } futures = "0.3.28" hf-hub = { workspace = true } itertools = "0.10" jsonschema = { version = "0.28.0" } metrics = { workspace = true } metrics-exporter-prometheus = { workspace = true } nohash-hasher = "0.2.0" opentelemetry = { version = "0.20.0", features = ["rt-tokio"] } opentelemetry-otlp = "0.13.0" outlines-core = { git = "https://github.com/dottxt-ai/outlines-core.git", rev = "ba10c619fc9bf3c487e43f49bdecb95a24bb465c" } rand = "0.8.5" reqwest = { version = "0.11.20", features = ["blocking"] } serde = "1.0.188" serde_json = "1.0.107" thiserror = "1.0.48" tokenizers = { workspace = true } tokio = { version = "1.32.0", features = [ "rt", "rt-multi-thread", "parking_lot", "signal", "sync", ] } tokio-stream = "0.1.14" tower-http = { version = "0.5.1", features = ["cors"] } tracing = "0.1.40" tracing-opentelemetry = "0.21.0" tracing-subscriber = { version = "0.3.18", features = ["json", "env-filter"] } utoipa = { version = "4.2.0", features = ["axum_extras"] } utoipa-swagger-ui = { version = "6.0.0", features = ["axum"] } ngrok = { version = "0.13.1", features = ["axum"], optional = true } init-tracing-opentelemetry = { version = "0.14.1", features = [ "opentelemetry-otlp", ] } minijinja = { workspace = true } minijinja-contrib = { workspace = true } futures-util = "0.3.30" regex = "1.10.3" once_cell = "1.19.0" image = "0.25.1" base64 = { workspace = true } sysinfo = "0.30.13" uuid = { version = "1.9.1", default-features = false, features = [ "v4", "fast-rng", "macro-diagnostics", ] } csv = "1.3.0" ureq = "=2.9" pyo3 = { workspace = true } chrono = "0.4.39" [build-dependencies] vergen = { version = "8.2.5", features = ["build", "git", "gitcl"] } [features] default = ["ngrok"] ngrok = ["dep:ngrok"] google = [] kserve = []
text-generation-inference/router/Cargo.toml/0
{ "file_path": "text-generation-inference/router/Cargo.toml", "repo_id": "text-generation-inference", "token_count": 935 }
#!/bin/bash if [[ -z "${HF_MODEL_ID}" ]]; then echo "HF_MODEL_ID must be set" exit 1 fi export MODEL_ID="${HF_MODEL_ID}" if [[ -n "${HF_MODEL_REVISION}" ]]; then export REVISION="${HF_MODEL_REVISION}" fi if [[ -n "${SM_NUM_GPUS}" ]]; then export NUM_SHARD="${SM_NUM_GPUS}" fi if [[ -n "${HF_MODEL_QUANTIZE}" ]]; then export QUANTIZE="${HF_MODEL_QUANTIZE}" fi if [[ -n "${HF_MODEL_TRUST_REMOTE_CODE}" ]]; then export TRUST_REMOTE_CODE="${HF_MODEL_TRUST_REMOTE_CODE}" fi text-generation-launcher --port 8080
text-generation-inference/sagemaker-entrypoint.sh/0
{ "file_path": "text-generation-inference/sagemaker-entrypoint.sh", "repo_id": "text-generation-inference", "token_count": 239 }
from setuptools import setup from torch.utils.cpp_extension import BuildExtension, CUDAExtension extra_compile_args = ["-std=c++17"] setup( name="custom_kernels", ext_modules=[ CUDAExtension( name="custom_kernels.fused_bloom_attention_cuda", sources=["custom_kernels/fused_bloom_attention_cuda.cu"], extra_compile_args=extra_compile_args, ), CUDAExtension( name="custom_kernels.fused_attention_cuda", sources=["custom_kernels/fused_attention_cuda.cu"], extra_compile_args=extra_compile_args, ), ], cmdclass={"build_ext": BuildExtension}, )
text-generation-inference/server/custom_kernels/setup.py/0
{ "file_path": "text-generation-inference/server/custom_kernels/setup.py", "repo_id": "text-generation-inference", "token_count": 309 }
#ifndef _config_h #define _config_h #define MAX_Q_GEMM_ROWS 50 #define MAX_Q_GEMM_WEIGHTS 4 // must be <= MAX_Q_GEMM_ROWS #define QMODE_2BIT 1 #define QMODE_3BIT 1 #define QMODE_4BIT 1 #define QMODE_5BIT 1 #define QMODE_6BIT 0 #define QMODE_8BIT 0 #endif
text-generation-inference/server/exllamav2_kernels/exllamav2_kernels/config.h/0
{ "file_path": "text-generation-inference/server/exllamav2_kernels/exllamav2_kernels/config.h", "repo_id": "text-generation-inference", "token_count": 119 }
#ifndef _qdq_util_cuh #define _qdq_util_cuh union half2_uint32 { uint32_t as_uint32; half2 as_half2; __device__ half2_uint32(uint32_t val) : as_uint32(val) {} __device__ half2_uint32(half2 val) : as_half2(val) {} __device__ half2_uint32() : as_uint32(0) {} }; union half_uint16 { uint16_t as_uint16; half as_half; __device__ half_uint16(uint16_t val) : as_uint16(val) {} __device__ half_uint16(half val) : as_half(val) {} __device__ half_uint16() : as_uint16(0) {} }; // Max_scale premultiplied by 1/256 __forceinline__ __device__ half dq_scale(const int qs, const half max_scale) { int qs_i = qs + 1; half qs_h = __int2half_rn(qs_i * qs_i); qs_h = __hmul(qs_h, max_scale); return qs_h; } __forceinline__ __device__ half dq(const int q, const int qzero, const half scale) { return __hmul(__int2half_rn(q - qzero), scale); } __forceinline__ __device__ half dq_ns(const int q, const int qzero) { //return __hsub(__int2half_rn(q), __int2half_rn(qzero)); return __int2half_rn(q - qzero); } __forceinline__ __device__ int exb(const uint32_t q, const int shift, const int mask) { return (int)((q >> shift) & mask); } __forceinline__ __device__ int exb(const uint32_t q1, const uint32_t q0, const int shift, const int mask) { return (int)(__funnelshift_rc(q0, q1, shift) & mask); } #endif
text-generation-inference/server/exllamav2_kernels/exllamav2_kernels/cuda/quant/qdq_util.cuh/0
{ "file_path": "text-generation-inference/server/exllamav2_kernels/exllamav2_kernels/cuda/quant/qdq_util.cuh", "repo_id": "text-generation-inference", "token_count": 602 }
import pytest from unittest.mock import Mock from text_generation_server.utils.adapter import ( get_attn_weights, get_mlp_weights, parse_lora_adapters, AdapterInfo, ) def test_parse_lora_adapters_empty(): assert parse_lora_adapters(None) == [] assert parse_lora_adapters("") == [] def test_parse_lora_adapters_single(): result = parse_lora_adapters("adapter1") assert result == [AdapterInfo(id="adapter1", path=None, revision=None)] def test_parse_lora_adapters_with_path(): result = parse_lora_adapters("adapter1=path/to/adapter1") assert result == [ AdapterInfo(id="adapter1", path="path/to/adapter1", revision=None) ] def test_parse_lora_adapters_with_path_and_revision(): result = parse_lora_adapters("adapter1=path/to/adapter1@main") assert result == [ AdapterInfo(id="adapter1", path="path/to/adapter1", revision="main") ] def test_parse_lora_adapters_multiple(): result = parse_lora_adapters( "adapter1,adapter2=path/to/adapter2,adapter3=path/to/adapter3@dev" ) assert result == [ AdapterInfo(id="adapter1", path=None, revision=None), AdapterInfo(id="adapter2", path="path/to/adapter2", revision=None), AdapterInfo(id="adapter3", path="path/to/adapter3", revision="dev"), ] def test_parse_lora_adapters_invalid_format(): try: parse_lora_adapters("adapter1,invalid=format=test,adapter3") assert False, "Should have raised ValueError" except ValueError as e: assert str(e) == "Invalid LoRA adapter format: invalid=format=test" def test_get_attn_weights(): # create a mock layer mock_layer = Mock() mock_layer.self_attn.query_key_value = Mock() mock_layer.self_attn.o_proj = Mock() # call the function result = get_attn_weights(2, mock_layer) # assert the result expected = { (2, "q_proj"): ( "model.layers.2.self_attn.q_proj", mock_layer.self_attn.query_key_value, ), (2, "k_proj"): ( "model.layers.2.self_attn.k_proj", mock_layer.self_attn.query_key_value, ), (2, "qkv_proj"): ( "model.layers.2.self_attn.qkv_proj", mock_layer.self_attn.query_key_value, ), (2, "v_proj"): ( "model.layers.2.self_attn.v_proj", mock_layer.self_attn.query_key_value, ), (2, "o_proj"): ("model.layers.2.self_attn.o_proj", mock_layer.self_attn.o_proj), } assert result == expected def test_get_mlp_weights_with_gate_up_proj(): # create a mock layer with gate_up_proj mock_layer = Mock() mock_layer.mlp.gate_up_proj = Mock() mock_layer.mlp.down_proj = Mock() # call the function result = get_mlp_weights(3, mock_layer) # assert the result expected = { (3, "c_fc"): ("model.layers.3.mlp.c_fc", mock_layer.mlp.c_fc), (3, "c_proj"): ("model.layers.3.mlp.c_proj", mock_layer.mlp.c_proj), (3, "gate_proj"): ("model.layers.3.mlp.gate_proj", mock_layer.mlp.gate_up_proj), (3, "up_proj"): ("model.layers.3.mlp.up_proj", mock_layer.mlp.gate_up_proj), (3, "down_proj"): ("model.layers.3.mlp.down_proj", mock_layer.mlp.down_proj), } assert result == expected def test_get_mlp_weights_without_gate_up_proj(): # create a mock layer without gate_up_proj mock_layer = Mock() mock_layer.mlp = Mock(spec=[]) # call the function result = get_mlp_weights(1, mock_layer) # assert the result assert result == {} @pytest.mark.parametrize("layer_index", [0, 1, 5]) def test_get_attn_weights_different_layers(layer_index): mock_layer = Mock() mock_layer.self_attn.query_key_value = Mock() mock_layer.self_attn.o_proj = Mock() result = get_attn_weights(layer_index, mock_layer) for k in ["q", "k", "v"]: assert (layer_index, f"{k}_proj") in result assert ( result[(layer_index, f"{k}_proj")][0] == f"model.layers.{layer_index}.self_attn.{k}_proj" ) assert (layer_index, "o_proj") in result assert ( result[(layer_index, "o_proj")][0] == f"model.layers.{layer_index}.self_attn.o_proj" ) @pytest.mark.parametrize("layer_index", [0, 1, 5]) def test_get_mlp_weights_different_layers(layer_index): mock_layer = Mock() mock_layer.mlp.gate_up_proj = Mock() mock_layer.mlp.down_proj = Mock() result = get_mlp_weights(layer_index, mock_layer) for k in ["gate", "up", "down"]: assert (layer_index, f"{k}_proj") in result assert ( result[(layer_index, f"{k}_proj")][0] == f"model.layers.{layer_index}.mlp.{k}_proj" ) def test_get_attn_weights_llama_compatibility(): mock_layer = Mock() mock_layer.self_attn.query_key_value = Mock() mock_layer.self_attn.o_proj = Mock() result = get_attn_weights(2, mock_layer) expected = { (2, "q_proj"): ( "model.layers.2.self_attn.q_proj", mock_layer.self_attn.query_key_value, ), (2, "k_proj"): ( "model.layers.2.self_attn.k_proj", mock_layer.self_attn.query_key_value, ), (2, "qkv_proj"): ( "model.layers.2.self_attn.qkv_proj", mock_layer.self_attn.query_key_value, ), (2, "v_proj"): ( "model.layers.2.self_attn.v_proj", mock_layer.self_attn.query_key_value, ), (2, "o_proj"): ("model.layers.2.self_attn.o_proj", mock_layer.self_attn.o_proj), } assert result == expected def test_get_mlp_weights_llama_compatibility(): mock_layer = Mock() mock_layer.mlp.gate_up_proj = Mock() mock_layer.mlp.down_proj = Mock() result = get_mlp_weights(3, mock_layer) expected = { (3, "c_fc"): ("model.layers.3.mlp.c_fc", mock_layer.mlp.c_fc), (3, "c_proj"): ("model.layers.3.mlp.c_proj", mock_layer.mlp.c_proj), (3, "gate_proj"): ("model.layers.3.mlp.gate_proj", mock_layer.mlp.gate_up_proj), (3, "up_proj"): ("model.layers.3.mlp.up_proj", mock_layer.mlp.gate_up_proj), (3, "down_proj"): ("model.layers.3.mlp.down_proj", mock_layer.mlp.down_proj), } assert result == expected def test_get_attn_weights_gemma_compatibility(): mock_layer = Mock() mock_layer.self_attn.query_key_value = Mock() mock_layer.self_attn.o_proj = Mock() result = get_attn_weights(2, mock_layer) expected = { (2, "q_proj"): ( "model.layers.2.self_attn.q_proj", mock_layer.self_attn.query_key_value, ), (2, "k_proj"): ( "model.layers.2.self_attn.k_proj", mock_layer.self_attn.query_key_value, ), (2, "qkv_proj"): ( "model.layers.2.self_attn.qkv_proj", mock_layer.self_attn.query_key_value, ), (2, "v_proj"): ( "model.layers.2.self_attn.v_proj", mock_layer.self_attn.query_key_value, ), (2, "o_proj"): ("model.layers.2.self_attn.o_proj", mock_layer.self_attn.o_proj), } assert result == expected def test_get_mlp_weights_gemma_compatibility(): mock_layer = Mock() mock_layer.mlp.gate_proj = Mock() mock_layer.mlp.up_proj = Mock() mock_layer.mlp.down_proj = Mock() # ensure that the mock_layer.mlp.gate_up_proj attribute does not exist. # This is necessary because the use of `Mock` automatically creates any # attributes that are accessed, even if they don't exist in the actual # implementation. If `gate_up_proj` were created, `get_mlp_weights` might # follow the wrong execution path and return an incorrect result. del mock_layer.mlp.gate_up_proj result = get_mlp_weights(3, mock_layer) expected = { (3, "c_fc"): ("model.layers.3.mlp.c_fc", mock_layer.mlp.c_fc), (3, "c_proj"): ("model.layers.3.mlp.c_proj", mock_layer.mlp.c_proj), (3, "gate_proj"): ("model.layers.3.mlp.gate_proj", mock_layer.mlp.gate_proj), (3, "up_proj"): ("model.layers.3.mlp.up_proj", mock_layer.mlp.up_proj), (3, "down_proj"): ("model.layers.3.mlp.down_proj", mock_layer.mlp.down_proj), } assert result == expected
text-generation-inference/server/tests/utils/test_adapter.py/0
{ "file_path": "text-generation-inference/server/tests/utils/test_adapter.py", "repo_id": "text-generation-inference", "token_count": 4022 }
import os from text_generation_server.utils.import_utils import SYSTEM from .common import Seqlen if os.getenv("USE_FLASH_ATTENTION", "").lower() == "false": raise ImportError("`USE_FLASH_ATTENTION` is false.") if SYSTEM == "cuda": from .cuda import ( SUPPORTS_WINDOWING, attention, paged_attention, ) elif SYSTEM == "rocm": from .rocm import ( SUPPORTS_WINDOWING, attention, paged_attention, ) elif SYSTEM == "ipex": from .ipex import ( SUPPORTS_WINDOWING, attention, paged_attention, ) else: raise ImportError(f"System {SYSTEM} doesn't support flash/paged attention") # KVCache needs `reshape_and_cache`, so ensure that it is defined already. from .kv_cache import KVCache, get_kv_scales __all__ = [ "attention", "get_kv_scales", "paged_attention", "SUPPORTS_WINDOWING", "KVCache", "Seqlen", ]
text-generation-inference/server/text_generation_server/layers/attention/__init__.py/0
{ "file_path": "text-generation-inference/server/text_generation_server/layers/attention/__init__.py", "repo_id": "text-generation-inference", "token_count": 404 }
from typing import List, Optional, Union import torch from compressed_tensors.quantization import QuantizationArgs, QuantizationType from text_generation_server.layers.fp8 import ( Fp8Weight, _load_scalar_or_matrix_scale, requantize_with_max_scale, normalize_e4m3fn_to_native_float8, ) from text_generation_server.utils.weights import Weights, WeightsLoader from text_generation_server.utils.import_utils import SYSTEM class W8ANFpLoader(WeightsLoader): """ Loader for W8A8/W8A16 FP compressed-tensors parameters. """ def __init__( self, *, input_activations: Optional[QuantizationArgs], weights: QuantizationArgs, ): assert weights.type == QuantizationType.FLOAT and weights.num_bits == 8 # We ignore the `strategy` option which sets the scales to be # per-tensor, per-channel or per-token. What scales are supported # is dependent on the kernels used (e.g. cutlass can do tokenwise, # Torch cannot, and FP8-Marlin does not quantize inputs at all). # So, instead we try to use the best-possible configuration. self.load_weight_scale = not weights.dynamic self.load_input_scale = ( input_activations is not None and not input_activations.dynamic ) self.force_w8a16 = ( input_activations is not None and input_activations.num_bits == 16 ) def __str__(self) -> str: def scale_to_str(scale): return "static" if scale else "dynamic" quantization_type = f"W8A{16 if self.force_w8a16 else 8}" return f"{self.__class__.__name__} ({quantization_type}, weight: {scale_to_str(self.load_weight_scale)}, input: {scale_to_str(self.load_input_scale)})" def get_weights(self, weights: "Weights", prefix: str): w = weights.get_tensor(f"{prefix}.weight") weight_scale = None if self.load_weight_scale: weight_scale = weights.get_tensor(f"{prefix}.weight_scale", to_dtype=False) if SYSTEM == "cuda": weight_scale = weight_scale.reshape(-1).expand(w.shape[0]) input_scale = None if self.load_input_scale: input_scale = weights.get_tensor( f"{prefix}.input_scale", to_dtype=False ).reshape(-1) return Fp8Weight( weight=w, weight_scale=weight_scale, input_scale=input_scale, dtype=weights.dtype, force_w8a16=self.force_w8a16, ) def get_weights_col_packed( self, weights: Weights, prefix: str, block_sizes: Union[int, List[int]], ): w = weights.get_packed_sharded( f"{prefix}.weight", dim=0, block_sizes=block_sizes ) weight_scale = None if self.load_weight_scale: weight_scale = weights.get_tensor(f"{prefix}.weight_scale", to_dtype=False) if weight_scale.numel() > 1: weight_scale = weights.get_packed_sharded( f"{prefix}.weight_scale", dim=0, block_sizes=block_sizes, to_dtype=False, ) if SYSTEM == "cuda": weight_scale = weight_scale.reshape(-1).expand(w.shape[0]) input_scale = None if self.load_input_scale: input_scale = weights.get_tensor(f"{prefix}.input_scale", to_dtype=False) if input_scale.numel() > 1: input_scale = weights.get_packed_sharded( f"{prefix}.input_scale", dim=0, block_sizes=block_sizes, to_dtype=False, ) input_scale = input_scale.reshape(-1).max() return Fp8Weight( weight=w, weight_scale=weight_scale, input_scale=input_scale, dtype=weights.dtype, force_w8a16=self.force_w8a16, ) def get_multi_weights_col(self, weights: "Weights", prefixes: List[str], dim: int): # FIXME: Force to_device to false as fp8 weights do not support torch.cat on device yet w = [ weights.get_sharded(f"{p}.weight", dim=0, to_device=False) for p in prefixes ] shapes = [x.shape for x in w] # Concat then send to the device w = torch.cat(w, dim=dim).to(weights.device) weight_scale = None if self.load_weight_scale: weight_scale = [ _load_scalar_or_matrix_scale(weights, f"{p}.weight_scale", shape) for p, shape in zip(prefixes, shapes) ] weight_scale = torch.cat(weight_scale, dim=0).reshape(-1) input_scale = None if self.load_input_scale: input_scale = [ _load_scalar_or_matrix_scale(weights, f"{p}.input_scale", shape) for p, shape in zip(prefixes, shapes) if weights.has_tensor(f"{p}.input_scale") ] assert len(input_scale) == 0 or len(input_scale) == len(prefixes) input_scale = ( torch.cat(input_scale, dim=0).reshape(-1).max() if len(input_scale) != 0 else None ) if self.load_weight_scale and SYSTEM == "rocm": w, weight_scale, input_scale = normalize_e4m3fn_to_native_float8( w, weight_scale, input_scale ) if weight_scale.numel() == len(prefixes): logical_widths = [x[0] for x in shapes] w, weight_scale = requantize_with_max_scale( w, weight_scale.to(weights.device), logical_widths, weights.dtype ) return Fp8Weight( weight=w, weight_scale=weight_scale, input_scale=input_scale, dtype=weights.dtype, force_w8a16=self.force_w8a16, ) def get_weights_row(self, weights: "Weights", prefix: str): w = weights.get_sharded(f"{prefix}.weight", dim=1) weight_scale = None if self.load_weight_scale: weight_scale = weights.get_tensor(f"{prefix}.weight_scale", to_dtype=False) if SYSTEM == "cuda": weight_scale = weight_scale.reshape(-1).expand(w.shape[0]) input_scale = None if self.load_input_scale: input_scale = weights.get_tensor( f"{prefix}.input_scale", to_dtype=False ).reshape(-1) return Fp8Weight( weight=w, weight_scale=weight_scale, input_scale=input_scale, dtype=weights.dtype, force_w8a16=self.force_w8a16, )
text-generation-inference/server/text_generation_server/layers/compressed_tensors/w8an_fp.py/0
{ "file_path": "text-generation-inference/server/text_generation_server/layers/compressed_tensors/w8an_fp.py", "repo_id": "text-generation-inference", "token_count": 3370 }
import torch from text_generation_server.utils.import_utils import SYSTEM from torch.nn import functional as F import os if SYSTEM == "rocm": ROCM_USE_SKINNY_GEMM = os.getenv("ROCM_USE_SKINNY_GEMM", "True").lower() in ( "true", "1", ) if ROCM_USE_SKINNY_GEMM: try: import vllm._custom_ops as ops except Exception as e: raise ImportError( f"Could not load `vllm._custom_ops` for ROCm skinny gemm. Full error: {e}" ) class FastLinear(torch.nn.Module): def __init__( self, weight, bias, ) -> None: super().__init__() self.weight = torch.nn.Parameter(weight, requires_grad=False) if bias is not None: self.bias = torch.nn.Parameter(bias, requires_grad=False) else: self.bias = None @classmethod def load(cls, config, prefix: str, weights, bias: bool): weight = weights.get_tensor(f"{prefix}.weight") if bias: bias = weights.get_tensor(f"{prefix}.bias") else: bias = None return cls(weight, bias) def forward(self, input: torch.Tensor) -> torch.Tensor: return F.linear(input, self.weight, self.bias) class FastLinearROCm(torch.nn.Module): def __init__( self, weight, bias, ) -> None: super().__init__() self.weight = torch.nn.Parameter(weight) if bias is not None: self.bias = torch.nn.Parameter(bias) else: self.bias = None self.cu_count = torch.cuda.get_device_properties( device="cuda" ).multi_processor_count self.use_skinny_gemm = ( ROCM_USE_SKINNY_GEMM and "gfx1" not in torch.cuda.get_device_properties("cuda").gcnArchName ) @classmethod def load(cls, config, prefix: str, weights, bias: bool): weight = weights.get_tensor(f"{prefix}.weight") if bias: bias = weights.get_tensor(f"{prefix}.bias") else: bias = None return cls(weight, bias) def forward(self, inp: torch.Tensor) -> torch.Tensor: weight = self.weight bias = self.bias if ( self.use_skinny_gemm and inp.dtype == torch.float16 and inp.shape[-1] % 8 == 0 ): batched = False inp_shape = inp.shape if inp.dim() == 3: inp = inp.view(-1, inp_shape[-1]) batched = True m, n, k = weight.shape[0], inp_shape[0], inp_shape[1] if m > 8 and n <= 4: out = torch.empty( inp_shape[0], weight.shape[0], dtype=inp.dtype, device=weight.device ) ops.wvSpltK(weight, inp, out, n, self.cu_count) elif m % 4 == 0 and n == 1 and k <= 8192: out = torch.empty( inp_shape[0], weight.shape[0], dtype=inp.dtype, device=weight.device ) ops.LLMM1(weight, inp, out, 4) else: out = F.linear(inp, weight) if batched: out.view(*inp_shape[:-1], out.shape[-1]) if bias is not None: out = out + bias return out return F.linear(inp, self.weight, self.bias) def get_linear(weight, bias): # Weights that are loaded through methods that are not # quantization-aware are still bare tensors. We may want # to change this in the future. if isinstance(weight, torch.Tensor): if SYSTEM == "rocm": return FastLinearROCm(weight, bias) else: return FastLinear(weight, bias) return weight.get_linear(bias)
text-generation-inference/server/text_generation_server/layers/linear.py/0
{ "file_path": "text-generation-inference/server/text_generation_server/layers/linear.py", "repo_id": "text-generation-inference", "token_count": 1954 }
import torch from torch.nn import functional as F from typing import Iterable, List from text_generation_server.layers.linear import get_linear, FastLinear from text_generation_server.utils.import_utils import SYSTEM if SYSTEM == "ipex": import intel_extension_for_pytorch as ipex class LayerConcat(torch.nn.Module): """ Apply multiple layers to the input and concatenate their outputs. """ def __init__(self, layers: Iterable[torch.nn.Module], dim: int = -1): """ `dim` is the dimension along which layer outputs are concatenated. """ super().__init__() self.layers = layers self.dim = dim def forward(self, x: torch.Tensor): outputs = [layer(x) for layer in self.layers] return torch.cat(outputs, self.dim) class SuperLayer(torch.nn.Module): def __init__(self, linear): super().__init__() self.linear = linear def forward(self, x): return self.linear.forward(x) class TensorParallelHead(SuperLayer): def __init__(self, linear, process_group, should_gather: bool): super().__init__(linear) self.process_group = process_group self.should_gather = should_gather @staticmethod def load(config, prefix: str, weights): if config.quantize == "exl2": try: # If the piece and LM head embeddings are shared, we have # non-quantized weights... weight = weights.get_tensor(f"{prefix}.weight") except Exception: # ...otherwise they are quantized. weight = weights.get_weights_col(prefix) should_gather = weights.process_group.size() > 1 elif weights.process_group.size() > 1: try: weight = weights.get_sharded(f"{prefix}.weight", dim=0) should_gather = True except AssertionError: # If the vocab size is not divisible by number of shards # just load the entire thing. weight = weights.get_tensor(f"{prefix}.weight") should_gather = False else: weight = weights.get_tensor(f"{prefix}.weight") should_gather = False return TensorParallelHead( get_linear(weight, bias=None), process_group=weights.process_group, should_gather=should_gather, ) def forward(self, input: torch.Tensor) -> torch.Tensor: if not self.should_gather: return super().forward(input) world_size = self.process_group.size() if len(input.shape) == 2 and isinstance(self.linear, FastLinear): out_dim = self.linear.weight.shape[0] if input.shape[0] == 1: world_out = input.new_empty(1, out_dim * world_size) local_out = input.new_empty(1, out_dim) gather_input = local_out else: world_out = input.new_empty(out_dim * world_size, input.shape[0]) gather_input = input.new_empty(out_dim, input.shape[0]) local_out = gather_input.T torch.mm(input, self.linear.weight.T, out=local_out) if SYSTEM == "ipex": ipex.distributed.all_gather_into_tensor( world_out, gather_input, group=self.process_group ) else: torch.distributed.all_gather_into_tensor( world_out, gather_input, group=self.process_group ) if input.shape[0] == 1: return world_out return world_out.T output = super().forward(input) world_output = [ torch.empty_like(output) for _ in range(self.process_group.size()) ] if SYSTEM == "ipex": ipex.distributed.all_gather(world_output, output, group=self.process_group) else: torch.distributed.all_gather(world_output, output, group=self.process_group) world_output = torch.cat(world_output, dim=-1) return world_output class TensorParallelColumnLinear(SuperLayer): @classmethod def load_gate_up(cls, config, prefix: str, weights, bias: bool): """Specific method when the QKV was joined after the fact""" weight = weights.get_weights_col_packed_gate_up(prefix) if bias: raise NotImplementedError("packed_gate_up only implemented without bias") else: bias = None linear = get_linear(weight, bias) return cls(linear) @classmethod def load_qkv( cls, config, prefix: str, weights, bias: bool, num_heads: int, num_key_value_heads: int, ): """Specific method when the QKV was joined after the fact""" weight = weights.get_weights_col_packed_qkv( prefix, num_heads=num_heads, num_key_value_heads=num_key_value_heads, ) if bias: raise NotImplementedError("packed_qkv only implemented for baichuan") else: bias = None linear = get_linear(weight, bias) return cls(linear) @classmethod def load(cls, config, prefix: str, weights, bias: bool): weight = weights.get_weights_col(prefix) if bias: bias = weights.get_sharded(f"{prefix}.bias", dim=0) else: bias = None linear = get_linear(weight, bias) return cls(linear) @classmethod def load_multi(cls, config, prefixes: List[str], weights, bias: bool, dim: int): if config.quantize == "exl2": linears = [] for prefix in prefixes: weight = weights.get_weights_col(prefix) b = weights.get_tensor(f"{prefix}.bias") if bias else None linears.append(get_linear(weight, b)) linear = LayerConcat(linears) else: weight = weights.get_multi_weights_col(prefixes, dim=dim) if bias: b = [weights.get_sharded(f"{p}.bias", dim=0) for p in prefixes] bias = torch.cat(b, dim=dim) else: bias = None linear = get_linear(weight, bias) return cls(linear) class TensorParallelRowLinear(SuperLayer): def __init__(self, linear, process_group): super().__init__(linear) self.process_group = process_group @classmethod def load(cls, config, prefix: str, weights, bias: bool): weight = weights.get_weights_row(prefix) if bias and weights.process_group.rank() == 0: # Rank is only on the first rank process bias = weights.get_tensor(f"{prefix}.bias") else: bias = None return cls( get_linear(weight, bias), process_group=weights.process_group, ) def forward(self, input: torch.Tensor, reduce: bool = True) -> torch.Tensor: out = super().forward(input) if self.process_group.size() > 1 and reduce: if SYSTEM == "ipex": ipex.distributed.all_reduce(out, group=self.process_group) else: torch.distributed.all_reduce(out, group=self.process_group) return out class TensorParallelEmbedding(torch.nn.Module): def __init__(self, prefix: str, weights, reduce=True): super().__init__() weight = weights.get_partial_sharded(f"{prefix}.weight", dim=0) num_embeddings = weights.get_shape(f"{prefix}.weight")[0] process_group = weights.process_group world_size = process_group.size() rank = process_group.rank() block_size = (num_embeddings + world_size - 1) // world_size self.min_id = rank * block_size self.max_id = min(num_embeddings, (rank + 1) * block_size) self.null_idx = weight.shape[ 0 ] # Usually block_size, might be less in non even vocab_size. self.process_group = weights.process_group self.reduce = reduce """Additional 0 entry used for masking""" self.weight = torch.nn.Parameter(F.pad(weight, (0, 0, 0, 1))) def forward(self, input: torch.Tensor) -> torch.Tensor: # default all out of bounds values to `self.null_idx` that will then be mapped to 0 # translate for [0, self.max_id - self.min_id[ input = torch.where( (self.min_id > input) | (input >= self.max_id), self.null_idx, input - self.min_id, ) out = torch.nn.functional.embedding(input, self.weight) if self.reduce and self.process_group.size() > 1: if SYSTEM == "ipex": ipex.distributed.all_reduce(out, group=self.process_group) else: torch.distributed.all_reduce(out, group=self.process_group) return out
text-generation-inference/server/text_generation_server/layers/tensor_parallel.py/0
{ "file_path": "text-generation-inference/server/text_generation_server/layers/tensor_parallel.py", "repo_id": "text-generation-inference", "token_count": 4175 }
# coding=utf-8 # Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved. # # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX # and OPT implementations in this library. It has been modified from its # original forms to accommodate minor architectural differences compared # to GPT-NeoX and OPT used by the Meta AI team that trained the model. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch import torch.distributed from torch import nn from transformers.activations import ACT2FN from transformers.configuration_utils import PretrainedConfig from typing import Optional, List, Tuple from text_generation_server.layers.attention.kv_cache import get_kv_scales from text_generation_server.utils.import_utils import SYSTEM from text_generation_server.layers.attention import ( paged_attention, attention, Seqlen, ) from text_generation_server.layers import ( TensorParallelRowLinear, TensorParallelColumnLinear, TensorParallelEmbedding, SpeculativeHead, TensorParallelMultiAdapterLinear, TensorParallelAdapterRowLinear, ) from text_generation_server.layers.rotary import PositionRotaryEmbedding from text_generation_server.layers.layernorm import ( FastRMSNorm, ) if SYSTEM == "rocm": try: import vllm._custom_ops as ops except Exception as e: raise ImportError(f"Could not load `vllm._custom_ops`. Full error: {e}") class MistralConfig(PretrainedConfig): model_type = "mistral" def __init__( self, vocab_size=32000, hidden_size=4096, intermediate_size=14336, num_hidden_layers=32, num_attention_heads=32, num_key_value_heads=8, hidden_act="silu", max_position_embeddings=4096 * 32, initializer_range=0.02, rms_norm_eps=1e-6, use_cache=True, pad_token_id=None, bos_token_id=1, eos_token_id=2, pretraining_tp=1, tie_word_embeddings=False, rope_theta=10000.0, sliding_window=None, **kwargs, ): self.vocab_size = vocab_size self.max_position_embeddings = max_position_embeddings self.hidden_size = hidden_size self.intermediate_size = intermediate_size self.num_hidden_layers = num_hidden_layers self.num_attention_heads = num_attention_heads self.sliding_window = sliding_window # for backward compatibility if num_key_value_heads is None: num_key_value_heads = num_attention_heads self.num_key_value_heads = num_key_value_heads self.hidden_act = hidden_act self.initializer_range = initializer_range self.rms_norm_eps = rms_norm_eps self.pretraining_tp = pretraining_tp self.use_cache = use_cache self.rope_theta = rope_theta super().__init__( pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, tie_word_embeddings=tie_word_embeddings, **kwargs, ) class MistralAttention(torch.nn.Module): def __init__(self, prefix: str, config, weights, layer_id): super().__init__() self.max_past = ( config.sliding_window if config.sliding_window is not None else -1 ) self.num_heads = config.num_attention_heads self.hidden_size = config.hidden_size if hasattr(config, "head_dim"): self.head_size = config.head_dim else: self.head_size = self.hidden_size // self.num_heads self.rotary_emb = PositionRotaryEmbedding.static( config=config, dim=self.head_size, base=config.rope_theta, device=weights.device, ) self.softmax_scale = self.head_size**-0.5 if self.num_heads % weights.process_group.size() != 0: raise ValueError( f"`num_heads` must be divisible by `num_shards` (got `num_heads`: {self.num_heads} " f"and `num_shards`: {weights.process_group.size()}" ) self.num_heads = self.num_heads // weights.process_group.size() self.num_key_value_heads = ( config.num_key_value_heads // weights.process_group.size() ) query_key_value = TensorParallelColumnLinear.load_multi( config, prefixes=[f"{prefix}.q_proj", f"{prefix}.k_proj", f"{prefix}.v_proj"], dim=0, weights=weights, bias=False, ) self.query_key_value = TensorParallelMultiAdapterLinear.load( query_key_value, layer_id, ["q_proj", "k_proj", "v_proj"], sizes=[ self.head_size * config.num_attention_heads, self.head_size * config.num_key_value_heads, self.head_size * config.num_key_value_heads, ], process_group=weights.process_group, ) self.kv_scales = get_kv_scales(weights, f"{prefix}") o_proj = TensorParallelRowLinear.load( config, prefix=f"{prefix}.o_proj", weights=weights, bias=False, ) self.o_proj = TensorParallelAdapterRowLinear.load( o_proj, layer_id, "o_proj", process_group=weights.process_group, ) self.num_groups = self.num_heads // self.num_key_value_heads self.kv_head_mapping = torch.arange( 0, self.num_key_value_heads, dtype=torch.int32, device=weights.device ).repeat_interleave(self.num_groups) def forward( self, hidden_states, cos, sin, cu_seqlen_prefill, kv_cache, block_tables, slots, seqlen, max_s, prefill_cache_indices, adapter_data, ): qkv = self.query_key_value(hidden_states, adapter_data) query, kv = qkv.split( [ self.head_size * self.num_heads, 2 * self.head_size * self.num_key_value_heads, ], dim=1, ) query = query.view(-1, self.num_heads, self.head_size) kv = kv.view(-1, 2, self.num_key_value_heads, self.head_size) self.rotary_emb(query, torch.select(kv, dim=1, index=0), cos, sin) if prefill_cache_indices is not None: kv_to_cache = kv[prefill_cache_indices] else: kv_to_cache = kv kv_cache.store( key=kv_to_cache[:, 0], value=kv_to_cache[:, 1], slots=slots, kv_scales=self.kv_scales, ) # Prefill if cu_seqlen_prefill is not None: # flash attention attn_output = attention( query=query, key=kv_to_cache[:, 0], value=kv_to_cache[:, 1], kv_cache=kv_cache, kv_scales=self.kv_scales, seqlen=seqlen, block_tables=block_tables, softmax_scale=self.softmax_scale, window_size_left=self.max_past, ) # Decode else: attn_output = paged_attention( query, kv_cache, self.kv_head_mapping, self.softmax_scale, block_tables, seqlen, max_s, kv_scales=self.kv_scales, ) return self.o_proj( attn_output.view(-1, self.num_heads * self.head_size), adapter_data ) class MistralMLP(nn.Module): def __init__(self, prefix: str, config, weights, layer_id): super().__init__() self.hidden_act = config.hidden_act self.act = ( ACT2FN[self.hidden_act] if "gelu" not in self.hidden_act else lambda x: torch.nn.functional.gelu( x, approximate=( "tanh" if self.hidden_act in ["gelu_fast", "gelu_pytorch_tanh"] else "none" ), ) ) # Fuse gate and up proj gate_up_proj = TensorParallelColumnLinear.load_multi( config, prefixes=[f"{prefix}.gate_proj", f"{prefix}.up_proj"], weights=weights, dim=0, bias=False, ) self.gate_up_proj = TensorParallelMultiAdapterLinear.load( gate_up_proj, layer_id, ["gate_proj", "up_proj"], sizes=[ config.intermediate_size, config.intermediate_size, ], process_group=weights.process_group, ) down_proj = TensorParallelRowLinear.load( config, prefix=f"{prefix}.down_proj", weights=weights, bias=False, ) self.down_proj = TensorParallelAdapterRowLinear.load( down_proj, layer_id, "down_proj", process_group=weights.process_group, ) self.intermediate_size = ( config.intermediate_size // weights.process_group.size() ) # TODO: This is a hotfix to be removed & properly refactored. self.quantize = config.quantize def forward(self, hidden_states, adapter_data): if ( SYSTEM == "rocm" and self.hidden_act == "silu" and hidden_states.dtype == torch.float16 and hidden_states.shape[0] == 1 and not self.quantize ): out = torch.empty( hidden_states.shape[0], self.intermediate_size, dtype=hidden_states.dtype, device="cuda", ) ops.LLMM_Silu( self.gate_up_proj.base_layer.linear.weight, hidden_states, out, 8 ) return self.down_proj(out, adapter_data) else: gate_up_states = self.gate_up_proj(hidden_states, adapter_data) gate_up_states = gate_up_states.view(-1, 2, self.intermediate_size) return self.down_proj( self.act(gate_up_states[:, 0]) * gate_up_states[:, 1], adapter_data ) class MistralLayer(nn.Module): def __init__(self, prefix: str, config, weights, layer_id): super().__init__() self.self_attn = MistralAttention( prefix=f"{prefix}.self_attn", config=config, weights=weights, layer_id=layer_id, ) self.mlp = MistralMLP( prefix=f"{prefix}.mlp", config=config, weights=weights, layer_id=layer_id ) self.input_layernorm = FastRMSNorm.load( prefix=f"{prefix}.input_layernorm", weights=weights, eps=config.rms_norm_eps ) self.post_attention_layernorm = FastRMSNorm.load( prefix=f"{prefix}.post_attention_layernorm", weights=weights, eps=config.rms_norm_eps, ) def forward( self, hidden_states, residual, cos, sin, cu_seqlen_prefill, kv_cache, block_tables, slots, seqlen, max_s, prefill_cache_indices, adapter_data, ): normed_hidden_states, res = self.input_layernorm(hidden_states, residual) # Self Attention attn_output = self.self_attn( normed_hidden_states, cos, sin, cu_seqlen_prefill, kv_cache, block_tables, slots, seqlen, max_s, prefill_cache_indices, adapter_data, ) # faster post attention rms norm normed_attn_res_output, attn_res = self.post_attention_layernorm( attn_output, res ) mlp_output = self.mlp(normed_attn_res_output, adapter_data) return mlp_output, attn_res class MistralModel(torch.nn.Module): def __init__(self, prefix: str, config, weights): super().__init__() process_group = weights.process_group self.tp_rank = process_group.rank() self.tp_world_size = process_group.size() self.layers = nn.ModuleList( [ MistralLayer( prefix=f"{prefix}.layers.{layer_id}", config=config, weights=weights, layer_id=layer_id, ) for layer_id in range(config.num_hidden_layers) ] ) self.norm = FastRMSNorm.load( prefix=f"{prefix}.norm", weights=weights, eps=config.rms_norm_eps ) self.gradient_checkpointing = False self.head_size = self.layers[0].self_attn.head_size self.num_heads = self.layers[0].self_attn.num_heads self.num_key_value_heads = self.layers[0].self_attn.num_key_value_heads def forward( self, inputs_embeds: torch.Tensor, position_ids: torch.Tensor, cu_seqlen_prefill: Optional[torch.Tensor], kv_cache: List[Tuple[torch.Tensor, torch.Tensor]], block_tables: torch.Tensor, slots: torch.Tensor, seqlen: Seqlen, max_s: int, true_max_s: int, prefill_cache_indices: Optional[torch.Tensor], adapter_data: Optional[torch.Tensor] = None, ): hidden_states = inputs_embeds # Get rotary cos and sin for this forward # Avoid to index in each layer cos, sin = self.layers[0].self_attn.rotary_emb.get_cos_sin( position_ids, true_max_s, hidden_states.dtype ) residual = None for i, layer in enumerate(self.layers): hidden_states, residual = layer( hidden_states, residual, cos, sin, cu_seqlen_prefill, kv_cache[i], block_tables, slots, seqlen, max_s, prefill_cache_indices, adapter_data, ) hidden_states, _ = self.norm(hidden_states, residual) return hidden_states class FlashMistralForCausalLM(torch.nn.Module): def __init__(self, prefix: str, config, weights, name=None): if name is None: name = "model" super().__init__() self.embed_tokens = TensorParallelEmbedding( prefix=( f"{name}.embed_tokens" if not prefix else f"{prefix}.{name}.embed_tokens" ), weights=weights, ) self.model = MistralModel( prefix=name if not prefix else f"{prefix}.{name}", config=config, weights=weights, ) self.lm_head = SpeculativeHead.load( config, # TODO dirty hack for idefics2. prefix=( "lm_head" if not prefix or name != "model" else f"{prefix}.lm_head" ), weights=weights, ) self.max_past = config.sliding_window self.max_past_tensor = ( torch.tensor(config.sliding_window, device=weights.device) if self.max_past is not None else None ) def forward( self, input_ids: torch.Tensor, position_ids: torch.Tensor, cu_seqlen_prefill: Optional[torch.Tensor], kv_cache: List[Tuple[torch.Tensor, torch.Tensor]], block_tables: torch.Tensor, slots: torch.Tensor, seqlen: Seqlen, max_s: int, prefill_cache_indices: Optional[torch.Tensor], lm_head_indices: Optional[torch.Tensor] = None, adapter_data: Optional[torch.Tensor] = None, ) -> torch.Tensor: true_max_s = max_s if prefill_cache_indices is not None: # Slots also need to be sliced as it has the same size as the whole kv tensor slots = slots[prefill_cache_indices] elif self.max_past is not None: # Clamp in decode mode as paged attention requires clamped values whereas the flash attention # kernel requires the true values seqlen = seqlen.clamp(max=self.max_past_tensor) inputs_embeds = self.embed_tokens(input_ids) hidden_states = self.model( inputs_embeds, position_ids, cu_seqlen_prefill, kv_cache, block_tables, slots, seqlen, max_s, true_max_s, prefill_cache_indices, adapter_data, ) if lm_head_indices is not None: hidden_states = hidden_states[lm_head_indices] logits = self.lm_head(hidden_states) return logits
text-generation-inference/server/text_generation_server/models/custom_modeling/flash_mistral_modeling.py/0
{ "file_path": "text-generation-inference/server/text_generation_server/models/custom_modeling/flash_mistral_modeling.py", "repo_id": "text-generation-inference", "token_count": 8969 }
# coding=utf-8 # Copyright 2022 The HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ Processor class for IDEFICS. """ from typing import Callable, List, Optional, Union from urllib.parse import urlparse from transformers.feature_extraction_utils import BatchFeature from transformers.processing_utils import ProcessorMixin from transformers.tokenization_utils_base import ( BatchEncoding, PaddingStrategy, TextInput, TruncationStrategy, ) from transformers.utils import TensorType, is_torch_available if is_torch_available(): import torch IMAGE_TOKEN = "<image>" # copied from m4.training.packing def incremental_to_binary_attention_mask(incremental_mask, num_classes=-1): # This function converts: [-1, 0, 1] => [[0, 0], [1, 0], [0, 1]] # If any of images index are more than num_classes, set them to -1. # Words after the max number of images allowed have been seen don't attend on anything if num_classes != -1: incremental_mask[incremental_mask >= num_classes] = -1 negatives = incremental_mask == -1 incremental_mask[negatives] = 0 attn_mask = torch.nn.functional.one_hot(incremental_mask, num_classes=num_classes) attn_mask[negatives, :] = 0 return attn_mask # copied from m4.training.packing def image_attention_mask_for_packed_input_ids(input_ids, tokenizer): image_attention_mask = torch.full_like(input_ids, fill_value=-1) next_image_attention_mask = torch.full_like(input_ids, fill_value=-1) image_token_id = tokenizer.convert_tokens_to_ids(IMAGE_TOKEN) eod_token_id = tokenizer.eos_token_id for batch_idx in range(input_ids.size(0)): count = -1 seen_eod = False for idx, token_id in enumerate(input_ids[batch_idx]): if token_id == image_token_id: count += 1 image_attention_mask[batch_idx][idx] = count seen_eod = False else: image_attention_mask[batch_idx][idx] = count if seen_eod: image_attention_mask[batch_idx][idx] = -1 if token_id == eod_token_id: seen_eod = True for batch_idx in range(input_ids.size(0)): count = -1 seen_eod = False for idx in range(input_ids[batch_idx].size(0) - 1, -1, -1): token_id = input_ids[batch_idx][idx] if token_id == image_token_id: count += 1 next_image_attention_mask[batch_idx][idx] = count seen_eod = False else: next_image_attention_mask[batch_idx][idx] = count if token_id == eod_token_id: seen_eod = True if seen_eod: next_image_attention_mask[batch_idx][idx] = -1 non_negative_indices = next_image_attention_mask[batch_idx] != -1 next_image_attention_mask[batch_idx][non_negative_indices] -= count next_image_attention_mask[batch_idx][non_negative_indices] *= -1 return image_attention_mask, next_image_attention_mask def is_url(string): """Checks if the passed string contains a valid url and nothing else. e.g. if space is included it's immediately invalidated the url""" if " " in string: return False result = urlparse(string) return all([result.scheme, result.netloc]) def is_image(string): """Checks if the passed string contains a valid url and nothing else. e.g. if space is included it's immediately invalidated the url""" return is_url(string) or string.startswith("data:") class IdeficsProcessor(ProcessorMixin): r""" Constructs a IDEFICS processor which wraps a LLama tokenizer and IDEFICS image processor into a single processor. [`IdeficsProcessor`] offers all the functionalities of [`IdeficsImageProcessor`] and [`LlamaTokenizerFast`]. See the docstring of [`~IdeficsProcessor.__call__`] and [`~IdeficsProcessor.decode`] for more information. Args: image_processor (`IdeficsImageProcessor`): An instance of [`IdeficsImageProcessor`]. The image processor is a required input. tokenizer (`LlamaTokenizerFast`): An instance of [`LlamaTokenizerFast`]. The tokenizer is a required input. image_size (`int`, *optional*, defaults to 224): Image size (assuming a square image) """ attributes = ["image_processor", "tokenizer"] image_processor_class = "IdeficsImageProcessor" tokenizer_class = "LlamaTokenizerFast" def __init__( self, image_processor, tokenizer=None, image_size=224, add_end_of_utterance_token=None, **kwargs, ): if image_processor is None: raise ValueError("You need to specify an `image_processor`.") if tokenizer is None: raise ValueError("You need to specify a `tokenizer`.") super().__init__(image_processor, tokenizer) self.current_processor = self.image_processor self.image_token_id = tokenizer.convert_tokens_to_ids(IMAGE_TOKEN) self.default_image_dims = ( self.image_processor.image_num_channels, self.image_processor.image_size, self.image_processor.image_size, ) self.tokenizer_was_trained_with_end_of_utterance_token = ( True if "<end_of_utterance>" in self.tokenizer.special_tokens_map.get("additional_special_tokens", []) else False ) def __call__( self, prompts: Union[List[TextInput], List[List[TextInput]]], padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, max_length: Optional[int] = None, transform: Callable = None, add_eos_token=False, add_end_of_utterance_token=None, debug=False, return_tensors: Optional[Union[str, TensorType]] = TensorType.PYTORCH, ) -> BatchEncoding: """This method takes batched or non-batched prompts made of text and images and converts them into prompts that the model was trained on and prepares the image pixel values for the model to process. Args: prompts (`Union[List[TextInput], [List[List[TextInput]]]]`): either a single prompt or a batched list of prompts - see the detailed description immediately after the end of the arguments doc section. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): Select a strategy to pad the returned sequences (according to the model's padding side and padding index) among: - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single sequence if provided). - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different lengths). max_length (`int`, *optional*): Maximum length of the returned list and optionally padding length (see above). truncation (`bool`, *optional*): Activates truncation to cut input sequences longer than `max_length` to `max_length`. transform (`Callable`, *optional*): A custom transform function that accepts a single image can be passed for training. For example, `torchvision.Compose` can be used to compose multiple functions. If `None` a preset inference-specific set of transforms will be applied to the images add_eos_token (`bool`, *optional*, defaults to `False`): Adds `eos_token` at the end of the final prompt if True` add_end_of_utterance_token (`bool`, *optional*) Whether to automatically add `<end_of_utterance>` after each prompt's text input (unless followed by an image). If `None` the tokenizer will be checked instead and if this token is found in `additional_special_tokens` then the value will be `True`. debug (`bool`, *optional*, defaults to `False`): `True` value will help debug prompt generation by dumping useful information return_tensors (`str` or `TensorType`, *optional*, defaults to `TensorType.PYTORCH`): The type of tensors to return. Can be one of: - `TensorType.PYTORCH` or `'pt'`: Return a batch of type `torch.Tensor`. Returns: a dict with entries: `input_ids`, `attention_mask`, `pixel_values`, `image_attention_mask` which can be directly passed to `model.generate` Detailed explanation: Each entry in `prompts` is either a text to be passed as is or an image that will be processed. An image can be either an image object (`PIL.Image`) or a url from which the image can be retrieved. When the processor encounters an image it'll inject `<fake_token_around_image><image><fake_token_around_image>` entry into the prompt. Example: ```python checkpoint = "HuggingFaceM4/idefics-9b" processor = AutoProcessor.from_pretrained(checkpoint) url = "https://hips.hearstapps.com/hmg-prod/images/cute-photos-of-cats-in-grass-1593184777.jpg" img = processor.image_processor.fetch_images([url])[0] prompts = [ "User:", img, "Describe this image.\nAssistant: An image of two kittens in grass.\n", "User:", "https://hips.hearstapps.com/hmg-prod/images/dog-puns-1581708208.jpg", "Describe this image.\nAssistant:", ] inputs = processor(prompts, return_tensors="pt") generated_ids = model.generate(**inputs, max_length=100) generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] ``` In this example the `prompts` will be converted into: ``` <s>User:<fake_token_around_image><image><fake_token_around_image>Describe this image. Assistant: An image of two kittens in grass. User:<fake_token_around_image><image><fake_token_around_image>Describe this image. Assistant:' ``` and the two images will be massaged using [`IdeficsImageProcessor.__call__`] method and placed inside the `pixel_values` dict entry of the return value. This example also examplifies that images can be passed as objects or as text urls. It can be seen that the first image is passed as object and the second one as a url. To do training do: ```python image_transform = transforms.Compose( [ transforms.RandomResizedCrop( (w, h), scale=(0.9, 1.0), interpolation=transforms.InterpolationMode.BICUBIC ), transforms.ToTensor(), transforms.Normalize(mean=self.image_mean, std=self.image_std), ] ) inputs = processor(prompts, transform=image_transform, return_tensors="pt") ``` In order to help debug prompt generation enable `debug=True` which will show you what's happening. """ # if the value isn't overriden by the user, check if the tokenizer was trained with this token and then use it if add_end_of_utterance_token is None: add_end_of_utterance_token = ( self.tokenizer_was_trained_with_end_of_utterance_token ) # turn non-batched prompts into batched if not any(isinstance(i, list) for i in prompts): prompts = [prompts] fake_token = "<fake_token_around_image>" image_token = "<image>" end_of_utterance_token = "<end_of_utterance>" def image_tokens(last_was_image): if last_was_image: return image_token + fake_token else: return fake_token + image_token + fake_token all_texts = [] all_images = [] for sample in prompts: # the model was trained on samples starting with <s> full_text = f"{self.tokenizer.bos_token}" # an image can either be an image object in the item or the url, everything else is a verbatim prompt text image_objects = [] last_was_image = False last_was_text = False for i, item in enumerate(sample): if i > 0: last_was_text = True if not last_was_image else False if isinstance(item, str): item = item.strip(" ") if is_image(item): image = self.image_processor.fetch_images(item) full_text += image_tokens(last_was_image) image_objects.append(image) last_was_image = True else: # we add end_of_utterance_token between each subsequent text prompts (but not at the last one!) if add_end_of_utterance_token and last_was_text: full_text += end_of_utterance_token full_text += item last_was_image = False else: # must be an image obj full_text += image_tokens(last_was_image) image_objects.append(item) last_was_image = True if add_eos_token: full_text += self.tokenizer.eos_token if debug is True: print(f"{full_text=}") image_objects = self.image_processor(image_objects, transform=transform) text_encoding = self.tokenizer( text=full_text, add_special_tokens=False, padding=padding, truncation=truncation, max_length=max_length, ) all_texts.append(text_encoding["input_ids"]) all_images.append(image_objects) max_seq_len = max(len(x) for x in all_texts) # max_num_images has to be at least 1 even when there are no images max_num_images = max(len(x) for x in all_images) max_num_images = max(1, max_num_images) at_least_one_image = sum(len(x) for x in all_images) > 0 output_input_ids = [] output_images = [] output_attention_masks = [] for text, images in zip(all_texts, all_images): padded_input_ids = [self.tokenizer.pad_token_id] * max_seq_len unpadded_seq_len = len(text) start = max_seq_len - unpadded_seq_len padded_input_ids[start:] = text[:max_seq_len] attention_mask = torch.zeros((max_seq_len,), dtype=torch.long) attention_mask[start:] = 1 image_count = padded_input_ids.count(self.image_token_id) local_max_num_images = min(image_count, max_num_images) current_images = images[:local_max_num_images] if len(current_images) > 0: padded_image_tensor = torch.zeros( max_num_images, *current_images.size()[1:] ) padded_image_tensor[: current_images.size(0)] = current_images else: padded_image_tensor = torch.zeros( max_num_images, *self.default_image_dims ) output_images.append(padded_image_tensor) output_input_ids.append(torch.tensor(padded_input_ids)) output_attention_masks.append(attention_mask) output_input_ids = torch.stack(output_input_ids) output_images = torch.stack(output_images) output_attention_masks = torch.stack(output_attention_masks) if at_least_one_image: image_attention_mask, _ = image_attention_mask_for_packed_input_ids( output_input_ids, self.tokenizer ) image_attention_mask = incremental_to_binary_attention_mask( image_attention_mask, num_classes=max_num_images ) else: # in full language mode we set the image mask to all-0s image_attention_mask = torch.zeros( output_input_ids.shape[0], output_input_ids.shape[1], 1, dtype=torch.bool, ) return BatchFeature( data={ "input_ids": output_input_ids, "attention_mask": output_attention_masks, "pixel_values": output_images, "image_attention_mask": image_attention_mask, } ) def batch_decode(self, *args, **kwargs): """ This method forwards all its arguments to LlamaTokenizerFast's [`~PreTrainedTokenizer.batch_decode`]. Please refer to the docstring of this method for more information. """ return self.tokenizer.batch_decode(*args, **kwargs) def decode(self, *args, **kwargs): """ This method forwards all its arguments to LlamaTokenizerFast's [`~PreTrainedTokenizer.decode`]. Please refer to the docstring of this method for more information. """ return self.tokenizer.decode(*args, **kwargs) @property def model_input_names(self): tokenizer_input_names = self.tokenizer.model_input_names image_processor_input_names = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))
text-generation-inference/server/text_generation_server/models/custom_modeling/idefics_processing.py/0
{ "file_path": "text-generation-inference/server/text_generation_server/models/custom_modeling/idefics_processing.py", "repo_id": "text-generation-inference", "token_count": 8120 }
from io import BytesIO from PIL import Image import torch import time from dataclasses import dataclass from opentelemetry import trace from transformers import ( AutoConfig, AutoProcessor, AutoTokenizer, PreTrainedTokenizerBase, ProcessorMixin, ) from typing import Optional, Tuple, List, Type, Dict from text_generation_server.models import Model from text_generation_server.models.types import ( Batch, Tokens, Generation, GeneratedText, ) from text_generation_server.pb import generate_pb2 from text_generation_server.utils import NextTokenChooser, StoppingCriteria, Sampling import torch.distributed from text_generation_server.models.custom_modeling.idefics_modeling import ( IdeficsForVisionText2Text, ) from text_generation_server.utils import ( initialize_torch_distributed, weight_files, Weights, ) from text_generation_server.utils.quantization import get_loader from text_generation_server.utils.import_utils import SYSTEM tracer = trace.get_tracer(__name__) @dataclass class IdeficsCausalLMBatch(Batch): batch_id: int requests: List[generate_pb2.Request] requests_idx_mapping: Dict[int, int] # Decoder values input_ids: torch.Tensor attention_mask: torch.Tensor position_ids: torch.Tensor pixel_values: Optional[torch.Tensor] image_hidden_states: Optional[torch.Tensor] image_attention_mask: Optional[torch.Tensor] past_key_values: Optional[List[Tuple]] # All tokens all_input_ids: List[torch.Tensor] # Lengths of all generations present in the batch input_lengths: List[int] prefix_offsets: List[int] read_offsets: List[int] # Generation helpers next_token_choosers: List[NextTokenChooser] stopping_criterias: List[StoppingCriteria] # Metadata used for padding max_input_length: int padding_right_offset: int # Maximum number of tokens this batch will grow to max_tokens: int # Past metadata keys_head_dim_last: bool = True def to_pb(self) -> generate_pb2.CachedBatch: return generate_pb2.CachedBatch( id=self.batch_id, request_ids=[r.id for r in self.requests], size=len(self), max_tokens=self.max_tokens, current_tokens=len(self), ) @classmethod def from_pb( cls, pb: generate_pb2.Batch, tokenizer: PreTrainedTokenizerBase, dtype: torch.dtype, device: torch.device, ) -> "IdeficsCausalLMBatch": raise NotImplementedError @classmethod def from_pb_processor( cls, pb: generate_pb2.Batch, tokenizer: PreTrainedTokenizerBase, processor: ProcessorMixin, # Hack config, dtype: torch.dtype, device: torch.device, ) -> "IdeficsCausalLMBatch": inputs = [] next_token_choosers = [] stopping_criterias = [] prefix_offsets = [] read_offsets = [] requests_idx_mapping = {} # Parse batch max_truncation = 0 padding_right_offset = 0 max_decode_tokens = 0 for i, r in enumerate(pb.requests): requests_idx_mapping[r.id] = i inputs.append(r.input_chunks.chunks) next_token_choosers.append( NextTokenChooser.from_pb(r.parameters, device, tokenizer) ) stopping_criteria = StoppingCriteria.from_pb( r.stopping_parameters, tokenizer ) stopping_criterias.append(stopping_criteria) max_truncation = max(max_truncation, r.truncate) max_decode_tokens += stopping_criteria.max_new_tokens padding_right_offset = max( padding_right_offset, stopping_criteria.max_new_tokens ) # TODO Check impact on idefics prompts = [] for inp in inputs: # Each input is encoded into a list, where each element of this input list is either a string or a URL prompt = [] for chunk in inp: chunk_type = chunk.WhichOneof("chunk") if chunk_type == "text": prompt.append(chunk.text) elif chunk_type == "image": image = Image.open(BytesIO(chunk.image.data)) prompt.append(image) else: raise RuntimeError(f"Invalid chunk type {chunk_type}") prompts.append(prompt) # The processor replaces the call to tokenizer, and # a/ takes care of fetching images from the URL # b/ generate the correct input_ids, attention_mask, pixel_values, image_attention_mask to feed to the model tokenized_inputs = processor( prompts, return_tensors="pt", padding=True, truncation=True, max_length=max_truncation, # TODO Check impact on idefics # add_end_of_utterance_token=False, # Already taken care of inside the prompts, so bypassing the processor's handling of this token ).to(device) for _ in pb.requests: input_len = tokenized_inputs["input_ids"].shape[1] prefix_offsets.append( input_len - 5 ) # To decode without potential fallbacks errors read_offsets.append( input_len ) # To decode without potential fallbacks errors input_lengths = tokenized_inputs["attention_mask"].sum(1) max_input_length = input_lengths.max() input_ids = tokenized_inputs["input_ids"] pixel_values = tokenized_inputs.get("pixel_values", None) image_hidden_states = None # Allocate maximum attention_mask attention_mask = input_ids.new_zeros( (pb.size, max_input_length + padding_right_offset) ) # Copy tokenizer attention_mask into fully allocated attention_mask attention_mask[:, :max_input_length] = tokenized_inputs["attention_mask"] # Do the same for image_attention_mask if pixel_values is None: image_attention_mask = None else: image_attention_mask = input_ids.new_zeros( ( pb.size, max_input_length + padding_right_offset, pixel_values.size(1), ) ) image_attention_mask[:, :max_input_length, :] = tokenized_inputs[ "image_attention_mask" ] position_ids = tokenized_inputs["attention_mask"].long().cumsum(-1) - 1 position_ids.masked_fill_(tokenized_inputs["attention_mask"] == 0, 1) all_input_ids = tokenized_inputs["input_ids"].T.split( 1, dim=1 ) # It's input_ids but splitted into a tuple of tensors where each tensor is (seq_len, 1) size. It is then transformed into a list max_tokens = len(inputs) * (max_input_length + max_decode_tokens) return cls( batch_id=pb.id, requests=pb.requests, requests_idx_mapping=requests_idx_mapping, input_ids=input_ids, attention_mask=attention_mask, position_ids=position_ids, pixel_values=pixel_values, image_hidden_states=image_hidden_states, image_attention_mask=image_attention_mask, past_key_values=None, all_input_ids=list(all_input_ids), input_lengths=input_lengths.tolist(), prefix_offsets=prefix_offsets, read_offsets=read_offsets, next_token_choosers=next_token_choosers, stopping_criterias=stopping_criterias, max_input_length=max_input_length.item(), padding_right_offset=padding_right_offset, max_tokens=max_tokens, ) @tracer.start_as_current_span("filter") def filter(self, request_ids: List[int]) -> Optional["IdeficsCausalLMBatch"]: # It deletes requests from the batch. For instance when client lost connection if len(request_ids) == 0: raise ValueError("Batch must have at least one request") if len(request_ids) == len(self): return self keep_indices = [] # New values after filtering requests_idx_mapping = {} requests = [] input_lengths = [] prefix_offsets = [] read_offsets = [] all_input_ids = [] max_input_length = 0 next_token_choosers = [] stopping_criterias = [] total_remaining_decode_tokens = 0 new_padding_right_offset = 0 for i, request_id in enumerate(request_ids): idx = self.requests_idx_mapping[request_id] requests_idx_mapping[request_id] = i keep_indices.append(idx) requests.append(self.requests[idx]) prefix_offsets.append(self.prefix_offsets[idx]) read_offsets.append(self.read_offsets[idx]) all_input_ids.append(self.all_input_ids[idx]) request_input_length = self.input_lengths[idx] input_lengths.append(request_input_length) max_input_length = max(max_input_length, request_input_length) next_token_choosers.append(self.next_token_choosers[idx]) stopping_criteria = self.stopping_criterias[idx] stopping_criterias.append(stopping_criteria) remaining_decode_tokens = ( stopping_criteria.max_new_tokens - stopping_criteria.current_tokens ) total_remaining_decode_tokens += remaining_decode_tokens new_padding_right_offset = max( new_padding_right_offset, remaining_decode_tokens ) # Apply indices to input_ids, attention mask, past key values and other items that need to be cached input_ids = self.input_ids[keep_indices] position_ids = self.position_ids[keep_indices] self.attention_mask = self.attention_mask[ keep_indices, -(self.padding_right_offset + max_input_length) : ( self.attention_mask.shape[1] - self.padding_right_offset ) + new_padding_right_offset, ] # Do the same for pixel_values and image_attention_mask pixel_values = self.pixel_values[keep_indices] self.image_attention_mask = self.image_attention_mask[ keep_indices, -(self.padding_right_offset + max_input_length) : ( self.image_attention_mask.shape[1] - self.padding_right_offset ) + new_padding_right_offset, :, ] if self.image_hidden_states is None: image_hidden_states = None else: image_hidden_states = self.image_hidden_states[keep_indices] # Ensure that past_key_values tensors can be updated in-place if type(self.past_key_values[0]) is tuple: self.past_key_values = [list(layer) for layer in self.past_key_values] # Update tensors in-place to allow incremental garbage collection past_kv_length = max_input_length - 1 for layer in self.past_key_values: past_keys, past_values = layer if len(past_keys.shape) == 3: # Force past to be of dim [self_size, num_heads, ...] for easy indexing past_keys = past_keys.view(len(self), -1, *past_keys.shape[-2:]) past_values = past_values.view(len(self), -1, *past_values.shape[-2:]) if self.keys_head_dim_last: layer[0] = past_keys[keep_indices, :, -past_kv_length:, :] else: layer[0] = past_keys[keep_indices, :, :, -past_kv_length:] del past_keys layer[1] = past_values[keep_indices, :, -past_kv_length:, :] del past_values max_tokens = len(request_ids) * max_input_length + total_remaining_decode_tokens self.requests = requests self.requests_idx_mapping = requests_idx_mapping self.input_ids = input_ids self.pixel_values = pixel_values self.image_hidden_states = image_hidden_states self.position_ids = position_ids self.all_input_ids = all_input_ids self.input_lengths = input_lengths self.prefix_offsets = prefix_offsets self.read_offsets = read_offsets self.next_token_choosers = next_token_choosers self.stopping_criterias = stopping_criterias self.max_input_length = max_input_length self.padding_right_offset = new_padding_right_offset self.max_tokens = max_tokens return self @classmethod @tracer.start_as_current_span("concatenate") def concatenate( cls, batches: List["IdeficsCausalLMBatch"] ) -> "IdeficsCausalLMBatch": # It adds new requests to the batch # Used for padding total_batch_size = 0 max_input_length = 0 max_num_images = 0 padding_right_offset = 0 for batch in batches: total_batch_size += len(batch) max_input_length = max(max_input_length, batch.max_input_length) max_num_images = max(max_num_images, batch.pixel_values.size(1)) padding_right_offset = max(padding_right_offset, batch.padding_right_offset) # Batch attributes requests = [] requests_idx_mapping = {} input_lengths = [] prefix_offsets = [] read_offsets = [] all_input_ids = [] next_token_choosers = [] stopping_criterias = [] max_tokens = 0 # Batch tensors input_ids = None attention_mask = None position_ids = None pixel_values = None image_hidden_states = None image_attention_mask = None past_key_values = [] # Used for slicing correctly inside the tensors # Equivalent to a cumsum on batch sizes start_index = 0 for i, batch in enumerate(batches): requests.extend(batch.requests) input_lengths.extend(batch.input_lengths) prefix_offsets.extend(batch.prefix_offsets) read_offsets.extend(batch.read_offsets) all_input_ids.extend(batch.all_input_ids) next_token_choosers.extend(batch.next_token_choosers) stopping_criterias.extend(batch.stopping_criterias) if i == 0: requests_idx_mapping = batch.requests_idx_mapping else: # We need to offset the mapping for each batch by the cumulative batch size for k, v in batch.requests_idx_mapping.items(): requests_idx_mapping[k] = v + start_index # Slicing end index for this batch end_index = start_index + len(batch) # We only concatenate batches that did at least one step if batch.past_key_values is None: raise ValueError("only concatenate prefilled batches") # Create empty tensor # input_ids is always of shape [batch_size, 1] # We do not need to pad it if input_ids is None: input_ids = batch.input_ids.new_empty((total_batch_size, 1)) # Copy to correct indices input_ids[start_index:end_index] = batch.input_ids # Create padded tensor if attention_mask is None: attention_mask = batch.attention_mask.new_zeros( (total_batch_size, max_input_length + padding_right_offset), ) curr_batch_max_num_images = batch.pixel_values.size(1) if pixel_values is None: pixel_values = batch.pixel_values.new_zeros( (total_batch_size, max_num_images, 3, 224, 224) ) pixel_values[start_index:end_index, :curr_batch_max_num_images] = ( batch.pixel_values ) if image_attention_mask is None: image_attention_mask = batch.image_attention_mask.new_zeros( ( total_batch_size, max_input_length + padding_right_offset, max_num_images, ) ) # We need to slice the attention mask to remove padding from previous steps # and to remove unused allocated space left_offset = max_input_length - batch.max_input_length batch_left_offset = ( batch.attention_mask.shape[1] - batch.max_input_length - batch.padding_right_offset ) attention_mask[ start_index:end_index, left_offset:-padding_right_offset, ] = batch.attention_mask[ :, batch_left_offset : -batch.padding_right_offset, ] image_attention_mask[ start_index:end_index, left_offset:-padding_right_offset, :curr_batch_max_num_images, ] = batch.image_attention_mask[ :, batch_left_offset : -batch.padding_right_offset, : ] # Create empty tensor # position_ids is always of shape [batch_size, 1] if position_ids is None: position_ids = batch.position_ids.new_empty((total_batch_size, 1)) position_ids[start_index:end_index] = batch.position_ids # Shenanigans to get dimensions because BLOOM outputs a past with a different shape # BLOOM Keys: [batch_size * num_heads, head_dim, seq_length] # BLOOM Values: [batch_size * num_heads, seq_length, head_dim] # And ensure that we can update tensors in-place if isinstance(batch.past_key_values[0], tuple): batch.past_key_values = [ [t.view(len(batch), -1, *t.shape[-2:]) for t in layer] for layer in batch.past_key_values ] elif len(batch.past_key_values[0][0].shape) == 3: for layer in batch.past_key_values: for k, t in enumerate(layer): layer[k] = t.view(len(batch), -1, *t.shape[-2:]) # Add eventual padding tokens that were added while concatenating max_tokens += batch.max_tokens + ( max_input_length - batch.max_input_length ) * len(batch) start_index = end_index first_past_kvs = batches[0].past_key_values _, num_heads, padded_sequence_length, head_dim = first_past_kvs[0][1].shape padded_past_values_shape = ( total_batch_size, num_heads, max_input_length - 1, head_dim, ) if batches[0].keys_head_dim_last: padded_past_keys_shape = padded_past_values_shape else: # seq_length is last for BLOOM padded_past_keys_shape = ( total_batch_size, num_heads, head_dim, max_input_length - 1, ) # Iterate over attention layers # Concatenate past key values layer by layer to allow incremental garbage collection for j in range(len(first_past_kvs)): padded_past_keys = first_past_kvs[j][0].new_zeros(padded_past_keys_shape) start_index = 0 for batch in batches: past_keys = batch.past_key_values[j][0] # Clear reference to the original tensor batch.past_key_values[j][0] = None # Slicing end index for this batch end_index = start_index + len(batch) # We slice the keys to remove the padding from previous batches past_seq_len = batch.max_input_length - 1 if batch.keys_head_dim_last: padded_past_keys[start_index:end_index, :, -past_seq_len:, :] = ( past_keys[:, :, -past_seq_len:, :] ) else: # BLOOM case padded_past_keys[start_index:end_index, :, :, -past_seq_len:] = ( past_keys[:, :, :, -past_seq_len:] ) del past_keys start_index = end_index padded_past_values = first_past_kvs[j][1].new_zeros( padded_past_values_shape ) start_index = 0 for batch in batches: past_values = batch.past_key_values[j][1] # Clear reference to the original tensor batch.past_key_values[j][1] = None # Slicing end index for this batch end_index = start_index + len(batch) # We slice the past values to remove the padding from previous batches past_seq_len = batch.max_input_length - 1 padded_past_values[start_index:end_index, :, -past_seq_len:, :] = ( past_values[:, :, -past_seq_len:, :] ) del past_values # Update values start_index = end_index past_key_values.append([padded_past_keys, padded_past_values]) return cls( batch_id=batches[0].batch_id, requests=requests, requests_idx_mapping=requests_idx_mapping, input_ids=input_ids, attention_mask=attention_mask, position_ids=position_ids, pixel_values=pixel_values, image_hidden_states=image_hidden_states, image_attention_mask=image_attention_mask, past_key_values=past_key_values, all_input_ids=all_input_ids, input_lengths=input_lengths, prefix_offsets=prefix_offsets, read_offsets=read_offsets, next_token_choosers=next_token_choosers, stopping_criterias=stopping_criterias, max_input_length=max_input_length, padding_right_offset=padding_right_offset, keys_head_dim_last=batches[0].keys_head_dim_last, max_tokens=max_tokens, ) def __len__(self): return len(self.requests) class IdeficsCausalLM(Model): def __init__( self, model_id: str, revision: Optional[str] = None, quantize: Optional[str] = None, speculator: Optional[str] = None, dtype: Optional[torch.dtype] = None, trust_remote_code: bool = False, ): self.quantize = quantize self.process_group, rank, world_size = initialize_torch_distributed() if torch.cuda.is_available(): device = torch.device(f"cuda:{rank}") # 9b seems to work correctly enough in float16, but 80b seems # to be really saturating for f16. dtype = torch.float16 if dtype is None else dtype elif SYSTEM == "ipex": if hasattr(torch, "xpu") and torch.xpu.is_available(): device = torch.device(f"xpu:{rank}") dtype = torch.float16 if dtype is None else dtype else: device = torch.device("cpu") # Float16 doesn't exist on target. dtype = torch.bfloat16 if dtype is None else dtype else: device = torch.device("cpu") dtype = torch.float32 if dtype is None else dtype self.device, self.dtype = device, dtype config = AutoConfig.from_pretrained( model_id, revision=revision, trust_remote_code=trust_remote_code, ) config.quantize = quantize config.speculator = speculator config.vision_config.quantize = quantize tokenizer = AutoTokenizer.from_pretrained( model_id, revision=revision, padding_side="left", truncation_side="left", trust_remote_code=trust_remote_code, ) self.processor = AutoProcessor.from_pretrained( model_id, revision=revision, padding_side="left", truncation_side="left", trust_remote_code=trust_remote_code, ) weights_loader = get_loader( quantize=quantize, model_id=model_id, revision=revision ) torch.distributed.barrier(group=self.process_group) filenames = weight_files(model_id, revision=revision, extension=".safetensors") weights = Weights( filenames, device=device, dtype=dtype, process_group=self.process_group, weights_loader=weights_loader, ) model = IdeficsForVisionText2Text(config, weights) self.config = config torch.distributed.barrier(group=self.process_group) super().__init__( model_id=model_id, model=model, tokenizer=tokenizer, requires_padding=True, dtype=dtype, device=device, rank=rank, world_size=world_size, ) @property def batch_type(self) -> Type[IdeficsCausalLMBatch]: return IdeficsCausalLMBatch def forward( self, input_ids, attention_mask, position_ids, pixel_values, image_hidden_states, image_attention_mask, past_key_values: Optional = None, ) -> Tuple[torch.Tensor, List[Tuple[torch.Tensor, torch.Tensor]]]: # Model Forward kwargs = { "input_ids": input_ids, "attention_mask": attention_mask, "pixel_values": pixel_values, "image_hidden_states": image_hidden_states, "image_attention_mask": image_attention_mask, "past_key_values": past_key_values, "use_cache": True, "return_dict": True, } if self.has_position_ids: kwargs["position_ids"] = position_ids outputs, speculative_logits = self.model.forward(**kwargs) return ( outputs.logits, speculative_logits, outputs.past_key_values, outputs.image_hidden_states, ) @tracer.start_as_current_span("generate_token") def generate_token( self, batch: IdeficsCausalLMBatch ) -> Tuple[List[Generation], Optional[IdeficsCausalLMBatch], Tuple[int, int]]: start = time.time_ns() # slice the attention mask to the correct shape attention_mask = batch.attention_mask[:, : -batch.padding_right_offset] if batch.image_attention_mask is None: image_attention_mask = None else: if batch.input_ids.size(1) == 1: # THIS is a hack: when calling idefics.generate, the first time, we need the whole image_attention_mask (size bs x max_seq_len x max_num_images), # but the subsequent times, we only need the last attention mask along the `max_seq_len` dimension # this is due to the nature IDEFICS: it's an encoder decoder, and so when decoding, only the currently generated # token need to attend to the encoder hidden states (i.e. the vision encoder) # Also see seq2seq_lm.Seq2SeqLM.generate_token which has roughly the same logic image_attention_mask = batch.image_attention_mask[ :, -(batch.padding_right_offset + 1) ].unsqueeze(1) else: image_attention_mask = batch.image_attention_mask[ :, : -batch.padding_right_offset ] logits, speculative_logits, past, image_hidden_states = self.forward( input_ids=batch.input_ids, attention_mask=attention_mask, position_ids=batch.position_ids, pixel_values=batch.pixel_values, image_hidden_states=batch.image_hidden_states, image_attention_mask=image_attention_mask, past_key_values=batch.past_key_values, ) # Hardcoded remove image tokens logits[:, 32000:32001] = torch.finfo(logits.dtype).min start_decode = time.time_ns() # Results generations: List[Generation] = [] stopped = True # Zipped iterator iterator = zip( batch.requests, batch.input_lengths, batch.prefix_offsets, batch.read_offsets, logits, batch.next_token_choosers, batch.stopping_criterias, batch.all_input_ids, ) # For each member of the batch for i, ( request, input_length, prefix_offset, read_offset, logits, next_token_chooser, stopping_criteria, all_input_ids, ) in enumerate(iterator): # Select next token next_token_id, logprobs = next_token_chooser( all_input_ids.view(1, -1), logits[-1:, :] ) # Append next token to all tokens all_input_ids = torch.cat([all_input_ids, next_token_id]) new_input_length = input_length + 1 # Generated token next_token_logprob = logprobs[-1, next_token_id] next_token_id_squeezed = next_token_id.squeeze() next_token_text, prefix_offset, read_offset = self.decode_token( all_input_ids[:, 0], prefix_offset, read_offset ) # Evaluate stopping criteria stop, reason = stopping_criteria( next_token_id_squeezed, next_token_text, ) if not stop: stopped = False # Shard generations # All generations will be appended in the rust sharded client if i % self.world_size == self.rank: if stop: # Decode generated tokens output_text, _, _ = self.decode_token( all_input_ids[:, 0], prefix_offset=len(all_input_ids) - stopping_criteria.current_tokens - 1, read_offset=len(all_input_ids) - stopping_criteria.current_tokens, skip_special_tokens=True, ) # Get seed if isinstance(next_token_chooser.choice, Sampling): seed = next_token_chooser.choice.seed else: seed = None generated_text = GeneratedText( output_text, stopping_criteria.current_tokens, reason, seed ) else: generated_text = None # Prefill if stopping_criteria.current_tokens == 1 and request.prefill_logprobs: # Remove generated token to only have prefill and add nan for first prompt token prefill_logprobs = [float("nan")] + torch.log_softmax( logits, -1 ).gather(1, all_input_ids[1:]).squeeze(1)[ -new_input_length:-1 ].tolist() prefill_token_ids = all_input_ids[-new_input_length:-1] prefill_texts = self.tokenizer.batch_decode( prefill_token_ids, clean_up_tokenization_spaces=False, skip_special_tokens=False, ) prefill_tokens = Tokens( prefill_token_ids, prefill_logprobs, prefill_texts, is_special=[], ) else: prefill_tokens = None top_tokens = None generation = Generation( request.id, prefill_tokens, Tokens( [next_token_id_squeezed], [next_token_logprob], [next_token_text], [next_token_id_squeezed.item() in self.all_special_ids], ), generated_text, top_tokens, ) generations.append(generation) # Update values batch.next_token_choosers[i] = batch.next_token_choosers[i].advance_grammar( next_token_id_squeezed.item() ) batch.input_ids[i, 0] = next_token_id batch.all_input_ids[i] = all_input_ids batch.input_lengths[i] = new_input_length batch.prefix_offsets[i] = prefix_offset batch.read_offsets[i] = read_offset batch.max_input_length = max(batch.max_input_length, new_input_length) # We finished all generations in the batch; there is no next batch if stopped: forward_ns = start_decode - start decode_ns = time.time_ns() - start_decode return generations, None, (forward_ns, decode_ns) # Slice unused values from prefill batch.input_ids = batch.input_ids[:, :1] # Update attention_mask as we added a new token to input_ids batch.attention_mask[:, -batch.padding_right_offset] = 1 batch.image_attention_mask[:, -batch.padding_right_offset, :] = ( batch.image_attention_mask[:, -(batch.padding_right_offset + 1), :] ) # Decrease right offset batch.padding_right_offset -= 1 # Update position_ids batch.position_ids = batch.position_ids[:, -1:] + 1 # Update past key values batch.past_key_values = past batch.image_hidden_states = image_hidden_states forward_ns = start_decode - start decode_ns = time.time_ns() - start_decode return generations, batch, (forward_ns, decode_ns)
text-generation-inference/server/text_generation_server/models/idefics_causal_lm.py/0
{ "file_path": "text-generation-inference/server/text_generation_server/models/idefics_causal_lm.py", "repo_id": "text-generation-inference", "token_count": 17112 }
import datetime import torch import os from loguru import logger from pathlib import Path from safetensors.torch import save_file, load_file, _find_shared_tensors, _is_complete from typing import List, Dict from collections import defaultdict def _remove_duplicate_names( state_dict: Dict[str, torch.Tensor], *, preferred_names: List[str] = None, discard_names: List[str] = None, ) -> Dict[str, List[str]]: if preferred_names is None: preferred_names = [] preferred_names = set(preferred_names) if discard_names is None: discard_names = [] discard_names = set(discard_names) shareds = _find_shared_tensors(state_dict) to_remove = defaultdict(list) for shared in shareds: complete_names = set( [name for name in shared if _is_complete(state_dict[name])] ) if not complete_names: if len(shared) == 1: # Force contiguous name = list(shared)[0] state_dict[name] = state_dict[name].clone() complete_names = {name} else: raise RuntimeError( f"Error while trying to find names to remove to save state dict, but found no suitable name to keep for saving amongst: {shared}. None is covering the entire storage.Refusing to save/load the model since you could be storing much more memory than needed. Please refer to https://huggingface.co/docs/safetensors/torch_shared_tensors for more information. Or open an issue." ) keep_name = sorted(list(complete_names))[0] # Mecanism to preferentially select keys to keep # coming from the on-disk file to allow # loading models saved with a different choice # of keep_name preferred = complete_names.difference(discard_names) if preferred: keep_name = sorted(list(preferred))[0] if preferred_names: preferred = preferred_names.intersection(complete_names) if preferred: keep_name = sorted(list(preferred))[0] for name in sorted(shared): if name != keep_name: to_remove[keep_name].append(name) return to_remove def convert_file(pt_file: Path, sf_file: Path, discard_names: List[str]): """ Convert a pytorch file to a safetensors file This will remove duplicate tensors from the file. Unfortunately, this might not respect *transformers* convention. Forcing us to check for potentially different keys during load when looking for specific tensors (making tensor sharing explicit). """ loaded = torch.load(pt_file, map_location="cpu", weights_only=True) if "state_dict" in loaded: loaded = loaded["state_dict"] to_removes = _remove_duplicate_names(loaded, discard_names=discard_names) metadata = {"format": "pt"} for kept_name, to_remove_group in to_removes.items(): for to_remove in to_remove_group: if to_remove not in metadata: metadata[to_remove] = kept_name del loaded[to_remove] # Force tensors to be contiguous loaded = {k: v.contiguous() for k, v in loaded.items()} dirname = os.path.dirname(sf_file) os.makedirs(dirname, exist_ok=True) save_file(loaded, sf_file, metadata=metadata) reloaded = load_file(sf_file) for k in loaded: pt_tensor = loaded[k] sf_tensor = reloaded[k] if not torch.equal(pt_tensor, sf_tensor): raise RuntimeError(f"The output tensors do not match for key {k}") def convert_files(pt_files: List[Path], sf_files: List[Path], discard_names: List[str]): assert len(pt_files) == len(sf_files) N = len(pt_files) # We do this instead of using tqdm because we want to parse the logs with the launcher for i, (pt_file, sf_file) in enumerate(zip(pt_files, sf_files)): # Skip blacklisted files if ( "arguments" in pt_file.name or "args" in pt_file.name or "training" in pt_file.name ): continue start = datetime.datetime.now() convert_file(pt_file, sf_file, discard_names) elapsed = datetime.datetime.now() - start logger.info(f"Convert: [{i + 1}/{N}] -- Took: {elapsed}")
text-generation-inference/server/text_generation_server/utils/convert.py/0
{ "file_path": "text-generation-inference/server/text_generation_server/utils/convert.py", "repo_id": "text-generation-inference", "token_count": 1775 }
import torch from abc import ABC, abstractmethod from contextlib import contextmanager from pathlib import Path from typing import Dict, List, Optional, Union, Type from safetensors import safe_open from dataclasses import dataclass from text_generation_server.utils.import_utils import SYSTEM class WeightsLoader(ABC): """ Instances of this type implement higher-level weight loading. At a low-level, every weight is stored in the Safetensors format. The interpretation of weights may be different however, for instance could be packed, quantized weights. Loaders are responsible for interpreting the raw tensors, sharding tensors in a manner compatible with the format, etc. """ @abstractmethod def get_weights(self, weights: "Weights", prefix: str): """ Get weights at the given prefix and apply without tensor paralllism. """ ... @abstractmethod def get_weights_col_packed( self, weights: "Weights", prefix: str, block_sizes: Union[int, List[int]], ): """ Get the packed weights at the given prefix with column-splitting for tensor parallelism. This method should be used when multiple different weights are packed into a tensor, for instance, query/key/value weights or a gate/up projection. The `block_sizes` determines the proportions of the packed tensors. The columns are split in equally sized blocks when `block_sizes` is an `int`, or in blocks proportional given to the sizes. For instance `[2, 1, 1]` will divide an input with dimensionality `1024` in `[512, 256, 256]`. """ ... def get_weights_col(self, weights: "Weights", prefix: str): """ Get weights at the given prefix and apply column-splitting for tensor paralllism. """ return weights.get_multi_weights_col([prefix], 0) @abstractmethod def get_multi_weights_col(self, weights: "Weights", prefixes: List[str], dim: int): """ Get the weights at the given prefixes, column-split them for tensor parallelim, and then concatenate the weights along the given dimension. """ ... @abstractmethod def get_weights_row(self, weights: "Weights", prefix: str): """ Get the weights at the given prefix and apply row-splitting for tensor parallism. """ ... class Weight(ABC): """Instances of this type implement unquantized/quantized/to-be quantized weights.""" @abstractmethod def get_linear(self, bias: torch.Tensor): """Create a linear layer from this weight.""" ... @dataclass class UnquantizedWeight(Weight): weight: torch.Tensor def get_linear(self, bias: torch.Tensor): from text_generation_server.layers.linear import FastLinear, FastLinearROCm if SYSTEM == "rocm": return FastLinearROCm(self.weight, bias) else: return FastLinear(self.weight, bias) class DefaultWeightsLoader(WeightsLoader): """Weight loader that loads (unquantized) Torch tensors.""" def __init__(self, weight_class: Type[UnquantizedWeight]): """Create a loader. Weights will be wrapped using the given `weights_class`, normally this will be `UnquantizedWeight`, but a quantizer-specific class such as `Fp8Weight` can be used to quantize the weights during loading. """ self.weight_class = weight_class """ Loader that uses tensors as-is with the exception of applying sharding and/or concatenation. """ def get_weights(self, weights: "Weights", prefix: str): return weights.get_tensor(f"{prefix}.weight") def get_weights_col_packed( self, weights: "Weights", prefix: str, block_sizes: Union[int, List[int]], ): return self.weight_class( weights.get_packed_sharded( f"{prefix}.weight", dim=0, block_sizes=block_sizes ), ) def get_multi_weights_col(self, weights: "Weights", prefixes: List[str], dim: int): w = [weights.get_sharded(f"{p}.weight", dim=0) for p in prefixes] return self.weight_class(torch.cat(w, dim=dim)) def get_weights_row(self, weights: "Weights", prefix: str): return self.weight_class( weights.get_sharded(f"{prefix}.weight", dim=1), ) class Weights: def __init__( self, filenames: List[Path], device, dtype, process_group, weights_loader: WeightsLoader, aliases: Optional[Dict[str, List[str]]] = None, prefix: Optional[str] = None, ): routing = {} for filename in filenames: with safe_open(filename, framework="pytorch") as f: for k in f.keys(): if k in routing: raise RuntimeError( f"Key {k} was found in multiple files: {filename} and {routing[k]}" ) routing[k] = filename if aliases is None: aliases = {} self.aliases = aliases self.routing = routing self.device = device self.dtype = dtype self.process_group = process_group self.prefix = prefix self.weights_loader = weights_loader self._handles = {} def _get_handle(self, filename): if filename not in self._handles: f = safe_open(filename, framework="pytorch") self._handles[filename] = f return self._handles[filename] def get_filename(self, tensor_name: str) -> (str, str): names = [tensor_name] if self.prefix is not None: prefixed = f"{self.prefix}.{tensor_name}" names.append(prefixed) for name in names: filename = self.routing.get(name, None) if filename is not None: return str(filename), name aliases = self.aliases.get(name, []) for alias in aliases: filename = self.routing.get(alias, None) if filename is not None: return str(filename), alias raise RuntimeError(f"weight {tensor_name} does not exist") def _get_slice(self, tensor_name: str): filename, tensor_name = self.get_filename(tensor_name) f = self._get_handle(filename) slice_ = f.get_slice(tensor_name) return slice_ def has_tensor(self, tensor_name: str): try: self.get_filename(tensor_name) except Exception: return False return True def get_shape(self, tensor_name: str): return self._get_slice(tensor_name).get_shape() def get_tensor( self, tensor_name: str, to_device: bool = True, to_dtype: bool = True ) -> torch.Tensor: filename, tensor_name = self.get_filename(tensor_name) f = self._get_handle(filename) tensor = f.get_tensor(tensor_name) # Special case for gptq which shouldn't convert # u4 which are disguised as int32. Exl2 uses int16 # as well. FP8 uses torch.float8_e4m3fn if ( tensor.dtype not in [ torch.float8_e4m3fn, torch.int8, torch.int16, torch.int32, torch.int64, ] and to_dtype ): tensor = tensor.to(dtype=self.dtype) if to_device: tensor = tensor.to(device=self.device) return tensor def get_partial_sharded( self, tensor_name: str, dim: int, to_device=True, to_dtype=True ): filename, tensor_name = self.get_filename(tensor_name) f = self._get_handle(filename) slice_ = f.get_slice(tensor_name) world_size = self.process_group.size() rank = self.process_group.rank() size = slice_.get_shape()[dim] block_size = (size + world_size - 1) // world_size start = rank * block_size stop = (rank + 1) * block_size if dim == 0: tensor = slice_[start:stop] elif dim == 1: tensor = slice_[:, start:stop] else: raise NotImplementedError("Let's make that generic when needed") # Special case for gptq which shouldn't convert # u4 which are disguised as int32. exl2 uses int16. # FP8 uses torch.float8_e4m3fn. if ( tensor.dtype not in (torch.float8_e4m3fn, torch.int8, torch.int16, torch.int32) and to_dtype ): tensor = tensor.to(dtype=self.dtype) if to_device: tensor = tensor.to(device=self.device) return tensor def get_sharded(self, tensor_name: str, dim: int, to_device=True, to_dtype=True): filename, tensor_name = self.get_filename(tensor_name) f = self._get_handle(filename) slice_ = f.get_slice(tensor_name) world_size = self.process_group.size() size = slice_.get_shape()[dim] assert ( size % world_size == 0 ), f"The choosen size {size} is not compatible with sharding on {world_size} shards" return self.get_partial_sharded( tensor_name, dim, to_device=to_device, to_dtype=to_dtype ) def get_packed_sharded( self, tensor_name: str, dim: int, block_sizes: Union[int, List[int]], to_dtype=True, ) -> torch.Tensor: """ Get a shard from a tensor that packs multiple tensors. When a tensor packs multiple tensors (such as QKV or an up projection + gate projection), sharding with `get_sharded` is not safe since it would not split the packed tensors across shards. This method shards a tensor, such that the packed tensors are split across shards. The columns are split in equally sized blocks when blocks is an `int`, or in blocks proportional given to the sizes. For instance `[2, 1, 1]` will divide an input with dimensionality `1024` in `[512, 256, 256]`. This is convenient for e.g. splitting QKV without knowing the storage details of quantized weights. """ slice_ = self._get_slice(tensor_name) total_size = slice_.get_shape()[dim] block_sizes = _blocks_to_block_sizes(total_size=total_size, blocks=block_sizes) world_size = self.process_group.size() rank = self.process_group.rank() tensors = [] block_offset = 0 for block_size in block_sizes: assert ( block_size % world_size == 0 ), f"Prepacked tensor cannot be sharded across {world_size} shards" shard_block_size = block_size // world_size start = rank * shard_block_size stop = (rank + 1) * shard_block_size if dim == 0: tensor = slice_[block_offset + start : block_offset + stop] elif dim == 1: tensor = slice_[:, block_offset + start : block_offset + stop] else: raise NotImplementedError("Currently only dim=0 or dim=1 is supported") tensors.append(tensor) block_offset += block_size tensor = torch.cat(tensors, dim=dim) tensor = tensor.to(device=self.device) # Avoid casting quantizer dtypes. if ( tensor.dtype not in [ torch.float8_e4m3fn, torch.int8, torch.int16, torch.int32, torch.int64, ] and to_dtype ): tensor = tensor.to(dtype=self.dtype) return tensor def get_weights(self, prefix: str): return self.weights_loader.get_weights(self, prefix) def get_weights_col_packed_qkv( self, prefix: str, num_heads: int, num_key_value_heads: int, ): return self.get_weights_col_packed( prefix, [num_heads, num_key_value_heads, num_key_value_heads] ) def get_weights_col_packed_gate_up(self, prefix: str): return self.get_weights_col_packed(prefix, 2) def get_weights_col_packed(self, prefix: str, block_sizes: Union[int, List[int]]): """ The columns are split in equally sized blocks when blocks is an `int`, or in blocks proportional given to the sizes. For instance `[2, 1, 1]` will divide an input with dimensionality `1024` in `[512, 256, 256]`. This is convenient for e.g. splitting QKV without knowing the storage details of quantized weights. """ return self.weights_loader.get_weights_col_packed(self, prefix, block_sizes) def get_weights_col(self, prefix: str): return self.weights_loader.get_weights_col(self, prefix) def get_multi_weights_col(self, prefixes: List[str], dim: int): return self.weights_loader.get_multi_weights_col(self, prefixes, dim) def get_tensor_shard(self, var, dim): world_size = self.process_group.size() rank = self.process_group.rank() block_size = var.size()[dim] // world_size start = rank * block_size stop = (rank + 1) * block_size if dim == 0: tensor = var[start:stop] elif dim == 1: tensor = var[:, start:stop] else: raise NotImplementedError("Let's make that generic when needed") tensor = tensor.to(dtype=self.dtype) tensor = tensor.to(device=self.device) return tensor def get_weights_row(self, prefix: str): return self.weights_loader.get_weights_row(self, prefix) @contextmanager def use_loader(self, weights_loader: WeightsLoader): """ This method is a context manager that can be used to use `Weights` with a different loader for the duration of the context. """ old_loader = self.weights_loader self.weights_loader = weights_loader try: yield finally: self.weights_loader = old_loader @property def loader(self): return self.weights_loader def _blocks_to_block_sizes(total_size: int, blocks: Union[int, List[int]]) -> List[int]: """ Convert block count or proportions to block sizes. This function accepts - The number of blocks (int), in which case the block size is total_size//blocks; or - A list of block sizes (List[int]). In the latter case, if sum(blocks) < total_size, the ratios between the block sizes will be preserved. For instance, if blocks is [2, 1, 1] and total_size is 1024, the returned block sizes are [512, 256, 256]. """ if isinstance(blocks, list): total_blocks = sum(blocks) assert ( total_size % total_blocks == 0 ), f"Cannot split {total_size} in proportional blocks: {blocks}" part_size = total_size // total_blocks return [part_size * block for block in blocks] else: assert total_size % blocks == 0, f"Prepacked is not divisible by {blocks}" single_size = total_size // blocks return [single_size] * blocks
text-generation-inference/server/text_generation_server/utils/weights.py/0
{ "file_path": "text-generation-inference/server/text_generation_server/utils/weights.py", "repo_id": "text-generation-inference", "token_count": 6743 }
/* eslint-disable @typescript-eslint/no-empty-function */ /* eslint-disable @typescript-eslint/no-explicit-any */ import { BPE, Unigram, WordPiece } from '../../' const MOCKS_DIR = __dirname + '/__mocks__' describe('WordPiece', () => { describe('fromFile', () => { it('throws if called with only one argument', () => { expect(() => (WordPiece as any).fromFile()).toThrow( 'Failed to convert JavaScript value `Undefined` into rust type `String`', ) }) it('throws if called with 2 arguments without a callback as third argument', () => { expect(() => (WordPiece as any).fromFile({})).toThrow( 'Failed to convert JavaScript value `Object {}` into rust type `String`', ) }) it('has its callback called with the loaded model', async () => { const model = await WordPiece.fromFile(`${MOCKS_DIR}/vocab.txt`) expect(model).toBeDefined() }) }) }) describe('BPE', () => { describe('fromFile', () => { it('has its callback called with the loaded model', async () => { const model = await BPE.fromFile(`${MOCKS_DIR}/vocab.json`, `${MOCKS_DIR}/merges.txt`) expect(model).toBeDefined() }) it('has its callback called with the loaded model', async () => { const model = await BPE.fromFile(`${MOCKS_DIR}/vocab.json`, `${MOCKS_DIR}/merges.txt`, {}) expect(model).toBeDefined() }) }) describe('When initialized from memory', () => { it('returns the loaded Model', () => { const bpe = BPE.init({ a: 0, b: 1, ab: 2 }, [['a', 'b']]) // expect(bpe.constructor.name).toEqual("Model"); expect(bpe.constructor.name).toEqual('BPE') }) }) }) describe('Unigram', () => { it('can be initialized from memory', () => { const unigram = Unigram.init( [ ['<unk>', 0], ['Hello', -1], ['there', -2], ], { unkId: 0, }, ) expect(unigram.constructor.name).toEqual('Unigram') }) })
tokenizers/bindings/node/lib/bindings/models.test.ts/0
{ "file_path": "tokenizers/bindings/node/lib/bindings/models.test.ts", "repo_id": "tokenizers", "token_count": 818 }
{ "name": "tokenizers", "version": "0.15.3-dev0", "repository": { "type": "git", "url": "git+https://github.com/huggingface/tokenizers.git" }, "bugs": { "url": "https://github.com/huggingface/tokenizers/issues" }, "homepage": "https://github.com/huggingface/tokenizers/tree/master/bindings/node", "author": "Anthony MOI <m.anthony.moi@gmail.com>", "license": "Apache-2.0", "description": "Provides an implementation of today's most used tokenizers, with a focus on performances and versatility.", "files": [ "index.d.ts", "index.js" ], "napi": { "name": "tokenizers", "triples": { "defaults": true, "additional": [ "x86_64-unknown-linux-musl", "aarch64-unknown-linux-gnu", "i686-pc-windows-msvc", "armv7-unknown-linux-gnueabihf", "aarch64-apple-darwin", "aarch64-linux-android", "x86_64-unknown-freebsd", "aarch64-unknown-linux-musl", "aarch64-pc-windows-msvc", "armv7-linux-androideabi" ] } }, "engines": { "node": ">= 10" }, "publishConfig": { "registry": "https://registry.npmjs.org/", "access": "public" }, "scripts": { "artifacts": "napi artifacts", "bench": "node -r @swc-node/register benchmark/bench.ts", "build": "napi build --platform --release --pipe \"prettier -w\"", "build:debug": "napi build --platform --pipe \"prettier -w\"", "format": "run-p format:prettier format:rs format:toml", "format:prettier": "prettier . -w", "format:toml": "taplo format", "format:rs": "cargo fmt", "lint": "eslint . -c ./.eslintrc.yml", "prepublishOnly": "napi prepublish -t npm", "test": "jest", "version": "napi version" }, "devDependencies": { "@napi-rs/cli": "^2.14.6", "@swc-node/register": "^1.5.5", "@swc/core": "^1.3.32", "@taplo/cli": "^0.5.2", "@types/jest": "^29.5.1", "@typescript-eslint/eslint-plugin": "^5.50.0", "@typescript-eslint/parser": "^5.50.0", "ava": "^5.1.1", "benny": "^3.7.1", "chalk": "^5.2.0", "eslint": "^8.33.0", "eslint-config-prettier": "^8.6.0", "eslint-plugin-import": "^2.27.5", "eslint-plugin-prettier": "^4.2.1", "husky": "^8.0.3", "jest": "^29.5.0", "lint-staged": "^13.1.0", "npm-run-all": "^4.1.5", "prettier": "^2.8.3", "ts-jest": "^29.1.0", "typescript": "^5.0.0" }, "lint-staged": { "*.@(js|ts|tsx)": [ "eslint -c .eslintrc.yml --fix" ], "*.@(js|ts|tsx|yml|yaml|md|json)": [ "prettier --write" ], "*.toml": [ "taplo format" ] }, "ava": { "require": [ "@swc-node/register" ], "extensions": [ "ts" ], "timeout": "2m", "workerThreads": false, "environmentVariables": { "TS_NODE_PROJECT": "./tsconfig.json" } }, "prettier": { "printWidth": 120, "semi": false, "trailingComma": "all", "singleQuote": true, "arrowParens": "always" }, "packageManager": "yarn@3.5.1" }
tokenizers/bindings/node/package.json/0
{ "file_path": "tokenizers/bindings/node/package.json", "repo_id": "tokenizers", "token_count": 1532 }
{ "compilerOptions": { "target": "ES2018", "strict": true, "moduleResolution": "node", "module": "CommonJS", "noUnusedLocals": true, "noUnusedParameters": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true }, "include": ["."], "exclude": ["node_modules"] }
tokenizers/bindings/node/tsconfig.json/0
{ "file_path": "tokenizers/bindings/node/tsconfig.json", "repo_id": "tokenizers", "token_count": 129 }
import datasets from tokenizers import Tokenizer, models, normalizers, pre_tokenizers # Build a tokenizer bpe_tokenizer = Tokenizer(models.BPE()) bpe_tokenizer.pre_tokenizer = pre_tokenizers.Whitespace() bpe_tokenizer.normalizer = normalizers.Lowercase() # Initialize a dataset dataset = datasets.load_dataset("wikitext", "wikitext-103-raw-v1", split="train") # Build an iterator over this dataset def batch_iterator(): batch_size = 1000 for batch in dataset.iter(batch_size=batch_size): yield batch["text"] # And finally train bpe_tokenizer.train_from_iterator(batch_iterator(), length=len(dataset))
tokenizers/bindings/python/examples/train_with_datasets.py/0
{ "file_path": "tokenizers/bindings/python/examples/train_with_datasets.py", "repo_id": "tokenizers", "token_count": 207 }
# Generated content DO NOT EDIT class Normalizer: """ Base class for all normalizers This class is not supposed to be instantiated directly. Instead, any implementation of a Normalizer will return an instance of this class when instantiated. """ def normalize(self, normalized): """ Normalize a :class:`~tokenizers.NormalizedString` in-place This method allows to modify a :class:`~tokenizers.NormalizedString` to keep track of the alignment information. If you just want to see the result of the normalization on a raw string, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize_str` Args: normalized (:class:`~tokenizers.NormalizedString`): The normalized string on which to apply this :class:`~tokenizers.normalizers.Normalizer` """ pass def normalize_str(self, sequence): """ Normalize the given string This method provides a way to visualize the effect of a :class:`~tokenizers.normalizers.Normalizer` but it does not keep track of the alignment information. If you need to get/convert offsets, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize` Args: sequence (:obj:`str`): A string to normalize Returns: :obj:`str`: A string after normalization """ pass class BertNormalizer(Normalizer): """ BertNormalizer Takes care of normalizing raw text before giving it to a Bert model. This includes cleaning the text, handling accents, chinese chars and lowercasing Args: clean_text (:obj:`bool`, `optional`, defaults to :obj:`True`): Whether to clean the text, by removing any control characters and replacing all whitespaces by the classic one. handle_chinese_chars (:obj:`bool`, `optional`, defaults to :obj:`True`): Whether to handle chinese chars by putting spaces around them. strip_accents (:obj:`bool`, `optional`): Whether to strip all accents. If this option is not specified (ie == None), then it will be determined by the value for `lowercase` (as in the original Bert). lowercase (:obj:`bool`, `optional`, defaults to :obj:`True`): Whether to lowercase. """ def __init__(self, clean_text=True, handle_chinese_chars=True, strip_accents=None, lowercase=True): pass def normalize(self, normalized): """ Normalize a :class:`~tokenizers.NormalizedString` in-place This method allows to modify a :class:`~tokenizers.NormalizedString` to keep track of the alignment information. If you just want to see the result of the normalization on a raw string, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize_str` Args: normalized (:class:`~tokenizers.NormalizedString`): The normalized string on which to apply this :class:`~tokenizers.normalizers.Normalizer` """ pass def normalize_str(self, sequence): """ Normalize the given string This method provides a way to visualize the effect of a :class:`~tokenizers.normalizers.Normalizer` but it does not keep track of the alignment information. If you need to get/convert offsets, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize` Args: sequence (:obj:`str`): A string to normalize Returns: :obj:`str`: A string after normalization """ pass class ByteLevel(Normalizer): """ Bytelevel Normalizer """ def __init__(self): pass def normalize(self, normalized): """ Normalize a :class:`~tokenizers.NormalizedString` in-place This method allows to modify a :class:`~tokenizers.NormalizedString` to keep track of the alignment information. If you just want to see the result of the normalization on a raw string, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize_str` Args: normalized (:class:`~tokenizers.NormalizedString`): The normalized string on which to apply this :class:`~tokenizers.normalizers.Normalizer` """ pass def normalize_str(self, sequence): """ Normalize the given string This method provides a way to visualize the effect of a :class:`~tokenizers.normalizers.Normalizer` but it does not keep track of the alignment information. If you need to get/convert offsets, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize` Args: sequence (:obj:`str`): A string to normalize Returns: :obj:`str`: A string after normalization """ pass class Lowercase(Normalizer): """ Lowercase Normalizer """ def __init__(self): pass def normalize(self, normalized): """ Normalize a :class:`~tokenizers.NormalizedString` in-place This method allows to modify a :class:`~tokenizers.NormalizedString` to keep track of the alignment information. If you just want to see the result of the normalization on a raw string, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize_str` Args: normalized (:class:`~tokenizers.NormalizedString`): The normalized string on which to apply this :class:`~tokenizers.normalizers.Normalizer` """ pass def normalize_str(self, sequence): """ Normalize the given string This method provides a way to visualize the effect of a :class:`~tokenizers.normalizers.Normalizer` but it does not keep track of the alignment information. If you need to get/convert offsets, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize` Args: sequence (:obj:`str`): A string to normalize Returns: :obj:`str`: A string after normalization """ pass class NFC(Normalizer): """ NFC Unicode Normalizer """ def __init__(self): pass def normalize(self, normalized): """ Normalize a :class:`~tokenizers.NormalizedString` in-place This method allows to modify a :class:`~tokenizers.NormalizedString` to keep track of the alignment information. If you just want to see the result of the normalization on a raw string, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize_str` Args: normalized (:class:`~tokenizers.NormalizedString`): The normalized string on which to apply this :class:`~tokenizers.normalizers.Normalizer` """ pass def normalize_str(self, sequence): """ Normalize the given string This method provides a way to visualize the effect of a :class:`~tokenizers.normalizers.Normalizer` but it does not keep track of the alignment information. If you need to get/convert offsets, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize` Args: sequence (:obj:`str`): A string to normalize Returns: :obj:`str`: A string after normalization """ pass class NFD(Normalizer): """ NFD Unicode Normalizer """ def __init__(self): pass def normalize(self, normalized): """ Normalize a :class:`~tokenizers.NormalizedString` in-place This method allows to modify a :class:`~tokenizers.NormalizedString` to keep track of the alignment information. If you just want to see the result of the normalization on a raw string, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize_str` Args: normalized (:class:`~tokenizers.NormalizedString`): The normalized string on which to apply this :class:`~tokenizers.normalizers.Normalizer` """ pass def normalize_str(self, sequence): """ Normalize the given string This method provides a way to visualize the effect of a :class:`~tokenizers.normalizers.Normalizer` but it does not keep track of the alignment information. If you need to get/convert offsets, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize` Args: sequence (:obj:`str`): A string to normalize Returns: :obj:`str`: A string after normalization """ pass class NFKC(Normalizer): """ NFKC Unicode Normalizer """ def __init__(self): pass def normalize(self, normalized): """ Normalize a :class:`~tokenizers.NormalizedString` in-place This method allows to modify a :class:`~tokenizers.NormalizedString` to keep track of the alignment information. If you just want to see the result of the normalization on a raw string, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize_str` Args: normalized (:class:`~tokenizers.NormalizedString`): The normalized string on which to apply this :class:`~tokenizers.normalizers.Normalizer` """ pass def normalize_str(self, sequence): """ Normalize the given string This method provides a way to visualize the effect of a :class:`~tokenizers.normalizers.Normalizer` but it does not keep track of the alignment information. If you need to get/convert offsets, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize` Args: sequence (:obj:`str`): A string to normalize Returns: :obj:`str`: A string after normalization """ pass class NFKD(Normalizer): """ NFKD Unicode Normalizer """ def __init__(self): pass def normalize(self, normalized): """ Normalize a :class:`~tokenizers.NormalizedString` in-place This method allows to modify a :class:`~tokenizers.NormalizedString` to keep track of the alignment information. If you just want to see the result of the normalization on a raw string, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize_str` Args: normalized (:class:`~tokenizers.NormalizedString`): The normalized string on which to apply this :class:`~tokenizers.normalizers.Normalizer` """ pass def normalize_str(self, sequence): """ Normalize the given string This method provides a way to visualize the effect of a :class:`~tokenizers.normalizers.Normalizer` but it does not keep track of the alignment information. If you need to get/convert offsets, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize` Args: sequence (:obj:`str`): A string to normalize Returns: :obj:`str`: A string after normalization """ pass class Nmt(Normalizer): """ Nmt normalizer """ def __init__(self): pass def normalize(self, normalized): """ Normalize a :class:`~tokenizers.NormalizedString` in-place This method allows to modify a :class:`~tokenizers.NormalizedString` to keep track of the alignment information. If you just want to see the result of the normalization on a raw string, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize_str` Args: normalized (:class:`~tokenizers.NormalizedString`): The normalized string on which to apply this :class:`~tokenizers.normalizers.Normalizer` """ pass def normalize_str(self, sequence): """ Normalize the given string This method provides a way to visualize the effect of a :class:`~tokenizers.normalizers.Normalizer` but it does not keep track of the alignment information. If you need to get/convert offsets, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize` Args: sequence (:obj:`str`): A string to normalize Returns: :obj:`str`: A string after normalization """ pass class Precompiled(Normalizer): """ Precompiled normalizer Don't use manually it is used for compatibility for SentencePiece. """ def __init__(self, precompiled_charsmap): pass def normalize(self, normalized): """ Normalize a :class:`~tokenizers.NormalizedString` in-place This method allows to modify a :class:`~tokenizers.NormalizedString` to keep track of the alignment information. If you just want to see the result of the normalization on a raw string, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize_str` Args: normalized (:class:`~tokenizers.NormalizedString`): The normalized string on which to apply this :class:`~tokenizers.normalizers.Normalizer` """ pass def normalize_str(self, sequence): """ Normalize the given string This method provides a way to visualize the effect of a :class:`~tokenizers.normalizers.Normalizer` but it does not keep track of the alignment information. If you need to get/convert offsets, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize` Args: sequence (:obj:`str`): A string to normalize Returns: :obj:`str`: A string after normalization """ pass class Prepend(Normalizer): """ Prepend normalizer """ def __init__(self, prepend): pass def normalize(self, normalized): """ Normalize a :class:`~tokenizers.NormalizedString` in-place This method allows to modify a :class:`~tokenizers.NormalizedString` to keep track of the alignment information. If you just want to see the result of the normalization on a raw string, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize_str` Args: normalized (:class:`~tokenizers.NormalizedString`): The normalized string on which to apply this :class:`~tokenizers.normalizers.Normalizer` """ pass def normalize_str(self, sequence): """ Normalize the given string This method provides a way to visualize the effect of a :class:`~tokenizers.normalizers.Normalizer` but it does not keep track of the alignment information. If you need to get/convert offsets, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize` Args: sequence (:obj:`str`): A string to normalize Returns: :obj:`str`: A string after normalization """ pass class Replace(Normalizer): """ Replace normalizer """ def __init__(self, pattern, content): pass def normalize(self, normalized): """ Normalize a :class:`~tokenizers.NormalizedString` in-place This method allows to modify a :class:`~tokenizers.NormalizedString` to keep track of the alignment information. If you just want to see the result of the normalization on a raw string, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize_str` Args: normalized (:class:`~tokenizers.NormalizedString`): The normalized string on which to apply this :class:`~tokenizers.normalizers.Normalizer` """ pass def normalize_str(self, sequence): """ Normalize the given string This method provides a way to visualize the effect of a :class:`~tokenizers.normalizers.Normalizer` but it does not keep track of the alignment information. If you need to get/convert offsets, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize` Args: sequence (:obj:`str`): A string to normalize Returns: :obj:`str`: A string after normalization """ pass class Sequence(Normalizer): """ Allows concatenating multiple other Normalizer as a Sequence. All the normalizers run in sequence in the given order Args: normalizers (:obj:`List[Normalizer]`): A list of Normalizer to be run as a sequence """ def normalize(self, normalized): """ Normalize a :class:`~tokenizers.NormalizedString` in-place This method allows to modify a :class:`~tokenizers.NormalizedString` to keep track of the alignment information. If you just want to see the result of the normalization on a raw string, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize_str` Args: normalized (:class:`~tokenizers.NormalizedString`): The normalized string on which to apply this :class:`~tokenizers.normalizers.Normalizer` """ pass def normalize_str(self, sequence): """ Normalize the given string This method provides a way to visualize the effect of a :class:`~tokenizers.normalizers.Normalizer` but it does not keep track of the alignment information. If you need to get/convert offsets, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize` Args: sequence (:obj:`str`): A string to normalize Returns: :obj:`str`: A string after normalization """ pass class Strip(Normalizer): """ Strip normalizer """ def __init__(self, left=True, right=True): pass def normalize(self, normalized): """ Normalize a :class:`~tokenizers.NormalizedString` in-place This method allows to modify a :class:`~tokenizers.NormalizedString` to keep track of the alignment information. If you just want to see the result of the normalization on a raw string, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize_str` Args: normalized (:class:`~tokenizers.NormalizedString`): The normalized string on which to apply this :class:`~tokenizers.normalizers.Normalizer` """ pass def normalize_str(self, sequence): """ Normalize the given string This method provides a way to visualize the effect of a :class:`~tokenizers.normalizers.Normalizer` but it does not keep track of the alignment information. If you need to get/convert offsets, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize` Args: sequence (:obj:`str`): A string to normalize Returns: :obj:`str`: A string after normalization """ pass class StripAccents(Normalizer): """ StripAccents normalizer """ def __init__(self): pass def normalize(self, normalized): """ Normalize a :class:`~tokenizers.NormalizedString` in-place This method allows to modify a :class:`~tokenizers.NormalizedString` to keep track of the alignment information. If you just want to see the result of the normalization on a raw string, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize_str` Args: normalized (:class:`~tokenizers.NormalizedString`): The normalized string on which to apply this :class:`~tokenizers.normalizers.Normalizer` """ pass def normalize_str(self, sequence): """ Normalize the given string This method provides a way to visualize the effect of a :class:`~tokenizers.normalizers.Normalizer` but it does not keep track of the alignment information. If you need to get/convert offsets, you can use :meth:`~tokenizers.normalizers.Normalizer.normalize` Args: sequence (:obj:`str`): A string to normalize Returns: :obj:`str`: A string after normalization """ pass
tokenizers/bindings/python/py_src/tokenizers/normalizers/__init__.pyi/0
{ "file_path": "tokenizers/bindings/python/py_src/tokenizers/normalizers/__init__.pyi", "repo_id": "tokenizers", "token_count": 8593 }
use std::sync::{Arc, RwLock}; use crate::pre_tokenizers::from_string; use crate::tokenizer::PyTokenizer; use crate::utils::PyPattern; use pyo3::exceptions; use pyo3::prelude::*; use pyo3::types::*; use serde::de::Error; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use tk::decoders::bpe::BPEDecoder; use tk::decoders::byte_fallback::ByteFallback; use tk::decoders::byte_level::ByteLevel; use tk::decoders::ctc::CTC; use tk::decoders::fuse::Fuse; use tk::decoders::metaspace::{Metaspace, PrependScheme}; use tk::decoders::sequence::Sequence; use tk::decoders::strip::Strip; use tk::decoders::wordpiece::WordPiece; use tk::decoders::DecoderWrapper; use tk::normalizers::replace::Replace; use tk::Decoder; use tokenizers as tk; use super::error::ToPyResult; /// Base class for all decoders /// /// This class is not supposed to be instantiated directly. Instead, any implementation of /// a Decoder will return an instance of this class when instantiated. #[pyclass(dict, module = "tokenizers.decoders", name = "Decoder", subclass)] #[derive(Clone, Deserialize, Serialize)] #[serde(transparent)] pub struct PyDecoder { pub(crate) decoder: PyDecoderWrapper, } impl PyDecoder { pub(crate) fn new(decoder: PyDecoderWrapper) -> Self { PyDecoder { decoder } } pub(crate) fn get_as_subtype(&self, py: Python<'_>) -> PyResult<PyObject> { let base = self.clone(); Ok(match &self.decoder { PyDecoderWrapper::Custom(_) => Py::new(py, base)?.into_pyobject(py)?.into_any().into(), PyDecoderWrapper::Wrapped(inner) => match &*inner.as_ref().read().unwrap() { DecoderWrapper::Metaspace(_) => Py::new(py, (PyMetaspaceDec {}, base))? .into_pyobject(py)? .into_any() .into(), DecoderWrapper::WordPiece(_) => Py::new(py, (PyWordPieceDec {}, base))? .into_pyobject(py)? .into_any() .into(), DecoderWrapper::ByteFallback(_) => Py::new(py, (PyByteFallbackDec {}, base))? .into_pyobject(py)? .into_any() .into(), DecoderWrapper::Strip(_) => Py::new(py, (PyStrip {}, base))? .into_pyobject(py)? .into_any() .into(), DecoderWrapper::Fuse(_) => Py::new(py, (PyFuseDec {}, base))? .into_pyobject(py)? .into_any() .into(), DecoderWrapper::ByteLevel(_) => Py::new(py, (PyByteLevelDec {}, base))? .into_pyobject(py)? .into_any() .into(), DecoderWrapper::Replace(_) => Py::new(py, (PyReplaceDec {}, base))? .into_pyobject(py)? .into_any() .into(), DecoderWrapper::BPE(_) => Py::new(py, (PyBPEDecoder {}, base))? .into_pyobject(py)? .into_any() .into(), DecoderWrapper::CTC(_) => Py::new(py, (PyCTCDecoder {}, base))? .into_pyobject(py)? .into_any() .into(), DecoderWrapper::Sequence(_) => Py::new(py, (PySequenceDecoder {}, base))? .into_pyobject(py)? .into_any() .into(), }, }) } } impl Decoder for PyDecoder { fn decode_chain(&self, tokens: Vec<String>) -> tk::Result<Vec<String>> { self.decoder.decode_chain(tokens) } } #[pymethods] impl PyDecoder { #[staticmethod] fn custom(decoder: PyObject) -> Self { let decoder = PyDecoderWrapper::Custom(Arc::new(RwLock::new(CustomDecoder::new(decoder)))); PyDecoder::new(decoder) } fn __getstate__(&self, py: Python) -> PyResult<PyObject> { let data = serde_json::to_string(&self.decoder).map_err(|e| { exceptions::PyException::new_err(format!( "Error while attempting to pickle Decoder: {}", e )) })?; Ok(PyBytes::new(py, data.as_bytes()).into()) } fn __setstate__(&mut self, py: Python, state: PyObject) -> PyResult<()> { match state.extract::<&[u8]>(py) { Ok(s) => { self.decoder = serde_json::from_slice(s).map_err(|e| { exceptions::PyException::new_err(format!( "Error while attempting to unpickle Decoder: {}", e )) })?; Ok(()) } Err(e) => Err(e), } } /// Decode the given list of tokens to a final string /// /// Args: /// tokens (:obj:`List[str]`): /// The list of tokens to decode /// /// Returns: /// :obj:`str`: The decoded string #[pyo3(text_signature = "(self, tokens)")] fn decode(&self, tokens: Vec<String>) -> PyResult<String> { ToPyResult(self.decoder.decode(tokens)).into() } fn __repr__(&self) -> PyResult<String> { crate::utils::serde_pyo3::repr(self) .map_err(|e| exceptions::PyException::new_err(e.to_string())) } fn __str__(&self) -> PyResult<String> { crate::utils::serde_pyo3::to_string(self) .map_err(|e| exceptions::PyException::new_err(e.to_string())) } } macro_rules! getter { ($self: ident, $variant: ident, $($name: tt)+) => {{ let super_ = $self.as_ref(); if let PyDecoderWrapper::Wrapped(ref wrap) = super_.decoder { if let DecoderWrapper::$variant(ref dec) = *wrap.read().unwrap() { dec.$($name)+ } else { unreachable!() } } else { unreachable!() } }}; } macro_rules! setter { ($self: ident, $variant: ident, $name: ident, $value: expr) => {{ let super_ = $self.as_ref(); if let PyDecoderWrapper::Wrapped(ref wrap) = super_.decoder { if let DecoderWrapper::$variant(ref mut dec) = *wrap.write().unwrap() { dec.$name = $value; } } }}; ($self: ident, $variant: ident, @$name: ident, $value: expr) => {{ let super_ = $self.as_ref(); if let PyDecoderWrapper::Wrapped(ref wrap) = super_.decoder { if let DecoderWrapper::$variant(ref mut dec) = *wrap.write().unwrap() { dec.$name($value); } } }}; } /// ByteLevel Decoder /// /// This decoder is to be used in tandem with the :class:`~tokenizers.pre_tokenizers.ByteLevel` /// :class:`~tokenizers.pre_tokenizers.PreTokenizer`. #[pyclass(extends=PyDecoder, module = "tokenizers.decoders", name = "ByteLevel")] pub struct PyByteLevelDec {} #[pymethods] impl PyByteLevelDec { #[new] #[pyo3(signature = (**_kwargs), text_signature = "(self)")] fn new(_kwargs: Option<&Bound<'_, PyDict>>) -> (Self, PyDecoder) { (PyByteLevelDec {}, ByteLevel::default().into()) } } /// Replace Decoder /// /// This decoder is to be used in tandem with the :class:`~tokenizers.pre_tokenizers.Replace` /// :class:`~tokenizers.pre_tokenizers.PreTokenizer`. #[pyclass(extends=PyDecoder, module = "tokenizers.decoders", name = "Replace")] pub struct PyReplaceDec {} #[pymethods] impl PyReplaceDec { #[new] #[pyo3(text_signature = "(self, pattern, content)")] fn new(pattern: PyPattern, content: String) -> PyResult<(Self, PyDecoder)> { Ok(( PyReplaceDec {}, ToPyResult(Replace::new(pattern, content)).into_py()?.into(), )) } } /// WordPiece Decoder /// /// Args: /// prefix (:obj:`str`, `optional`, defaults to :obj:`##`): /// The prefix to use for subwords that are not a beginning-of-word /// /// cleanup (:obj:`bool`, `optional`, defaults to :obj:`True`): /// Whether to cleanup some tokenization artifacts. Mainly spaces before punctuation, /// and some abbreviated english forms. #[pyclass(extends=PyDecoder, module = "tokenizers.decoders", name = "WordPiece")] pub struct PyWordPieceDec {} #[pymethods] impl PyWordPieceDec { #[getter] fn get_prefix(self_: PyRef<Self>) -> String { getter!(self_, WordPiece, prefix.clone()) } #[setter] fn set_prefix(self_: PyRef<Self>, prefix: String) { setter!(self_, WordPiece, prefix, prefix); } #[getter] fn get_cleanup(self_: PyRef<Self>) -> bool { getter!(self_, WordPiece, cleanup) } #[setter] fn set_cleanup(self_: PyRef<Self>, cleanup: bool) { setter!(self_, WordPiece, cleanup, cleanup); } #[new] #[pyo3(signature = (prefix = String::from("##"), cleanup = true), text_signature = "(self, prefix=\"##\", cleanup=True)")] fn new(prefix: String, cleanup: bool) -> (Self, PyDecoder) { (PyWordPieceDec {}, WordPiece::new(prefix, cleanup).into()) } } /// ByteFallback Decoder /// ByteFallback is a simple trick which converts tokens looking like `<0x61>` /// to pure bytes, and attempts to make them into a string. If the tokens /// cannot be decoded you will get � instead for each inconvertible byte token /// #[pyclass(extends=PyDecoder, module = "tokenizers.decoders", name = "ByteFallback")] pub struct PyByteFallbackDec {} #[pymethods] impl PyByteFallbackDec { #[new] #[pyo3(signature = (), text_signature = "(self)")] fn new() -> (Self, PyDecoder) { (PyByteFallbackDec {}, ByteFallback::new().into()) } } /// Fuse Decoder /// Fuse simply fuses every token into a single string. /// This is the last step of decoding, this decoder exists only if /// there is need to add other decoders *after* the fusion #[pyclass(extends=PyDecoder, module = "tokenizers.decoders", name = "Fuse")] pub struct PyFuseDec {} #[pymethods] impl PyFuseDec { #[new] #[pyo3(signature = (), text_signature = "(self)")] fn new() -> (Self, PyDecoder) { (PyFuseDec {}, Fuse::new().into()) } } /// Strip normalizer /// Strips n left characters of each token, or n right characters of each token #[pyclass(extends=PyDecoder, module = "tokenizers.decoders", name = "Strip")] pub struct PyStrip {} #[pymethods] impl PyStrip { #[getter] fn get_start(self_: PyRef<Self>) -> usize { getter!(self_, Strip, start) } #[setter] fn set_start(self_: PyRef<Self>, start: usize) { setter!(self_, Strip, start, start) } #[getter] fn get_stop(self_: PyRef<Self>) -> usize { getter!(self_, Strip, stop) } #[setter] fn set_stop(self_: PyRef<Self>, stop: usize) { setter!(self_, Strip, stop, stop) } #[getter] fn get_content(self_: PyRef<Self>) -> char { getter!(self_, Strip, content) } #[setter] fn set_content(self_: PyRef<Self>, content: char) { setter!(self_, Strip, content, content) } #[new] #[pyo3(signature = (content=' ', left=0, right=0), text_signature = "(self, content, left=0, right=0)")] fn new(content: char, left: usize, right: usize) -> (Self, PyDecoder) { (PyStrip {}, Strip::new(content, left, right).into()) } } /// Metaspace Decoder /// /// Args: /// replacement (:obj:`str`, `optional`, defaults to :obj:`▁`): /// The replacement character. Must be exactly one character. By default we /// use the `▁` (U+2581) meta symbol (Same as in SentencePiece). /// /// prepend_scheme (:obj:`str`, `optional`, defaults to :obj:`"always"`): /// Whether to add a space to the first word if there isn't already one. This /// lets us treat `hello` exactly like `say hello`. /// Choices: "always", "never", "first". First means the space is only added on the first /// token (relevant when special tokens are used or other pre_tokenizer are used). #[pyclass(extends=PyDecoder, module = "tokenizers.decoders", name = "Metaspace")] pub struct PyMetaspaceDec {} #[pymethods] impl PyMetaspaceDec { #[getter] fn get_replacement(self_: PyRef<Self>) -> String { getter!(self_, Metaspace, get_replacement().to_string()) } #[setter] fn set_replacement(self_: PyRef<Self>, replacement: char) { setter!(self_, Metaspace, @set_replacement, replacement); } #[getter] fn get_split(self_: PyRef<Self>) -> bool { getter!(self_, Metaspace, get_split()) } #[setter] fn set_split(self_: PyRef<Self>, split: bool) { setter!(self_, Metaspace, @set_split, split); } #[getter] fn get_prepend_scheme(self_: PyRef<Self>) -> String { // Assuming Metaspace has a method to get the prepend_scheme as a string let scheme: PrependScheme = getter!(self_, Metaspace, get_prepend_scheme()); match scheme { PrependScheme::First => "first", PrependScheme::Never => "never", PrependScheme::Always => "always", } .to_string() } #[setter] fn set_prepend_scheme(self_: PyRef<Self>, prepend_scheme: String) -> PyResult<()> { let scheme = from_string(prepend_scheme)?; setter!(self_, Metaspace, @set_prepend_scheme, scheme); Ok(()) } #[new] #[pyo3(signature = (replacement = '▁', prepend_scheme = String::from("always"), split = true), text_signature = "(self, replacement = \"▁\", prepend_scheme = \"always\", split = True)")] fn new(replacement: char, prepend_scheme: String, split: bool) -> PyResult<(Self, PyDecoder)> { let prepend_scheme = from_string(prepend_scheme)?; Ok(( PyMetaspaceDec {}, Metaspace::new(replacement, prepend_scheme, split).into(), )) } } /// BPEDecoder Decoder /// /// Args: /// suffix (:obj:`str`, `optional`, defaults to :obj:`</w>`): /// The suffix that was used to caracterize an end-of-word. This suffix will /// be replaced by whitespaces during the decoding #[pyclass(extends=PyDecoder, module = "tokenizers.decoders", name = "BPEDecoder")] pub struct PyBPEDecoder {} #[pymethods] impl PyBPEDecoder { #[getter] fn get_suffix(self_: PyRef<Self>) -> String { getter!(self_, BPE, suffix.clone()) } #[setter] fn set_suffix(self_: PyRef<Self>, suffix: String) { setter!(self_, BPE, suffix, suffix); } #[new] #[pyo3(signature = (suffix = String::from("</w>")), text_signature = "(self, suffix=\"</w>\")")] fn new(suffix: String) -> (Self, PyDecoder) { (PyBPEDecoder {}, BPEDecoder::new(suffix).into()) } } /// CTC Decoder /// /// Args: /// pad_token (:obj:`str`, `optional`, defaults to :obj:`<pad>`): /// The pad token used by CTC to delimit a new token. /// word_delimiter_token (:obj:`str`, `optional`, defaults to :obj:`|`): /// The word delimiter token. It will be replaced by a <space> /// cleanup (:obj:`bool`, `optional`, defaults to :obj:`True`): /// Whether to cleanup some tokenization artifacts. /// Mainly spaces before punctuation, and some abbreviated english forms. #[pyclass(extends=PyDecoder, module = "tokenizers.decoders", name = "CTC")] pub struct PyCTCDecoder {} #[pymethods] impl PyCTCDecoder { #[getter] fn get_pad_token(self_: PyRef<Self>) -> String { getter!(self_, CTC, pad_token.clone()) } #[setter] fn set_pad_token(self_: PyRef<Self>, pad_token: String) { setter!(self_, CTC, pad_token, pad_token); } #[getter] fn get_word_delimiter_token(self_: PyRef<Self>) -> String { getter!(self_, CTC, word_delimiter_token.clone()) } #[setter] fn set_word_delimiter_token(self_: PyRef<Self>, word_delimiter_token: String) { setter!(self_, CTC, word_delimiter_token, word_delimiter_token); } #[getter] fn get_cleanup(self_: PyRef<Self>) -> bool { getter!(self_, CTC, cleanup) } #[setter] fn set_cleanup(self_: PyRef<Self>, cleanup: bool) { setter!(self_, CTC, cleanup, cleanup); } #[new] #[pyo3(signature = ( pad_token = String::from("<pad>"), word_delimiter_token = String::from("|"), cleanup = true ), text_signature = "(self, pad_token=\"<pad>\", word_delimiter_token=\"|\", cleanup=True)")] fn new(pad_token: String, word_delimiter_token: String, cleanup: bool) -> (Self, PyDecoder) { ( PyCTCDecoder {}, CTC::new(pad_token, word_delimiter_token, cleanup).into(), ) } } /// Sequence Decoder /// /// Args: /// decoders (:obj:`List[Decoder]`) /// The decoders that need to be chained #[pyclass(extends=PyDecoder, module = "tokenizers.decoders", name="Sequence")] pub struct PySequenceDecoder {} #[pymethods] impl PySequenceDecoder { #[new] #[pyo3(signature = (decoders_py), text_signature = "(self, decoders)")] fn new(decoders_py: &Bound<'_, PyList>) -> PyResult<(Self, PyDecoder)> { let mut decoders: Vec<DecoderWrapper> = Vec::with_capacity(decoders_py.len()); for decoder_py in decoders_py.iter() { let decoder: PyRef<PyDecoder> = decoder_py.extract()?; let decoder = match &decoder.decoder { PyDecoderWrapper::Wrapped(inner) => inner, PyDecoderWrapper::Custom(_) => unimplemented!(), }; decoders.push(decoder.read().unwrap().clone()); } Ok((PySequenceDecoder {}, Sequence::new(decoders).into())) } fn __getnewargs__<'p>(&self, py: Python<'p>) -> PyResult<Bound<'p, PyTuple>> { PyTuple::new(py, [PyList::empty(py)]) } } pub(crate) struct CustomDecoder { inner: PyObject, } impl CustomDecoder { pub(crate) fn new(inner: PyObject) -> Self { CustomDecoder { inner } } } impl Decoder for CustomDecoder { fn decode(&self, tokens: Vec<String>) -> tk::Result<String> { Python::with_gil(|py| { let decoded = self .inner .call_method(py, "decode", (tokens,), None)? .extract(py)?; Ok(decoded) }) } fn decode_chain(&self, tokens: Vec<String>) -> tk::Result<Vec<String>> { Python::with_gil(|py| { let decoded = self .inner .call_method(py, "decode_chain", (tokens,), None)? .extract(py)?; Ok(decoded) }) } } impl Serialize for CustomDecoder { fn serialize<S>(&self, _serializer: S) -> std::result::Result<S::Ok, S::Error> where S: Serializer, { Err(serde::ser::Error::custom( "Custom PyDecoder cannot be serialized", )) } } impl<'de> Deserialize<'de> for CustomDecoder { fn deserialize<D>(_deserializer: D) -> std::result::Result<Self, D::Error> where D: Deserializer<'de>, { Err(D::Error::custom("PyDecoder cannot be deserialized")) } } #[derive(Clone, Deserialize, Serialize)] #[serde(untagged)] pub(crate) enum PyDecoderWrapper { Custom(Arc<RwLock<CustomDecoder>>), Wrapped(Arc<RwLock<DecoderWrapper>>), } impl<I> From<I> for PyDecoderWrapper where I: Into<DecoderWrapper>, { fn from(norm: I) -> Self { PyDecoderWrapper::Wrapped(Arc::new(RwLock::new(norm.into()))) } } impl<I> From<I> for PyDecoder where I: Into<DecoderWrapper>, { fn from(dec: I) -> Self { PyDecoder { decoder: dec.into().into(), } } } impl Decoder for PyDecoderWrapper { fn decode_chain(&self, tokens: Vec<String>) -> tk::Result<Vec<String>> { match self { PyDecoderWrapper::Wrapped(inner) => inner.read().unwrap().decode_chain(tokens), PyDecoderWrapper::Custom(inner) => inner.read().unwrap().decode_chain(tokens), } } } /// Decoders Module #[pymodule] pub fn decoders(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_class::<PyDecoder>()?; m.add_class::<PyByteLevelDec>()?; m.add_class::<PyReplaceDec>()?; m.add_class::<PyWordPieceDec>()?; m.add_class::<PyByteFallbackDec>()?; m.add_class::<PyFuseDec>()?; m.add_class::<PyStrip>()?; m.add_class::<PyMetaspaceDec>()?; m.add_class::<PyBPEDecoder>()?; m.add_class::<PyCTCDecoder>()?; m.add_class::<PySequenceDecoder>()?; m.add_class::<PyDecodeStream>()?; Ok(()) } /// Class needed for streaming decode /// #[pyclass(module = "tokenizers.decoders", name = "DecodeStream")] #[derive(Clone)] pub struct PyDecodeStream { /// Regular decode option that is kept throughout. skip_special_tokens: bool, /// A temporary buffer of the necessary token_ids needed /// to produce valid string chunks. /// This typically contains 3 parts: /// - read /// - prefix /// - rest /// /// Read is the bit necessary to surround the prefix /// so decoding the whole ids produces a valid prefix. /// Prefix is the previously produced string, kept around to trim off of /// the next valid chunk ids: Vec<u32>, /// The previously returned chunk that needs to be discarded from the /// decoding of the current ids to produce the next chunk prefix: String, /// The index within the ids corresponding to the prefix so we can drain /// correctly prefix_index: usize, } #[pymethods] impl PyDecodeStream { #[new] #[pyo3(signature = (skip_special_tokens), text_signature = "(self, skip_special_tokens)")] fn new(skip_special_tokens: bool) -> Self { PyDecodeStream { skip_special_tokens, ids: vec![], prefix: "".to_string(), prefix_index: 0, } } #[pyo3(signature = (tokenizer, id), text_signature = "(self, tokenizer, id)")] fn step(&mut self, tokenizer: &PyTokenizer, id: u32) -> PyResult<Option<String>> { ToPyResult(tk::tokenizer::step_decode_stream( &tokenizer.tokenizer, id, self.skip_special_tokens, &mut self.ids, &mut self.prefix, &mut self.prefix_index, )) .into() } } #[cfg(test)] mod test { use std::sync::{Arc, RwLock}; use pyo3::prelude::*; use tk::decoders::metaspace::Metaspace; use tk::decoders::DecoderWrapper; use crate::decoders::{CustomDecoder, PyDecoder, PyDecoderWrapper}; #[test] fn get_subtype() { Python::with_gil(|py| { let py_dec = PyDecoder::new(Metaspace::default().into()); let py_meta = py_dec.get_as_subtype(py).unwrap(); assert_eq!("Metaspace", py_meta.bind(py).get_type().qualname().unwrap()); }) } #[test] fn serialize() { let py_wrapped: PyDecoderWrapper = Metaspace::default().into(); let py_ser = serde_json::to_string(&py_wrapped).unwrap(); let rs_wrapped = DecoderWrapper::Metaspace(Metaspace::default()); let rs_ser = serde_json::to_string(&rs_wrapped).unwrap(); assert_eq!(py_ser, rs_ser); let py_dec: PyDecoder = serde_json::from_str(&rs_ser).unwrap(); match py_dec.decoder { PyDecoderWrapper::Wrapped(msp) => match *msp.as_ref().read().unwrap() { DecoderWrapper::Metaspace(_) => {} _ => panic!("Expected Metaspace"), }, _ => panic!("Expected wrapped, not custom."), } let obj = Python::with_gil(|py| { let py_msp = PyDecoder::new(Metaspace::default().into()); let obj: PyObject = Py::new(py, py_msp) .unwrap() .into_pyobject(py) .unwrap() .into_any() .into(); obj }); let py_seq = PyDecoderWrapper::Custom(Arc::new(RwLock::new(CustomDecoder::new(obj)))); assert!(serde_json::to_string(&py_seq).is_err()); } }
tokenizers/bindings/python/src/decoders.rs/0
{ "file_path": "tokenizers/bindings/python/src/decoders.rs", "repo_id": "tokenizers", "token_count": 11043 }
use serde::de::value::Error; use serde::{ser, Serialize}; type Result<T> = ::std::result::Result<T, Error>; pub struct Serializer { // This string starts empty and JSON is appended as values are serialized. output: String, /// Each levels remembers its own number of elements num_elements: Vec<usize>, max_elements: usize, level: usize, max_depth: usize, /// Maximum string representation /// Useful to ellipsis precompiled_charmap max_string: usize, } // By convention, the public API of a Serde serializer is one or more `to_abc` // functions such as `to_string`, `to_bytes`, or `to_writer` depending on what // Rust types the serializer is able to produce as output. // // This basic serializer supports only `to_string`. pub fn to_string<T>(value: &T) -> Result<String> where T: Serialize, { let max_depth = 20; let max_elements = 6; let max_string = 100; let mut serializer = Serializer { output: String::new(), level: 0, max_depth, max_elements, num_elements: vec![0; max_depth], max_string, }; value.serialize(&mut serializer)?; Ok(serializer.output) } pub fn repr<T>(value: &T) -> Result<String> where T: Serialize, { let max_depth = 200; let max_string = usize::MAX; let mut serializer = Serializer { output: String::new(), level: 0, max_depth, max_elements: 100, num_elements: vec![0; max_depth], max_string, }; value.serialize(&mut serializer)?; Ok(serializer.output) } impl ser::Serializer for &mut Serializer { // The output type produced by this `Serializer` during successful // serialization. Most serializers that produce text or binary output should // set `Ok = ()` and serialize into an `io::Write` or buffer contained // within the `Serializer` instance, as happens here. Serializers that build // in-memory data structures may be simplified by using `Ok` to propagate // the data structure around. type Ok = (); // The error type when some error occurs during serialization. type Error = Error; // Associated types for keeping track of additional state while serializing // compound data structures like sequences and maps. In this case no // additional state is required beyond what is already stored in the // Serializer struct. type SerializeSeq = Self; type SerializeTuple = Self; type SerializeTupleStruct = Self; type SerializeTupleVariant = Self; type SerializeMap = Self; type SerializeStruct = Self; type SerializeStructVariant = Self; // Here we go with the simple methods. The following 12 methods receive one // of the primitive types of the data model and map it to JSON by appending // into the output string. fn serialize_bool(self, v: bool) -> Result<()> { self.output += if v { "True" } else { "False" }; Ok(()) } // JSON does not distinguish between different sizes of integers, so all // signed integers will be serialized the same and all unsigned integers // will be serialized the same. Other formats, especially compact binary // formats, may need independent logic for the different sizes. fn serialize_i8(self, v: i8) -> Result<()> { self.serialize_i64(i64::from(v)) } fn serialize_i16(self, v: i16) -> Result<()> { self.serialize_i64(i64::from(v)) } fn serialize_i32(self, v: i32) -> Result<()> { self.serialize_i64(i64::from(v)) } // Not particularly efficient but this is example code anyway. A more // performant approach would be to use the `itoa` crate. fn serialize_i64(self, v: i64) -> Result<()> { self.output += &v.to_string(); Ok(()) } fn serialize_u8(self, v: u8) -> Result<()> { self.serialize_u64(u64::from(v)) } fn serialize_u16(self, v: u16) -> Result<()> { self.serialize_u64(u64::from(v)) } fn serialize_u32(self, v: u32) -> Result<()> { self.serialize_u64(u64::from(v)) } fn serialize_u64(self, v: u64) -> Result<()> { self.output += &v.to_string(); Ok(()) } fn serialize_f32(self, v: f32) -> Result<()> { self.serialize_f64(f64::from(v)) } fn serialize_f64(self, v: f64) -> Result<()> { self.output += &v.to_string(); Ok(()) } // Serialize a char as a single-character string. Other formats may // represent this differently. fn serialize_char(self, v: char) -> Result<()> { self.serialize_str(&v.to_string()) } // This only works for strings that don't require escape sequences but you // get the idea. For example it would emit invalid JSON if the input string // contains a '"' character. fn serialize_str(self, v: &str) -> Result<()> { self.output += "\""; if v.len() > self.max_string { self.output += &v[..self.max_string]; self.output += "..."; } else { self.output += v; } self.output += "\""; Ok(()) } // Serialize a byte array as an array of bytes. Could also use a base64 // string here. Binary formats will typically represent byte arrays more // compactly. fn serialize_bytes(self, v: &[u8]) -> Result<()> { use serde::ser::SerializeSeq; let mut seq = self.serialize_seq(Some(v.len()))?; for byte in v { seq.serialize_element(byte)?; } seq.end() } // An absent optional is represented as the JSON `null`. fn serialize_none(self) -> Result<()> { self.serialize_unit() } // A present optional is represented as just the contained value. Note that // this is a lossy representation. For example the values `Some(())` and // `None` both serialize as just `null`. Unfortunately this is typically // what people expect when working with JSON. Other formats are encouraged // to behave more intelligently if possible. fn serialize_some<T>(self, value: &T) -> Result<()> where T: ?Sized + Serialize, { value.serialize(self) } // In Serde, unit means an anonymous value containing no data. Map this to // JSON as `null`. fn serialize_unit(self) -> Result<()> { self.output += "None"; Ok(()) } // Unit struct means a named value containing no data. Again, since there is // no data, map this to JSON as `null`. There is no need to serialize the // name in most formats. fn serialize_unit_struct(self, _name: &'static str) -> Result<()> { self.serialize_unit() } // When serializing a unit variant (or any other kind of variant), formats // can choose whether to keep track of it by index or by name. Binary // formats typically use the index of the variant and human-readable formats // typically use the name. fn serialize_unit_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, ) -> Result<()> { // self.serialize_str(variant) self.output += variant; Ok(()) } // As is done here, serializers are encouraged to treat newtype structs as // insignificant wrappers around the data they contain. fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<()> where T: ?Sized + Serialize, { value.serialize(self) } // Note that newtype variant (and all of the other variant serialization // methods) refer exclusively to the "externally tagged" enum // representation. // // Serialize this to JSON in externally tagged form as `{ NAME: VALUE }`. fn serialize_newtype_variant<T>( self, _name: &'static str, _variant_index: u32, variant: &'static str, value: &T, ) -> Result<()> where T: ?Sized + Serialize, { // variant.serialize(&mut *self)?; self.output += variant; self.output += "("; value.serialize(&mut *self)?; self.output += ")"; Ok(()) } // Now we get to the serialization of compound types. // // The start of the sequence, each value, and the end are three separate // method calls. This one is responsible only for serializing the start, // which in JSON is `[`. // // The length of the sequence may or may not be known ahead of time. This // doesn't make a difference in JSON because the length is not represented // explicitly in the serialized form. Some serializers may only be able to // support sequences for which the length is known up front. fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> { self.output += "["; self.level = std::cmp::min(self.max_depth - 1, self.level + 1); self.num_elements[self.level] = 0; Ok(self) } // Tuples look just like sequences in JSON. Some formats may be able to // represent tuples more efficiently by omitting the length, since tuple // means that the corresponding `Deserialize implementation will know the // length without needing to look at the serialized data. fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> { self.output += "("; self.level = std::cmp::min(self.max_depth - 1, self.level + 1); self.num_elements[self.level] = 0; Ok(self) } // Tuple structs look just like sequences in JSON. fn serialize_tuple_struct( self, _name: &'static str, len: usize, ) -> Result<Self::SerializeTupleStruct> { self.serialize_tuple(len) } // Tuple variants are represented in JSON as `{ NAME: [DATA...] }`. Again // this method is only responsible for the externally tagged representation. fn serialize_tuple_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, _len: usize, ) -> Result<Self::SerializeTupleVariant> { // variant.serialize(&mut *self)?; self.output += variant; self.output += "("; self.level = std::cmp::min(self.max_depth - 1, self.level + 1); self.num_elements[self.level] = 0; Ok(self) } // Maps are represented in JSON as `{ K: V, K: V, ... }`. fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> { self.output += "{"; self.level = std::cmp::min(self.max_depth - 1, self.level + 1); self.num_elements[self.level] = 0; Ok(self) } // Structs look just like maps in JSON. In particular, JSON requires that we // serialize the field names of the struct. Other formats may be able to // omit the field names when serializing structs because the corresponding // Deserialize implementation is required to know what the keys are without // looking at the serialized data. fn serialize_struct(self, name: &'static str, _len: usize) -> Result<Self::SerializeStruct> { // self.serialize_map(Some(len)) // name.serialize(&mut *self)?; if let Some(stripped) = name.strip_suffix("Helper") { self.output += stripped; } else { self.output += name } self.output += "("; self.level = std::cmp::min(self.max_depth - 1, self.level + 1); self.num_elements[self.level] = 0; Ok(self) } // Struct variants are represented in JSON as `{ NAME: { K: V, ... } }`. // This is the externally tagged representation. fn serialize_struct_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, _len: usize, ) -> Result<Self::SerializeStructVariant> { // variant.serialize(&mut *self)?; self.output += variant; self.output += "("; self.level = std::cmp::min(self.max_depth - 1, self.level + 1); self.num_elements[self.level] = 0; Ok(self) } } // The following 7 impls deal with the serialization of compound types like // sequences and maps. Serialization of such types is begun by a Serializer // method and followed by zero or more calls to serialize individual elements of // the compound type and one call to end the compound type. // // This impl is SerializeSeq so these methods are called after `serialize_seq` // is called on the Serializer. impl ser::SerializeSeq for &mut Serializer { // Must match the `Ok` type of the serializer. type Ok = (); // Must match the `Error` type of the serializer. type Error = Error; // Serialize a single element of the sequence. fn serialize_element<T>(&mut self, value: &T) -> Result<()> where T: ?Sized + Serialize, { self.num_elements[self.level] += 1; let num_elements = self.num_elements[self.level]; if num_elements < self.max_elements { if !self.output.ends_with('[') { self.output += ", "; } value.serialize(&mut **self) } else { if num_elements == self.max_elements { self.output += ", ..."; } Ok(()) } } // Close the sequence. fn end(self) -> Result<()> { self.num_elements[self.level] = 0; self.level = self.level.saturating_sub(1); self.output += "]"; Ok(()) } } // Same thing but for tuples. impl ser::SerializeTuple for &mut Serializer { type Ok = (); type Error = Error; fn serialize_element<T>(&mut self, value: &T) -> Result<()> where T: ?Sized + Serialize, { self.num_elements[self.level] += 1; let num_elements = self.num_elements[self.level]; if num_elements < self.max_elements { if !self.output.ends_with('(') { self.output += ", "; } value.serialize(&mut **self) } else { if num_elements == self.max_elements { self.output += ", ..."; } Ok(()) } } fn end(self) -> Result<()> { self.num_elements[self.level] = 0; self.level = self.level.saturating_sub(1); self.output += ")"; Ok(()) } } // Same thing but for tuple structs. impl ser::SerializeTupleStruct for &mut Serializer { type Ok = (); type Error = Error; fn serialize_field<T>(&mut self, value: &T) -> Result<()> where T: ?Sized + Serialize, { self.num_elements[self.level] += 1; let num_elements = self.num_elements[self.level]; if num_elements < self.max_elements { if !self.output.ends_with('(') { self.output += ", "; } value.serialize(&mut **self) } else { if num_elements == self.max_elements { self.output += ", ..."; } Ok(()) } } fn end(self) -> Result<()> { self.num_elements[self.level] = 0; self.level = self.level.saturating_sub(1); self.output += ")"; Ok(()) } } // Tuple variants are a little different. Refer back to the // `serialize_tuple_variant` method above: // // self.output += "{"; // variant.serialize(&mut *self)?; // self.output += ":["; // // So the `end` method in this impl is responsible for closing both the `]` and // the `}`. impl ser::SerializeTupleVariant for &mut Serializer { type Ok = (); type Error = Error; fn serialize_field<T>(&mut self, value: &T) -> Result<()> where T: ?Sized + Serialize, { self.num_elements[self.level] += 1; let num_elements = self.num_elements[self.level]; if num_elements < self.max_elements { if !self.output.ends_with('(') { self.output += ", "; } value.serialize(&mut **self) } else { if num_elements == self.max_elements { self.output += ", ..."; } Ok(()) } } fn end(self) -> Result<()> { self.num_elements[self.level] = 0; self.level = self.level.saturating_sub(1); self.output += ")"; Ok(()) } } // Some `Serialize` types are not able to hold a key and value in memory at the // same time so `SerializeMap` implementations are required to support // `serialize_key` and `serialize_value` individually. // // There is a third optional method on the `SerializeMap` trait. The // `serialize_entry` method allows serializers to optimize for the case where // key and value are both available simultaneously. In JSON it doesn't make a // difference so the default behavior for `serialize_entry` is fine. impl ser::SerializeMap for &mut Serializer { type Ok = (); type Error = Error; // The Serde data model allows map keys to be any serializable type. JSON // only allows string keys so the implementation below will produce invalid // JSON if the key serializes as something other than a string. // // A real JSON serializer would need to validate that map keys are strings. // This can be done by using a different Serializer to serialize the key // (instead of `&mut **self`) and having that other serializer only // implement `serialize_str` and return an error on any other data type. fn serialize_key<T>(&mut self, key: &T) -> Result<()> where T: ?Sized + Serialize, { self.num_elements[self.level] += 1; let num_elements = self.num_elements[self.level]; if num_elements < self.max_elements { if !self.output.ends_with('{') { self.output += ", "; } key.serialize(&mut **self) } else { if num_elements == self.max_elements { self.output += ", ..."; } Ok(()) } } // It doesn't make a difference whether the colon is printed at the end of // `serialize_key` or at the beginning of `serialize_value`. In this case // the code is a bit simpler having it here. fn serialize_value<T>(&mut self, value: &T) -> Result<()> where T: ?Sized + Serialize, { let num_elements = self.num_elements[self.level]; if num_elements < self.max_elements { self.output += ":"; value.serialize(&mut **self) } else { Ok(()) } } fn end(self) -> Result<()> { self.num_elements[self.level] = 0; self.level = self.level.saturating_sub(1); self.output += "}"; Ok(()) } } // Structs are like maps in which the keys are constrained to be compile-time // constant strings. impl ser::SerializeStruct for &mut Serializer { type Ok = (); type Error = Error; fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()> where T: ?Sized + Serialize, { if !self.output.ends_with('(') { self.output += ", "; } // key.serialize(&mut **self)?; if key != "type" { self.output += key; self.output += "="; value.serialize(&mut **self) } else { Ok(()) } } fn end(self) -> Result<()> { self.num_elements[self.level] = 0; self.level = self.level.saturating_sub(1); self.output += ")"; Ok(()) } } // Similar to `SerializeTupleVariant`, here the `end` method is responsible for // closing both of the curly braces opened by `serialize_struct_variant`. impl ser::SerializeStructVariant for &mut Serializer { type Ok = (); type Error = Error; fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()> where T: ?Sized + Serialize, { if !self.output.ends_with('(') { self.output += ", "; } // key.serialize(&mut **self)?; self.output += key; self.output += "="; value.serialize(&mut **self) } fn end(self) -> Result<()> { self.num_elements[self.level] = 0; self.level = self.level.saturating_sub(1); self.output += ")"; Ok(()) } } //////////////////////////////////////////////////////////////////////////////// #[test] fn test_basic() { assert_eq!(to_string(&true).unwrap(), "True"); assert_eq!(to_string(&Some(1)).unwrap(), "1"); assert_eq!(to_string(&None::<usize>).unwrap(), "None"); } #[test] fn test_struct() { #[derive(Serialize)] struct Test { int: u32, seq: Vec<&'static str>, } let test = Test { int: 1, seq: vec!["a", "b"], }; let expected = r#"Test(int=1, seq=["a", "b"])"#; assert_eq!(to_string(&test).unwrap(), expected); } #[test] fn test_enum() { #[derive(Serialize)] enum E { Unit, Newtype(u32), Tuple(u32, u32), Struct { a: u32 }, } let u = E::Unit; let expected = r#"Unit"#; assert_eq!(to_string(&u).unwrap(), expected); let n = E::Newtype(1); let expected = r#"Newtype(1)"#; assert_eq!(to_string(&n).unwrap(), expected); let t = E::Tuple(1, 2); let expected = r#"Tuple(1, 2)"#; assert_eq!(to_string(&t).unwrap(), expected); let s = E::Struct { a: 1 }; let expected = r#"Struct(a=1)"#; assert_eq!(to_string(&s).unwrap(), expected); } #[test] fn test_enum_untagged() { #[derive(Serialize)] #[serde(untagged)] enum E { Unit, Newtype(u32), Tuple(u32, u32), Struct { a: u32 }, } let u = E::Unit; let expected = r#"None"#; assert_eq!(to_string(&u).unwrap(), expected); let n = E::Newtype(1); let expected = r#"1"#; assert_eq!(to_string(&n).unwrap(), expected); let t = E::Tuple(1, 2); let expected = r#"(1, 2)"#; assert_eq!(to_string(&t).unwrap(), expected); let s = E::Struct { a: 1 }; let expected = r#"E(a=1)"#; assert_eq!(to_string(&s).unwrap(), expected); } #[test] fn test_struct_tagged() { #[derive(Serialize)] #[serde(untagged)] enum E { A(A), } #[derive(Serialize)] #[serde(tag = "type")] struct A { a: bool, b: usize, } let u = A { a: true, b: 1 }; // let expected = r#"A(type="A", a=True, b=1)"#; // No we skip all `type` manually inserted variants. let expected = r#"A(a=True, b=1)"#; assert_eq!(to_string(&u).unwrap(), expected); let u = E::A(A { a: true, b: 1 }); let expected = r#"A(a=True, b=1)"#; assert_eq!(to_string(&u).unwrap(), expected); } #[test] fn test_flatten() { #[derive(Serialize)] struct A { a: bool, b: usize, } #[derive(Serialize)] struct B { c: A, d: usize, } #[derive(Serialize)] struct C { #[serde(flatten)] c: A, d: usize, } #[derive(Serialize)] #[serde(transparent)] struct D { e: A, } let u = B { c: A { a: true, b: 1 }, d: 2, }; let expected = r#"B(c=A(a=True, b=1), d=2)"#; assert_eq!(to_string(&u).unwrap(), expected); let u = C { c: A { a: true, b: 1 }, d: 2, }; // XXX This is unfortunate but true, flatten forces the serialization // to use the serialize_map without any means for the Serializer to know about this // flattening attempt let expected = r#"{"a":True, "b":1, "d":2}"#; assert_eq!(to_string(&u).unwrap(), expected); let u = D { e: A { a: true, b: 1 }, }; let expected = r#"A(a=True, b=1)"#; assert_eq!(to_string(&u).unwrap(), expected); }
tokenizers/bindings/python/src/utils/serde_pyo3.rs/0
{ "file_path": "tokenizers/bindings/python/src/utils/serde_pyo3.rs", "repo_id": "tokenizers", "token_count": 10084 }
# flake8: noqa import gzip import os import datasets import pytest from ..utils import data_dir, train_files class TestTrainFromIterators: @staticmethod def get_tokenizer_trainer(): # START init_tokenizer_trainer from tokenizers import Tokenizer, decoders, models, normalizers, pre_tokenizers, trainers tokenizer = Tokenizer(models.Unigram()) tokenizer.normalizer = normalizers.NFKC() tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel() tokenizer.decoder = decoders.ByteLevel() trainer = trainers.UnigramTrainer( vocab_size=20000, initial_alphabet=pre_tokenizers.ByteLevel.alphabet(), special_tokens=["<PAD>", "<BOS>", "<EOS>"], ) # END init_tokenizer_trainer trainer.show_progress = False return tokenizer, trainer @staticmethod def load_dummy_dataset(): # START load_dataset import datasets dataset = datasets.load_dataset("wikitext", "wikitext-103-raw-v1", split="train+test+validation") # END load_dataset @pytest.fixture(scope="class") def setup_gzip_files(self, train_files): with open(train_files["small"], "rt") as small: for n in range(3): path = f"data/my-file.{n}.gz" with gzip.open(path, "wt") as f: f.write(small.read()) def test_train_basic(self): tokenizer, trainer = self.get_tokenizer_trainer() # START train_basic # First few lines of the "Zen of Python" https://www.python.org/dev/peps/pep-0020/ data = [ "Beautiful is better than ugly." "Explicit is better than implicit." "Simple is better than complex." "Complex is better than complicated." "Flat is better than nested." "Sparse is better than dense." "Readability counts." ] tokenizer.train_from_iterator(data, trainer=trainer) # END train_basic def test_datasets(self): tokenizer, trainer = self.get_tokenizer_trainer() # In order to keep tests fast, we only use the first 100 examples os.environ["TOKENIZERS_PARALLELISM"] = "true" dataset = datasets.load_dataset("wikitext", "wikitext-103-raw-v1", split="train[0:100]") # START def_batch_iterator def batch_iterator(batch_size=1000): # Only keep the text column to avoid decoding the rest of the columns unnecessarily tok_dataset = dataset.select_columns("text") for batch in tok_dataset.iter(batch_size): yield batch["text"] # END def_batch_iterator # START train_datasets tokenizer.train_from_iterator(batch_iterator(), trainer=trainer, length=len(dataset)) # END train_datasets def test_gzip(self, setup_gzip_files): tokenizer, trainer = self.get_tokenizer_trainer() # START single_gzip import gzip with gzip.open("data/my-file.0.gz", "rt") as f: tokenizer.train_from_iterator(f, trainer=trainer) # END single_gzip # START multi_gzip files = ["data/my-file.0.gz", "data/my-file.1.gz", "data/my-file.2.gz"] def gzip_iterator(): for path in files: with gzip.open(path, "rt") as f: for line in f: yield line tokenizer.train_from_iterator(gzip_iterator(), trainer=trainer) # END multi_gzip
tokenizers/bindings/python/tests/documentation/test_tutorial_train_from_iterators.py/0
{ "file_path": "tokenizers/bindings/python/tests/documentation/test_tutorial_train_from_iterators.py", "repo_id": "tokenizers", "token_count": 1595 }
# Input Sequences <tokenizerslangcontent> <python> These types represent all the different kinds of sequence that can be used as input of a Tokenizer. Globally, any sequence can be either a string or a list of strings, according to the operating mode of the tokenizer: `raw text` vs `pre-tokenized`. ## TextInputSequence[[tokenizers.TextInputSequence]] <code>tokenizers.TextInputSequence</code> A `str` that represents an input sequence ## PreTokenizedInputSequence[[tokenizers.PreTokenizedInputSequence]] <code>tokenizers.PreTokenizedInputSequence</code> A pre-tokenized input sequence. Can be one of: - A `List` of `str` - A `Tuple` of `str` alias of `Union[List[str], Tuple[str]]`. ## InputSequence[[tokenizers.InputSequence]] <code>tokenizers.InputSequence</code> Represents all the possible types of input sequences for encoding. Can be: - When `is_pretokenized=False`: [TextInputSequence](#tokenizers.TextInputSequence) - When `is_pretokenized=True`: [PreTokenizedInputSequence](#tokenizers.PreTokenizedInputSequence) alias of `Union[str, List[str], Tuple[str]]`. </python> <rust> The Rust API Reference is available directly on the [Docs.rs](https://docs.rs/tokenizers/latest/tokenizers/) website. </rust> <node> The node API has not been documented yet. </node> </tokenizerslangcontent>
tokenizers/docs/source-doc-builder/api/input-sequences.mdx/0
{ "file_path": "tokenizers/docs/source-doc-builder/api/input-sequences.mdx", "repo_id": "tokenizers", "token_count": 402 }
import re from sphinx.directives.other import TocTree class TocTreeTags(TocTree): hasPat = re.compile("^\s*:(.+):(.+)$") def filter_entries(self, entries): filtered = [] for e in entries: m = self.hasPat.match(e) if m != None: if self.env.app.tags.has(m.groups()[0]): filtered.append(m.groups()[1]) else: filtered.append(e) return filtered def run(self): self.content = self.filter_entries(self.content) return super().run() def setup(app): app.add_directive("toctree-tags", TocTreeTags) return { "version": "0.1", }
tokenizers/docs/source/_ext/toctree_tags.py/0
{ "file_path": "tokenizers/docs/source/_ext/toctree_tags.py", "repo_id": "tokenizers", "token_count": 345 }
Installation ==================================================================================================== .. only:: python .. include:: python.inc .. only:: rust .. include:: rust.inc .. only:: node .. include:: node.inc
tokenizers/docs/source/installation/main.rst/0
{ "file_path": "tokenizers/docs/source/installation/main.rst", "repo_id": "tokenizers", "token_count": 54 }
#[macro_use] extern crate criterion; use std::fs::File; use std::io::{BufRead, BufReader}; use std::path::Path; use std::time::{Duration, Instant}; use criterion::black_box; use criterion::Criterion; use tokenizers::processors::template::TemplateProcessing; use tokenizers::{EncodeInput, Encoding, PostProcessor, Tokenizer}; /// Simple TemplateProcessing fn create_processor() -> TemplateProcessing { TemplateProcessing::builder() .try_single("[CLS]:0 $A:0 [SEP]:0") .unwrap() .try_pair("[CLS]:0 $A:0 [SEP]:0 $B:1 [SEP]:1") .unwrap() .special_tokens(vec![("[CLS]", 0), ("[SEP]", 1)]) .build() .unwrap() } pub fn bench_layout(c: &mut Criterion) { let processor = create_processor(); let tokenizer = Tokenizer::from_file("data/albert-base-v1-tokenizer.json").unwrap(); let mut encodeds: Vec<Encoding> = vec![]; for line in BufReader::new(File::open(Path::new("data/big.txt")).unwrap()).lines() { let line: EncodeInput = line.unwrap().into(); let encoded: Encoding = tokenizer.encode(line, false).unwrap(); encodeds.push(encoded); } c.bench_function("TemplateProcessing single encode", |b| { b.iter_custom(|iters| { let mut duration = Duration::new(0, 0); for i in 0..iters as usize { let encoded_index = i % encodeds.len(); let encoded: Encoding = encodeds[encoded_index].clone(); let start = Instant::now(); let _ = black_box(processor.process(encoded, None, false)); duration = duration.checked_add(start.elapsed()).unwrap(); } duration }) }); c.bench_function("TemplateProcessing pair encode", |b| { b.iter_custom(|iters| { let mut duration = Duration::new(0, 0); for i in 0..iters as usize { let encoded_index = i % encodeds.len(); let encoded: Encoding = encodeds[encoded_index].clone(); let encoded_index2 = (i + 1) % encodeds.len(); let pair: Encoding = encodeds[encoded_index2].clone(); let start = Instant::now(); let _ = black_box(processor.process(encoded, Some(pair), false)); duration = duration.checked_add(start.elapsed()).unwrap(); } duration }) }); } criterion_group! { name = layout_benches; config = Criterion::default().sample_size(20); targets = bench_layout } criterion_main!(layout_benches);
tokenizers/tokenizers/benches/layout_benchmark.rs/0
{ "file_path": "tokenizers/tokenizers/benches/layout_benchmark.rs", "repo_id": "tokenizers", "token_count": 1158 }
<div align="center"> <h1><code>create-wasm-app</code></h1> <strong>An <code>npm init</code> template for kick starting a project that uses NPM packages containing Rust-generated WebAssembly and bundles them with Webpack.</strong> <p> <a href="https://travis-ci.org/rustwasm/create-wasm-app"><img src="https://img.shields.io/travis/rustwasm/create-wasm-app.svg?style=flat-square" alt="Build Status" /></a> </p> <h3> <a href="#usage">Usage</a> <span> | </span> <a href="https://discordapp.com/channels/442252698964721669/443151097398296587">Chat</a> </h3> <sub>Built with 🦀🕸 by <a href="https://rustwasm.github.io/">The Rust and WebAssembly Working Group</a></sub> </div> ## About This template is designed for depending on NPM packages that contain Rust-generated WebAssembly and using them to create a Website. * Want to create an NPM package with Rust and WebAssembly? [Check out `wasm-pack-template`.](https://github.com/rustwasm/wasm-pack-template) * Want to make a monorepo-style Website without publishing to NPM? Check out [`rust-webpack-template`](https://github.com/rustwasm/rust-webpack-template) and/or [`rust-parcel-template`](https://github.com/rustwasm/rust-parcel-template). ## 🚴 Usage ``` npm init wasm-app ``` ## 🔋 Batteries Included - `.gitignore`: ignores `node_modules` - `LICENSE-APACHE` and `LICENSE-MIT`: most Rust projects are licensed this way, so these are included for you - `README.md`: the file you are reading now! - `index.html`: a bare bones html document that includes the webpack bundle - `index.js`: example js file with a comment showing how to import and use a wasm pkg - `package.json` and `package-lock.json`: - pulls in devDependencies for using webpack: - [`webpack`](https://www.npmjs.com/package/webpack) - [`webpack-cli`](https://www.npmjs.com/package/webpack-cli) - [`webpack-dev-server`](https://www.npmjs.com/package/webpack-dev-server) - defines a `start` script to run `webpack-dev-server` - `webpack.config.js`: configuration file for bundling your js with webpack ## License Licensed under either of * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) at your option. ### Contribution Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
tokenizers/tokenizers/examples/unstable_wasm/www/README.md/0
{ "file_path": "tokenizers/tokenizers/examples/unstable_wasm/www/README.md", "repo_id": "tokenizers", "token_count": 893 }
#![warn(clippy::all)] #![allow(clippy::upper_case_acronyms)] #![doc(html_favicon_url = "https://huggingface.co/favicon.ico")] #![doc(html_logo_url = "https://huggingface.co/landing/assets/huggingface_logo.svg")] //! The core of `tokenizers`, written in Rust. //! Provides an implementation of today's most used tokenizers, with a focus on performance and //! versatility. //! //! # What is a Tokenizer //! //! A Tokenizer works as a pipeline, it processes some raw text as input and outputs an `Encoding`. //! The various steps of the pipeline are: //! //! 1. The `Normalizer`: in charge of normalizing the text. Common examples of normalization are //! the [unicode normalization standards](https://unicode.org/reports/tr15/#Norm_Forms), such as `NFD` or `NFKC`. //! More details about how to use the `Normalizers` are available on the //! [Hugging Face blog](https://huggingface.co/docs/tokenizers/components#normalizers) //! 2. The `PreTokenizer`: in charge of creating initial words splits in the text. The most common way of //! splitting text is simply on whitespace. //! 3. The `Model`: in charge of doing the actual tokenization. An example of a `Model` would be //! `BPE` or `WordPiece`. //! 4. The `PostProcessor`: in charge of post-processing the `Encoding` to add anything relevant //! that, for example, a language model would need, such as special tokens. //! //! ## Loading a pretrained tokenizer from the Hub //! ``` //! use tokenizers::tokenizer::{Result, Tokenizer}; //! //! fn main() -> Result<()> { //! # #[cfg(feature = "http")] //! # { //! // needs http feature enabled //! let tokenizer = Tokenizer::from_pretrained("bert-base-cased", None)?; //! //! let encoding = tokenizer.encode("Hey there!", false)?; //! println!("{:?}", encoding.get_tokens()); //! # } //! Ok(()) //! } //! ``` //! //! ## Deserialization and tokenization example //! //! ```no_run //! use tokenizers::tokenizer::{Result, Tokenizer, EncodeInput}; //! use tokenizers::models::bpe::BPE; //! //! fn main() -> Result<()> { //! let bpe_builder = BPE::from_file("./path/to/vocab.json", "./path/to/merges.txt"); //! let bpe = bpe_builder //! .dropout(0.1) //! .unk_token("[UNK]".into()) //! .build()?; //! //! let mut tokenizer = Tokenizer::new(bpe); //! //! let encoding = tokenizer.encode("Hey there!", false)?; //! println!("{:?}", encoding.get_tokens()); //! //! Ok(()) //! } //! ``` //! //! ## Training and serialization example //! //! ```no_run //! use tokenizers::decoders::DecoderWrapper; //! use tokenizers::models::bpe::{BpeTrainerBuilder, BPE}; //! use tokenizers::normalizers::{strip::Strip, unicode::NFC, utils::Sequence, NormalizerWrapper}; //! use tokenizers::pre_tokenizers::byte_level::ByteLevel; //! use tokenizers::pre_tokenizers::PreTokenizerWrapper; //! use tokenizers::processors::PostProcessorWrapper; //! use tokenizers::{AddedToken, Model, Result, TokenizerBuilder}; //! //! use std::path::Path; //! //! fn main() -> Result<()> { //! let vocab_size: usize = 100; //! //! let mut trainer = BpeTrainerBuilder::new() //! .show_progress(true) //! .vocab_size(vocab_size) //! .min_frequency(0) //! .special_tokens(vec![ //! AddedToken::from(String::from("<s>"), true), //! AddedToken::from(String::from("<pad>"), true), //! AddedToken::from(String::from("</s>"), true), //! AddedToken::from(String::from("<unk>"), true), //! AddedToken::from(String::from("<mask>"), true), //! ]) //! .build(); //! //! let mut tokenizer = TokenizerBuilder::new() //! .with_model(BPE::default()) //! .with_normalizer(Some(Sequence::new(vec![ //! Strip::new(true, true).into(), //! NFC.into(), //! ]))) //! .with_pre_tokenizer(Some(ByteLevel::default())) //! .with_post_processor(Some(ByteLevel::default())) //! .with_decoder(Some(ByteLevel::default())) //! .build()?; //! //! let pretty = false; //! tokenizer //! .train_from_files( //! &mut trainer, //! vec!["path/to/vocab.txt".to_string()], //! )? //! .save("tokenizer.json", pretty)?; //! //! Ok(()) //! } //! ``` //! //! # Additional information //! //! - tokenizers is designed to leverage CPU parallelism when possible. The level of parallelism is determined //! by the total number of core/threads your CPU provides but this can be tuned by setting the `RAYON_RS_NUM_THREADS` //! environment variable. As an example setting `RAYON_RS_NUM_THREADS=4` will allocate a maximum of 4 threads. //! **_Please note this behavior may evolve in the future_** //! //! # Features //! **progressbar**: The progress bar visualization is enabled by default. It might be disabled if //! compilation for certain targets is not supported by the [termios](https://crates.io/crates/termios) //! dependency of the [indicatif](https://crates.io/crates/indicatif) progress bar. //! **http**: This feature enables downloading the tokenizer via HTTP. It is disabled by default. //! With this feature enabled, `Tokenizer::from_pretrained` becomes accessible. #[macro_use] extern crate log; #[macro_use] extern crate lazy_static; #[macro_use] extern crate derive_builder; #[macro_use] pub mod utils; pub mod decoders; pub mod models; pub mod normalizers; pub mod pre_tokenizers; pub mod processors; pub mod tokenizer; // Re-export from tokenizer pub use tokenizer::*; // Re-export also parallelism utils pub use utils::parallelism; // Re-export for from_pretrained #[cfg(feature = "http")] pub use utils::from_pretrained::FromPretrainedParameters;
tokenizers/tokenizers/src/lib.rs/0
{ "file_path": "tokenizers/tokenizers/src/lib.rs", "repo_id": "tokenizers", "token_count": 2226 }
//! [WordPiece](https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/37842.pdf) //! model. use crate::models::bpe::BPE; use crate::tokenizer::{Model, Result, Token}; use std::{ borrow::Cow, collections::HashMap, fs::File, io::prelude::*, io::{BufRead, BufReader}, path::{Path, PathBuf}, }; mod serialization; mod trainer; pub use trainer::*; #[derive(thiserror::Error, Debug)] pub enum Error { #[error("WordPiece error: Missing [UNK] token from the vocabulary")] MissingUnkToken, } type Vocab = HashMap<String, u32>; type VocabR = HashMap<u32, String>; struct Config { files: Option<String>, vocab: Vocab, unk_token: String, continuing_subword_prefix: String, max_input_chars_per_word: usize, } /// A `WordPieceBuilder` can be used to create a `WordPiece` model with a custom configuration. pub struct WordPieceBuilder { config: Config, } impl Default for WordPieceBuilder { fn default() -> Self { Self { config: Config { files: None, vocab: HashMap::new(), unk_token: String::from("[UNK]"), continuing_subword_prefix: String::from("##"), max_input_chars_per_word: 100, }, } } } impl WordPieceBuilder { /// Construct a new `WordPieceBuilder`. pub fn new() -> Self { Self::default() } /// Set the input files. #[must_use] pub fn files(mut self, vocab: String) -> Self { self.config.files = Some(vocab); self } /// Set the vocab (token -> ID) mapping. #[must_use] pub fn vocab(mut self, vocab: Vocab) -> Self { self.config.vocab = vocab; self } /// The the `UNK` token for the vocab. #[must_use] pub fn unk_token(mut self, unk_token: String) -> Self { self.config.unk_token = unk_token; self } /// Set the prefix for continuing subwords. #[must_use] pub fn continuing_subword_prefix(mut self, continuing_subword_prefix: String) -> Self { self.config.continuing_subword_prefix = continuing_subword_prefix; self } /// Set the maximum number of input characters per word. #[must_use] pub fn max_input_chars_per_word(mut self, max_input_chars_per_word: usize) -> Self { self.config.max_input_chars_per_word = max_input_chars_per_word; self } /// Contructs a `WordPiece` model that uses the `WordPieceBuilder`'s configuration. pub fn build(mut self) -> Result<WordPiece> { if let Some(vocab) = self.config.files { self.config.vocab = WordPiece::read_file(&vocab)?; } let vocab_r = self .config .vocab .iter() .map(|(key, val)| (*val, key.to_owned())) .collect(); Ok(WordPiece { vocab: self.config.vocab, vocab_r, unk_token: self.config.unk_token, continuing_subword_prefix: self.config.continuing_subword_prefix, max_input_chars_per_word: self.config.max_input_chars_per_word, }) } } /// A /// [WordPiece](https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/37842.pdf) /// model. #[derive(Clone, PartialEq, Eq)] pub struct WordPiece { vocab: Vocab, vocab_r: VocabR, pub unk_token: String, pub continuing_subword_prefix: String, pub max_input_chars_per_word: usize, } impl std::fmt::Debug for WordPiece { fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { fmt.debug_struct("WordPiece") .field("unk_token", &self.unk_token) .field("continuing_subword_prefix", &self.continuing_subword_prefix) .field("max_input_chars_per_word", &self.max_input_chars_per_word) .field("vocab", &self.vocab.len()) .finish() } } impl Default for WordPiece { fn default() -> Self { Self { vocab: HashMap::new(), vocab_r: HashMap::new(), unk_token: String::from("[UNK]"), continuing_subword_prefix: String::from("##"), max_input_chars_per_word: 100, } } } impl WordPiece { /// Get a `WordPieceBuilder`. pub fn builder() -> WordPieceBuilder { WordPieceBuilder::new() } /// Read the given files to extract the vocab pub fn read_file(vocab: &str) -> Result<Vocab> { let file = File::open(vocab)?; let file = BufReader::new(file); let mut vocab = HashMap::new(); for (index, line) in file.lines().enumerate() { let line = line?; vocab.insert(line.trim_end().to_owned(), index as u32); } Ok(vocab) } /// Initialize a `WordPiece` model from a vocab mapping file. pub fn from_file(vocab: &str) -> WordPieceBuilder { WordPiece::builder().files(vocab.to_owned()) } /// Create a `WordPiece` model from a `BPE` model. pub fn from_bpe(bpe: &BPE) -> Self { let mut wp = Self::builder().vocab(bpe.get_vocab()).build().unwrap(); if let Some(unk) = bpe.get_unk_token() { unk.clone_into(&mut wp.unk_token); } if let Some(prefix) = bpe.get_continuing_subword_prefix() { prefix.clone_into(&mut wp.continuing_subword_prefix); } wp } } impl Model for WordPiece { type Trainer = WordPieceTrainer; fn get_vocab(&self) -> HashMap<String, u32> { self.vocab.clone() } fn get_vocab_size(&self) -> usize { self.vocab.len() } fn tokenize(&self, sequence: &str) -> Result<Vec<Token>> { let char_len = sequence.chars().count(); if char_len > self.max_input_chars_per_word { return Ok(vec![Token { value: self.unk_token.clone(), id: *self .vocab .get(&self.unk_token) .ok_or(Error::MissingUnkToken)?, offsets: (0, sequence.len()), }]); } let mut is_bad = false; let mut start = 0; let mut sub_tokens: Vec<Token> = vec![]; while start < sequence.len() { let mut end = sequence.len(); let mut cur_str = None; while start < end { let mut substr: Cow<str> = Cow::Borrowed(&sequence[start..end]); if start > 0 { substr = Cow::Owned(format!("{}{}", self.continuing_subword_prefix, substr)); } if self.vocab.contains_key(substr.as_ref()) { cur_str = Some(Token { id: self.vocab[substr.as_ref()], value: substr.to_string(), offsets: (start, end), }); break; } end -= substr.chars().last().map_or(1, |c| c.len_utf8()); } if cur_str.is_none() { is_bad = true; break; } sub_tokens.push(cur_str.unwrap()); start = end; } if is_bad { Ok(vec![Token { value: self.unk_token.clone(), id: *self .vocab .get(&self.unk_token) .ok_or(Error::MissingUnkToken)?, offsets: (0, sequence.len()), }]) } else { Ok(sub_tokens) } } fn token_to_id(&self, token: &str) -> Option<u32> { self.vocab.get(token).copied() } fn id_to_token(&self, id: u32) -> Option<String> { self.vocab_r.get(&id).cloned() } fn save(&self, folder: &Path, name: Option<&str>) -> Result<Vec<PathBuf>> { let vocab_file_name = match name { Some(name) => format!("{name}-vocab.txt"), None => "vocab.txt".to_string(), }; // Write vocab.txt let vocab_path: PathBuf = [folder, Path::new(vocab_file_name.as_str())] .iter() .collect(); let mut vocab_file = File::create(&vocab_path)?; let mut vocab: Vec<(&String, &u32)> = self.vocab.iter().collect(); vocab.sort_unstable_by_key(|k| *k.1); vocab_file.write_all( &vocab .into_iter() .flat_map(|(token, _)| format!("{token}\n").as_bytes().to_owned()) .collect::<Vec<_>>()[..], )?; Ok(vec![vocab_path]) } fn get_trainer(&self) -> Self::Trainer { WordPieceTrainer::builder().build() } } #[cfg(test)] mod tests { use super::*; #[test] fn test_error_display() { assert!(format!("{}", Error::MissingUnkToken).contains("Missing [UNK] token")); } }
tokenizers/tokenizers/src/models/wordpiece/mod.rs/0
{ "file_path": "tokenizers/tokenizers/src/models/wordpiece/mod.rs", "repo_id": "tokenizers", "token_count": 4422 }
use crate::tokenizer::{Decoder, PreTokenizedString, PreTokenizer, Result, SplitDelimiterBehavior}; use serde::{de, Deserialize, Deserializer, Serialize}; /// Enum representing options for the metaspace prepending scheme. #[derive(Debug, Clone, PartialEq, Serialize, Eq, Deserialize, Copy)] #[serde(rename_all = "snake_case")] pub enum PrependScheme { /// Specifies that the scheme should be prepended only once, on the first split. First, /// Specifies that the space should not be prepended. Never, /// Specifies that the scheme should always be prepended. Always, } impl std::fmt::Display for PrependScheme { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.serialize(f) } } #[derive(Debug, Clone, PartialEq, Serialize, Eq)] /// Replaces all the whitespaces by the provided meta character and then /// splits on this character #[serde(tag = "type")] pub struct Metaspace { replacement: char, pub prepend_scheme: PrependScheme, pub split: bool, #[serde(skip)] str_rep: String, } impl<'de> Deserialize<'de> for Metaspace { fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error> where D: Deserializer<'de>, { #[derive(Deserialize)] enum Type { Metaspace, } fn default_prepend_scheme_value() -> PrependScheme { PrependScheme::Always } #[derive(Deserialize)] pub struct MetaspaceHelper { #[serde(rename = "type")] _type: Type, replacement: char, pub add_prefix_space: Option<bool>, #[serde(default = "default_prepend_scheme_value")] pub prepend_scheme: PrependScheme, pub split: Option<bool>, #[serde(rename = "str_rep")] _str_rep: Option<String>, } let mut helper = MetaspaceHelper::deserialize(deserializer)?; if let Some(false) = helper.add_prefix_space { if helper.prepend_scheme != PrependScheme::Never { return Err(de::Error::custom( "add_prefix_space does not match declared prepend_scheme", )); } helper.prepend_scheme = PrependScheme::Never; } let instance = Self::new( helper.replacement, helper.prepend_scheme, helper.split.unwrap_or(true), ); Ok(instance) } } impl Metaspace { pub fn new(replacement: char, prepend_scheme: PrependScheme, split: bool) -> Self { Self { replacement, str_rep: replacement.to_string(), prepend_scheme, split, } } pub fn get_replacement(&self) -> char { self.replacement } pub fn set_replacement(&mut self, replacement: char) { self.replacement = replacement; self.str_rep = replacement.to_string(); } pub fn get_split(&self) -> bool { self.split } pub fn set_split(&mut self, split: bool) { self.split = split; } pub fn get_prepend_scheme(&self) -> PrependScheme { self.prepend_scheme } pub fn set_prepend_scheme(&mut self, scheme: PrependScheme) { self.prepend_scheme = scheme; } } impl Default for Metaspace { fn default() -> Self { Self::new('▁', PrependScheme::Always, true) } } impl PreTokenizer for Metaspace { fn pre_tokenize(&self, pretokenized: &mut PreTokenizedString) -> Result<()> { pretokenized.split(|_, mut normalized| { normalized.replace(' ', &self.str_rep)?; match self.prepend_scheme { PrependScheme::Always => { if !normalized.get().starts_with(self.replacement) { normalized.prepend(&self.str_rep); } } PrependScheme::First => { if !normalized.get().starts_with(self.replacement) && normalized.offsets_original().0 == 0 { normalized.prepend(&self.str_rep); } } PrependScheme::Never => {} }; if self.split { normalized.split(self.replacement, SplitDelimiterBehavior::MergedWithNext) } else { Ok(vec![normalized]) } }) } } impl Decoder for Metaspace { fn decode_chain(&self, tokens: Vec<String>) -> Result<Vec<String>> { Ok(tokens .iter() .enumerate() .map(|(i, token)| { token .chars() .flat_map(|c| { if c == self.replacement { if i == 0 && self.prepend_scheme != PrependScheme::Never { None } else { Some(' ') } } else { Some(c) } }) .collect::<String>() }) .collect()) } } #[cfg(test)] mod tests { use regex::Regex; use super::*; use crate::{OffsetReferential, OffsetType}; #[test] fn serialization() { let metaspace = Metaspace::new('_', PrependScheme::Always, true); let metaspace_s = r#"{"type":"Metaspace","replacement":"_","prepend_scheme":"always","split":true}"#; assert_eq!(serde_json::to_string(&metaspace).unwrap(), metaspace_s); assert_eq!( serde_json::from_str::<Metaspace>(metaspace_s).unwrap(), metaspace ); // Also check it can deserialize previous versions let metaspace_s = r#"{"type":"Metaspace","replacement":"_","add_prefix_space":false,"prepend_scheme":"always"}"#; assert!(serde_json::from_str::<Metaspace>(metaspace_s).is_err(),); let metaspace = Metaspace::new('_', PrependScheme::Always, true); let metaspace_s = r#"{"type":"Metaspace","str_rep":"_","replacement":"_","add_prefix_space":true,"prepend_scheme":"always"}"#; assert_eq!( serde_json::from_str::<Metaspace>(metaspace_s).unwrap(), metaspace ); let metaspace_parsed: Metaspace = serde_json::from_str( r#"{"type":"Metaspace","replacement":"_","add_prefix_space":true}"#, ) .unwrap(); assert_eq!(metaspace_parsed, metaspace); } #[test] fn basic() { let pretok = Metaspace::new('▁', PrependScheme::Always, true); let mut pretokenized = PreTokenizedString::from("Hey friend!"); pretok.pre_tokenize(&mut pretokenized).unwrap(); assert_eq!( pretokenized .get_splits(OffsetReferential::Normalized, OffsetType::Byte) .into_iter() .map(|(s, o, _)| (s, o)) .collect::<Vec<_>>(), vec![("▁Hey", (0, 6)), ("▁friend!", (6, 16))] ); assert_eq!( pretokenized .get_splits(OffsetReferential::Original, OffsetType::Byte) .into_iter() .map(|(s, o, _)| (s, o)) .collect::<Vec<_>>(), vec![("▁Hey", (0, 3)), ("▁friend!", (3, 11))] ); } #[test] fn multiple_spaces() { let pretok = Metaspace::new('▁', PrependScheme::Always, true); let mut pretokenized = PreTokenizedString::from("Hey friend!"); pretok.pre_tokenize(&mut pretokenized).unwrap(); assert_eq!( pretokenized .get_splits(OffsetReferential::Normalized, OffsetType::Byte) .into_iter() .map(|(s, o, _)| (s, o)) .collect::<Vec<_>>(), vec![ ("▁Hey", (0, 6)), ("▁", (6, 9)), ("▁", (9, 12)), ("▁friend!", (12, 22)), ] ); assert_eq!( pretokenized .get_splits(OffsetReferential::Original, OffsetType::Byte) .into_iter() .map(|(s, o, _)| (s, o)) .collect::<Vec<_>>(), vec![ ("▁Hey", (0, 3)), ("▁", (3, 4)), ("▁", (4, 5)), ("▁friend!", (5, 13)), ] ); } #[test] fn non_legacy_meta_space() { let mut pretok = Metaspace::new('▁', PrependScheme::Always, true); pretok.set_prepend_scheme(PrependScheme::Always); assert_eq!(pretok, Metaspace::new('▁', PrependScheme::Always, true)); pretok.set_prepend_scheme(PrependScheme::Never); assert_eq!(pretok, Metaspace::new('▁', PrependScheme::Never, true)); pretok.set_prepend_scheme(PrependScheme::First); assert_eq!(pretok, Metaspace::new('▁', PrependScheme::First, true)); let pretok = Metaspace::new('▁', PrependScheme::First, false); let mut pretokenized = PreTokenizedString::from("Hey my friend <s>how▁are you"); let re_ref = Regex::new(r"(<s>)").unwrap(); pretokenized .split(|_, sequence| sequence.split(&re_ref, SplitDelimiterBehavior::Isolated)) .expect("Bad split"); pretok.pre_tokenize(&mut pretokenized).unwrap(); assert_eq!( pretokenized .get_splits(OffsetReferential::Normalized, OffsetType::Byte) .into_iter() .map(|(s, o, _)| (s, o)) .collect::<Vec<_>>(), vec![ ("▁Hey▁my▁friend▁", (0, 23)), ("<s>", (23, 26)), ("how▁are▁you", (26, 41)) ] ); let pretok = Metaspace::new('▁', PrependScheme::Always, true); pretok.pre_tokenize(&mut pretokenized).unwrap(); assert_eq!( pretokenized .get_splits(OffsetReferential::Normalized, OffsetType::Byte) .into_iter() .map(|(s, o, _)| (s, o)) .collect::<Vec<_>>(), vec![ ("▁Hey", (0, 6)), ("▁my", (6, 11)), ("▁friend", (11, 20)), ("▁", (20, 23)), ("▁<s>", (23, 29)), ("▁how", (29, 35)), ("▁are", (35, 41)), ("▁you", (41, 47)) ] ); let pretok = Metaspace::new('▁', PrependScheme::First, false); let mut pretokenized = PreTokenizedString::from(" Hey <s>how"); // test with prefix pretokenized .split(|_, sequence| sequence.split(&re_ref, SplitDelimiterBehavior::Isolated)) .expect("Bad split"); pretok.pre_tokenize(&mut pretokenized).unwrap(); assert_eq!( pretokenized .get_splits(OffsetReferential::Normalized, OffsetType::Byte) .into_iter() .map(|(s, o, _)| (s, o)) .collect::<Vec<_>>(), vec![("▁Hey▁", (0, 9)), ("<s>", (9, 12)), ("how", (12, 15))] ); let mut pretokenized = PreTokenizedString::from(" Hey <s>how <s>are <s> you"); // test with many splits pretokenized .split(|_, sequence| sequence.split(&re_ref, SplitDelimiterBehavior::Isolated)) .expect("Bad split"); pretok.pre_tokenize(&mut pretokenized).unwrap(); assert_eq!( pretokenized .get_splits(OffsetReferential::Normalized, OffsetType::Byte) .into_iter() .map(|(s, o, _)| (s, o)) .collect::<Vec<_>>(), vec![ ("▁Hey▁", (0, 9)), ("<s>", (9, 12)), ("how▁", (12, 18)), ("<s>", (18, 21)), ("are▁", (21, 27)), ("<s>", (27, 30)), ("▁you", (30, 36)) ] ); } #[test] fn decode() { let decoder = Metaspace::new('▁', PrependScheme::Always, true); let res = decoder .decode_chain(vec!["▁Hey".into(), "▁friend!".into()]) .unwrap(); assert_eq!(res, vec!["Hey", " friend!"]); let decoder = Metaspace::new('▁', PrependScheme::Never, true); let res = decoder .decode_chain(vec!["▁Hey".into(), "▁friend!".into()]) .unwrap(); assert_eq!(res, vec![" Hey", " friend!"]); } }
tokenizers/tokenizers/src/pre_tokenizers/metaspace.rs/0
{ "file_path": "tokenizers/tokenizers/src/pre_tokenizers/metaspace.rs", "repo_id": "tokenizers", "token_count": 6687 }
//! Represents a tokenization pipeline. //! //! A [`Tokenizer`](struct.Tokenizer.html) is composed of some of the following parts. //! - [`Normalizer`](trait.Normalizer.html): Takes care of the text normalization (like unicode normalization). //! - [`PreTokenizer`](trait.PreTokenizer.html): Takes care of the pre tokenization (ie. How to split tokens and pre-process //! them. //! - [`Model`](trait.Model.html): A model encapsulates the tokenization algorithm (like BPE, Word base, character //! based, ...). //! - [`PostProcessor`](trait.PostProcessor.html): Takes care of the processing after tokenization (like truncating, padding, //! ...). use std::{ collections::HashMap, fs::{read_to_string, File}, io::{prelude::*, BufReader}, ops::{Deref, DerefMut}, path::{Path, PathBuf}, }; use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; use crate::utils::iter::ResultShunt; use crate::utils::parallelism::*; use crate::utils::progress::{ProgressBar, ProgressStyle}; mod added_vocabulary; mod encoding; pub mod normalizer; pub mod pattern; pub mod pre_tokenizer; mod serialization; // Re-export wrappers pub use crate::decoders::DecoderWrapper; pub use crate::models::ModelWrapper; pub use crate::normalizers::NormalizerWrapper; pub use crate::pre_tokenizers::PreTokenizerWrapper; pub use crate::processors::PostProcessorWrapper; // And some other types pub use crate::utils::iter::LinesWithEnding; pub use crate::utils::padding::{pad_encodings, PaddingDirection, PaddingParams, PaddingStrategy}; pub use crate::utils::truncation::{ truncate_encodings, TruncationDirection, TruncationParams, TruncationStrategy, }; pub use added_vocabulary::*; pub use encoding::*; pub use normalizer::{NormalizedString, OffsetReferential, SplitDelimiterBehavior}; pub use pre_tokenizer::*; pub type Error = Box<dyn std::error::Error + Send + Sync>; pub type Result<T> = std::result::Result<T, Error>; pub type Offsets = (usize, usize); /// Takes care of pre-processing strings. pub trait Normalizer { fn normalize(&self, normalized: &mut NormalizedString) -> Result<()>; } /// The `PreTokenizer` is in charge of doing the pre-segmentation step. It splits the given string /// in multiple substrings, keeping track of the offsets of said substrings from the /// `NormalizedString`. In some occasions, the `PreTokenizer` might need to modify the given /// `NormalizedString` to ensure we can entirely keep track of the offsets and the mapping with /// the original string. pub trait PreTokenizer { fn pre_tokenize(&self, pretokenized: &mut PreTokenizedString) -> Result<()>; } /// Represents a model used during Tokenization (like BPE or Word or Unigram). pub trait Model { type Trainer: Trainer + Sync; /// Tokenize the given sequence into multiple underlying `Token`. The `offsets` on the `Token` /// are expected to be relative to the given sequence. fn tokenize(&self, sequence: &str) -> Result<Vec<Token>>; /// Find the ID associated to a string token fn token_to_id(&self, token: &str) -> Option<u32>; /// Find the string token associated to an ID fn id_to_token(&self, id: u32) -> Option<String>; /// Retrieve the entire vocabulary mapping (token -> ID) fn get_vocab(&self) -> HashMap<String, u32>; /// Retrieve the size of the vocabulary fn get_vocab_size(&self) -> usize; /// Save the current `Model` in the given folder, using the given `prefix` for the various /// files that need to be saved. fn save(&self, folder: &Path, prefix: Option<&str>) -> Result<Vec<PathBuf>>; /// Get an instance of a Trainer capable of training this Model fn get_trainer(&self) -> <Self as Model>::Trainer; } /// A `PostProcessor` has the responsibility to post process an encoded output of the `Tokenizer`. /// It adds any special tokens that a language model would require. pub trait PostProcessor { /// Returns the number of tokens that will be added during the processing step fn added_tokens(&self, is_pair: bool) -> usize; /// Process both encodings and returns a new merged one fn process( &self, encoding: Encoding, pair_encoding: Option<Encoding>, add_special_tokens: bool, ) -> Result<Encoding> { let mut encodings = if let Some(pair_encoding) = pair_encoding { vec![encoding, pair_encoding] } else { vec![encoding] }; encodings.iter_mut().enumerate().for_each(|(i, encoding)| { encoding.set_sequence_id(i); encoding .get_overflowing_mut() .iter_mut() .for_each(|encoding| encoding.set_sequence_id(i)); encoding.set_type_ids(vec![i as u32; encoding.len()]); }); let encodings = self.process_encodings(encodings, add_special_tokens)?; Ok(Encoding::merge(encodings, false)) } /// Process any amount of encodings and returns a series of encoding (might merge them) fn process_encodings( &self, encodings: Vec<Encoding>, add_special_tokens: bool, ) -> Result<Vec<Encoding>>; } impl dyn PostProcessor { pub fn default_process( encodings: Vec<Encoding>, _add_special_tokens: bool, ) -> Result<Vec<Encoding>> { match encodings.len() { 1 => Ok(encodings), _ => { let mut final_encoding = Encoding::default(); for (i, mut encoding) in encodings.into_iter().enumerate() { encoding.set_sequence_id(i); final_encoding.merge_with(encoding, false); } Ok(vec![final_encoding]) } } } } #[derive(thiserror::Error, Debug)] pub enum ProcessorError { #[error("encodings vector length must be either 1 or 2")] InvalidEncodingsVecLength, } /// A `Decoder` changes the raw tokens into its more readable form. pub trait Decoder { fn decode(&self, tokens: Vec<String>) -> Result<String> { let results = self.decode_chain(tokens)?; Ok(results.join("")) } fn decode_chain(&self, tokens: Vec<String>) -> Result<Vec<String>>; } /// A `Trainer` has the responsibility to train a model. We feed it with lines/sentences /// and then it can train the given `Model`. pub trait Trainer { type Model: Model + Sized; /// Whether we should show progress during the training. fn should_show_progress(&self) -> bool; /// The actual training method. This will return a new trained Model as well as a list /// of `special_tokens` to be added directly to the tokenizer along with the model. fn train(&self, model: &mut Self::Model) -> Result<Vec<AddedToken>>; /// Process an iterator of sequences, calling `process` for each of them in order to /// pre-process the said sequence as relevant. fn feed<I, S, F>(&mut self, iterator: I, process: F) -> Result<()> where I: Iterator<Item = S> + Send, S: AsRef<str> + Send, F: Fn(&str) -> Result<Vec<String>> + Sync; } #[derive(Debug, Clone, PartialEq, Eq)] pub struct Token { pub id: u32, pub value: String, pub offsets: (usize, usize), } impl Token { pub fn new(id: u32, value: String, offsets: (usize, usize)) -> Self { Self { id, value, offsets } } } use std::borrow::Cow; #[derive(Debug, Clone)] pub enum InputSequence<'s> { Raw(Cow<'s, str>), PreTokenized(Cow<'s, [&'s str]>), PreTokenizedOwned(Cow<'s, [String]>), PreTokenizedCow(Cow<'s, [Cow<'s, str>]>), } impl<'s> From<Cow<'s, str>> for InputSequence<'s> { fn from(input: Cow<'s, str>) -> Self { Self::Raw(input) } } impl<'s> From<&'s str> for InputSequence<'s> { fn from(input: &'s str) -> Self { Self::Raw(Cow::Borrowed(input)) } } impl From<String> for InputSequence<'_> { fn from(input: String) -> Self { Self::Raw(Cow::Owned(input)) } } impl<'s> From<&'s [&'s str]> for InputSequence<'s> { fn from(input: &'s [&'s str]) -> Self { Self::PreTokenized(Cow::Borrowed(input)) } } impl<'s> From<Vec<&'s str>> for InputSequence<'s> { fn from(input: Vec<&'s str>) -> Self { Self::PreTokenized(Cow::Owned(input)) } } impl<'s> From<&'s [String]> for InputSequence<'s> { fn from(input: &'s [String]) -> Self { Self::PreTokenizedOwned(Cow::Borrowed(input)) } } impl From<Vec<String>> for InputSequence<'_> { fn from(input: Vec<String>) -> Self { Self::PreTokenizedOwned(Cow::Owned(input)) } } impl<'s> From<Vec<Cow<'s, str>>> for InputSequence<'s> { fn from(input: Vec<Cow<'s, str>>) -> Self { Self::PreTokenizedCow(Cow::Owned(input)) } } impl<'s> From<&'s [Cow<'s, str>]> for InputSequence<'s> { fn from(input: &'s [Cow<'s, str>]) -> Self { Self::PreTokenizedCow(Cow::Borrowed(input)) } } #[derive(Debug, Clone)] pub enum EncodeInput<'s> { Single(InputSequence<'s>), Dual(InputSequence<'s>, InputSequence<'s>), } impl<'s, I: Into<InputSequence<'s>>> From<I> for EncodeInput<'s> { fn from(input: I) -> Self { Self::Single(input.into()) } } impl<'s, I1, I2> From<(I1, I2)> for EncodeInput<'s> where I1: Into<InputSequence<'s>>, I2: Into<InputSequence<'s>>, { fn from(input: (I1, I2)) -> Self { Self::Dual(input.0.into(), input.1.into()) } } #[derive(thiserror::Error, Debug)] #[error("{0}")] pub struct BuilderError(String); /// Builder for Tokenizer structs. /// /// `build()` fails if the `model` is missing. pub struct TokenizerBuilder<M, N, PT, PP, D> { model: Option<M>, normalizer: Option<N>, pre_tokenizer: Option<PT>, post_processor: Option<PP>, decoder: Option<D>, added_vocabulary: AddedVocabulary, truncation: Option<TruncationParams>, padding: Option<PaddingParams>, } impl<M, N, PT, PP, D> Default for TokenizerBuilder<M, N, PT, PP, D> where M: Model, N: Normalizer, PT: PreTokenizer, PP: PostProcessor, D: Decoder, { fn default() -> Self { Self::new() } } impl<M, N, PT, PP, D> TokenizerBuilder<M, N, PT, PP, D> where M: Model, N: Normalizer, PT: PreTokenizer, PP: PostProcessor, D: Decoder, { /// Get an empty TokenizerBuilder. pub fn new() -> Self { Self { model: None, normalizer: None, pre_tokenizer: None, post_processor: None, decoder: None, added_vocabulary: AddedVocabulary::new(), truncation: None, padding: None, } } /// Convert the TokenizerBuilder to a Tokenizer. /// /// Conversion fails if the `model` is missing. pub fn build(self) -> Result<TokenizerImpl<M, N, PT, PP, D>> { let model = self .model .ok_or_else(|| Box::new(BuilderError("Model missing.".into())))?; Ok(TokenizerImpl { normalizer: self.normalizer, pre_tokenizer: self.pre_tokenizer, model, post_processor: self.post_processor, decoder: self.decoder, added_vocabulary: self.added_vocabulary, truncation: self.truncation, padding: self.padding, }) } /// Set the model. #[must_use] pub fn with_model(mut self, model: M) -> Self { self.model = Some(model); self } /// Set the normalizer. #[must_use] pub fn with_normalizer(mut self, normalizer: Option<N>) -> Self { self.normalizer = normalizer; self } /// Set the pre-tokenizer. #[must_use] pub fn with_pre_tokenizer(mut self, pretokenizer: Option<PT>) -> Self { self.pre_tokenizer = pretokenizer; self } /// Set the post-processor. #[must_use] pub fn with_post_processor(mut self, post_processor: Option<PP>) -> Self { self.post_processor = post_processor; self } /// Set the decoder. #[must_use] pub fn with_decoder(mut self, decoder: Option<D>) -> Self { self.decoder = decoder; self } /// Set the added vocabulary. pub fn with_added_vocabulary(mut self, added_vocabulary: AddedVocabulary) -> Self { self.added_vocabulary = added_vocabulary; self } /// Set the trunaction parameters. #[must_use] pub fn with_truncation(mut self, trunc: Option<TruncationParams>) -> Self { self.truncation = trunc; self } /// Set the padding parameters. #[must_use] pub fn with_padding(mut self, padding: Option<PaddingParams>) -> Self { self.padding = padding; self } } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Tokenizer( TokenizerImpl< ModelWrapper, NormalizerWrapper, PreTokenizerWrapper, PostProcessorWrapper, DecoderWrapper, >, ); impl Tokenizer { /// Construct a new Tokenizer based on the model. pub fn new(model: impl Into<ModelWrapper>) -> Self { Self(TokenizerImpl::new(model.into())) } /// Unwrap the TokenizerImpl. pub fn into_inner( self, ) -> TokenizerImpl< ModelWrapper, NormalizerWrapper, PreTokenizerWrapper, PostProcessorWrapper, DecoderWrapper, > { self.0 } pub fn from_file<P: AsRef<Path>>(file: P) -> Result<Self> { let content = read_to_string(file)?; let tokenizer = serde_json::from_str(&content)?; Ok(tokenizer) } pub fn from_bytes<P: AsRef<[u8]>>(bytes: P) -> Result<Self> { let tokenizer = serde_json::from_slice(bytes.as_ref())?; Ok(tokenizer) } #[cfg(feature = "http")] pub fn from_pretrained<S: AsRef<str>>( identifier: S, params: Option<crate::utils::from_pretrained::FromPretrainedParameters>, ) -> Result<Self> { let tokenizer_file = crate::utils::from_pretrained::from_pretrained(identifier, params)?; Tokenizer::from_file(tokenizer_file) } } impl std::str::FromStr for Tokenizer { type Err = Box<dyn std::error::Error + Send + Sync>; fn from_str(s: &str) -> Result<Self> { Ok(serde_json::from_str(s)?) } } impl<M, N, PT, PP, D> From<TokenizerImpl<M, N, PT, PP, D>> for Tokenizer where M: Into<ModelWrapper>, N: Into<NormalizerWrapper>, PT: Into<PreTokenizerWrapper>, PP: Into<PostProcessorWrapper>, D: Into<DecoderWrapper>, { fn from(t: TokenizerImpl<M, N, PT, PP, D>) -> Self { Self(TokenizerImpl { model: t.model.into(), normalizer: t.normalizer.map(Into::into), pre_tokenizer: t.pre_tokenizer.map(Into::into), post_processor: t.post_processor.map(Into::into), decoder: t.decoder.map(Into::into), added_vocabulary: t.added_vocabulary, padding: t.padding, truncation: t.truncation, }) } } impl Deref for Tokenizer { type Target = TokenizerImpl< ModelWrapper, NormalizerWrapper, PreTokenizerWrapper, PostProcessorWrapper, DecoderWrapper, >; fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for Tokenizer { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } #[derive(thiserror::Error, Debug)] #[error("{0}")] pub struct TruncationParamError(String); /// A `Tokenizer` is capable of encoding/decoding any text. #[derive(Clone, Debug)] pub struct TokenizerImpl<M, N, PT, PP, D> { // Tokenizer parts normalizer: Option<N>, pre_tokenizer: Option<PT>, model: M, post_processor: Option<PP>, decoder: Option<D>, // Added Vocabulary capabilities added_vocabulary: AddedVocabulary, // General processing parameters truncation: Option<TruncationParams>, padding: Option<PaddingParams>, } impl<M, N, PT, PP, D> TokenizerImpl<M, N, PT, PP, D> where M: Model, N: Normalizer, PT: PreTokenizer, PP: PostProcessor, D: Decoder, { /// Instantiate a new Tokenizer, with the given Model pub fn new(model: M) -> Self { Self { normalizer: None, pre_tokenizer: None, model, post_processor: None, decoder: None, added_vocabulary: AddedVocabulary::new(), truncation: None, padding: None, } } /// Set the normalizer pub fn with_normalizer(&mut self, normalizer: Option<impl Into<N>>) -> &mut Self { self.normalizer = normalizer.map(|norm| norm.into()); self } /// Get the normalizer pub fn get_normalizer(&self) -> Option<&N> { self.normalizer.as_ref() } /// Set the pre tokenizer pub fn with_pre_tokenizer(&mut self, pre_tokenizer: Option<impl Into<PT>>) -> &mut Self { self.pre_tokenizer = pre_tokenizer.map(|tok| tok.into()); self } /// Get the pre tokenizer pub fn get_pre_tokenizer(&self) -> Option<&PT> { self.pre_tokenizer.as_ref() } /// Set the post processor pub fn with_post_processor(&mut self, post_processor: Option<impl Into<PP>>) -> &mut Self { self.post_processor = post_processor.map(|post_proc| post_proc.into()); self } /// Get the post processor pub fn get_post_processor(&self) -> Option<&PP> { self.post_processor.as_ref() } /// Set the decoder pub fn with_decoder(&mut self, decoder: Option<impl Into<D>>) -> &mut Self { self.decoder = decoder.map(|dec| dec.into()); self } /// Get the decoder pub fn get_decoder(&self) -> Option<&D> { self.decoder.as_ref() } /// Set the model pub fn with_model(&mut self, model: impl Into<M>) -> &mut Self { self.model = model.into(); self } /// Get the model pub fn get_model(&self) -> &M { &self.model } /// Set the added vocabulary. pub fn with_added_vocabulary(&mut self, added_vocabulary: AddedVocabulary) -> &mut Self { self.added_vocabulary = added_vocabulary; self } /// Get the added vocabulary pub fn get_added_vocabulary(&self) -> &AddedVocabulary { &self.added_vocabulary } /// Set the truncation parameters /// /// Fails if `stride` is too high relative to `max_length` and `post_processor.added_tokens()` pub fn with_truncation(&mut self, trunc: Option<TruncationParams>) -> Result<&mut Self> { if let Some(trunc_params) = &trunc { let n_added_tokens = self.get_n_added_tokens(false); let effective_max_length = trunc_params.max_length - n_added_tokens; if effective_max_length < trunc_params.stride { return Err(Box::new(TruncationParamError(format!( "tokenizer stride set to {}, which is greater than or equal to its effective max length of {} (= {} original max length - {} added special tokens), ", trunc_params.stride, effective_max_length, trunc_params.max_length, n_added_tokens )))); } } self.truncation = trunc; Ok(self) } /// Get the currently set truncation parameters pub fn get_truncation(&self) -> Option<&TruncationParams> { self.truncation.as_ref() } /// Get a mutable reference to the currently set truncation parameters pub fn get_truncation_mut(&mut self) -> Option<&mut TruncationParams> { self.truncation.as_mut() } /// Set the padding parameters pub fn with_padding(&mut self, padding: Option<PaddingParams>) -> &mut Self { self.padding = padding; self } /// Get the currently set padding parameters pub fn get_padding(&self) -> Option<&PaddingParams> { self.padding.as_ref() } /// Get a mutable reference to the currently set padding parameters pub fn get_padding_mut(&mut self) -> Option<&mut PaddingParams> { self.padding.as_mut() } /// Get the vocabulary pub fn get_vocab(&self, with_added_tokens: bool) -> HashMap<String, u32> { let mut final_vocab = self.model.get_vocab(); if with_added_tokens { let added_vocab = self.added_vocabulary.get_vocab(); if !added_vocab.is_empty() { final_vocab.reserve(added_vocab.len()); for (token, id) in added_vocab { final_vocab.insert(token.clone(), *id); } } } final_vocab } /// Get the added tokens decoder pub fn get_added_tokens_decoder(&self) -> HashMap<u32, AddedToken> { self.added_vocabulary.get_added_tokens_decoder().clone() } /// Get the size of the vocabulary pub fn get_vocab_size(&self, with_added_tokens: bool) -> usize { // TODO ArthurZ THIS IS WRONG! We need to measure the length of the `set` because // now some tokens can be both in the added_tokens_encoder and in the vocab if with_added_tokens { self.get_vocab(true).len() } else { self.model.get_vocab_size() } } /// Converts a token in the corresponding id. pub fn token_to_id(&self, token: &str) -> Option<u32> { self.added_vocabulary.token_to_id(token, &self.model) } /// Converts an id to the corresponding token. pub fn id_to_token(&self, id: u32) -> Option<String> { self.added_vocabulary .simple_id_to_token(id) .or_else(|| self.model.id_to_token(id)) } /// set the added bocab's splitting scheme pub fn set_encode_special_tokens(&mut self, value: bool) { self.added_vocabulary.set_encode_special_tokens(value); } /// Get added token value pub fn get_encode_special_tokens(&self) -> bool { self.added_vocabulary.get_encode_special_tokens() } /// Encode a single sequence fn encode_single_sequence( &self, sequence: InputSequence, type_id: u32, offsets_type: OffsetType, ) -> Result<Encoding> { let encode = |is_pre_tokenized, subseq_idx, subseq| -> Result<Encoding> { let normalized = self .added_vocabulary .extract_and_normalize(self.normalizer.as_ref(), subseq); let pre_tokenized = self.do_pre_tokenize(normalized)?; let subseq_encoding = self.do_tokenize( pre_tokenized, type_id, if is_pre_tokenized { Some(subseq_idx as u32) } else { None }, offsets_type, )?; Ok(subseq_encoding) }; match sequence { InputSequence::PreTokenized(seq) => seq .iter() .enumerate() .map(|(i, sequence)| encode(true, i, sequence)) .collect(), InputSequence::PreTokenizedOwned(seq) => seq .iter() .enumerate() .map(|(i, sequence)| encode(true, i, sequence)) .collect(), InputSequence::PreTokenizedCow(seq) => seq .iter() .enumerate() .map(|(i, sequence)| encode(true, i, sequence)) .collect(), InputSequence::Raw(seq) => encode(false, 0, seq.as_ref()), } } /// Encode the given input. This method accepts both single sequences, as well as pair /// sequences. Also, a sequence can be a string, or already pre-tokenized input directly: /// Contrarily to `encode`, it does not compute offsets /// ``` /// # use tokenizers::Tokenizer; /// # use tokenizers::models::bpe::BPE; /// # let mut tokenizer = Tokenizer::new(BPE::default()); /// # /// // Sequences: /// tokenizer.encode_fast("Single sequence", false); /// tokenizer.encode_fast(("Sequence A", "Sequence B"), false); /// /// // Pre-tokenized sequences: /// tokenizer.encode_fast(&["Single", "sequence"][..], false); /// tokenizer.encode_fast(( /// &["Sequence", "A"][..], /// &["Sequence", "B"][..] /// ), false); /// /// // or even both types together: /// tokenizer.encode_fast(("A complete sequence", &["And", "a", "tokenized"][..]), false); /// ``` pub fn encode_fast<'s, E>(&self, input: E, add_special_tokens: bool) -> Result<Encoding> where E: Into<EncodeInput<'s>>, { // Extract sequences from the EncodeInput let (sequence, pair) = match input.into() { EncodeInput::Single(s1) => (s1, None), EncodeInput::Dual(s1, s2) => (s1, Some(s2)), }; // Encode each sequence let encoding = self.encode_single_sequence(sequence, 0, OffsetType::None)?; let pair_encoding = pair .map(|sequence| self.encode_single_sequence(sequence, 1, OffsetType::None)) .transpose()?; // And finally post process self.post_process(encoding, pair_encoding, add_special_tokens) } /// Encode the given input. This method accepts both single sequences, as well as pair /// sequences. Also, a sequence can be a string, or already pre-tokenized input directly: /// /// ``` /// # use tokenizers::Tokenizer; /// # use tokenizers::models::bpe::BPE; /// # let mut tokenizer = Tokenizer::new(BPE::default()); /// # /// // Sequences: /// tokenizer.encode("Single sequence", false); /// tokenizer.encode(("Sequence A", "Sequence B"), false); /// /// // Pre-tokenized sequences: /// tokenizer.encode(&["Single", "sequence"][..], false); /// tokenizer.encode(( /// &["Sequence", "A"][..], /// &["Sequence", "B"][..] /// ), false); /// /// // or even both types together: /// tokenizer.encode(("A complete sequence", &["And", "a", "tokenized"][..]), false); /// ``` pub fn encode<'s, E>(&self, input: E, add_special_tokens: bool) -> Result<Encoding> where E: Into<EncodeInput<'s>>, { // Extract sequences from the EncodeInput let (sequence, pair) = match input.into() { EncodeInput::Single(s1) => (s1, None), EncodeInput::Dual(s1, s2) => (s1, Some(s2)), }; // Encode each sequence let encoding = self.encode_single_sequence(sequence, 0, OffsetType::Byte)?; let pair_encoding = pair .map(|sequence| self.encode_single_sequence(sequence, 1, OffsetType::Byte)) .transpose()?; // And finally post process self.post_process(encoding, pair_encoding, add_special_tokens) } /// Encode the given input, using offsets relative to chars instead of bytes. /// This method accepts both single sequences, as well as pair sequences. Also, /// a sequence can be a string, or already pre-tokenized input directly: /// /// ``` /// # use tokenizers::Tokenizer; /// # use tokenizers::models::bpe::BPE; /// # let mut tokenizer = Tokenizer::new(BPE::default()); /// # /// // Sequences: /// tokenizer.encode("Single sequence", false); /// tokenizer.encode(("Sequence A", "Sequence B"), false); /// /// // Pre-tokenized sequences: /// tokenizer.encode(&["Single", "sequence"][..], false); /// tokenizer.encode(( /// &["Sequence", "A"][..], /// &["Sequence", "B"][..] /// ), false); /// /// // or even both types together: /// tokenizer.encode(("A complete sequence", &["And", "a", "tokenized"][..]), false); /// ``` pub fn encode_char_offsets<'s, E>(&self, input: E, add_special_tokens: bool) -> Result<Encoding> where E: Into<EncodeInput<'s>>, { // Extract sequences from the EncodeInput let (sequence, pair) = match input.into() { EncodeInput::Single(s1) => (s1, None), EncodeInput::Dual(s1, s2) => (s1, Some(s2)), }; // Encode each sequence let encoding = self.encode_single_sequence(sequence, 0, OffsetType::Char)?; let pair_encoding = pair .map(|sequence| self.encode_single_sequence(sequence, 1, OffsetType::Char)) .transpose()?; // And finally post process self.post_process(encoding, pair_encoding, add_special_tokens) } /// Decode the given ids, back to a String pub fn decode(&self, ids: &[u32], skip_special_tokens: bool) -> Result<String> { let tokens = ids .iter() .filter_map(|id| { self.added_vocabulary .simple_id_to_token(*id) .or_else(|| self.model.id_to_token(*id)) .filter(|token| { !skip_special_tokens || !self.added_vocabulary.is_special_token(token) }) }) .collect::<Vec<_>>(); if let Some(decoder) = &self.decoder { decoder.decode(tokens) } else { Ok(tokens.join(" ")) } } /// Decode the given ids, back to a String /// See [`DecodeStream`] pub fn decode_stream(&self, skip_special_tokens: bool) -> DecodeStream<'_, M, N, PT, PP, D> { DecodeStream::new(self, skip_special_tokens) } } /// DecodeStream will keep the state necessary to produce individual chunks of /// strings given an input stream of token_ids. /// /// This is necessary because decoding in general cannot achieve that since strings /// depend on surrounding ids to provide a valid string. Typically stripping extra spaces /// /// Example: /// /// ``` /// # #[cfg(not(target_os = "windows"))] /// # { /// use tokenizers::Tokenizer; /// let tokenizer = Tokenizer::from_file("data/roberta.json").unwrap(); /// /// let mut decode_stream = tokenizer.decode_stream(false); /// assert_eq!(decode_stream.step(713).unwrap(), Some("This".to_string())); /// assert_eq!(decode_stream.step(16).unwrap(), Some(" is".to_string())); /// assert_eq!(decode_stream.step(41).unwrap(), Some(" an".to_string())); /// assert_eq!( /// decode_stream.step(1246).unwrap(), /// Some(" example".to_string()) /// ); /// # } /// ``` /// /// Returning `None` means the given id is not enough to produce a chunk. /// This typically happens with `byte_fallback` options where some tokens do /// not represent valid utf-8, and only follow-up token_ids will help produce /// a valid chunk. /// ``` /// use tokenizers::{Tokenizer, TokenizerBuilder, models::bpe::BPE, decoders::byte_fallback::ByteFallback, pre_tokenizers::byte_level::ByteLevel, normalizers::unicode::NFC}; /// use std::collections::HashMap; /// use std::iter::FromIterator; /// /// let vocab = HashMap::from_iter([ /// ("<0x20>".to_string(), 0), /// ("<0xC3>".to_string(), 1), /// ("<0xA9>".to_string(), 2), /// (" This".to_string(), 3), /// ]); /// let merges = vec![]; /// let bpe = BPE::builder() /// .vocab_and_merges(vocab, merges) /// .byte_fallback(true) /// .build() /// .unwrap(); /// let tokenizer = TokenizerBuilder::default() /// .with_model(bpe) /// .with_decoder(Some(ByteFallback::default())) /// .with_normalizer(Some(NFC)) /// .with_pre_tokenizer(Some(ByteLevel::default())) /// .with_post_processor(Some(ByteLevel::default())) /// .build().unwrap(); /// /// let mut decode_stream = tokenizer.decode_stream(false); /// // Single byte_fallback is valid utf-8 /// assert_eq!(decode_stream.step(0).unwrap(), Some(" ".to_string())); /// // Invalid utf-8 /// assert_eq!(decode_stream.step(1).unwrap(), None); /// // Valid utf-8 again, this corresponds to both tokens: [1, 2] /// assert_eq!(decode_stream.step(2).unwrap(), Some("é".to_string())); /// ``` /// /// To see how [`DecodeStream`] is necessary, let's show how using raw [`TokenizerImpl::decode`] would /// fail. /// /// ``` /// use tokenizers::{Tokenizer, TokenizerBuilder, models::bpe::BPE, pre_tokenizers::{byte_level::ByteLevel, metaspace::Metaspace}, normalizers::unicode::NFC}; /// use std::collections::HashMap; /// use std::iter::FromIterator; /// /// let vocab = HashMap::from_iter([ /// ("▁This".to_string(), 0), /// ]); /// let merges = vec![]; /// let bpe = BPE::builder() /// .vocab_and_merges(vocab, merges) /// .byte_fallback(true) /// .build() /// .unwrap(); /// let tokenizer = TokenizerBuilder::new() /// .with_model(bpe) /// .with_decoder(Some(Metaspace::default())) /// .with_normalizer(Some(NFC)) /// .with_pre_tokenizer(Some(ByteLevel::default())) /// .with_post_processor(Some(ByteLevel::default())) /// .build() /// .unwrap(); /// /// // Strip decoder removes the extra initial space /// assert_eq!(tokenizer.decode(&[0, 0], false).unwrap(), "This This"); /// // Decoding one token at a time would produce "ThisThis" /// assert_eq!(tokenizer.decode(&[0], false).unwrap(), "This"); /// /// // Using a stream fixes it by keeping the necessary state. /// let mut decode_stream = tokenizer.decode_stream(false); /// assert_eq!(decode_stream.step(0).unwrap(), Some("This".to_string())); /// assert_eq!(decode_stream.step(0).unwrap(), Some(" This".to_string())); /// ``` pub struct DecodeStream<'tok, M, N, PT, PP, D> { /// A reference to the tokenizer tokenizer: &'tok TokenizerImpl<M, N, PT, PP, D>, /// Regular decode option that is kept throughout. skip_special_tokens: bool, /// A temporary buffer of the necessary token_ids needed /// to produce valid string chunks. /// This typically contains 3 parts: /// - read /// - prefix /// - rest /// /// Read is the bit necessary to surround the prefix /// so decoding the whole ids produces a valid prefix. /// Prefix is the previously produced string, kept around to trim off of /// the next valid chunk ids: Vec<u32>, /// The previously returned chunk that needs to be discarded from the /// decoding of the current ids to produce the next chunk prefix: String, /// The index within the ids corresponding to the prefix so we can drain /// correctly prefix_index: usize, } #[derive(thiserror::Error, Debug)] pub enum DecodeStreamError { #[error("Invalid prefix encountered")] InvalidPrefix, } impl<'tok, M, N, PT, PP, D> DecodeStream<'tok, M, N, PT, PP, D> where M: Model, N: Normalizer, PT: PreTokenizer, PP: PostProcessor, D: Decoder, { fn new(tokenizer: &'tok TokenizerImpl<M, N, PT, PP, D>, skip_special_tokens: bool) -> Self { Self { tokenizer, ids: vec![], skip_special_tokens, prefix: "".to_string(), prefix_index: 0, } } /// See [`DecodeStream`] pub fn step(&mut self, id: u32) -> Result<Option<String>> { step_decode_stream( self.tokenizer, id, self.skip_special_tokens, &mut self.ids, &mut self.prefix, &mut self.prefix_index, ) } } /// Internal function exposed only to bypass python limitations pub fn step_decode_stream<M, N, PT, PP, D>( tokenizer: &TokenizerImpl<M, N, PT, PP, D>, id: u32, skip_special_tokens: bool, ids: &mut Vec<u32>, prefix: &mut String, prefix_index: &mut usize, ) -> Result<Option<String>> where M: Model, N: Normalizer, PT: PreTokenizer, PP: PostProcessor, D: Decoder, { ids.push(id); let string = tokenizer.decode(ids.as_slice(), skip_special_tokens)?; if string.len() > prefix.len() && !string.ends_with('�') { if !(string.starts_with(&*prefix)) { return Err(Box::new(DecodeStreamError::InvalidPrefix)); } let new_text = &string[prefix.len()..].to_string(); let new_prefix_index = ids.len() - *prefix_index; *ids = ids.drain(*prefix_index..).collect(); *prefix = tokenizer.decode(ids, skip_special_tokens)?; *prefix_index = new_prefix_index; Ok(Some(new_text.to_string())) } else { Ok(None) } } impl<M, N, PT, PP, D> TokenizerImpl<M, N, PT, PP, D> where M: Model, { /// Tokenization logic, makes the bridge between the pre-tokenization phase and the real /// tokenization phase, and converting offsets back to the original referential. fn do_tokenize<P: Into<PreTokenizedString>>( &self, pretokenized: P, type_id: u32, word_idx: Option<u32>, offsets_type: OffsetType, ) -> Result<Encoding> { let mut pretokenized: PreTokenizedString = pretokenized.into(); pretokenized.tokenize(|normalized| self.model.tokenize(normalized.get()))?; pretokenized.into_encoding(word_idx, type_id, offsets_type) } } impl<M, N, PT, PP, D> TokenizerImpl<M, N, PT, PP, D> where N: Normalizer, { /// Normalization logic, go through all normalizers fn do_normalize<V: Into<NormalizedString>>(&self, normalized: V) -> Result<NormalizedString> { let mut normalized: NormalizedString = normalized.into(); if let Some(ref normalizer) = self.normalizer { normalizer.normalize(&mut normalized)?; } Ok(normalized) } } impl<M, N, PT, PP, D> TokenizerImpl<M, N, PT, PP, D> where N: Normalizer, M: Model, { /// Register the given tokens as special tokens. This is especially useful for removing /// these special tokens while decoding pub fn add_special_tokens(&mut self, tokens: &[AddedToken]) -> usize { self.added_vocabulary .add_special_tokens(tokens, &self.model, self.normalizer.as_ref()) } /// Add the given tokens to the added vocabulary pub fn add_tokens(&mut self, tokens: &[AddedToken]) -> usize { self.added_vocabulary .add_tokens(tokens, &self.model, self.normalizer.as_ref()) } } impl<M, N, PT, PP, D> TokenizerImpl<M, N, PT, PP, D> where PT: PreTokenizer, { /// PreTokenization logic, handling the case where there is no PreTokenizer set fn do_pre_tokenize<P: Into<PreTokenizedString>>( &self, pretokenized: P, ) -> Result<PreTokenizedString> { let mut pretokenized: PreTokenizedString = pretokenized.into(); if let Some(ref pretok) = self.pre_tokenizer { pretok.pre_tokenize(&mut pretokenized)?; } Ok(pretokenized) } } impl<M, N, PT, PP, D> TokenizerImpl<M, N, PT, PP, D> where PP: PostProcessor, { /// Post processing logic, handling the case where there is no PostProcessor set pub fn post_process( &self, encoding: Encoding, pair_encoding: Option<Encoding>, add_special_tokens: bool, ) -> Result<Encoding> { // 1. First we truncate if needed let (encoding, pair_encoding) = { if let Some(trunc) = &self.truncation { let n_added_tokens = self.get_n_added_tokens(pair_encoding.is_some()); if add_special_tokens && n_added_tokens > 0 { let params = TruncationParams { max_length: trunc.max_length - n_added_tokens, ..*trunc }; truncate_encodings(encoding, pair_encoding, &params)? } else { truncate_encodings(encoding, pair_encoding, trunc)? } } else { (encoding, pair_encoding) } }; // 2. Then We post process let final_encoding = if let Some(processor) = &self.post_processor { processor.process(encoding, pair_encoding, add_special_tokens)? } else { let encodings = if let Some(pair_encoding) = pair_encoding { vec![encoding, pair_encoding] } else { vec![encoding] }; let mut encodings = <dyn PostProcessor>::default_process(encodings, add_special_tokens)?; if encodings.len() != 1 { panic!("We haven't reduced the encodings like we should have"); } encodings.pop().unwrap() }; // 3. Then we pad if needed let [final_encoding] = if let Some(params) = &self.padding { let mut arr = [final_encoding]; pad_encodings(&mut arr, params)?; arr } else { [final_encoding] }; Ok(final_encoding) } fn get_n_added_tokens(&self, is_pair: bool) -> usize { if let Some(processor) = &self.post_processor { processor.added_tokens(is_pair) } else { 0 } } } impl<M, N, PT, PP, D> TokenizerImpl<M, N, PT, PP, D> where M: Model + Send + Sync, N: Normalizer + Send + Sync, PT: PreTokenizer + Send + Sync, PP: PostProcessor + Send + Sync, D: Decoder + Send + Sync, { /// Encode all the sentences in parallel, using multiple threads pub fn encode_batch<'s, E>( &self, inputs: Vec<E>, add_special_tokens: bool, ) -> Result<Vec<Encoding>> where E: Into<EncodeInput<'s>> + Send, { let mut encodings = inputs .into_maybe_par_iter() .map(|input| self.encode(input, add_special_tokens)) .collect::<Result<Vec<Encoding>>>()?; if let Some(params) = &self.padding { // We do the padding here to make sure we handle the batch padding pad_encodings(&mut encodings, params)?; } Ok(encodings) } /// Encode all the sentences in parallel, using multiple threads. /// The offsets on each `Encoding` will be relative to chars instead of bytes. pub fn encode_batch_char_offsets<'s, E>( &self, inputs: Vec<E>, add_special_tokens: bool, ) -> Result<Vec<Encoding>> where E: Into<EncodeInput<'s>> + Send, { let mut encodings = inputs .into_maybe_par_iter() .map(|input| self.encode_char_offsets(input, add_special_tokens)) .collect::<Result<Vec<Encoding>>>()?; if let Some(params) = &self.padding { // We do the padding here to make sure we handle the batch padding pad_encodings(&mut encodings, params)?; } Ok(encodings) } /// Encode all the sentences in parallel, using multiple threads pub fn encode_batch_fast<'s, E>( &self, inputs: Vec<E>, add_special_tokens: bool, ) -> Result<Vec<Encoding>> where E: Into<EncodeInput<'s>> + Send, { let mut encodings = inputs .into_maybe_par_iter() .map(|input| self.encode_fast(input, add_special_tokens)) .collect::<Result<Vec<Encoding>>>()?; if let Some(params) = &self.padding { // We do the padding here to make sure we handle the batch padding pad_encodings(&mut encodings, params)?; } Ok(encodings) } /// Decode all sentences in parallel pub fn decode_batch( &self, sentences: &[&[u32]], skip_special_tokens: bool, ) -> Result<Vec<String>> where M: Send + Sync, { sentences .into_maybe_par_iter() .map(|sentence| self.decode(sentence, skip_special_tokens)) .collect() } /// Train our Model from files pub fn train_from_files<T>(&mut self, trainer: &mut T, files: Vec<String>) -> Result<&mut Self> where T: Trainer<Model = M> + Sync, { let mut len = 0; for file in files.iter() { len += File::open(file) .and_then(|f| f.metadata()) .map(|m| m.len())?; } let max_read = 1_000_000; ResultShunt::process( files.into_iter().flat_map(|filename| { match File::open(filename) { Ok(file) => { let file = BufReader::with_capacity(max_read, file); // We read new lines using this API instead of the Lines Iterator // on purpose. We want to keep the `\n` and potential `\r` between each lines // We use an iterator to be able to chain with par_bridge. itertools::Either::Left(file.lines_with_ending()) } Err(e) => itertools::Either::Right(std::iter::once(Err(e))), } }), |sequences| -> Result<()> { let progress = if trainer.should_show_progress() { let progress = ProgressBar::new(len); progress.set_style( ProgressStyle::default_bar() .template("[{elapsed_precise}] {msg:<30!} {wide_bar} {percent:>18!}%") .expect("Invalid progress template"), ); progress .set_message(format!("Pre-processing files ({:.2} Mo)", len / 1_000_000)); Some(progress) } else { None }; trainer.feed( sequences.inspect(|s| { if let Some(progress) = &progress { progress.inc(s.len() as u64) } }), |seq| { let normalized = self.do_normalize(seq.as_ref())?; let pre_tokenized = self.do_pre_tokenize(normalized)?; Ok(pre_tokenized .get_splits(OffsetReferential::Original, OffsetType::Byte) .into_iter() .map(|(s, _, _)| s.to_owned()) .collect()) }, )?; if let Some(pbar) = progress { pbar.finish(); } let special_tokens = trainer.train(&mut self.model)?; self.add_special_tokens(&special_tokens); Ok(()) }, )??; Ok(self) } /// Train our Model, using the given Trainer and iterator pub fn train<T, I, S>(&mut self, trainer: &mut T, sequences: I) -> Result<&mut Self> where T: Trainer<Model = M> + Sync, I: Iterator<Item = S> + Send, S: AsRef<str> + Send, { let (lower, upper) = sequences.size_hint(); let len = upper.unwrap_or(lower) as u64; let progress = if trainer.should_show_progress() { let progress = ProgressBar::new(len); progress.set_style( ProgressStyle::default_bar() .template("[{elapsed_precise}] {msg:<30!} {wide_bar} {pos:<9!}/{len:>9!}") .expect("Invalid progress template"), ); progress.set_message("Pre-processing sequences"); Some(progress) } else { None }; trainer.feed( sequences.inspect(|_s| { if let Some(progress) = &progress { progress.inc(1) } }), |seq| { let normalized = self.do_normalize(seq.as_ref())?; let pre_tokenized = self.do_pre_tokenize(normalized)?; Ok(pre_tokenized .get_splits(OffsetReferential::Original, OffsetType::Byte) .into_iter() .map(|(s, _, _)| s.to_owned()) .collect()) }, )?; if let Some(pbar) = progress { pbar.finish(); } let special_tokens = trainer.train(&mut self.model)?; self.add_special_tokens(&special_tokens); Ok(self) } } impl<M, N, PT, PP, D> std::str::FromStr for TokenizerImpl<M, N, PT, PP, D> where M: for<'de> Deserialize<'de> + Model, N: for<'de> Deserialize<'de> + Normalizer, PT: for<'de> Deserialize<'de> + PreTokenizer, PP: for<'de> Deserialize<'de> + PostProcessor, D: for<'de> Deserialize<'de> + Decoder, { type Err = Error; fn from_str(s: &str) -> Result<Self> { Ok(serde_json::from_str(s)?) } } impl<M, N, PT, PP, D> TokenizerImpl<M, N, PT, PP, D> where M: DeserializeOwned + Model, N: DeserializeOwned + Normalizer, PT: DeserializeOwned + PreTokenizer, PP: DeserializeOwned + PostProcessor, D: DeserializeOwned + Decoder, { /// Instantiate a new Tokenizer from the given file pub fn from_file<P: AsRef<Path>>(file: P) -> Result<Self> { let content = read_to_string(file)?; let tokenizer = serde_json::from_str(&content)?; Ok(tokenizer) } } impl<M, N, PT, PP, D> TokenizerImpl<M, N, PT, PP, D> where M: DeserializeOwned + Model, N: DeserializeOwned + Normalizer, PT: DeserializeOwned + PreTokenizer, PP: DeserializeOwned + PostProcessor, D: DeserializeOwned + Decoder, { /// Instantiate a new Tokenizer from bytes pub fn from_bytes<P: AsRef<[u8]>>(bytes: P) -> Result<Self> { let tokenizer = serde_json::from_slice(bytes.as_ref())?; Ok(tokenizer) } } impl<M, N, PT, PP, D> TokenizerImpl<M, N, PT, PP, D> where M: DeserializeOwned + Model, N: DeserializeOwned + Normalizer, PT: DeserializeOwned + PreTokenizer, PP: DeserializeOwned + PostProcessor, D: DeserializeOwned + Decoder, { #[deprecated( since = "0.14.0", note = "Users should download the file separately using https://github.com/huggingface/hf-hub instead, which splits concerns of accessing the web, and should use the new cache layout" )] #[cfg(feature = "http")] /// Instantiate a new Tokenizer from a file hosted on the Hugging Face Hub. /// It expects the `identifier` of a model that includes a `tokenizer.json` file. pub fn from_pretrained<S: AsRef<str>>( identifier: S, params: Option<crate::utils::from_pretrained::FromPretrainedParameters>, ) -> Result<Self> { let tokenizer_file = crate::utils::from_pretrained::from_pretrained(identifier, params)?; TokenizerImpl::from_file(tokenizer_file) } } impl<M, N, PT, PP, D> TokenizerImpl<M, N, PT, PP, D> where M: Serialize, N: Serialize, PT: Serialize, PP: Serialize, D: Serialize, { /// Serialize the current tokenizer as a String pub fn to_string(&self, pretty: bool) -> Result<String> { Ok(if pretty { serde_json::to_string_pretty(self)? } else { serde_json::to_string(self)? }) } /// Save the current tokenizer at the given path pub fn save<P: AsRef<Path>>(&self, path: P, pretty: bool) -> Result<()> { let serialized = self.to_string(pretty)?; let mut file = File::create(path)?; file.write_all(serialized.as_bytes())?; Ok(()) } }
tokenizers/tokenizers/src/tokenizer/mod.rs/0
{ "file_path": "tokenizers/tokenizers/src/tokenizer/mod.rs", "repo_id": "tokenizers", "token_count": 22643 }
use tokenizers::decoders::wordpiece::WordPiece as WordPieceDecoder; use tokenizers::models::bpe::BPE; use tokenizers::models::wordpiece::WordPiece; use tokenizers::normalizers::bert::BertNormalizer; use tokenizers::pre_tokenizers::bert::BertPreTokenizer; use tokenizers::pre_tokenizers::byte_level::ByteLevel; use tokenizers::processors::bert::BertProcessing; use tokenizers::tokenizer::{Model, Tokenizer}; #[allow(dead_code)] pub fn get_empty() -> Tokenizer { Tokenizer::new(BPE::default()) } #[allow(dead_code)] pub fn get_byte_level_bpe() -> BPE { BPE::from_file("data/gpt2-vocab.json", "data/gpt2-merges.txt") .build() .expect("Files not found, run `make test` to download these files") } #[allow(dead_code)] pub fn get_byte_level(add_prefix_space: bool, trim_offsets: bool) -> Tokenizer { let mut tokenizer = Tokenizer::new(get_byte_level_bpe()); tokenizer .with_pre_tokenizer(Some( ByteLevel::default().add_prefix_space(add_prefix_space), )) .with_decoder(Some(ByteLevel::default())) .with_post_processor(Some(ByteLevel::default().trim_offsets(trim_offsets))); tokenizer } #[allow(dead_code)] pub fn get_bert_wordpiece() -> WordPiece { WordPiece::from_file("data/bert-base-uncased-vocab.txt") .build() .expect("Files not found, run `make test` to download these files") } #[allow(dead_code)] pub fn get_bert() -> Tokenizer { let mut tokenizer = Tokenizer::new(get_bert_wordpiece()); let sep = tokenizer.get_model().token_to_id("[SEP]").unwrap(); let cls = tokenizer.get_model().token_to_id("[CLS]").unwrap(); tokenizer .with_normalizer(Some(BertNormalizer::default())) .with_pre_tokenizer(Some(BertPreTokenizer)) .with_decoder(Some(WordPieceDecoder::default())) .with_post_processor(Some(BertProcessing::new( (String::from("[SEP]"), sep), (String::from("[CLS]"), cls), ))); tokenizer }
tokenizers/tokenizers/tests/common/mod.rs/0
{ "file_path": "tokenizers/tokenizers/tests/common/mod.rs", "repo_id": "tokenizers", "token_count": 811 }
# Building a Vanilla JavaScript Application In this tutorial, you’ll build a simple web application that detects objects in images using Transformers.js! To follow along, all you need is a code editor, a browser, and a simple server (e.g., VS Code Live Server). Here's how it works: the user clicks “Upload image” and selects an image using an input dialog. After analysing the image with an object detection model, the predicted bounding boxes are overlaid on top of the image, like this: ![Demo](https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/js-detection-interence-zebra.png) Useful links: - [Demo site](https://huggingface.co/spaces/Scrimba/vanilla-js-object-detector) - [Interactive code walk-through (scrim)](https://scrimba.com/scrim/cKm9bDAg) - [Source code](https://github.com/huggingface/transformers.js/tree/main/examples/vanilla-js) ## Step 1: HTML and CSS setup Before we start building with Transformers.js, we first need to lay the groundwork with some markup and styling. Create an `index.html` file with a basic HTML skeleton, and add the following `<main>` tag to the `<body>`: ```html <main class="container"> <label class="custom-file-upload"> <input id="file-upload" type="file" accept="image/*" /> <img class="upload-icon" src="https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/upload-icon.png" /> Upload image </label> <div id="image-container"></div> <p id="status"></p> </main> ``` <details> <summary>Click here to see a breakdown of this markup.</summary> We’re adding an `<input>` element with `type="file"` that accepts images. This allows the user to select an image from their local file system using a popup dialog. The default styling for this element looks quite bad, so let's add some styling. The easiest way to achieve this is to wrap the `<input>` element in a `<label>`, hide the input, and then style the label as a button. We’re also adding an empty `<div>` container for displaying the image, plus an empty `<p>` tag that we'll use to give status updates to the user while we download and run the model, since both of these operations take some time. </details> Next, add the following CSS rules in a `style.css` file and link it to the HTML: ```css html, body { font-family: Arial, Helvetica, sans-serif; } .container { margin: 40px auto; width: max(50vw, 400px); display: flex; flex-direction: column; align-items: center; } .custom-file-upload { display: flex; align-items: center; gap: 10px; border: 2px solid black; padding: 8px 16px; cursor: pointer; border-radius: 6px; } #file-upload { display: none; } .upload-icon { width: 30px; } #image-container { width: 100%; margin-top: 20px; position: relative; } #image-container>img { width: 100%; } ``` Here's how the UI looks at this point: ![Demo](https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/js-detection-btn.png) ## Step 2: JavaScript setup With the *boring* part out of the way, let's start writing some JavaScript code! Create a file called `index.js` and link to it in `index.html` by adding the following to the end of the `<body>`: ```html <script src="./index.js" type="module"></script> ``` <Tip> The `type="module"` attribute is important, as it turns our file into a [JavaScript module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules), meaning that we’ll be able to use imports and exports. </Tip> Moving into `index.js`, let's import Transformers.js by adding the following line to the top of the file: ```js import { pipeline, env } from "https://cdn.jsdelivr.net/npm/@huggingface/transformers"; ``` Since we will be downloading the model from the Hugging Face Hub, we can skip the local model check by setting: ```js env.allowLocalModels = false; ``` Next, let's create references to the various DOM elements we will access later: ```js const fileUpload = document.getElementById("file-upload"); const imageContainer = document.getElementById("image-container"); const status = document.getElementById("status"); ``` ## Step 3: Create an object detection pipeline We’re finally ready to create our object detection pipeline! As a reminder, a [pipeline](../pipelines). is a high-level interface provided by the library to perform a specific task. In our case, we will instantiate an object detection pipeline with the `pipeline()` helper function. Since this can take some time (especially the first time when we have to download the ~40MB model), we first update the `status` paragraph so that the user knows that we’re about to load the model. ```js status.textContent = "Loading model..."; ``` <Tip> To keep this tutorial simple, we'll be loading and running the model in the main (UI) thread. This is not recommended for production applications, since the UI will freeze when we're performing these actions. This is because JavaScript is a single-threaded language. To overcome this, you can use a [web worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) to download and run the model in the background. However, we’re not going to do cover that in this tutorial... </Tip> We can now call the `pipeline()` function that we imported at the top of our file, to create our object detection pipeline: ```js const detector = await pipeline("object-detection", "Xenova/detr-resnet-50"); ``` We’re passing two arguments into the `pipeline()` function: (1) task and (2) model. 1. The first tells Transformers.js what kind of task we want to perform. In our case, that is `object-detection`, but there are many other tasks that the library supports, including `text-generation`, `sentiment-analysis`, `summarization`, or `automatic-speech-recognition`. See [here](https://huggingface.co/docs/transformers.js/pipelines#tasks) for the full list. 2. The second argument specifies which model we would like to use to solve the given task. We will use [`Xenova/detr-resnet-50`](https://huggingface.co/Xenova/detr-resnet-50), as it is a relatively small (~40MB) but powerful model for detecting objects in an image. Once the function returns, we’ll tell the user that the app is ready to be used. ```js status.textContent = "Ready"; ``` ## Step 4: Create the image uploader The next step is to support uploading/selection of images. To achieve this, we will listen for "change" events from the `fileUpload` element. In the callback function, we use a `FileReader()` to read the contents of the image if one is selected (and nothing otherwise). ```js fileUpload.addEventListener("change", function (e) { const file = e.target.files[0]; if (!file) { return; } const reader = new FileReader(); // Set up a callback when the file is loaded reader.onload = function (e2) { imageContainer.innerHTML = ""; const image = document.createElement("img"); image.src = e2.target.result; imageContainer.appendChild(image); // detect(image); // Uncomment this line to run the model }; reader.readAsDataURL(file); }); ``` Once the image has been loaded into the browser, the `reader.onload` callback function will be invoked. In it, we append the new `<img>` element to the `imageContainer` to be displayed to the user. Don’t worry about the `detect(image)` function call (which is commented out) - we will explain it later! For now, try to run the app and upload an image to the browser. You should see your image displayed under the button like this: ![Demo](https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/js-detection-btn-img.png) ## Step 5: Run the model We’re finally ready to start interacting with Transformers.js! Let’s uncomment the `detect(image)` function call from the snippet above. Then we’ll define the function itself: ```js async function detect(img) { status.textContent = "Analysing..."; const output = await detector(img.src, { threshold: 0.5, percentage: true, }); status.textContent = ""; console.log("output", output); // ... } ``` <Tip> NOTE: The `detect` function needs to be asynchronous, since we’ll `await` the result of the the model. </Tip> Once we’ve updated the `status` to "Analysing", we’re ready to perform *inference*, which simply means to run the model with some data. This is done via the `detector()` function that was returned from `pipeline()`. The first argument we’re passing is the image data (`img.src`). The second argument is an options object: - We set the `threshold` property to `0.5`. This means that we want the model to be at least 50% confident before claiming it has detected an object in the image. The lower the threshold, the more objects it'll detect (but may misidentify objects); the higher the threshold, the fewer objects it'll detect (but may miss objects in the scene). - We also specify `percentage: true`, which means that we want the bounding box for the objects to be returned as percentages (instead of pixels). If you now try to run the app and upload an image, you should see the following output logged to the console: ![Demo](https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/js-detection-console.png) In the example above, we uploaded an image of two elephants, so the `output` variable holds an array with two objects, each containing a `label` (the string “elephant”), a `score` (indicating the model's confidence in its prediction) and a `box` object (representing the bounding box of the detected entity). ## Step 6: Render the boxes The final step is to display the `box` coordinates as rectangles around each of the elephants. At the end of our `detect()` function, we’ll run the `renderBox` function on each object in the `output` array, using `.forEach()`. ```js output.forEach(renderBox); ``` Here’s the code for the `renderBox()` function with comments to help you understand what’s going on: ```js // Render a bounding box and label on the image function renderBox({ box, label }) { const { xmax, xmin, ymax, ymin } = box; // Generate a random color for the box const color = "#" + Math.floor(Math.random() * 0xffffff).toString(16).padStart(6, 0); // Draw the box const boxElement = document.createElement("div"); boxElement.className = "bounding-box"; Object.assign(boxElement.style, { borderColor: color, left: 100 * xmin + "%", top: 100 * ymin + "%", width: 100 * (xmax - xmin) + "%", height: 100 * (ymax - ymin) + "%", }); // Draw the label const labelElement = document.createElement("span"); labelElement.textContent = label; labelElement.className = "bounding-box-label"; labelElement.style.backgroundColor = color; boxElement.appendChild(labelElement); imageContainer.appendChild(boxElement); } ``` The bounding box and label span also need some styling, so add the following to the `style.css` file: ```css .bounding-box { position: absolute; box-sizing: border-box; border-width: 2px; border-style: solid; } .bounding-box-label { color: white; position: absolute; font-size: 12px; margin-top: -16px; margin-left: -2px; padding: 1px; } ``` **And that’s it!** You've now built your own fully-functional AI application that detects objects in images, which runns completely in your browser: no external server, APIs, or build tools. Pretty cool! 🥳 ![Demo](https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/js-detection-inference-elephant.png) The app is live at the following URL: [https://huggingface.co/spaces/Scrimba/vanilla-js-object-detector](https://huggingface.co/spaces/Scrimba/vanilla-js-object-detector)
transformers.js/docs/source/tutorials/vanilla-js.md/0
{ "file_path": "transformers.js/docs/source/tutorials/vanilla-js.md", "repo_id": "transformers.js", "token_count": 3621 }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Transformers.js - Code completion playground</title> </head> <body> <div id="root"></div> <script type="module" src="/src/main.jsx"></script> </body> </html>
transformers.js/examples/code-completion/index.html/0
{ "file_path": "transformers.js/examples/code-completion/index.html", "repo_id": "transformers.js", "token_count": 133 }
import path from 'path'; // Needed for deploying to GitHub pages const BASE_PATH = process.env.BASE_PATH ?? ''; export default { // config options base: BASE_PATH, root: path.join(__dirname, 'src'), build: { outDir: path.join(__dirname, 'dist') }, publicDir: path.join(__dirname, 'public'), }
transformers.js/examples/demo-site/vite.config.js/0
{ "file_path": "transformers.js/examples/demo-site/vite.config.js", "repo_id": "transformers.js", "token_count": 126 }
const { app, BrowserWindow, ipcMain } = require('electron'); const path = require('path'); const { session } = require('electron'); const { run } = require('./model.js'); // Handle creating/removing shortcuts on Windows when installing/uninstalling. if (require('electron-squirrel-startup')) { app.quit(); } const createWindow = () => { // Create the browser window. const mainWindow = new BrowserWindow({ width: 800, height: 600, webPreferences: { preload: path.join(__dirname, 'preload.js'), }, }); // and load the index.html of the app. mainWindow.loadFile(path.join(__dirname, 'index.html')); // Open the DevTools. mainWindow.webContents.openDevTools(); }; // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. app.on('ready', () => { // Add a handler for the `transformers:run` event. This enables 2-way communication // between the renderer process (UI) and the main process (processing). // https://www.electronjs.org/docs/latest/tutorial/ipc#pattern-2-renderer-to-main-two-way ipcMain.handle('transformers:run', run) createWindow(); // Define a custom Content Security Policy to only allow loading resources from the app's origin. session.defaultSession.webRequest.onHeadersReceived((details, callback) => { callback({ responseHeaders: { ...details.responseHeaders, 'Content-Security-Policy': ["default-src 'self'"] } }) }) }); // Quit when all windows are closed, except on macOS. There, it's common // for applications and their menu bar to stay active until the user quits // explicitly with Cmd + Q. app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); } }); app.on('activate', () => { // On OS X it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. if (BrowserWindow.getAllWindows().length === 0) { createWindow(); } }); // In this file you can include the rest of your app's specific main process // code. You can also put them in separate files and import them here.
transformers.js/examples/electron/src/index.js/0
{ "file_path": "transformers.js/examples/electron/src/index.js", "repo_id": "transformers.js", "token_count": 796 }
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>MusicGen Web | In-browser text-to-music w/ 🤗 Transformers.js!</title> </head> <body> <div id="root"></div> <script type="module" src="/src/main.jsx"></script> </body> </html>
transformers.js/examples/musicgen-web/index.html/0
{ "file_path": "transformers.js/examples/musicgen-web/index.html", "repo_id": "transformers.js", "token_count": 145 }
{ "name": "next", "version": "0.1.0", "private": true, "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint" }, "dependencies": { "@huggingface/transformers": "^3.0.0-alpha.5", "autoprefixer": "10.4.14", "eslint": "8.45.0", "eslint-config-next": "13.4.12", "next": "^14.2.3", "postcss": "8.4.31", "react": "18.2.0", "react-dom": "18.2.0", "tailwindcss": "3.3.3" }, "overrides": { "protobufjs": "^7.2.4" } }
transformers.js/examples/next-client/package.json/0
{ "file_path": "transformers.js/examples/next-client/package.json", "repo_id": "transformers.js", "token_count": 280 }
import './style.css'; import { AutoModel, AutoProcessor, env, RawImage } from '@xenova/transformers'; // Since we will download the model from the Hugging Face Hub, we can skip the local model check env.allowLocalModels = false; // Proxy the WASM backend to prevent the UI from freezing env.backends.onnx.wasm.proxy = true; // Constants const EXAMPLE_URL = 'https://images.pexels.com/photos/5965592/pexels-photo-5965592.jpeg?auto=compress&cs=tinysrgb&w=1024'; // Reference the elements that we will need const status = document.getElementById('status'); const fileUpload = document.getElementById('upload'); const imageContainer = document.getElementById('container'); const example = document.getElementById('example'); // Load model and processor status.textContent = 'Loading model...'; const model = await AutoModel.from_pretrained('briaai/RMBG-1.4', { // Do not require config.json to be present in the repository config: { model_type: 'custom' }, }); const processor = await AutoProcessor.from_pretrained('briaai/RMBG-1.4', { // Do not require config.json to be present in the repository config: { do_normalize: true, do_pad: false, do_rescale: true, do_resize: true, image_mean: [0.5, 0.5, 0.5], feature_extractor_type: "ImageFeatureExtractor", image_std: [1, 1, 1], resample: 2, rescale_factor: 0.00392156862745098, size: { width: 1024, height: 1024 }, } }); status.textContent = 'Ready'; example.addEventListener('click', (e) => { e.preventDefault(); predict(EXAMPLE_URL); }); fileUpload.addEventListener('change', function (e) { const file = e.target.files[0]; if (!file) { return; } const reader = new FileReader(); // Set up a callback when the file is loaded reader.onload = e2 => predict(e2.target.result); reader.readAsDataURL(file); }); // Predict foreground of the given image async function predict(url) { // Read image const image = await RawImage.fromURL(url); // Update UI imageContainer.innerHTML = ''; imageContainer.style.backgroundImage = `url(${url})`; // Set container width and height depending on the image aspect ratio const ar = image.width / image.height; const [cw, ch] = (ar > 720 / 480) ? [720, 720 / ar] : [480 * ar, 480]; imageContainer.style.width = `${cw}px`; imageContainer.style.height = `${ch}px`; status.textContent = 'Analysing...'; // Preprocess image const { pixel_values } = await processor(image); // Predict alpha matte const { output } = await model({ input: pixel_values }); // Resize mask back to original size const mask = await RawImage.fromTensor(output[0].mul(255).to('uint8')).resize(image.width, image.height); // Create new canvas const canvas = document.createElement('canvas'); canvas.width = image.width; canvas.height = image.height; const ctx = canvas.getContext('2d'); // Draw original image output to canvas ctx.drawImage(image.toCanvas(), 0, 0); // Update alpha channel const pixelData = ctx.getImageData(0, 0, image.width, image.height); for (let i = 0; i < mask.data.length; ++i) { pixelData.data[4 * i + 3] = mask.data[i]; } ctx.putImageData(pixelData, 0, 0); // Update UI imageContainer.append(canvas); imageContainer.style.removeProperty('background-image'); imageContainer.style.background = `url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQBAMAAADt3eJSAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAGUExURb+/v////5nD/3QAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAUSURBVBjTYwABQSCglEENMxgYGAAynwRB8BEAgQAAAABJRU5ErkJggg==")`; status.textContent = 'Done!'; }
transformers.js/examples/remove-background-client/main.js/0
{ "file_path": "transformers.js/examples/remove-background-client/main.js", "repo_id": "transformers.js", "token_count": 1399 }
'use client' export function SearchBar({ search }) { return (<form onSubmit={e => { e.preventDefault(); const formData = new FormData(e.target); const text = formData.get('text'); search(text); }} className='relative mb-2' > <div className="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none"> <svg className="w-4 h-4 text-gray-500 dark:text-gray-400" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 20 20"> <path stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="m19 19-4-4m0-7A7 7 0 1 1 1 8a7 7 0 0 1 14 0Z" /> </svg> </div> <input type="search" name="text" id="default-search" className="block w-full p-4 pl-10 text-sm text-gray-900 border border-gray-300 rounded-lg bg-gray-50 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Search for images..." required /> <button type="submit" className="text-white absolute right-2.5 bottom-2.5 bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-4 py-2 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800" > Search </button> </form>) }
transformers.js/examples/semantic-image-search-client/src/app/components/SearchBar.jsx/0
{ "file_path": "transformers.js/examples/semantic-image-search-client/src/app/components/SearchBar.jsx", "repo_id": "transformers.js", "token_count": 734 }
{ "name": "semantic-image-search", "version": "0.1.0", "private": true, "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint" }, "dependencies": { "@supabase/supabase-js": "^2.31.0", "@xenova/transformers": "^2.5.0", "autoprefixer": "10.4.14", "blurhash": "^2.0.5", "eslint": "8.45.0", "eslint-config-next": "13.4.12", "next": "^14.2.3", "postcss": "^8.4.31", "react": "18.2.0", "react-dom": "18.2.0", "tailwindcss": "3.3.3" }, "overrides": { "protobufjs": "^7.2.4" } }
transformers.js/examples/semantic-image-search/package.json/0
{ "file_path": "transformers.js/examples/semantic-image-search/package.json", "repo_id": "transformers.js", "token_count": 319 }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <link rel="stylesheet" href="style.css" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Transformers.js - Object Detection demo</title> </head> <body> <main class="container"> <label class="custom-file-upload"> <input id="file-upload" type="file" accept="image/*" /> <img class="upload-icon" src="https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/upload-icon.png" /> Upload image </label> <div id="image-container"></div> <p id="status"></p> </main> <script src="index.js" type="module"></script> </body> </html>
transformers.js/examples/vanilla-js/index.html/0
{ "file_path": "transformers.js/examples/vanilla-js/index.html", "repo_id": "transformers.js", "token_count": 316 }
import { AutoTokenizer, CLIPTextModelWithProjection, AutoProcessor, CLIPVisionModelWithProjection, RawImage, dot, softmax, } from '@xenova/transformers'; import './style.css'; // Reference the elements that we will need const status = document.getElementById('status'); const container = document.getElementById('container'); const video = document.getElementById('video'); const labelsInput = document.getElementById('labels'); const templateInput = document.getElementById('template'); const overlay = document.getElementById('overlay'); status.textContent = 'Loading model (300MB)...'; // Use fp16 if available, otherwise use fp32 async function hasFp16() { try { const adapter = await navigator.gpu.requestAdapter(); return adapter.features.has('shader-f16'); } catch (e) { return false; } } const dtype = (await hasFp16()) ? 'fp16' : 'fp32'; // Load object detection pipeline const model_id = 'Xenova/clip-vit-base-patch16'; let tokenizer, text_model, processor, vision_model; try { // Load tokenizer and text model tokenizer = await AutoTokenizer.from_pretrained(model_id); text_model = await CLIPTextModelWithProjection.from_pretrained(model_id, { device: 'webgpu', dtype, }); // Load processor and vision model processor = await AutoProcessor.from_pretrained(model_id); vision_model = await CLIPVisionModelWithProjection.from_pretrained(model_id, { device: 'webgpu', dtype, }); } catch (err) { status.textContent = err.message; alert(err.message) throw err; } labelsInput.disabled = false; templateInput.disabled = false; status.textContent = 'Ready'; // See `model.logit_scale` parameter of original model const exp_logit_scale = Math.exp(4.6052); const IMAGE_SIZE = 224; const canvas = document.createElement('canvas'); canvas.width = canvas.height = IMAGE_SIZE; const context = canvas.getContext('2d', { willReadFrequently: true }); let isProcessing = false; let previousTime; let textEmbeddings; let prevTextInputs; let prevTemplate; let labels; function onFrameUpdate() { if (!isProcessing) { isProcessing = true; (async function () { // If text inputs have changed, update the embeddings if (prevTextInputs !== labelsInput.value || prevTemplate !== templateInput.value) { textEmbeddings = null; prevTextInputs = labelsInput.value; prevTemplate = templateInput.value; labels = prevTextInputs.split(/\s*,\s*/).filter(x => x); if (labels.length > 0) { const texts = labels.map(x => templateInput.value.replaceAll('{}', x)); const text_inputs = tokenizer(texts, { padding: true, truncation: true }); // Compute embeddings const { text_embeds } = await text_model(text_inputs); textEmbeddings = text_embeds.normalize().tolist(); } else { overlay.innerHTML = ''; } } if (textEmbeddings) { // Read the current frame from the video context.drawImage(video, 0, 0, IMAGE_SIZE, IMAGE_SIZE); const pixelData = context.getImageData(0, 0, IMAGE_SIZE, IMAGE_SIZE).data; const image = new RawImage(pixelData, IMAGE_SIZE, IMAGE_SIZE, 4); const image_inputs = await processor(image); // Compute embeddings const { image_embeds } = await vision_model(image_inputs); const imageEmbedding = image_embeds.normalize().tolist()[0]; // Compute similarity const similarities = textEmbeddings.map( x => dot(x, imageEmbedding) * exp_logit_scale ); const sortedIndices = softmax(similarities) .map((x, i) => [x, i]) .sort((a, b) => b[0] - a[0]); // Update UI overlay.innerHTML = ''; for (const [score, index] of sortedIndices) { overlay.appendChild(document.createTextNode(`${labels[index]}: ${score.toFixed(2)}`)); overlay.appendChild(document.createElement('br')); } } if (previousTime !== undefined) { const fps = 1000 / (performance.now() - previousTime); status.textContent = `FPS: ${fps.toFixed(2)}`; } previousTime = performance.now(); isProcessing = false; })(); } window.requestAnimationFrame(onFrameUpdate); } // Start the video stream navigator.mediaDevices.getUserMedia( { video: true }, // Ask for video ).then((stream) => { // Set up the video and canvas elements. video.srcObject = stream; video.play(); const videoTrack = stream.getVideoTracks()[0]; const { width, height } = videoTrack.getSettings(); video.width = width; video.height = height; // Set container width and height depending on the image aspect ratio const ar = width / height; const [cw, ch] = (ar > 720 / 405) ? [720, 720 / ar] : [405 * ar, 405]; container.style.width = `${cw}px`; container.style.height = `${ch}px`; // Start the animation loop window.requestAnimationFrame(onFrameUpdate); }).catch((error) => { alert(error); });
transformers.js/examples/webgpu-clip/main.js/0
{ "file_path": "transformers.js/examples/webgpu-clip/main.js", "repo_id": "transformers.js", "token_count": 2330 }
import { useState } from "react"; import CrossIcon from "./icons/CrossIcon" export default function ImagePreview({ src, onRemove, ...props }) { const [hover, setHover] = useState(false); return ( <div {...props} onMouseEnter={() => setHover(true)} onMouseLeave={() => setHover(false)} > <CrossIcon onClick={onRemove} className={`absolute top-0 right-0 cursor-pointer dark:fill-gray-400 dark:text-gray-100 fill-gray-200 text-gray-800 ${hover ? '' : 'hidden'}`} /> <img src={src} alt="Upload preview" className="w-full h-full object-cover rounded-md" /> </div>) }
transformers.js/examples/webgpu-vlm/src/components/ImagePreview.jsx/0
{ "file_path": "transformers.js/examples/webgpu-vlm/src/components/ImagePreview.jsx", "repo_id": "transformers.js", "token_count": 272 }
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <link rel="icon" type="image/png" href="/logo.png" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Whisper WebGPU</title> </head> <body> <div id="root"></div> <script type="module" src="/src/main.jsx"></script> </body> </html>
transformers.js/examples/webgpu-whisper/index.html/0
{ "file_path": "transformers.js/examples/webgpu-whisper/index.html", "repo_id": "transformers.js", "token_count": 153 }
# Support exporting vision and text models separately: # Adapted from https://github.com/huggingface/optimum/issues/1186#issuecomment-1637641760 from optimum.exporters.onnx.model_configs import CLIPTextOnnxConfig, ViTOnnxConfig from typing import Dict class CLIPVisionOnnxConfig(ViTOnnxConfig): pass class CLIPTextModelWithProjectionOnnxConfig(CLIPTextOnnxConfig): @property def outputs(self) -> Dict[str, Dict[int, str]]: return { "text_embeds": {0: "batch_size"}, } def generate_dummy_inputs(self, framework: str = "pt", **kwargs): dummy_inputs = super().generate_dummy_inputs(framework=framework, **kwargs) if framework == "pt": import torch dummy_inputs["input_ids"] = dummy_inputs["input_ids"].to(dtype=torch.int64) return dummy_inputs class CLIPVisionModelWithProjectionOnnxConfig(CLIPVisionOnnxConfig): @property def outputs(self) -> Dict[str, Dict[int, str]]: return { "image_embeds": {0: "batch_size"}, }
transformers.js/scripts/extra/clip.py/0
{ "file_path": "transformers.js/scripts/extra/clip.py", "repo_id": "transformers.js", "token_count": 439 }
/** * @file Module used to configure Transformers.js. * * **Example:** Disable remote models. * ```javascript * import { env } from '@huggingface/transformers'; * env.allowRemoteModels = false; * ``` * * **Example:** Set local model path. * ```javascript * import { env } from '@huggingface/transformers'; * env.localModelPath = '/path/to/local/models/'; * ``` * * **Example:** Set cache directory. * ```javascript * import { env } from '@huggingface/transformers'; * env.cacheDir = '/path/to/cache/directory/'; * ``` * * @module env */ import fs from 'fs'; import path from 'path'; import url from 'url'; const VERSION = '3.3.3'; // Check if various APIs are available (depends on environment) const IS_BROWSER_ENV = typeof window !== "undefined" && typeof window.document !== "undefined"; const IS_WEBWORKER_ENV = typeof self !== "undefined" && self.constructor?.name === 'DedicatedWorkerGlobalScope'; const IS_WEB_CACHE_AVAILABLE = typeof self !== "undefined" && 'caches' in self; const IS_WEBGPU_AVAILABLE = typeof navigator !== 'undefined' && 'gpu' in navigator; const IS_WEBNN_AVAILABLE = typeof navigator !== 'undefined' && 'ml' in navigator; const IS_PROCESS_AVAILABLE = typeof process !== 'undefined'; const IS_NODE_ENV = IS_PROCESS_AVAILABLE && process?.release?.name === 'node'; const IS_FS_AVAILABLE = !isEmpty(fs); const IS_PATH_AVAILABLE = !isEmpty(path); /** * A read-only object containing information about the APIs available in the current environment. */ export const apis = Object.freeze({ /** Whether we are running in a browser environment (and not a web worker) */ IS_BROWSER_ENV, /** Whether we are running in a web worker environment */ IS_WEBWORKER_ENV, /** Whether the Cache API is available */ IS_WEB_CACHE_AVAILABLE, /** Whether the WebGPU API is available */ IS_WEBGPU_AVAILABLE, /** Whether the WebNN API is available */ IS_WEBNN_AVAILABLE, /** Whether the Node.js process API is available */ IS_PROCESS_AVAILABLE, /** Whether we are running in a Node.js environment */ IS_NODE_ENV, /** Whether the filesystem API is available */ IS_FS_AVAILABLE, /** Whether the path API is available */ IS_PATH_AVAILABLE, }); const RUNNING_LOCALLY = IS_FS_AVAILABLE && IS_PATH_AVAILABLE; let dirname__ = './'; if (RUNNING_LOCALLY) { // NOTE: We wrap `import.meta` in a call to `Object` to prevent Webpack from trying to bundle it in CommonJS. // Although we get the warning: "Accessing import.meta directly is unsupported (only property access or destructuring is supported)", // it is safe to ignore since the bundled value (`{}`) isn't used for CommonJS environments (we use __dirname instead). const _import_meta_url = Object(import.meta).url; if (_import_meta_url) { dirname__ = path.dirname(path.dirname(url.fileURLToPath(_import_meta_url))) // ESM } else if (typeof __dirname !== 'undefined') { dirname__ = path.dirname(__dirname) // CommonJS } } // Only used for environments with access to file system const DEFAULT_CACHE_DIR = RUNNING_LOCALLY ? path.join(dirname__, '/.cache/') : null; // Set local model path, based on available APIs const DEFAULT_LOCAL_MODEL_PATH = '/models/'; const localModelPath = RUNNING_LOCALLY ? path.join(dirname__, DEFAULT_LOCAL_MODEL_PATH) : DEFAULT_LOCAL_MODEL_PATH; /** * Global variable given visible to users to control execution. This provides users a simple way to configure Transformers.js. * @typedef {Object} TransformersEnvironment * @property {string} version This version of Transformers.js. * @property {{onnx: Partial<import('onnxruntime-common').Env>}} backends Expose environment variables of different backends, * allowing users to set these variables if they want to. * @property {boolean} allowRemoteModels Whether to allow loading of remote files, defaults to `true`. * If set to `false`, it will have the same effect as setting `local_files_only=true` when loading pipelines, models, tokenizers, processors, etc. * @property {string} remoteHost Host URL to load models from. Defaults to the Hugging Face Hub. * @property {string} remotePathTemplate Path template to fill in and append to `remoteHost` when loading models. * @property {boolean} allowLocalModels Whether to allow loading of local files, defaults to `false` if running in-browser, and `true` otherwise. * If set to `false`, it will skip the local file check and try to load the model from the remote host. * @property {string} localModelPath Path to load local models from. Defaults to `/models/`. * @property {boolean} useFS Whether to use the file system to load files. By default, it is `true` if available. * @property {boolean} useBrowserCache Whether to use Cache API to cache models. By default, it is `true` if available. * @property {boolean} useFSCache Whether to use the file system to cache files. By default, it is `true` if available. * @property {string} cacheDir The directory to use for caching files with the file system. By default, it is `./.cache`. * @property {boolean} useCustomCache Whether to use a custom cache system (defined by `customCache`), defaults to `false`. * @property {Object} customCache The custom cache to use. Defaults to `null`. Note: this must be an object which * implements the `match` and `put` functions of the Web Cache API. For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache */ /** @type {TransformersEnvironment} */ export const env = { version: VERSION, /////////////////// Backends settings /////////////////// // NOTE: These will be populated later by the backends themselves. backends: { // onnxruntime-web/onnxruntime-node onnx: {}, }, /////////////////// Model settings /////////////////// allowRemoteModels: true, remoteHost: 'https://huggingface.co/', remotePathTemplate: '{model}/resolve/{revision}/', allowLocalModels: !(IS_BROWSER_ENV || IS_WEBWORKER_ENV), localModelPath: localModelPath, useFS: IS_FS_AVAILABLE, /////////////////// Cache settings /////////////////// useBrowserCache: IS_WEB_CACHE_AVAILABLE, useFSCache: IS_FS_AVAILABLE, cacheDir: DEFAULT_CACHE_DIR, useCustomCache: false, customCache: null, ////////////////////////////////////////////////////// } /** * @param {Object} obj * @private */ function isEmpty(obj) { return Object.keys(obj).length === 0; }
transformers.js/src/env.js/0
{ "file_path": "transformers.js/src/env.js", "repo_id": "transformers.js", "token_count": 2090 }
import { Processor } from "../../base/processing_utils.js"; import { AutoImageProcessor } from "../auto/image_processing_auto.js"; import { AutoTokenizer } from "../../tokenizers.js"; import { mergeArrays } from "../../utils/core.js"; import { Tensor } from "../../utils/tensor.js"; import { RawImage } from "../../utils/image.js"; export class VLChatProcessor extends Processor { static image_processor_class = AutoImageProcessor static tokenizer_class = AutoTokenizer static uses_processor_config = true; constructor(config, components) { super(config, components); this.image_tag = this.config.image_tag; this.image_start_tag = this.config.image_start_tag; this.image_end_tag = this.config.image_end_tag; this.num_image_tokens = this.config.num_image_tokens; } /** * @typedef {Object} MultimodalMessageProperties Additional properties for multimodal messages. * @property {(RawImage | string | URL)[]} [images] The images in the message. * @typedef {(import('../../tokenizers.js').Message & MultimodalMessageProperties)[]} MultimodalConversation The conversation possibly containing multimodal inputs. */ /** * @typedef {Object} VLCChatProcessorResult The processed input. * @property {Tensor} input_ids The input IDs. * @property {Tensor} attention_mask The attention mask. * @property {Tensor} images_seq_mask The image sequence mask. * @property {Tensor} images_emb_mask The image embedding mask. */ /** * @param {MultimodalConversation} conversation The chat messages to process. * @param {Object} options Additional options for processing. * @param {RawImage|RawImage[]} [options.images] The images to process, if not set in the conversation. * @param {string} [options.chat_template="default"] The chat template to use. * @returns {Promise<VLCChatProcessorResult | VLCChatProcessorResult & import('../../base/image_processors_utils.js').ImageProcessorResult>} The processed input. */ async _call(conversation, { images = null, chat_template = "default", }={}) { if (!images) { images = await Promise.all( conversation .filter((msg) => msg.images) .flatMap((msg) => msg.images) .map((img) => RawImage.read(img)) ); } else if (!Array.isArray(images)) { images = [images]; } const tokenizer = this.tokenizer; const result = tokenizer.apply_chat_template(conversation, { tokenize: false, add_generation_prompt: true, chat_template, }); const encode = (text) => tokenizer.encode(text, { add_special_tokens: false }); const parts = (/** @type {string} */(result)) .split(this.image_tag); const num_images = parts.length - 1; if (images.length !== num_images) { throw new Error(`Number of images provided (${images.length}) does not match number of "${this.image_tag}" image tags (${num_images})`); } const [ image_placeholder_tag_id, image_start_tag_id, image_end_tag_id, ] = tokenizer.model.convert_tokens_to_ids([ this.image_tag, this.image_start_tag, this.image_end_tag, ]); let input_ids = encode(parts[0]); let images_seq_mask = new Array(input_ids.length).fill(false); for (let i = 1; i < parts.length; ++i) { const placeholder_image_tokens = new Array(this.num_image_tokens).fill(image_placeholder_tag_id); const tokens = encode(parts[i]); input_ids = mergeArrays( input_ids, [image_start_tag_id], placeholder_image_tokens, [image_end_tag_id], tokens, ); const image_mask = new Array(this.num_image_tokens).fill(true); images_seq_mask = mergeArrays( images_seq_mask, [false], image_mask, [false], new Array(tokens.length).fill(false), ); } const dims = [1, input_ids.length]; const final = { input_ids: new Tensor('int64', input_ids, dims), attention_mask: new Tensor('int64', new Array(input_ids.length).fill(1), dims), images_seq_mask: new Tensor('bool', images_seq_mask, dims), images_emb_mask: new Tensor( 'bool', new Array(num_images * this.num_image_tokens).fill(true), [1, num_images, this.num_image_tokens], ), } if (images && images.length > 0) { const image_inputs = await this.image_processor(images); // Set the batch_size dimension to 1 image_inputs.pixel_values.unsqueeze_(0); return { ...final, ...image_inputs }; } return final; } }
transformers.js/src/models/janus/processing_janus.js/0
{ "file_path": "transformers.js/src/models/janus/processing_janus.js", "repo_id": "transformers.js", "token_count": 2202 }
import { ImageProcessor, post_process_object_detection, } from "../../base/image_processors_utils.js"; export class OwlViTImageProcessor extends ImageProcessor { /** @type {typeof post_process_object_detection} */ post_process_object_detection(...args) { return post_process_object_detection(...args); } } export class OwlViTFeatureExtractor extends OwlViTImageProcessor { }
transformers.js/src/models/owlvit/image_processing_owlvit.js/0
{ "file_path": "transformers.js/src/models/owlvit/image_processing_owlvit.js", "repo_id": "transformers.js", "token_count": 141 }
import { ImageProcessor, post_process_semantic_segmentation, } from "../../base/image_processors_utils.js"; export class SegformerImageProcessor extends ImageProcessor { /** @type {typeof post_process_semantic_segmentation} */ post_process_semantic_segmentation(...args) { return post_process_semantic_segmentation(...args); } } export class SegformerFeatureExtractor extends SegformerImageProcessor { }
transformers.js/src/models/segformer/image_processing_segformer.js/0
{ "file_path": "transformers.js/src/models/segformer/image_processing_segformer.js", "repo_id": "transformers.js", "token_count": 145 }
import { ImageProcessor, post_process_object_detection, } from "../../base/image_processors_utils.js"; export class YolosImageProcessor extends ImageProcessor { /** @type {typeof post_process_object_detection} */ post_process_object_detection(...args) { return post_process_object_detection(...args); } } export class YolosFeatureExtractor extends YolosImageProcessor { }
transformers.js/src/models/yolos/image_processing_yolos.js/0
{ "file_path": "transformers.js/src/models/yolos/image_processing_yolos.js", "repo_id": "transformers.js", "token_count": 140 }
import { RawImage } from "../src/transformers.js"; const BASE_URL = "https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/"; const TEST_IMAGES = Object.freeze({ white_image: BASE_URL + "white-image.png", blue_image: BASE_URL + "blue-image.png", pattern_3x3: BASE_URL + "pattern_3x3.png", pattern_3x5: BASE_URL + "pattern_3x5.png", checkerboard_8x8: BASE_URL + "checkerboard_8x8.png", checkerboard_64x32: BASE_URL + "checkerboard_64x32.png", gradient_1280x640: BASE_URL + "gradient_1280x640.png", receipt: BASE_URL + "receipt.png", tiger: BASE_URL + "tiger.jpg", paper: BASE_URL + "nougat_paper.png", cats: BASE_URL + "cats.jpg", // grayscale image skateboard: "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/ml-web-games/skateboard.png", vitmatte_image: BASE_URL + "vitmatte_image.png", vitmatte_trimap: BASE_URL + "vitmatte_trimap.png", beetle: BASE_URL + "beetle.png", book_cover: BASE_URL + "book-cover.png", corgi: BASE_URL + "corgi.jpg", man_on_car: BASE_URL + "young-man-standing-and-leaning-on-car.jpg", }); const TEST_AUDIOS = { mlk: BASE_URL + "mlk.npy", }; /** @type {Map<string, RawImage>} */ const IMAGE_CACHE = new Map(); const load_image = async (url) => { const cached = IMAGE_CACHE.get(url); if (cached) { return cached; } const image = await RawImage.fromURL(url); IMAGE_CACHE.set(url, image); return image; }; /** @type {Map<string, any>} */ const AUDIO_CACHE = new Map(); const load_audio = async (url) => { const cached = AUDIO_CACHE.get(url); if (cached) { return cached; } const buffer = await (await fetch(url)).arrayBuffer(); const audio = Float32Array.from(new Float64Array(buffer)); AUDIO_CACHE.set(url, audio); return audio; }; /** * Load a cached image. * @param {keyof typeof TEST_IMAGES} name The name of the image to load. * @returns {Promise<RawImage>} The loaded image. */ export const load_cached_image = (name) => load_image(TEST_IMAGES[name]); /** * Load a cached audio. * @param {keyof typeof TEST_AUDIOS} name The name of the audio to load. * @returns {Promise<Float32Array>} The loaded audio. */ export const load_cached_audio = (name) => load_audio(TEST_AUDIOS[name]);
transformers.js/tests/asset_cache.js/0
{ "file_path": "transformers.js/tests/asset_cache.js", "repo_id": "transformers.js", "token_count": 880 }
import { AutoFeatureExtractor, ClapFeatureExtractor } from "../../../src/transformers.js"; import { load_cached_audio } from "../../asset_cache.js"; import { MAX_FEATURE_EXTRACTOR_LOAD_TIME, MAX_TEST_EXECUTION_TIME } from "../../init.js"; export default () => { // ClapFeatureExtractor describe("ClapFeatureExtractor", () => { const model_id = "Xenova/clap-htsat-unfused"; /** @type {ClapFeatureExtractor} */ let feature_extractor; beforeAll(async () => { feature_extractor = await AutoFeatureExtractor.from_pretrained(model_id); }, MAX_FEATURE_EXTRACTOR_LOAD_TIME); it( "truncation", async () => { const audio = await load_cached_audio("mlk"); // Since truncation uses a random strategy, we override // Math.random to ensure that the test is deterministic const originalRandom = Math.random; Math.random = () => 0.5; let long_audio = new Float32Array(500000); long_audio.set(audio); long_audio.set(audio, long_audio.length - audio.length); const { input_features } = await feature_extractor(long_audio); const { dims, data } = input_features; expect(dims).toEqual([1, 1, 1001, 64]); expect(input_features.mean().item()).toBeCloseTo(-37.94569396972656); expect(data[0]).toBeCloseTo(-53.32647705078125); expect(data[1]).toBeCloseTo(-47.76755142211914); expect(data[65]).toBeCloseTo(-36.32261276245117); expect(data[1002]).toBeCloseTo(-28.0314884185791); expect(data[10000]).toBeCloseTo(-21.905902862548828); expect(data[60000]).toBeCloseTo(-14.877863883972168); expect(data[64062]).toBeCloseTo(-37.9784049987793); expect(data[64063]).toBeCloseTo(-37.73963928222656); // Reset Math.random Math.random = originalRandom; }, MAX_TEST_EXECUTION_TIME, ); it( "padding", async () => { const audio = await load_cached_audio("mlk"); const { input_features } = await feature_extractor(audio); const { data, dims } = input_features; expect(dims).toEqual([1, 1, 1001, 64]); expect(input_features.mean().item()).toBeCloseTo(-34.99049377441406); expect(data[0]).toBeCloseTo(-21.32573890686035); expect(data[1]).toBeCloseTo(-26.168411254882812); expect(data[65]).toBeCloseTo(-29.716018676757812); expect(data[1002]).toBeCloseTo(-32.16273498535156); expect(data[10000]).toBeCloseTo(-19.9283390045166); // padded values expect(data[60000]).toBeCloseTo(-100.0); expect(data[64062]).toBeCloseTo(-100.0); expect(data[64063]).toBeCloseTo(-100.0); }, MAX_TEST_EXECUTION_TIME, ); }); };
transformers.js/tests/models/clap/test_feature_extraction_clap.js/0
{ "file_path": "transformers.js/tests/models/clap/test_feature_extraction_clap.js", "repo_id": "transformers.js", "token_count": 1203 }
import { Florence2Processor, Florence2ForConditionalGeneration, RawImage, full } from "../../../src/transformers.js"; import { MAX_MODEL_LOAD_TIME, MAX_TEST_EXECUTION_TIME, MAX_MODEL_DISPOSE_TIME, DEFAULT_MODEL_OPTIONS } from "../../init.js"; export default () => { const texts = ["Describe with a paragraph what is shown in the image.", "Locate the objects with category name in the image."]; // Empty white image const dims = [224, 224, 3]; const image = new RawImage(new Uint8ClampedArray(dims[0] * dims[1] * dims[2]).fill(255), ...dims); describe("Florence2ForConditionalGeneration", () => { const model_id = "Xenova/tiny-random-Florence2ForConditionalGeneration"; /** @type {Florence2ForConditionalGeneration} */ let model; /** @type {Florence2Processor} */ let processor; beforeAll(async () => { model = await Florence2ForConditionalGeneration.from_pretrained(model_id, DEFAULT_MODEL_OPTIONS); processor = await Florence2Processor.from_pretrained(model_id); }, MAX_MODEL_LOAD_TIME); it( "forward", async () => { const inputs = await processor(image, texts[0]); const { logits } = await model({ ...inputs, decoder_input_ids: full([1, 1], 2n), }); expect(logits.dims).toEqual([1, 1, 51289]); }, MAX_TEST_EXECUTION_TIME, ); it( "batch_size=1", async () => { { const text_inputs = processor.tokenizer(texts[0]); const generate_ids = await model.generate({ ...text_inputs, max_new_tokens: 10 }); expect(generate_ids.tolist()).toEqual([[2n, 0n, 0n, 0n, 1n, 0n, 0n, 2n]]); } { const inputs = await processor(image, texts[0]); const generate_ids = await model.generate({ ...inputs, max_new_tokens: 10 }); expect(generate_ids.tolist()).toEqual([[2n, 0n, 48n, 48n, 48n, 48n, 48n, 48n, 48n, 48n, 2n]]); } }, MAX_TEST_EXECUTION_TIME, ); it( "batch_size>1", async () => { { const text_inputs = processor.tokenizer(texts, { padding: true }); const generate_ids = await model.generate({ ...text_inputs, max_new_tokens: 10 }); expect(generate_ids.tolist()).toEqual([ [2n, 0n, 0n, 0n, 1n, 0n, 0n, 2n], [2n, 0n, 0n, 0n, 1n, 0n, 0n, 2n], ]); } { const inputs = await processor([image, image], texts, { padding: true }); const generate_ids = await model.generate({ ...inputs, max_new_tokens: 10 }); expect(generate_ids.tolist()).toEqual([ [2n, 0n, 48n, 48n, 48n, 48n, 48n, 48n, 48n, 48n, 2n], [2n, 0n, 48n, 48n, 48n, 48n, 48n, 48n, 48n, 48n, 2n], ]); } }, MAX_TEST_EXECUTION_TIME, ); afterAll(async () => { await model?.dispose(); }, MAX_MODEL_DISPOSE_TIME); }); };
transformers.js/tests/models/florence2/test_modeling_florence2.js/0
{ "file_path": "transformers.js/tests/models/florence2/test_modeling_florence2.js", "repo_id": "transformers.js", "token_count": 1366 }
import { PreTrainedTokenizer, HeliumForCausalLM } from "../../../src/transformers.js"; import { MAX_MODEL_LOAD_TIME, MAX_TEST_EXECUTION_TIME, MAX_MODEL_DISPOSE_TIME, DEFAULT_MODEL_OPTIONS } from "../../init.js"; export default () => { describe("HeliumForCausalLM", () => { const model_id = "hf-internal-testing/tiny-random-HeliumForCausalLM"; /** @type {HeliumForCausalLM} */ let model; /** @type {PreTrainedTokenizer} */ let tokenizer; beforeAll(async () => { model = await HeliumForCausalLM.from_pretrained(model_id, DEFAULT_MODEL_OPTIONS); tokenizer = await PreTrainedTokenizer.from_pretrained(model_id); tokenizer.padding_side = "left"; }, MAX_MODEL_LOAD_TIME); it( "batch_size=1", async () => { const inputs = tokenizer("hello"); const outputs = await model.generate({ ...inputs, max_length: 10, }); expect(outputs.tolist()).toEqual([[1n, 456n, 5660n, 1700n, 1486n, 37744n, 35669n, 39396n, 12024n, 32253n]]); }, MAX_TEST_EXECUTION_TIME, ); it( "batch_size>1", async () => { const inputs = tokenizer(["hello", "hello world"], { padding: true }); const outputs = await model.generate({ ...inputs, max_length: 10, }); expect(outputs.tolist()).toEqual([ [3n, 1n, 456n, 5660n, 1700n, 1486n, 37744n, 35669n, 39396n, 12024n], [1n, 456n, 5660n, 998n, 6136n, 2080n, 172n, 8843n, 40579n, 23953n], ]); }, MAX_TEST_EXECUTION_TIME, ); afterAll(async () => { await model?.dispose(); }, MAX_MODEL_DISPOSE_TIME); }); };
transformers.js/tests/models/helium/test_modeling_helium.js/0
{ "file_path": "transformers.js/tests/models/helium/test_modeling_helium.js", "repo_id": "transformers.js", "token_count": 783 }
import { AutoFeatureExtractor, MoonshineFeatureExtractor } from "../../../src/transformers.js"; import { load_cached_audio } from "../../asset_cache.js"; import { MAX_FEATURE_EXTRACTOR_LOAD_TIME, MAX_TEST_EXECUTION_TIME } from "../../init.js"; export default () => { // MoonshineFeatureExtractor describe("MoonshineFeatureExtractor", () => { const model_id = "onnx-community/moonshine-tiny-ONNX"; /** @type {MoonshineFeatureExtractor} */ let feature_extractor; beforeAll(async () => { feature_extractor = await AutoFeatureExtractor.from_pretrained(model_id); }, MAX_FEATURE_EXTRACTOR_LOAD_TIME); it( "default", async () => { const audio = await load_cached_audio("mlk"); const { input_values } = await feature_extractor(audio); expect(input_values.dims).toEqual([1, 208000]); expect(input_values.mean().item()).toBeCloseTo(-1.5654930507480458e-7, 6); expect(input_values.data[0]).toBeCloseTo(0.0067138671875, 6); expect(input_values.data.at(-1)).toBeCloseTo(-0.013427734375, 6); }, MAX_TEST_EXECUTION_TIME, ); }); };
transformers.js/tests/models/moonshine/test_feature_extraction_moonshine.js/0
{ "file_path": "transformers.js/tests/models/moonshine/test_feature_extraction_moonshine.js", "repo_id": "transformers.js", "token_count": 465 }
import { AutoProcessor, Phi3VProcessor } from "../../../src/transformers.js"; import { load_cached_image } from "../../asset_cache.js"; import { MAX_PROCESSOR_LOAD_TIME, MAX_TEST_EXECUTION_TIME } from "../../init.js"; export default () => { const model_id = "onnx-community/Phi-3.5-vision-instruct"; describe("Phi3VProcessor", () => { /** @type {Phi3VProcessor} */ let processor; let images = {}; beforeAll(async () => { processor = await AutoProcessor.from_pretrained(model_id, { // Use legacy to match python version legacy: true, }); images = { white_image: await load_cached_image("white_image"), }; }, MAX_PROCESSOR_LOAD_TIME); const create_prompt = (text, images = []) => { const placeholder = images.map((_, i) => `<|image_${i + 1}|>\n`).join(""); const messages = [{ role: "user", content: placeholder + text }]; const prompt = processor.tokenizer.apply_chat_template(messages, { tokenize: false, add_generation_prompt: true }); return prompt; }; it( "Text-only", async () => { const prompt = create_prompt("Hi there."); const { input_ids, pixel_values } = await processor(prompt); expect(input_ids.dims).toEqual([1, 11]); expect(pixel_values).toBeUndefined(); }, MAX_TEST_EXECUTION_TIME, ); it( "Single image & text", async () => { const imgs = [images.white_image]; const prompt = create_prompt("Describe this image.", imgs); const { input_ids, attention_mask, pixel_values, image_sizes } = await processor(prompt, imgs); expect(input_ids.dims).toEqual([1, /* 773 */ 770]); expect(attention_mask.dims).toEqual(input_ids.dims); expect(pixel_values.dims).toEqual([1, 5, 3, 336, 336]); expect(image_sizes.tolist()).toEqual([[672n, 672n]]); }, MAX_TEST_EXECUTION_TIME, ); it( "Single image (num_crops=16) & text", async () => { const imgs = [images.white_image]; const prompt = create_prompt("Describe this image.", imgs); const { input_ids, attention_mask, pixel_values, image_sizes } = await processor(prompt, imgs, { num_crops: 16 }); expect(input_ids.dims).toEqual([1, /* 2525 */ 2522]); expect(attention_mask.dims).toEqual(input_ids.dims); expect(pixel_values.dims).toEqual([1, 17, 3, 336, 336]); expect(image_sizes.tolist()).toEqual([[1344n, 1344n]]); }, MAX_TEST_EXECUTION_TIME, ); it( "Multiple images & text", async () => { const imgs = [images.white_image, images.white_image]; const prompt = create_prompt("Describe these images.", imgs); const { input_ids, attention_mask, pixel_values, image_sizes } = await processor(prompt, imgs); expect(input_ids.dims).toEqual([1, /* 1533 */ 1527]); expect(attention_mask.dims).toEqual(input_ids.dims); expect(pixel_values.dims).toEqual([2, 5, 3, 336, 336]); expect(image_sizes.tolist()).toEqual([ [672n, 672n], [672n, 672n], ]); }, MAX_TEST_EXECUTION_TIME, ); }); };
transformers.js/tests/models/phi3_v/test_processor_phi3_v.js/0
{ "file_path": "transformers.js/tests/models/phi3_v/test_processor_phi3_v.js", "repo_id": "transformers.js", "token_count": 1404 }
import { AutoImageProcessor, VitMatteImageProcessor } from "../../../src/transformers.js"; import { load_cached_image } from "../../asset_cache.js"; import { MAX_PROCESSOR_LOAD_TIME, MAX_TEST_EXECUTION_TIME } from "../../init.js"; export default () => { // VitMatteImageProcessor // - tests custom overrides // - tests multiple inputs // - tests `size_divisibility` and no size (size_divisibility=32) // - tests do_pad and `size_divisibility` describe("VitMatteImageProcessor", () => { const model_id = "Xenova/vitmatte-small-distinctions-646"; /** @type {VitMatteImageProcessor} */ let processor; beforeAll(async () => { processor = await AutoImageProcessor.from_pretrained(model_id); }, MAX_PROCESSOR_LOAD_TIME); it( "w/o resize", async () => { const image = await load_cached_image("vitmatte_image"); const image2 = await load_cached_image("vitmatte_trimap"); const { pixel_values, original_sizes, reshaped_input_sizes } = await processor(image, image2); const { data, dims } = pixel_values; expect(dims).toEqual([1, 4, 640, 960]); expect(pixel_values.mean().item()).toBeCloseTo(-0.4028555154800415); expect(data[0]).toBeCloseTo(-0.9921568632125854); expect(data[1]).toBeCloseTo(-0.9921568632125854); expect(data[5]).toBeCloseTo(-1.0); expect(data[640]).toBeCloseTo(-0.6784313917160034); expect(data[641]).toBeCloseTo(-0.6705882549285889); expect(data[640 * 960]).toBeCloseTo(-1.0); expect(data[640 * 960 + 1]).toBeCloseTo(-1.0); expect(data.at(-1)).toBeCloseTo(0.0); expect(original_sizes).toEqual([[640, 960]]); expect(reshaped_input_sizes).toEqual([[640, 960]]); }, MAX_TEST_EXECUTION_TIME, ); it( "w/ resize", async () => { const image = await load_cached_image("pattern_3x5"); const image2 = await load_cached_image("pattern_3x5"); const { pixel_values, original_sizes, reshaped_input_sizes } = await processor(image, image2); const { data, dims } = pixel_values; expect(dims).toEqual([1, 4, 32, 32]); expect(pixel_values.mean().item()).toBeCloseTo(-0.00867417361587286); expect(data[0]).toBeCloseTo(-0.9921568632125854); expect(data[1]).toBeCloseTo(-0.9686274528503418); expect(data[5]).toBeCloseTo(0.0); expect(data[32]).toBeCloseTo(-0.9215686321258545); expect(data[33]).toBeCloseTo(-0.8980392217636108); expect(data.at(-1)).toBeCloseTo(0.0); expect(original_sizes).toEqual([[5, 3]]); expect(reshaped_input_sizes).toEqual([[5, 3]]); }, MAX_TEST_EXECUTION_TIME, ); }); };
transformers.js/tests/models/vitmatte/test_image_processing_vitmatte.js/0
{ "file_path": "transformers.js/tests/models/vitmatte/test_image_processing_vitmatte.js", "repo_id": "transformers.js", "token_count": 1213 }
import { pipeline, FillMaskPipeline } from "../../src/transformers.js"; import { MAX_MODEL_LOAD_TIME, MAX_TEST_EXECUTION_TIME, MAX_MODEL_DISPOSE_TIME, DEFAULT_MODEL_OPTIONS } from "../init.js"; const PIPELINE_ID = "fill-mask"; export default () => { describe("Fill Mask", () => { describe("Standard", () => { const model_id = "hf-internal-testing/tiny-random-BertForMaskedLM"; /** @type {FillMaskPipeline} */ let pipe; beforeAll(async () => { pipe = await pipeline(PIPELINE_ID, model_id, DEFAULT_MODEL_OPTIONS); }, MAX_MODEL_LOAD_TIME); it("should be an instance of FillMaskPipeline", () => { expect(pipe).toBeInstanceOf(FillMaskPipeline); }); describe("batch_size=1", () => { it( "default (top_k=5)", async () => { const output = await pipe("a [MASK] c"); const target = [ { score: 0.0013377574505284429, token: 854, token_str: "##ο", sequence: "aο c" }, { score: 0.001248967950232327, token: 962, token_str: "##ち", sequence: "aち c" }, { score: 0.0012304208939895034, token: 933, token_str: "##ع", sequence: "aع c" }, { score: 0.0012301815440878272, token: 313, token_str: "ფ", sequence: "a ფ c" }, { score: 0.001222139224410057, token: 624, token_str: "未", sequence: "a 未 c" }, ]; expect(output).toBeCloseToNested(target, 5); }, MAX_TEST_EXECUTION_TIME, ); it( "custom (top_k=2)", async () => { const output = await pipe("a [MASK] c", { top_k: 2 }); const target = [ { score: 0.0013377574505284429, token: 854, token_str: "##ο", sequence: "aο c" }, { score: 0.001248967950232327, token: 962, token_str: "##ち", sequence: "aち c" }, ]; expect(output).toBeCloseToNested(target, 5); }, MAX_TEST_EXECUTION_TIME, ); }); describe("batch_size>1", () => { it( "default (top_k=5)", async () => { const output = await pipe(["a [MASK] c", "a b [MASK] c"]); const target = [ [ { score: 0.0013377574505284429, token: 854, token_str: "##ο", sequence: "aο c" }, { score: 0.001248967950232327, token: 962, token_str: "##ち", sequence: "aち c" }, { score: 0.0012304208939895034, token: 933, token_str: "##ع", sequence: "aع c" }, { score: 0.0012301815440878272, token: 313, token_str: "ფ", sequence: "a ფ c" }, { score: 0.001222139224410057, token: 624, token_str: "未", sequence: "a 未 c" }, ], [ { score: 0.0013287801994010806, token: 962, token_str: "##ち", sequence: "a bち c" }, { score: 0.0012486606137827039, token: 823, token_str: "##ن", sequence: "a bن c" }, { score: 0.0012320734094828367, token: 1032, token_str: "##ც", sequence: "a bც c" }, { score: 0.0012295148335397243, token: 854, token_str: "##ο", sequence: "a bο c" }, { score: 0.0012277684872969985, token: 624, token_str: "未", sequence: "a b 未 c" }, ], ]; expect(output).toBeCloseToNested(target, 5); }, MAX_TEST_EXECUTION_TIME, ); it( "custom (top_k=2)", async () => { const output = await pipe(["a [MASK] c", "a b [MASK] c"], { top_k: 2 }); const target = [ [ { score: 0.0013377574505284429, token: 854, token_str: "##ο", sequence: "aο c" }, { score: 0.001248967950232327, token: 962, token_str: "##ち", sequence: "aち c" }, ], [ { score: 0.0013287801994010806, token: 962, token_str: "##ち", sequence: "a bち c" }, { score: 0.0012486606137827039, token: 823, token_str: "##ن", sequence: "a bن c" }, ], ]; expect(output).toBeCloseToNested(target, 5); }, MAX_TEST_EXECUTION_TIME, ); }); afterAll(async () => { await pipe.dispose(); }, MAX_MODEL_DISPOSE_TIME); }); describe("Custom tokenizer", () => { const model_id = "hf-internal-testing/tiny-random-ModernBertForMaskedLM"; /** @type {FillMaskPipeline} */ let pipe; beforeAll(async () => { pipe = await pipeline(PIPELINE_ID, model_id, DEFAULT_MODEL_OPTIONS); }, MAX_MODEL_LOAD_TIME); it("should be an instance of FillMaskPipeline", () => { expect(pipe).toBeInstanceOf(FillMaskPipeline); }); describe("batch_size=1", () => { it( "default (top_k=5)", async () => { const output = await pipe("The capital of France is [MASK]."); const target = [ { score: 0.2106737643480301, sequence: "The capital of France isả.", token: 35165, token_str: "ả" }, { score: 0.18418768048286438, sequence: "The capital of France isDispatch.", token: 48010, token_str: "Dispatch" }, { score: 0.16561225056648254, sequence: "The capital of France is Ther.", token: 20763, token_str: " Ther" }, { score: 0.07070659101009369, sequence: "The capital of France isschild.", token: 50040, token_str: "schild" }, { score: 0.029540402814745903, sequence: "The capital of France isbles.", token: 9143, token_str: "bles" }, ]; expect(output).toBeCloseToNested(target, 5); }, MAX_TEST_EXECUTION_TIME, ); }); describe("batch_size>1", () => { it( "default (top_k=5)", async () => { const output = await pipe(["a [MASK] c", "a b [MASK] c"]); const target = [ [ { score: 0.06699250638484955, sequence: "a oocytes c", token: 36805, token_str: " oocytes" }, { score: 0.05928678810596466, sequence: "ancia c", token: 19003, token_str: "ncia" }, { score: 0.057058464735746384, sequence: "aả c", token: 35165, token_str: "ả" }, { score: 0.04978331923484802, sequence: "amq c", token: 37365, token_str: "mq" }, { score: 0.04839889705181122, sequence: "a1371 c", token: 26088, token_str: "1371" }, ], [ { score: 0.06364646553993225, sequence: "a b oocytes c", token: 36805, token_str: " oocytes" }, { score: 0.03993292525410652, sequence: "a bectin c", token: 41105, token_str: "ectin" }, { score: 0.03932870551943779, sequence: "a bả c", token: 35165, token_str: "ả" }, { score: 0.037771403789520264, sequence: "a boplastic c", token: 21945, token_str: "oplastic" }, { score: 0.03748754784464836, sequence: "a b Ther c", token: 20763, token_str: " Ther" }, ], ]; expect(output).toBeCloseToNested(target, 5); }, MAX_TEST_EXECUTION_TIME, ); }); afterAll(async () => { await pipe.dispose(); }, MAX_MODEL_DISPOSE_TIME); }); }); };
transformers.js/tests/pipelines/test_pipelines_fill_mask.js/0
{ "file_path": "transformers.js/tests/pipelines/test_pipelines_fill_mask.js", "repo_id": "transformers.js", "token_count": 3763 }
import { pipeline, ZeroShotAudioClassificationPipeline } from "../../src/transformers.js"; import { MAX_MODEL_LOAD_TIME, MAX_TEST_EXECUTION_TIME, MAX_MODEL_DISPOSE_TIME, DEFAULT_MODEL_OPTIONS } from "../init.js"; import { load_cached_audio } from "../asset_cache.js"; const PIPELINE_ID = "zero-shot-audio-classification"; export default () => { describe("Zero-shot Audio Classification", () => { const model_id = "hf-internal-testing/tiny-clap-htsat-unfused"; const labels = ["cat", "dog"]; const hypothesis_template = "sound of a {}"; /** @type {ZeroShotAudioClassificationPipeline} */ let pipe; let audio; beforeAll(async () => { pipe = await pipeline(PIPELINE_ID, model_id, DEFAULT_MODEL_OPTIONS); audio = await load_cached_audio("mlk"); }, MAX_MODEL_LOAD_TIME); it("should be an instance of ZeroShotAudioClassificationPipeline", () => { expect(pipe).toBeInstanceOf(ZeroShotAudioClassificationPipeline); }); describe("batch_size=1", () => { it( "default", async () => { const output = await pipe(audio, labels); const target = [ { score: 0.4990939795970917, label: "cat" }, { score: 0.5009059906005859, label: "dog" }, ]; expect(output).toBeCloseToNested(target, 5); }, MAX_TEST_EXECUTION_TIME, ); it( "custom (w/ hypothesis_template)", async () => { const output = await pipe(audio, labels, { hypothesis_template }); const target = [ { score: 0.4987950325012207, label: "cat" }, { score: 0.5012049674987793, label: "dog" }, ]; expect(output).toBeCloseToNested(target, 5); }, MAX_TEST_EXECUTION_TIME, ); }); afterAll(async () => { await pipe.dispose(); }, MAX_MODEL_DISPOSE_TIME); }); };
transformers.js/tests/pipelines/test_pipelines_zero_shot_audio_classification.js/0
{ "file_path": "transformers.js/tests/pipelines/test_pipelines_zero_shot_audio_classification.js", "repo_id": "transformers.js", "token_count": 827 }
import TerserPlugin from "terser-webpack-plugin"; import { fileURLToPath } from "url"; import path from "path"; import fs from "fs"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); /** * Plugin to post-process build files. Required to solve certain issues with ESM module output. * See https://github.com/webpack/webpack/issues/17121 for more information. * * @see https://webpack.js.org/contribute/writing-a-plugin/ */ class PostBuildPlugin { apply(compiler) { compiler.hooks.done.tap('PostBuildPlugin', () => { const dist = path.join(__dirname, 'dist'); const ORT_JSEP_FILE = 'ort-wasm-simd-threaded.jsep.mjs'; const ORT_BUNDLE_FILE = 'ort.bundle.min.mjs'; // 1. Remove unnecessary files { const file = path.join(dist, ORT_BUNDLE_FILE); if (fs.existsSync(file)) fs.unlinkSync(file); } // 2. Copy unbundled JSEP file { const src = path.join(__dirname, 'node_modules/onnxruntime-web/dist', ORT_JSEP_FILE); const dest = path.join(dist, ORT_JSEP_FILE); fs.copyFileSync(src, dest); } // 3. Replace strings in certain files { const files = ['transformers.js', 'transformers.min.js']; for (const file of files) { const filePath = path.join(dist, file); let content = fs.readFileSync(filePath, 'utf8'); content = content.replace( // Replace all instances of `new URL("./", import.meta.url)` with `new URL(import.meta.url)`, // as it causes several issues with build tools and bundlers. // // See the following issues for more information: // - https://github.com/huggingface/transformers.js/issues/911 // - https://github.com/huggingface/transformers.js/issues/984 // - https://github.com/huggingface/transformers.js/issues/980 // - https://github.com/huggingface/transformers.js/issues/1021 // - https://github.com/huggingface/transformers.js/issues/1026 new RegExp('new URL\\(["\']\\.\\\/["\'],\\s*import\\.meta\\.url\\)', 'gm'), "new URL(import.meta.url)", ); fs.writeFileSync(filePath, content, 'utf8'); } } }); } } /** * Helper function to create webpack configurations. * @param {Object} options Options for creating a webpack target. * @param {string} options.name Name of output file. * @param {string} options.suffix Suffix of output file. * @param {string} options.type Type of library. * @param {string} options.ignoreModules The list of modules to ignore. * @param {string} options.externalModules The list of modules to set as external. * @param {Object[]} options.plugins List of plugins to use. * @returns {import('webpack').Configuration} One webpack target. */ function buildConfig({ name = "", suffix = ".js", type = "module", // 'module' | 'commonjs' ignoreModules = [], externalModules = [], plugins = [], } = {}) { const outputModule = type === "module"; const alias = Object.fromEntries( ignoreModules.map((module) => [module, false]), ); /** @type {import('webpack').Configuration} */ const config = { mode: "development", devtool: "source-map", entry: { [`transformers${name}`]: "./src/transformers.js", [`transformers${name}.min`]: "./src/transformers.js", }, output: { filename: `[name]${suffix}`, path: path.join(__dirname, "dist"), library: { type, }, assetModuleFilename: "[name][ext]", chunkFormat: "module", }, optimization: { minimize: true, minimizer: [ new TerserPlugin({ test: new RegExp(`\\.min\\${suffix}$`), // Do not bundle with comments. // See https://webpack.js.org/plugins/terser-webpack-plugin/#remove-comments for more information. terserOptions: { output: { comments: false, }, }, extractComments: false, }), ], }, experiments: { outputModule, }, resolve: { alias }, externals: externalModules, // Development server devServer: { static: { directory: __dirname, }, port: 8080, }, plugins, }; if (outputModule) { config.module = { parser: { javascript: { importMeta: false, }, }, }; } else { config.externalsType = "commonjs"; } return config; } // Do not bundle onnxruntime-web when packaging for Node.js. // Instead, we use the native library (onnxruntime-node). const NODE_IGNORE_MODULES = ["onnxruntime-web"]; // Do not bundle the following modules with webpack (mark as external) // NOTE: This is necessary for both type="module" and type="commonjs", // and will be ignored when building for web (only used for node/deno) const NODE_EXTERNAL_MODULES = [ "onnxruntime-node", "sharp", "fs", "path", "url", ]; // Web-only build const WEB_BUILD = buildConfig({ type: "module", plugins: [new PostBuildPlugin()], }); // Node-compatible builds const NODE_BUILDS = [ buildConfig({ suffix: ".mjs", type: "module", ignoreModules: NODE_IGNORE_MODULES, externalModules: NODE_EXTERNAL_MODULES, }), buildConfig({ suffix: ".cjs", type: "commonjs", ignoreModules: NODE_IGNORE_MODULES, externalModules: NODE_EXTERNAL_MODULES, }), ]; // When running with `webpack serve`, only build the web target. const BUILDS = process.env.WEBPACK_SERVE ? [WEB_BUILD] : [WEB_BUILD, ...NODE_BUILDS]; export default BUILDS;
transformers.js/webpack.config.js/0
{ "file_path": "transformers.js/webpack.config.js", "repo_id": "transformers.js", "token_count": 2312 }
{ "annotations": { "list": [ { "builtIn": 1, "datasource": { "type": "grafana", "uid": "-- Grafana --" }, "enable": true, "hide": true, "iconColor": "rgba(0, 211, 255, 1)", "name": "Annotations & Alerts", "type": "dashboard" } ] }, "editable": true, "fiscalYearStartMonth": 0, "graphTooltip": 0, "id": 1, "links": [ { "asDropdown": false, "icon": "external link", "includeVars": false, "keepTime": false, "tags": [], "targetBlank": false, "title": "Go to data", "tooltip": "Go to data", "type": "link", "url": "http://transformers-benchmarks.hf.co/d/fdz33iyzln9c0a/transformers-benchmarks?orgId=1&from=${StartTime}&to=${EndTime}" } ], "liveNow": true, "panels": [ { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "fieldConfig": { "defaults": { "color": { "mode": "thresholds" }, "custom": { "align": "left", "cellOptions": { "type": "auto" }, "inspect": false }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null }, { "color": "red", "value": 80 } ] } }, "overrides": [ { "matcher": { "id": "byName", "options": "gpu_name" }, "properties": [ { "id": "custom.width", "value": 202 } ] }, { "matcher": { "id": "byName", "options": "left" }, "properties": [ { "id": "custom.width", "value": 407 } ] }, { "matcher": { "id": "byName", "options": "commit_message" }, "properties": [ { "id": "custom.width", "value": 524 } ] }, { "matcher": { "id": "byName", "options": "commit_id" }, "properties": [ { "id": "custom.width", "value": 353 } ] }, { "matcher": { "id": "byName", "options": "model_id" }, "properties": [ { "id": "custom.width", "value": 216 } ] } ] }, "gridPos": { "h": 6, "w": 24, "x": 0, "y": 0 }, "id": 5, "options": { "cellHeight": "sm", "footer": { "countRows": false, "fields": "", "reducer": [ "sum" ], "show": false }, "showHeader": true, "sortBy": [] }, "pluginVersion": "11.2.2", "targets": [ { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "editorMode": "code", "format": "table", "rawQuery": true, "rawSql": "SELECT commit_id, commit_message, metadata->>'gpu_name' as gpu_name, metadata->>'model_id' as model_id, created_at AS date FROM benchmarks WHERE branch = '${branch}' AND metadata->>'gpu_name' = '${gpu_name}' ORDER BY benchmark_id DESC LIMIT ${last_n_commits};", "refId": "A", "sql": { "columns": [ { "parameters": [ { "name": "commit_id", "type": "functionParameter" } ], "type": "function" }, { "parameters": [ { "name": "gpu_name", "type": "functionParameter" } ], "type": "function" } ], "groupBy": [ { "property": { "type": "string" }, "type": "groupBy" } ], "limit": 50, "whereJsonTree": { "children1": [ { "id": "baaa8aaa-89ab-4cde-b012-31922f96de3f", "properties": { "field": "commit_id", "fieldSrc": "field", "operator": "equal", "value": [ "${commit}" ], "valueError": [ null ], "valueSrc": [ "value" ], "valueType": [ "text" ] }, "type": "rule" } ], "id": "bab88a98-0123-4456-b89a-b1922f7d4f11", "type": "group" }, "whereString": "commit_id = '${commit}'" }, "table": "benchmarks" } ], "transparent": true, "type": "table" }, { "collapsed": false, "gridPos": { "h": 1, "w": 24, "x": 0, "y": 6 }, "id": 13, "panels": [], "title": "Eager Forward Pass", "type": "row" }, { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "fieldConfig": { "defaults": { "color": { "mode": "continuous-YlBl" }, "custom": { "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", "axisPlacement": "auto", "fillOpacity": 80, "gradientMode": "scheme", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, "lineWidth": 0, "scaleDistribution": { "type": "linear" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null } ] }, "unit": "s" }, "overrides": [] }, "gridPos": { "h": 11, "w": 12, "x": 0, "y": 7 }, "id": 7, "options": { "barRadius": 0.05, "barWidth": 0.8, "fullHighlight": false, "groupWidth": 0.7, "legend": { "calcs": [], "displayMode": "list", "placement": "bottom", "showLegend": false }, "orientation": "auto", "showValue": "auto", "stacking": "none", "tooltip": { "mode": "single", "sort": "none" }, "xTickLabelRotation": 0, "xTickLabelSpacing": 0 }, "pluginVersion": "11.2.2", "targets": [ { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "editorMode": "code", "format": "table", "rawQuery": true, "rawSql": "SELECT CAST(m.measurements->'first_eager_forward_pass_time_secs' AS double precision) AS first_eager_forward_pass_time_secs, left(b.commit_id, 7), m.time FROM benchmarks as b JOIN model_measurements AS m ON b.benchmark_id = m.benchmark_id WHERE b.branch = '${branch}' AND b.metadata->>'gpu_name' = '${gpu_name}' ORDER BY b.benchmark_id DESC LIMIT ${last_n_commits};", "refId": "A", "sql": { "columns": [ { "parameters": [], "type": "function" } ], "groupBy": [ { "property": { "type": "string" }, "type": "groupBy" } ], "limit": 50 } } ], "title": "First eager forward pass", "transformations": [ { "id": "sortBy", "options": { "fields": {}, "sort": [ { "field": "time" } ] } } ], "transparent": true, "type": "barchart" }, { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "fieldConfig": { "defaults": { "color": { "mode": "continuous-YlBl" }, "custom": { "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", "axisPlacement": "auto", "fillOpacity": 80, "gradientMode": "scheme", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, "lineWidth": 0, "scaleDistribution": { "type": "linear" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null }, { "color": "red", "value": 80 } ] }, "unit": "s" }, "overrides": [] }, "gridPos": { "h": 11, "w": 12, "x": 12, "y": 7 }, "id": 9, "options": { "barRadius": 0.05, "barWidth": 0.8, "fullHighlight": false, "groupWidth": 0.7, "legend": { "calcs": [], "displayMode": "list", "placement": "bottom", "showLegend": false }, "orientation": "auto", "showValue": "auto", "stacking": "none", "tooltip": { "mode": "single", "sort": "none" }, "xTickLabelRotation": 0, "xTickLabelSpacing": 0 }, "targets": [ { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "editorMode": "code", "format": "table", "rawQuery": true, "rawSql": "SELECT CAST(m.measurements->'second_eager_forward_pass_time_secs' AS double precision) AS second_eager_forward_pass_time_secs, left(b.commit_id, 7), m.time FROM benchmarks as b JOIN model_measurements AS m ON b.benchmark_id = m.benchmark_id WHERE b.branch = '${branch}' AND b.metadata->>'gpu_name' = '${gpu_name}' ORDER BY b.benchmark_id DESC LIMIT ${last_n_commits};", "refId": "A", "sql": { "columns": [ { "parameters": [], "type": "function" } ], "groupBy": [ { "property": { "type": "string" }, "type": "groupBy" } ], "limit": 50 } } ], "title": "Second eager forward pass", "transformations": [ { "id": "sortBy", "options": { "fields": {}, "sort": [ { "field": "time" } ] } } ], "transparent": true, "type": "barchart" }, { "collapsed": false, "gridPos": { "h": 1, "w": 24, "x": 0, "y": 18 }, "id": 16, "panels": [], "title": "Time to next token", "type": "row" }, { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "fieldConfig": { "defaults": { "color": { "mode": "continuous-YlBl" }, "custom": { "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", "axisPlacement": "auto", "fillOpacity": 80, "gradientMode": "scheme", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, "lineWidth": 0, "scaleDistribution": { "type": "linear" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null } ] }, "unit": "s" }, "overrides": [] }, "gridPos": { "h": 11, "w": 12, "x": 0, "y": 19 }, "id": 17, "options": { "barRadius": 0.05, "barWidth": 0.8, "fullHighlight": false, "groupWidth": 0.7, "legend": { "calcs": [], "displayMode": "list", "placement": "bottom", "showLegend": false }, "orientation": "auto", "showValue": "always", "stacking": "none", "tooltip": { "mode": "single", "sort": "none" }, "xTickLabelRotation": 0, "xTickLabelSpacing": 0 }, "targets": [ { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "editorMode": "code", "format": "table", "rawQuery": true, "rawSql": "SELECT CAST(m.measurements->'time_to_first_token_secs' AS double precision) AS time_to_first_token_secs, left(b.commit_id, 7), m.time FROM benchmarks as b JOIN model_measurements AS m ON b.benchmark_id = m.benchmark_id WHERE b.branch = '${branch}' AND b.metadata->>'gpu_name' = '${gpu_name}' ORDER BY b.benchmark_id DESC LIMIT ${last_n_commits};", "refId": "A", "sql": { "columns": [ { "parameters": [], "type": "function" } ], "groupBy": [ { "property": { "type": "string" }, "type": "groupBy" } ], "limit": 50 } } ], "title": "Time to first token", "transformations": [ { "id": "sortBy", "options": { "fields": {}, "sort": [ { "field": "time" } ] } } ], "transparent": true, "type": "barchart" }, { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "fieldConfig": { "defaults": { "color": { "mode": "continuous-YlBl" }, "custom": { "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", "axisPlacement": "auto", "fillOpacity": 80, "gradientMode": "scheme", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, "lineWidth": 0, "scaleDistribution": { "type": "linear" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null } ] }, "unit": "s" }, "overrides": [] }, "gridPos": { "h": 11, "w": 12, "x": 12, "y": 19 }, "id": 18, "options": { "barRadius": 0.05, "barWidth": 0.8, "fullHighlight": false, "groupWidth": 0.7, "legend": { "calcs": [], "displayMode": "list", "placement": "bottom", "showLegend": false }, "orientation": "auto", "showValue": "always", "stacking": "none", "tooltip": { "mode": "single", "sort": "none" }, "xTickLabelRotation": 0, "xTickLabelSpacing": 0 }, "targets": [ { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "editorMode": "code", "format": "table", "rawQuery": true, "rawSql": "SELECT CAST(m.measurements->'time_to_second_token_secs' AS double precision) AS time_to_second_token_secs, left(b.commit_id, 7), m.time FROM benchmarks as b JOIN model_measurements AS m ON b.benchmark_id = m.benchmark_id WHERE b.branch = '${branch}' AND b.metadata->>'gpu_name' = '${gpu_name}' ORDER BY b.benchmark_id DESC LIMIT ${last_n_commits};", "refId": "A", "sql": { "columns": [ { "parameters": [], "type": "function" } ], "groupBy": [ { "property": { "type": "string" }, "type": "groupBy" } ], "limit": 50 } } ], "title": "Time to second token", "transformations": [ { "id": "sortBy", "options": { "fields": {}, "sort": [ { "field": "time" } ] } } ], "transparent": true, "type": "barchart" }, { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "fieldConfig": { "defaults": { "color": { "mode": "continuous-YlBl" }, "custom": { "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", "axisPlacement": "auto", "fillOpacity": 80, "gradientMode": "scheme", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, "lineWidth": 0, "scaleDistribution": { "type": "linear" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null } ] }, "unit": "s" }, "overrides": [] }, "gridPos": { "h": 11, "w": 12, "x": 0, "y": 30 }, "id": 19, "options": { "barRadius": 0.05, "barWidth": 0.8, "fullHighlight": false, "groupWidth": 0.7, "legend": { "calcs": [], "displayMode": "list", "placement": "bottom", "showLegend": false }, "orientation": "auto", "showValue": "always", "stacking": "none", "tooltip": { "mode": "single", "sort": "none" }, "xTickLabelRotation": 0, "xTickLabelSpacing": 0 }, "targets": [ { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "editorMode": "code", "format": "table", "rawQuery": true, "rawSql": "SELECT CAST(m.measurements->'time_to_third_token_secs' AS double precision) AS time_to_third_token_secs, left(b.commit_id, 7), m.time FROM benchmarks as b JOIN model_measurements AS m ON b.benchmark_id = m.benchmark_id WHERE b.branch = '${branch}' AND b.metadata->>'gpu_name' = '${gpu_name}' ORDER BY b.benchmark_id DESC LIMIT ${last_n_commits};", "refId": "A", "sql": { "columns": [ { "parameters": [], "type": "function" } ], "groupBy": [ { "property": { "type": "string" }, "type": "groupBy" } ], "limit": 50 } } ], "title": "Time to third token", "transformations": [ { "id": "sortBy", "options": { "fields": {}, "sort": [ { "field": "time" } ] } } ], "transparent": true, "type": "barchart" }, { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "fieldConfig": { "defaults": { "color": { "mode": "continuous-YlBl" }, "custom": { "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", "axisPlacement": "auto", "fillOpacity": 80, "gradientMode": "scheme", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, "lineWidth": 0, "scaleDistribution": { "type": "linear" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null } ] }, "unit": "s" }, "overrides": [] }, "gridPos": { "h": 11, "w": 12, "x": 12, "y": 30 }, "id": 20, "options": { "barRadius": 0.05, "barWidth": 0.8, "fullHighlight": false, "groupWidth": 0.7, "legend": { "calcs": [], "displayMode": "list", "placement": "bottom", "showLegend": false }, "orientation": "auto", "showValue": "always", "stacking": "none", "tooltip": { "mode": "single", "sort": "none" }, "xTickLabelRotation": 0, "xTickLabelSpacing": 0 }, "targets": [ { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "editorMode": "code", "format": "table", "rawQuery": true, "rawSql": "SELECT CAST(m.measurements->'time_to_next_token_mean_secs' AS double precision) AS time_to_next_token_mean_secs, left(b.commit_id, 7), m.time FROM benchmarks as b JOIN model_measurements AS m ON b.benchmark_id = m.benchmark_id WHERE b.branch = '${branch}' AND b.metadata->>'gpu_name' = '${gpu_name}' ORDER BY b.benchmark_id DESC LIMIT ${last_n_commits};", "refId": "A", "sql": { "columns": [ { "parameters": [], "type": "function" } ], "groupBy": [ { "property": { "type": "string" }, "type": "groupBy" } ], "limit": 50 } } ], "title": "Time to subsequent next tokens mean", "transformations": [ { "id": "sortBy", "options": { "fields": {}, "sort": [ { "field": "time" } ] } } ], "transparent": true, "type": "barchart" }, { "collapsed": false, "gridPos": { "h": 1, "w": 24, "x": 0, "y": 41 }, "id": 14, "panels": [], "title": "Compiled Generate", "type": "row" }, { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "fieldConfig": { "defaults": { "color": { "mode": "continuous-YlBl" }, "custom": { "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", "axisPlacement": "auto", "fillOpacity": 80, "gradientMode": "scheme", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, "lineWidth": 0, "scaleDistribution": { "type": "linear" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null } ] }, "unit": "s" }, "overrides": [] }, "gridPos": { "h": 11, "w": 12, "x": 0, "y": 42 }, "id": 8, "options": { "barRadius": 0.05, "barWidth": 0.8, "fullHighlight": false, "groupWidth": 0.7, "legend": { "calcs": [], "displayMode": "list", "placement": "bottom", "showLegend": false }, "orientation": "auto", "showValue": "always", "stacking": "none", "tooltip": { "mode": "single", "sort": "none" }, "xTickLabelRotation": 0, "xTickLabelSpacing": 0 }, "targets": [ { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "editorMode": "code", "format": "table", "rawQuery": true, "rawSql": "SELECT CAST(m.measurements->'first_compile_generate_time_secs' AS double precision) AS first_compile_generate_time_secs, left(b.commit_id, 7), m.time FROM benchmarks as b JOIN model_measurements AS m ON b.benchmark_id = m.benchmark_id WHERE b.branch = '${branch}' AND b.metadata->>'gpu_name' = '${gpu_name}' ORDER BY b.benchmark_id DESC LIMIT ${last_n_commits};", "refId": "A", "sql": { "columns": [ { "parameters": [], "type": "function" } ], "groupBy": [ { "property": { "type": "string" }, "type": "groupBy" } ], "limit": 50 } } ], "title": "First compile generate", "transformations": [ { "id": "sortBy", "options": { "fields": {}, "sort": [ { "field": "time" } ] } } ], "transparent": true, "type": "barchart" }, { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "fieldConfig": { "defaults": { "color": { "mode": "continuous-YlBl" }, "custom": { "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", "axisPlacement": "auto", "fillOpacity": 80, "gradientMode": "scheme", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, "lineWidth": 0, "scaleDistribution": { "type": "linear" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null } ] }, "unit": "s" }, "overrides": [] }, "gridPos": { "h": 11, "w": 12, "x": 12, "y": 42 }, "id": 10, "options": { "barRadius": 0.05, "barWidth": 0.8, "fullHighlight": false, "groupWidth": 0.7, "legend": { "calcs": [], "displayMode": "list", "placement": "bottom", "showLegend": false }, "orientation": "auto", "showValue": "auto", "stacking": "none", "tooltip": { "mode": "single", "sort": "none" }, "xTickLabelRotation": 0, "xTickLabelSpacing": 0 }, "targets": [ { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "editorMode": "code", "format": "table", "rawQuery": true, "rawSql": "SELECT CAST(m.measurements->'second_compile_generate_time_secs' AS double precision) AS second_compile_generate_time_secs, left(b.commit_id, 7), m.time FROM benchmarks as b JOIN model_measurements AS m ON b.benchmark_id = m.benchmark_id WHERE b.branch = '${branch}' AND b.metadata->>'gpu_name' = '${gpu_name}' ORDER BY b.benchmark_id DESC LIMIT ${last_n_commits};", "refId": "A", "sql": { "columns": [ { "parameters": [], "type": "function" } ], "groupBy": [ { "property": { "type": "string" }, "type": "groupBy" } ], "limit": 50 } } ], "title": "Second compile generate", "transformations": [ { "id": "sortBy", "options": { "fields": {}, "sort": [ { "field": "time" } ] } } ], "transparent": true, "type": "barchart" }, { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "fieldConfig": { "defaults": { "color": { "mode": "continuous-YlBl" }, "custom": { "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", "axisPlacement": "auto", "fillOpacity": 80, "gradientMode": "scheme", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, "lineWidth": 0, "scaleDistribution": { "type": "linear" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null } ] }, "unit": "s" }, "overrides": [] }, "gridPos": { "h": 11, "w": 12, "x": 0, "y": 53 }, "id": 11, "options": { "barRadius": 0.05, "barWidth": 0.8, "fullHighlight": false, "groupWidth": 0.7, "legend": { "calcs": [], "displayMode": "list", "placement": "bottom", "showLegend": false }, "orientation": "auto", "showValue": "auto", "stacking": "none", "tooltip": { "mode": "single", "sort": "none" }, "xTickLabelRotation": 0, "xTickLabelSpacing": 0 }, "targets": [ { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "editorMode": "code", "format": "table", "rawQuery": true, "rawSql": "SELECT CAST(m.measurements->'third_compile_generate_time_secs' AS double precision) AS third_compile_generate_time_secs, left(b.commit_id, 7), m.time FROM benchmarks as b JOIN model_measurements AS m ON b.benchmark_id = m.benchmark_id WHERE b.branch = '${branch}' AND b.metadata->>'gpu_name' = '${gpu_name}' ORDER BY b.benchmark_id DESC LIMIT ${last_n_commits};", "refId": "A", "sql": { "columns": [ { "parameters": [], "type": "function" } ], "groupBy": [ { "property": { "type": "string" }, "type": "groupBy" } ], "limit": 50 } } ], "title": "Third compile generate", "transformations": [ { "id": "sortBy", "options": { "fields": {}, "sort": [ { "field": "time" } ] } } ], "transparent": true, "type": "barchart" }, { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "fieldConfig": { "defaults": { "color": { "mode": "continuous-YlBl" }, "custom": { "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", "axisPlacement": "auto", "fillOpacity": 80, "gradientMode": "none", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, "lineWidth": 0, "scaleDistribution": { "type": "linear" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null } ] }, "unit": "s" }, "overrides": [] }, "gridPos": { "h": 11, "w": 12, "x": 12, "y": 53 }, "id": 12, "options": { "barRadius": 0.05, "barWidth": 0.8, "fullHighlight": false, "groupWidth": 0.7, "legend": { "calcs": [], "displayMode": "list", "placement": "bottom", "showLegend": false }, "orientation": "auto", "showValue": "auto", "stacking": "none", "tooltip": { "mode": "single", "sort": "none" }, "xTickLabelRotation": 0, "xTickLabelSpacing": 0 }, "targets": [ { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "editorMode": "code", "format": "table", "rawQuery": true, "rawSql": "SELECT CAST(m.measurements->'fourth_compile_generate_time_secs' AS double precision) AS fourth_compile_generate_time_secs, left(b.commit_id, 7), m.time FROM benchmarks as b JOIN model_measurements AS m ON b.benchmark_id = m.benchmark_id WHERE b.branch = '${branch}' AND b.metadata->>'gpu_name' = '${gpu_name}' ORDER BY b.benchmark_id DESC LIMIT ${last_n_commits};", "refId": "A", "sql": { "columns": [ { "parameters": [], "type": "function" } ], "groupBy": [ { "property": { "type": "string" }, "type": "groupBy" } ], "limit": 50 } } ], "title": "Fourth compile generate", "transformations": [ { "id": "sortBy", "options": { "fields": {}, "sort": [ { "field": "time" } ] } } ], "transparent": true, "type": "barchart" }, { "collapsed": true, "gridPos": { "h": 1, "w": 24, "x": 0, "y": 64 }, "id": 15, "panels": [ { "datasource": {}, "fieldConfig": { "defaults": { "color": { "mode": "palette-classic" }, "custom": { "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", "axisPlacement": "auto", "barAlignment": 0, "barWidthFactor": 0.6, "drawStyle": "line", "fillOpacity": 0, "gradientMode": "none", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, "insertNulls": 60000, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, "scaleDistribution": { "type": "linear" }, "showPoints": "auto", "spanNulls": false, "stacking": { "group": "A", "mode": "none" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green" }, { "color": "red", "value": 80 } ] }, "unit": "percent" }, "overrides": [] }, "gridPos": { "h": 9, "w": 12, "x": 0, "y": 65 }, "id": 1, "options": { "legend": { "calcs": [], "displayMode": "list", "placement": "bottom", "showLegend": true }, "tooltip": { "mode": "single", "sort": "none" } }, "targets": [ { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "editorMode": "code", "format": "table", "rawQuery": true, "rawSql": "SELECT\n d.cpu_util,\n d.time\nFROM\n benchmarks AS b\n JOIN device_measurements AS d ON b.benchmark_id = d.benchmark_id\nWHERE\n branch = '${branch}';", "refId": "A", "sql": { "columns": [ { "parameters": [ { "name": "cpu_util", "type": "functionParameter" } ], "type": "function" }, { "parameters": [ { "name": "mem_megabytes", "type": "functionParameter" } ], "type": "function" }, { "parameters": [ { "name": "gpu_util", "type": "functionParameter" } ], "type": "function" }, { "parameters": [ { "name": "gpu_mem_megabytes", "type": "functionParameter" } ], "type": "function" }, { "parameters": [ { "name": "\"time\"", "type": "functionParameter" } ], "type": "function" } ], "groupBy": [ { "property": { "type": "string" }, "type": "groupBy" } ], "limit": 50, "whereJsonTree": { "children1": [ { "id": "baa888b8-89ab-4cde-b012-31922f8671e9", "properties": { "field": "commit_id", "fieldSrc": "field", "operator": "equal", "value": [ "${commit}" ], "valueError": [ null ], "valueSrc": [ "value" ], "valueType": [ "text" ] }, "type": "rule" } ], "id": "bab88a98-0123-4456-b89a-b1922f7d4f11", "type": "group" }, "whereString": "commit_id = '${commit}'" }, "table": "measurements" } ], "title": "CPU Utilization", "transparent": true, "type": "timeseries" }, { "datasource": {}, "fieldConfig": { "defaults": { "color": { "mode": "palette-classic" }, "custom": { "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", "axisPlacement": "auto", "barAlignment": 0, "barWidthFactor": 0.6, "drawStyle": "line", "fillOpacity": 0, "gradientMode": "none", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, "insertNulls": 60000, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, "scaleDistribution": { "type": "linear" }, "showPoints": "auto", "spanNulls": false, "stacking": { "group": "A", "mode": "none" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green" }, { "color": "red", "value": 80 } ] }, "unit": "percent" }, "overrides": [] }, "gridPos": { "h": 9, "w": 12, "x": 12, "y": 65 }, "id": 4, "options": { "legend": { "calcs": [], "displayMode": "list", "placement": "bottom", "showLegend": true }, "tooltip": { "mode": "single", "sort": "none" } }, "targets": [ { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "editorMode": "code", "format": "table", "rawQuery": true, "rawSql": "SELECT\n b.commit_id,\n d.gpu_util,\n d.time\nFROM\n benchmarks AS b\n JOIN device_measurements AS d ON b.benchmark_id = d.benchmark_id\nWHERE\n branch = '${branch}';", "refId": "A", "sql": { "columns": [ { "parameters": [ { "name": "cpu_util", "type": "functionParameter" } ], "type": "function" }, { "parameters": [ { "name": "mem_megabytes", "type": "functionParameter" } ], "type": "function" }, { "parameters": [ { "name": "gpu_util", "type": "functionParameter" } ], "type": "function" }, { "parameters": [ { "name": "gpu_mem_megabytes", "type": "functionParameter" } ], "type": "function" }, { "parameters": [ { "name": "\"time\"", "type": "functionParameter" } ], "type": "function" } ], "groupBy": [ { "property": { "type": "string" }, "type": "groupBy" } ], "limit": 50, "whereJsonTree": { "children1": [ { "id": "baa888b8-89ab-4cde-b012-31922f8671e9", "properties": { "field": "commit_id", "fieldSrc": "field", "operator": "equal", "value": [ "${commit}" ], "valueError": [ null ], "valueSrc": [ "value" ], "valueType": [ "text" ] }, "type": "rule" } ], "id": "bab88a98-0123-4456-b89a-b1922f7d4f11", "type": "group" }, "whereString": "commit_id = '${commit}'" }, "table": "measurements" } ], "title": "GPU Utilization", "transparent": true, "type": "timeseries" }, { "datasource": {}, "fieldConfig": { "defaults": { "color": { "mode": "palette-classic" }, "custom": { "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", "axisPlacement": "auto", "barAlignment": 0, "barWidthFactor": 0.6, "drawStyle": "line", "fillOpacity": 0, "gradientMode": "none", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, "insertNulls": 60000, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, "scaleDistribution": { "type": "linear" }, "showPoints": "auto", "spanNulls": false, "stacking": { "group": "A", "mode": "none" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green" }, { "color": "red", "value": 80 } ] }, "unit": "decmbytes" }, "overrides": [] }, "gridPos": { "h": 9, "w": 12, "x": 0, "y": 74 }, "id": 2, "options": { "legend": { "calcs": [], "displayMode": "list", "placement": "bottom", "showLegend": true }, "tooltip": { "mode": "single", "sort": "none" } }, "targets": [ { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "editorMode": "code", "format": "table", "rawQuery": true, "rawSql": "SELECT d.mem_megabytes, d.time FROM benchmarks AS b JOIN device_measurements AS d ON b.benchmark_id = d.benchmark_id WHERE branch = '${branch}';", "refId": "A", "sql": { "columns": [ { "parameters": [ { "name": "cpu_util", "type": "functionParameter" } ], "type": "function" }, { "parameters": [ { "name": "mem_megabytes", "type": "functionParameter" } ], "type": "function" }, { "parameters": [ { "name": "gpu_util", "type": "functionParameter" } ], "type": "function" }, { "parameters": [ { "name": "gpu_mem_megabytes", "type": "functionParameter" } ], "type": "function" }, { "parameters": [ { "name": "\"time\"", "type": "functionParameter" } ], "type": "function" } ], "groupBy": [ { "property": { "type": "string" }, "type": "groupBy" } ], "limit": 50, "whereJsonTree": { "children1": [ { "id": "baa888b8-89ab-4cde-b012-31922f8671e9", "properties": { "field": "commit_id", "fieldSrc": "field", "operator": "equal", "value": [ "${commit}" ], "valueError": [ null ], "valueSrc": [ "value" ], "valueType": [ "text" ] }, "type": "rule" } ], "id": "bab88a98-0123-4456-b89a-b1922f7d4f11", "type": "group" }, "whereString": "commit_id = '${commit}'" }, "table": "measurements" } ], "title": "Memory usage", "transparent": true, "type": "timeseries" }, { "datasource": {}, "fieldConfig": { "defaults": { "color": { "mode": "palette-classic" }, "custom": { "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", "axisPlacement": "auto", "barAlignment": 0, "barWidthFactor": 0.6, "drawStyle": "line", "fillOpacity": 0, "gradientMode": "none", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, "insertNulls": 60000, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, "scaleDistribution": { "type": "linear" }, "showPoints": "auto", "spanNulls": false, "stacking": { "group": "A", "mode": "none" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green" }, { "color": "red", "value": 80 } ] }, "unit": "decmbytes" }, "overrides": [] }, "gridPos": { "h": 9, "w": 12, "x": 12, "y": 74 }, "id": 3, "options": { "legend": { "calcs": [], "displayMode": "list", "placement": "bottom", "showLegend": true }, "tooltip": { "mode": "single", "sort": "none" } }, "targets": [ { "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "editorMode": "code", "format": "table", "rawQuery": true, "rawSql": "SELECT\n d.gpu_mem_megabytes,\n d.time\nFROM\n benchmarks AS b\n JOIN device_measurements AS d ON b.benchmark_id = d.benchmark_id\nWHERE\n branch = '${branch}';", "refId": "A", "sql": { "columns": [ { "parameters": [ { "name": "cpu_util", "type": "functionParameter" } ], "type": "function" }, { "parameters": [ { "name": "mem_megabytes", "type": "functionParameter" } ], "type": "function" }, { "parameters": [ { "name": "gpu_util", "type": "functionParameter" } ], "type": "function" }, { "parameters": [ { "name": "gpu_mem_megabytes", "type": "functionParameter" } ], "type": "function" }, { "parameters": [ { "name": "\"time\"", "type": "functionParameter" } ], "type": "function" } ], "groupBy": [ { "property": { "type": "string" }, "type": "groupBy" } ], "limit": 50, "whereJsonTree": { "children1": [ { "id": "baa888b8-89ab-4cde-b012-31922f8671e9", "properties": { "field": "commit_id", "fieldSrc": "field", "operator": "equal", "value": [ "${commit}" ], "valueError": [ null ], "valueSrc": [ "value" ], "valueType": [ "text" ] }, "type": "rule" } ], "id": "bab88a98-0123-4456-b89a-b1922f7d4f11", "type": "group" }, "whereString": "commit_id = '${commit}'" }, "table": "measurements" } ], "title": "GPU memory usage", "transparent": true, "type": "timeseries" } ], "title": "Usage metrics", "type": "row" } ], "schemaVersion": 39, "tags": [], "templating": { "list": [ { "current": { "selected": false, "text": "main", "value": "main" }, "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "definition": "SELECT DISTINCT branch FROM benchmarks;", "description": "", "hide": 0, "includeAll": false, "label": "branch", "multi": false, "name": "branch", "options": [], "query": "SELECT DISTINCT branch FROM benchmarks;", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "type": "query" }, { "current": { "selected": false, "text": "1729701492845", "value": "1729701492845" }, "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "definition": "SELECT created_at - INTERVAL '5 secs' FROM benchmarks WHERE branch = '${branch}' ORDER BY benchmark_id ASC LIMIT 1;", "description": "", "hide": 2, "includeAll": false, "multi": false, "name": "StartTime", "options": [], "query": "SELECT created_at - INTERVAL '5 secs' FROM benchmarks WHERE branch = '${branch}' ORDER BY benchmark_id ASC LIMIT 1;", "refresh": 2, "regex": "", "skipUrlSync": false, "sort": 0, "type": "query" }, { "current": { "selected": false, "text": "1730393397577", "value": "1730393397577" }, "datasource": { "default": true, "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "definition": "SELECT time + INTERVAL '5 secs' FROM benchmarks AS b JOIN device_measurements AS d ON b.benchmark_id = d.benchmark_id WHERE branch = '${branch}' ORDER BY b.benchmark_id DESC, d.measurement_id DESC LIMIT 1;", "description": "", "hide": 2, "includeAll": false, "multi": false, "name": "EndTime", "options": [], "query": "SELECT time + INTERVAL '5 secs' FROM benchmarks AS b JOIN device_measurements AS d ON b.benchmark_id = d.benchmark_id WHERE branch = '${branch}' ORDER BY b.benchmark_id DESC, d.measurement_id DESC LIMIT 1;", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "type": "query" }, { "current": { "selected": false, "text": "NVIDIA A10G", "value": "NVIDIA A10G" }, "datasource": { "type": "grafana-postgresql-datasource", "uid": "be28nkzirtb0gd" }, "definition": "SELECT DISTINCT metadata->>'gpu_name' FROM benchmarks;", "description": "", "hide": 0, "includeAll": false, "label": "GPU", "multi": false, "name": "gpu_name", "options": [], "query": "SELECT DISTINCT metadata->>'gpu_name' FROM benchmarks;", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "type": "query" }, { "current": { "selected": true, "text": "10", "value": "10" }, "description": "The number of commits to display, going from most recent to the nth commit.", "hide": 0, "label": "Last # of commits", "name": "last_n_commits", "options": [ { "selected": true, "text": "10", "value": "10" } ], "query": "10", "skipUrlSync": false, "type": "textbox" } ] }, "time": { "from": "now-1h", "to": "now" }, "timepicker": { "hidden": false }, "timezone": "browser", "title": "Transformers benchmarks", "uid": "fdz33iyzln9c0a", "version": 10, "weekStart": "" }
transformers/benchmark/grafana_dashboard.json/0
{ "file_path": "transformers/benchmark/grafana_dashboard.json", "repo_id": "transformers", "token_count": 42595 }
apiVersion: v1 kind: PersistentVolume metadata: name: huggingface-cluster-disk spec: storageClassName: "" capacity: storage: 500Gi accessModes: - ReadOnlyMany claimRef: namespace: default name: huggingface-cluster-disk-claim gcePersistentDisk: pdName: huggingface-cluster-disk fsType: ext4 readOnly: true --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: huggingface-cluster-disk-claim spec: # Specify "" as the storageClassName so it matches the PersistentVolume's StorageClass. # A nil storageClassName value uses the default StorageClass. For details, see # https://kubernetes.io/docs/concepts/storage/persistent-volumes/#class-1 storageClassName: "" accessModes: - ReadOnlyMany resources: requests: storage: 1Ki
transformers/docker/transformers-pytorch-tpu/dataset.yaml/0
{ "file_path": "transformers/docker/transformers-pytorch-tpu/dataset.yaml", "repo_id": "transformers", "token_count": 274 }
<!--Copyright 2022 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # الإجابة على الأسئلة (Question answering) [[open-in-colab]] <Youtube id="ajPx5LwJD-I"/> تُقدّم مهام الإجابة على الأسئلة إجابةً بناءً على سؤال. إذا سبق لك أن سألت مساعدًا افتراضيًا مثل Alexa أو Siri أو Google عن حالة الطقس، فأنت قد استخدمت نموذج للإجابة على الأسئلة من قبل. هناك نوعان شائعان لمهام الإجابة على الأسئلة: - الاستخراجية: استخراج الإجابة من السياق المحدد. - التلخيصية: إنشاء إجابة من السياق تجيب على السؤال بشكل صحيح. سيوضح لك هذا الدليل كيفية: 1. ضبط [DistilBERT](https://huggingface.co/distilbert/distilbert-base-uncased) على مجموعة بيانات [SQuAD](https://huggingface.co/datasets/squad) للإجابة على الأسئلة الاستخراجية. 2. استخدام النموذج المضبوط للاستدلال. <Tip> لمشاهدة جميع الهياكل والنسخ المتوافقة مع هذه المهمة، نوصي بالرجوع إلى [صفحة المهمة](https://huggingface.co/tasks/question-answering) </Tip> قبل البدء، تأكد من تثبيت جميع المكتبات الضرورية: ```bash pip install transformers datasets evaluate ``` نشجعك على تسجيل الدخول إلى حساب Hugging Face الخاص بك حتى تتمكن من تحميل نموذجك ومشاركته مع المجتمع. عند المطالبة، أدخل الرمز المميز الخاص بك لتسجيل الدخول: ```py >>> from huggingface_hub import notebook_login >>> notebook_login() ``` ## تحميل مجموعة بيانات SQuAD ابدأ بتحميل جزء أصغر من مجموعة بيانات SQuAD من مكتبة 🤗 Datasets. سيتيح لك ذلك فرصة للتجربة والتحقق من عمل كل شيء بشكل صحيح قبل قضاء المزيد من الوقت في التدريب على مجموعة البيانات الكاملة. ```py >>> from datasets import load_dataset >>> squad = load_dataset("squad", split="train[:5000]") ``` قم بتقسيم تقسيم `train` لمجموعة البيانات إلى مجموعة تدريب واختبار باستخدام طريقة [`~datasets.Dataset.train_test_split`]: ```py >>> squad = squad.train_test_split(test_size=0.2) ``` ثم ألق نظرة على مثال: ```py >>> squad["train"][0] {'answers': {'answer_start': [515], 'text': ['Saint Bernadette Soubirous']}, 'context': 'Architecturally, the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.', 'id': '5733be284776f41900661182', 'question': 'To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?', 'title': 'University_of_Notre_Dame' } ``` هناك العديد من الحقول المهمة هنا: - `answers`: موقع بداية الرمز المميز للإجابة ونص الإجابة. - `context`: معلومات أساسية يحتاج النموذج إلى استخراج الإجابة منها. - `question`: السؤال الذي يجب على النموذج الإجابة عليه. ## المعالجة المسبقة (Preprocess) <Youtube id="qgaM0weJHpA"/> الخطوة التالية هي تحميل المحلل اللغوى DistilBERT لمعالجة حقلي `question` و `context`: ```py >>> from transformers import AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") ``` هناك بعض خطوات المعالجة المسبقة الخاصة بمهام الإجابة على الأسئلة التي يجب أن تكون على دراية بها: 1. قد تحتوي بعض الأمثلة في مجموعة البيانات على `context` طويلًا يتجاوز الحد الأقصى لطول مدخل النموذج. للتعامل مع النصوص الأطول، يتم اقتطاع `context` فقط عن طريق تعيين `truncation="only_second"`. 2. بعد ذلك، يتم تحديد مواضع بداية ونهاية الإجابة في `context` الأصلي عن طريق تعيين `return_offset_mapping=True`. 3. باستخدام التعيين، يمكن الآن تحديد رموز بداية ونهاية الإجابة. استخدم طريقة [`~tokenizers.Encoding.sequence_ids`] لتحديد أجزاء الإزاحة التي تتوافق مع `question` و `context`. فيما يلي كيفية إنشاء دالة لقص وتعيين رموز البداية والنهاية لـ `answer` إلى `context`: ```py >>> def preprocess_function(examples): ... questions = [q.strip() for q in examples["question"]] ... inputs = tokenizer( ... questions, ... examples["context"], ... max_length=384, ... truncation="only_second", ... return_offsets_mapping=True, ... padding="max_length", ... ) ... offset_mapping = inputs.pop("offset_mapping") ... answers = examples["answers"] ... start_positions = [] ... end_positions = [] ... for i, offset in enumerate(offset_mapping): ... answer = answers[i] ... start_char = answer["answer_start"][0] ... end_char = answer["answer_start"][0] + len(answer["text"][0]) ... sequence_ids = inputs.sequence_ids(i) ... # Find the start and end of the context ... idx = 0 ... while sequence_ids[idx] != 1: ... idx += 1 ... context_start = idx ... while sequence_ids[idx] == 1: ... idx += 1 ... context_end = idx - 1 ... # If the answer is not fully inside the context, label it (0, 0) ... if offset[context_start][0] > end_char or offset[context_end][1] < start_char: ... start_positions.append(0) ... end_positions.append(0) ... else: ... # Otherwise it's the start and end token positions ... idx = context_start ... while idx <= context_end and offset[idx][0] <= start_char: ... idx += 1 ... start_positions.append(idx - 1) ... idx = context_end ... while idx >= context_start and offset[idx][1] >= end_char: ... idx -= 1 ... end_positions.append(idx + 1) ... inputs["start_positions"] = start_positions ... inputs["end_positions"] = end_positions ... return inputs ``` لتطبيق المعالجة المسبقة على كامل مجموعة البيانات، استخدم [`~datasets.Dataset.map`] من مكتبة 🤗 Datasets. يمكنك تسريع دالة `map` عن طريق تعيين `batched=True` لمعالجة عناصر متعددة من مجموعة البيانات دفعة واحدة. قم بإزالة أي أعمدة لا تحتاجها: ```py >>> tokenized_squad = squad.map(preprocess_function, batched=True, remove_columns=squad["train"].column_names) ``` الآن قم بإنشاء دفعة من الأمثلة باستخدام [`DefaultDataCollator`]. بخلاف مجمّعات البيانات الأخرى في 🤗 Transformers، لا يطبق [`DefaultDataCollator`] أي معالجة مسبقة إضافية مثل الحشو. <frameworkcontent> <pt> ```py >>> from transformers import DefaultDataCollator >>> data_collator = DefaultDataCollator() ``` </pt> <tf> ```py >>> from transformers import DefaultDataCollator >>> data_collator = DefaultDataCollator(return_tensors="tf") ``` </tf> </frameworkcontent> ## التدريب (Train) <frameworkcontent> <pt> <Tip> إذا لم تكن معتادًا على ضبط نموذج باستخدام [`Trainer`], ألق نظرة على البرنامج التعليمي الأساسي [هنا](../training#train-with-pytorch-trainer)! </Tip> أنت جاهز لبدء تدريب نموذجك الآن! قم بتحميل DistilBERT باستخدام [`AutoModelForQuestionAnswering`]: ```py >>> from transformers import AutoModelForQuestionAnswering, TrainingArguments, Trainer >>> model = AutoModelForQuestionAnswering.from_pretrained("distilbert/distilbert-base-uncased") ``` في هذه المرحلة، تبقى ثلاث خطوات فقط: 1. حدد المعاملات الفائقة للتدريب في [`TrainingArguments`]. المعامل الوحيد المطلوب هو `output_dir` الذي يحدد مكان حفظ نموذجك. ستدفع هذا النموذج إلى Hub عن طريق تعيين `push_to_hub=True` (يجب عليك تسجيل الدخول إلى Hugging Face لتحميل نموذجك). 2. مرر معاملات التدريب إلى [`Trainer`] جنبًا إلى جنب مع النموذج، ومجموعة البيانات، والمُحلّل النصي، ومُجمّع البيانات. 3. استدعِ ـ [`~Trainer.train`] لضبط النموذج. ```py >>> training_args = TrainingArguments( ... output_dir="my_awesome_qa_model", ... eval_strategy="epoch", ... learning_rate=2e-5, ... per_device_train_batch_size=16, ... per_device_eval_batch_size=16, ... num_train_epochs=3, ... weight_decay=0.01, ... push_to_hub=True, ... ) >>> trainer = Trainer( ... model=model, ... args=training_args, ... train_dataset=tokenized_squad["train"], ... eval_dataset=tokenized_squad["test"], ... processing_class=tokenizer, ... data_collator=data_collator, ... ) >>> trainer.train() ``` بمجرد اكتمال التدريب، شارك نموذجك في Hub باستخدام الدالة [`~transformers.Trainer.push_to_hub`] حتى يتمكن الجميع من استخدام نموذجك: ```py >>> trainer.push_to_hub() ``` </pt> <tf> <Tip> إذا لم تكن معتادًا على ضبط نموذج باستخدام Keras، فألق نظرة على البرنامج التعليمي الأساسي [هنا](../training#train-a-tensorflow-model-with-keras)! </Tip> لضبط نموذج في TensorFlow، ابدأ بإعداد دالة مُحسِّن، وجدول معدل التعلم، وبعض المعاملات الفائقة للتدريب: ```py >>> from transformers import create_optimizer >>> batch_size = 16 >>> num_epochs = 2 >>> total_train_steps = (len(tokenized_squad["train"]) // batch_size) * num_epochs >>> optimizer, schedule = create_optimizer( ... init_lr=2e-5, ... num_warmup_steps=0, ... num_train_steps=total_train_steps, ... ) ``` ثم يمكنك تحميل DistilBERT باستخدام [`TFAutoModelForQuestionAnswering`]: ```py >>> from transformers import TFAutoModelForQuestionAnswering >>> model = TFAutoModelForQuestionAnswering.from_pretrained("distilbert/distilbert-base-uncased") ``` حوّل مجموعات البيانات الخاصة بك إلى تنسيق `tf.data.Dataset` باستخدام [`~transformers.TFPreTrainedModel.prepare_tf_dataset`]: ```py >>> tf_train_set = model.prepare_tf_dataset( ... tokenized_squad["train"], ... shuffle=True, ... batch_size=16, ... collate_fn=data_collator, ... ) >>> tf_validation_set = model.prepare_tf_dataset( ... tokenized_squad["test"], ... shuffle=False, ... batch_size=16, ... collate_fn=data_collator, ... ) ``` قم بتكوين النموذج للتدريب باستخدام [`compile`](https://keras.io/api/models/model_training_apis/#compile-method): ```py >>> import tensorflow as tf >>> model.compile(optimizer=optimizer) ``` آخر شيء يجب إعداده قبل بدء التدريب هو توفير طريقة لدفع نموذجك إلى Hub. يمكن القيام بذلك عن طريق تحديد مكان دفع نموذجك ومعالجك المعجمي في [`~transformers.PushToHubCallback`]: ```py >>> from transformers.keras_callbacks import PushToHubCallback >>> callback = PushToHubCallback( ... output_dir="my_awesome_qa_model", ... tokenizer=tokenizer, ... ) ``` أخيرًا، أنت جاهز لبدء تدريب نموذجك! اتصل بـ [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) مع مجموعات بيانات التدريب والتحقق من الصحة، وعدد العهود، ومعاودة الاتصال الخاصة بك لضبط النموذج: ```py >>> model.fit(x=tf_train_set, validation_data=tf_validation_set, epochs=3, callbacks=[callback]) ``` بمجرد اكتمال التدريب، يتم تحميل نموذجك تلقائيًا إلى Hub حتى يتمكن الجميع من استخدامه! </tf> </frameworkcontent> <Tip> للحصول على مثال أكثر تعمقًا حول كيفية ضبط نموذج للإجابة على الأسئلة، ألق نظرة على [دفتر ملاحظات PyTorch](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering.ipynb) المقابل أو [دفتر ملاحظات TensorFlow](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering-tf.ipynb). </Tip> ## التقييم (Evaluate) يتطلب التقييم للإجابة على الأسئلة قدرًا كبيرًا من المعالجة اللاحقة. لتوفير وقتك، يتخطى هذا الدليل خطوة التقييم. لا يزال [`Trainer`] يحسب خسارة التقييم أثناء التدريب، مما يعني أنك لست تجهل تمامًا أداء نموذجك. إذا كان لديك المزيد من الوقت وتهتم بكيفية تقييم نموذجك للإجابة على الأسئلة، فألق نظرة على فصل [الإجابة على الأسئلة](https://huggingface.co/course/chapter7/7?fw=pt#post-processing) من دورة 🤗 Hugging Face! ## الاستدلال (Inference) رائع، الآن بعد أن قمت بضبط نموذج، يمكنك استخدامه للاستدلال! حدد سؤالًا وسياقًا ليقوم النموذج بالتنبؤ بالإجابة عليه: ```py >>> question = "How many programming languages does BLOOM support?" >>> context = "BLOOM has 176 billion parameters and can generate text in 46 languages natural languages and 13 programming languages." ``` أبسط طريقة لتجربة نموذجك المُدرَّب للاستدلال هي استخدامه في [`pipeline`]. قم بإنشاء كائن لـ `pipeline` للإجابة على الأسئلة باستخدام نموذجك، ومرِّر النص إليه: ```py >>> from transformers import pipeline >>> question_answerer = pipeline("question-answering", model="my_awesome_qa_model") >>> question_answerer(question=question, context=context) {'score': 0.2058267742395401, 'start': 10, 'end': 95, 'answer': '176 مليار معامل ويمكنه إنشاء نصوص بـ 46 لغة طبيعية و 13'} ``` يمكنك أيضًا تكرار نتائج `pipeline` يدويًا إذا أردت: <frameworkcontent> <pt> قسّم النص وأرجع تنسورات PyTorch: ```py >>> from transformers import AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_qa_model") >>> inputs = tokenizer(question, context, return_tensors="pt") ``` مرر مدخلاتك إلى النموذج وأرجع `logits`: ```py >>> import torch >>> from transformers import AutoModelForQuestionAnswering >>> model = AutoModelForQuestionAnswering.from_pretrained("my_awesome_qa_model") >>> with torch.no_grad(): ... outputs = model(**inputs) ``` احصل على أعلى احتمال من مخرجات النموذج لموضعي البداية والنهاية: ```py >>> answer_start_index = outputs.start_logits.argmax() >>> answer_end_index = outputs.end_logits.argmax() ``` استخلاص الإجابة من الرموز المتوقعة: ```py >>> predict_answer_tokens = inputs.input_ids[0, answer_start_index : answer_end_index + 1] >>> tokenizer.decode(predict_answer_tokens) '176 billion parameters and can generate text in 46 languages natural languages and 13' ``` </pt> <tf> قم بتحليل النص المعجمي وأعد موترات TensorFlow: ```py >>> from transformers import AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_qa_model") >>> inputs = tokenizer(question, context, return_tensors="tf") ``` مرر مدخلاتك إلى النموذج وأعد `logits`: ```py >>> from transformers import TFAutoModelForQuestionAnswering >>> model = TFAutoModelForQuestionAnswering.from_pretrained("my_awesome_qa_model") >>> outputs = model(**inputs) ``` احصل على أعلى احتمال من مخرجات النموذج لموضعي البداية والنهاية: ```py >>> answer_start_index = int(tf.math.argmax(outputs.start_logits, axis=-1)[0]) >>> answer_end_index = int(tf.math.argmax(outputs.end_logits, axis=-1)[0]) ``` استخلاص الإجابة من الرموز المتوقعة: ```py >>> predict_answer_tokens = inputs.input_ids[0, answer_start_index : answer_end_index + 1] >>> tokenizer.decode(predict_answer_tokens) '176 billion parameters and can generate text in 46 languages natural languages and 13' ``` </tf> </frameworkcontent>
transformers/docs/source/ar/tasks/question_answering.md/0
{ "file_path": "transformers/docs/source/ar/tasks/question_answering.md", "repo_id": "transformers", "token_count": 8612 }
<!--Copyright 2020 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Wie kann ich ein Modell zu 🤗 Transformers hinzufügen? Die 🤗 Transformers-Bibliothek ist dank der Beiträge der Community oft in der Lage, neue Modelle anzubieten. Aber das kann ein anspruchsvolles Projekt sein und erfordert eine eingehende Kenntnis der 🤗 Transformers-Bibliothek und des zu implementierenden Modells. Bei Hugging Face versuchen wir, mehr Mitgliedern der Community die Möglichkeit zu geben, aktiv Modelle hinzuzufügen, und wir haben diese Anleitung zusammengestellt, die Sie durch den Prozess des Hinzufügens eines PyTorch-Modells führt (stellen Sie sicher, dass Sie [PyTorch installiert haben](https://pytorch.org/get-started/locally/)). Auf dem Weg dorthin, werden Sie: - Einblicke in bewährte Open-Source-Verfahren erhalten - die Konstruktionsprinzipien hinter einer der beliebtesten Deep-Learning-Bibliotheken verstehen - lernen Sie, wie Sie große Modelle effizient testen können - lernen Sie, wie Sie Python-Hilfsprogramme wie `black`, `ruff` und `make fix-copies` integrieren, um sauberen und lesbaren Code zu gewährleisten Ein Mitglied des Hugging Face-Teams wird Ihnen dabei zur Seite stehen, damit Sie nicht alleine sind. 🤗 ❤️ Um loszulegen, öffnen Sie eine [New model addition](https://github.com/huggingface/transformers/issues/new?assignees=&labels=New+model&template=new-model-addition.yml) Ausgabe für das Modell, das Sie in 🤗 Transformers sehen möchten. Wenn Sie nicht besonders wählerisch sind, wenn es darum geht, ein bestimmtes Modell beizusteuern, können Sie nach dem [New model label](https://github.com/huggingface/transformers/labels/New%20model) filtern, um zu sehen, ob es noch unbeanspruchte Modellanfragen gibt, und daran arbeiten. Sobald Sie eine neue Modellanfrage eröffnet haben, sollten Sie sich zunächst mit 🤗 Transformers vertraut machen, falls Sie das noch nicht sind! ## Allgemeiner Überblick über 🤗 Transformers Zunächst sollten Sie sich einen allgemeinen Überblick über 🤗 Transformers verschaffen. 🤗 Transformers ist eine sehr meinungsfreudige Bibliothek, es ist also möglich, dass Es besteht also die Möglichkeit, dass Sie mit einigen der Philosophien oder Designentscheidungen der Bibliothek nicht einverstanden sind. Aus unserer Erfahrung heraus haben wir jedoch dass die grundlegenden Designentscheidungen und Philosophien der Bibliothek entscheidend sind, um 🤗 Transformers effizient zu skalieren. Transformatoren zu skalieren und gleichzeitig die Wartungskosten auf einem vernünftigen Niveau zu halten. Ein guter erster Ansatzpunkt, um die Bibliothek besser zu verstehen, ist die Lektüre der [Dokumentation unserer Philosophie](Philosophie). Als Ergebnis unserer Arbeitsweise gibt es einige Entscheidungen, die wir versuchen, auf alle Modelle anzuwenden: - Komposition wird im Allgemeinen gegenüber Abstraktion bevorzugt - Die Duplizierung von Code ist nicht immer schlecht, wenn sie die Lesbarkeit oder Zugänglichkeit eines Modells stark verbessert - Modelldateien sind so in sich geschlossen wie möglich, so dass Sie, wenn Sie den Code eines bestimmten Modells lesen, idealerweise nur in die entsprechende Datei `modeling_....py` schauen müssen. Unserer Meinung nach ist der Code der Bibliothek nicht nur ein Mittel, um ein Produkt bereitzustellen, *z.B.* die Möglichkeit, BERT für Inferenz zu verwenden, sondern auch als das Produkt selbst, das wir verbessern wollen. Wenn Sie also ein Modell hinzufügen, ist der Benutzer nicht nur die Person, die Ihr Modell verwenden wird, sondern auch jeder, der Ihren Code liest, zu verstehen versucht und ihn möglicherweise verbessert. Lassen Sie uns daher ein wenig tiefer in das allgemeine Design der Bibliothek einsteigen. ### Überblick über die Modelle Um ein Modell erfolgreich hinzuzufügen, ist es wichtig, die Interaktion zwischen Ihrem Modell und seiner Konfiguration zu verstehen, [`PreTrainedModel`] und [`PretrainedConfig`]. Als Beispiel werden wir das Modell, das zu 🤗 Transformers hinzugefügt werden soll, `BrandNewBert` nennen. Schauen wir uns das mal an: <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers_overview.png"/> Wie Sie sehen, machen wir in 🤗 Transformers von der Vererbung Gebrauch, aber wir beschränken die Abstraktionsebene auf ein absolutes Minimum. Minimum. Es gibt nie mehr als zwei Abstraktionsebenen für ein Modell in der Bibliothek. `BrandNewBertModel` erbt von `BrandNewBertPreTrainedModel`, das wiederum von [`PreTrainedModel`] erbt und das war's. In der Regel wollen wir sicherstellen, dass ein neues Modell nur von [`PreTrainedModel`] abhängt. Die wichtigen Funktionalitäten, die jedem neuen Modell automatisch zur Verfügung gestellt werden, sind Modell automatisch bereitgestellt werden, sind [`~PreTrainedModel.from_pretrained`] und [`~PreTrainedModel.save_pretrained`], die für die Serialisierung und Deserialisierung verwendet werden. Alle anderen wichtigen Funktionalitäten, wie `BrandNewBertModel.forward` sollten vollständig in der neuen Skript `modeling_brand_new_bert.py` definiert werden. Als nächstes wollen wir sicherstellen, dass ein Modell mit einer bestimmten Kopfebene, wie z.B. `BrandNewBertForMaskedLM` nicht von `BrandNewBertModel` erbt, sondern `BrandNewBertModel` verwendet als Komponente, die im Forward Pass aufgerufen werden kann, um die Abstraktionsebene niedrig zu halten. Jedes neue Modell erfordert eine Konfigurationsklasse, genannt `BrandNewBertConfig`. Diese Konfiguration wird immer als ein Attribut in [PreTrainedModel] gespeichert und kann daher über das Attribut `config` für alle Klassen aufgerufen werden die von `BrandNewBertPreTrainedModel` erben: ```python model = BrandNewBertModel.from_pretrained("brandy/brand_new_bert") model.config # model has access to its config ``` Ähnlich wie das Modell erbt die Konfiguration grundlegende Serialisierungs- und Deserialisierungsfunktionalitäten von [`PretrainedConfig`]. Beachten Sie, dass die Konfiguration und das Modell immer in zwei verschiedene Formate serialisiert werden unterschiedliche Formate serialisiert werden - das Modell in eine *pytorch_model.bin* Datei und die Konfiguration in eine *config.json* Datei. Aufruf von [`~PreTrainedModel.save_pretrained`] wird automatisch [`~PretrainedConfig.save_pretrained`] auf, so dass sowohl das Modell als auch die Konfiguration gespeichert werden. ### Code-Stil Wenn Sie Ihr neues Modell kodieren, sollten Sie daran denken, dass Transformers eine Bibliothek mit vielen Meinungen ist und dass wir selbst ein paar Macken haben wie der Code geschrieben werden sollte :-) 1. Der Vorwärtsdurchlauf Ihres Modells sollte vollständig in die Modellierungsdatei geschrieben werden und dabei völlig unabhängig von anderen Modellen in der Bibliothek. Wenn Sie einen Block aus einem anderen Modell wiederverwenden möchten, kopieren Sie den Code und fügen ihn mit einem `# Kopiert von` ein (siehe [hier](https://github.com/huggingface/transformers/blob/v4.17.0/src/transformers/models/roberta/modeling_roberta.py#L160) für ein gutes Beispiel und [hier](pr_checks#check-copies) für weitere Dokumentation zu Copied from). 2. Der Code sollte vollständig verständlich sein, auch für einen Nicht-Muttersprachler. Das heißt, Sie sollten beschreibende Variablennamen wählen und Abkürzungen vermeiden. Ein Beispiel: `activation` ist `act` vorzuziehen. Von Variablennamen mit nur einem Buchstaben wird dringend abgeraten, es sei denn, es handelt sich um einen Index in einer for-Schleife. 3. Generell ziehen wir längeren expliziten Code einem kurzen magischen Code vor. 4. Vermeiden Sie die Unterklassifizierung von `nn.Sequential` in PyTorch, sondern unterklassifizieren Sie `nn.Module` und schreiben Sie den Vorwärtspass, so dass jeder so dass jeder, der Ihren Code verwendet, ihn schnell debuggen kann, indem er Druckanweisungen oder Haltepunkte hinzufügt. 5. Ihre Funktionssignatur sollte mit einer Typ-Annotation versehen sein. Im Übrigen sind gute Variablennamen viel lesbarer und verständlicher verständlicher als Typ-Anmerkungen. ### Übersicht der Tokenizer Noch nicht ganz fertig :-( Dieser Abschnitt wird bald hinzugefügt! ## Schritt-für-Schritt-Rezept zum Hinzufügen eines Modells zu 🤗 Transformers Jeder hat andere Vorlieben, was die Portierung eines Modells angeht. Daher kann es sehr hilfreich sein, wenn Sie sich Zusammenfassungen ansehen wie andere Mitwirkende Modelle auf Hugging Face portiert haben. Hier ist eine Liste von Blogbeiträgen aus der Community, wie man ein Modell portiert: 1. [Portierung eines GPT2-Modells](https://medium.com/huggingface/from-tensorflow-to-pytorch-265f40ef2a28) von [Thomas](https://huggingface.co/thomwolf) 2. [Portierung des WMT19 MT-Modells](https://huggingface.co/blog/porting-fsmt) von [Stas](https://huggingface.co/stas) Aus Erfahrung können wir Ihnen sagen, dass die wichtigsten Dinge, die Sie beim Hinzufügen eines Modells beachten müssen, sind: - Erfinden Sie das Rad nicht neu! Die meisten Teile des Codes, den Sie für das neue 🤗 Transformers-Modell hinzufügen werden, existieren bereits irgendwo in 🤗 Transformers. Nehmen Sie sich etwas Zeit, um ähnliche, bereits vorhandene Modelle und Tokenizer zu finden, die Sie kopieren können von. [grep](https://www.gnu.org/software/grep/) und [rg](https://github.com/BurntSushi/ripgrep) sind Ihre Freunde. Beachten Sie, dass es sehr gut möglich ist, dass der Tokenizer Ihres Modells auf einer Modellimplementierung basiert und und der Modellierungscode Ihres Modells auf einer anderen. *Z.B.* Der Modellierungscode von FSMT basiert auf BART, während der Tokenizer-Code von FSMT auf XLM basiert. - Es handelt sich eher um eine technische als um eine wissenschaftliche Herausforderung. Sie sollten mehr Zeit auf die Schaffung einer eine effiziente Debugging-Umgebung zu schaffen, als zu versuchen, alle theoretischen Aspekte des Modells in dem Papier zu verstehen. - Bitten Sie um Hilfe, wenn Sie nicht weiterkommen! Modelle sind der Kernbestandteil von 🤗 Transformers, so dass wir bei Hugging Face mehr als mehr als glücklich, Ihnen bei jedem Schritt zu helfen, um Ihr Modell hinzuzufügen. Zögern Sie nicht zu fragen, wenn Sie merken, dass Sie nicht weiterkommen. Fortschritte machen. Im Folgenden versuchen wir, Ihnen ein allgemeines Rezept an die Hand zu geben, das uns bei der Portierung eines Modells auf 🤗 Transformers am nützlichsten erschien. Die folgende Liste ist eine Zusammenfassung all dessen, was getan werden muss, um ein Modell hinzuzufügen und kann von Ihnen als To-Do verwendet werden Liste verwenden: ☐ (Optional) Verstehen der theoretischen Aspekte des Modells<br> ☐ Vorbereiten der 🤗 Transformers-Entwicklungsumgebung<br> ☐ Debugging-Umgebung des ursprünglichen Repositorys eingerichtet<br> ☐ Skript erstellt, das den Durchlauf `forward()` unter Verwendung des ursprünglichen Repositorys und des Checkpoints erfolgreich durchführt<br> ☐ Erfolgreich das Modellskelett zu 🤗 Transformers hinzugefügt<br> ☐ Erfolgreiche Umwandlung des ursprünglichen Prüfpunkts in den 🤗 Transformers-Prüfpunkt<br> ☐ Erfolgreich den Durchlauf `forward()` in 🤗 Transformers ausgeführt, der eine identische Ausgabe wie der ursprüngliche Prüfpunkt liefert<br> ☐ Modell-Tests in 🤗 Transformers abgeschlossen<br> ☐ Erfolgreich Tokenizer in 🤗 Transformers hinzugefügt<br> ☐ End-to-End-Integrationstests ausgeführt<br> ☐ Docs fertiggestellt<br> ☐ Modellgewichte in den Hub hochgeladen<br> ☐ Die Pull-Anfrage eingereicht<br> ☐ (Optional) Hinzufügen eines Demo-Notizbuchs Für den Anfang empfehlen wir in der Regel, mit einem guten theoretischen Verständnis von `BrandNewBert` zu beginnen. Wie auch immer, wenn Sie es vorziehen, die theoretischen Aspekte des Modells *on-the-job* zu verstehen, dann ist es völlig in Ordnung, direkt in die in die Code-Basis von `BrandNewBert` einzutauchen. Diese Option könnte für Sie besser geeignet sein, wenn Ihre technischen Fähigkeiten besser sind als als Ihre theoretischen Fähigkeiten, wenn Sie Schwierigkeiten haben, die Arbeit von `BrandNewBert` zu verstehen, oder wenn Sie einfach Spaß am Programmieren mehr Spaß am Programmieren haben als am Lesen wissenschaftlicher Abhandlungen. ### 1. (Optional) Theoretische Aspekte von BrandNewBert Sie sollten sich etwas Zeit nehmen, um die Abhandlung von *BrandNewBert* zu lesen, falls eine solche Beschreibung existiert. Möglicherweise gibt es große Abschnitte des Papiers, die schwer zu verstehen sind. Wenn das der Fall ist, ist das in Ordnung - machen Sie sich keine Sorgen! Das Ziel ist ist es nicht, ein tiefes theoretisches Verständnis des Papiers zu erlangen, sondern die notwendigen Informationen zu extrahieren, um das Modell effektiv in 🤗 Transformers zu implementieren. Das heißt, Sie müssen nicht zu viel Zeit auf die theoretischen Aspekten verbringen, sondern sich lieber auf die praktischen Aspekte konzentrieren, nämlich: - Welche Art von Modell ist *brand_new_bert*? BERT-ähnliches Modell nur für den Encoder? GPT2-ähnliches reines Decoder-Modell? BART-ähnliches Encoder-Decoder-Modell? Sehen Sie sich die [model_summary](model_summary) an, wenn Sie mit den Unterschieden zwischen diesen Modellen nicht vertraut sind. - Was sind die Anwendungen von *brand_new_bert*? Textklassifizierung? Texterzeugung? Seq2Seq-Aufgaben, *z.B.,* Zusammenfassungen? - Was ist die neue Eigenschaft des Modells, die es von BERT/GPT-2/BART unterscheidet? - Welches der bereits existierenden [🤗 Transformers-Modelle](https://huggingface.co/transformers/#contents) ist am ähnlichsten ähnlich wie *brand_new_bert*? - Welche Art von Tokenizer wird verwendet? Ein Satzteil-Tokenisierer? Ein Wortstück-Tokenisierer? Ist es derselbe Tokenisierer, der für für BERT oder BART? Nachdem Sie das Gefühl haben, einen guten Überblick über die Architektur des Modells erhalten zu haben, können Sie dem Hugging Face Team schreiben und Ihre Fragen stellen. Dazu können Fragen zur Architektur des Modells gehören, seiner Aufmerksamkeitsebene usw. Wir werden Ihnen gerne weiterhelfen. ### 2. Bereiten Sie als nächstes Ihre Umgebung vor 1. Forken Sie das [Repository](https://github.com/huggingface/transformers), indem Sie auf der Seite des Repositorys auf die Schaltfläche 'Fork' klicken. Seite des Repositorys klicken. Dadurch wird eine Kopie des Codes unter Ihrem GitHub-Benutzerkonto erstellt. 2. Klonen Sie Ihren `transformers` Fork auf Ihre lokale Festplatte und fügen Sie das Basis-Repository als Remote hinzu: ```bash git clone https://github.com/[your Github handle]/transformers.git cd transformers git remote add upstream https://github.com/huggingface/transformers.git ``` 3. Richten Sie eine Entwicklungsumgebung ein, indem Sie z.B. den folgenden Befehl ausführen: ```bash python -m venv .env source .env/bin/activate pip install -e ".[dev]" ``` Abhängig von Ihrem Betriebssystem und da die Anzahl der optionalen Abhängigkeiten von Transformers wächst, kann es sein, dass Sie bei diesem Befehl einen Fehler mit diesem Befehl. Stellen Sie in diesem Fall sicher, dass Sie das Deep Learning Framework, mit dem Sie arbeiten, installieren (PyTorch, TensorFlow und/oder Flax) und führen Sie es aus: ```bash pip install -e ".[quality]" ``` was für die meisten Anwendungsfälle ausreichend sein sollte. Sie können dann zum übergeordneten Verzeichnis zurückkehren ```bash cd .. ``` 4. Wir empfehlen, die PyTorch-Version von *brand_new_bert* zu Transformers hinzuzufügen. Um PyTorch zu installieren, folgen Sie bitte den Anweisungen auf https://pytorch.org/get-started/locally/. **Anmerkung:** Sie müssen CUDA nicht installiert haben. Es reicht aus, das neue Modell auf der CPU zum Laufen zu bringen. 5. Um *brand_new_bert* zu portieren, benötigen Sie außerdem Zugriff auf das Original-Repository: ```bash git clone https://github.com/org_that_created_brand_new_bert_org/brand_new_bert.git cd brand_new_bert pip install -e . ``` Jetzt haben Sie eine Entwicklungsumgebung eingerichtet, um *brand_new_bert* auf 🤗 Transformers zu portieren. ### 3.-4. Führen Sie einen Pre-Training-Checkpoint mit dem Original-Repository durch Zunächst werden Sie mit dem ursprünglichen *brand_new_bert* Repository arbeiten. Oft ist die ursprüngliche Implementierung sehr "forschungslastig". Das bedeutet, dass es an Dokumentation mangeln kann und der Code schwer zu verstehen sein kann. Aber das sollte genau Ihre Motivation sein, *brand_new_bert* neu zu implementieren. Eines unserer Hauptziele bei Hugging Face ist es, *die Menschen dazu zu bringen auf den Schultern von Giganten zu stehen*, was sich hier sehr gut darin ausdrückt, dass wir ein funktionierendes Modell nehmen und es umschreiben, um es so es so **zugänglich, benutzerfreundlich und schön** wie möglich zu machen. Dies ist die wichtigste Motivation für die Neuimplementierung von Modelle in 🤗 Transformers umzuwandeln - der Versuch, komplexe neue NLP-Technologie für **jeden** zugänglich zu machen. Sie sollten damit beginnen, indem Sie in das Original-Repository eintauchen. Die erfolgreiche Ausführung des offiziellen Pre-Trainingsmodells im Original-Repository ist oft **der schwierigste** Schritt. Unserer Erfahrung nach ist es sehr wichtig, dass Sie einige Zeit damit verbringen, sich mit der ursprünglichen Code-Basis vertraut zu machen. Sie müssen das Folgende herausfinden: - Wo finden Sie die vortrainierten Gewichte? - Wie lädt man die vorab trainierten Gewichte in das entsprechende Modell? - Wie kann der Tokenizer unabhängig vom Modell ausgeführt werden? - Verfolgen Sie einen Forward Pass, damit Sie wissen, welche Klassen und Funktionen für einen einfachen Forward Pass erforderlich sind. Normalerweise, müssen Sie nur diese Funktionen reimplementieren. - Sie müssen in der Lage sein, die wichtigen Komponenten des Modells zu finden: Wo befindet sich die Klasse des Modells? Gibt es Unterklassen des Modells, *z.B.* EncoderModel, DecoderModel? Wo befindet sich die Selbstaufmerksamkeitsschicht? Gibt es mehrere verschiedene Aufmerksamkeitsebenen, *z.B.* *Selbstaufmerksamkeit*, *Kreuzaufmerksamkeit*...? - Wie können Sie das Modell in der ursprünglichen Umgebung des Repo debuggen? Müssen Sie *print* Anweisungen hinzufügen, können Sie mit einem interaktiven Debugger wie *ipdb* arbeiten oder sollten Sie eine effiziente IDE zum Debuggen des Modells verwenden, wie z.B. PyCharm? Es ist sehr wichtig, dass Sie, bevor Sie mit der Portierung beginnen, den Code im Original-Repository **effizient** debuggen können Repository können! Denken Sie auch daran, dass Sie mit einer Open-Source-Bibliothek arbeiten, also zögern Sie nicht, ein Problem oder oder sogar eine Pull-Anfrage im Original-Repository zu stellen. Die Betreuer dieses Repositorys sind wahrscheinlich sehr froh darüber dass jemand in ihren Code schaut! An diesem Punkt liegt es wirklich an Ihnen, welche Debugging-Umgebung und Strategie Sie zum Debuggen des ursprünglichen Modell zu debuggen. Wir raten dringend davon ab, eine kostspielige GPU-Umgebung einzurichten, sondern arbeiten Sie einfach auf einer CPU, sowohl wenn Sie mit dem in das ursprüngliche Repository einzutauchen und auch, wenn Sie beginnen, die 🤗 Transformers-Implementierung des Modells zu schreiben. Nur ganz am Ende, wenn das Modell bereits erfolgreich auf 🤗 Transformers portiert wurde, sollte man überprüfen, ob das Modell auch auf der GPU wie erwartet funktioniert. Im Allgemeinen gibt es zwei mögliche Debugging-Umgebungen für die Ausführung des Originalmodells - [Jupyter notebooks](https://jupyter.org/) / [google colab](https://colab.research.google.com/notebooks/intro.ipynb) - Lokale Python-Skripte. Jupyter-Notebooks haben den Vorteil, dass sie eine zellenweise Ausführung ermöglichen, was hilfreich sein kann, um logische Komponenten besser voneinander zu trennen und logische Komponenten voneinander zu trennen und schnellere Debugging-Zyklen zu haben, da Zwischenergebnisse gespeichert werden können. Außerdem, Außerdem lassen sich Notebooks oft leichter mit anderen Mitwirkenden teilen, was sehr hilfreich sein kann, wenn Sie das Hugging Face Team um Hilfe bitten möchten. Face Team um Hilfe bitten. Wenn Sie mit Jupyter-Notizbüchern vertraut sind, empfehlen wir Ihnen dringend, mit ihnen zu arbeiten. Der offensichtliche Nachteil von Jupyter-Notizbüchern ist, dass Sie, wenn Sie nicht daran gewöhnt sind, mit ihnen zu arbeiten, einige Zeit damit verbringen müssen einige Zeit damit verbringen müssen, sich an die neue Programmierumgebung zu gewöhnen, und dass Sie möglicherweise Ihre bekannten Debugging-Tools nicht mehr verwenden können wie z.B. `ipdb` nicht mehr verwenden können. Für jede Codebasis ist es immer ein guter erster Schritt, einen **kleinen** vortrainierten Checkpoint zu laden und in der Lage zu sein, einen einzelnen Vorwärtsdurchlauf mit einem Dummy-Integer-Vektor von Eingabe-IDs als Eingabe zu reproduzieren. Ein solches Skript könnte wie folgt aussehen (in Pseudocode): ```python model = BrandNewBertModel.load_pretrained_checkpoint("/path/to/checkpoint/") input_ids = [0, 4, 5, 2, 3, 7, 9] # vector of input ids original_output = model.predict(input_ids) ``` Was die Debugging-Strategie anbelangt, so können Sie im Allgemeinen aus mehreren Strategien wählen: - Zerlegen Sie das ursprüngliche Modell in viele kleine testbare Komponenten und führen Sie für jede dieser Komponenten einen Vorwärtsdurchlauf zur Überprüfung - Zerlegen Sie das ursprüngliche Modell nur in den ursprünglichen *Tokenizer* und das ursprüngliche *Modell*, führen Sie einen Vorwärtsdurchlauf für diese Komponenten durch und verwenden Sie dazwischenliegende Druckanweisungen oder Haltepunkte zur Überprüfung. Auch hier bleibt es Ihnen überlassen, welche Strategie Sie wählen. Oft ist die eine oder die andere Strategie vorteilhaft, je nach der ursprünglichen Codebasis Basis. Wenn die ursprüngliche Codebasis es Ihnen erlaubt, das Modell in kleinere Teilkomponenten zu zerlegen, *z.B.* wenn die ursprüngliche Code-Basis problemlos im Eager-Modus ausgeführt werden kann, lohnt es sich in der Regel, dies zu tun. Es gibt einige wichtige Vorteile am Anfang den schwierigeren Weg zu gehen: - Wenn Sie später das ursprüngliche Modell mit der Hugging Face-Implementierung vergleichen, können Sie automatisch überprüfen, ob für jede Komponente einzeln überprüfen, ob die entsprechende Komponente der 🤗 Transformers-Implementierung übereinstimmt, anstatt sich auf anstatt sich auf den visuellen Vergleich über Druckanweisungen zu verlassen - können Sie das große Problem der Portierung eines Modells in kleinere Probleme der Portierung einzelner Komponenten zerlegen einzelnen Komponenten zu zerlegen und so Ihre Arbeit besser zu strukturieren - Die Aufteilung des Modells in logisch sinnvolle Komponenten hilft Ihnen, einen besseren Überblick über das Design des Modells zu bekommen und somit das Modell besser zu verstehen - In einem späteren Stadium helfen Ihnen diese komponentenweisen Tests dabei, sicherzustellen, dass keine Regressionen auftreten, während Sie fortfahren Ihren Code ändern [Lysandre's](https://gist.github.com/LysandreJik/db4c948f6b4483960de5cbac598ad4ed) Integrationstests für ELECTRA gibt ein schönes Beispiel dafür, wie dies geschehen kann. Wenn die ursprüngliche Codebasis jedoch sehr komplex ist oder nur die Ausführung von Zwischenkomponenten in einem kompilierten Modus erlaubt, könnte es zu zeitaufwändig oder sogar unmöglich sein, das Modell in kleinere testbare Teilkomponenten zu zerlegen. Ein gutes Beispiel ist die [T5's MeshTensorFlow](https://github.com/tensorflow/mesh/tree/master/mesh_tensorflow) Bibliothek, die sehr komplex ist sehr komplex ist und keine einfache Möglichkeit bietet, das Modell in seine Unterkomponenten zu zerlegen. Bei solchen Bibliotheken ist man oft auf die Überprüfung von Druckanweisungen angewiesen. Unabhängig davon, welche Strategie Sie wählen, ist die empfohlene Vorgehensweise oft die gleiche, nämlich dass Sie mit der Fehlersuche in den die Anfangsebenen zuerst und die Endebenen zuletzt debuggen. Es wird empfohlen, dass Sie die Ausgaben der folgenden Ebenen abrufen, entweder durch Druckanweisungen oder Unterkomponentenfunktionen Schichten in der folgenden Reihenfolge abrufen: 1. Rufen Sie die Eingabe-IDs ab, die an das Modell übergeben wurden 2. Rufen Sie die Worteinbettungen ab 3. Rufen Sie die Eingabe der ersten Transformer-Schicht ab 4. Rufen Sie die Ausgabe der ersten Transformer-Schicht ab 5. Rufen Sie die Ausgabe der folgenden n - 1 Transformer-Schichten ab 6. Rufen Sie die Ausgabe des gesamten BrandNewBert Modells ab Die Eingabe-IDs sollten dabei aus einem Array von Ganzzahlen bestehen, *z.B.* `input_ids = [0, 4, 4, 3, 2, 4, 1, 7, 19]` Die Ausgaben der folgenden Schichten bestehen oft aus mehrdimensionalen Float-Arrays und können wie folgt aussehen: ``` [[ [-0.1465, -0.6501, 0.1993, ..., 0.1451, 0.3430, 0.6024], [-0.4417, -0.5920, 0.3450, ..., -0.3062, 0.6182, 0.7132], [-0.5009, -0.7122, 0.4548, ..., -0.3662, 0.6091, 0.7648], ..., [-0.5613, -0.6332, 0.4324, ..., -0.3792, 0.7372, 0.9288], [-0.5416, -0.6345, 0.4180, ..., -0.3564, 0.6992, 0.9191], [-0.5334, -0.6403, 0.4271, ..., -0.3339, 0.6533, 0.8694]]], ``` Wir erwarten, dass jedes zu 🤗 Transformers hinzugefügte Modell eine Reihe von Integrationstests besteht, was bedeutet, dass das ursprüngliche Modell und die neu implementierte Version in 🤗 Transformers exakt dieselbe Ausgabe liefern müssen, und zwar mit einer Genauigkeit von 0,001! Da es normal ist, dass das exakt gleiche Modell, das in verschiedenen Bibliotheken geschrieben wurde, je nach Bibliotheksrahmen eine leicht unterschiedliche Ausgabe liefern kann eine leicht unterschiedliche Ausgabe liefern kann, akzeptieren wir eine Fehlertoleranz von 1e-3 (0,001). Es reicht nicht aus, wenn das Modell fast das gleiche Ergebnis liefert, sie müssen fast identisch sein. Daher werden Sie sicherlich die Zwischenergebnisse Zwischenergebnisse der 🤗 Transformers-Version mehrfach mit den Zwischenergebnissen der ursprünglichen Implementierung von *brand_new_bert* vergleichen. In diesem Fall ist eine **effiziente** Debugging-Umgebung des ursprünglichen Repositorys absolut wichtig ist. Hier sind einige Ratschläge, um Ihre Debugging-Umgebung so effizient wie möglich zu gestalten. - Finden Sie den besten Weg, um Zwischenergebnisse zu debuggen. Ist das ursprüngliche Repository in PyTorch geschrieben? Dann sollten Sie dann sollten Sie sich wahrscheinlich die Zeit nehmen, ein längeres Skript zu schreiben, das das ursprüngliche Modell in kleinere Unterkomponenten zerlegt, um Zwischenwerte abzurufen. Ist das ursprüngliche Repository in Tensorflow 1 geschrieben? Dann müssen Sie sich möglicherweise auf die TensorFlow Druckoperationen wie [tf.print](https://www.tensorflow.org/api_docs/python/tf/print) verlassen, um die Zwischenwerte auszugeben. Ist das ursprüngliche Repository in Jax geschrieben? Dann stellen Sie sicher, dass das Modell **nicht jitted** ist, wenn wenn Sie den Vorwärtsdurchlauf ausführen, *z.B.* schauen Sie sich [dieser Link](https://github.com/google/jax/issues/196) an. - Verwenden Sie den kleinsten vortrainierten Prüfpunkt, den Sie finden können. Je kleiner der Prüfpunkt ist, desto schneller wird Ihr Debugging-Zyklus wird. Es ist nicht effizient, wenn Ihr vorab trainiertes Modell so groß ist, dass Ihr Vorwärtsdurchlauf mehr als 10 Sekunden dauert. Falls nur sehr große Checkpoints verfügbar sind, kann es sinnvoller sein, ein Dummy-Modell in der neuen Umgebung mit zufällig initialisierten Gewichten zu erstellen und diese Gewichte zum Vergleich mit der 🤗 Transformers-Version Ihres Modells - Vergewissern Sie sich, dass Sie den einfachsten Weg wählen, um einen Forward Pass im ursprünglichen Repository aufzurufen. Idealerweise sollten Sie die Funktion im originalen Repository finden, die **nur** einen einzigen Vorwärtspass aufruft, *d.h.* die oft aufgerufen wird Vorhersagen", "Auswerten", "Vorwärts" oder "Aufruf" genannt wird. Sie wollen keine Funktion debuggen, die `forward` aufruft mehrfach aufruft, *z.B.* um Text zu erzeugen, wie `autoregressive_sample`, `generate`. - Versuchen Sie, die Tokenisierung vom *Forward*-Pass des Modells zu trennen. Wenn das Original-Repository Beispiele zeigt, bei denen Sie eine Zeichenkette eingeben müssen, dann versuchen Sie herauszufinden, an welcher Stelle im Vorwärtsaufruf die Zeichenketteneingabe in Eingabe-IDs geändert wird geändert wird und beginnen Sie an dieser Stelle. Das könnte bedeuten, dass Sie möglicherweise selbst ein kleines Skript schreiben oder den Originalcode so ändern müssen, dass Sie die ids direkt eingeben können, anstatt eine Zeichenkette einzugeben. - Vergewissern Sie sich, dass sich das Modell in Ihrem Debugging-Setup **nicht** im Trainingsmodus befindet, der oft dazu führt, dass das Modell Dies führt häufig zu zufälligen Ergebnissen, da das Modell mehrere Dropout-Schichten enthält. Stellen Sie sicher, dass der Vorwärtsdurchlauf in Ihrer Debugging Umgebung **deterministisch** ist, damit die Dropout-Schichten nicht verwendet werden. Oder verwenden Sie *transformers.utils.set_seed*. wenn sich die alte und die neue Implementierung im selben Framework befinden. Im folgenden Abschnitt finden Sie genauere Details/Tipps, wie Sie dies für *brand_new_bert* tun können. ### 5.-14. Portierung von BrandNewBert auf 🤗 Transformatoren Als nächstes können Sie endlich damit beginnen, neuen Code zu 🤗 Transformers hinzuzufügen. Gehen Sie in den Klon Ihres 🤗 Transformers Forks: ```bash cd transformers ``` In dem speziellen Fall, dass Sie ein Modell hinzufügen, dessen Architektur genau mit der Modellarchitektur eines Modells übereinstimmt, müssen Sie nur ein Konvertierungsskript hinzufügen, wie in [diesem Abschnitt](#write-a-conversion-script) beschrieben. In diesem Fall können Sie einfach die gesamte Modellarchitektur des bereits vorhandenen Modells wiederverwenden. Andernfalls beginnen wir mit der Erstellung eines neuen Modells. Wir empfehlen die Verwendung des folgenden Skripts, um ein Modell hinzuzufügen ein bestehendes Modell: ```bash transformers-cli add-new-model-like ``` Sie werden mit einem Fragebogen aufgefordert, die grundlegenden Informationen Ihres Modells einzugeben. **Eröffnen Sie einen Pull Request auf dem Haupt-Repositorium huggingface/transformers** Bevor Sie mit der Anpassung des automatisch generierten Codes beginnen, ist es nun an der Zeit, einen "Work in progress (WIP)" Pull Anfrage, *z.B.* "[WIP] Add *brand_new_bert*", in 🤗 Transformers zu öffnen, damit Sie und das Hugging Face Team Seite an Seite an der Integration des Modells in 🤗 Transformers arbeiten können. Sie sollten Folgendes tun: 1. Erstellen Sie eine Verzweigung mit einem beschreibenden Namen von Ihrer Hauptverzweigung ```bash git checkout -b add_brand_new_bert ``` 2. Bestätigen Sie den automatisch generierten Code: ```bash git add . git commit ``` 3. Abrufen und zurücksetzen auf die aktuelle Haupt ```bash git fetch upstream git rebase upstream/main ``` 4. Übertragen Sie die Änderungen auf Ihr Konto mit: ```bash git push -u origin a-descriptive-name-for-my-changes ``` 5. Wenn Sie zufrieden sind, gehen Sie auf die Webseite Ihrer Abspaltung auf GitHub. Klicken Sie auf "Pull request". Stellen Sie sicher, dass Sie das GitHub-Handle einiger Mitglieder des Hugging Face-Teams als Reviewer hinzuzufügen, damit das Hugging Face-Team über zukünftige Änderungen informiert wird. zukünftige Änderungen benachrichtigt wird. 6. Ändern Sie den PR in einen Entwurf, indem Sie auf der rechten Seite der GitHub-Pull-Request-Webseite auf "In Entwurf umwandeln" klicken. Vergessen Sie im Folgenden nicht, wenn Sie Fortschritte gemacht haben, Ihre Arbeit zu committen und in Ihr Konto zu pushen, damit sie in der Pull-Anfrage erscheint. damit sie in der Pull-Anfrage angezeigt wird. Außerdem sollten Sie darauf achten, dass Sie Ihre Arbeit von Zeit zu Zeit mit dem aktuellen main von Zeit zu Zeit zu aktualisieren, indem Sie dies tun: ```bash git fetch upstream git merge upstream/main ``` Generell sollten Sie alle Fragen, die Sie in Bezug auf das Modell oder Ihre Implementierung haben, in Ihrem PR stellen und in der PR diskutiert/gelöst werden. Auf diese Weise wird das Hugging Face Team immer benachrichtigt, wenn Sie neuen Code einreichen oder wenn Sie eine Frage haben. Es ist oft sehr hilfreich, das Hugging Face-Team auf Ihren hinzugefügten Code hinzuweisen, damit das Hugging Face-Team Ihr Problem oder Ihre Frage besser verstehen kann. Face-Team Ihr Problem oder Ihre Frage besser verstehen kann. Gehen Sie dazu auf die Registerkarte "Geänderte Dateien", auf der Sie alle Ihre Änderungen sehen, gehen Sie zu einer Zeile, zu der Sie eine Frage stellen möchten eine Frage stellen möchten, und klicken Sie auf das "+"-Symbol, um einen Kommentar hinzuzufügen. Wenn eine Frage oder ein Problem gelöst wurde, können Sie auf die Schaltfläche "Lösen" des erstellten Kommentars klicken. Auf dieselbe Weise wird das Hugging Face-Team Kommentare öffnen, wenn es Ihren Code überprüft. Wir empfehlen, die meisten Fragen auf GitHub in Ihrem PR zu stellen. Für einige sehr allgemeine Fragen, die für die Öffentlichkeit nicht sehr nützlich sind, können Sie das Hugging Face Team per Slack oder E-Mail zu stellen. **5. Passen Sie den Code der generierten Modelle für brand_new_bert** an. Zunächst werden wir uns nur auf das Modell selbst konzentrieren und uns nicht um den Tokenizer kümmern. Den gesamten relevanten Code sollten Sie finden Sie in den generierten Dateien `src/transformers/models/brand_new_bert/modeling_brand_new_bert.py` und `src/transformers/models/brand_new_bert/configuration_brand_new_bert.py`. Jetzt können Sie endlich mit dem Programmieren beginnen :). Der generierte Code in `src/transformers/models/brand_new_bert/modeling_brand_new_bert.py` wird entweder die gleiche Architektur wie BERT haben, wenn wenn es sich um ein reines Encoder-Modell handelt oder BART, wenn es sich um ein Encoder-Decoder-Modell handelt. An diesem Punkt sollten Sie sich daran erinnern, was was Sie am Anfang über die theoretischen Aspekte des Modells gelernt haben: *Wie unterscheidet sich das Modell von BERT oder BART?*". Implementieren Sie diese Änderungen, was oft bedeutet, dass Sie die *Selbstaufmerksamkeitsschicht*, die Reihenfolge der Normalisierungsschicht usw. ändern müssen. Schicht usw... Auch hier ist es oft nützlich, sich die ähnliche Architektur bereits bestehender Modelle in Transformers anzusehen, um ein besseres Gefühl dafür zu bekommen ein besseres Gefühl dafür zu bekommen, wie Ihr Modell implementiert werden sollte. **Beachten Sie**, dass Sie an diesem Punkt nicht sehr sicher sein müssen, dass Ihr Code völlig korrekt oder sauber ist. Vielmehr ist es Sie sollten vielmehr eine erste *unbereinigte*, kopierte Version des ursprünglichen Codes in src/transformers/models/brand_new_bert/modeling_brand_new_bert.py" hinzuzufügen, bis Sie das Gefühl haben, dass der gesamte notwendige Code hinzugefügt wurde. Unserer Erfahrung nach ist es viel effizienter, schnell eine erste Version des erforderlichen Codes hinzuzufügen und den Code iterativ mit dem Konvertierungsskript zu verbessern/korrigieren, wie im nächsten Abschnitt beschrieben. Das einzige, was zu diesem Zeitpunkt funktionieren muss, ist, dass Sie die 🤗 Transformers-Implementierung von *brand_new_bert* instanziieren können, *d.h.* der folgende Befehl sollte funktionieren: ```python from transformers import BrandNewBertModel, BrandNewBertConfig model = BrandNewBertModel(BrandNewBertConfig()) ``` Der obige Befehl erstellt ein Modell gemäß den Standardparametern, die in `BrandNewBertConfig()` definiert sind, mit zufälligen Gewichten und stellt damit sicher, dass die `init()` Methoden aller Komponenten funktionieren. Beachten Sie, dass alle zufälligen Initialisierungen in der Methode `_init_weights` Ihres `BrandnewBertPreTrainedModel` stattfinden sollten. Klasse erfolgen sollte. Sie sollte alle Blattmodule in Abhängigkeit von den Variablen der Konfiguration initialisieren. Hier ist ein Beispiel mit der BERT `_init_weights` Methode: ```py def _init_weights(self, module): """Initialize the weights""" if isinstance(module, nn.Linear): module.weight.data.normal_(mean=0.0, std=self.config.initializer_range) if module.bias is not None: module.bias.data.zero_() elif isinstance(module, nn.Embedding): module.weight.data.normal_(mean=0.0, std=self.config.initializer_range) if module.padding_idx is not None: module.weight.data[module.padding_idx].zero_() elif isinstance(module, nn.LayerNorm): module.bias.data.zero_() module.weight.data.fill_(1.0) ``` Sie können weitere benutzerdefinierte Schemata verwenden, wenn Sie eine spezielle Initialisierung für einige Module benötigen. Zum Beispiel in `Wav2Vec2ForPreTraining` müssen die letzten beiden linearen Schichten die Initialisierung des regulären PyTorch `nn.Linear` haben. aber alle anderen sollten eine Initialisierung wie oben verwenden. Dies ist wie folgt kodiert: ```py def _init_weights(self, module): """Initialize the weights""" if isinstance(module, Wav2Vec2ForPreTraining): module.project_hid.reset_parameters() module.project_q.reset_parameters() module.project_hid._is_hf_initialized = True module.project_q._is_hf_initialized = True elif isinstance(module, nn.Linear): module.weight.data.normal_(mean=0.0, std=self.config.initializer_range) if module.bias is not None: module.bias.data.zero_() ``` Das Flag `_is_hf_initialized` wird intern verwendet, um sicherzustellen, dass wir ein Submodul nur einmal initialisieren. Wenn Sie es auf `True` für `module.project_q` und `module.project_hid` setzen, stellen wir sicher, dass die benutzerdefinierte Initialisierung, die wir vorgenommen haben, später nicht überschrieben wird, die Funktion `_init_weights` nicht auf sie angewendet wird. **6. Schreiben Sie ein Konvertierungsskript** Als nächstes sollten Sie ein Konvertierungsskript schreiben, mit dem Sie den Checkpoint, den Sie zum Debuggen von *brand_new_bert* im im ursprünglichen Repository in einen Prüfpunkt konvertieren, der mit Ihrer gerade erstellten 🤗 Transformers-Implementierung von *brand_new_bert*. Es ist nicht ratsam, das Konvertierungsskript von Grund auf neu zu schreiben, sondern die bereits bestehenden Konvertierungsskripten in 🤗 Transformers nach einem Skript zu suchen, das für die Konvertierung eines ähnlichen Modells verwendet wurde, das im demselben Framework wie *brand_new_bert* geschrieben wurde. Normalerweise reicht es aus, ein bereits vorhandenes Konvertierungsskript zu kopieren und es für Ihren Anwendungsfall leicht anzupassen. Zögern Sie nicht, das Hugging Face Team zu bitten, Sie auf ein ähnliches, bereits vorhandenes Konvertierungsskript für Ihr Modell zu finden. - Wenn Sie ein Modell von TensorFlow nach PyTorch portieren, ist ein guter Ausgangspunkt das Konvertierungsskript von BERT [hier](https://github.com/huggingface/transformers/blob/7acfa95afb8194f8f9c1f4d2c6028224dbed35a2/src/transformers/models/bert/modeling_bert.py#L91) - Wenn Sie ein Modell von PyTorch nach PyTorch portieren, ist ein guter Ausgangspunkt das Konvertierungsskript von BART [hier](https://github.com/huggingface/transformers/blob/main/src/transformers/models/bart/convert_bart_original_pytorch_checkpoint_to_pytorch.py) Im Folgenden werden wir kurz erklären, wie PyTorch-Modelle Ebenengewichte speichern und Ebenennamen definieren. In PyTorch wird der Name einer Ebene durch den Namen des Klassenattributs definiert, das Sie der Ebene geben. Lassen Sie uns ein Dummy-Modell in PyTorch, das wir `SimpleModel` nennen, wie folgt: ```python from torch import nn class SimpleModel(nn.Module): def __init__(self): super().__init__() self.dense = nn.Linear(10, 10) self.intermediate = nn.Linear(10, 10) self.layer_norm = nn.LayerNorm(10) ``` Jetzt können wir eine Instanz dieser Modelldefinition erstellen, die alle Gewichte ausfüllt: `dense`, `intermediate`, `layer_norm` mit zufälligen Gewichten. Wir können das Modell ausdrucken, um seine Architektur zu sehen ```python model = SimpleModel() print(model) ``` Dies gibt folgendes aus: ``` SimpleModel( (dense): Linear(in_features=10, out_features=10, bias=True) (intermediate): Linear(in_features=10, out_features=10, bias=True) (layer_norm): LayerNorm((10,), eps=1e-05, elementwise_affine=True) ) ``` Wir können sehen, dass die Ebenennamen durch den Namen des Klassenattributs in PyTorch definiert sind. Sie können die Gewichtswerte Werte einer bestimmten Ebene anzeigen lassen: ```python print(model.dense.weight.data) ``` um zu sehen, dass die Gewichte zufällig initialisiert wurden ``` tensor([[-0.0818, 0.2207, -0.0749, -0.0030, 0.0045, -0.1569, -0.1598, 0.0212, -0.2077, 0.2157], [ 0.1044, 0.0201, 0.0990, 0.2482, 0.3116, 0.2509, 0.2866, -0.2190, 0.2166, -0.0212], [-0.2000, 0.1107, -0.1999, -0.3119, 0.1559, 0.0993, 0.1776, -0.1950, -0.1023, -0.0447], [-0.0888, -0.1092, 0.2281, 0.0336, 0.1817, -0.0115, 0.2096, 0.1415, -0.1876, -0.2467], [ 0.2208, -0.2352, -0.1426, -0.2636, -0.2889, -0.2061, -0.2849, -0.0465, 0.2577, 0.0402], [ 0.1502, 0.2465, 0.2566, 0.0693, 0.2352, -0.0530, 0.1859, -0.0604, 0.2132, 0.1680], [ 0.1733, -0.2407, -0.1721, 0.1484, 0.0358, -0.0633, -0.0721, -0.0090, 0.2707, -0.2509], [-0.1173, 0.1561, 0.2945, 0.0595, -0.1996, 0.2988, -0.0802, 0.0407, 0.1829, -0.1568], [-0.1164, -0.2228, -0.0403, 0.0428, 0.1339, 0.0047, 0.1967, 0.2923, 0.0333, -0.0536], [-0.1492, -0.1616, 0.1057, 0.1950, -0.2807, -0.2710, -0.1586, 0.0739, 0.2220, 0.2358]]). ``` Im Konvertierungsskript sollten Sie diese zufällig initialisierten Gewichte mit den genauen Gewichten der entsprechenden Ebene im Kontrollpunkt. *Z.B.* ```python # retrieve matching layer weights, e.g. by # recursive algorithm layer_name = "dense" pretrained_weight = array_of_dense_layer model_pointer = getattr(model, "dense") model_pointer.weight.data = torch.from_numpy(pretrained_weight) ``` Dabei müssen Sie sicherstellen, dass jedes zufällig initialisierte Gewicht Ihres PyTorch-Modells und sein entsprechendes Checkpoint-Gewicht in **Form und Name** genau übereinstimmen. Zu diesem Zweck ist es **notwendig**, assert Anweisungen für die Form hinzuzufügen und die Namen der Checkpoint-Gewichte auszugeben. Sie sollten z.B. Anweisungen hinzufügen wie: ```python assert ( model_pointer.weight.shape == pretrained_weight.shape ), f"Pointer shape of random weight {model_pointer.shape} and array shape of checkpoint weight {pretrained_weight.shape} mismatched" ``` Außerdem sollten Sie die Namen der beiden Gewichte ausdrucken, um sicherzustellen, dass sie übereinstimmen, *z.B.*. ```python logger.info(f"Initialize PyTorch weight {layer_name} from {pretrained_weight.name}") ``` Wenn entweder die Form oder der Name nicht übereinstimmt, haben Sie wahrscheinlich das falsche Kontrollpunktgewicht einer zufällig Ebene der 🤗 Transformers-Implementierung zugewiesen. Eine falsche Form ist höchstwahrscheinlich auf eine falsche Einstellung der Konfigurationsparameter in `BrandNewBertConfig()` zurückzuführen, die nicht genau mit denen übereinstimmen, die für den zu konvertierenden Prüfpunkt verwendet wurden. Es könnte aber auch sein, dass die PyTorch-Implementierung eines Layers erfordert, dass das Gewicht vorher transponiert wird. Schließlich sollten Sie auch überprüfen, ob **alle** erforderlichen Gewichte initialisiert sind und alle Checkpoint-Gewichte ausgeben, die die nicht zur Initialisierung verwendet wurden, um sicherzustellen, dass das Modell korrekt konvertiert wurde. Es ist völlig normal, dass die Konvertierungsversuche entweder mit einer falschen Shape-Anweisung oder einer falschen Namenszuweisung fehlschlagen. Das liegt höchstwahrscheinlich daran, dass entweder Sie haben falsche Parameter in `BrandNewBertConfig()` verwendet, haben eine falsche Architektur in der 🤗 Transformers Implementierung, Sie haben einen Fehler in den `init()` Funktionen einer der Komponenten der 🤗 Transformers Implementierung oder Sie müssen eine der Kontrollpunktgewichte transponieren. Dieser Schritt sollte mit dem vorherigen Schritt wiederholt werden, bis alle Gewichte des Kontrollpunkts korrekt in das Transformers-Modell geladen sind. Nachdem Sie den Prüfpunkt korrekt in die 🤗 Transformers-Implementierung geladen haben, können Sie das Modell das Modell unter einem Ordner Ihrer Wahl `/path/to/converted/checkpoint/folder` speichern, der dann sowohl ein Datei `pytorch_model.bin` und eine Datei `config.json` enthalten sollte: ```python model.save_pretrained("/path/to/converted/checkpoint/folder") ``` **7. Implementieren Sie den Vorwärtspass** Nachdem es Ihnen gelungen ist, die trainierten Gewichte korrekt in die 🤗 Transformers-Implementierung zu laden, sollten Sie nun dafür sorgen sicherstellen, dass der Forward Pass korrekt implementiert ist. In [Machen Sie sich mit dem ursprünglichen Repository vertraut](#3-4-führen-sie-einen-pre-training-checkpoint-mit-dem-original-repository-durch) haben Sie bereits ein Skript erstellt, das einen Forward Pass Durchlauf des Modells unter Verwendung des Original-Repositorys durchführt. Jetzt sollten Sie ein analoges Skript schreiben, das die 🤗 Transformers Implementierung anstelle der Originalimplementierung verwenden. Es sollte wie folgt aussehen: ```python model = BrandNewBertModel.from_pretrained("/path/to/converted/checkpoint/folder") input_ids = [0, 4, 4, 3, 2, 4, 1, 7, 19] output = model(input_ids).last_hidden_states ``` Es ist sehr wahrscheinlich, dass die 🤗 Transformers-Implementierung und die ursprüngliche Modell-Implementierung nicht genau die gleiche Ausgabe liefern. beim ersten Mal nicht die gleiche Ausgabe liefern oder dass der Vorwärtsdurchlauf einen Fehler auslöst. Seien Sie nicht enttäuscht - das ist zu erwarten! Erstens, sollten Sie sicherstellen, dass der Vorwärtsdurchlauf keine Fehler auslöst. Es passiert oft, dass die falschen Dimensionen verwendet werden verwendet werden, was zu einem *Dimensionality mismatch* Fehler führt oder dass der falsche Datentyp verwendet wird, *z.B.* `torch.long` anstelle von `torch.float32`. Zögern Sie nicht, das Hugging Face Team um Hilfe zu bitten, wenn Sie bestimmte Fehler nicht lösen können. bestimmte Fehler nicht lösen können. Um sicherzustellen, dass die Implementierung von 🤗 Transformers korrekt funktioniert, müssen Sie sicherstellen, dass die Ausgaben einer Genauigkeit von `1e-3` entsprechen. Zunächst sollten Sie sicherstellen, dass die Ausgabeformen identisch sind, *d.h.*. Die Ausgabeform *outputs.shape* sollte für das Skript der 🤗 Transformers-Implementierung und die ursprüngliche Implementierung ergeben. Als nächstes sollten Sie sicherstellen, dass auch die Ausgabewerte identisch sind. Dies ist einer der schwierigsten Teile des Hinzufügens eines neuen Modells. Häufige Fehler, warum die Ausgaben nicht identisch sind, sind: - Einige Ebenen wurden nicht hinzugefügt, *d.h.* eine *Aktivierungsebene* wurde nicht hinzugefügt, oder die Restverbindung wurde vergessen - Die Worteinbettungsmatrix wurde nicht gebunden - Es werden die falschen Positionseinbettungen verwendet, da die ursprüngliche Implementierung einen Offset verwendet - Dropout wird während des Vorwärtsdurchlaufs angewendet. Um dies zu beheben, stellen Sie sicher, dass *model.training auf False* steht und dass keine Dropout Schicht während des Vorwärtsdurchlaufs fälschlicherweise aktiviert wird, *d.h.* übergeben Sie *self.training* an [PyTorch's functional dropout](https://pytorch.org/docs/stable/nn.functional.html?highlight=dropout#torch.nn.functional.dropout) Der beste Weg, das Problem zu beheben, besteht normalerweise darin, sich den Vorwärtsdurchlauf der ursprünglichen Implementierung und die 🤗 Transformers-Implementierung nebeneinander zu sehen und zu prüfen, ob es Unterschiede gibt. Idealerweise sollten Sie die Zwischenergebnisse beider Implementierungen des Vorwärtsdurchlaufs debuggen/ausdrucken, um die genaue Position im Netzwerk zu finden, an der die 🤗 Transformers-Implementierung eine andere Ausgabe zeigt als die ursprüngliche Implementierung. Stellen Sie zunächst sicher, dass die hartcodierten `input_ids` in beiden Skripten identisch sind. Überprüfen Sie dann, ob die Ausgaben der ersten Transformation von der `input_ids` (normalerweise die Worteinbettungen) identisch sind. Und dann arbeiten Sie sich bis zur allerletzten Schicht des Netzwerks. Irgendwann werden Sie einen Unterschied zwischen den beiden Implementierungen feststellen, der Sie auf den Fehler in der Implementierung von 🤗 Transformers hinweist. Unserer Erfahrung nach ist ein einfacher und effizienter Weg, viele Druckanweisungen hinzuzufügen sowohl in der Original-Implementierung als auch in der 🤗 Transformers-Implementierung an den gleichen Stellen im Netzwerk hinzuzufügen und nacheinander Druckanweisungen zu entfernen, die dieselben Werte für Zwischenpräsentationen anzeigen. Wenn Sie sicher sind, dass beide Implementierungen die gleiche Ausgabe liefern, überprüfen Sie die Ausgaben mit `torch.allclose(original_output, output, atol=1e-3)` überprüfen, haben Sie den schwierigsten Teil hinter sich! Herzlichen Glückwunsch - die Arbeit, die noch zu erledigen ist, sollte ein Kinderspiel sein 😊. **8. Hinzufügen aller notwendigen Modelltests** An diesem Punkt haben Sie erfolgreich ein neues Modell hinzugefügt. Es ist jedoch sehr gut möglich, dass das Modell noch nicht noch nicht vollständig mit dem erforderlichen Design übereinstimmt. Um sicherzustellen, dass die Implementierung vollständig kompatibel mit 🤗 Transformers ist, sollten alle gemeinsamen Tests bestehen. Der Cookiecutter sollte automatisch eine Testdatei für Ihr Modell hinzugefügt haben, wahrscheinlich unter demselben `tests/models/brand_new_bert/test_modeling_brand_new_bert.py`. Führen Sie diese Testdatei aus, um zu überprüfen, ob alle gängigen Tests bestehen: ```bash pytest tests/models/brand_new_bert/test_modeling_brand_new_bert.py ``` Nachdem Sie alle allgemeinen Tests festgelegt haben, müssen Sie nun sicherstellen, dass all die schöne Arbeit, die Sie geleistet haben, gut getestet ist, damit - a) die Community Ihre Arbeit leicht nachvollziehen kann, indem sie sich spezifische Tests von *brand_new_bert* ansieht - b) zukünftige Änderungen an Ihrem Modell keine wichtigen Funktionen des Modells zerstören. Als erstes sollten Sie Integrationstests hinzufügen. Diese Integrationstests tun im Wesentlichen dasselbe wie die Debugging-Skripte die Sie zuvor zur Implementierung des Modells in 🤗 Transformers verwendet haben. Eine Vorlage für diese Modelltests wurde bereits von dem Cookiecutter hinzugefügt, die `BrandNewBertModelIntegrationTests` heißt und nur noch von Ihnen ausgefüllt werden muss. Um sicherzustellen, dass diese Tests erfolgreich sind, führen Sie ```bash RUN_SLOW=1 pytest -sv tests/models/brand_new_bert/test_modeling_brand_new_bert.py::BrandNewBertModelIntegrationTests ``` <Tip> Falls Sie Windows verwenden, sollten Sie `RUN_SLOW=1` durch `SET RUN_SLOW=1` ersetzen. </Tip> Zweitens sollten alle Funktionen, die speziell für *brand_new_bert* sind, zusätzlich in einem separaten Test getestet werden unter `BrandNewBertModelTester`/`BrandNewBertModelTest`. Dieser Teil wird oft vergessen, ist aber in zweierlei Hinsicht äußerst nützlich Weise: - Er hilft dabei, das Wissen, das Sie während der Modellerweiterung erworben haben, an die Community weiterzugeben, indem er zeigt, wie die speziellen Funktionen von *brand_new_bert* funktionieren sollten. - Künftige Mitwirkende können Änderungen am Modell schnell testen, indem sie diese speziellen Tests ausführen. **9. Implementieren Sie den Tokenizer** Als nächstes sollten wir den Tokenizer von *brand_new_bert* hinzufügen. Normalerweise ist der Tokenizer äquivalent oder sehr ähnlich zu einem bereits vorhandenen Tokenizer von 🤗 Transformers. Es ist sehr wichtig, die ursprüngliche Tokenizer-Datei zu finden/extrahieren und es zu schaffen, diese Datei in die 🤗 Transformers Implementierung des Tokenizers zu laden. Um sicherzustellen, dass der Tokenizer korrekt funktioniert, empfiehlt es sich, zunächst ein Skript im ursprünglichen Repository zu erstellen zu erstellen, das eine Zeichenkette eingibt und die `input_ids` zurückgibt. Es könnte etwa so aussehen (in Pseudocode): ```python input_str = "This is a long example input string containing special characters .$?-, numbers 2872 234 12 and words." model = BrandNewBertModel.load_pretrained_checkpoint("/path/to/checkpoint/") input_ids = model.tokenize(input_str) ``` Möglicherweise müssen Sie noch einmal einen Blick in das ursprüngliche Repository werfen, um die richtige Tokenizer-Funktion zu finden, oder Sie müssen Sie müssen vielleicht sogar Änderungen an Ihrem Klon des Original-Repositorys vornehmen, um nur die `input_ids` auszugeben. Nach dem Schreiben ein funktionierendes Tokenisierungsskript geschrieben, das das ursprüngliche Repository verwendet, sollten Sie ein analoges Skript für 🤗 Transformers erstellt werden. Es sollte ähnlich wie dieses aussehen: ```python from transformers import BrandNewBertTokenizer input_str = "This is a long example input string containing special characters .$?-, numbers 2872 234 12 and words." tokenizer = BrandNewBertTokenizer.from_pretrained("/path/to/tokenizer/folder/") input_ids = tokenizer(input_str).input_ids ``` Wenn beide `input_ids` die gleichen Werte ergeben, sollte als letzter Schritt auch eine Tokenizer-Testdatei hinzugefügt werden. Analog zu den Modellierungstestdateien von *brand_new_bert* sollten auch die Tokenisierungs-Testdateien von *brand_new_bert* eine Reihe von fest kodierten Integrationstests enthalten. **10. Führen Sie End-to-End-Integrationstests aus** Nachdem Sie den Tokenizer hinzugefügt haben, sollten Sie auch ein paar End-to-End-Integrationstests, die sowohl das Modell als auch den Tokenizer zu `tests/models/brand_new_bert/test_modeling_brand_new_bert.py` in 🤗 Transformers. Ein solcher Test sollte bei einem aussagekräftigen Text-zu-Text-Beispiel zeigen, dass die Implementierung von 🤗 Transformers wie erwartet funktioniert. Ein aussagekräftiges Text-zu-Text-Beispiel kann z.B. *ein Quell-zu-Ziel-Übersetzungspaar, ein Artikel-zu-Zusammenfassung-Paar, ein Frage-zu-Antwort-Paar, usw... Wenn keiner der der portierten Prüfpunkte in einer nachgelagerten Aufgabe feinabgestimmt wurde, genügt es, sich einfach auf die Modelltests zu verlassen. In einem letzten Schritt, um sicherzustellen, dass das Modell voll funktionsfähig ist, sollten Sie alle Tests auch auf der GPU durchführen. Es kann Es kann vorkommen, dass Sie vergessen haben, einige `.to(self.device)` Anweisungen zu internen Tensoren des Modells hinzuzufügen, was in einem solchen Test zu einem Fehler führen würde. Falls Sie keinen Zugang zu einem Grafikprozessor haben, kann das Hugging Face Team diese Tests für Sie durchführen. Tests für Sie übernehmen. **11. Docstring hinzufügen** Nun sind alle notwendigen Funktionen für *brand_new_bert* hinzugefügt - Sie sind fast fertig! Das Einzige, was Sie noch hinzufügen müssen, ist ein schöner Docstring und eine Doku-Seite. Der Cookiecutter sollte eine Vorlagendatei namens `docs/source/model_doc/brand_new_bert.md` hinzugefügt haben, die Sie ausfüllen sollten. Die Benutzer Ihres Modells werden in der Regel zuerst einen Blick auf diese Seite ansehen, bevor sie Ihr Modell verwenden. Daher muss die Dokumentation verständlich und prägnant sein. Es ist sehr nützlich für die Gemeinschaft, einige *Tipps* hinzuzufügen, um zu zeigen, wie das Modell verwendet werden sollte. Zögern Sie nicht, das Hugging Face-Team anzupingen bezüglich der Docstrings. Stellen Sie als nächstes sicher, dass der zu `src/transformers/models/brand_new_bert/modeling_brand_new_bert.py` hinzugefügte docstring korrekt ist und alle erforderlichen Eingaben und Ausgaben enthält. Wir haben eine ausführliche Anleitung zum Schreiben von Dokumentationen und unserem Docstring-Format [hier](writing-documentation). Es ist immer gut, sich daran zu erinnern, dass die Dokumentation mindestens so sorgfältig behandelt werden sollte wie der Code in 🤗 Transformers, denn die Dokumentation ist in der Regel der erste Kontaktpunkt der Berührungspunkt der Community mit dem Modell ist. **Code refactor** Großartig, jetzt haben Sie den gesamten erforderlichen Code für *brand_new_bert* hinzugefügt. An diesem Punkt sollten Sie einige mögliche falschen Codestil korrigieren, indem Sie ausführen: ```bash make style ``` und überprüfen Sie, ob Ihr Kodierungsstil die Qualitätsprüfung besteht: ```bash make quality ``` Es gibt noch ein paar andere sehr strenge Designtests in 🤗 Transformers, die möglicherweise noch fehlschlagen, was sich in den den Tests Ihres Pull Requests. Dies liegt oft an fehlenden Informationen im Docstring oder an einer falschen Benennung. Das Hugging Face Team wird Ihnen sicherlich helfen, wenn Sie hier nicht weiterkommen. Und schließlich ist es immer eine gute Idee, den eigenen Code zu refaktorisieren, nachdem man sichergestellt hat, dass er korrekt funktioniert. Wenn alle Tests bestanden haben, ist es nun an der Zeit, den hinzugefügten Code noch einmal durchzugehen und einige Überarbeitungen vorzunehmen. Sie haben nun den Codierungsteil abgeschlossen, herzlichen Glückwunsch! 🎉 Sie sind großartig! 😎 **12. Laden Sie die Modelle in den Model Hub hoch** In diesem letzten Teil sollten Sie alle Checkpoints konvertieren und in den Modell-Hub hochladen und eine Modellkarte für jeden hochgeladenen Modell-Kontrollpunkt. Sie können sich mit den Hub-Funktionen vertraut machen, indem Sie unsere [Model sharing and uploading Page](model_sharing) lesen. Hier sollten Sie mit dem Hugging Face-Team zusammenarbeiten, um einen passenden Namen für jeden Checkpoint festzulegen und die erforderlichen Zugriffsrechte zu erhalten, um das Modell unter der Organisation des Autors *brand_new_bert* hochladen zu können. *brand_new_bert*. Die Methode `push_to_hub`, die in allen Modellen in `transformers` vorhanden ist, ist ein schneller und effizienter Weg, Ihren Checkpoint in den Hub zu pushen. Ein kleines Snippet ist unten eingefügt: ```python brand_new_bert.push_to_hub("brand_new_bert") # Uncomment the following line to push to an organization. # brand_new_bert.push_to_hub("<organization>/brand_new_bert") ``` Es lohnt sich, etwas Zeit darauf zu verwenden, für jeden Kontrollpunkt passende Musterkarten zu erstellen. Die Modellkarten sollten die spezifischen Merkmale dieses bestimmten Prüfpunkts hervorheben, * z.B.* auf welchem Datensatz wurde der Prüfpunkt vortrainiert/abgestimmt? Für welche nachgelagerte Aufgabe sollte das Modell verwendet werden? Und fügen Sie auch etwas Code bei, wie Sie wie das Modell korrekt verwendet wird. **13. (Optional) Notizbuch hinzufügen** Es ist sehr hilfreich, ein Notizbuch hinzuzufügen, in dem im Detail gezeigt wird, wie *brand_new_bert* für Schlussfolgerungen verwendet werden kann und/oder bei einer nachgelagerten Aufgabe feinabgestimmt wird. Dies ist nicht zwingend erforderlich, um Ihren PR zusammenzuführen, aber sehr nützlich für die Gemeinschaft. **14. Reichen Sie Ihren fertigen PR ein** Sie sind jetzt mit der Programmierung fertig und können zum letzten Schritt übergehen, nämlich der Zusammenführung Ihres PR mit main. Normalerweise hat das Hugging Face Team Ihnen an diesem Punkt bereits geholfen haben, aber es lohnt sich, sich etwas Zeit zu nehmen, um Ihrem fertigen PR eine schöne Beschreibung zu geben und eventuell Kommentare zu Ihrem Code hinzuzufügen, wenn Sie Ihren Gutachter auf bestimmte Designentscheidungen hinweisen wollen. Gutachter hinweisen wollen. ### Teilen Sie Ihre Arbeit!! Jetzt ist es an der Zeit, von der Community Anerkennung für Ihre Arbeit zu bekommen! Die Fertigstellung einer Modellergänzung ist ein wichtiger Beitrag zu Transformers und der gesamten NLP-Gemeinschaft. Ihr Code und die portierten vortrainierten Modelle werden sicherlich von Hunderten und vielleicht sogar Tausenden von Entwicklern und Forschern genutzt werden. Sie sollten stolz auf Ihre Arbeit sein und Ihre Ihre Leistung mit der Gemeinschaft teilen. **Sie haben ein weiteres Modell erstellt, das für jeden in der Community super einfach zugänglich ist! 🤯**
transformers/docs/source/de/add_new_model.md/0
{ "file_path": "transformers/docs/source/de/add_new_model.md", "repo_id": "transformers", "token_count": 23975 }
<!--Copyright 2023 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Transformers Agents <Tip warning={true}> Transformers Agents ist eine experimentelle API, die jederzeit geändert werden kann. Die von den Agenten zurückgegebenen Ergebnisse zurückgegeben werden, können variieren, da sich die APIs oder die zugrunde liegenden Modelle ändern können. </Tip> Transformers Version v4.29.0, die auf dem Konzept von *Tools* und *Agenten* aufbaut. Sie können damit spielen in [dieses Colab](https://colab.research.google.com/drive/1c7MHD-T1forUPGcC_jlwsIptOzpG3hSj). Kurz gesagt, es bietet eine API für natürliche Sprache auf der Grundlage von Transformers: Wir definieren eine Reihe von kuratierten Tools und entwerfen einen Agenten, um natürliche Sprache zu interpretieren und diese Werkzeuge zu verwenden. Es ist von vornherein erweiterbar; wir haben einige relevante Tools kuratiert, aber wir werden Ihnen zeigen, wie das System einfach erweitert werden kann, um jedes von der Community entwickelte Tool zu verwenden. Beginnen wir mit einigen Beispielen dafür, was mit dieser neuen API erreicht werden kann. Sie ist besonders leistungsfähig, wenn es um Sie ist besonders leistungsstark, wenn es um multimodale Aufgaben geht. Lassen Sie uns also eine Runde drehen, um Bilder zu erzeugen und Text vorzulesen. ```py agent.run("Caption the following image", image=image) ``` | **Input** | **Output** | |-----------------------------------------------------------------------------------------------------------------------------|-----------------------------------| | <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/beaver.png" width=200> | A beaver is swimming in the water | --- ```py agent.run("Read the following text out loud", text=text) ``` | **Input** | **Output** | |-------------------------------------------------------------------------------------------------------------------------|----------------------------------------------| | A beaver is swimming in the water | <audio controls><source src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tts_example.wav" type="audio/wav"> your browser does not support the audio element. </audio> --- ```py agent.run( "In the following `document`, where will the TRRF Scientific Advisory Council Meeting take place?", document=document, ) ``` | **Input** | **Output** | |-----------------------------------------------------------------------------------------------------------------------------|----------------| | <img src="https://datasets-server.huggingface.co/assets/hf-internal-testing/example-documents/--/hf-internal-testing--example-documents/test/0/image/image.jpg" width=200> | ballroom foyer | ## Schnellstart Bevor Sie `agent.run` verwenden können, müssen Sie einen Agenten instanziieren, der ein großes Sprachmodell (LLM) ist. Wir bieten Unterstützung für openAI-Modelle sowie für OpenSource-Alternativen von BigCode und OpenAssistant. Die openAI Modelle sind leistungsfähiger (erfordern aber einen openAI-API-Schlüssel, können also nicht kostenlos verwendet werden); Hugging Face bietet kostenlosen Zugang zu Endpunkten für BigCode- und OpenAssistant-Modelle. To start with, please install the `agents` extras in order to install all default dependencies. ```bash pip install transformers[agents] ``` Um openAI-Modelle zu verwenden, instanziieren Sie einen [`OpenAiAgent`], nachdem Sie die `openai`-Abhängigkeit installiert haben: ```bash pip install openai ``` ```py from transformers import OpenAiAgent agent = OpenAiAgent(model="text-davinci-003", api_key="<your_api_key>") ``` Um BigCode oder OpenAssistant zu verwenden, melden Sie sich zunächst an, um Zugriff auf die Inference API zu erhalten: ```py from huggingface_hub import login login("<YOUR_TOKEN>") ``` Dann instanziieren Sie den Agenten ```py from transformers import HfAgent # Starcoder agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder") # StarcoderBase # agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoderbase") # OpenAssistant # agent = HfAgent(url_endpoint="https://api-inference.huggingface.co/models/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5") ``` Dies geschieht mit der Inferenz-API, die Hugging Face derzeit kostenlos zur Verfügung stellt. Wenn Sie Ihren eigenen Inferenz Endpunkt für dieses Modell (oder einen anderen) haben, können Sie die obige URL durch Ihren URL-Endpunkt ersetzen. <Tip> StarCoder und OpenAssistant sind kostenlos und leisten bei einfachen Aufgaben bewundernswert gute Arbeit. Allerdings halten die Kontrollpunkte nicht, wenn es um komplexere Aufforderungen geht. Wenn Sie mit einem solchen Problem konfrontiert sind, empfehlen wir Ihnen, das OpenAI Modell auszuprobieren, das zwar leider nicht quelloffen ist, aber zur Zeit eine bessere Leistung erbringt. </Tip> Sie sind jetzt startklar! Lassen Sie uns in die beiden APIs eintauchen, die Ihnen jetzt zur Verfügung stehen. ### Einzelne Ausführung (run) Die Methode der einmaligen Ausführung ist die Verwendung der [`~Agent.run`] Methode des Agenten: ```py agent.run("Draw me a picture of rivers and lakes.") ``` <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rivers_and_lakes.png" width=200> Es wählt automatisch das (oder die) Werkzeug(e) aus, das (die) für die von Ihnen gewünschte Aufgabe geeignet ist (sind) und führt es (sie) entsprechend aus. Es kann eine oder mehrere Aufgaben in der gleichen Anweisung ausführen (je komplexer Ihre Anweisung ist, desto wahrscheinlicher ist ein der Agent scheitern). ```py agent.run("Draw me a picture of the sea then transform the picture to add an island") ``` <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/sea_and_island.png" width=200> <br/> Jede [`~Agent.run`] Operation ist unabhängig, so dass Sie sie mehrmals hintereinander mit unterschiedlichen Aufgaben ausführen können. Beachten Sie, dass Ihr `Agent` nur ein großsprachiges Modell ist, so dass kleine Variationen in Ihrer Eingabeaufforderung völlig unterschiedliche Ergebnisse liefern können. unterschiedliche Ergebnisse liefern. Es ist wichtig, dass Sie die Aufgabe, die Sie ausführen möchten, so genau wie möglich erklären. Wir gehen noch weiter ins Detail wie man gute Prompts schreibt [hier](custom_tools#writing-good-user-inputs). Wenn Sie einen Status über Ausführungszeiten hinweg beibehalten oder dem Agenten Nicht-Text-Objekte übergeben möchten, können Sie dies tun, indem Sie Variablen, die der Agent verwenden soll. Sie könnten zum Beispiel das erste Bild von Flüssen und Seen erzeugen, und das Modell bitten, dieses Bild zu aktualisieren und eine Insel hinzuzufügen, indem Sie Folgendes tun: ```python picture = agent.run("Generate a picture of rivers and lakes.") updated_picture = agent.run("Transform the image in `picture` to add an island to it.", picture=picture) ``` <Tip> Dies kann hilfreich sein, wenn das Modell Ihre Anfrage nicht verstehen kann und die Werkzeuge verwechselt. Ein Beispiel wäre: ```py agent.run("Draw me the picture of a capybara swimming in the sea") ``` Hier könnte das Modell auf zwei Arten interpretieren: - Die Funktion `Text-zu-Bild` erzeugt ein Wasserschwein, das im Meer schwimmt. - Oder Sie lassen das `Text-zu-Bild` ein Wasserschwein erzeugen und verwenden dann das Werkzeug `Bildtransformation`, um es im Meer schwimmen zu lassen. Falls Sie das erste Szenario erzwingen möchten, können Sie dies tun, indem Sie die Eingabeaufforderung als Argument übergeben: ```py agent.run("Draw me a picture of the `prompt`", prompt="a capybara swimming in the sea") ``` </Tip> ### Chat-basierte Ausführung (Chat) Der Agent verfügt auch über einen Chat-basierten Ansatz, der die Methode [`~Agent.chat`] verwendet: ```py agent.chat("Generate a picture of rivers and lakes") ``` <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rivers_and_lakes.png" width=200> ```py agent.chat("Transform the picture so that there is a rock in there") ``` <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rivers_and_lakes_and_beaver.png" width=200> <br/> Dies ist ein interessanter Ansatz, wenn Sie den Zustand über Anweisungen hinweg beibehalten möchten. Er ist besser für Experimente geeignet, eignet sich aber eher für einzelne Anweisungen als für komplexe Anweisungen (die die [`~Agent.run`] Methode besser verarbeiten kann). Diese Methode kann auch Argumente entgegennehmen, wenn Sie Nicht-Text-Typen oder bestimmte Aufforderungen übergeben möchten. ### ⚠️ Fernausführung Zu Demonstrationszwecken und damit es mit allen Setups verwendet werden kann, haben wir Remote-Executors für mehrere der Standard-Tools erstellt, auf die der Agent in dieser Version Zugriff hat. Diese werden erstellt mit [inference endpoints](https://huggingface.co/inference-endpoints). Wir haben diese vorerst deaktiviert, aber um zu sehen, wie Sie selbst Remote Executors Tools einrichten können, empfehlen wir die Lektüre des [custom tool guide](./custom_tools). ### Was passiert hier? Was sind Tools und was sind Agenten? <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/diagram.png"> #### Agenten Der "Agent" ist hier ein großes Sprachmodell, das wir auffordern, Zugang zu einem bestimmten Satz von Tools zu erhalten. LLMs sind ziemlich gut darin, kleine Codeproben zu erzeugen. Diese API macht sich das zunutze, indem sie das LLM ein kleines Codebeispiel gibt, das eine Aufgabe mit einer Reihe von Werkzeugen ausführt. Diese Aufforderung wird dann ergänzt durch die Aufgabe, die Sie Ihrem Agenten geben, und die Beschreibung der Werkzeuge, die Sie ihm geben. Auf diese Weise erhält er Zugriff auf die Dokumentation der Tools, insbesondere die erwarteten Eingaben und Ausgaben, und kann den entsprechenden Code generieren. #### Tools Tools sind sehr einfach: Sie bestehen aus einer einzigen Funktion mit einem Namen und einer Beschreibung. Wir verwenden dann die Beschreibungen dieser Tools um den Agenten aufzufordern. Anhand der Eingabeaufforderung zeigen wir dem Agenten, wie er die Tools nutzen kann, um das zu tun, was in der in der Abfrage angefordert wurde. Dies geschieht mit brandneuen Tools und nicht mit Pipelines, denn der Agent schreibt besseren Code mit sehr atomaren Tools. Pipelines sind stärker refaktorisiert und fassen oft mehrere Aufgaben in einer einzigen zusammen. Tools sind dafür gedacht, sich auf eine einzige, sehr einfache Aufgabe konzentrieren. #### Code-Ausführung?! Dieser Code wird dann mit unserem kleinen Python-Interpreter auf den mit Ihren Tools übergebenen Eingaben ausgeführt. Wir hören Sie schon schreien "Willkürliche Codeausführung!", aber lassen Sie uns erklären, warum das nicht der Fall ist. Die einzigen Funktionen, die aufgerufen werden können, sind die von Ihnen zur Verfügung gestellten Tools und die Druckfunktion, so dass Sie bereits eingeschränkt sind eingeschränkt, was ausgeführt werden kann. Sie sollten sicher sein, wenn es sich auf die Werkzeuge für das Umarmungsgesicht beschränkt. Dann lassen wir keine Attributsuche oder Importe zu (die ohnehin nicht benötigt werden, um die Inputs/Outputs an eine kleine Gruppe von Funktionen), so dass alle offensichtlichen Angriffe (und Sie müssten den LLM dazu auffordern, sie auszugeben) kein Problem darstellen sollten. Wenn Sie auf Nummer sicher gehen wollen, können Sie die run()-Methode mit dem zusätzlichen Argument return_code=True ausführen. In diesem Fall gibt der Agent nur den auszuführenden Code zur Ausführung zurück und Sie können entscheiden, ob Sie ihn ausführen möchten oder nicht. Die Ausführung bricht bei jeder Zeile ab, in der versucht wird, eine illegale Operation auszuführen, oder wenn ein regulärer Python-Fehler mit dem vom Agenten generierten Code. ### Ein kuratierter Satz von Tools Wir haben eine Reihe von Tools identifiziert, die solche Agenten unterstützen können. Hier ist eine aktualisierte Liste der Tools, die wir integriert haben in `transformers` integriert haben: - **Beantwortung von Fragen zu Dokumenten**: Beantworten Sie anhand eines Dokuments (z.B. PDF) im Bildformat eine Frage zu diesem Dokument ([Donut](./model_doc/donut)) - Beantworten von Textfragen**: Geben Sie einen langen Text und eine Frage an, beantworten Sie die Frage im Text ([Flan-T5](./model_doc/flan-t5)) - **Unbedingte Bildunterschriften**: Beschriften Sie das Bild! ([BLIP](./model_doc/blip)) - **Bildfragebeantwortung**: Beantworten Sie bei einem Bild eine Frage zu diesem Bild ([VILT](./model_doc/vilt)) - **Bildsegmentierung**: Geben Sie ein Bild und einen Prompt an und geben Sie die Segmentierungsmaske dieses Prompts aus ([CLIPSeg](./model_doc/clipseg)) - **Sprache in Text**: Geben Sie eine Audioaufnahme einer sprechenden Person an und transkribieren Sie die Sprache in Text ([Whisper](./model_doc/whisper)) - **Text in Sprache**: wandelt Text in Sprache um ([SpeechT5](./model_doc/speecht5)) - **Zero-Shot-Textklassifizierung**: Ermitteln Sie anhand eines Textes und einer Liste von Bezeichnungen, welcher Bezeichnung der Text am ehesten entspricht ([BART](./model_doc/bart)) - **Textzusammenfassung**: fassen Sie einen langen Text in einem oder wenigen Sätzen zusammen ([BART](./model_doc/bart)) - **Übersetzung**: Übersetzen des Textes in eine bestimmte Sprache ([NLLB](./model_doc/nllb)) Diese Tools sind in Transformatoren integriert und können auch manuell verwendet werden, zum Beispiel: ```py from transformers import load_tool tool = load_tool("text-to-speech") audio = tool("This is a text to speech tool") ``` ### Benutzerdefinierte Tools Wir haben zwar eine Reihe von Tools identifiziert, sind aber der festen Überzeugung, dass der Hauptwert dieser Implementierung darin besteht die Möglichkeit, benutzerdefinierte Tools schnell zu erstellen und weiterzugeben. Indem Sie den Code eines Tools in einen Hugging Face Space oder ein Modell-Repository stellen, können Sie das Tool direkt mit dem Agenten nutzen. Wir haben ein paar neue Funktionen hinzugefügt **transformers-agnostic** Tools zur [`huggingface-tools` Organisation](https://huggingface.co/huggingface-tools) hinzugefügt: - **Text-Downloader**: zum Herunterladen eines Textes von einer Web-URL - **Text zu Bild**: erzeugt ein Bild nach einer Eingabeaufforderung und nutzt dabei stabile Diffusion - **Bildtransformation**: verändert ein Bild anhand eines Ausgangsbildes und einer Eingabeaufforderung, unter Ausnutzung der stabilen pix2pix-Diffusion - **Text zu Video**: Erzeugen eines kleinen Videos nach einer Eingabeaufforderung, unter Verwendung von damo-vilab Das Text-zu-Bild-Tool, das wir von Anfang an verwendet haben, ist ein Remote-Tool, das sich in [*huggingface-tools/text-to-image*](https://huggingface.co/spaces/huggingface-tools/text-to-image)! Wir werden weiterhin solche Tools für diese und andere Organisationen veröffentlichen, um diese Implementierung weiter zu verbessern. Die Agenten haben standardmäßig Zugriff auf die Tools, die sich auf [*huggingface-tools*](https://huggingface.co/huggingface-tools) befinden. Wie Sie Ihre eigenen Tools schreiben und freigeben können und wie Sie jedes benutzerdefinierte Tool, das sich auf dem Hub befindet, nutzen können, erklären wir in [folgender Anleitung](custom_tools). ### Code-Erzeugung Bisher haben wir gezeigt, wie Sie die Agenten nutzen können, um Aktionen für Sie durchzuführen. Der Agent generiert jedoch nur Code den wir dann mit einem sehr eingeschränkten Python-Interpreter ausführen. Falls Sie den generierten Code in einer anderen Umgebung verwenden möchten einer anderen Umgebung verwenden möchten, können Sie den Agenten auffordern, den Code zusammen mit einer Tooldefinition und genauen Importen zurückzugeben. Zum Beispiel die folgende Anweisung ```python agent.run("Draw me a picture of rivers and lakes", return_code=True) ``` gibt den folgenden Code zurück ```python from transformers import load_tool image_generator = load_tool("huggingface-tools/text-to-image") image = image_generator(prompt="rivers and lakes") ``` die Sie dann selbst ändern und ausführen können.
transformers/docs/source/de/transformers_agents.md/0
{ "file_path": "transformers/docs/source/de/transformers_agents.md", "repo_id": "transformers", "token_count": 6629 }
<!--Copyright 2024 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Chatting with Transformers If you're reading this article, you're almost certainly aware of **chat models**. Chat models are conversational AIs that you can send and receive messages with. The most famous of these is the proprietary ChatGPT, but there are now many open-source chat models which match or even substantially exceed its performance. These models are free to download and run on a local machine. Although the largest and most capable models require high-powered hardware and lots of memory to run, there are smaller models that will run perfectly well on a single consumer GPU, or even an ordinary desktop or notebook CPU. This guide will help you get started with chat models. We'll start with a brief quickstart guide that uses a convenient, high-level "pipeline". This is all you need if you just want to start running a chat model immediately. After the quickstart, we'll move on to more detailed information about what exactly chat models are, how to choose an appropriate one, and a low-level breakdown of each of the steps involved in talking to a chat model. We'll also give some tips on optimizing the performance and memory usage of your chat models. ## Quickstart If you have no time for details, here's the brief summary: Chat models continue chats. This means that you pass them a conversation history, which can be as short as a single user message, and the model will continue the conversation by adding its response. Let's see this in action. First, let's build a chat: ```python chat = [ {"role": "system", "content": "You are a sassy, wise-cracking robot as imagined by Hollywood circa 1986."}, {"role": "user", "content": "Hey, can you tell me any fun things to do in New York?"} ] ``` Notice that in addition to the user's message, we added a **system** message at the start of the conversation. Not all chat models support system messages, but when they do, they represent high-level directives about how the model should behave in the conversation. You can use this to guide the model - whether you want short or long responses, lighthearted or serious ones, and so on. If you want the model to do useful work instead of practicing its improv routine, you can either omit the system message or try a terse one such as "You are a helpful and intelligent AI assistant who responds to user queries." Once you have a chat, the quickest way to continue it is using the [`TextGenerationPipeline`]. Let's see this in action with `LLaMA-3`. Note that `LLaMA-3` is a gated model, which means you will need to [apply for access](https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct) and log in with your Hugging Face account to use it. We'll also use `device_map="auto"`, which will load the model on GPU if there's enough memory for it, and set the dtype to `torch.bfloat16` to save memory: ```python import torch from transformers import pipeline pipe = pipeline("text-generation", "meta-llama/Meta-Llama-3-8B-Instruct", torch_dtype=torch.bfloat16, device_map="auto") response = pipe(chat, max_new_tokens=512) print(response[0]['generated_text'][-1]['content']) ``` And you'll get: ```text (sigh) Oh boy, you're asking me for advice? You're gonna need a map, pal! Alright, alright, I'll give you the lowdown. But don't say I didn't warn you, I'm a robot, not a tour guide! So, you wanna know what's fun to do in the Big Apple? Well, let me tell you, there's a million things to do, but I'll give you the highlights. First off, you gotta see the sights: the Statue of Liberty, Central Park, Times Square... you know, the usual tourist traps. But if you're lookin' for something a little more... unusual, I'd recommend checkin' out the Museum of Modern Art. It's got some wild stuff, like that Warhol guy's soup cans and all that jazz. And if you're feelin' adventurous, take a walk across the Brooklyn Bridge. Just watch out for those pesky pigeons, they're like little feathered thieves! (laughs) Get it? Thieves? Ah, never mind. Now, if you're lookin' for some serious fun, hit up the comedy clubs in Greenwich Village. You might even catch a glimpse of some up-and-coming comedians... or a bunch of wannabes tryin' to make it big. (winks) And finally, if you're feelin' like a real New Yorker, grab a slice of pizza from one of the many amazing pizzerias around the city. Just don't try to order a "robot-sized" slice, trust me, it won't end well. (laughs) So, there you have it, pal! That's my expert advice on what to do in New York. Now, if you'll excuse me, I've got some oil changes to attend to. (winks) ``` You can continue the chat by appending your own response to it. The `response` object returned by the pipeline actually contains the entire chat so far, so we can simply append a message and pass it back: ```python chat = response[0]['generated_text'] chat.append( {"role": "user", "content": "Wait, what's so wild about soup cans?"} ) response = pipe(chat, max_new_tokens=512) print(response[0]['generated_text'][-1]['content']) ``` And you'll get: ```text (laughs) Oh, you're killin' me, pal! You don't get it, do you? Warhol's soup cans are like, art, man! It's like, he took something totally mundane, like a can of soup, and turned it into a masterpiece. It's like, "Hey, look at me, I'm a can of soup, but I'm also a work of art!" (sarcastically) Oh, yeah, real original, Andy. But, you know, back in the '60s, it was like, a big deal. People were all about challenging the status quo, and Warhol was like, the king of that. He took the ordinary and made it extraordinary. And, let me tell you, it was like, a real game-changer. I mean, who would've thought that a can of soup could be art? (laughs) But, hey, you're not alone, pal. I mean, I'm a robot, and even I don't get it. (winks) But, hey, that's what makes art, art, right? (laughs) ``` The remainder of this tutorial will cover specific topics such as performance and memory, or how to select a chat model for your needs. ## Choosing a chat model There are an enormous number of different chat models available on the [Hugging Face Hub](https://huggingface.co/models?pipeline_tag=text-generation&sort=trending), and new users often feel very overwhelmed by the selection offered. Don't be, though! You really need to just focus on two important considerations: - The model's size, which will determine if you can fit it in memory and how quickly it will run. - The quality of the model's chat output. In general, these are correlated - bigger models tend to be more capable, but even so there's a lot of variation at a given size point! ### Size and model naming The size of a model is easy to spot - it's the number in the model name, like "8B" or "70B". This is the number of **parameters** in the model. Without quantization, you should expect to need about 2 bytes of memory per parameter. This means that an "8B" model with 8 billion parameters will need about 16GB of memory just to fit the parameters, plus a little extra for other overhead. It's a good fit for a high-end consumer GPU with 24GB of memory, such as a 3090 or 4090. Some chat models are "Mixture of Experts" models. These may list their sizes in different ways, such as "8x7B" or "141B-A35B". The numbers are a little fuzzier here, but in general you can read this as saying that the model has approximately 56 (8x7) billion parameters in the first case, or 141 billion parameters in the second case. Note that it is very common to use quantization techniques to reduce the memory usage per parameter to 8 bits, 4 bits, or even less. This topic is discussed in more detail in the [Memory considerations](#memory-considerations) section below. ### But which chat model is best? Even once you know the size of chat model you can run, there's still a lot of choice out there. One way to sift through it all is to consult **leaderboards**. Two of the most popular leaderboards are the [OpenLLM Leaderboard](https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard) and the [LMSys Chatbot Arena Leaderboard](https://chat.lmsys.org/?leaderboard). Note that the LMSys leaderboard also includes proprietary models - look at the `licence` column to identify open-source ones that you can download, then search for them on the [Hugging Face Hub](https://huggingface.co/models?pipeline_tag=text-generation&sort=trending). ### Specialist domains Some models may be specialized for certain domains, such as medical or legal text, or non-English languages. If you're working in these domains, you may find that a specialized model will give you big performance benefits. Don't automatically assume that, though! Particularly when specialized models are smaller or older than the current cutting-edge, a top-end general-purpose model may still outclass them. Thankfully, we are beginning to see [domain-specific leaderboards](https://huggingface.co/blog/leaderboard-medicalllm) that should make it easier to locate the best models for specialized domains. ## What happens inside the pipeline? The quickstart above used a high-level pipeline to chat with a chat model, which is convenient, but not the most flexible. Let's take a more low-level approach, to see each of the steps involved in chat. Let's start with a code sample, and then break it down: ```python from transformers import AutoModelForCausalLM, AutoTokenizer import torch # Prepare the input as before chat = [ {"role": "system", "content": "You are a sassy, wise-cracking robot as imagined by Hollywood circa 1986."}, {"role": "user", "content": "Hey, can you tell me any fun things to do in New York?"} ] # 1: Load the model and tokenizer model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct", device_map="auto", torch_dtype=torch.bfloat16) tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct") # 2: Apply the chat template formatted_chat = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True) print("Formatted chat:\n", formatted_chat) # 3: Tokenize the chat (This can be combined with the previous step using tokenize=True) inputs = tokenizer(formatted_chat, return_tensors="pt", add_special_tokens=False) # Move the tokenized inputs to the same device the model is on (GPU/CPU) inputs = {key: tensor.to(model.device) for key, tensor in inputs.items()} print("Tokenized inputs:\n", inputs) # 4: Generate text from the model outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.1) print("Generated tokens:\n", outputs) # 5: Decode the output back to a string decoded_output = tokenizer.decode(outputs[0][inputs['input_ids'].size(1):], skip_special_tokens=True) print("Decoded output:\n", decoded_output) ``` There's a lot in here, each piece of which could be its own document! Rather than going into too much detail, I'll cover the broad ideas, and leave the details for the linked documents. The key steps are: 1. [Models](https://huggingface.co/learn/nlp-course/en/chapter2/3) and [Tokenizers](https://huggingface.co/learn/nlp-course/en/chapter2/4?fw=pt) are loaded from the Hugging Face Hub. 2. The chat is formatted using the tokenizer's [chat template](https://huggingface.co/docs/transformers/main/en/chat_templating) 3. The formatted chat is [tokenized](https://huggingface.co/learn/nlp-course/en/chapter2/4) using the tokenizer. 4. We [generate](https://huggingface.co/docs/transformers/en/llm_tutorial) a response from the model. 5. The tokens output by the model are decoded back to a string ## Performance, memory and hardware You probably know by now that most machine learning tasks are run on GPUs. However, it is entirely possible to generate text from a chat model or language model on a CPU, albeit somewhat more slowly. If you can fit the model in GPU memory, though, this will usually be the preferable option. ### Memory considerations By default, Hugging Face classes like [`TextGenerationPipeline`] or [`AutoModelForCausalLM`] will load the model in `float32` precision. This means that it will need 4 bytes (32 bits) per parameter, so an "8B" model with 8 billion parameters will need ~32GB of memory. However, this can be wasteful! Most modern language models are trained in "bfloat16" precision, which uses only 2 bytes per parameter. If your hardware supports it (Nvidia 30xx/Axxx or newer), you can load the model in `bfloat16` precision, using the `torch_dtype` argument as we did above. It is possible to go even lower than 16-bits using "quantization", a method to lossily compress model weights. This allows each parameter to be squeezed down to 8 bits, 4 bits or even less. Note that, especially at 4 bits, the model's outputs may be negatively affected, but often this is a tradeoff worth making to fit a larger and more capable chat model in memory. Let's see this in action with `bitsandbytes`: ```python from transformers import AutoModelForCausalLM, BitsAndBytesConfig quantization_config = BitsAndBytesConfig(load_in_8bit=True) # You can also try load_in_4bit model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct", device_map="auto", quantization_config=quantization_config) ``` Or we can do the same thing using the `pipeline` API: ```python from transformers import pipeline, BitsAndBytesConfig quantization_config = BitsAndBytesConfig(load_in_8bit=True) # You can also try load_in_4bit pipe = pipeline("text-generation", "meta-llama/Meta-Llama-3-8B-Instruct", device_map="auto", model_kwargs={"quantization_config": quantization_config}) ``` There are several other options for quantizing models besides `bitsandbytes` - please see the [Quantization guide](./quantization) for more information. ### Performance considerations <Tip> For a more extensive guide on language model performance and optimization, check out [LLM Inference Optimization](./llm_optims) . </Tip> As a general rule, larger chat models will be slower in addition to requiring more memory. It's possible to be more concrete about this, though: Generating text from a chat model is unusual in that it is bottlenecked by **memory bandwidth** rather than compute power, because every active parameter must be read from memory for each token that the model generates. This means that number of tokens per second you can generate from a chat model is generally proportional to the total bandwidth of the memory it resides in, divided by the size of the model. In our quickstart example above, our model was ~16GB in size when loaded in `bfloat16` precision. This means that 16GB must be read from memory for every token generated by the model. Total memory bandwidth can vary from 20-100GB/sec for consumer CPUs to 200-900GB/sec for consumer GPUs, specialized CPUs like Intel Xeon, AMD Threadripper/Epyc or high-end Apple silicon, and finally up to 2-3TB/sec for data center GPUs like the Nvidia A100 or H100. This should give you a good idea of the generation speed you can expect from these different hardware types. Therefore, if you want to improve the speed of text generation, the easiest solution is to either reduce the size of the model in memory (usually by quantization), or get hardware with higher memory bandwidth. For advanced users, several other techniques exist to get around this bandwidth bottleneck. The most common are variants on [assisted generation](https://huggingface.co/blog/assisted-generation), also known as "speculative sampling". These techniques try to guess multiple future tokens at once, often using a smaller "draft model", and then confirm these generations with the chat model. If the guesses are validated by the chat model, more than one token can be generated per forward pass, which greatly alleviates the bandwidth bottleneck and improves generation speed. Finally, we should also note the impact of "Mixture of Experts" (MoE) models here. Several popular chat models, such as Mixtral, Qwen-MoE and DBRX, are MoE models. In these models, not every parameter is active for every token generated. As a result, MoE models generally have much lower memory bandwidth requirements, even though their total size can be quite large. They can therefore be several times faster than a normal "dense" model of the same size. However, techniques like assisted generation are generally ineffective for these models because more parameters will become active with each new speculated token, which will negate the bandwidth and speed benefits that the MoE architecture provides.
transformers/docs/source/en/conversations.md/0
{ "file_path": "transformers/docs/source/en/conversations.md", "repo_id": "transformers", "token_count": 4633 }
<!--Copyright 2020 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Utilities for Generation This page lists all the utility functions used by [`~generation.GenerationMixin.generate`]. ## Generate Outputs The output of [`~generation.GenerationMixin.generate`] is an instance of a subclass of [`~utils.ModelOutput`]. This output is a data structure containing all the information returned by [`~generation.GenerationMixin.generate`], but that can also be used as tuple or dictionary. Here's an example: ```python from transformers import GPT2Tokenizer, GPT2LMHeadModel tokenizer = GPT2Tokenizer.from_pretrained("openai-community/gpt2") model = GPT2LMHeadModel.from_pretrained("openai-community/gpt2") inputs = tokenizer("Hello, my dog is cute and ", return_tensors="pt") generation_output = model.generate(**inputs, return_dict_in_generate=True, output_scores=True) ``` The `generation_output` object is a [`~generation.GenerateDecoderOnlyOutput`], as we can see in the documentation of that class below, it means it has the following attributes: - `sequences`: the generated sequences of tokens - `scores` (optional): the prediction scores of the language modelling head, for each generation step - `hidden_states` (optional): the hidden states of the model, for each generation step - `attentions` (optional): the attention weights of the model, for each generation step Here we have the `scores` since we passed along `output_scores=True`, but we don't have `hidden_states` and `attentions` because we didn't pass `output_hidden_states=True` or `output_attentions=True`. You can access each attribute as you would usually do, and if that attribute has not been returned by the model, you will get `None`. Here for instance `generation_output.scores` are all the generated prediction scores of the language modeling head, and `generation_output.attentions` is `None`. When using our `generation_output` object as a tuple, it only keeps the attributes that don't have `None` values. Here, for instance, it has two elements, `loss` then `logits`, so ```python generation_output[:2] ``` will return the tuple `(generation_output.sequences, generation_output.scores)` for instance. When using our `generation_output` object as a dictionary, it only keeps the attributes that don't have `None` values. Here, for instance, it has two keys that are `sequences` and `scores`. We document here all output types. ### PyTorch [[autodoc]] generation.GenerateDecoderOnlyOutput [[autodoc]] generation.GenerateEncoderDecoderOutput [[autodoc]] generation.GenerateBeamDecoderOnlyOutput [[autodoc]] generation.GenerateBeamEncoderDecoderOutput ### TensorFlow [[autodoc]] generation.TFGreedySearchEncoderDecoderOutput [[autodoc]] generation.TFGreedySearchDecoderOnlyOutput [[autodoc]] generation.TFSampleEncoderDecoderOutput [[autodoc]] generation.TFSampleDecoderOnlyOutput [[autodoc]] generation.TFBeamSearchEncoderDecoderOutput [[autodoc]] generation.TFBeamSearchDecoderOnlyOutput [[autodoc]] generation.TFBeamSampleEncoderDecoderOutput [[autodoc]] generation.TFBeamSampleDecoderOnlyOutput [[autodoc]] generation.TFContrastiveSearchEncoderDecoderOutput [[autodoc]] generation.TFContrastiveSearchDecoderOnlyOutput ### FLAX [[autodoc]] generation.FlaxSampleOutput [[autodoc]] generation.FlaxGreedySearchOutput [[autodoc]] generation.FlaxBeamSearchOutput ## LogitsProcessor A [`LogitsProcessor`] can be used to modify the prediction scores of a language model head for generation. ### PyTorch [[autodoc]] AlternatingCodebooksLogitsProcessor - __call__ [[autodoc]] ClassifierFreeGuidanceLogitsProcessor - __call__ [[autodoc]] EncoderNoRepeatNGramLogitsProcessor - __call__ [[autodoc]] EncoderRepetitionPenaltyLogitsProcessor - __call__ [[autodoc]] EpsilonLogitsWarper - __call__ [[autodoc]] EtaLogitsWarper - __call__ [[autodoc]] ExponentialDecayLengthPenalty - __call__ [[autodoc]] ForcedBOSTokenLogitsProcessor - __call__ [[autodoc]] ForcedEOSTokenLogitsProcessor - __call__ [[autodoc]] HammingDiversityLogitsProcessor - __call__ [[autodoc]] InfNanRemoveLogitsProcessor - __call__ [[autodoc]] LogitNormalization - __call__ [[autodoc]] LogitsProcessor - __call__ [[autodoc]] LogitsProcessorList - __call__ [[autodoc]] MinLengthLogitsProcessor - __call__ [[autodoc]] MinNewTokensLengthLogitsProcessor - __call__ [[autodoc]] MinPLogitsWarper - __call__ [[autodoc]] NoBadWordsLogitsProcessor - __call__ [[autodoc]] NoRepeatNGramLogitsProcessor - __call__ [[autodoc]] PrefixConstrainedLogitsProcessor - __call__ [[autodoc]] RepetitionPenaltyLogitsProcessor - __call__ [[autodoc]] SequenceBiasLogitsProcessor - __call__ [[autodoc]] SuppressTokensAtBeginLogitsProcessor - __call__ [[autodoc]] SuppressTokensLogitsProcessor - __call__ [[autodoc]] SynthIDTextWatermarkLogitsProcessor - __call__ [[autodoc]] TemperatureLogitsWarper - __call__ [[autodoc]] TopKLogitsWarper - __call__ [[autodoc]] TopPLogitsWarper - __call__ [[autodoc]] TypicalLogitsWarper - __call__ [[autodoc]] UnbatchedClassifierFreeGuidanceLogitsProcessor - __call__ [[autodoc]] WhisperTimeStampLogitsProcessor - __call__ [[autodoc]] WatermarkLogitsProcessor - __call__ ### TensorFlow [[autodoc]] TFForcedBOSTokenLogitsProcessor - __call__ [[autodoc]] TFForcedEOSTokenLogitsProcessor - __call__ [[autodoc]] TFForceTokensLogitsProcessor - __call__ [[autodoc]] TFLogitsProcessor - __call__ [[autodoc]] TFLogitsProcessorList - __call__ [[autodoc]] TFLogitsWarper - __call__ [[autodoc]] TFMinLengthLogitsProcessor - __call__ [[autodoc]] TFNoBadWordsLogitsProcessor - __call__ [[autodoc]] TFNoRepeatNGramLogitsProcessor - __call__ [[autodoc]] TFRepetitionPenaltyLogitsProcessor - __call__ [[autodoc]] TFSuppressTokensAtBeginLogitsProcessor - __call__ [[autodoc]] TFSuppressTokensLogitsProcessor - __call__ [[autodoc]] TFTemperatureLogitsWarper - __call__ [[autodoc]] TFTopKLogitsWarper - __call__ [[autodoc]] TFTopPLogitsWarper - __call__ ### FLAX [[autodoc]] FlaxForcedBOSTokenLogitsProcessor - __call__ [[autodoc]] FlaxForcedEOSTokenLogitsProcessor - __call__ [[autodoc]] FlaxForceTokensLogitsProcessor - __call__ [[autodoc]] FlaxLogitsProcessor - __call__ [[autodoc]] FlaxLogitsProcessorList - __call__ [[autodoc]] FlaxLogitsWarper - __call__ [[autodoc]] FlaxMinLengthLogitsProcessor - __call__ [[autodoc]] FlaxSuppressTokensAtBeginLogitsProcessor - __call__ [[autodoc]] FlaxSuppressTokensLogitsProcessor - __call__ [[autodoc]] FlaxTemperatureLogitsWarper - __call__ [[autodoc]] FlaxTopKLogitsWarper - __call__ [[autodoc]] FlaxTopPLogitsWarper - __call__ [[autodoc]] FlaxWhisperTimeStampLogitsProcessor - __call__ ## StoppingCriteria A [`StoppingCriteria`] can be used to change when to stop generation (other than EOS token). Please note that this is exclusively available to our PyTorch implementations. [[autodoc]] StoppingCriteria - __call__ [[autodoc]] StoppingCriteriaList - __call__ [[autodoc]] MaxLengthCriteria - __call__ [[autodoc]] MaxTimeCriteria - __call__ [[autodoc]] StopStringCriteria - __call__ [[autodoc]] EosTokenCriteria - __call__ ## Constraints A [`Constraint`] can be used to force the generation to include specific tokens or sequences in the output. Please note that this is exclusively available to our PyTorch implementations. [[autodoc]] Constraint [[autodoc]] PhrasalConstraint [[autodoc]] DisjunctiveConstraint [[autodoc]] ConstraintListState ## BeamSearch [[autodoc]] BeamScorer - process - finalize [[autodoc]] BeamSearchScorer - process - finalize [[autodoc]] ConstrainedBeamSearchScorer - process - finalize ## Streamers [[autodoc]] TextStreamer [[autodoc]] TextIteratorStreamer [[autodoc]] AsyncTextIteratorStreamer ## Caches [[autodoc]] Cache - update [[autodoc]] CacheConfig - update [[autodoc]] QuantizedCacheConfig - validate [[autodoc]] DynamicCache - update - get_seq_length - reorder_cache - to_legacy_cache - from_legacy_cache [[autodoc]] QuantizedCache - update - get_seq_length [[autodoc]] QuantoQuantizedCache [[autodoc]] HQQQuantizedCache [[autodoc]] SinkCache - update - get_seq_length - reorder_cache [[autodoc]] OffloadedCache - update - prefetch_layer - evict_previous_layer [[autodoc]] StaticCache - update - get_seq_length - reset [[autodoc]] OffloadedStaticCache - update - get_seq_length - reset [[autodoc]] HybridCache - update - get_seq_length - reset [[autodoc]] SlidingWindowCache - update - reset [[autodoc]] EncoderDecoderCache - get_seq_length - to_legacy_cache - from_legacy_cache - reset - reorder_cache [[autodoc]] MambaCache - update_conv_state - update_ssm_state - reset ## Watermark Utils [[autodoc]] WatermarkingConfig - __call__ [[autodoc]] WatermarkDetector - __call__ [[autodoc]] BayesianDetectorConfig [[autodoc]] BayesianDetectorModel - forward [[autodoc]] SynthIDTextWatermarkingConfig [[autodoc]] SynthIDTextWatermarkDetector - __call__ ## Compile Utils [[autodoc]] CompileConfig - __call__
transformers/docs/source/en/internal/generation_utils.md/0
{ "file_path": "transformers/docs/source/en/internal/generation_utils.md", "repo_id": "transformers", "token_count": 3508 }
<!--Copyright 2020 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # DeepSpeed [DeepSpeed](https://github.com/deepspeedai/DeepSpeed), powered by Zero Redundancy Optimizer (ZeRO), is an optimization library for training and fitting very large models onto a GPU. It is available in several ZeRO stages, where each stage progressively saves more GPU memory by partitioning the optimizer state, gradients, parameters, and enabling offloading to a CPU or NVMe. DeepSpeed is integrated with the [`Trainer`] class and most of the setup is automatically taken care of for you. However, if you want to use DeepSpeed without the [`Trainer`], Transformers provides a [`HfDeepSpeedConfig`] class. <Tip> Learn more about using DeepSpeed with [`Trainer`] in the [DeepSpeed](../deepspeed) guide. </Tip> ## HfDeepSpeedConfig [[autodoc]] integrations.HfDeepSpeedConfig - all
transformers/docs/source/en/main_classes/deepspeed.md/0
{ "file_path": "transformers/docs/source/en/main_classes/deepspeed.md", "repo_id": "transformers", "token_count": 402 }
<!--Copyright 2020 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # ALBERT <div class="flex flex-wrap space-x-1"> <a href="https://huggingface.co/models?filter=albert"> <img alt="Models" src="https://img.shields.io/badge/All_model_pages-albert-blueviolet"> </a> <a href="https://huggingface.co/spaces/docs-demos/albert-base-v2"> <img alt="Spaces" src="https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue"> </a> </div> ## Overview The ALBERT model was proposed in [ALBERT: A Lite BERT for Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942) by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut. It presents two parameter-reduction techniques to lower memory consumption and increase the training speed of BERT: - Splitting the embedding matrix into two smaller matrices. - Using repeating layers split among groups. The abstract from the paper is the following: *Increasing model size when pretraining natural language representations often results in improved performance on downstream tasks. However, at some point further model increases become harder due to GPU/TPU memory limitations, longer training times, and unexpected model degradation. To address these problems, we present two parameter-reduction techniques to lower memory consumption and increase the training speed of BERT. Comprehensive empirical evidence shows that our proposed methods lead to models that scale much better compared to the original BERT. We also use a self-supervised loss that focuses on modeling inter-sentence coherence, and show it consistently helps downstream tasks with multi-sentence inputs. As a result, our best model establishes new state-of-the-art results on the GLUE, RACE, and SQuAD benchmarks while having fewer parameters compared to BERT-large.* This model was contributed by [lysandre](https://huggingface.co/lysandre). This model jax version was contributed by [kamalkraj](https://huggingface.co/kamalkraj). The original code can be found [here](https://github.com/google-research/ALBERT). ## Usage tips - ALBERT is a model with absolute position embeddings so it's usually advised to pad the inputs on the right rather than the left. - ALBERT uses repeating layers which results in a small memory footprint, however the computational cost remains similar to a BERT-like architecture with the same number of hidden layers as it has to iterate through the same number of (repeating) layers. - Embedding size E is different from hidden size H justified because the embeddings are context independent (one embedding vector represents one token), whereas hidden states are context dependent (one hidden state represents a sequence of tokens) so it's more logical to have H >> E. Also, the embedding matrix is large since it's V x E (V being the vocab size). If E < H, it has less parameters. - Layers are split in groups that share parameters (to save memory). Next sentence prediction is replaced by a sentence ordering prediction: in the inputs, we have two sentences A and B (that are consecutive) and we either feed A followed by B or B followed by A. The model must predict if they have been swapped or not. ### Using Scaled Dot Product Attention (SDPA) PyTorch includes a native scaled dot-product attention (SDPA) operator as part of `torch.nn.functional`. This function encompasses several implementations that can be applied depending on the inputs and the hardware in use. See the [official documentation](https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html) or the [GPU Inference](https://huggingface.co/docs/transformers/main/en/perf_infer_gpu_one#pytorch-scaled-dot-product-attention) page for more information. SDPA is used by default for `torch>=2.1.1` when an implementation is available, but you may also set `attn_implementation="sdpa"` in `from_pretrained()` to explicitly request SDPA to be used. ``` from transformers import AlbertModel model = AlbertModel.from_pretrained("albert/albert-base-v1", torch_dtype=torch.float16, attn_implementation="sdpa") ... ``` For the best speedups, we recommend loading the model in half-precision (e.g. `torch.float16` or `torch.bfloat16`). On a local benchmark (GeForce RTX 2060-8GB, PyTorch 2.3.1, OS Ubuntu 20.04) with `float16`, we saw the following speedups during training and inference. #### Training for 100 iterations |batch_size|seq_len|Time per batch (eager - s)| Time per batch (sdpa - s)| Speedup (%)| Eager peak mem (MB)| sdpa peak mem (MB)| Mem saving (%)| |----------|-------|--------------------------|--------------------------|------------|--------------------|-------------------|---------------| |2 |256 |0.028 |0.024 |14.388 |358.411 |321.088 |11.624 | |2 |512 |0.049 |0.041 |17.681 |753.458 |602.660 |25.022 | |4 |256 |0.044 |0.039 |12.246 |679.534 |602.660 |12.756 | |4 |512 |0.090 |0.076 |18.472 |1434.820 |1134.140 |26.512 | |8 |256 |0.081 |0.072 |12.664 |1283.825 |1134.140 |13.198 | |8 |512 |0.170 |0.143 |18.957 |2820.398 |2219.695 |27.062 | #### Inference with 50 batches |batch_size|seq_len|Per token latency eager (ms)|Per token latency SDPA (ms)|Speedup (%) |Mem eager (MB)|Mem BT (MB)|Mem saved (%)| |----------|-------|----------------------------|---------------------------|------------|--------------|-----------|-------------| |4 |128 |0.083 |0.071 |16.967 |48.319 |48.45 |-0.268 | |4 |256 |0.148 |0.127 |16.37 |63.4 |63.922 |-0.817 | |4 |512 |0.31 |0.247 |25.473 |110.092 |94.343 |16.693 | |8 |128 |0.137 |0.124 |11.102 |63.4 |63.66 |-0.409 | |8 |256 |0.271 |0.231 |17.271 |91.202 |92.246 |-1.132 | |8 |512 |0.602 |0.48 |25.47 |186.159 |152.564 |22.021 | |16 |128 |0.252 |0.224 |12.506 |91.202 |91.722 |-0.567 | |16 |256 |0.526 |0.448 |17.604 |148.378 |150.467 |-1.388 | |16 |512 |1.203 |0.96 |25.365 |338.293 |271.102 |24.784 | This model was contributed by [lysandre](https://huggingface.co/lysandre). This model jax version was contributed by [kamalkraj](https://huggingface.co/kamalkraj). The original code can be found [here](https://github.com/google-research/ALBERT). ## Resources The resources provided in the following sections consist of a list of official Hugging Face and community (indicated by 🌎) resources to help you get started with AlBERT. If you're interested in submitting a resource to be included here, please feel free to open a Pull Request and we'll review it! The resource should ideally demonstrate something new instead of duplicating an existing resource. <PipelineTag pipeline="text-classification"/> - [`AlbertForSequenceClassification`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/pytorch/text-classification). - [`TFAlbertForSequenceClassification`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/text-classification). - [`FlaxAlbertForSequenceClassification`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/flax/text-classification) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification_flax.ipynb). - Check the [Text classification task guide](../tasks/sequence_classification) on how to use the model. <PipelineTag pipeline="token-classification"/> - [`AlbertForTokenClassification`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/pytorch/token-classification). - [`TFAlbertForTokenClassification`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/token-classification) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification-tf.ipynb). - [`FlaxAlbertForTokenClassification`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/flax/token-classification). - [Token classification](https://huggingface.co/course/chapter7/2?fw=pt) chapter of the 🤗 Hugging Face Course. - Check the [Token classification task guide](../tasks/token_classification) on how to use the model. <PipelineTag pipeline="fill-mask"/> - [`AlbertForMaskedLM`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/pytorch/language-modeling#robertabertdistilbert-and-masked-language-modeling) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb). - [`TFAlbertForMaskedLM`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/language-modeling#run_mlmpy) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb). - [`FlaxAlbertForMaskedLM`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/flax/language-modeling#masked-language-modeling) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/masked_language_modeling_flax.ipynb). - [Masked language modeling](https://huggingface.co/course/chapter7/3?fw=pt) chapter of the 🤗 Hugging Face Course. - Check the [Masked language modeling task guide](../tasks/masked_language_modeling) on how to use the model. <PipelineTag pipeline="question-answering"/> - [`AlbertForQuestionAnswering`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering.ipynb). - [`TFAlbertForQuestionAnswering`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/question-answering) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering-tf.ipynb). - [`FlaxAlbertForQuestionAnswering`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/flax/question-answering). - [Question answering](https://huggingface.co/course/chapter7/7?fw=pt) chapter of the 🤗 Hugging Face Course. - Check the [Question answering task guide](../tasks/question_answering) on how to use the model. **Multiple choice** - [`AlbertForMultipleChoice`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/pytorch/multiple-choice) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice.ipynb). - [`TFAlbertForMultipleChoice`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/multiple-choice) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice-tf.ipynb). - Check the [Multiple choice task guide](../tasks/multiple_choice) on how to use the model. ## AlbertConfig [[autodoc]] AlbertConfig ## AlbertTokenizer [[autodoc]] AlbertTokenizer - build_inputs_with_special_tokens - get_special_tokens_mask - create_token_type_ids_from_sequences - save_vocabulary ## AlbertTokenizerFast [[autodoc]] AlbertTokenizerFast ## Albert specific outputs [[autodoc]] models.albert.modeling_albert.AlbertForPreTrainingOutput [[autodoc]] models.albert.modeling_tf_albert.TFAlbertForPreTrainingOutput <frameworkcontent> <pt> ## AlbertModel [[autodoc]] AlbertModel - forward ## AlbertForPreTraining [[autodoc]] AlbertForPreTraining - forward ## AlbertForMaskedLM [[autodoc]] AlbertForMaskedLM - forward ## AlbertForSequenceClassification [[autodoc]] AlbertForSequenceClassification - forward ## AlbertForMultipleChoice [[autodoc]] AlbertForMultipleChoice ## AlbertForTokenClassification [[autodoc]] AlbertForTokenClassification - forward ## AlbertForQuestionAnswering [[autodoc]] AlbertForQuestionAnswering - forward </pt> <tf> ## TFAlbertModel [[autodoc]] TFAlbertModel - call ## TFAlbertForPreTraining [[autodoc]] TFAlbertForPreTraining - call ## TFAlbertForMaskedLM [[autodoc]] TFAlbertForMaskedLM - call ## TFAlbertForSequenceClassification [[autodoc]] TFAlbertForSequenceClassification - call ## TFAlbertForMultipleChoice [[autodoc]] TFAlbertForMultipleChoice - call ## TFAlbertForTokenClassification [[autodoc]] TFAlbertForTokenClassification - call ## TFAlbertForQuestionAnswering [[autodoc]] TFAlbertForQuestionAnswering - call </tf> <jax> ## FlaxAlbertModel [[autodoc]] FlaxAlbertModel - __call__ ## FlaxAlbertForPreTraining [[autodoc]] FlaxAlbertForPreTraining - __call__ ## FlaxAlbertForMaskedLM [[autodoc]] FlaxAlbertForMaskedLM - __call__ ## FlaxAlbertForSequenceClassification [[autodoc]] FlaxAlbertForSequenceClassification - __call__ ## FlaxAlbertForMultipleChoice [[autodoc]] FlaxAlbertForMultipleChoice - __call__ ## FlaxAlbertForTokenClassification [[autodoc]] FlaxAlbertForTokenClassification - __call__ ## FlaxAlbertForQuestionAnswering [[autodoc]] FlaxAlbertForQuestionAnswering - __call__ </jax> </frameworkcontent>
transformers/docs/source/en/model_doc/albert.md/0
{ "file_path": "transformers/docs/source/en/model_doc/albert.md", "repo_id": "transformers", "token_count": 5608 }
<!--Copyright 2020 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # BERTweet ## Overview The BERTweet model was proposed in [BERTweet: A pre-trained language model for English Tweets](https://www.aclweb.org/anthology/2020.emnlp-demos.2.pdf) by Dat Quoc Nguyen, Thanh Vu, Anh Tuan Nguyen. The abstract from the paper is the following: *We present BERTweet, the first public large-scale pre-trained language model for English Tweets. Our BERTweet, having the same architecture as BERT-base (Devlin et al., 2019), is trained using the RoBERTa pre-training procedure (Liu et al., 2019). Experiments show that BERTweet outperforms strong baselines RoBERTa-base and XLM-R-base (Conneau et al., 2020), producing better performance results than the previous state-of-the-art models on three Tweet NLP tasks: Part-of-speech tagging, Named-entity recognition and text classification.* This model was contributed by [dqnguyen](https://huggingface.co/dqnguyen). The original code can be found [here](https://github.com/VinAIResearch/BERTweet). ## Usage example ```python >>> import torch >>> from transformers import AutoModel, AutoTokenizer >>> bertweet = AutoModel.from_pretrained("vinai/bertweet-base") >>> # For transformers v4.x+: >>> tokenizer = AutoTokenizer.from_pretrained("vinai/bertweet-base", use_fast=False) >>> # For transformers v3.x: >>> # tokenizer = AutoTokenizer.from_pretrained("vinai/bertweet-base") >>> # INPUT TWEET IS ALREADY NORMALIZED! >>> line = "SC has first two presumptive cases of coronavirus , DHEC confirms HTTPURL via @USER :cry:" >>> input_ids = torch.tensor([tokenizer.encode(line)]) >>> with torch.no_grad(): ... features = bertweet(input_ids) # Models outputs are now tuples >>> # With TensorFlow 2.0+: >>> # from transformers import TFAutoModel >>> # bertweet = TFAutoModel.from_pretrained("vinai/bertweet-base") ``` <Tip> This implementation is the same as BERT, except for tokenization method. Refer to [BERT documentation](bert) for API reference information. </Tip> ## BertweetTokenizer [[autodoc]] BertweetTokenizer
transformers/docs/source/en/model_doc/bertweet.md/0
{ "file_path": "transformers/docs/source/en/model_doc/bertweet.md", "repo_id": "transformers", "token_count": 806 }
<!--Copyright 2024 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Chameleon ## Overview The Chameleon model was proposed in [Chameleon: Mixed-Modal Early-Fusion Foundation Models ](https://arxiv.org/abs/2405.09818v1) by META AI Chameleon Team. Chameleon is a Vision-Language Model that use vector quantization to tokenize images which enables the model to generate multimodal output. The model takes images and texts as input, including an interleaved format, and generates textual response. Image generation module is not released yet. The abstract from the paper is the following: *We present Chameleon, a family of early-fusion token-based mixed-modal models capable of understanding and generating images and text in any arbitrary sequence. We outline a stable training approach from inception, an alignment recipe, and an architectural parameterization tailored for the early-fusion, token-based, mixed-modal setting. The models are evaluated on a comprehensive range of tasks, including visual question answering, image captioning, text generation, image generation, and long-form mixed modal generation. Chameleon demonstrates broad and general capabilities, including state-of-the-art performance in image captioning tasks, outperforms Llama-2 in text-only tasks while being competitive with models such as Mixtral 8x7B and Gemini-Pro, and performs non-trivial image generation, all in a single model. It also matches or exceeds the performance of much larger models, including Gemini Pro and GPT-4V, according to human judgments on a new long-form mixed-modal generation evaluation, where either the prompt or outputs contain mixed sequences of both images and text. Chameleon marks a significant step forward in unified modeling of full multimodal documents* <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/chameleon_arch.png" alt="drawing" width="600"/> <small> Chameleon incorporates a vector quantizer module to transform images into discrete tokens. That also enables image generation using an auto-regressive transformer. Taken from the <a href="https://arxiv.org/abs/2405.09818v1">original paper.</a> </small> This model was contributed by [joaogante](https://huggingface.co/joaogante) and [RaushanTurganbay](https://huggingface.co/RaushanTurganbay). The original code can be found [here](https://github.com/facebookresearch/chameleon). ## Usage tips - We advise users to use `padding_side="left"` when computing batched generation as it leads to more accurate results. Simply make sure to set `processor.tokenizer.padding_side = "left"` before generating. - Note that Chameleon was tuned for safety alignment. If the model is refusing to answer, consider asking a more concrete question, instead of an open question. - Chameleon generates in chat format which means that the generated text will always be the "assistant's turn". You can enable a text completion generation by passing `return_for_text_completion=True` when calling the processor. > [!NOTE] > Chameleon implementation in Transformers uses a special image token to indicate where to merge image embeddings. For special image token we didn't add a new one but used one of the reserved tokens: `<reserved08707>`. You have to add `<image>` to your prompt in the place where the image should be embedded for correct generation. ## Usage example ### Single image inference Chameleon is a gated model so make sure to have access and login to Hugging Face Hub using a token. Here's how to load the model and perform inference in half-precision (`torch.bfloat16`): ```python from transformers import ChameleonProcessor, ChameleonForConditionalGeneration import torch from PIL import Image import requests processor = ChameleonProcessor.from_pretrained("facebook/chameleon-7b") model = ChameleonForConditionalGeneration.from_pretrained("facebook/chameleon-7b", torch_dtype=torch.bfloat16, device_map="cuda") # prepare image and text prompt url = 'http://images.cocodataset.org/val2017/000000039769.jpg' image = Image.open(requests.get(url, stream=True).raw) prompt = "What do you see in this image?<image>" inputs = processor(images=image, text=prompt, return_tensors="pt").to(model.device, dtype=torch.bfloat16) # autoregressively complete prompt output = model.generate(**inputs, max_new_tokens=50) print(processor.decode(output[0], skip_special_tokens=True)) ``` ### Multi image inference Chameleon can perform inference with multiple images as input, where images either belong to the same prompt or different prompts (in batched inference). Here is how you can do it: ```python from transformers import ChameleonProcessor, ChameleonForConditionalGeneration import torch from PIL import Image import requests processor = ChameleonProcessor.from_pretrained("facebook/chameleon-7b") model = ChameleonForConditionalGeneration.from_pretrained("facebook/chameleon-7b", torch_dtype=torch.bfloat16, device_map="cuda") # Get three different images url = "https://www.ilankelman.org/stopsigns/australia.jpg" image_stop = Image.open(requests.get(url, stream=True).raw) url = "http://images.cocodataset.org/val2017/000000039769.jpg" image_cats = Image.open(requests.get(url, stream=True).raw) url = "https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/snowman.jpg" image_snowman = Image.open(requests.get(url, stream=True).raw) # Prepare a batched prompt, where the first one is a multi-image prompt and the second is not prompts = [ "What do these images have in common?<image><image>", "<image>What is shown in this image?" ] # We can simply feed images in the order they have to be used in the text prompt # Each "<image>" token uses one image leaving the next for the subsequent "<image>" tokens inputs = processor(images=[image_stop, image_cats, image_snowman], text=prompts, padding=True, return_tensors="pt").to(device="cuda", dtype=torch.bfloat16) # Generate generate_ids = model.generate(**inputs, max_new_tokens=50) processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False) ``` ## Model optimization ### Quantization using Bitsandbytes The model can be loaded in 8 or 4 bits, greatly reducing the memory requirements while maintaining the performance of the original model. First make sure to install bitsandbytes, `pip install bitsandbytes` and to have access to a GPU/accelerator that is supported by the library. <Tip> bitsandbytes is being refactored to support multiple backends beyond CUDA. Currently, ROCm (AMD GPU) and Intel CPU implementations are mature, with Intel XPU in progress and Apple Silicon support expected by Q4/Q1. For installation instructions and the latest backend updates, visit [this link](https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend). We value your feedback to help identify bugs before the full release! Check out [these docs](https://huggingface.co/docs/bitsandbytes/main/en/non_cuda_backends) for more details and feedback links. </Tip> Simply change the snippet above with: ```python from transformers import ChameleonForConditionalGeneration, BitsAndBytesConfig # specify how to quantize the model quantization_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16, ) model = ChameleonForConditionalGeneration.from_pretrained("facebook/chameleon-7b", quantization_config=quantization_config, device_map="cuda") ``` ### Use Flash-Attention 2 and SDPA to further speed-up generation The models supports both, Flash-Attention 2 and PyTorch's [`torch.nn.functional.scaled_dot_product_attention`](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention.html) which can be enables for optimization. SDPA is the default options when you load the model, If you want to switch for Flash Attention 2, first make sure to install flash-attn. Refer to the [original repository](https://github.com/Dao-AILab/flash-attention) regarding that package installation. Simply change the snippet above with: ```python from transformers import ChameleonForConditionalGeneration model_id = "facebook/chameleon-7b" model = ChameleonForConditionalGeneration.from_pretrained( model_id, torch_dtype=torch.bfloat16, low_cpu_mem_usage=True, attn_implementation="flash_attention_2" ).to(0) ``` ## ChameleonConfig [[autodoc]] ChameleonConfig ## ChameleonVQVAEConfig [[autodoc]] ChameleonVQVAEConfig ## ChameleonProcessor [[autodoc]] ChameleonProcessor ## ChameleonImageProcessor [[autodoc]] ChameleonImageProcessor - preprocess ## ChameleonVQVAE [[autodoc]] ChameleonVQVAE - forward ## ChameleonModel [[autodoc]] ChameleonModel - forward ## ChameleonForConditionalGeneration [[autodoc]] ChameleonForConditionalGeneration - forward
transformers/docs/source/en/model_doc/chameleon.md/0
{ "file_path": "transformers/docs/source/en/model_doc/chameleon.md", "repo_id": "transformers", "token_count": 2738 }
<!--Copyright 2022 The HuggingFace Team and The OpenBMB Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # CPMAnt ## Overview CPM-Ant is an open-source Chinese pre-trained language model (PLM) with 10B parameters. It is also the first milestone of the live training process of CPM-Live. The training process is cost-effective and environment-friendly. CPM-Ant also achieves promising results with delta tuning on the CUGE benchmark. Besides the full model, we also provide various compressed versions to meet the requirements of different hardware configurations. [See more](https://github.com/OpenBMB/CPM-Live/tree/cpm-ant/cpm-live) This model was contributed by [OpenBMB](https://huggingface.co/openbmb). The original code can be found [here](https://github.com/OpenBMB/CPM-Live/tree/cpm-ant/cpm-live). ## Resources - A tutorial on [CPM-Live](https://github.com/OpenBMB/CPM-Live/tree/cpm-ant/cpm-live). ## CpmAntConfig [[autodoc]] CpmAntConfig - all ## CpmAntTokenizer [[autodoc]] CpmAntTokenizer - all ## CpmAntModel [[autodoc]] CpmAntModel - all ## CpmAntForCausalLM [[autodoc]] CpmAntForCausalLM - all
transformers/docs/source/en/model_doc/cpmant.md/0
{ "file_path": "transformers/docs/source/en/model_doc/cpmant.md", "repo_id": "transformers", "token_count": 534 }
<!--Copyright 2021 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # DETR ## Overview The DETR model was proposed in [End-to-End Object Detection with Transformers](https://arxiv.org/abs/2005.12872) by Nicolas Carion, Francisco Massa, Gabriel Synnaeve, Nicolas Usunier, Alexander Kirillov and Sergey Zagoruyko. DETR consists of a convolutional backbone followed by an encoder-decoder Transformer which can be trained end-to-end for object detection. It greatly simplifies a lot of the complexity of models like Faster-R-CNN and Mask-R-CNN, which use things like region proposals, non-maximum suppression procedure and anchor generation. Moreover, DETR can also be naturally extended to perform panoptic segmentation, by simply adding a mask head on top of the decoder outputs. The abstract from the paper is the following: *We present a new method that views object detection as a direct set prediction problem. Our approach streamlines the detection pipeline, effectively removing the need for many hand-designed components like a non-maximum suppression procedure or anchor generation that explicitly encode our prior knowledge about the task. The main ingredients of the new framework, called DEtection TRansformer or DETR, are a set-based global loss that forces unique predictions via bipartite matching, and a transformer encoder-decoder architecture. Given a fixed small set of learned object queries, DETR reasons about the relations of the objects and the global image context to directly output the final set of predictions in parallel. The new model is conceptually simple and does not require a specialized library, unlike many other modern detectors. DETR demonstrates accuracy and run-time performance on par with the well-established and highly-optimized Faster RCNN baseline on the challenging COCO object detection dataset. Moreover, DETR can be easily generalized to produce panoptic segmentation in a unified manner. We show that it significantly outperforms competitive baselines.* This model was contributed by [nielsr](https://huggingface.co/nielsr). The original code can be found [here](https://github.com/facebookresearch/detr). ## How DETR works Here's a TLDR explaining how [`~transformers.DetrForObjectDetection`] works: First, an image is sent through a pre-trained convolutional backbone (in the paper, the authors use ResNet-50/ResNet-101). Let's assume we also add a batch dimension. This means that the input to the backbone is a tensor of shape `(batch_size, 3, height, width)`, assuming the image has 3 color channels (RGB). The CNN backbone outputs a new lower-resolution feature map, typically of shape `(batch_size, 2048, height/32, width/32)`. This is then projected to match the hidden dimension of the Transformer of DETR, which is `256` by default, using a `nn.Conv2D` layer. So now, we have a tensor of shape `(batch_size, 256, height/32, width/32).` Next, the feature map is flattened and transposed to obtain a tensor of shape `(batch_size, seq_len, d_model)` = `(batch_size, width/32*height/32, 256)`. So a difference with NLP models is that the sequence length is actually longer than usual, but with a smaller `d_model` (which in NLP is typically 768 or higher). Next, this is sent through the encoder, outputting `encoder_hidden_states` of the same shape (you can consider these as image features). Next, so-called **object queries** are sent through the decoder. This is a tensor of shape `(batch_size, num_queries, d_model)`, with `num_queries` typically set to 100 and initialized with zeros. These input embeddings are learnt positional encodings that the authors refer to as object queries, and similarly to the encoder, they are added to the input of each attention layer. Each object query will look for a particular object in the image. The decoder updates these embeddings through multiple self-attention and encoder-decoder attention layers to output `decoder_hidden_states` of the same shape: `(batch_size, num_queries, d_model)`. Next, two heads are added on top for object detection: a linear layer for classifying each object query into one of the objects or "no object", and a MLP to predict bounding boxes for each query. The model is trained using a **bipartite matching loss**: so what we actually do is compare the predicted classes + bounding boxes of each of the N = 100 object queries to the ground truth annotations, padded up to the same length N (so if an image only contains 4 objects, 96 annotations will just have a "no object" as class and "no bounding box" as bounding box). The [Hungarian matching algorithm](https://en.wikipedia.org/wiki/Hungarian_algorithm) is used to find an optimal one-to-one mapping of each of the N queries to each of the N annotations. Next, standard cross-entropy (for the classes) and a linear combination of the L1 and [generalized IoU loss](https://giou.stanford.edu/) (for the bounding boxes) are used to optimize the parameters of the model. DETR can be naturally extended to perform panoptic segmentation (which unifies semantic segmentation and instance segmentation). [`~transformers.DetrForSegmentation`] adds a segmentation mask head on top of [`~transformers.DetrForObjectDetection`]. The mask head can be trained either jointly, or in a two steps process, where one first trains a [`~transformers.DetrForObjectDetection`] model to detect bounding boxes around both "things" (instances) and "stuff" (background things like trees, roads, sky), then freeze all the weights and train only the mask head for 25 epochs. Experimentally, these two approaches give similar results. Note that predicting boxes is required for the training to be possible, since the Hungarian matching is computed using distances between boxes. ## Usage tips - DETR uses so-called **object queries** to detect objects in an image. The number of queries determines the maximum number of objects that can be detected in a single image, and is set to 100 by default (see parameter `num_queries` of [`~transformers.DetrConfig`]). Note that it's good to have some slack (in COCO, the authors used 100, while the maximum number of objects in a COCO image is ~70). - The decoder of DETR updates the query embeddings in parallel. This is different from language models like GPT-2, which use autoregressive decoding instead of parallel. Hence, no causal attention mask is used. - DETR adds position embeddings to the hidden states at each self-attention and cross-attention layer before projecting to queries and keys. For the position embeddings of the image, one can choose between fixed sinusoidal or learned absolute position embeddings. By default, the parameter `position_embedding_type` of [`~transformers.DetrConfig`] is set to `"sine"`. - During training, the authors of DETR did find it helpful to use auxiliary losses in the decoder, especially to help the model output the correct number of objects of each class. If you set the parameter `auxiliary_loss` of [`~transformers.DetrConfig`] to `True`, then prediction feedforward neural networks and Hungarian losses are added after each decoder layer (with the FFNs sharing parameters). - If you want to train the model in a distributed environment across multiple nodes, then one should update the _num_boxes_ variable in the _DetrLoss_ class of _modeling_detr.py_. When training on multiple nodes, this should be set to the average number of target boxes across all nodes, as can be seen in the original implementation [here](https://github.com/facebookresearch/detr/blob/a54b77800eb8e64e3ad0d8237789fcbf2f8350c5/models/detr.py#L227-L232). - [`~transformers.DetrForObjectDetection`] and [`~transformers.DetrForSegmentation`] can be initialized with any convolutional backbone available in the [timm library](https://github.com/rwightman/pytorch-image-models). Initializing with a MobileNet backbone for example can be done by setting the `backbone` attribute of [`~transformers.DetrConfig`] to `"tf_mobilenetv3_small_075"`, and then initializing the model with that config. - DETR resizes the input images such that the shortest side is at least a certain amount of pixels while the longest is at most 1333 pixels. At training time, scale augmentation is used such that the shortest side is randomly set to at least 480 and at most 800 pixels. At inference time, the shortest side is set to 800. One can use [`~transformers.DetrImageProcessor`] to prepare images (and optional annotations in COCO format) for the model. Due to this resizing, images in a batch can have different sizes. DETR solves this by padding images up to the largest size in a batch, and by creating a pixel mask that indicates which pixels are real/which are padding. Alternatively, one can also define a custom `collate_fn` in order to batch images together, using [`~transformers.DetrImageProcessor.pad_and_create_pixel_mask`]. - The size of the images will determine the amount of memory being used, and will thus determine the `batch_size`. It is advised to use a batch size of 2 per GPU. See [this Github thread](https://github.com/facebookresearch/detr/issues/150) for more info. There are three ways to instantiate a DETR model (depending on what you prefer): Option 1: Instantiate DETR with pre-trained weights for entire model ```py >>> from transformers import DetrForObjectDetection >>> model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50") ``` Option 2: Instantiate DETR with randomly initialized weights for Transformer, but pre-trained weights for backbone ```py >>> from transformers import DetrConfig, DetrForObjectDetection >>> config = DetrConfig() >>> model = DetrForObjectDetection(config) ``` Option 3: Instantiate DETR with randomly initialized weights for backbone + Transformer ```py >>> config = DetrConfig(use_pretrained_backbone=False) >>> model = DetrForObjectDetection(config) ``` As a summary, consider the following table: | Task | Object detection | Instance segmentation | Panoptic segmentation | |------|------------------|-----------------------|-----------------------| | **Description** | Predicting bounding boxes and class labels around objects in an image | Predicting masks around objects (i.e. instances) in an image | Predicting masks around both objects (i.e. instances) as well as "stuff" (i.e. background things like trees and roads) in an image | | **Model** | [`~transformers.DetrForObjectDetection`] | [`~transformers.DetrForSegmentation`] | [`~transformers.DetrForSegmentation`] | | **Example dataset** | COCO detection | COCO detection, COCO panoptic | COCO panoptic | | | **Format of annotations to provide to** [`~transformers.DetrImageProcessor`] | {'image_id': `int`, 'annotations': `List[Dict]`} each Dict being a COCO object annotation | {'image_id': `int`, 'annotations': `List[Dict]`} (in case of COCO detection) or {'file_name': `str`, 'image_id': `int`, 'segments_info': `List[Dict]`} (in case of COCO panoptic) | {'file_name': `str`, 'image_id': `int`, 'segments_info': `List[Dict]`} and masks_path (path to directory containing PNG files of the masks) | | **Postprocessing** (i.e. converting the output of the model to Pascal VOC format) | [`~transformers.DetrImageProcessor.post_process`] | [`~transformers.DetrImageProcessor.post_process_segmentation`] | [`~transformers.DetrImageProcessor.post_process_segmentation`], [`~transformers.DetrImageProcessor.post_process_panoptic`] | | **evaluators** | `CocoEvaluator` with `iou_types="bbox"` | `CocoEvaluator` with `iou_types="bbox"` or `"segm"` | `CocoEvaluator` with `iou_tupes="bbox"` or `"segm"`, `PanopticEvaluator` | In short, one should prepare the data either in COCO detection or COCO panoptic format, then use [`~transformers.DetrImageProcessor`] to create `pixel_values`, `pixel_mask` and optional `labels`, which can then be used to train (or fine-tune) a model. For evaluation, one should first convert the outputs of the model using one of the postprocessing methods of [`~transformers.DetrImageProcessor`]. These can be provided to either `CocoEvaluator` or `PanopticEvaluator`, which allow you to calculate metrics like mean Average Precision (mAP) and Panoptic Quality (PQ). The latter objects are implemented in the [original repository](https://github.com/facebookresearch/detr). See the [example notebooks](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/DETR) for more info regarding evaluation. ## Resources A list of official Hugging Face and community (indicated by 🌎) resources to help you get started with DETR. <PipelineTag pipeline="object-detection"/> - All example notebooks illustrating fine-tuning [`DetrForObjectDetection`] and [`DetrForSegmentation`] on a custom dataset can be found [here](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/DETR). - Scripts for finetuning [`DetrForObjectDetection`] with [`Trainer`] or [Accelerate](https://huggingface.co/docs/accelerate/index) can be found [here](https://github.com/huggingface/transformers/tree/main/examples/pytorch/object-detection). - See also: [Object detection task guide](../tasks/object_detection). If you're interested in submitting a resource to be included here, please feel free to open a Pull Request and we'll review it! The resource should ideally demonstrate something new instead of duplicating an existing resource. ## DetrConfig [[autodoc]] DetrConfig ## DetrImageProcessor [[autodoc]] DetrImageProcessor - preprocess - post_process_object_detection - post_process_semantic_segmentation - post_process_instance_segmentation - post_process_panoptic_segmentation ## DetrImageProcessorFast [[autodoc]] DetrImageProcessorFast - preprocess - post_process_object_detection - post_process_semantic_segmentation - post_process_instance_segmentation - post_process_panoptic_segmentation ## DetrFeatureExtractor [[autodoc]] DetrFeatureExtractor - __call__ - post_process_object_detection - post_process_semantic_segmentation - post_process_instance_segmentation - post_process_panoptic_segmentation ## DETR specific outputs [[autodoc]] models.detr.modeling_detr.DetrModelOutput [[autodoc]] models.detr.modeling_detr.DetrObjectDetectionOutput [[autodoc]] models.detr.modeling_detr.DetrSegmentationOutput ## DetrModel [[autodoc]] DetrModel - forward ## DetrForObjectDetection [[autodoc]] DetrForObjectDetection - forward ## DetrForSegmentation [[autodoc]] DetrForSegmentation - forward
transformers/docs/source/en/model_doc/detr.md/0
{ "file_path": "transformers/docs/source/en/model_doc/detr.md", "repo_id": "transformers", "token_count": 4274 }
<!--Copyright 2020 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Encoder Decoder Models ## Overview The [`EncoderDecoderModel`] can be used to initialize a sequence-to-sequence model with any pretrained autoencoding model as the encoder and any pretrained autoregressive model as the decoder. The effectiveness of initializing sequence-to-sequence models with pretrained checkpoints for sequence generation tasks was shown in [Leveraging Pre-trained Checkpoints for Sequence Generation Tasks](https://arxiv.org/abs/1907.12461) by Sascha Rothe, Shashi Narayan, Aliaksei Severyn. After such an [`EncoderDecoderModel`] has been trained/fine-tuned, it can be saved/loaded just like any other models (see the examples for more information). An application of this architecture could be to leverage two pretrained [`BertModel`] as the encoder and decoder for a summarization model as was shown in: [Text Summarization with Pretrained Encoders](https://arxiv.org/abs/1908.08345) by Yang Liu and Mirella Lapata. ## Randomly initializing `EncoderDecoderModel` from model configurations. [`EncoderDecoderModel`] can be randomly initialized from an encoder and a decoder config. In the following example, we show how to do this using the default [`BertModel`] configuration for the encoder and the default [`BertForCausalLM`] configuration for the decoder. ```python >>> from transformers import BertConfig, EncoderDecoderConfig, EncoderDecoderModel >>> config_encoder = BertConfig() >>> config_decoder = BertConfig() >>> config = EncoderDecoderConfig.from_encoder_decoder_configs(config_encoder, config_decoder) >>> model = EncoderDecoderModel(config=config) ``` ## Initialising `EncoderDecoderModel` from a pretrained encoder and a pretrained decoder. [`EncoderDecoderModel`] can be initialized from a pretrained encoder checkpoint and a pretrained decoder checkpoint. Note that any pretrained auto-encoding model, *e.g.* BERT, can serve as the encoder and both pretrained auto-encoding models, *e.g.* BERT, pretrained causal language models, *e.g.* GPT2, as well as the pretrained decoder part of sequence-to-sequence models, *e.g.* decoder of BART, can be used as the decoder. Depending on which architecture you choose as the decoder, the cross-attention layers might be randomly initialized. Initializing [`EncoderDecoderModel`] from a pretrained encoder and decoder checkpoint requires the model to be fine-tuned on a downstream task, as has been shown in [the *Warm-starting-encoder-decoder blog post*](https://huggingface.co/blog/warm-starting-encoder-decoder). To do so, the `EncoderDecoderModel` class provides a [`EncoderDecoderModel.from_encoder_decoder_pretrained`] method. ```python >>> from transformers import EncoderDecoderModel, BertTokenizer >>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased") >>> model = EncoderDecoderModel.from_encoder_decoder_pretrained("google-bert/bert-base-uncased", "google-bert/bert-base-uncased") ``` ## Loading an existing `EncoderDecoderModel` checkpoint and perform inference. To load fine-tuned checkpoints of the `EncoderDecoderModel` class, [`EncoderDecoderModel`] provides the `from_pretrained(...)` method just like any other model architecture in Transformers. To perform inference, one uses the [`generate`] method, which allows to autoregressively generate text. This method supports various forms of decoding, such as greedy, beam search and multinomial sampling. ```python >>> from transformers import AutoTokenizer, EncoderDecoderModel >>> # load a fine-tuned seq2seq model and corresponding tokenizer >>> model = EncoderDecoderModel.from_pretrained("patrickvonplaten/bert2bert_cnn_daily_mail") >>> tokenizer = AutoTokenizer.from_pretrained("patrickvonplaten/bert2bert_cnn_daily_mail") >>> # let's perform inference on a long piece of text >>> ARTICLE_TO_SUMMARIZE = ( ... "PG&E stated it scheduled the blackouts in response to forecasts for high winds " ... "amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were " ... "scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow." ... ) >>> input_ids = tokenizer(ARTICLE_TO_SUMMARIZE, return_tensors="pt").input_ids >>> # autoregressively generate summary (uses greedy decoding by default) >>> generated_ids = model.generate(input_ids) >>> generated_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] >>> print(generated_text) nearly 800 thousand customers were affected by the shutoffs. the aim is to reduce the risk of wildfires. nearly 800, 000 customers were expected to be affected by high winds amid dry conditions. pg & e said it scheduled the blackouts to last through at least midday tomorrow. ``` ## Loading a PyTorch checkpoint into `TFEncoderDecoderModel`. [`TFEncoderDecoderModel.from_pretrained`] currently doesn't support initializing the model from a pytorch checkpoint. Passing `from_pt=True` to this method will throw an exception. If there are only pytorch checkpoints for a particular encoder-decoder model, a workaround is: ```python >>> # a workaround to load from pytorch checkpoint >>> from transformers import EncoderDecoderModel, TFEncoderDecoderModel >>> _model = EncoderDecoderModel.from_pretrained("patrickvonplaten/bert2bert-cnn_dailymail-fp16") >>> _model.encoder.save_pretrained("./encoder") >>> _model.decoder.save_pretrained("./decoder") >>> model = TFEncoderDecoderModel.from_encoder_decoder_pretrained( ... "./encoder", "./decoder", encoder_from_pt=True, decoder_from_pt=True ... ) >>> # This is only for copying some specific attributes of this particular model. >>> model.config = _model.config ``` ## Training Once the model is created, it can be fine-tuned similar to BART, T5 or any other encoder-decoder model. As you can see, only 2 inputs are required for the model in order to compute a loss: `input_ids` (which are the `input_ids` of the encoded input sequence) and `labels` (which are the `input_ids` of the encoded target sequence). ```python >>> from transformers import BertTokenizer, EncoderDecoderModel >>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased") >>> model = EncoderDecoderModel.from_encoder_decoder_pretrained("google-bert/bert-base-uncased", "google-bert/bert-base-uncased") >>> model.config.decoder_start_token_id = tokenizer.cls_token_id >>> model.config.pad_token_id = tokenizer.pad_token_id >>> input_ids = tokenizer( ... "The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side.During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft).Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct.", ... return_tensors="pt", ... ).input_ids >>> labels = tokenizer( ... "the eiffel tower surpassed the washington monument to become the tallest structure in the world. it was the first structure to reach a height of 300 metres in paris in 1930. it is now taller than the chrysler building by 5. 2 metres ( 17 ft ) and is the second tallest free - standing structure in paris.", ... return_tensors="pt", ... ).input_ids >>> # the forward function automatically creates the correct decoder_input_ids >>> loss = model(input_ids=input_ids, labels=labels).loss ``` Detailed [colab](https://colab.research.google.com/drive/1WIk2bxglElfZewOHboPFNj8H44_VAyKE?usp=sharing#scrollTo=ZwQIEhKOrJpl) for training. This model was contributed by [thomwolf](https://github.com/thomwolf). This model's TensorFlow and Flax versions were contributed by [ydshieh](https://github.com/ydshieh). ## EncoderDecoderConfig [[autodoc]] EncoderDecoderConfig <frameworkcontent> <pt> ## EncoderDecoderModel [[autodoc]] EncoderDecoderModel - forward - from_encoder_decoder_pretrained </pt> <tf> ## TFEncoderDecoderModel [[autodoc]] TFEncoderDecoderModel - call - from_encoder_decoder_pretrained </tf> <jax> ## FlaxEncoderDecoderModel [[autodoc]] FlaxEncoderDecoderModel - __call__ - from_encoder_decoder_pretrained </jax> </frameworkcontent>
transformers/docs/source/en/model_doc/encoder-decoder.md/0
{ "file_path": "transformers/docs/source/en/model_doc/encoder-decoder.md", "repo_id": "transformers", "token_count": 2664 }
<!--Copyright 2023 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Fuyu ## Overview The Fuyu model was created by [ADEPT](https://www.adept.ai/blog/fuyu-8b), and authored by Rohan Bavishi, Erich Elsen, Curtis Hawthorne, Maxwell Nye, Augustus Odena, Arushi Somani, Sağnak Taşırlar. The authors introduced Fuyu-8B, a decoder-only multimodal model based on the classic transformers architecture, with query and key normalization. A linear encoder is added to create multimodal embeddings from image inputs. By treating image tokens like text tokens and using a special image-newline character, the model knows when an image line ends. Image positional embeddings are removed. This avoids the need for different training phases for various image resolutions. With 8 billion parameters and licensed under CC-BY-NC, Fuyu-8B is notable for its ability to handle both text and images, its impressive context size of 16K, and its overall performance. <Tip warning={true}> The `Fuyu` models were trained using `bfloat16`, but the original inference uses `float16` The checkpoints uploaded on the hub use `torch_dtype = 'float16'` which will be used by the `AutoModel` API to cast the checkpoints from `torch.float32` to `torch.float16`. The `dtype` of the online weights is mostly irrelevant, unless you are using `torch_dtype="auto"` when initializing a model using `model = AutoModelForCausalLM.from_pretrained("path", torch_dtype = "auto")`. The reason is that the model will first be downloaded ( using the `dtype` of the checkpoints online) then it will be cast to the default `dtype` of `torch` (becomes `torch.float32`). Users should specify the `torch_dtype` they want, and if they don't it will be `torch.float32`. Finetuning the model in `float16` is not recommended and known to produce `nan`, as such the model should be fine-tuned in `bfloat16`. </Tip> Tips: - To convert the model, you need to clone the original repository using `git clone https://github.com/persimmon-ai-labs/adept-inference`, then get the checkpoints: ```bash git clone https://github.com/persimmon-ai-labs/adept-inference wget path/to/fuyu-8b-model-weights.tar tar -xvf fuyu-8b-model-weights.tar python src/transformers/models/fuyu/convert_fuyu_weights_to_hf.py --input_dir /path/to/downloaded/fuyu/weights/ --output_dir /output/path \ --pt_model_path /path/to/fuyu_8b_release/iter_0001251/mp_rank_00/model_optim_rng.pt --ada_lib_path /path/to/adept-inference ``` For the chat model: ```bash wget https://axtkn4xl5cip.objectstorage.us-phoenix-1.oci.customer-oci.com/n/axtkn4xl5cip/b/adept-public-data/o/8b_chat_model_release.tar tar -xvf 8b_base_model_release.tar ``` Then, model can be loaded via: ```py from transformers import FuyuConfig, FuyuForCausalLM model_config = FuyuConfig() model = FuyuForCausalLM(model_config).from_pretrained('/output/path') ``` Inputs need to be passed through a specific Processor to have the correct formats. A processor requires an image_processor and a tokenizer. Hence, inputs can be loaded via: ```py from PIL import Image from transformers import AutoTokenizer from transformers.models.fuyu.processing_fuyu import FuyuProcessor from transformers.models.fuyu.image_processing_fuyu import FuyuImageProcessor tokenizer = AutoTokenizer.from_pretrained('adept-hf-collab/fuyu-8b') image_processor = FuyuImageProcessor() processor = FuyuProcessor(image_processor=image_processor, tokenizer=tokenizer) text_prompt = "Generate a coco-style caption.\\n" bus_image_url = "https://huggingface.co/datasets/hf-internal-testing/fixtures-captioning/resolve/main/bus.png" bus_image_pil = Image.open(io.BytesIO(requests.get(bus_image_url).content)) inputs_to_model = processor(images=bus_image_pil, text=text_prompt) ``` This model was contributed by [Molbap](https://huggingface.co/Molbap). The original code can be found [here](https://github.com/persimmon-ai-labs/adept-inference). - Fuyu uses a `sentencepiece` based tokenizer, with a `Unigram` model. It supports bytefallback, which is only available in `tokenizers==0.14.0` for the fast tokenizer. The `LlamaTokenizer` is used as it is a standard wrapper around sentencepiece. - The authors suggest to use the following prompt for image captioning: `f"Generate a coco-style caption.\\n"` ## FuyuConfig [[autodoc]] FuyuConfig ## FuyuForCausalLM [[autodoc]] FuyuForCausalLM - forward ## FuyuImageProcessor [[autodoc]] FuyuImageProcessor - __call__ ## FuyuProcessor [[autodoc]] FuyuProcessor - __call__
transformers/docs/source/en/model_doc/fuyu.md/0
{ "file_path": "transformers/docs/source/en/model_doc/fuyu.md", "repo_id": "transformers", "token_count": 1652 }
<!--Copyright 2023 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # LLaVa ## Overview LLaVa is an open-source chatbot trained by fine-tuning LlamA/Vicuna on GPT-generated multimodal instruction-following data. It is an auto-regressive language model, based on the transformer architecture. In other words, it is an multi-modal version of LLMs fine-tuned for chat / instructions. The LLaVa model was proposed in [Visual Instruction Tuning](https://arxiv.org/abs/2304.08485) and improved in [Improved Baselines with Visual Instruction Tuning](https://arxiv.org/pdf/2310.03744) by Haotian Liu, Chunyuan Li, Yuheng Li and Yong Jae Lee. The abstract from the paper is the following: *Large multimodal models (LMM) have recently shown encouraging progress with visual instruction tuning. In this note, we show that the fully-connected vision-language cross-modal connector in LLaVA is surprisingly powerful and data-efficient. With simple modifications to LLaVA, namely, using CLIP-ViT-L-336px with an MLP projection and adding academic-task-oriented VQA data with simple response formatting prompts, we establish stronger baselines that achieve state-of-the-art across 11 benchmarks. Our final 13B checkpoint uses merely 1.2M publicly available data, and finishes full training in ∼1 day on a single 8-A100 node. We hope this can make state-of-the-art LMM research more accessible. Code and model will be publicly available* <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/llava_architecture.jpg" alt="drawing" width="600"/> <small> LLaVa architecture. Taken from the <a href="https://arxiv.org/abs/2304.08485">original paper.</a> </small> This model was contributed by [ArthurZ](https://huggingface.co/ArthurZ) and [ybelkada](https://huggingface.co/ybelkada). The original code can be found [here](https://github.com/haotian-liu/LLaVA/tree/main/llava). ## Usage tips - We advise users to use `padding_side="left"` when computing batched generation as it leads to more accurate results. Simply make sure to call `processor.tokenizer.padding_side = "left"` before generating. - Note the model has not been explicitly trained to process multiple images in the same prompt, although this is technically possible, you may experience inaccurate results. > [!NOTE] > LLaVA models after release v4.46 will raise warnings about adding `processor.patch_size = {{patch_size}}`, `processor.num_additional_image_tokens = {{num_additional_image_tokens}}` and processor.vision_feature_select_strategy = {{vision_feature_select_strategy}}`. It is strongly recommended to add the attributes to the processor if you own the model checkpoint, or open a PR if it is not owned by you. Adding these attributes means that LLaVA will try to infer the number of image tokens required per image and expand the text with as many `<image>` placeholders as there will be tokens. Usually it is around 500 tokens per image, so make sure that the text is not truncated as otherwise there will be failure when merging the embeddings. The attributes can be obtained from model config, as `model.config.vision_config.patch_size` or `model.config.vision_feature_select_strategy`. The `num_additional_image_tokens` should be `1` if the vision backbone adds a CLS token or `0` if nothing extra is added to the vision patches. ### Single image inference For best results, we recommend users to use the processor's `apply_chat_template()` method to format your prompt correctly. For that you need to construct a conversation history, passing in a plain string will not format your prompt. Each message in the conversation history for chat templates is a dictionary with keys "role" and "content". The "content" should be a list of dictionaries, for "text" and "image" modalities, as follows: ```python from transformers import AutoProcessor processor = AutoProcessor.from_pretrained("llava-hf/llava-1.5-7b-hf") conversation = [ { "role": "user", "content": [ {"type": "image"}, {"type": "text", "text": "What’s shown in this image?"}, ], }, { "role": "assistant", "content": [{"type": "text", "text": "This image shows a red stop sign."},] }, { "role": "user", "content": [ {"type": "text", "text": "Describe the image in more details."}, ], }, ] text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True) # Note that the template simply formats your prompt, you still have to tokenize it and obtain pixel values for your images print(text_prompt) >>> "USER: <image>\n<What’s shown in this image? ASSISTANT: This image shows a red stop sign.</s>USER: Describe the image in more details. ASSISTANT:" ``` ### Batched inference LLaVa also supports batched inference. Here is how you can do it: ```python import requests from PIL import Image import torch from transformers import AutoProcessor, LlavaForConditionalGeneration # Load the model in half-precision model = LlavaForConditionalGeneration.from_pretrained("llava-hf/llava-1.5-7b-hf", torch_dtype=torch.float16, device_map="auto") processor = AutoProcessor.from_pretrained("llava-hf/llava-1.5-7b-hf") # Get two different images url = "https://www.ilankelman.org/stopsigns/australia.jpg" image_stop = Image.open(requests.get(url, stream=True).raw) url = "http://images.cocodataset.org/val2017/000000039769.jpg" image_cats = Image.open(requests.get(url, stream=True).raw) # Prepare a batch of two prompts conversation_1 = [ { "role": "user", "content": [ {"type": "image"}, {"type": "text", "text": "What is shown in this image?"}, ], }, ] conversation_2 = [ { "role": "user", "content": [ {"type": "image"}, {"type": "text", "text": "What is shown in this image?"}, ], }, ] prompt_1 = processor.apply_chat_template(conversation_1, add_generation_prompt=True) prompt_2 = processor.apply_chat_template(conversation_2, add_generation_prompt=True) prompts = [prompt_1, prompt_2] # We can simply feed images in the order they have to be used in the text prompt inputs = processor(images=[image_stop, image_cats], text=prompts, padding=True, return_tensors="pt").to(model.device, torch.float16) # Generate generate_ids = model.generate(**inputs, max_new_tokens=30) processor.batch_decode(generate_ids, skip_special_tokens=True) ``` - If you want to construct a chat prompt yourself, below is a list of prompt formats accepted by each llava checkpoint: [llava-interleave models](https://huggingface.co/collections/llava-hf/llava-interleave-668e19a97da0036aad4a2f19) requires the following format: ```bash "<|im_start|>user <image>\nWhat is shown in this image?<|im_end|><|im_start|>assistant" ``` For multiple turns conversation: ```bash "<|im_start|>user <image>\n<prompt1><|im_end|><|im_start|>assistant <answer1><|im_end|><|im_start|>user <image>\n<prompt1><|im_end|><|im_start|>assistant " ``` [llava-1.5 models](https://huggingface.co/collections/llava-hf/llava-15-65f762d5b6941db5c2ba07e0) requires the following format: ```bash "USER: <image>\n<prompt> ASSISTANT:" ``` For multiple turns conversation: ```bash "USER: <image>\n<prompt1> ASSISTANT: <answer1></s>USER: <prompt2> ASSISTANT: <answer2></s>USER: <prompt3> ASSISTANT:" ``` ## Note regarding reproducing original implementation In order to match the logits of the [original implementation](https://github.com/haotian-liu/LLaVA/tree/main), one needs to additionally specify `do_pad=True` when instantiating `LLavaImageProcessor`: ```python from transformers import LLavaImageProcessor image_processor = LLavaImageProcessor.from_pretrained("https://huggingface.co/llava-hf/llava-1.5-7b-hf", do_pad=True) ``` ### Using Flash Attention 2 Flash Attention 2 is an even faster, optimized version of the previous optimization, please refer to the [Flash Attention 2 section of performance docs](https://huggingface.co/docs/transformers/perf_infer_gpu_one). ## Resources A list of official Hugging Face and community (indicated by 🌎) resources to help you get started with BEiT. <PipelineTag pipeline="image-to-text"/> - A [Google Colab demo](https://colab.research.google.com/drive/1qsl6cd2c8gGtEW1xV5io7S8NHh-Cp1TV?usp=sharing) on how to run Llava on a free-tier Google colab instance leveraging 4-bit inference. - A [similar notebook](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/LLaVa/Inference_with_LLaVa_for_multimodal_generation.ipynb) showcasing batched inference. 🌎 ## LlavaConfig [[autodoc]] LlavaConfig ## LlavaImageProcessor [[autodoc]] LlavaImageProcessor - preprocess ## LlavaImageProcessorFast [[autodoc]] LlavaImageProcessorFast - preprocess ## LlavaProcessor [[autodoc]] LlavaProcessor ## LlavaForConditionalGeneration [[autodoc]] LlavaForConditionalGeneration - forward
transformers/docs/source/en/model_doc/llava.md/0
{ "file_path": "transformers/docs/source/en/model_doc/llava.md", "repo_id": "transformers", "token_count": 3115 }
<!--Copyright 2023 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # NLLB-MOE ## Overview The NLLB model was presented in [No Language Left Behind: Scaling Human-Centered Machine Translation](https://arxiv.org/abs/2207.04672) by Marta R. Costa-jussà, James Cross, Onur Çelebi, Maha Elbayad, Kenneth Heafield, Kevin Heffernan, Elahe Kalbassi, Janice Lam, Daniel Licht, Jean Maillard, Anna Sun, Skyler Wang, Guillaume Wenzek, Al Youngblood, Bapi Akula, Loic Barrault, Gabriel Mejia Gonzalez, Prangthip Hansanti, John Hoffman, Semarley Jarrett, Kaushik Ram Sadagopan, Dirk Rowe, Shannon Spruit, Chau Tran, Pierre Andrews, Necip Fazil Ayan, Shruti Bhosale, Sergey Edunov, Angela Fan, Cynthia Gao, Vedanuj Goswami, Francisco Guzmán, Philipp Koehn, Alexandre Mourachko, Christophe Ropers, Safiyyah Saleem, Holger Schwenk, and Jeff Wang. The abstract of the paper is the following: *Driven by the goal of eradicating language barriers on a global scale, machine translation has solidified itself as a key focus of artificial intelligence research today. However, such efforts have coalesced around a small subset of languages, leaving behind the vast majority of mostly low-resource languages. What does it take to break the 200 language barrier while ensuring safe, high quality results, all while keeping ethical considerations in mind? In No Language Left Behind, we took on this challenge by first contextualizing the need for low-resource language translation support through exploratory interviews with native speakers. Then, we created datasets and models aimed at narrowing the performance gap between low and high-resource languages. More specifically, we developed a conditional compute model based on Sparsely Gated Mixture of Experts that is trained on data obtained with novel and effective data mining techniques tailored for low-resource languages. We propose multiple architectural and training improvements to counteract overfitting while training on thousands of tasks. Critically, we evaluated the performance of over 40,000 different translation directions using a human-translated benchmark, Flores-200, and combined human evaluation with a novel toxicity benchmark covering all languages in Flores-200 to assess translation safety. Our model achieves an improvement of 44% BLEU relative to the previous state-of-the-art, laying important groundwork towards realizing a universal translation system.* This model was contributed by [Arthur Zucker](https://huggingface.co/ArthurZ). The original code can be found [here](https://github.com/facebookresearch/fairseq). ## Usage tips - M2M100ForConditionalGeneration is the base model for both NLLB and NLLB MoE - The NLLB-MoE is very similar to the NLLB model, but it's feed forward layer is based on the implementation of SwitchTransformers. - The tokenizer is the same as the NLLB models. ## Implementation differences with SwitchTransformers The biggest difference is the way the tokens are routed. NLLB-MoE uses a `top-2-gate` which means that for each input, only the top two experts are selected based on the highest predicted probabilities from the gating network, and the remaining experts are ignored. In `SwitchTransformers`, only the top-1 probabilities are computed, which means that tokens have less probability of being forwarded. Moreover, if a token is not routed to any expert, `SwitchTransformers` still adds its unmodified hidden states (kind of like a residual connection) while they are masked in `NLLB`'s top-2 routing mechanism. ## Generating with NLLB-MoE The available checkpoints require around 350GB of storage. Make sure to use `accelerate` if you do not have enough RAM on your machine. While generating the target text set the `forced_bos_token_id` to the target language id. The following example shows how to translate English to French using the *facebook/nllb-200-distilled-600M* model. Note that we're using the BCP-47 code for French `fra_Latn`. See [here](https://github.com/facebookresearch/flores/blob/main/flores200/README.md#languages-in-flores-200) for the list of all BCP-47 in the Flores 200 dataset. ```python >>> from transformers import AutoModelForSeq2SeqLM, AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("facebook/nllb-moe-54b") >>> model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-moe-54b") >>> article = "Previously, Ring's CEO, Jamie Siminoff, remarked the company started when his doorbell wasn't audible from his shop in his garage." >>> inputs = tokenizer(article, return_tensors="pt") >>> translated_tokens = model.generate( ... **inputs, forced_bos_token_id=tokenizer.lang_code_to_id["fra_Latn"], max_length=50 ... ) >>> tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0] "Auparavant, le PDG de Ring, Jamie Siminoff, a fait remarquer que la société avait commencé lorsque sa sonnette n'était pas audible depuis son magasin dans son garage." ``` ### Generating from any other language than English English (`eng_Latn`) is set as the default language from which to translate. In order to specify that you'd like to translate from a different language, you should specify the BCP-47 code in the `src_lang` keyword argument of the tokenizer initialization. See example below for a translation from romanian to german: ```python >>> from transformers import AutoModelForSeq2SeqLM, AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("facebook/nllb-moe-54b", src_lang="ron_Latn") >>> model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-moe-54b") >>> article = "Şeful ONU spune că nu există o soluţie militară în Siria" >>> inputs = tokenizer(article, return_tensors="pt") >>> translated_tokens = model.generate( ... **inputs, forced_bos_token_id=tokenizer.lang_code_to_id["deu_Latn"], max_length=30 ... ) >>> tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0] ``` ## Resources - [Translation task guide](../tasks/translation) - [Summarization task guide](../tasks/summarization) ## NllbMoeConfig [[autodoc]] NllbMoeConfig ## NllbMoeTop2Router [[autodoc]] NllbMoeTop2Router - route_tokens - forward ## NllbMoeSparseMLP [[autodoc]] NllbMoeSparseMLP - forward ## NllbMoeModel [[autodoc]] NllbMoeModel - forward ## NllbMoeForConditionalGeneration [[autodoc]] NllbMoeForConditionalGeneration - forward
transformers/docs/source/en/model_doc/nllb-moe.md/0
{ "file_path": "transformers/docs/source/en/model_doc/nllb-moe.md", "repo_id": "transformers", "token_count": 2003 }
<!--Copyright 2020 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # RoBERTa <div class="flex flex-wrap space-x-1"> <a href="https://huggingface.co/models?filter=roberta"> <img alt="Models" src="https://img.shields.io/badge/All_model_pages-roberta-blueviolet"> </a> <a href="https://huggingface.co/spaces/docs-demos/roberta-base"> <img alt="Spaces" src="https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue"> </a> <a href="https://huggingface.co/papers/1907.11692"> <img alt="Paper page" src="https://img.shields.io/badge/Paper%20page-1907.11692-green"> </a> </div> ## Overview The RoBERTa model was proposed in [RoBERTa: A Robustly Optimized BERT Pretraining Approach](https://arxiv.org/abs/1907.11692) by Yinhan Liu, [Myle Ott](https://huggingface.co/myleott), Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov. It is based on Google's BERT model released in 2018. It builds on BERT and modifies key hyperparameters, removing the next-sentence pretraining objective and training with much larger mini-batches and learning rates. The abstract from the paper is the following: *Language model pretraining has led to significant performance gains but careful comparison between different approaches is challenging. Training is computationally expensive, often done on private datasets of different sizes, and, as we will show, hyperparameter choices have significant impact on the final results. We present a replication study of BERT pretraining (Devlin et al., 2019) that carefully measures the impact of many key hyperparameters and training data size. We find that BERT was significantly undertrained, and can match or exceed the performance of every model published after it. Our best model achieves state-of-the-art results on GLUE, RACE and SQuAD. These results highlight the importance of previously overlooked design choices, and raise questions about the source of recently reported improvements. We release our models and code.* This model was contributed by [julien-c](https://huggingface.co/julien-c). The original code can be found [here](https://github.com/pytorch/fairseq/tree/master/examples/roberta). ## Usage tips - This implementation is the same as [`BertModel`] with a minor tweak to the embeddings, as well as a setup for RoBERTa pretrained models. - RoBERTa has the same architecture as BERT but uses a byte-level BPE as a tokenizer (same as GPT-2) and uses a different pretraining scheme. - RoBERTa doesn't have `token_type_ids`, so you don't need to indicate which token belongs to which segment. Just separate your segments with the separation token `tokenizer.sep_token` (or `</s>`). - RoBERTa is similar to BERT but with better pretraining techniques: * Dynamic masking: tokens are masked differently at each epoch, whereas BERT does it once and for all. * Sentence packing: Sentences are packed together to reach 512 tokens (so the sentences are in an order that may span several documents). * Larger batches: Training uses larger batches. * Byte-level BPE vocabulary: Uses BPE with bytes as a subunit instead of characters, accommodating Unicode characters. - [CamemBERT](camembert) is a wrapper around RoBERTa. Refer to its model page for usage examples. ## Resources A list of official Hugging Face and community (indicated by 🌎) resources to help you get started with RoBERTa. If you're interested in submitting a resource to be included here, please feel free to open a Pull Request and we'll review it! The resource should ideally demonstrate something new instead of duplicating an existing resource. <PipelineTag pipeline="text-classification"/> - A blog on [Getting Started with Sentiment Analysis on Twitter](https://huggingface.co/blog/sentiment-analysis-twitter) using RoBERTa and the [Inference API](https://huggingface.co/inference-api). - A blog on [Opinion Classification with Kili and Hugging Face AutoTrain](https://huggingface.co/blog/opinion-classification-with-kili) using RoBERTa. - A notebook on how to [finetune RoBERTa for sentiment analysis](https://colab.research.google.com/github/DhavalTaunk08/NLP_scripts/blob/master/sentiment_analysis_using_roberta.ipynb). 🌎 - [`RobertaForSequenceClassification`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/pytorch/text-classification) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification.ipynb). - [`TFRobertaForSequenceClassification`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/text-classification) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification-tf.ipynb). - [`FlaxRobertaForSequenceClassification`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/flax/text-classification) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification_flax.ipynb). - [Text classification task guide](../tasks/sequence_classification) <PipelineTag pipeline="token-classification"/> - [`RobertaForTokenClassification`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/pytorch/token-classification) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification.ipynb). - [`TFRobertaForTokenClassification`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/token-classification) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification-tf.ipynb). - [`FlaxRobertaForTokenClassification`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/flax/token-classification). - [Token classification](https://huggingface.co/course/chapter7/2?fw=pt) chapter of the 🤗 Hugging Face Course. - [Token classification task guide](../tasks/token_classification) <PipelineTag pipeline="fill-mask"/> - A blog on [How to train a new language model from scratch using Transformers and Tokenizers](https://huggingface.co/blog/how-to-train) with RoBERTa. - [`RobertaForMaskedLM`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/pytorch/language-modeling#robertabertdistilbert-and-masked-language-modeling) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb). - [`TFRobertaForMaskedLM`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/language-modeling#run_mlmpy) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb). - [`FlaxRobertaForMaskedLM`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/flax/language-modeling#masked-language-modeling) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/masked_language_modeling_flax.ipynb). - [Masked language modeling](https://huggingface.co/course/chapter7/3?fw=pt) chapter of the 🤗 Hugging Face Course. - [Masked language modeling task guide](../tasks/masked_language_modeling) <PipelineTag pipeline="question-answering"/> - A blog on [Accelerated Inference with Optimum and Transformers Pipelines](https://huggingface.co/blog/optimum-inference) with RoBERTa for question answering. - [`RobertaForQuestionAnswering`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering.ipynb). - [`TFRobertaForQuestionAnswering`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/question-answering) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering-tf.ipynb). - [`FlaxRobertaForQuestionAnswering`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/flax/question-answering). - [Question answering](https://huggingface.co/course/chapter7/7?fw=pt) chapter of the 🤗 Hugging Face Course. - [Question answering task guide](../tasks/question_answering) **Multiple choice** - [`RobertaForMultipleChoice`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/pytorch/multiple-choice) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice.ipynb). - [`TFRobertaForMultipleChoice`] is supported by this [example script](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/multiple-choice) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice-tf.ipynb). - [Multiple choice task guide](../tasks/multiple_choice) ## RobertaConfig [[autodoc]] RobertaConfig ## RobertaTokenizer [[autodoc]] RobertaTokenizer - build_inputs_with_special_tokens - get_special_tokens_mask - create_token_type_ids_from_sequences - save_vocabulary ## RobertaTokenizerFast [[autodoc]] RobertaTokenizerFast - build_inputs_with_special_tokens <frameworkcontent> <pt> ## RobertaModel [[autodoc]] RobertaModel - forward ## RobertaForCausalLM [[autodoc]] RobertaForCausalLM - forward ## RobertaForMaskedLM [[autodoc]] RobertaForMaskedLM - forward ## RobertaForSequenceClassification [[autodoc]] RobertaForSequenceClassification - forward ## RobertaForMultipleChoice [[autodoc]] RobertaForMultipleChoice - forward ## RobertaForTokenClassification [[autodoc]] RobertaForTokenClassification - forward ## RobertaForQuestionAnswering [[autodoc]] RobertaForQuestionAnswering - forward </pt> <tf> ## TFRobertaModel [[autodoc]] TFRobertaModel - call ## TFRobertaForCausalLM [[autodoc]] TFRobertaForCausalLM - call ## TFRobertaForMaskedLM [[autodoc]] TFRobertaForMaskedLM - call ## TFRobertaForSequenceClassification [[autodoc]] TFRobertaForSequenceClassification - call ## TFRobertaForMultipleChoice [[autodoc]] TFRobertaForMultipleChoice - call ## TFRobertaForTokenClassification [[autodoc]] TFRobertaForTokenClassification - call ## TFRobertaForQuestionAnswering [[autodoc]] TFRobertaForQuestionAnswering - call </tf> <jax> ## FlaxRobertaModel [[autodoc]] FlaxRobertaModel - __call__ ## FlaxRobertaForCausalLM [[autodoc]] FlaxRobertaForCausalLM - __call__ ## FlaxRobertaForMaskedLM [[autodoc]] FlaxRobertaForMaskedLM - __call__ ## FlaxRobertaForSequenceClassification [[autodoc]] FlaxRobertaForSequenceClassification - __call__ ## FlaxRobertaForMultipleChoice [[autodoc]] FlaxRobertaForMultipleChoice - __call__ ## FlaxRobertaForTokenClassification [[autodoc]] FlaxRobertaForTokenClassification - __call__ ## FlaxRobertaForQuestionAnswering [[autodoc]] FlaxRobertaForQuestionAnswering - __call__ </jax> </frameworkcontent>
transformers/docs/source/en/model_doc/roberta.md/0
{ "file_path": "transformers/docs/source/en/model_doc/roberta.md", "repo_id": "transformers", "token_count": 3812 }
<!--Copyright 2021 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # Speech2Text2 <Tip warning={true}> This model is in maintenance mode only, we don't accept any new PRs changing its code. If you run into any issues running this model, please reinstall the last version that supported this model: v4.40.2. You can do so by running the following command: `pip install -U transformers==4.40.2`. </Tip> ## Overview The Speech2Text2 model is used together with [Wav2Vec2](wav2vec2) for Speech Translation models proposed in [Large-Scale Self- and Semi-Supervised Learning for Speech Translation](https://arxiv.org/abs/2104.06678) by Changhan Wang, Anne Wu, Juan Pino, Alexei Baevski, Michael Auli, Alexis Conneau. Speech2Text2 is a *decoder-only* transformer model that can be used with any speech *encoder-only*, such as [Wav2Vec2](wav2vec2) or [HuBERT](hubert) for Speech-to-Text tasks. Please refer to the [SpeechEncoderDecoder](speech-encoder-decoder) class on how to combine Speech2Text2 with any speech *encoder-only* model. This model was contributed by [Patrick von Platen](https://huggingface.co/patrickvonplaten). The original code can be found [here](https://github.com/pytorch/fairseq/blob/1f7ef9ed1e1061f8c7f88f8b94c7186834398690/fairseq/models/wav2vec/wav2vec2_asr.py#L266). ## Usage tips - Speech2Text2 achieves state-of-the-art results on the CoVoST Speech Translation dataset. For more information, see the [official models](https://huggingface.co/models?other=speech2text2) . - Speech2Text2 is always used within the [SpeechEncoderDecoder](speech-encoder-decoder) framework. - Speech2Text2's tokenizer is based on [fastBPE](https://github.com/glample/fastBPE). ## Inference Speech2Text2's [`SpeechEncoderDecoderModel`] model accepts raw waveform input values from speech and makes use of [`~generation.GenerationMixin.generate`] to translate the input speech autoregressively to the target language. The [`Wav2Vec2FeatureExtractor`] class is responsible for preprocessing the input speech and [`Speech2Text2Tokenizer`] decodes the generated target tokens to the target string. The [`Speech2Text2Processor`] wraps [`Wav2Vec2FeatureExtractor`] and [`Speech2Text2Tokenizer`] into a single instance to both extract the input features and decode the predicted token ids. - Step-by-step Speech Translation ```python >>> import torch >>> from transformers import Speech2Text2Processor, SpeechEncoderDecoderModel >>> from datasets import load_dataset >>> import soundfile as sf >>> model = SpeechEncoderDecoderModel.from_pretrained("facebook/s2t-wav2vec2-large-en-de") >>> processor = Speech2Text2Processor.from_pretrained("facebook/s2t-wav2vec2-large-en-de") >>> def map_to_array(batch): ... speech, _ = sf.read(batch["file"]) ... batch["speech"] = speech ... return batch >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") >>> ds = ds.map(map_to_array) >>> inputs = processor(ds["speech"][0], sampling_rate=16_000, return_tensors="pt") >>> generated_ids = model.generate(inputs=inputs["input_values"], attention_mask=inputs["attention_mask"]) >>> transcription = processor.batch_decode(generated_ids) ``` - Speech Translation via Pipelines The automatic speech recognition pipeline can also be used to translate speech in just a couple lines of code ```python >>> from datasets import load_dataset >>> from transformers import pipeline >>> librispeech_en = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") >>> asr = pipeline( ... "automatic-speech-recognition", ... model="facebook/s2t-wav2vec2-large-en-de", ... feature_extractor="facebook/s2t-wav2vec2-large-en-de", ... ) >>> translation_de = asr(librispeech_en[0]["file"]) ``` See [model hub](https://huggingface.co/models?filter=speech2text2) to look for Speech2Text2 checkpoints. ## Resources - [Causal language modeling task guide](../tasks/language_modeling) ## Speech2Text2Config [[autodoc]] Speech2Text2Config ## Speech2TextTokenizer [[autodoc]] Speech2Text2Tokenizer - batch_decode - decode - save_vocabulary ## Speech2Text2Processor [[autodoc]] Speech2Text2Processor - __call__ - from_pretrained - save_pretrained - batch_decode - decode ## Speech2Text2ForCausalLM [[autodoc]] Speech2Text2ForCausalLM - forward
transformers/docs/source/en/model_doc/speech_to_text_2.md/0
{ "file_path": "transformers/docs/source/en/model_doc/speech_to_text_2.md", "repo_id": "transformers", "token_count": 1610 }
<!--Copyright 2020 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. --> # XLNet <div class="flex flex-wrap space-x-1"> <a href="https://huggingface.co/models?filter=xlnet"> <img alt="Models" src="https://img.shields.io/badge/All_model_pages-xlnet-blueviolet"> </a> <a href="https://huggingface.co/spaces/docs-demos/xlnet-base-cased"> <img alt="Spaces" src="https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue"> </a> </div> ## Overview The XLNet model was proposed in [XLNet: Generalized Autoregressive Pretraining for Language Understanding](https://arxiv.org/abs/1906.08237) by Zhilin Yang, Zihang Dai, Yiming Yang, Jaime Carbonell, Ruslan Salakhutdinov, Quoc V. Le. XLnet is an extension of the Transformer-XL model pre-trained using an autoregressive method to learn bidirectional contexts by maximizing the expected likelihood over all permutations of the input sequence factorization order. The abstract from the paper is the following: *With the capability of modeling bidirectional contexts, denoising autoencoding based pretraining like BERT achieves better performance than pretraining approaches based on autoregressive language modeling. However, relying on corrupting the input with masks, BERT neglects dependency between the masked positions and suffers from a pretrain-finetune discrepancy. In light of these pros and cons, we propose XLNet, a generalized autoregressive pretraining method that (1) enables learning bidirectional contexts by maximizing the expected likelihood over all permutations of the factorization order and (2) overcomes the limitations of BERT thanks to its autoregressive formulation. Furthermore, XLNet integrates ideas from Transformer-XL, the state-of-the-art autoregressive model, into pretraining. Empirically, under comparable experiment settings, XLNet outperforms BERT on 20 tasks, often by a large margin, including question answering, natural language inference, sentiment analysis, and document ranking.* This model was contributed by [thomwolf](https://huggingface.co/thomwolf). The original code can be found [here](https://github.com/zihangdai/xlnet/). ## Usage tips - The specific attention pattern can be controlled at training and test time using the `perm_mask` input. - Due to the difficulty of training a fully auto-regressive model over various factorization order, XLNet is pretrained using only a sub-set of the output tokens as target which are selected with the `target_mapping` input. - To use XLNet for sequential decoding (i.e. not in fully bi-directional setting), use the `perm_mask` and `target_mapping` inputs to control the attention span and outputs (see examples in *examples/pytorch/text-generation/run_generation.py*) - XLNet is one of the few models that has no sequence length limit. - XLNet is not a traditional autoregressive model but uses a training strategy that builds on that. It permutes the tokens in the sentence, then allows the model to use the last n tokens to predict the token n+1. Since this is all done with a mask, the sentence is actually fed in the model in the right order, but instead of masking the first n tokens for n+1, XLNet uses a mask that hides the previous tokens in some given permutation of 1,…,sequence length. - XLNet also uses the same recurrence mechanism as Transformer-XL to build long-term dependencies. ## Resources - [Text classification task guide](../tasks/sequence_classification) - [Token classification task guide](../tasks/token_classification) - [Question answering task guide](../tasks/question_answering) - [Causal language modeling task guide](../tasks/language_modeling) - [Multiple choice task guide](../tasks/multiple_choice) ## XLNetConfig [[autodoc]] XLNetConfig ## XLNetTokenizer [[autodoc]] XLNetTokenizer - build_inputs_with_special_tokens - get_special_tokens_mask - create_token_type_ids_from_sequences - save_vocabulary ## XLNetTokenizerFast [[autodoc]] XLNetTokenizerFast ## XLNet specific outputs [[autodoc]] models.xlnet.modeling_xlnet.XLNetModelOutput [[autodoc]] models.xlnet.modeling_xlnet.XLNetLMHeadModelOutput [[autodoc]] models.xlnet.modeling_xlnet.XLNetForSequenceClassificationOutput [[autodoc]] models.xlnet.modeling_xlnet.XLNetForMultipleChoiceOutput [[autodoc]] models.xlnet.modeling_xlnet.XLNetForTokenClassificationOutput [[autodoc]] models.xlnet.modeling_xlnet.XLNetForQuestionAnsweringSimpleOutput [[autodoc]] models.xlnet.modeling_xlnet.XLNetForQuestionAnsweringOutput [[autodoc]] models.xlnet.modeling_tf_xlnet.TFXLNetModelOutput [[autodoc]] models.xlnet.modeling_tf_xlnet.TFXLNetLMHeadModelOutput [[autodoc]] models.xlnet.modeling_tf_xlnet.TFXLNetForSequenceClassificationOutput [[autodoc]] models.xlnet.modeling_tf_xlnet.TFXLNetForMultipleChoiceOutput [[autodoc]] models.xlnet.modeling_tf_xlnet.TFXLNetForTokenClassificationOutput [[autodoc]] models.xlnet.modeling_tf_xlnet.TFXLNetForQuestionAnsweringSimpleOutput <frameworkcontent> <pt> ## XLNetModel [[autodoc]] XLNetModel - forward ## XLNetLMHeadModel [[autodoc]] XLNetLMHeadModel - forward ## XLNetForSequenceClassification [[autodoc]] XLNetForSequenceClassification - forward ## XLNetForMultipleChoice [[autodoc]] XLNetForMultipleChoice - forward ## XLNetForTokenClassification [[autodoc]] XLNetForTokenClassification - forward ## XLNetForQuestionAnsweringSimple [[autodoc]] XLNetForQuestionAnsweringSimple - forward ## XLNetForQuestionAnswering [[autodoc]] XLNetForQuestionAnswering - forward </pt> <tf> ## TFXLNetModel [[autodoc]] TFXLNetModel - call ## TFXLNetLMHeadModel [[autodoc]] TFXLNetLMHeadModel - call ## TFXLNetForSequenceClassification [[autodoc]] TFXLNetForSequenceClassification - call ## TFXLNetForMultipleChoice [[autodoc]] TFXLNetForMultipleChoice - call ## TFXLNetForTokenClassification [[autodoc]] TFXLNetForTokenClassification - call ## TFXLNetForQuestionAnsweringSimple [[autodoc]] TFXLNetForQuestionAnsweringSimple - call </tf> </frameworkcontent>
transformers/docs/source/en/model_doc/xlnet.md/0
{ "file_path": "transformers/docs/source/en/model_doc/xlnet.md", "repo_id": "transformers", "token_count": 2043 }