liveBook · Manning

Chapter 7: Generating Python code

What you will learn in chapter 7 of Domain-Specific Small Language Models

The chapter introduces Python code generation as a way to examine specialized open-source LLMs and judge their outputs.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces Python code generation as a way to examine specialized open-source LLMs and judge their outputs.

Key terms: open source models, Python, quality of the outputs

7.1 Using Transformers to generate code

This section explains why coding assistants remain tools for supervised software engineering and why smaller, specialized coding models are promising alternatives to large generalist models.

Key terms: SWE-bench, generalist models, expert human supervision, dedicated models, human oversight, guardrails, data leakage, specialization, coding LLMs, focused data pool

7.2 Generating Python code with a Transformer architecture

7.2.1 Python code generation with CodeGen

This section uses CodeGen 350M mono to demonstrate Python code prompting and to measure how ONNX conversion and 8-bit quantization affect CPU inference.

Key terms: CodeGen, mono, multi, 350M, prompt, function definition, natural-language prompts, inline comment, unit tests, Optimum

7.2.2 Using ONNX with models not supported by Optimum

This section explains how to convert a model to ONNX when Optimum does not support its architecture, before using ONNX Runtime for 8-bit quantization.

Key terms: Optimum, mid-level approach, transformers.onnx, --feature, causal-lm, --model

7.2.3 Model evaluation

This section explains a workflow for evaluating Python code generated by CodeGen: prepare HumanEval prompts, filter invalid Python with an AST, and run tests on valid outputs, while noting robustness and safety considerations.

Key terms: HumanEval, prompt, test, CodeGen tokenizer, ast, abstract syntax tree (AST), valid Python, HumanEval test, passed, execution.py

7.2.4 Python code generation with better models

This section shows how to generate and infill Python code with CodeGen 2.5, introduces StarCoder2 as another code-generation model, and notes their deployment and licensing considerations.

Key terms: CodeGen 2.5, FlashAttention, infill sampling, GPU HBM, SRAM, Apache 2.0, CodeGen 2.5 instruct, mono, multi, CodeGen 2.5 mono

7.3 Coding assistance on commodity hardware

This section shows how to run a quantized code-generation model as a local coding assistant on a modest laptop, and how to prepare a GGUF model when one is not already suitable.

Key terms: GGUF, Transformers, llama.cpp, quantized, llama-cpp-python, Llama, StarCoder v2 3B, 4-bit K_M, context size, GPU layers

7.4 Summary

Concepts explained in this chapter

← Chapter 6: Quantizing for your production environment Chapter 8: Generating protein structures →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.