TensorRT-LLMs/llm-api/index.html
2024-12-25 13:44:02 +08:00

329 lines
23 KiB
HTML

<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="../">
<head>
<meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>API Introduction &mdash; tensorrt_llm documentation</title>
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
<link rel="stylesheet" type="text/css" href="../_static/css/theme.css?v=e59714d7" />
<link rel="stylesheet" type="text/css" href="../_static/copybutton.css?v=76b2166b" />
<script src="../_static/jquery.js?v=5d32c60e"></script>
<script src="../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
<script src="../_static/documentation_options.js?v=5929fcd5"></script>
<script src="../_static/doctools.js?v=888ff710"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../_static/clipboard.min.js?v=a7894cd8"></script>
<script src="../_static/copybutton.js?v=65e89d2a"></script>
<script src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="API Reference" href="reference.html" />
<link rel="prev" title="Installing on Grace Hopper" href="../installation/grace-hopper.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../index.html" class="icon icon-home">
tensorrt_llm
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<p class="caption" role="heading"><span class="caption-text">Getting Started</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../overview.html">Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="../quick-start-guide.html">Quick Start Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="../key-features.html">Key Features</a></li>
<li class="toctree-l1"><a class="reference internal" href="../release-notes.html">Release Notes</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Installation</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../installation/linux.html">Installing on Linux</a></li>
<li class="toctree-l1"><a class="reference internal" href="../installation/build-from-source-linux.html">Building from Source Code on Linux</a></li>
<li class="toctree-l1"><a class="reference internal" href="../installation/windows.html">Installing on Windows</a></li>
<li class="toctree-l1"><a class="reference internal" href="../installation/build-from-source-windows.html">Building from Source Code on Windows</a></li>
<li class="toctree-l1"><a class="reference internal" href="../installation/grace-hopper.html">Installing on Grace Hopper</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">LLM API</span></p>
<ul class="current">
<li class="toctree-l1 current"><a class="current reference internal" href="#">API Introduction</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#supported-models">Supported Models</a></li>
<li class="toctree-l2"><a class="reference internal" href="#model-preparation">Model Preparation</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#hugging-face-hub">Hugging Face Hub</a></li>
<li class="toctree-l3"><a class="reference internal" href="#local-hugging-face-models">Local Hugging Face Models</a></li>
<li class="toctree-l3"><a class="reference internal" href="#local-tensorrt-llm-engine">Local TensorRT-LLM Engine</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#tips-and-troubleshooting">Tips and Troubleshooting</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="reference.html">API Reference</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">LLM API Examples</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../llm-api-examples/index.html">LLM Examples Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../llm-api-examples/customization.html">Common Customizations</a></li>
<li class="toctree-l1"><a class="reference internal" href="../llm-api-examples/llm_api_examples.html">Examples</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Model Definition API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../python-api/tensorrt_llm.layers.html">Layers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../python-api/tensorrt_llm.functional.html">Functionals</a></li>
<li class="toctree-l1"><a class="reference internal" href="../python-api/tensorrt_llm.models.html">Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../python-api/tensorrt_llm.plugin.html">Plugin</a></li>
<li class="toctree-l1"><a class="reference internal" href="../python-api/tensorrt_llm.quantization.html">Quantization</a></li>
<li class="toctree-l1"><a class="reference internal" href="../python-api/tensorrt_llm.runtime.html">Runtime</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../_cpp_gen/executor.html">Executor</a></li>
<li class="toctree-l1"><a class="reference internal" href="../_cpp_gen/runtime.html">Runtime</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Command-Line Reference</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../commands/trtllm-build.html">trtllm-build</a></li>
<li class="toctree-l1"><a class="reference internal" href="../commands/trtllm-serve.html">trtllm-serve</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Architecture</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../architecture/overview.html">TensorRT-LLM Architecture</a></li>
<li class="toctree-l1"><a class="reference internal" href="../architecture/core-concepts.html">Model Definition</a></li>
<li class="toctree-l1"><a class="reference internal" href="../architecture/core-concepts.html#compilation">Compilation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../architecture/core-concepts.html#runtime">Runtime</a></li>
<li class="toctree-l1"><a class="reference internal" href="../architecture/core-concepts.html#multi-gpu-and-multi-node-support">Multi-GPU and Multi-Node Support</a></li>
<li class="toctree-l1"><a class="reference internal" href="../architecture/checkpoint.html">TensorRT-LLM Checkpoint</a></li>
<li class="toctree-l1"><a class="reference internal" href="../architecture/workflow.html">TensorRT-LLM Build Workflow</a></li>
<li class="toctree-l1"><a class="reference internal" href="../architecture/add-model.html">Adding a Model</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Advanced</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../advanced/gpt-attention.html">Multi-Head, Multi-Query, and Group-Query Attention</a></li>
<li class="toctree-l1"><a class="reference internal" href="../advanced/gpt-runtime.html">C++ GPT Runtime</a></li>
<li class="toctree-l1"><a class="reference internal" href="../advanced/executor.html">Executor API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../advanced/graph-rewriting.html">Graph Rewriting Module</a></li>
<li class="toctree-l1"><a class="reference internal" href="../advanced/inference-request.html">Inference Request</a></li>
<li class="toctree-l1"><a class="reference internal" href="../advanced/inference-request.html#responses">Responses</a></li>
<li class="toctree-l1"><a class="reference internal" href="../advanced/lora.html">Run gpt-2b + LoRA using GptManager / cpp runtime</a></li>
<li class="toctree-l1"><a class="reference internal" href="../advanced/expert-parallelism.html">Expert Parallelism in TensorRT-LLM</a></li>
<li class="toctree-l1"><a class="reference internal" href="../advanced/kv-cache-reuse.html">KV cache reuse</a></li>
<li class="toctree-l1"><a class="reference internal" href="../advanced/speculative-decoding.html">Speculative Sampling</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Performance</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../performance/perf-overview.html">Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="../performance/perf-benchmarking.html">Benchmarking</a></li>
<li class="toctree-l1"><a class="reference internal" href="../performance/perf-best-practices.html">Best Practices</a></li>
<li class="toctree-l1"><a class="reference internal" href="../performance/perf-analysis.html">Performance Analysis</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Reference</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../reference/troubleshooting.html">Troubleshooting</a></li>
<li class="toctree-l1"><a class="reference internal" href="../reference/support-matrix.html">Support Matrix</a></li>
<li class="toctree-l1"><a class="reference internal" href="../reference/precision.html">Numerical Precision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../reference/memory.html">Memory Usage of TensorRT-LLM</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Blogs</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../blogs/H100vsA100.html">H100 has 4.6x A100 Performance in TensorRT-LLM, achieving 10,000 tok/s at 100ms to first token</a></li>
<li class="toctree-l1"><a class="reference internal" href="../blogs/H200launch.html">H200 achieves nearly 12,000 tokens/sec on Llama2-13B with TensorRT-LLM</a></li>
<li class="toctree-l1"><a class="reference internal" href="../blogs/Falcon180B-H200.html">Falcon-180B on a single H200 GPU with INT4 AWQ, and 6.7x faster Llama-70B over A100</a></li>
<li class="toctree-l1"><a class="reference internal" href="../blogs/quantization-in-TRT-LLM.html">Speed up inference with SOTA quantization techniques in TRT-LLM</a></li>
<li class="toctree-l1"><a class="reference internal" href="../blogs/XQA-kernel.html">New XQA-kernel provides 2.4x more Llama-70B throughput within the same latency budget</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">tensorrt_llm</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
<li class="breadcrumb-item active">API Introduction</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/llm-api/index.md.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<section id="api-introduction">
<h1>API Introduction<a class="headerlink" href="#api-introduction" title="Link to this heading"></a></h1>
<p>The LLM API is a high-level Python API and designed for LLM workflows.
This API is under development and might have breaking changes in the future.</p>
<section id="supported-models">
<h2>Supported Models<a class="headerlink" href="#supported-models" title="Link to this heading"></a></h2>
<ul class="simple">
<li><p>Llama (including variants Mistral, Mixtral, InternLM)</p></li>
<li><p>GPT (including variants Starcoder-1/2, Santacoder)</p></li>
<li><p>Gemma-1/2</p></li>
<li><p>Phi-1/2/3</p></li>
<li><p>ChatGLM (including variants glm-10b, chatglm, chatglm2, chatglm3, glm4)</p></li>
<li><p>QWen-1/1.5/2</p></li>
<li><p>Falcon</p></li>
<li><p>Baichuan-1/2</p></li>
<li><p>GPT-J</p></li>
<li><p>Mamba-1/2</p></li>
</ul>
</section>
<section id="model-preparation">
<h2>Model Preparation<a class="headerlink" href="#model-preparation" title="Link to this heading"></a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">LLM</span></code> class supports input from any of following:</p>
<ol class="arabic simple">
<li><p><strong>Hugging Face Hub</strong>: Triggers a download from the Hugging Face model hub, such as <code class="docutils literal notranslate"><span class="pre">TinyLlama/TinyLlama-1.1B-Chat-v1.0</span></code>.</p></li>
<li><p><strong>Local Hugging Face models</strong>: Uses a locally stored Hugging Face model.</p></li>
<li><p><strong>Local TensorRT-LLM engine</strong>: Built by <code class="docutils literal notranslate"><span class="pre">trtllm-build</span></code> tool or saved by the Python LLM API.</p></li>
</ol>
<p>You can use any of these formats interchangeably with the <code class="docutils literal notranslate"><span class="pre">LLM(model=&lt;any-model-path&gt;)</span></code> constructor.
The following sections describe how to use these different formats for the LLM API.</p>
<section id="hugging-face-hub">
<h3>Hugging Face Hub<a class="headerlink" href="#hugging-face-hub" title="Link to this heading"></a></h3>
<p>Using the Hugging Face Hub is as simple as specifying the repo name in the LLM constructor:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">llm</span> <span class="o">=</span> <span class="n">LLM</span><span class="p">(</span><span class="n">model</span><span class="o">=</span><span class="s2">&quot;TinyLlama/TinyLlama-1.1B-Chat-v1.0&quot;</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="local-hugging-face-models">
<h3>Local Hugging Face Models<a class="headerlink" href="#local-hugging-face-models" title="Link to this heading"></a></h3>
<p>Given the popularity of the Hugging Face model hub, the API supports the Hugging Face format as one of the starting points.
To use the API with Llama 3.1 models, download the model from the <a class="reference external" href="https://huggingface.co/meta-llama/Meta-Llama-3.1-8B">Meta Llama 3.1 8B model page</a> by using the following command:</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="go">git lfs install</span>
<span class="go">git clone https://huggingface.co/meta-llama/Meta-Llama-3.1-8B</span>
</pre></div>
</div>
<p>After the model download is complete, you can load the model:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">llm</span> <span class="o">=</span> <span class="n">LLM</span><span class="p">(</span><span class="n">model</span><span class="o">=&lt;</span><span class="n">path_to_meta_llama_from_hf</span><span class="o">&gt;</span><span class="p">)</span>
</pre></div>
</div>
<p>Using this model is subject to a <a class="reference external" href="https://ai.meta.com/resources/models-and-libraries/llama-downloads/">particular</a> license. Agree to the terms and <a class="reference external" href="https://huggingface.co/meta-llama/Meta-Llama-3-8B?clone=true">authenticate with Hugging Face</a> to begin the download.</p>
</section>
<section id="local-tensorrt-llm-engine">
<h3>Local TensorRT-LLM Engine<a class="headerlink" href="#local-tensorrt-llm-engine" title="Link to this heading"></a></h3>
<p>The LLM API can use a TensorRT-LLM engine.
There are two ways to build a TensorRT-LLM engine:</p>
<ol class="arabic">
<li><p>You can build the TensorRT-LLM engine from the Hugging Face model directly with the <a class="reference internal" href="../commands/trtllm-build.html"><span class="std std-doc"><code class="docutils literal notranslate"><span class="pre">trtllm-build</span></code></span></a> tool and then save the engine to disk for later use.
Refer to the <a class="reference external" href="https://github.com/NVIDIA/TensorRT-LLM/tree/main/examples/llama">README</a> in the <a class="reference external" href="https://github.com/NVIDIA/TensorRT-LLM/tree/main/examples/llama"><code class="docutils literal notranslate"><span class="pre">examples/llama</span></code></a> repository on GitHub.</p>
<p>After the engine building is finished, you can load the model:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">llm</span> <span class="o">=</span> <span class="n">LLM</span><span class="p">(</span><span class="n">model</span><span class="o">=&lt;</span><span class="n">path_to_trt_engine</span><span class="o">&gt;</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p>Alternatively, you can use an <code class="docutils literal notranslate"><span class="pre">LLM</span></code> instance to create the engine and persist to local disk:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">llm</span> <span class="o">=</span> <span class="n">LLM</span><span class="p">(</span><span class="o">&lt;</span><span class="n">model</span><span class="o">-</span><span class="n">path</span><span class="o">&gt;</span><span class="p">)</span>
<span class="c1"># Save engine to local disk</span>
<span class="n">llm</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="o">&lt;</span><span class="n">engine</span><span class="o">-</span><span class="nb">dir</span><span class="o">&gt;</span><span class="p">)</span>
</pre></div>
</div>
<p>The engine can be loaded using the <code class="docutils literal notranslate"><span class="pre">model</span></code> argument as shown in the first approach.</p>
</li>
</ol>
</section>
</section>
<section id="tips-and-troubleshooting">
<h2>Tips and Troubleshooting<a class="headerlink" href="#tips-and-troubleshooting" title="Link to this heading"></a></h2>
<p>The following tips typically assist new LLM API users who are familiar with other APIs that are part of TensorRT-LLM:</p>
<ul>
<li><p>RuntimeError: only rank 0 can start multi-node session, got 1</p>
<p>There is no need to add an <code class="docutils literal notranslate"><span class="pre">mpirun</span></code> prefix for launching single node multi-GPU inference with the LLM API.</p>
<p>For example, you can run <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">llm_inference_distributed.py</span></code> to perform multi-GPU on a single node.</p>
</li>
<li><p>Hang issue on Slurm Node</p>
<p>If you experience a hang or other issue on a node managed with Slurm, add prefix <code class="docutils literal notranslate"><span class="pre">mpirun</span> <span class="pre">-n</span> <span class="pre">1</span> <span class="pre">--oversubscribe</span> <span class="pre">--allow-run-as-root</span></code> to your launch script.</p>
<p>For example, try <code class="docutils literal notranslate"><span class="pre">mpirun</span> <span class="pre">-n</span> <span class="pre">1</span> <span class="pre">--oversubscribe</span> <span class="pre">--allow-run-as-root</span> <span class="pre">python</span> <span class="pre">llm_inference_distributed.py</span></code>.</p>
</li>
<li><p>MPI_ABORT was invoked on rank 1 in communicator MPI_COMM_WORLD with errorcode 1.</p>
<p>Because the LLM API relies on the <code class="docutils literal notranslate"><span class="pre">mpi4py</span></code> library, put the LLM class in a function and protect the main entrypoint to the program under the <code class="docutils literal notranslate"><span class="pre">__main__</span></code> namespace to avoid a <a class="reference external" href="https://mpi4py.readthedocs.io/en/stable/mpi4py.futures.html#mpipoolexecutor">recursive spawn</a> process in <code class="docutils literal notranslate"><span class="pre">mpi4py</span></code>.</p>
<p>This limitation is applicable for multi-GPU inference only.</p>
</li>
<li><p>Cannot quit after generation</p>
<p>The LLM instance manages threads and processes, which may prevent its reference count from reaching zero. To address this issue, there are two common solutions:</p>
<ol class="arabic simple">
<li><p>Wrap the LLM instance in a function, as demonstrated in the quickstart guide. This will reduce the reference count and trigger the shutdown process.</p></li>
<li><p>Use LLM as an contextmanager, with the following code: <code class="docutils literal notranslate"><span class="pre">with</span> <span class="pre">LLM(...)</span> <span class="pre">as</span> <span class="pre">llm:</span> <span class="pre">...</span></code>, the shutdown methed will be invoked automatically once it goes out of the <code class="docutils literal notranslate"><span class="pre">with</span></code>-statement block.</p></li>
</ol>
</li>
</ul>
</section>
</section>
</div>
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="../installation/grace-hopper.html" class="btn btn-neutral float-left" title="Installing on Grace Hopper" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="reference.html" class="btn btn-neutral float-right" title="API Reference" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<div role="contentinfo">
<jinja2.runtime.BlockReference object at 0x7fed9b9f93d0>
<div class="footer">
<p>
Copyright © 2024 NVIDIA Corporation
</p>
<p>
<a class="Link" href="https://www.nvidia.com/en-us/about-nvidia/privacy-policy/" target="_blank" rel="noopener"
data-cms-ai="0">Privacy Policy</a> |
<a class="Link" href="https://www.nvidia.com/en-us/about-nvidia/privacy-center/" target="_blank" rel="noopener"
data-cms-ai="0">Manage My Privacy</a> |
<a class="Link" href="https://www.nvidia.com/en-us/preferences/start/" target="_blank" rel="noopener"
data-cms-ai="0">Do Not Sell or Share My Data</a> |
<a class="Link" href="https://www.nvidia.com/en-us/about-nvidia/terms-of-service/" target="_blank"
rel="noopener" data-cms-ai="0">Terms of Service</a> |
<a class="Link" href="https://www.nvidia.com/en-us/about-nvidia/accessibility/" target="_blank" rel="noopener"
data-cms-ai="0">Accessibility</a> |
<a class="Link" href="https://www.nvidia.com/en-us/about-nvidia/company-policies/" target="_blank"
rel="noopener" data-cms-ai="0">Corporate Policies</a> |
<a class="Link" href="https://www.nvidia.com/en-us/product-security/" target="_blank" rel="noopener"
data-cms-ai="0">Product Security</a> |
<a class="Link" href="https://www.nvidia.com/en-us/contact/" target="_blank" rel="noopener"
data-cms-ai="0">Contact</a>
</p>
</div>
</div>
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>